20
20
import com .google .common .collect .ImmutableMap ;
21
21
import org .junit .Before ;
22
22
import org .junit .Test ;
23
- import org .openqa .selenium .TimeoutException ;
24
23
import org .openqa .selenium .environment .webserver .AppServer ;
25
24
import org .openqa .selenium .environment .webserver .NettyAppServer ;
26
25
import org .openqa .selenium .remote .http .netty .NettyClient ;
33
32
34
33
import static java .net .HttpURLConnection .HTTP_INTERNAL_ERROR ;
35
34
import static java .net .HttpURLConnection .HTTP_OK ;
36
- import static java .net .HttpURLConnection .HTTP_UNAVAILABLE ;
37
35
import static org .assertj .core .api .Assertions .assertThat ;
38
36
import static org .openqa .selenium .remote .http .Contents .asJson ;
39
37
import static org .openqa .selenium .remote .http .HttpMethod .GET ;
@@ -47,7 +45,7 @@ public class RetryRequestTest {
47
45
public void setUp () throws MalformedURLException {
48
46
ClientConfig config = ClientConfig .defaultConfig ()
49
47
.baseUrl (URI .create ("http://localhost:2345" ).toURL ())
50
- .readTimeout (Duration .ofMillis ( 1000 ));
48
+ .readTimeout (Duration .ofSeconds ( 1 ));
51
49
client = new NettyClient .Factory ().createClient (config );
52
50
}
53
51
@@ -77,7 +75,11 @@ public void shouldBeAbleToRetryARequestOnInternalServerError() {
77
75
AtomicInteger count = new AtomicInteger (0 );
78
76
AppServer server = new NettyAppServer (req -> {
79
77
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
+ }
81
83
});
82
84
server .start ();
83
85
@@ -87,7 +89,7 @@ public void shouldBeAbleToRetryARequestOnInternalServerError() {
87
89
String .format (REQUEST_PATH , uri .getHost (), uri .getPort ()));
88
90
HttpResponse response = client .execute (request );
89
91
90
- assertThat (response ).extracting (HttpResponse ::getStatus ).isEqualTo (HTTP_INTERNAL_ERROR );
92
+ assertThat (response ).extracting (HttpResponse ::getStatus ).isEqualTo (HTTP_OK );
91
93
assertThat (count .get ()).isEqualTo (3 );
92
94
93
95
server .stop ();
@@ -121,9 +123,13 @@ public void shouldRetryRequestOnServerUnavailableError() {
121
123
AtomicInteger count = new AtomicInteger (0 );
122
124
AppServer server = new NettyAppServer (req -> {
123
125
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
+ }
127
133
});
128
134
server .start ();
129
135
@@ -132,8 +138,7 @@ public void shouldRetryRequestOnServerUnavailableError() {
132
138
GET ,
133
139
String .format (REQUEST_PATH , uri .getHost (), uri .getPort ()));
134
140
HttpResponse response = client .execute (request );
135
-
136
- assertThat (response ).extracting (HttpResponse ::getStatus ).isEqualTo (HTTP_UNAVAILABLE );
141
+ assertThat (response ).extracting (HttpResponse ::getStatus ).isEqualTo (HTTP_OK );
137
142
assertThat (count .get ()).isEqualTo (3 );
138
143
139
144
server .stop ();
@@ -144,10 +149,12 @@ public void shouldBeAbleToRetryARequestOnTimeout() {
144
149
AtomicInteger count = new AtomicInteger (0 );
145
150
AppServer server = new NettyAppServer (req -> {
146
151
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
+ }
151
158
}
152
159
return new HttpResponse ();
153
160
});
@@ -158,13 +165,11 @@ public void shouldBeAbleToRetryARequestOnTimeout() {
158
165
GET ,
159
166
String .format (REQUEST_PATH , uri .getHost (), uri .getPort ()));
160
167
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 ();
168
173
}
169
174
170
175
@ Test (expected = UncheckedIOException .class )
0 commit comments