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"));
}
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();
}
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"));
}
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"));
}
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"));
}
Aggregations