Search in sources :

Example 6 with ConnectionPool

use of com.hotels.styx.client.connectionpool.ConnectionPool in project styx by ExpediaGroup.

the class StyxHostHttpClientTest method ignoresCancelledHeaders.

@Test
public void ignoresCancelledHeaders() {
    // Request observable unsubscribe/cancel has to be ignored after "onNext" event.
    // This is because Reactor Mono will automatically cancel after an event has
    // been published.
    Connection connection = mockConnection(just(response));
    ConnectionPool pool = mockPool(connection);
    Context context = mockContext();
    AtomicReference<LiveHttpResponse> transformedResponse = new AtomicReference<>();
    StyxHostHttpClient hostClient = new StyxHostHttpClient(pool);
    // The StepVerifier consumes the response event and then unsubscribes
    // from the response observable.
    StepVerifier.create(hostClient.sendRequest(request, context)).consumeNextWith(transformedResponse::set).verifyComplete();
    // The response is still alive...
    verify(pool, never()).returnConnection(any(Connection.class));
    verify(pool, never()).closeConnection(any(Connection.class));
    // ... until response body is consumed:
    transformedResponse.get().consume();
    // Finally, the connection is returned after the response body is fully consumed:
    verify(pool).returnConnection(any(Connection.class));
    verify(context).add(ORIGINID_CONTEXT_KEY, Id.id("mockorigin"));
}
Also used : ConnectionPool(com.hotels.styx.client.connectionpool.ConnectionPool) HttpInterceptorContext(com.hotels.styx.server.HttpInterceptorContext) Support.requestContext(com.hotels.styx.support.Support.requestContext) Context(com.hotels.styx.api.HttpInterceptor.Context) AtomicReference(java.util.concurrent.atomic.AtomicReference) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 7 with ConnectionPool

use of com.hotels.styx.client.connectionpool.ConnectionPool in project styx by ExpediaGroup.

the class OriginsInventoryTest method shutsConnectionPoolForRemovedOrigin.

@Test
public void shutsConnectionPoolForRemovedOrigin() {
    Origin originV1 = newOriginBuilder("acme01.com", 80).applicationId(GENERIC_APP).id("acme-01").build();
    Origin originV2 = newOriginBuilder("acme02.com", 80).applicationId(GENERIC_APP).id("acme-02").build();
    ConnectionPool.Factory connectionFactory = mock(ConnectionPool.Factory.class);
    ConnectionPool pool1 = mock(ConnectionPool.class);
    when(pool1.getOrigin()).thenReturn(originV1);
    ConnectionPool pool2 = mock(ConnectionPool.class);
    when(pool2.getOrigin()).thenReturn(originV2);
    when(connectionFactory.create(eq(originV1))).thenReturn(pool1);
    when(connectionFactory.create(eq(originV2))).thenReturn(pool2);
    inventory = new OriginsInventory(eventBus, GENERIC_APP, monitor, connectionFactory, hostClientFactory, new CentralisedMetrics(meterRegistry));
    inventory.setOrigins(originV1, originV2);
    inventory.setOrigins(originV2);
    verify(pool1).close();
}
Also used : DisableOrigin(com.hotels.styx.client.origincommands.DisableOrigin) EnableOrigin(com.hotels.styx.client.origincommands.EnableOrigin) Origin(com.hotels.styx.api.extension.Origin) ConnectionPool(com.hotels.styx.client.connectionpool.ConnectionPool) CentralisedMetrics(com.hotels.styx.metrics.CentralisedMetrics) Test(org.junit.jupiter.api.Test)

Example 8 with ConnectionPool

use of com.hotels.styx.client.connectionpool.ConnectionPool in project styx by ExpediaGroup.

the class StyxHostHttpClientTest method releasesConnectionWhenResponseFailsBeforeHeaders.

@Test
public void releasesConnectionWhenResponseFailsBeforeHeaders() {
    Connection connection = mockConnection(Flux.error(new RuntimeException()));
    ConnectionPool pool = mockPool(connection);
    Context context = mockContext();
    StyxHostHttpClient hostClient = new StyxHostHttpClient(pool);
    StepVerifier.create(hostClient.sendRequest(request, context)).expectNextCount(0).expectError().verify();
    verify(pool).closeConnection(any(Connection.class));
    verify(context).add(ORIGINID_CONTEXT_KEY, Id.id("mockorigin"));
}
Also used : ConnectionPool(com.hotels.styx.client.connectionpool.ConnectionPool) HttpInterceptorContext(com.hotels.styx.server.HttpInterceptorContext) Support.requestContext(com.hotels.styx.support.Support.requestContext) Context(com.hotels.styx.api.HttpInterceptor.Context) Test(org.junit.jupiter.api.Test)

