Search in sources :

Example 1 with Connection

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);
}
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 Connection

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);
}
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 Connection

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);
}
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 Connection

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);
}
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 5 with Connection

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

Aggregations

Connection (com.hotels.styx.client.Connection)10 Test (org.junit.jupiter.api.Test)9 ConnectionPoolSettings (com.hotels.styx.api.extension.service.ConnectionPoolSettings)5 ConnectionPoolSettings.defaultConnectionPoolSettings (com.hotels.styx.api.extension.service.ConnectionPoolSettings.defaultConnectionPoolSettings)5 Origin (com.hotels.styx.api.extension.Origin)2 Origin.newOriginBuilder (com.hotels.styx.api.extension.Origin.newOriginBuilder)2 ConnectionSettings (com.hotels.styx.client.ConnectionSettings)2 MultithreadedStressTester (com.hotels.styx.support.MultithreadedStressTester)1 Random (java.util.Random)1 Mockito.doAnswer (org.mockito.Mockito.doAnswer)1 AnswersWithDelay (org.mockito.internal.stubbing.answers.AnswersWithDelay)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1