use of org.apache.hc.core5.http2.nio.support.BasicPingHandler in project httpcomponents-core by apache.
the class H2IntegrationTest method testConnectionPing.
@Test
public void testConnectionPing() throws Exception {
server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
final InetSocketAddress serverEndpoint = server.start();
client.start();
final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
final ClientSessionEndpoint streamEndpoint = connectFuture.get();
final int n = 10;
final CountDownLatch latch = new CountDownLatch(n);
final AtomicInteger count = new AtomicInteger(0);
for (int i = 0; i < n; i++) {
streamEndpoint.execute(new BasicRequestProducer(Method.GET, createRequestURI(serverEndpoint, "/hello")), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
streamEndpoint.execute(new PingCommand(new BasicPingHandler(result -> {
if (result) {
count.incrementAndGet();
}
latch.countDown();
})), Command.Priority.NORMAL);
}
Assertions.assertTrue(latch.await(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
Assertions.assertEquals(n, count.get());
}
use of org.apache.hc.core5.http2.nio.support.BasicPingHandler in project httpcomponents-core by apache.
the class H2ConnPool method validateSession.
@Override
protected void validateSession(final IOSession ioSession, final Callback<Boolean> callback) {
if (ioSession.isOpen()) {
final TimeValue timeValue = validateAfterInactivity;
if (TimeValue.isNonNegative(timeValue)) {
final long lastAccessTime = Math.min(ioSession.getLastReadTime(), ioSession.getLastWriteTime());
final long deadline = lastAccessTime + timeValue.toMilliseconds();
if (deadline <= System.currentTimeMillis()) {
final Timeout socketTimeoutMillis = ioSession.getSocketTimeout();
ioSession.enqueue(new PingCommand(new BasicPingHandler(result -> {
ioSession.setSocketTimeout(socketTimeoutMillis);
callback.execute(result);
})), Command.Priority.NORMAL);
return;
}
}
callback.execute(true);
} else {
callback.execute(false);
}
}
Aggregations