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