Search in sources :

Example 36 with Origin

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

the class StyxHostHttpClientTest method mockOrigin.

Origin mockOrigin(String id) {
    Origin origin = mock(Origin.class);
    when(origin.id()).thenReturn(Id.id(id));
    return origin;
}
Also used : Origin(com.hotels.styx.api.extension.Origin)

Example 37 with Origin

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

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

the class SimpleConnectionPoolTest method borrowRetriesThreeTimesOnConnectionEstablishmentFailure.

@Test
public void borrowRetriesThreeTimesOnConnectionEstablishmentFailure() {
    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.just(connection4));
    SimpleConnectionPool pool = new SimpleConnectionPool(origin, defaultConnectionPoolSettings(), connectionFactory);
    StepVerifier.create(pool.borrowConnection()).expectNext(connection4).verifyComplete();
}
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)

Example 39 with Origin

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

the class SimpleConnectionPoolTest method shouldNotHandoutConnectionToCancelledSubscriberWhenCreatingNewConnection.

@Test
public void shouldNotHandoutConnectionToCancelledSubscriberWhenCreatingNewConnection() throws Exception {
    when(connectionFactory.createConnection(any(Origin.class), any(ConnectionSettings.class))).thenReturn(Mono.just(connection1));
    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();
    assertEquals(pool.stats().availableConnectionCount(), 1);
    // Waiting subscribers
    assertEquals(pool.stats().pendingConnectionCount(), 0);
    // Borrowed count
    assertEquals(pool.stats().busyConnectionCount(), 0);
}
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) 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 40 with Origin

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

the class StyxHttpClientTest method sendsToDefaultHttpsPort.

/*
     * StyxHttpClient.sendRequestInternal
     * - Uses default HTTPS port 443 when not specified in Host header
     */
@Test
public void sendsToDefaultHttpsPort() {
    NettyConnectionFactory factory = mockConnectionFactory();
    ArgumentCaptor<Origin> originCaptor = ArgumentCaptor.forClass(Origin.class);
    StyxHttpClient.sendRequestInternal(factory, get("/").header(HOST, "localhost").build().stream(), new StyxHttpClient.Builder().secure(true));
    verify(factory).createConnection(originCaptor.capture(), any(ConnectionSettings.class), any(SslContext.class));
    assertThat(originCaptor.getValue().port(), is(443));
}
Also used : Origin(com.hotels.styx.api.extension.Origin) NettyConnectionFactory(com.hotels.styx.client.netty.connectionpool.NettyConnectionFactory) SslContext(io.netty.handler.ssl.SslContext) 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