Skip to content

Commit d613b44

Browse files
39 timeout config v3 (#51)
* add httpTimeoutInSeconds parameter and tests for it
1 parent 0761a69 commit d613b44

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,14 @@ VisualRegressionTrackerConfig config = new VisualRegressionTrackerConfig(
5656
"develop",
5757

5858
// enableSoftAssert - Log errors instead of exceptions
59-
false
59+
false,
60+
61+
// ciBuildId - id of the build in CI system
62+
"CI_BUILD_ID",
63+
64+
// httpTimeoutInSeconds - define http socket timeout in seconds (default 10s)
65+
15
66+
6067
);
6168
```
6269

src/main/java/io/visual_regression_tracker/sdk_java/VisualRegressionTracker.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44
import java.util.Optional;
5+
import java.util.concurrent.TimeUnit;
56

67
import com.google.gson.Gson;
78
import io.visual_regression_tracker.sdk_java.request.BuildRequest;
@@ -31,7 +32,11 @@ public class VisualRegressionTracker {
3132
public VisualRegressionTracker(VisualRegressionTrackerConfig trackerConfig) {
3233
configuration = trackerConfig;
3334
paths = new PathProvider(trackerConfig.getApiUrl());
34-
client = new OkHttpClient();
35+
client = new OkHttpClient.Builder()
36+
.connectTimeout(configuration.getHttpTimeoutInSeconds(), TimeUnit.SECONDS)
37+
.readTimeout(configuration.getHttpTimeoutInSeconds(), TimeUnit.SECONDS)
38+
.writeTimeout(configuration.getHttpTimeoutInSeconds(), TimeUnit.SECONDS)
39+
.build();
3540
gson = new Gson();
3641
}
3742

src/main/java/io/visual_regression_tracker/sdk_java/VisualRegressionTrackerConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ public class VisualRegressionTrackerConfig {
2424
private Boolean enableSoftAssert = false;
2525
@Builder.Default
2626
private String ciBuildId = null;
27+
@Builder.Default
28+
private int httpTimeoutInSeconds = 10;
2729
}

src/test/java/io/visual_regression_tracker/sdk_java/VisualRegressionTrackerTest.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package io.visual_regression_tracker.sdk_java;
22

33
import java.io.IOException;
4+
import java.net.SocketTimeoutException;
45
import java.util.Arrays;
56
import java.util.Objects;
7+
import java.util.concurrent.TimeUnit;
68

79
import ch.qos.logback.classic.Level;
810
import ch.qos.logback.classic.Logger;
@@ -46,6 +48,7 @@ public class VisualRegressionTrackerTest {
4648
private final static String CI_BUILD_ID = "123456789";
4749
private final static String NAME = "Test name";
4850
private final static String IMAGE_BASE_64 = "image";
51+
private final static int HTTP_TIMOUT = 1;
4952

5053
private final Gson gson = new Gson();
5154

@@ -67,7 +70,8 @@ public void setup() {
6770
"XHGDZDFD3GMJDNM87JKEMP0JS1G5",
6871
"develop",
6972
false,
70-
CI_BUILD_ID);
73+
CI_BUILD_ID,
74+
HTTP_TIMOUT);
7175
vrt = new VisualRegressionTracker(config);
7276
vrtMocked = mock(VisualRegressionTracker.class);
7377
vrtMocked.paths = new PathProvider("baseApiUrl");
@@ -339,4 +343,44 @@ public void handleRequestShouldThrowIfNotSuccess() throws IOException {
339343

340344
assertThat(exceptionMessage, is(error));
341345
}
346+
347+
@Test
348+
public void httpTimoutWorks() throws IOException, InterruptedException {
349+
BuildResponse buildResponse = BuildResponse.builder()
350+
.id(BUILD_ID)
351+
.projectId(PROJECT_ID)
352+
.ciBuildId(CI_BUILD_ID)
353+
.build();
354+
String json = gson.toJson(buildResponse);
355+
//body size is 97 bytes, http timeout is 1s, set body read delay to 0.9s, wait that vrt get all values correctly
356+
server.enqueue(new MockResponse().throttleBody(json.length(), HTTP_TIMOUT * 1000 - 100, TimeUnit.MILLISECONDS)
357+
.setResponseCode(200)
358+
.setBody(json));
359+
360+
vrt.start();
361+
362+
server.takeRequest();
363+
364+
assertThat(vrt.buildId, is(BUILD_ID));
365+
assertThat(vrt.projectId, is(PROJECT_ID));
366+
}
367+
368+
@Test(expectedExceptions = SocketTimeoutException.class,
369+
expectedExceptionsMessageRegExp = "^(timeout|Read timed out)$")
370+
public void httpTimoutElapsed() throws IOException, InterruptedException {
371+
BuildResponse buildResponse = BuildResponse.builder()
372+
.id(BUILD_ID)
373+
.projectId(PROJECT_ID)
374+
.ciBuildId(CI_BUILD_ID)
375+
.build();
376+
String json = gson.toJson(buildResponse);
377+
//body size is 97 bytes, http timeout is 1s, set body read delay to 1s, wait for error
378+
server.enqueue(new MockResponse().throttleBody(json.length(), HTTP_TIMOUT, TimeUnit.SECONDS)
379+
.setResponseCode(200)
380+
.setBody(json));
381+
382+
vrt.start();
383+
384+
server.takeRequest();
385+
}
342386
}

0 commit comments

Comments
 (0)