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+ */
114package org .asynchttpclient ;
215
316import static org .asynchttpclient .Dsl .asyncHttpClient ;
619
720import java .util .ArrayList ;
821import java .util .List ;
22+ import java .util .stream .Collector ;
23+ import java .util .stream .Collectors ;
24+ import java .util .stream .Stream ;
925
1026import org .testng .annotations .Test ;
1127
@@ -16,7 +32,7 @@ public class ClientStatsTest extends AbstractBasicTest {
1632
1733 @ Test (groups = "standalone" )
1834 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 ))) {
2036 final String url = getTargetUrl ();
2137
2238 final ClientStats emptyStats = client .getClientStats ();
@@ -26,11 +42,10 @@ public void testClientStatus() throws Throwable {
2642 assertEquals (emptyStats .getIdleConnectionCount (), 0 );
2743 assertEquals (emptyStats .getTotalConnectionCount (), 0 );
2844
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 ());
3449
3550 Thread .sleep (2000 );
3651
@@ -41,9 +56,7 @@ public void testClientStatus() throws Throwable {
4156 assertEquals (activeStats .getIdleConnectionCount (), 0 );
4257 assertEquals (activeStats .getTotalConnectionCount (), 5 );
4358
44- for (final ListenableFuture <Response > future : futures ) {
45- future .get ();
46- }
59+ futures .forEach (future -> future .toCompletableFuture ().join ());
4760
4861 Thread .sleep (1000 );
4962
@@ -56,11 +69,10 @@ public void testClientStatus() throws Throwable {
5669
5770 // Let's make sure the active count is correct when reusing cached connections.
5871
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 ());
6476
6577 Thread .sleep (2000 );
6678
@@ -71,9 +83,7 @@ public void testClientStatus() throws Throwable {
7183 assertEquals (activeCachedStats .getIdleConnectionCount (), 2 );
7284 assertEquals (activeCachedStats .getTotalConnectionCount (), 5 );
7385
74- for (final ListenableFuture <Response > future : repeatedFutures ) {
75- future .get ();
76- }
86+ repeatedFutures .forEach (future -> future .toCompletableFuture ().join ());
7787
7888 Thread .sleep (1000 );
7989
@@ -97,7 +107,7 @@ public void testClientStatus() throws Throwable {
97107
98108 @ Test (groups = "standalone" )
99109 public void testClientStatusNoKeepalive () throws Throwable {
100- try (final DefaultAsyncHttpClient client = ( DefaultAsyncHttpClient ) asyncHttpClient (config ().setKeepAlive (false ))) {
110+ try (final AsyncHttpClient client = asyncHttpClient (config ().setKeepAlive (false ))) {
101111 final String url = getTargetUrl ();
102112
103113 final ClientStats emptyStats = client .getClientStats ();
@@ -107,11 +117,10 @@ public void testClientStatusNoKeepalive() throws Throwable {
107117 assertEquals (emptyStats .getIdleConnectionCount (), 0 );
108118 assertEquals (emptyStats .getTotalConnectionCount (), 0 );
109119
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 ());
115124
116125 Thread .sleep (2000 );
117126
@@ -122,9 +131,7 @@ public void testClientStatusNoKeepalive() throws Throwable {
122131 assertEquals (activeStats .getIdleConnectionCount (), 0 );
123132 assertEquals (activeStats .getTotalConnectionCount (), 5 );
124133
125- for (final ListenableFuture <Response > future : futures ) {
126- future .get ();
127- }
134+ futures .forEach (future -> future .toCompletableFuture ().join ());
128135
129136 Thread .sleep (1000 );
130137
@@ -137,11 +144,10 @@ public void testClientStatusNoKeepalive() throws Throwable {
137144
138145 // Let's make sure the active count is correct when reusing cached connections.
139146
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 ());
145151
146152 Thread .sleep (2000 );
147153
@@ -152,9 +158,7 @@ public void testClientStatusNoKeepalive() throws Throwable {
152158 assertEquals (activeCachedStats .getIdleConnectionCount (), 0 );
153159 assertEquals (activeCachedStats .getTotalConnectionCount (), 3 );
154160
155- for (final ListenableFuture <Response > future : repeatedFutures ) {
156- future .get ();
157- }
161+ repeatedFutures .forEach (future -> future .toCompletableFuture ().join ());
158162
159163 Thread .sleep (1000 );
160164
0 commit comments