aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-ws
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2021-02-26 12:06:31 -0600
committersonartech <sonartech@sonarsource.com>2021-03-03 20:12:50 +0000
commita3ab233a6c989f6bea8eab0f859282bb4246970f (patch)
treef5a7ca25a868712295cf1f348c88772ede90d648 /sonar-ws
parent9ac1ecab6661c86069e5e63e8cb31a885022e91a (diff)
downloadsonarqube-a3ab233a6c989f6bea8eab0f859282bb4246970f.tar.gz
sonarqube-a3ab233a6c989f6bea8eab0f859282bb4246970f.zip
SONAR-14527 Use 30s write timeout when submitting scanner report
Diffstat (limited to 'sonar-ws')
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/BaseRequest.java13
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java17
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/WsRequest.java2
-rw-r--r--sonar-ws/src/test/java/org/sonarqube/ws/client/BaseRequestTest.java7
-rw-r--r--sonar-ws/src/test/java/org/sonarqube/ws/client/HttpConnectorTest.java10
5 files changed, 42 insertions, 7 deletions
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/BaseRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/BaseRequest.java
index a048ddccac5..c81c0604ead 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/BaseRequest.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/BaseRequest.java
@@ -50,7 +50,8 @@ abstract class BaseRequest<SELF extends BaseRequest> implements WsRequest {
private final DefaultParameters parameters = new DefaultParameters();
private final DefaultHeaders headers = new DefaultHeaders();
private OptionalInt timeOutInMs = OptionalInt.empty();
-
+ private OptionalInt writeTimeOutInMs = OptionalInt.empty();
+
BaseRequest(String path) {
this.path = path;
}
@@ -75,6 +76,16 @@ abstract class BaseRequest<SELF extends BaseRequest> implements WsRequest {
return (SELF) this;
}
+ @Override
+ public OptionalInt getWriteTimeOutInMs() {
+ return writeTimeOutInMs;
+ }
+
+ public SELF setWriteTimeOutInMs(int writeTimeOutInMs) {
+ this.writeTimeOutInMs = OptionalInt.of(writeTimeOutInMs);
+ return (SELF) this;
+ }
+
/**
* Expected media type of response. Default is {@link MediaTypes#JSON}.
*/
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java
index db02624c0d1..d2ef094719d 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java
@@ -164,14 +164,19 @@ public class HttpConnector implements WsConnector {
.newBuilder();
}
- private static OkHttpClient prepareOkHttpClient(OkHttpClient okHttpClient, WsRequest wsRequest) {
- if (!wsRequest.getTimeOutInMs().isPresent()) {
+ static OkHttpClient prepareOkHttpClient(OkHttpClient okHttpClient, WsRequest wsRequest) {
+ if (!wsRequest.getTimeOutInMs().isPresent() && !wsRequest.getWriteTimeOutInMs().isPresent()) {
return okHttpClient;
}
+ OkHttpClient.Builder builder = okHttpClient.newBuilder();
+ if (wsRequest.getTimeOutInMs().isPresent()) {
+ builder.readTimeout(wsRequest.getTimeOutInMs().getAsInt(), TimeUnit.MILLISECONDS);
+ }
+ if (wsRequest.getWriteTimeOutInMs().isPresent()) {
+ builder.writeTimeout(wsRequest.getWriteTimeOutInMs().getAsInt(), TimeUnit.MILLISECONDS);
+ }
- return okHttpClient.newBuilder()
- .readTimeout(wsRequest.getTimeOutInMs().getAsInt(), TimeUnit.MILLISECONDS)
- .build();
+ return builder.build();
}
private static void completeUrlQueryParameters(BaseRequest<?> request, HttpUrl.Builder urlBuilder) {
@@ -197,7 +202,7 @@ public class HttpConnector implements WsConnector {
try {
return call.execute();
} catch (IOException e) {
- throw new IllegalStateException("Fail to request " + okRequest.url(), e);
+ throw new IllegalStateException("Fail to request url: " + okRequest.url(), e);
}
}
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/WsRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/WsRequest.java
index b7094817086..474f876a020 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/WsRequest.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/WsRequest.java
@@ -35,6 +35,8 @@ public interface WsRequest {
OptionalInt getTimeOutInMs();
+ OptionalInt getWriteTimeOutInMs();
+
/**
*
* In case of multi value parameters, returns the first value
diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/BaseRequestTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/BaseRequestTest.java
index 5e1a0f99d6a..01cf250ef94 100644
--- a/sonar-ws/src/test/java/org/sonarqube/ws/client/BaseRequestTest.java
+++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/BaseRequestTest.java
@@ -45,6 +45,13 @@ public class BaseRequestTest {
assertThat(underTest.getParams()).isEmpty();
assertThat(underTest.getMediaType()).isEqualTo(MediaTypes.JSON);
assertThat(underTest.getPath()).isEqualTo("api/foo");
+ assertThat(underTest.getWriteTimeOutInMs()).isEmpty();
+ }
+
+ @Test
+ public void set_write_timeout() {
+ underTest.setWriteTimeOutInMs(30_000);
+ assertThat(underTest.getWriteTimeOutInMs()).hasValue(30_000);
}
@Test
diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpConnectorTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpConnectorTest.java
index 97f6f9b2ad1..b95f9a8afb5 100644
--- a/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpConnectorTest.java
+++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpConnectorTest.java
@@ -30,6 +30,7 @@ import java.util.Random;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLSocketFactory;
import okhttp3.ConnectionSpec;
+import okhttp3.OkHttpClient;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
@@ -290,6 +291,15 @@ public class HttpConnectorTest {
}
@Test
+ public void override_timeouts_with_request() {
+ OkHttpClient client = new OkHttpClient.Builder().build();
+ WsRequest request = new PostRequest("abc").setWriteTimeOutInMs(123).setTimeOutInMs(234);
+ client = underTest.prepareOkHttpClient(client, request);
+ assertThat(client.writeTimeoutMillis()).isEqualTo(123);
+ assertThat(client.readTimeoutMillis()).isEqualTo(234);
+ }
+
+ @Test
public void send_user_agent() throws Exception {
answerHelloWorld();
underTest = HttpConnector.newBuilder()