2020import com .google .common .collect .ImmutableMap ;
2121import org .junit .Before ;
2222import org .junit .Test ;
23- import org .openqa .selenium .TimeoutException ;
2423import org .openqa .selenium .environment .webserver .AppServer ;
2524import org .openqa .selenium .environment .webserver .NettyAppServer ;
2625import org .openqa .selenium .remote .http .netty .NettyClient ;
3332
3433import static java .net .HttpURLConnection .HTTP_INTERNAL_ERROR ;
3534import static java .net .HttpURLConnection .HTTP_OK ;
36- import static java .net .HttpURLConnection .HTTP_UNAVAILABLE ;
3735import static org .assertj .core .api .Assertions .assertThat ;
3836import static org .openqa .selenium .remote .http .Contents .asJson ;
3937import static org .openqa .selenium .remote .http .HttpMethod .GET ;
@@ -47,7 +45,7 @@ public class RetryRequestTest {
4745 public void setUp () throws MalformedURLException {
4846 ClientConfig config = ClientConfig .defaultConfig ()
4947 .baseUrl (URI .create ("http://localhost:2345" ).toURL ())
50- .readTimeout (Duration .ofMillis ( 1000 ));
48+ .readTimeout (Duration .ofSeconds ( 1 ));
5149 client = new NettyClient .Factory ().createClient (config );
5250 }
5351
@@ -77,7 +75,11 @@ public void shouldBeAbleToRetryARequestOnInternalServerError() {
7775 AtomicInteger count = new AtomicInteger (0 );
7876 AppServer server = new NettyAppServer (req -> {
7977 count .incrementAndGet ();
80- return new HttpResponse ().setStatus (500 );
78+ if (count .get () <= 2 ) {
79+ return new HttpResponse ().setStatus (500 );
80+ } else {
81+ return new HttpResponse ();
82+ }
8183 });
8284 server .start ();
8385
@@ -87,7 +89,7 @@ public void shouldBeAbleToRetryARequestOnInternalServerError() {
8789 String .format (REQUEST_PATH , uri .getHost (), uri .getPort ()));
8890 HttpResponse response = client .execute (request );
8991
90- assertThat (response ).extracting (HttpResponse ::getStatus ).isEqualTo (HTTP_INTERNAL_ERROR );
92+ assertThat (response ).extracting (HttpResponse ::getStatus ).isEqualTo (HTTP_OK );
9193 assertThat (count .get ()).isEqualTo (3 );
9294
9395 server .stop ();
@@ -121,9 +123,13 @@ public void shouldRetryRequestOnServerUnavailableError() {
121123 AtomicInteger count = new AtomicInteger (0 );
122124 AppServer server = new NettyAppServer (req -> {
123125 count .incrementAndGet ();
124- return new HttpResponse ()
125- .setStatus (503 )
126- .setContent (asJson (ImmutableMap .of ("error" , "server down" )));
126+ if (count .get () <= 2 ) {
127+ return new HttpResponse ()
128+ .setStatus (503 )
129+ .setContent (asJson (ImmutableMap .of ("error" , "server down" )));
130+ } else {
131+ return new HttpResponse ();
132+ }
127133 });
128134 server .start ();
129135
@@ -132,8 +138,7 @@ public void shouldRetryRequestOnServerUnavailableError() {
132138 GET ,
133139 String .format (REQUEST_PATH , uri .getHost (), uri .getPort ()));
134140 HttpResponse response = client .execute (request );
135-
136- assertThat (response ).extracting (HttpResponse ::getStatus ).isEqualTo (HTTP_UNAVAILABLE );
141+ assertThat (response ).extracting (HttpResponse ::getStatus ).isEqualTo (HTTP_OK );
137142 assertThat (count .get ()).isEqualTo (3 );
138143
139144 server .stop ();
@@ -144,10 +149,12 @@ public void shouldBeAbleToRetryARequestOnTimeout() {
144149 AtomicInteger count = new AtomicInteger (0 );
145150 AppServer server = new NettyAppServer (req -> {
146151 count .incrementAndGet ();
147- try {
148- Thread .sleep (2000 );
149- } catch (InterruptedException e ) {
150- e .printStackTrace ();
152+ if (count .get () <= 3 ) {
153+ try {
154+ Thread .sleep (2000 );
155+ } catch (InterruptedException e ) {
156+ e .printStackTrace ();
157+ }
151158 }
152159 return new HttpResponse ();
153160 });
@@ -158,13 +165,11 @@ public void shouldBeAbleToRetryARequestOnTimeout() {
158165 GET ,
159166 String .format (REQUEST_PATH , uri .getHost (), uri .getPort ()));
160167
161- try {
162- client .execute (request );
163- } catch (TimeoutException e ) {
164- assertThat (count .get ()).isEqualTo (4 );
165- } finally {
166- server .stop ();
167- }
168+ HttpResponse response = client .execute (request );
169+ assertThat (response ).extracting (HttpResponse ::getStatus ).isEqualTo (HTTP_OK );
170+ assertThat (count .get ()).isEqualTo (4 );
171+
172+ server .stop ();
168173 }
169174
170175 @ Test (expected = UncheckedIOException .class )
0 commit comments