1
1
package org .asynchttpclient ;
2
2
3
3
import io .netty .util .Timer ;
4
- import org .asynchttpclient .DefaultAsyncHttpClientConfig .Builder ;
5
4
import org .asynchttpclient .cookie .CookieEvictionTask ;
6
5
import org .asynchttpclient .cookie .CookieStore ;
7
6
import org .asynchttpclient .cookie .ThreadSafeCookieStore ;
8
7
import org .testng .annotations .Test ;
9
8
10
- import java .io .Closeable ;
11
9
import java .io .IOException ;
12
10
import java .util .concurrent .TimeUnit ;
13
11
12
+ import static org .asynchttpclient .Dsl .*;
14
13
import static org .mockito .ArgumentMatchers .any ;
15
14
import static org .mockito .ArgumentMatchers .anyLong ;
16
15
import static org .mockito .Mockito .*;
@@ -20,62 +19,88 @@ public class DefaultAsyncHttpClientTest {
20
19
21
20
@ Test
22
21
public void testWithSharedNettyTimerShouldScheduleCookieEvictionOnlyOnce () throws IOException {
23
- final Timer nettyTimerMock = mock (Timer .class );
24
- final CookieStore cookieStore = new ThreadSafeCookieStore ();
25
- final DefaultAsyncHttpClientConfig config = new Builder ().setNettyTimer (nettyTimerMock ).setCookieStore (cookieStore ).build ();
26
- final AsyncHttpClient client1 = new DefaultAsyncHttpClient (config );
27
- final AsyncHttpClient client2 = new DefaultAsyncHttpClient (config );
28
-
29
- assertEquals (cookieStore .count (), 2 );
30
- verify (nettyTimerMock , times (1 )).newTimeout (any (CookieEvictionTask .class ), anyLong (), any (TimeUnit .class ));
31
-
32
- closeSilently (client1 );
33
- closeSilently (client2 );
22
+ Timer nettyTimerMock = mock (Timer .class );
23
+ CookieStore cookieStore = new ThreadSafeCookieStore ();
24
+ AsyncHttpClientConfig config = config ().setNettyTimer (nettyTimerMock ).setCookieStore (cookieStore ).build ();
25
+
26
+ try (AsyncHttpClient client1 = asyncHttpClient (config )) {
27
+ try (AsyncHttpClient client2 = asyncHttpClient (config )) {
28
+ assertEquals (cookieStore .count (), 2 );
29
+ verify (nettyTimerMock , times (1 )).newTimeout (any (CookieEvictionTask .class ), anyLong (), any (TimeUnit .class ));
30
+ }
31
+ }
34
32
}
35
33
36
34
@ Test
37
35
public void testWitDefaultConfigShouldScheduleCookieEvictionForEachAHC () throws IOException {
38
- final AsyncHttpClientConfig config1 = new DefaultAsyncHttpClientConfig .Builder ().build ();
39
- final DefaultAsyncHttpClient client1 = new DefaultAsyncHttpClient (config1 );
36
+ AsyncHttpClientConfig config1 = config ().build ();
37
+ try (AsyncHttpClient client1 = asyncHttpClient (config1 )) {
38
+ AsyncHttpClientConfig config2 = config ().build ();
39
+ try (AsyncHttpClient client2 = asyncHttpClient (config2 )) {
40
+ assertEquals (config1 .getCookieStore ().count (), 1 );
41
+ assertEquals (config2 .getCookieStore ().count (), 1 );
42
+ }
43
+ }
44
+ }
40
45
41
- final AsyncHttpClientConfig config2 = new DefaultAsyncHttpClientConfig .Builder ().build ();
42
- final DefaultAsyncHttpClient client2 = new DefaultAsyncHttpClient (config2 );
46
+ @ Test
47
+ public void testWithSharedCookieStoreButNonSharedTimerShouldScheduleCookieEvictionForFirstAHC () throws IOException {
48
+ CookieStore cookieStore = new ThreadSafeCookieStore ();
49
+ Timer nettyTimerMock1 = mock (Timer .class );
50
+ AsyncHttpClientConfig config1 = config ()
51
+ .setCookieStore (cookieStore ).setNettyTimer (nettyTimerMock1 ).build ();
52
+
53
+ try (AsyncHttpClient client1 = asyncHttpClient (config1 )) {
54
+ Timer nettyTimerMock2 = mock (Timer .class );
55
+ AsyncHttpClientConfig config2 = config ()
56
+ .setCookieStore (cookieStore ).setNettyTimer (nettyTimerMock2 ).build ();
57
+ try (AsyncHttpClient client2 = asyncHttpClient (config2 )) {
58
+ assertEquals (config1 .getCookieStore ().count (), 2 );
59
+ verify (nettyTimerMock1 , times (1 )).newTimeout (any (CookieEvictionTask .class ), anyLong (), any (TimeUnit .class ));
60
+ verify (nettyTimerMock2 , never ()).newTimeout (any (CookieEvictionTask .class ), anyLong (), any (TimeUnit .class ));
61
+ }
62
+ }
43
63
44
- assertEquals (config1 .getCookieStore ().count (), 1 );
45
- assertEquals (config2 .getCookieStore ().count (), 1 );
64
+ Timer nettyTimerMock3 = mock (Timer .class );
65
+ AsyncHttpClientConfig config3 = config ()
66
+ .setCookieStore (cookieStore ).setNettyTimer (nettyTimerMock3 ).build ();
46
67
47
- closeSilently (client1 );
48
- closeSilently (client2 );
68
+ try (AsyncHttpClient client2 = asyncHttpClient (config3 )) {
69
+ assertEquals (config1 .getCookieStore ().count (), 1 );
70
+ verify (nettyTimerMock3 , times (1 )).newTimeout (any (CookieEvictionTask .class ), anyLong (), any (TimeUnit .class ));
71
+ }
49
72
}
50
73
51
74
@ Test
52
- public void testWithSharedCookieStoreButNonSharedTimerShouldScheduleCookieEvictionForFirstAHC () throws IOException {
53
- final CookieStore cookieStore = new ThreadSafeCookieStore ();
54
- final Timer nettyTimerMock1 = mock (Timer .class );
55
- final AsyncHttpClientConfig config1 = new DefaultAsyncHttpClientConfig .Builder ()
56
- .setCookieStore (cookieStore ).setNettyTimer (nettyTimerMock1 ).build ();
57
- final DefaultAsyncHttpClient client1 = new DefaultAsyncHttpClient (config1 );
58
-
59
- final Timer nettyTimerMock2 = mock (Timer .class );
60
- final AsyncHttpClientConfig config2 = new DefaultAsyncHttpClientConfig .Builder ()
61
- .setCookieStore (cookieStore ).setNettyTimer (nettyTimerMock2 ).build ();
62
- final DefaultAsyncHttpClient client2 = new DefaultAsyncHttpClient (config2 );
63
-
64
- assertEquals (config1 .getCookieStore ().count (), 2 );
65
- verify (nettyTimerMock1 , times (1 )).newTimeout (any (CookieEvictionTask .class ), anyLong (), any (TimeUnit .class ));
66
- verify (nettyTimerMock2 , never ()).newTimeout (any (CookieEvictionTask .class ), anyLong (), any (TimeUnit .class ));
67
-
68
- closeSilently (client1 );
69
- closeSilently (client2 );
75
+ public void testWithSharedCookieStoreButNonSharedTimerShouldReScheduleCookieEvictionWhenFirstInstanceGetClosed () throws IOException {
76
+ CookieStore cookieStore = new ThreadSafeCookieStore ();
77
+ Timer nettyTimerMock1 = mock (Timer .class );
78
+ AsyncHttpClientConfig config1 = config ()
79
+ .setCookieStore (cookieStore ).setNettyTimer (nettyTimerMock1 ).build ();
80
+
81
+ try (AsyncHttpClient client1 = asyncHttpClient (config1 )) {
82
+ assertEquals (config1 .getCookieStore ().count (), 1 );
83
+ verify (nettyTimerMock1 , times (1 )).newTimeout (any (CookieEvictionTask .class ), anyLong (), any (TimeUnit .class ));
84
+ }
85
+
86
+ assertEquals (config1 .getCookieStore ().count (), 0 );
87
+
88
+ Timer nettyTimerMock2 = mock (Timer .class );
89
+ AsyncHttpClientConfig config2 = config ()
90
+ .setCookieStore (cookieStore ).setNettyTimer (nettyTimerMock2 ).build ();
91
+
92
+ try (AsyncHttpClient client2 = asyncHttpClient (config2 )) {
93
+ assertEquals (config1 .getCookieStore ().count (), 1 );
94
+ verify (nettyTimerMock2 , times (1 )).newTimeout (any (CookieEvictionTask .class ), anyLong (), any (TimeUnit .class ));
95
+ }
70
96
}
71
97
72
- private static void closeSilently (Closeable closeable ) {
73
- if (closeable != null ) {
74
- try {
75
- closeable .close ();
76
- } catch (IOException e ) {
77
- // swallow
78
- }
98
+ @ Test
99
+ public void testDisablingCookieStore () throws IOException {
100
+ AsyncHttpClientConfig config = config ()
101
+ .setCookieStore (null ).build ();
102
+ try (AsyncHttpClient client = asyncHttpClient (config )) {
103
+ //
79
104
}
80
105
}
81
106
}
0 commit comments