From eebd168bb5ee832190e9244dac03c14361511f2c Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Mon, 24 Mar 2014 12:12:25 +0100 Subject: [PATCH] SONAR-5028 Reuse property sonar.preview.readTimeout when downloading file sources in preview --- .../sonar/batch/bootstrap/AnalysisMode.java | 26 +++++++++++++++++++ .../batch/bootstrap/PreviewDatabase.java | 26 ++++--------------- .../org/sonar/batch/scan/LastSnapshots.java | 2 +- .../batch/bootstrap/AnalysisModeTest.java | 26 +++++++++++++++++++ .../batch/bootstrap/PreviewDatabaseTest.java | 17 +----------- .../sonar/batch/scan/LastSnapshotsTest.java | 11 ++++---- 6 files changed, 65 insertions(+), 43 deletions(-) diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/AnalysisMode.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/AnalysisMode.java index 69659133b10..b524b5b6acf 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/AnalysisMode.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/AnalysisMode.java @@ -33,8 +33,11 @@ public class AnalysisMode implements BatchComponent { 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); @@ -66,6 +69,29 @@ public class AnalysisMode implements BatchComponent { // 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; } + } diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/PreviewDatabase.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/PreviewDatabase.java index fcc28c62bb5..ae01712f712 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/PreviewDatabase.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/PreviewDatabase.java @@ -47,8 +47,6 @@ public class PreviewDatabase implements BatchComponent { 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; @@ -65,7 +63,7 @@ public class PreviewDatabase implements BatchComponent { 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"); @@ -73,21 +71,7 @@ public class PreviewDatabase implements BatchComponent { } } - // 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); @@ -96,13 +80,13 @@ public class PreviewDatabase implements BatchComponent { 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; } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/LastSnapshots.java b/sonar-batch/src/main/java/org/sonar/batch/scan/LastSnapshots.java index fd9b173c3ee..b36d4dc6852 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/LastSnapshots.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/LastSnapshots.java @@ -58,7 +58,7 @@ public class LastSnapshots implements BatchComponent { 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 ""; diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/AnalysisModeTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/AnalysisModeTest.java index 40b0d19ef8d..4370ffaf8b9 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/AnalysisModeTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/AnalysisModeTest.java @@ -89,4 +89,30 @@ public class AnalysisModeTest { 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); + } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/PreviewDatabaseTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/PreviewDatabaseTest.java index ae4eeea9c17..3140d9dd298 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/PreviewDatabaseTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/PreviewDatabaseTest.java @@ -64,6 +64,7 @@ public class PreviewDatabaseTest { mode = mock(AnalysisMode.class); when(mode.isPreview()).thenReturn(true); + when(mode.getPreviewReadTimeoutSec()).thenReturn(60); } @Test @@ -81,22 +82,6 @@ public class PreviewDatabaseTest { 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"); diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/LastSnapshotsTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/LastSnapshotsTest.java index e5dd32a8301..d6cddb4447d 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/LastSnapshotsTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/LastSnapshotsTest.java @@ -50,6 +50,7 @@ public class LastSnapshotsTest extends AbstractDbUnitTestCase { @Before public void before() { mode = mock(AnalysisMode.class); + when(mode.getPreviewReadTimeoutSec()).thenReturn(30); } @Test @@ -78,21 +79,21 @@ public class LastSnapshotsTest extends AbstractDbUnitTestCase { 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); @@ -105,14 +106,14 @@ public class LastSnapshotsTest extends AbstractDbUnitTestCase { 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 -- 2.39.5