1
+ /*
2
+ * Copyright (c) 2014 AsyncHttpClient Project. All rights reserved.
3
+ *
4
+ * This program is licensed to you under the Apache License Version 2.0,
5
+ * and you may not use this file except in compliance with the Apache License Version 2.0.
6
+ * You may obtain a copy of the Apache License Version 2.0 at
7
+ * http://www.apache.org/licenses/LICENSE-2.0.
8
+ *
9
+ * Unless required by applicable law or agreed to in writing,
10
+ * software distributed under the Apache License Version 2.0 is distributed on an
11
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
13
+ */
1
14
package org .asynchttpclient ;
2
15
3
16
import static org .asynchttpclient .Dsl .asyncHttpClient ;
6
19
7
20
import java .util .ArrayList ;
8
21
import java .util .List ;
22
+ import java .util .stream .Collector ;
23
+ import java .util .stream .Collectors ;
24
+ import java .util .stream .Stream ;
9
25
10
26
import org .testng .annotations .Test ;
11
27
@@ -16,7 +32,7 @@ public class ClientStatsTest extends AbstractBasicTest {
16
32
17
33
@ Test (groups = "standalone" )
18
34
public void testClientStatus () throws Throwable {
19
- try (final DefaultAsyncHttpClient client = ( DefaultAsyncHttpClient ) asyncHttpClient (config ().setKeepAlive (true ).setPooledConnectionIdleTimeout (5000 ))) {
35
+ try (final AsyncHttpClient client = asyncHttpClient (config ().setKeepAlive (true ).setPooledConnectionIdleTimeout (5000 ))) {
20
36
final String url = getTargetUrl ();
21
37
22
38
final ClientStats emptyStats = client .getClientStats ();
@@ -26,11 +42,10 @@ public void testClientStatus() throws Throwable {
26
42
assertEquals (emptyStats .getIdleConnectionCount (), 0 );
27
43
assertEquals (emptyStats .getTotalConnectionCount (), 0 );
28
44
29
- final List <ListenableFuture <Response >> futures = new ArrayList <>();
30
- for (int i = 0 ; i < 5 ; i ++) {
31
- logger .info ("{} requesting url [{}]..." , i , url );
32
- futures .add (client .prepareGet (url ).setHeader ("LockThread" , "6" ).execute ());
33
- }
45
+ final List <ListenableFuture <Response >> futures =
46
+ Stream .generate (() -> client .prepareGet (url ).setHeader ("LockThread" ,"6" ).execute ())
47
+ .limit (5 )
48
+ .collect (Collectors .toList ());
34
49
35
50
Thread .sleep (2000 );
36
51
@@ -41,9 +56,7 @@ public void testClientStatus() throws Throwable {
41
56
assertEquals (activeStats .getIdleConnectionCount (), 0 );
42
57
assertEquals (activeStats .getTotalConnectionCount (), 5 );
43
58
44
- for (final ListenableFuture <Response > future : futures ) {
45
- future .get ();
46
- }
59
+ futures .forEach (future -> future .toCompletableFuture ().join ());
47
60
48
61
Thread .sleep (1000 );
49
62
@@ -56,11 +69,10 @@ public void testClientStatus() throws Throwable {
56
69
57
70
// Let's make sure the active count is correct when reusing cached connections.
58
71
59
- final List <ListenableFuture <Response >> repeatedFutures = new ArrayList <>();
60
- for (int i = 0 ; i < 3 ; i ++) {
61
- logger .info ("{} requesting url [{}]..." , i , url );
62
- repeatedFutures .add (client .prepareGet (url ).setHeader ("LockThread" , "6" ).execute ());
63
- }
72
+ final List <ListenableFuture <Response >> repeatedFutures =
73
+ Stream .generate (() -> client .prepareGet (url ).setHeader ("LockThread" ,"6" ).execute ())
74
+ .limit (3 )
75
+ .collect (Collectors .toList ());
64
76
65
77
Thread .sleep (2000 );
66
78
@@ -71,9 +83,7 @@ public void testClientStatus() throws Throwable {
71
83
assertEquals (activeCachedStats .getIdleConnectionCount (), 2 );
72
84
assertEquals (activeCachedStats .getTotalConnectionCount (), 5 );
73
85
74
- for (final ListenableFuture <Response > future : repeatedFutures ) {
75
- future .get ();
76
- }
86
+ repeatedFutures .forEach (future -> future .toCompletableFuture ().join ());
77
87
78
88
Thread .sleep (1000 );
79
89
@@ -97,7 +107,7 @@ public void testClientStatus() throws Throwable {
97
107
98
108
@ Test (groups = "standalone" )
99
109
public void testClientStatusNoKeepalive () throws Throwable {
100
- try (final DefaultAsyncHttpClient client = ( DefaultAsyncHttpClient ) asyncHttpClient (config ().setKeepAlive (false ))) {
110
+ try (final AsyncHttpClient client = asyncHttpClient (config ().setKeepAlive (false ))) {
101
111
final String url = getTargetUrl ();
102
112
103
113
final ClientStats emptyStats = client .getClientStats ();
@@ -107,11 +117,10 @@ public void testClientStatusNoKeepalive() throws Throwable {
107
117
assertEquals (emptyStats .getIdleConnectionCount (), 0 );
108
118
assertEquals (emptyStats .getTotalConnectionCount (), 0 );
109
119
110
- final List <ListenableFuture <Response >> futures = new ArrayList <>();
111
- for (int i = 0 ; i < 5 ; i ++) {
112
- logger .info ("{} requesting url [{}]..." , i , url );
113
- futures .add (client .prepareGet (url ).setHeader ("LockThread" , "6" ).execute ());
114
- }
120
+ final List <ListenableFuture <Response >> futures =
121
+ Stream .generate (() -> client .prepareGet (url ).setHeader ("LockThread" ,"6" ).execute ())
122
+ .limit (5 )
123
+ .collect (Collectors .toList ());
115
124
116
125
Thread .sleep (2000 );
117
126
@@ -122,9 +131,7 @@ public void testClientStatusNoKeepalive() throws Throwable {
122
131
assertEquals (activeStats .getIdleConnectionCount (), 0 );
123
132
assertEquals (activeStats .getTotalConnectionCount (), 5 );
124
133
125
- for (final ListenableFuture <Response > future : futures ) {
126
- future .get ();
127
- }
134
+ futures .forEach (future -> future .toCompletableFuture ().join ());
128
135
129
136
Thread .sleep (1000 );
130
137
@@ -137,11 +144,10 @@ public void testClientStatusNoKeepalive() throws Throwable {
137
144
138
145
// Let's make sure the active count is correct when reusing cached connections.
139
146
140
- final List <ListenableFuture <Response >> repeatedFutures = new ArrayList <>();
141
- for (int i = 0 ; i < 3 ; i ++) {
142
- logger .info ("{} requesting url [{}]..." , i , url );
143
- repeatedFutures .add (client .prepareGet (url ).setHeader ("LockThread" , "6" ).execute ());
144
- }
147
+ final List <ListenableFuture <Response >> repeatedFutures =
148
+ Stream .generate (() -> client .prepareGet (url ).setHeader ("LockThread" ,"6" ).execute ())
149
+ .limit (3 )
150
+ .collect (Collectors .toList ());
145
151
146
152
Thread .sleep (2000 );
147
153
@@ -152,9 +158,7 @@ public void testClientStatusNoKeepalive() throws Throwable {
152
158
assertEquals (activeCachedStats .getIdleConnectionCount (), 0 );
153
159
assertEquals (activeCachedStats .getTotalConnectionCount (), 3 );
154
160
155
- for (final ListenableFuture <Response > future : repeatedFutures ) {
156
- future .get ();
157
- }
161
+ repeatedFutures .forEach (future -> future .toCompletableFuture ().join ());
158
162
159
163
Thread .sleep (1000 );
160
164
0 commit comments