use of com.hotels.styx.client.Connection in project styx by ExpediaGroup.
the class SimpleConnectionPoolTest method limitsPendingConnectionsDueToPoolSaturation.
@Test
public void limitsPendingConnectionsDueToPoolSaturation() {
when(connectionFactory.createConnection(any(Origin.class), any(ConnectionSettings.class))).thenReturn(Mono.just(connection1)).thenReturn(Mono.just(connection2)).thenReturn(Mono.just(connection3)).thenReturn(Mono.just(connection4));
ConnectionPoolSettings poolSettings = new ConnectionPoolSettings.Builder().maxConnectionsPerHost(2).maxPendingConnectionsPerHost(2).build();
SimpleConnectionPool pool = new SimpleConnectionPool(origin, poolSettings, connectionFactory);
// Saturate active connections:
CompletableFuture<Connection> pending1 = Mono.from(pool.borrowConnection()).toFuture();
CompletableFuture<Connection> pending2 = Mono.from(pool.borrowConnection()).toFuture();
assertTrue(pending1.isDone());
assertTrue(pending2.isDone());
// These are pending
CompletableFuture<Connection> pending3 = Mono.from(pool.borrowConnection()).toFuture();
CompletableFuture<Connection> pending4 = Mono.from(pool.borrowConnection()).toFuture();
assertFalse(pending3.isDone());
assertFalse(pending4.isDone());
// This throws an error:
StepVerifier.create(pool.borrowConnection()).expectError(MaxPendingConnectionsExceededException.class).verify();
assertEquals(pool.stats().connectionAttempts(), 2);
assertEquals(pool.stats().pendingConnectionCount(), 2);
assertEquals(pool.stats().busyConnectionCount(), 2);
assertEquals(pool.stats().availableConnectionCount(), 0);
assertEquals(pool.stats().terminatedConnections(), 0);
assertEquals(pool.stats().closedConnections(), 0);
}
use of com.hotels.styx.client.Connection in project styx by ExpediaGroup.
the class SimpleConnectionPoolTest method closeConnectionTriggersConnectionEstablishment.
@Test
public void closeConnectionTriggersConnectionEstablishment() throws ExecutionException, InterruptedException {
when(connectionFactory.createConnection(any(Origin.class), any(ConnectionSettings.class))).thenReturn(Mono.just(connection1)).thenReturn(Mono.just(connection2)).thenReturn(Mono.just(connection3)).thenReturn(Mono.just(connection4));
ConnectionPoolSettings poolSettings = new ConnectionPoolSettings.Builder().maxConnectionsPerHost(2).maxPendingConnectionsPerHost(2).build();
SimpleConnectionPool pool = new SimpleConnectionPool(origin, poolSettings, connectionFactory);
// Saturate pool:
StepVerifier.create(pool.borrowConnection()).expectNext(connection1).verifyComplete();
StepVerifier.create(pool.borrowConnection()).expectNext(connection2).verifyComplete();
assertEquals(pool.stats().busyConnectionCount(), 2);
assertEquals(pool.stats().availableConnectionCount(), 0);
// Create a pending connection
CompletableFuture<Connection> pending1 = Mono.from(pool.borrowConnection()).toFuture();
assertFalse(pending1.isDone());
assertEquals(pool.stats().busyConnectionCount(), 2);
assertEquals(pool.stats().pendingConnectionCount(), 1);
assertEquals(pool.stats().availableConnectionCount(), 0);
// Close a previously borrowed connection:
pool.closeConnection(connection1);
// A new connection is established and handed over to pending subscriber:
assertEquals(pending1.get(), connection3);
assertEquals(pool.stats().busyConnectionCount(), 2);
assertEquals(pool.stats().pendingConnectionCount(), 0);
assertEquals(pool.stats().availableConnectionCount(), 0);
assertEquals(pool.stats().closedConnections(), 1);
}
use of com.hotels.styx.client.Connection in project styx by ExpediaGroup.
the class SimpleConnectionPoolTest method shouldNotHandoutConnectionToCancelledSubscriberWhenConnectionIsReturned.
@Test
public void shouldNotHandoutConnectionToCancelledSubscriberWhenConnectionIsReturned() throws Exception {
EmitterProcessor<Connection> processor = EmitterProcessor.create();
when(connectionFactory.createConnection(any(Origin.class), any(ConnectionSettings.class))).thenReturn(Mono.from(processor));
ConnectionPoolSettings poolSettings = new ConnectionPoolSettings.Builder().pendingConnectionTimeout(100, MILLISECONDS).build();
SimpleConnectionPool simpleConnectionPool = new SimpleConnectionPool(origin, poolSettings, connectionFactory);
SimpleConnectionPool pool = spy(simpleConnectionPool);
when(pool.dequeue()).thenAnswer(new AnswersWithDelay(200, new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return invocation.callRealMethod();
}
}));
StepVerifier.create(pool.borrowConnection()).expectError(MaxPendingConnectionTimeoutException.class).verify();
pool.returnConnection(connection1);
assertEquals(pool.stats().availableConnectionCount(), 1);
// Waiting subscribers
assertEquals(pool.stats().pendingConnectionCount(), 0);
// Borrowed count
assertEquals(pool.stats().busyConnectionCount(), -1);
}
use of com.hotels.styx.client.Connection in project styx by ExpediaGroup.
the class SimpleConnectionPoolTest method givesReturnedConnectionsToPendingSubscibers.
@Test
public void givesReturnedConnectionsToPendingSubscibers() throws ExecutionException, InterruptedException, TimeoutException {
when(connectionFactory.createConnection(any(Origin.class), any(ConnectionSettings.class))).thenReturn(Mono.just(connection1)).thenReturn(Mono.just(connection2)).thenReturn(Mono.just(connection3)).thenReturn(Mono.just(connection4));
ConnectionPoolSettings poolSettings = new ConnectionPoolSettings.Builder().maxConnectionsPerHost(2).maxPendingConnectionsPerHost(2).build();
SimpleConnectionPool pool = new SimpleConnectionPool(origin, poolSettings, connectionFactory);
// Saturate active connections:
CompletableFuture<Connection> active1 = Mono.from(pool.borrowConnection()).toFuture();
CompletableFuture<Connection> active2 = Mono.from(pool.borrowConnection()).toFuture();
assertTrue(active1.isDone());
assertTrue(active2.isDone());
assertEquals(pool.stats().connectionAttempts(), 2);
assertEquals(pool.stats().busyConnectionCount(), 2);
// Create two pending connections
CompletableFuture<Connection> pending1 = Mono.from(pool.borrowConnection()).toFuture();
CompletableFuture<Connection> pending2 = Mono.from(pool.borrowConnection()).toFuture();
assertFalse(pending1.isDone());
assertFalse(pending2.isDone());
assertEquals(pool.stats().connectionAttempts(), 2);
assertEquals(pool.stats().busyConnectionCount(), 2);
assertEquals(pool.stats().pendingConnectionCount(), 2);
// Return active connections
pool.returnConnection(active1.get());
pool.returnConnection(active2.get());
// Show that returned connections are given to pending subscribers
assertEquals(pending1.get(), connection1);
assertEquals(pending2.get(), connection2);
assertEquals(pool.stats().connectionAttempts(), 2);
assertEquals(pool.stats().busyConnectionCount(), 2);
assertEquals(pool.stats().pendingConnectionCount(), 0);
}
use of com.hotels.styx.client.Connection in project styx by ExpediaGroup.
the class NettyConnectionTest method willNotNotifyListenersIfConnectionRemainActive.
@Test
public void willNotNotifyListenersIfConnectionRemainActive() throws Exception {
Connection connection = createConnection();
EventCapturingListener listener = new EventCapturingListener();
connection.addConnectionListener(listener);
assertThat(listener.closedConnection(), isAbsent());
}
Aggregations