Search in sources :

Example 1 with ConnectionPoolSettings

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);
}
Also used : Connection(com.hotels.styx.client.Connection) ConnectionPoolSettings.defaultConnectionPoolSettings(com.hotels.styx.api.extension.service.ConnectionPoolSettings.defaultConnectionPoolSettings) ConnectionPoolSettings(com.hotels.styx.api.extension.service.ConnectionPoolSettings) Test(org.junit.jupiter.api.Test)

Example 2 with ConnectionPoolSettings

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);
}
Also used : Connection(com.hotels.styx.client.Connection) ConnectionPoolSettings.defaultConnectionPoolSettings(com.hotels.styx.api.extension.service.ConnectionPoolSettings.defaultConnectionPoolSettings) ConnectionPoolSettings(com.hotels.styx.api.extension.service.ConnectionPoolSettings) Test(org.junit.jupiter.api.Test)

Example 3 with ConnectionPoolSettings

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);
}
Also used : Origin(com.hotels.styx.api.extension.Origin) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) AnswersWithDelay(org.mockito.internal.stubbing.answers.AnswersWithDelay) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) Connection(com.hotels.styx.client.Connection) ConnectionPoolSettings.defaultConnectionPoolSettings(com.hotels.styx.api.extension.service.ConnectionPoolSettings.defaultConnectionPoolSettings) ConnectionPoolSettings(com.hotels.styx.api.extension.service.ConnectionPoolSettings) ConnectionSettings(com.hotels.styx.client.ConnectionSettings) Test(org.junit.jupiter.api.Test)

Example 4 with ConnectionPoolSettings

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);
}
Also used : Origin(com.hotels.styx.api.extension.Origin) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) ConnectionPoolSettings.defaultConnectionPoolSettings(com.hotels.styx.api.extension.service.ConnectionPoolSettings.defaultConnectionPoolSettings) ConnectionPoolSettings(com.hotels.styx.api.extension.service.ConnectionPoolSettings) ConnectionSettings(com.hotels.styx.client.ConnectionSettings) Test(org.junit.jupiter.api.Test)

Example 5 with ConnectionPoolSettings

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);
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) AnswersWithDelay(org.mockito.internal.stubbing.answers.AnswersWithDelay) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) ConnectionPoolSettings.defaultConnectionPoolSettings(com.hotels.styx.api.extension.service.ConnectionPoolSettings.defaultConnectionPoolSettings) ConnectionPoolSettings(com.hotels.styx.api.extension.service.ConnectionPoolSettings) Test(org.junit.jupiter.api.Test)

Aggregations

ConnectionPoolSettings (com.hotels.styx.api.extension.service.ConnectionPoolSettings)9 ConnectionPoolSettings.defaultConnectionPoolSettings (com.hotels.styx.api.extension.service.ConnectionPoolSettings.defaultConnectionPoolSettings)8 Test (org.junit.jupiter.api.Test)8 Connection (com.hotels.styx.client.Connection)6 Origin.newOriginBuilder (com.hotels.styx.api.extension.Origin.newOriginBuilder)5 Origin (com.hotels.styx.api.extension.Origin)4 ConnectionSettings (com.hotels.styx.client.ConnectionSettings)4 Mockito.doAnswer (org.mockito.Mockito.doAnswer)3 AnswersWithDelay (org.mockito.internal.stubbing.answers.AnswersWithDelay)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 Answer (org.mockito.stubbing.Answer)3 HttpConfig.newHttpConfigBuilder (com.hotels.styx.client.HttpConfig.newHttpConfigBuilder)1 Builder.httpRequestOperationFactoryBuilder (com.hotels.styx.client.HttpRequestOperationFactory.Builder.httpRequestOperationFactoryBuilder)1 OriginStatsFactory (com.hotels.styx.client.OriginStatsFactory)1 CachingOriginStatsFactory (com.hotels.styx.client.OriginStatsFactory.CachingOriginStatsFactory)1 OriginsInventory (com.hotels.styx.client.OriginsInventory)1 ConnectionPool (com.hotels.styx.client.connectionpool.ConnectionPool)1 OriginHealthStatusMonitor (com.hotels.styx.client.healthcheck.OriginHealthStatusMonitor)1