11package io .visual_regression_tracker .sdk_java ;
22
33import java .io .IOException ;
4+ import java .net .SocketTimeoutException ;
45import java .util .Arrays ;
56import java .util .Objects ;
7+ import java .util .concurrent .TimeUnit ;
68
79import ch .qos .logback .classic .Level ;
810import 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