use of com.hotels.styx.api.extension.service.ConnectionPoolSettings 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.api.extension.service.ConnectionPoolSettings 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.api.extension.service.ConnectionPoolSettings 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.api.extension.service.ConnectionPoolSettings in project styx by ExpediaGroup.
the class SimpleConnectionPoolTest method limitsPendingConnectionsDueToConnectionEstablishment.
@Test
public void limitsPendingConnectionsDueToConnectionEstablishment() {
when(connectionFactory.createConnection(any(Origin.class), any(ConnectionSettings.class))).thenReturn(Mono.from(EmitterProcessor.create())).thenReturn(Mono.from(EmitterProcessor.create())).thenReturn(Mono.from(EmitterProcessor.create()));
ConnectionPoolSettings poolSettings = new ConnectionPoolSettings.Builder().maxPendingConnectionsPerHost(2).build();
SimpleConnectionPool pool = new SimpleConnectionPool(origin, poolSettings, connectionFactory);
Mono.from(pool.borrowConnection()).toFuture();
Mono.from(pool.borrowConnection()).toFuture();
StepVerifier.create(pool.borrowConnection()).expectError(MaxPendingConnectionsExceededException.class).verify();
assertEquals(pool.stats().connectionAttempts(), 2);
assertEquals(pool.stats().pendingConnectionCount(), 2);
}
use of com.hotels.styx.api.extension.service.ConnectionPoolSettings in project styx by ExpediaGroup.
the class SimpleConnectionPoolTest method shouldNotHandoutConnectionToCancelledSubscriberWhenConnectionAlreadyInPool.
@Test
public void shouldNotHandoutConnectionToCancelledSubscriberWhenConnectionAlreadyInPool() throws Exception {
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 connection1;
}
}));
StepVerifier.create(pool.borrowConnection()).expectError(MaxPendingConnectionTimeoutException.class).verify();
assertEquals(pool.stats().availableConnectionCount(), 1);
// Waiting subscribers
assertEquals(pool.stats().pendingConnectionCount(), 0);
// Borrowed count
assertEquals(pool.stats().busyConnectionCount(), 0);
}
Aggregations