Search in sources :

Example 6 with Connection

use of com.hotels.styx.client.Connection in project styx by ExpediaGroup.

the class NettyConnectionTest method notifiesListenersWhenConnectionIsClosed.

@Test
public void notifiesListenersWhenConnectionIsClosed() throws Exception {
    Connection connection = createConnection();
    EventCapturingListener listener = new EventCapturingListener();
    connection.addConnectionListener(listener);
    channel.close();
    assertThat(connection.isConnected(), is(false));
    assertThat(listener.closedConnection(), isValue(connection));
}
Also used : Connection(com.hotels.styx.client.Connection) Test(org.junit.jupiter.api.Test)

Example 7 with Connection

use of com.hotels.styx.client.Connection in project styx by ExpediaGroup.

the class SimpleConnectionPoolTest method emitsExceptionWhenPendingConnectionTimesOut.

@Test
public void emitsExceptionWhenPendingConnectionTimesOut() {
    EmitterProcessor<Connection> processor = EmitterProcessor.create();
    when(connectionFactory.createConnection(any(Origin.class), any(ConnectionSettings.class))).thenReturn(Mono.from(processor));
    ConnectionPoolSettings poolSettings = new ConnectionPoolSettings.Builder().pendingConnectionTimeout(500, MILLISECONDS).build();
    SimpleConnectionPool pool = new SimpleConnectionPool(origin, poolSettings, connectionFactory);
    StepVerifier.create(pool.borrowConnection()).expectError(MaxPendingConnectionTimeoutException.class).verifyThenAssertThat().tookMoreThan(Duration.ofMillis(500));
    // And then ensure connection is placed in the active queue:
    processor.onNext(mock(Connection.class));
    assertEquals(pool.stats().availableConnectionCount(), 1);
    assertEquals(pool.stats().pendingConnectionCount(), 0);
    assertEquals(pool.stats().busyConnectionCount(), 0);
    assertEquals(pool.stats().connectionAttempts(), 1);
}
Also used : Origin(com.hotels.styx.api.extension.Origin) 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 8 with Connection

use of com.hotels.styx.client.Connection in project styx by ExpediaGroup.

the class ExpiringConnectionTest method shouldExpireConnection.

@Test
public void shouldExpireConnection() {
    Connection trackedConnection = new StubConnectionFactory.StubConnection(null);
    ExpiringConnection connectionTracker = new ExpiringConnection(trackedConnection, 2, new OneSecondPerTickClock());
    assertThat(connectionTracker.isConnected(), is(true));
    assertThat(connectionTracker.isConnected(), is(false));
}
Also used : Connection(com.hotels.styx.client.Connection) Test(org.junit.jupiter.api.Test)

Example 9 with Connection

use of com.hotels.styx.client.Connection in project styx by ExpediaGroup.

the class SimpleConnectionPoolStressTest method canRoundTripBorrowedConnectionsFromMultipleThreads.

@Test
public void canRoundTripBorrowedConnectionsFromMultipleThreads() throws InterruptedException {
    MultithreadedStressTester stressTester = new MultithreadedStressTester(10, 250);
    Random returnOrClose = new Random();
    stressTester.stress(() -> {
        Connection connection = borrowConnectionSynchronously();
        if (returnOrClose.nextBoolean()) {
            releaseConnection(connection);
        } else {
            closeConnection(connection);
        }
    });
    stressTester.shutdown();
    assertThat("final busy connection count", pool.stats().busyConnectionCount(), is(0));
    assertThat("final available connection count", pool.stats().availableConnectionCount(), is(greaterThanOrEqualTo(0)));
}
Also used : Random(java.util.Random) Connection(com.hotels.styx.client.Connection) MultithreadedStressTester(com.hotels.styx.support.MultithreadedStressTester) Test(org.junit.jupiter.api.Test)

Example 10 with Connection

use of com.hotels.styx.client.Connection in project styx by ExpediaGroup.

the class SimpleConnectionPool method close.

@Override
public void close() {
    active = false;
    Connection con;
    while ((con = availableConnections.poll()) != null) {
        if (con.isConnected()) {
            doCloseConnection(con);
        }
    }
}
Also used : Connection(com.hotels.styx.client.Connection)

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