private static final Logger LOG = LoggerFactory.getLogger(AnalysisMode.class);
+ private static final int DEFAULT_PREVIEW_READ_TIMEOUT_SEC = 60;
+
private boolean preview;
private boolean incremental;
+ private int previewReadTimeoutSec;
public AnalysisMode(BootstrapSettings bootstrapSettings) {
init(bootstrapSettings);
// To stay compatible with plugins that use the old property to check mode
if (incremental || preview) {
bootstrapSettings.properties().put(CoreProperties.DRY_RUN, "true");
+ previewReadTimeoutSec = loadPreviewReadTimeout(bootstrapSettings);
+ }
+ }
+
+ // SONAR-4488 Allow to increase preview read timeout
+ private int loadPreviewReadTimeout(BootstrapSettings bootstrapSettings) {
+ int readTimeoutSec;
+ if (bootstrapSettings.property(CoreProperties.DRY_RUN_READ_TIMEOUT_SEC) != null) {
+ LOG.warn("Property {} is deprecated. Please use {} instead.", CoreProperties.DRY_RUN_READ_TIMEOUT_SEC, CoreProperties.PREVIEW_READ_TIMEOUT_SEC);
+ readTimeoutSec = Integer.parseInt(bootstrapSettings.property(CoreProperties.DRY_RUN_READ_TIMEOUT_SEC));
+ } else if (bootstrapSettings.property(CoreProperties.PREVIEW_READ_TIMEOUT_SEC) != null) {
+ readTimeoutSec = Integer.parseInt(bootstrapSettings.property(CoreProperties.PREVIEW_READ_TIMEOUT_SEC));
+ } else {
+ readTimeoutSec = DEFAULT_PREVIEW_READ_TIMEOUT_SEC;
}
+ return readTimeoutSec;
+ }
+
+ /**
+ * Read timeout used by HTTP request done in preview mode (SONAR-4488, SONAR-5028)
+ */
+ public int getPreviewReadTimeoutSec() {
+ return previewReadTimeoutSec;
}
+
}
private static final String USER = "sonar";
private static final String PASSWORD = USER;
- private static final int DEFAULT_PREVIEW_READ_TIMEOUT_SEC = 60;
-
private final Settings settings;
private final ServerClient server;
private final TempFolder tempUtils;
if (mode.isPreview()) {
File databaseFile = tempUtils.newFile("preview", ".h2.db");
- int readTimeoutSec = getReadTimeout();
+ int readTimeoutSec = mode.getPreviewReadTimeoutSec();
downloadDatabase(databaseFile, readTimeoutSec * 1000);
String databasePath = StringUtils.removeEnd(databaseFile.getAbsolutePath(), ".h2.db");
}
}
- // SONAR-4488 Allow to increase dryRun timeout
- private int getReadTimeout() {
- int readTimeoutSec;
- if (settings.hasKey(CoreProperties.DRY_RUN_READ_TIMEOUT_SEC)) {
- LOG.warn("Property {} is deprecated. Please use {} instead.", CoreProperties.DRY_RUN_READ_TIMEOUT_SEC, CoreProperties.PREVIEW_READ_TIMEOUT_SEC);
- readTimeoutSec = settings.getInt(CoreProperties.DRY_RUN_READ_TIMEOUT_SEC);
- } else if (settings.hasKey(CoreProperties.PREVIEW_READ_TIMEOUT_SEC)) {
- readTimeoutSec = settings.getInt(CoreProperties.PREVIEW_READ_TIMEOUT_SEC);
- } else {
- readTimeoutSec = DEFAULT_PREVIEW_READ_TIMEOUT_SEC;
- }
- return readTimeoutSec;
- }
-
- private void downloadDatabase(File toFile, int readTimeout) {
+ private void downloadDatabase(File toFile, int readTimeoutMillis) {
String projectKey = null;
try {
projectKey = settings.getString(CoreProperties.PROJECT_KEY_PROPERTY);
projectKey = String.format("%s:%s", projectKey, branch);
}
if (StringUtils.isBlank(projectKey)) {
- server.download("/batch_bootstrap/db", toFile, readTimeout);
+ server.download("/batch_bootstrap/db", toFile, readTimeoutMillis);
} else {
- server.download("/batch_bootstrap/db?project=" + projectKey, toFile, readTimeout);
+ server.download("/batch_bootstrap/db?project=" + projectKey, toFile, readTimeoutMillis);
}
LOG.debug("Dry Run database size: {}", FileUtils.byteCountToDisplaySize(FileUtils.sizeOf(toFile)));
} catch (SonarException e) {
- handleException(readTimeout, projectKey, e);
+ handleException(readTimeoutMillis, projectKey, e);
throw e;
}
}
private String loadSourceFromWs(Resource resource) {
try {
- return server.request("/api/sources?resource=" + resource.getEffectiveKey() + "&format=txt", false);
+ return server.request("/api/sources?resource=" + resource.getEffectiveKey() + "&format=txt", false, analysisMode.getPreviewReadTimeoutSec() * 1000);
} catch (HttpDownloader.HttpException he) {
if (he.getResponseCode() == 404) {
return "";
assertThat(mode.isPreview()).isTrue();
assertThat(mode.isIncremental()).isFalse();
}
+
+ @Test
+ public void should_get_default_preview_read_timeout() {
+ bootstrapSettings.properties().put(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_PREVIEW);
+ AnalysisMode mode = new AnalysisMode(bootstrapSettings);
+
+ assertThat(mode.getPreviewReadTimeoutSec()).isEqualTo(60);
+ }
+
+ @Test
+ public void should_download_database_with_deprecated_overriden_timeout() {
+ bootstrapSettings.properties().put(CoreProperties.DRY_RUN, "true");
+ bootstrapSettings.properties().put(CoreProperties.DRY_RUN_READ_TIMEOUT_SEC, "80");
+ AnalysisMode mode = new AnalysisMode(bootstrapSettings);
+
+ assertThat(mode.getPreviewReadTimeoutSec()).isEqualTo(80);
+ }
+
+ @Test
+ public void should_download_database_with_overriden_timeout() {
+ bootstrapSettings.properties().put(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_PREVIEW);
+ bootstrapSettings.properties().put(CoreProperties.PREVIEW_READ_TIMEOUT_SEC, "80");
+ AnalysisMode mode = new AnalysisMode(bootstrapSettings);
+
+ assertThat(mode.getPreviewReadTimeoutSec()).isEqualTo(80);
+ }
}
mode = mock(AnalysisMode.class);
when(mode.isPreview()).thenReturn(true);
+ when(mode.getPreviewReadTimeoutSec()).thenReturn(60);
}
@Test
verify(server).download("/batch_bootstrap/db?project=group:project", databaseFile, 60000);
}
- @Test
- public void should_download_database_with_deprecated_overriden_timeout() {
- settings.setProperty(CoreProperties.DRY_RUN_READ_TIMEOUT_SEC, 80);
- new PreviewDatabase(settings, server, tempUtils, mode).start();
-
- verify(server).download("/batch_bootstrap/db?project=group:project", databaseFile, 80000);
- }
-
- @Test
- public void should_download_database_with_overriden_timeout() {
- settings.setProperty(CoreProperties.PREVIEW_READ_TIMEOUT_SEC, 80);
- new PreviewDatabase(settings, server, tempUtils, mode).start();
-
- verify(server).download("/batch_bootstrap/db?project=group:project", databaseFile, 80000);
- }
-
@Test
public void should_download_database_on_branch() {
settings.setProperty(CoreProperties.PROJECT_BRANCH_PROPERTY, "mybranch");
@Before
public void before() {
mode = mock(AnalysisMode.class);
+ when(mode.getPreviewReadTimeoutSec()).thenReturn(30);
}
@Test
public void should_download_source_from_ws_if_preview_mode() {
setupData("last_snapshot");
ServerClient server = mock(ServerClient.class);
- when(server.request(anyString(), eq(false))).thenReturn("downloaded source of Bar.c");
+ when(server.request(anyString(), eq(false), eq(30 * 1000))).thenReturn("downloaded source of Bar.c");
when(mode.isPreview()).thenReturn(true);
LastSnapshots lastSnapshots = new LastSnapshots(mode, getSession(), server);
String source = lastSnapshots.getSource(newFile());
assertThat(source).isEqualTo("downloaded source of Bar.c");
- verify(server).request("/api/sources?resource=myproject:org/foo/Bar.c&format=txt", false);
+ verify(server).request("/api/sources?resource=myproject:org/foo/Bar.c&format=txt", false, 30 * 1000);
}
@Test
public void should_fail_to_download_source_from_ws() throws URISyntaxException {
setupData("last_snapshot");
ServerClient server = mock(ServerClient.class);
- when(server.request(anyString(), eq(false))).thenThrow(new HttpDownloader.HttpException(new URI(""), 500));
+ when(server.request(anyString(), eq(false), eq(30 * 1000))).thenThrow(new HttpDownloader.HttpException(new URI(""), 500));
when(mode.isPreview()).thenReturn(true);
LastSnapshots lastSnapshots = new LastSnapshots(mode, getSession(), server);
public void should_return_empty_source_if_preview_mode_and_no_last_snapshot() throws URISyntaxException {
setupData("last_snapshot");
ServerClient server = mock(ServerClient.class);
- when(server.request(anyString(), eq(false))).thenThrow(new HttpDownloader.HttpException(new URI(""), 404));
+ when(server.request(anyString(), eq(false), eq(30 * 1000))).thenThrow(new HttpDownloader.HttpException(new URI(""), 404));
when(mode.isPreview()).thenReturn(true);
LastSnapshots lastSnapshots = new LastSnapshots(mode, getSession(), server);
String source = lastSnapshots.getSource(newFile());
assertThat(source).isEqualTo("");
- verify(server).request("/api/sources?resource=myproject:org/foo/Bar.c&format=txt", false);
+ verify(server).request("/api/sources?resource=myproject:org/foo/Bar.c&format=txt", false, 30 * 1000);
}
@Test