14
14
15
15
import io .netty .handler .codec .http .DefaultHttpHeaders ;
16
16
import io .netty .handler .codec .http .EmptyHttpHeaders ;
17
+ import lombok .extern .slf4j .Slf4j ;
17
18
import lombok .val ;
18
19
import okhttp3 .MediaType ;
19
20
import okhttp3 .Request ;
20
21
import okhttp3 .RequestBody ;
21
22
import org .asynchttpclient .AsyncCompletionHandler ;
22
23
import org .asynchttpclient .AsyncHttpClient ;
24
+ import org .asynchttpclient .AsyncHttpClientConfig ;
23
25
import org .asynchttpclient .BoundRequestBuilder ;
26
+ import org .asynchttpclient .DefaultAsyncHttpClient ;
27
+ import org .asynchttpclient .DefaultAsyncHttpClientConfig ;
24
28
import org .asynchttpclient .Response ;
25
29
import org .mockito .ArgumentCaptor ;
26
30
import org .testng .Assert ;
33
37
import java .util .Arrays ;
34
38
import java .util .Collection ;
35
39
import java .util .concurrent .ExecutionException ;
40
+ import java .util .concurrent .TimeUnit ;
36
41
import java .util .concurrent .TimeoutException ;
37
42
import java .util .concurrent .atomic .AtomicInteger ;
43
+ import java .util .concurrent .atomic .AtomicReference ;
38
44
import java .util .function .Consumer ;
39
45
import java .util .function .Supplier ;
40
46
47
+ import static java .util .concurrent .TimeUnit .SECONDS ;
41
48
import static org .asynchttpclient .extras .retrofit .AsyncHttpClientCall .runConsumer ;
42
49
import static org .asynchttpclient .extras .retrofit .AsyncHttpClientCall .runConsumers ;
43
50
import static org .mockito .ArgumentMatchers .any ;
46
53
import static org .testng .Assert .assertNotEquals ;
47
54
import static org .testng .Assert .assertTrue ;
48
55
56
+ @ Slf4j
49
57
public class AsyncHttpClientCallTest {
50
58
static final Request REQUEST = new Request .Builder ().url ("http://www.google.com/" ).build ();
59
+ static final DefaultAsyncHttpClientConfig DEFAULT_AHC_CONFIG = new DefaultAsyncHttpClientConfig .Builder ()
60
+ .setRequestTimeout (1_000 )
61
+ .build ();
51
62
52
63
private AsyncHttpClient httpClient ;
53
64
private Supplier <AsyncHttpClient > httpClientSupplier = () -> httpClient ;
54
65
55
66
@ BeforeMethod
56
67
void setup () {
57
- this .httpClient = mock (AsyncHttpClient .class );
68
+ httpClient = mock (AsyncHttpClient .class );
69
+ when (httpClient .getConfig ()).thenReturn (DEFAULT_AHC_CONFIG );
58
70
}
59
71
60
72
@ Test (expectedExceptions = NullPointerException .class , dataProvider = "first" )
@@ -108,7 +120,6 @@ void shouldInvokeConsumersOnEachExecution(Consumer<AsyncCompletionHandler<?>> ha
108
120
.onRequestFailure (t -> numFailed .incrementAndGet ())
109
121
.onRequestSuccess (r -> numOk .incrementAndGet ())
110
122
.requestCustomizer (rb -> numRequestCustomizer .incrementAndGet ())
111
- .executeTimeoutMillis (1000 )
112
123
.build ();
113
124
114
125
// when
@@ -245,13 +256,12 @@ public void contentTypeHeaderIsPassedInRequest() throws Exception {
245
256
Request request = requestWithBody ();
246
257
247
258
ArgumentCaptor <org .asynchttpclient .Request > capture = ArgumentCaptor .forClass (org .asynchttpclient .Request .class );
248
- AsyncHttpClient client = mock (AsyncHttpClient .class );
249
259
250
- givenResponseIsProduced (client , aResponse ());
260
+ givenResponseIsProduced (httpClient , aResponse ());
251
261
252
- whenRequestIsMade (client , request );
262
+ whenRequestIsMade (httpClient , request );
253
263
254
- verify (client ).executeRequest (capture .capture (), any ());
264
+ verify (httpClient ).executeRequest (capture .capture (), any ());
255
265
256
266
org .asynchttpclient .Request ahcRequest = capture .getValue ();
257
267
@@ -263,11 +273,9 @@ public void contentTypeHeaderIsPassedInRequest() throws Exception {
263
273
264
274
@ Test
265
275
public void contenTypeIsOptionalInResponse () throws Exception {
266
- AsyncHttpClient client = mock ( AsyncHttpClient . class );
276
+ givenResponseIsProduced ( httpClient , responseWithBody ( null , "test" ) );
267
277
268
- givenResponseIsProduced (client , responseWithBody (null , "test" ));
269
-
270
- okhttp3 .Response response = whenRequestIsMade (client , REQUEST );
278
+ okhttp3 .Response response = whenRequestIsMade (httpClient , REQUEST );
271
279
272
280
assertEquals (response .code (), 200 );
273
281
assertEquals (response .header ("Server" ), "nginx" );
@@ -277,11 +285,9 @@ public void contenTypeIsOptionalInResponse() throws Exception {
277
285
278
286
@ Test
279
287
public void contentTypeIsProperlyParsedIfPresent () throws Exception {
280
- AsyncHttpClient client = mock (AsyncHttpClient .class );
281
-
282
- givenResponseIsProduced (client , responseWithBody ("text/plain" , "test" ));
288
+ givenResponseIsProduced (httpClient , responseWithBody ("text/plain" , "test" ));
283
289
284
- okhttp3 .Response response = whenRequestIsMade (client , REQUEST );
290
+ okhttp3 .Response response = whenRequestIsMade (httpClient , REQUEST );
285
291
286
292
assertEquals (response .code (), 200 );
287
293
assertEquals (response .header ("Server" ), "nginx" );
@@ -292,11 +298,9 @@ public void contentTypeIsProperlyParsedIfPresent() throws Exception {
292
298
293
299
@ Test
294
300
public void bodyIsNotNullInResponse () throws Exception {
295
- AsyncHttpClient client = mock (AsyncHttpClient .class );
296
-
297
- givenResponseIsProduced (client , responseWithNoBody ());
301
+ givenResponseIsProduced (httpClient , responseWithNoBody ());
298
302
299
- okhttp3 .Response response = whenRequestIsMade (client , REQUEST );
303
+ okhttp3 .Response response = whenRequestIsMade (httpClient , REQUEST );
300
304
301
305
assertEquals (response .code (), 200 );
302
306
assertEquals (response .header ("Server" ), "nginx" );
@@ -315,6 +319,40 @@ void getHttpClientShouldThrowISEIfSupplierReturnsNull() {
315
319
call .getHttpClient ();
316
320
}
317
321
322
+ @ Test
323
+ void shouldReturnTimeoutSpecifiedInAHCInstanceConfig () {
324
+ // given:
325
+ val cfgBuilder = new DefaultAsyncHttpClientConfig .Builder ();
326
+ //val clientRef = new AtomicReference<AsyncHttpClient>();
327
+ AsyncHttpClientConfig config = null ;
328
+
329
+ // and: setup call
330
+ val call = AsyncHttpClientCall .builder ()
331
+ .httpClientSupplier (httpClientSupplier )
332
+ .request (requestWithBody ())
333
+ .build ();
334
+
335
+ // when: set read timeout to 5s, req timeout to 6s
336
+ config = cfgBuilder .setReadTimeout ((int ) SECONDS .toMillis (5 ))
337
+ .setRequestTimeout ((int ) SECONDS .toMillis (6 ))
338
+ .build ();
339
+ when (httpClient .getConfig ()).thenReturn (config );
340
+
341
+ // then
342
+ assertEquals (call .getRequestTimeoutMillis (), SECONDS .toMillis (6 ));
343
+ assertEquals (call .timeout ().timeoutNanos (), SECONDS .toNanos (6 ));
344
+
345
+ // when: set read timeout to 10 seconds, req timeout to 7s
346
+ config = cfgBuilder .setReadTimeout ((int ) SECONDS .toMillis (10 ))
347
+ .setRequestTimeout ((int ) SECONDS .toMillis (7 ))
348
+ .build ();
349
+ when (httpClient .getConfig ()).thenReturn (config );
350
+
351
+ // then:
352
+ assertEquals (call .getRequestTimeoutMillis (), SECONDS .toMillis (10 ));
353
+ assertEquals (call .timeout ().timeoutNanos (), SECONDS .toNanos (10 ));
354
+ }
355
+
318
356
private void givenResponseIsProduced (AsyncHttpClient client , Response response ) {
319
357
when (client .executeRequest (any (org .asynchttpclient .Request .class ), any ())).thenAnswer (invocation -> {
320
358
AsyncCompletionHandler <Response > handler = invocation .getArgument (1 );
0 commit comments