Skip to content

Commit

Permalink
Small tweaks and changes in response to PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Diagoras committed Nov 5, 2016
1 parent 81cb9ba commit 552e4cd
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 58 deletions.
25 changes: 11 additions & 14 deletions client/src/main/java/org/asynchttpclient/ClientStats.java
@@ -1,18 +1,15 @@
/*
* Copyright 2010 Ning, Inc.
* Copyright (c) 2014 AsyncHttpClient Project. All rights reserved.
*
* This program is licensed to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package org.asynchttpclient;

Expand All @@ -26,8 +23,8 @@ public class ClientStats {
private final long activeConnectionCount;
private final long idleConnectionCount;

public ClientStats(final long activeConnectionCount,
final long idleConnectionCount) {
public ClientStats(long activeConnectionCount,
long idleConnectionCount) {
this.activeConnectionCount = activeConnectionCount;
this.idleConnectionCount = idleConnectionCount;
}
Expand Down
Expand Up @@ -55,6 +55,4 @@ public void flushPartitions(ChannelPoolPartitionSelector selector) {
public long getIdleChannelCount() {
return 0;
}


}
Expand Up @@ -358,12 +358,7 @@ public void flushPartitions(ChannelPoolPartitionSelector selector) {

@Override
public long getIdleChannelCount() {
return partitions.reduceValuesToLong(
Long.MAX_VALUE,
ConcurrentLinkedDeque::size,
0,
(left, right) -> left + right
);
return partitions.values().stream().mapToLong(ConcurrentLinkedDeque::size).sum();
}

public enum PoolLeaseStrategy {
Expand Down
72 changes: 38 additions & 34 deletions client/src/test/java/org/asynchttpclient/ClientStatsTest.java
@@ -1,3 +1,16 @@
/*
* Copyright (c) 2014 AsyncHttpClient Project. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package org.asynchttpclient;

import static org.asynchttpclient.Dsl.asyncHttpClient;
Expand All @@ -6,6 +19,9 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.testng.annotations.Test;

Expand All @@ -16,7 +32,7 @@ public class ClientStatsTest extends AbstractBasicTest {

@Test(groups = "standalone")
public void testClientStatus() throws Throwable {
try (final DefaultAsyncHttpClient client = (DefaultAsyncHttpClient) asyncHttpClient(config().setKeepAlive(true).setPooledConnectionIdleTimeout(5000))) {
try (final AsyncHttpClient client = asyncHttpClient(config().setKeepAlive(true).setPooledConnectionIdleTimeout(5000))) {
final String url = getTargetUrl();

final ClientStats emptyStats = client.getClientStats();
Expand All @@ -26,11 +42,10 @@ public void testClientStatus() throws Throwable {
assertEquals(emptyStats.getIdleConnectionCount(), 0);
assertEquals(emptyStats.getTotalConnectionCount(), 0);

final List<ListenableFuture<Response>> futures = new ArrayList<>();
for (int i = 0; i < 5; i++) {
logger.info("{} requesting url [{}]...", i, url);
futures.add(client.prepareGet(url).setHeader("LockThread", "6").execute());
}
final List<ListenableFuture<Response>> futures =
Stream.generate(() -> client.prepareGet(url).setHeader("LockThread","6").execute())
.limit(5)
.collect(Collectors.toList());

Thread.sleep(2000);

Expand All @@ -41,9 +56,7 @@ public void testClientStatus() throws Throwable {
assertEquals(activeStats.getIdleConnectionCount(), 0);
assertEquals(activeStats.getTotalConnectionCount(), 5);

for (final ListenableFuture<Response> future : futures) {
future.get();
}
futures.forEach(future -> future.toCompletableFuture().join());

Thread.sleep(1000);

Expand All @@ -56,11 +69,10 @@ public void testClientStatus() throws Throwable {

// Let's make sure the active count is correct when reusing cached connections.

final List<ListenableFuture<Response>> repeatedFutures = new ArrayList<>();
for (int i = 0; i < 3; i++) {
logger.info("{} requesting url [{}]...", i, url);
repeatedFutures.add(client.prepareGet(url).setHeader("LockThread", "6").execute());
}
final List<ListenableFuture<Response>> repeatedFutures =
Stream.generate(() -> client.prepareGet(url).setHeader("LockThread","6").execute())
.limit(3)
.collect(Collectors.toList());

Thread.sleep(2000);

Expand All @@ -71,9 +83,7 @@ public void testClientStatus() throws Throwable {
assertEquals(activeCachedStats.getIdleConnectionCount(), 2);
assertEquals(activeCachedStats.getTotalConnectionCount(), 5);

for (final ListenableFuture<Response> future : repeatedFutures) {
future.get();
}
repeatedFutures.forEach(future -> future.toCompletableFuture().join());

Thread.sleep(1000);

Expand All @@ -97,7 +107,7 @@ public void testClientStatus() throws Throwable {

@Test(groups = "standalone")
public void testClientStatusNoKeepalive() throws Throwable {
try (final DefaultAsyncHttpClient client = (DefaultAsyncHttpClient) asyncHttpClient(config().setKeepAlive(false))) {
try (final AsyncHttpClient client = asyncHttpClient(config().setKeepAlive(false))) {
final String url = getTargetUrl();

final ClientStats emptyStats = client.getClientStats();
Expand All @@ -107,11 +117,10 @@ public void testClientStatusNoKeepalive() throws Throwable {
assertEquals(emptyStats.getIdleConnectionCount(), 0);
assertEquals(emptyStats.getTotalConnectionCount(), 0);

final List<ListenableFuture<Response>> futures = new ArrayList<>();
for (int i = 0; i < 5; i++) {
logger.info("{} requesting url [{}]...", i, url);
futures.add(client.prepareGet(url).setHeader("LockThread", "6").execute());
}
final List<ListenableFuture<Response>> futures =
Stream.generate(() -> client.prepareGet(url).setHeader("LockThread","6").execute())
.limit(5)
.collect(Collectors.toList());

Thread.sleep(2000);

Expand All @@ -122,9 +131,7 @@ public void testClientStatusNoKeepalive() throws Throwable {
assertEquals(activeStats.getIdleConnectionCount(), 0);
assertEquals(activeStats.getTotalConnectionCount(), 5);

for (final ListenableFuture<Response> future : futures) {
future.get();
}
futures.forEach(future -> future.toCompletableFuture().join());

Thread.sleep(1000);

Expand All @@ -137,11 +144,10 @@ public void testClientStatusNoKeepalive() throws Throwable {

// Let's make sure the active count is correct when reusing cached connections.

final List<ListenableFuture<Response>> repeatedFutures = new ArrayList<>();
for (int i = 0; i < 3; i++) {
logger.info("{} requesting url [{}]...", i, url);
repeatedFutures.add(client.prepareGet(url).setHeader("LockThread", "6").execute());
}
final List<ListenableFuture<Response>> repeatedFutures =
Stream.generate(() -> client.prepareGet(url).setHeader("LockThread","6").execute())
.limit(3)
.collect(Collectors.toList());

Thread.sleep(2000);

Expand All @@ -152,9 +158,7 @@ public void testClientStatusNoKeepalive() throws Throwable {
assertEquals(activeCachedStats.getIdleConnectionCount(), 0);
assertEquals(activeCachedStats.getTotalConnectionCount(), 3);

for (final ListenableFuture<Response> future : repeatedFutures) {
future.get();
}
repeatedFutures.forEach(future -> future.toCompletableFuture().join());

Thread.sleep(1000);

Expand Down
Expand Up @@ -129,6 +129,6 @@ public ListenableFuture<Response> executeRequest(RequestBuilder requestBuilder)

@Override
public ClientStats getClientStats() {
return null;
throw new UnsupportedOperationException();
}
}
Expand Up @@ -125,6 +125,6 @@ public ListenableFuture<Response> executeRequest(RequestBuilder requestBuilder)

@Override
public ClientStats getClientStats() {
return null;
throw new UnsupportedOperationException();
}
}

0 comments on commit 552e4cd

Please sign in to comment.