Example 9 with ConnectionPool

use of com.hotels.styx.client.connectionpool.ConnectionPool in project styx by ExpediaGroup.

the class StyxHostHttpClientTest method releasesIfRequestIsCancelledBeforeHeaders.

@Test
public void releasesIfRequestIsCancelledBeforeHeaders() {
    Connection connection = mockConnection(EmitterProcessor.create());
    ConnectionPool pool = mockPool(connection);
    Context context = mockContext();
    StyxHostHttpClient hostClient = new StyxHostHttpClient(pool);
    AtomicReference<Subscription> subscription = new AtomicReference<>();
    Flux.from(hostClient.sendRequest(request, context)).subscribe(new BaseSubscriber<LiveHttpResponse>() {

        @Override
        protected void hookOnSubscribe(Subscription s) {
            super.hookOnSubscribe(s);
            s.request(1);
            subscription.set(s);
        }
    });
    subscription.get().cancel();
    verify(pool).closeConnection(any(Connection.class));
    verify(context).add(ORIGINID_CONTEXT_KEY, Id.id("mockorigin"));
}
Also used : ConnectionPool(com.hotels.styx.client.connectionpool.ConnectionPool) HttpInterceptorContext(com.hotels.styx.server.HttpInterceptorContext) Support.requestContext(com.hotels.styx.support.Support.requestContext) Context(com.hotels.styx.api.HttpInterceptor.Context) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscription(org.reactivestreams.Subscription) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 10 with ConnectionPool

use of com.hotels.styx.client.connectionpool.ConnectionPool in project styx by ExpediaGroup.

the class StyxHostHttpClientTest method ignoresResponseObservableErrorsAfterHeaders.

@Test
public void ignoresResponseObservableErrorsAfterHeaders() {
    Connection connection = mockConnection(responseProvider);
    ConnectionPool pool = mockPool(connection);
    Context context = mockContext();
    AtomicReference<LiveHttpResponse> newResponse = new AtomicReference<>();
    StyxHostHttpClient hostClient = new StyxHostHttpClient(pool);
    StepVerifier.create(hostClient.sendRequest(request, context)).then(() -> {
        responseProvider.onNext(response);
        responseProvider.onError(new RuntimeException("oh dear ..."));
    }).consumeNextWith(newResponse::set).expectError().verify();
    newResponse.get().consume();
    verify(pool).returnConnection(any(Connection.class));
    verify(context).add(ORIGINID_CONTEXT_KEY, Id.id("mockorigin"));
}
Also used : ConnectionPool(com.hotels.styx.client.connectionpool.ConnectionPool) HttpInterceptorContext(com.hotels.styx.server.HttpInterceptorContext) Support.requestContext(com.hotels.styx.support.Support.requestContext) Context(com.hotels.styx.api.HttpInterceptor.Context) AtomicReference(java.util.concurrent.atomic.AtomicReference) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

ConnectionPool (com.hotels.styx.client.connectionpool.ConnectionPool)14 Test (org.junit.jupiter.api.Test)11 Context (com.hotels.styx.api.HttpInterceptor.Context)7 HttpInterceptorContext (com.hotels.styx.server.HttpInterceptorContext)7 Support.requestContext (com.hotels.styx.support.Support.requestContext)7 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Origin (com.hotels.styx.api.extension.Origin)3 CentralisedMetrics (com.hotels.styx.metrics.CentralisedMetrics)3 DisableOrigin (com.hotels.styx.client.origincommands.DisableOrigin)2 EnableOrigin (com.hotels.styx.client.origincommands.EnableOrigin)2 Buffer (com.hotels.styx.api.Buffer)1 ByteStream (com.hotels.styx.api.ByteStream)1 HttpHandler (com.hotels.styx.api.HttpHandler)1 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)1 OriginsSnapshot (com.hotels.styx.api.extension.OriginsSnapshot)1 LoadBalancingMetricSupplier (com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancingMetricSupplier)1 Subscription (org.reactivestreams.Subscription)1