Skip to content

Commit 552e4cd

Browse files
committed
Small tweaks and changes in response to PR comments
1 parent 81cb9ba commit 552e4cd

File tree

6 files changed

+52
-58
lines changed

6 files changed

+52
-58
lines changed

client/src/main/java/org/asynchttpclient/ClientStats.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
/*
2-
* Copyright 2010 Ning, Inc.
2+
* Copyright (c) 2014 AsyncHttpClient Project. All rights reserved.
33
*
4-
* This program is licensed to you under the Apache License, version 2.0
5-
* (the "License"); you may not use this file except in compliance with the
6-
* License. You may obtain a copy of the License at:
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12-
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13-
* License for the specific language governing permissions and limitations
14-
* under the License.
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.
158
*
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.
1613
*/
1714
package org.asynchttpclient;
1815

@@ -26,8 +23,8 @@ public class ClientStats {
2623
private final long activeConnectionCount;
2724
private final long idleConnectionCount;
2825

29-
public ClientStats(final long activeConnectionCount,
30-
final long idleConnectionCount) {
26+
public ClientStats(long activeConnectionCount,
27+
long idleConnectionCount) {
3128
this.activeConnectionCount = activeConnectionCount;
3229
this.idleConnectionCount = idleConnectionCount;
3330
}

client/src/main/java/org/asynchttpclient/channel/NoopChannelPool.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,4 @@ public void flushPartitions(ChannelPoolPartitionSelector selector) {
5555
public long getIdleChannelCount() {
5656
return 0;
5757
}
58-
59-
6058
}

client/src/main/java/org/asynchttpclient/netty/channel/DefaultChannelPool.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -358,12 +358,7 @@ public void flushPartitions(ChannelPoolPartitionSelector selector) {
358358

359359
@Override
360360
public long getIdleChannelCount() {
361-
return partitions.reduceValuesToLong(
362-
Long.MAX_VALUE,
363-
ConcurrentLinkedDeque::size,
364-
0,
365-
(left, right) -> left + right
366-
);
361+
return partitions.values().stream().mapToLong(ConcurrentLinkedDeque::size).sum();
367362
}
368363

369364
public enum PoolLeaseStrategy {

client/src/test/java/org/asynchttpclient/ClientStatsTest.java

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
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+
*/
114
package org.asynchttpclient;
215

316
import static org.asynchttpclient.Dsl.asyncHttpClient;
@@ -6,6 +19,9 @@
619

720
import java.util.ArrayList;
821
import java.util.List;
22+
import java.util.stream.Collector;
23+
import java.util.stream.Collectors;
24+
import java.util.stream.Stream;
925

1026
import 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

extras/registry/src/test/java/org/asynchttpclient/extras/registry/BadAsyncHttpClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,6 @@ public ListenableFuture<Response> executeRequest(RequestBuilder requestBuilder)
129129

130130
@Override
131131
public ClientStats getClientStats() {
132-
return null;
132+
throw new UnsupportedOperationException();
133133
}
134134
}

extras/registry/src/test/java/org/asynchttpclient/extras/registry/TestAsyncHttpClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,6 @@ public ListenableFuture<Response> executeRequest(RequestBuilder requestBuilder)
125125

126126
@Override
127127
public ClientStats getClientStats() {
128-
return null;
128+
throw new UnsupportedOperationException();
129129
}
130130
}

0 commit comments

Comments
 (0)