diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2013-07-11 12:22:50 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2013-07-11 12:57:52 +0200 |
commit | 195d307840e5e4aaf6d64fab2227b5d8230108b3 (patch) | |
tree | dd28f7024e1c0985952415ade1ef3f01b8983342 /sonar-plugin-api/src | |
parent | ac46595adb827cbc615fa6cb2702d00f7bf41937 (diff) | |
download | sonarqube-195d307840e5e4aaf6d64fab2227b5d8230108b3.tar.gz sonarqube-195d307840e5e4aaf6d64fab2227b5d8230108b3.zip |
SONAR-4488 Increase timeout for /batch_bootstrap/db
Diffstat (limited to 'sonar-plugin-api/src')
3 files changed, 85 insertions, 9 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java b/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java index 1d0f2e8460a..e0f25637bcf 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java @@ -400,4 +400,9 @@ public interface CoreProperties { */ @Deprecated String CORE_COVERAGE_PLUGIN_PROPERTY = "sonar.core.codeCoveragePlugin"; + + /** + * @since 3.7 + */ + String DRY_RUN_READ_TIMEOUT = "sonar.dryRun.readTimeout"; } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java index cb64b82be95..deaf6d1fed0 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java @@ -37,6 +37,8 @@ import org.sonar.api.ServerComponent; import org.sonar.api.config.Settings; import org.sonar.api.platform.Server; +import javax.annotation.Nullable; + import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -59,12 +61,23 @@ public class HttpDownloader extends UriReader.SchemeProcessor implements BatchCo public static final int TIMEOUT_MILLISECONDS = 20 * 1000; private final BaseHttpDownloader downloader; + private final Integer readTimeout; public HttpDownloader(Server server, Settings settings) { + this(server, settings, null); + } + + public HttpDownloader(Server server, Settings settings, @Nullable Integer readTimeout) { + this.readTimeout = readTimeout; downloader = new BaseHttpDownloader(settings.getProperties(), server.getVersion()); } public HttpDownloader(Settings settings) { + this(settings, null); + } + + public HttpDownloader(Settings settings, @Nullable Integer readTimeout) { + this.readTimeout = readTimeout; downloader = new BaseHttpDownloader(settings.getProperties(), null); } @@ -86,7 +99,7 @@ public class HttpDownloader extends UriReader.SchemeProcessor implements BatchCo @Override String readString(URI uri, Charset charset) { try { - return CharStreams.toString(CharStreams.newReaderSupplier(downloader.newInputSupplier(uri), charset)); + return CharStreams.toString(CharStreams.newReaderSupplier(downloader.newInputSupplier(uri, this.readTimeout), charset)); } catch (IOException e) { throw failToDownload(uri, e); } @@ -98,7 +111,7 @@ public class HttpDownloader extends UriReader.SchemeProcessor implements BatchCo public byte[] download(URI uri) { try { - return ByteStreams.toByteArray(downloader.newInputSupplier(uri)); + return ByteStreams.toByteArray(downloader.newInputSupplier(uri, this.readTimeout)); } catch (IOException e) { throw failToDownload(uri, e); } @@ -110,7 +123,7 @@ public class HttpDownloader extends UriReader.SchemeProcessor implements BatchCo public InputStream openStream(URI uri) { try { - return downloader.newInputSupplier(uri).getInput(); + return downloader.newInputSupplier(uri, this.readTimeout).getInput(); } catch (IOException e) { throw failToDownload(uri, e); } @@ -118,7 +131,7 @@ public class HttpDownloader extends UriReader.SchemeProcessor implements BatchCo public void download(URI uri, File toFile) { try { - Files.copy(downloader.newInputSupplier(uri), toFile); + Files.copy(downloader.newInputSupplier(uri, this.readTimeout), toFile); } catch (IOException e) { FileUtils.deleteQuietly(toFile); throw failToDownload(uri, e); @@ -193,11 +206,25 @@ public class HttpDownloader extends UriReader.SchemeProcessor implements BatchCo } public InputSupplier<InputStream> newInputSupplier(URI uri) { - return new HttpInputSupplier(uri, userAgent, null, null); + return new HttpInputSupplier(uri, userAgent, null, null, TIMEOUT_MILLISECONDS); + } + + public InputSupplier<InputStream> newInputSupplier(URI uri, @Nullable Integer readTimeoutMillis) { + if (readTimeoutMillis != null) { + return new HttpInputSupplier(uri, userAgent, null, null, readTimeoutMillis); + } + return new HttpInputSupplier(uri, userAgent, null, null, TIMEOUT_MILLISECONDS); } public InputSupplier<InputStream> newInputSupplier(URI uri, String login, String password) { - return new HttpInputSupplier(uri, userAgent, login, password); + return new HttpInputSupplier(uri, userAgent, login, password, TIMEOUT_MILLISECONDS); + } + + public InputSupplier<InputStream> newInputSupplier(URI uri, String login, String password, @Nullable Integer readTimeoutMillis) { + if (readTimeoutMillis != null) { + return new HttpInputSupplier(uri, userAgent, login, password, readTimeoutMillis); + } + return new HttpInputSupplier(uri, userAgent, login, password, TIMEOUT_MILLISECONDS); } private static class HttpInputSupplier implements InputSupplier<InputStream> { @@ -205,12 +232,14 @@ public class HttpDownloader extends UriReader.SchemeProcessor implements BatchCo private final String password; private final URI uri; private final String userAgent; + private final int readTimeoutMillis; - HttpInputSupplier(URI uri, String userAgent, String login, String password) { + HttpInputSupplier(URI uri, String userAgent, String login, String password, int readTimeoutMillis) { this.uri = uri; this.userAgent = userAgent; this.login = login; this.password = password; + this.readTimeoutMillis = readTimeoutMillis; } public InputStream getInput() throws IOException { @@ -222,7 +251,7 @@ public class HttpDownloader extends UriReader.SchemeProcessor implements BatchCo connection.setRequestProperty("Authorization", "Basic " + encoded); } connection.setConnectTimeout(TIMEOUT_MILLISECONDS); - connection.setReadTimeout(TIMEOUT_MILLISECONDS); + connection.setReadTimeout(readTimeoutMillis); connection.setUseCaches(true); connection.setInstanceFollowRedirects(true); connection.setRequestProperty("User-Agent", userAgent); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java index 1074b651a07..7a752ab5264 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java @@ -20,10 +20,13 @@ package org.sonar.api.utils; import com.google.common.base.Charsets; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.simpleframework.http.Request; import org.simpleframework.http.Response; @@ -39,6 +42,7 @@ import java.net.InetSocketAddress; import java.net.Proxy; import java.net.ProxySelector; import java.net.SocketAddress; +import java.net.SocketTimeoutException; import java.net.URI; import java.net.URISyntaxException; import java.util.Arrays; @@ -54,6 +58,9 @@ public class HttpDownloaderTest { @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + private static SocketConnection socketConnection; private static String baseUrl; @@ -65,7 +72,15 @@ public class HttpDownloaderTest { if (req.getPath().getPath().contains("/redirect/")) { resp.setCode(303); resp.add("Location", "/"); - } else { + } + else { + if (req.getPath().getPath().contains("/timeout/")) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } resp.getPrintStream().append("agent=" + req.getValues("User-Agent").get(0)); } } catch (IOException e) { @@ -101,6 +116,33 @@ public class HttpDownloaderTest { assertThat(text.length()).isGreaterThan(10); } + @Test + public void readStringWithDefaultTimeout() throws URISyntaxException { + String text = new HttpDownloader(new Settings()).readString(new URI(baseUrl + "/timeout/"), Charsets.UTF_8); + assertThat(text.length()).isGreaterThan(10); + } + + @Test + public void readStringWithTimeout() throws URISyntaxException { + String text = new HttpDownloader(new Settings(), 2000).readString(new URI(baseUrl + "/timeout/"), Charsets.UTF_8); + assertThat(text.length()).isGreaterThan(10); + + thrown.expect(new BaseMatcher<Exception>() { + @Override + public boolean matches(Object ex) { + // TODO Auto-generated method stub + return ex instanceof SonarException && ((SonarException) ex).getCause() instanceof SocketTimeoutException; + } + + @Override + public void describeTo(Description arg0) { + // TODO Auto-generated method stub + + } + }); + new HttpDownloader(new Settings(), 100).readString(new URI(baseUrl + "/timeout/"), Charsets.UTF_8); + } + @Test(expected = SonarException.class) public void failIfServerDown() throws URISyntaxException { // I hope that the port 1 is not used ! |