Search in sources :

Example 16 with Origin

use of com.hotels.styx.api.extension.Origin in project styx by ExpediaGroup.

the class OriginTest method rejectsIfThereAreDuplicateHosts.

@Test
public void rejectsIfThereAreDuplicateHosts() {
    Origin origin1 = newOriginBuilder("localhost", 8080).applicationId("webapp").id("origin-01").build();
    Origin origin2 = newOriginBuilder("localhost", 8081).applicationId("webapp").id("origin-02").build();
    Origin origin3 = newOriginBuilder("localhost", 8081).applicationId("webapp").id("origin-03").build();
    assertThrows(IllegalArgumentException.class, () -> checkThatOriginsAreDistinct(List.of(origin1, origin2, origin3)));
}
Also used : Origin(com.hotels.styx.api.extension.Origin) Test(org.junit.jupiter.api.Test)

Example 17 with Origin

use of com.hotels.styx.api.extension.Origin in project styx by ExpediaGroup.

the class StyxHostHttpClientTest method mockPool.

ConnectionPool mockPool(Connection connection) {
    ConnectionPool pool = mock(ConnectionPool.class);
    when(pool.borrowConnection()).thenReturn(Flux.just(connection));
    Origin origin = mockOrigin("mockorigin");
    when(pool.getOrigin()).thenReturn(origin);
    return pool;
}
Also used : ConnectionPool(com.hotels.styx.client.connectionpool.ConnectionPool) Origin(com.hotels.styx.api.extension.Origin)

Example 18 with Origin

use of com.hotels.styx.api.extension.Origin 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 19 with Origin

use of com.hotels.styx.api.extension.Origin 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 20 with Origin

use of com.hotels.styx.api.extension.Origin in project styx by ExpediaGroup.

the class SimpleConnectionPoolTest method borrowGivesUpConnectionEstablishmentAttemptAfterThreeTries.

@Test
public void borrowGivesUpConnectionEstablishmentAttemptAfterThreeTries() {
    when(connectionFactory.createConnection(any(Origin.class), any(ConnectionSettings.class))).thenReturn(Mono.error(new OriginUnreachableException(origin, new RuntimeException()))).thenReturn(Mono.error(new OriginUnreachableException(origin, new RuntimeException()))).thenReturn(Mono.error(new OriginUnreachableException(origin, new RuntimeException())));
    SimpleConnectionPool pool = new SimpleConnectionPool(origin, defaultConnectionPoolSettings(), connectionFactory);
    Mono.from(pool.borrowConnection()).subscribe();
    assertEquals(pool.stats().pendingConnectionCount(), 1);
    assertEquals(pool.stats().connectionFailures(), 1);
    assertEquals(pool.stats().availableConnectionCount(), 0);
    assertEquals(pool.stats().closedConnections(), 0);
    assertEquals(pool.stats().terminatedConnections(), 0);
}
Also used : Origin(com.hotels.styx.api.extension.Origin) OriginUnreachableException(com.hotels.styx.api.exceptions.OriginUnreachableException) ConnectionSettings(com.hotels.styx.client.ConnectionSettings) Test(org.junit.jupiter.api.Test)

Aggregations

Origin (com.hotels.styx.api.extension.Origin)49 Test (org.junit.jupiter.api.Test)40 RemoteHost (com.hotels.styx.api.extension.RemoteHost)15 ActiveOrigins (com.hotels.styx.api.extension.ActiveOrigins)11 Origin.newOriginBuilder (com.hotels.styx.api.extension.Origin.newOriginBuilder)11 Random (java.util.Random)9 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)8 LoadBalancer (com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer)8 HttpHandler (com.hotels.styx.api.HttpHandler)6 BackendService (com.hotels.styx.api.extension.service.BackendService)6 ConnectionSettings (com.hotels.styx.client.ConnectionSettings)6 Id (com.hotels.styx.api.Id)5 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)5 OriginUnreachableException (com.hotels.styx.api.exceptions.OriginUnreachableException)5 DisableOrigin (com.hotels.styx.client.origincommands.DisableOrigin)5 EnableOrigin (com.hotels.styx.client.origincommands.EnableOrigin)5 CentralisedMetrics (com.hotels.styx.metrics.CentralisedMetrics)5 EventBus (com.google.common.eventbus.EventBus)4 HttpInterceptor (com.hotels.styx.api.HttpInterceptor)4 Context (com.hotels.styx.api.HttpInterceptor.Context)4