use of com.hotels.styx.api.exceptions.OriginUnreachableException in project styx by ExpediaGroup.
the class StyxBackendServiceClientTest method retriesWhenRetryPolicyTellsToRetry.
@Test
public void retriesWhenRetryPolicyTellsToRetry() {
RetryPolicy retryPolicy = mockRetryPolicy(true, false);
StyxHostHttpClient firstClient = mockHostClient(Flux.error(new OriginUnreachableException(ORIGIN_1, new RuntimeException("An error occurred"))));
StyxHostHttpClient secondClient = mockHostClient(Flux.just(response(OK).build()));
StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).metrics(metrics).loadBalancer(mockLoadBalancer(Optional.of(remoteHost(ORIGIN_1, toHandler(firstClient), firstClient)), Optional.of(remoteHost(ORIGIN_2, toHandler(secondClient), secondClient)))).retryPolicy(retryPolicy).build();
LiveHttpResponse response = Mono.from(styxHttpClient.sendRequest(SOME_REQ, requestContext())).block();
ArgumentCaptor<RetryPolicy.Context> retryContext = ArgumentCaptor.forClass(RetryPolicy.Context.class);
ArgumentCaptor<LoadBalancer> lbPreference = ArgumentCaptor.forClass(LoadBalancer.class);
ArgumentCaptor<LoadBalancer.Preferences> lbContext = ArgumentCaptor.forClass(LoadBalancer.Preferences.class);
verify(retryPolicy).evaluate(retryContext.capture(), lbPreference.capture(), lbContext.capture());
assertThat(retryContext.getValue().appId(), is(backendService.id()));
assertThat(retryContext.getValue().currentRetryCount(), is(1));
assertThat(retryContext.getValue().lastException().get(), Matchers.instanceOf(OriginUnreachableException.class));
assertThat(lbPreference.getValue(), notNullValue());
assertThat(lbContext.getValue().preferredOrigins(), is(Optional.empty()));
assertThat(lbContext.getValue().avoidOrigins(), is(asList(ORIGIN_1)));
assertThat(response.status(), is(OK));
InOrder ordered = inOrder(firstClient, secondClient);
ordered.verify(firstClient).sendRequest(eq(SOME_REQ), any(Context.class));
ordered.verify(secondClient).sendRequest(eq(SOME_REQ), any(Context.class));
}
use of com.hotels.styx.api.exceptions.OriginUnreachableException in project styx by ExpediaGroup.
the class StyxBackendServiceClientTest method stopsRetriesWhenRetryPolicyTellsToStop.
@Test
public void stopsRetriesWhenRetryPolicyTellsToStop() {
StyxHostHttpClient firstClient = mockHostClient(Flux.error(new OriginUnreachableException(ORIGIN_1, new RuntimeException("An error occurred"))));
StyxHostHttpClient secondClient = mockHostClient(Flux.error(new OriginUnreachableException(ORIGIN_2, new RuntimeException("An error occurred"))));
StyxHostHttpClient thirdClient = mockHostClient(Flux.error(new OriginUnreachableException(ORIGIN_2, new RuntimeException("An error occurred"))));
StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).metrics(metrics).loadBalancer(mockLoadBalancer(Optional.of(remoteHost(ORIGIN_1, toHandler(firstClient), firstClient)), Optional.of(remoteHost(ORIGIN_2, toHandler(secondClient), secondClient)), Optional.of(remoteHost(ORIGIN_3, toHandler(thirdClient), thirdClient)))).retryPolicy(mockRetryPolicy(true, false)).build();
StepVerifier.create(styxHttpClient.sendRequest(SOME_REQ, requestContext())).verifyError(OriginUnreachableException.class);
InOrder ordered = inOrder(firstClient, secondClient, thirdClient);
ordered.verify(firstClient).sendRequest(eq(SOME_REQ), any(Context.class));
ordered.verify(secondClient).sendRequest(eq(SOME_REQ), any(Context.class));
ordered.verify(thirdClient, never()).sendRequest(eq(SOME_REQ), any(Context.class));
}
use of com.hotels.styx.api.exceptions.OriginUnreachableException 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);
}
use of com.hotels.styx.api.exceptions.OriginUnreachableException in project styx by ExpediaGroup.
the class StyxBackendServiceClientTest method retriesAtMost3Times.
@Test
public void retriesAtMost3Times() {
StyxHostHttpClient firstClient = mockHostClient(Flux.error(new OriginUnreachableException(ORIGIN_1, new RuntimeException("An error occurred"))));
StyxHostHttpClient secondClient = mockHostClient(Flux.error(new OriginUnreachableException(ORIGIN_2, new RuntimeException("An error occurred"))));
StyxHostHttpClient thirdClient = mockHostClient(Flux.error(new OriginUnreachableException(ORIGIN_3, new RuntimeException("An error occurred"))));
StyxHostHttpClient fourthClient = mockHostClient(Flux.error(new OriginUnreachableException(ORIGIN_4, new RuntimeException("An error occurred"))));
StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).metrics(metrics).loadBalancer(mockLoadBalancer(Optional.of(remoteHost(ORIGIN_1, toHandler(firstClient), firstClient)), Optional.of(remoteHost(ORIGIN_2, toHandler(secondClient), secondClient)), Optional.of(remoteHost(ORIGIN_3, toHandler(thirdClient), thirdClient)), Optional.of(remoteHost(ORIGIN_4, toHandler(fourthClient), fourthClient)))).retryPolicy(mockRetryPolicy(true, true, true, true)).build();
StepVerifier.create(styxHttpClient.sendRequest(SOME_REQ, requestContext())).verifyError(NoAvailableHostsException.class);
InOrder ordered = inOrder(firstClient, secondClient, thirdClient, fourthClient);
ordered.verify(firstClient).sendRequest(eq(SOME_REQ), any(Context.class));
ordered.verify(secondClient).sendRequest(eq(SOME_REQ), any(Context.class));
ordered.verify(thirdClient).sendRequest(eq(SOME_REQ), any(Context.class));
ordered.verify(fourthClient, never()).sendRequest(any(LiveHttpRequest.class), any(Context.class));
}
use of com.hotels.styx.api.exceptions.OriginUnreachableException in project styx by ExpediaGroup.
the class SimpleConnectionPoolTest method borrowRetriesThreeTimesOnFailureDueToConnectionClosure.
@Test
public void borrowRetriesThreeTimesOnFailureDueToConnectionClosure() {
when(connectionFactory.createConnection(any(Origin.class), any(ConnectionSettings.class))).thenReturn(Mono.just(connection1)).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(connection1).then(() -> {
assertEquals(pool.stats().availableConnectionCount(), 0);
assertEquals(pool.stats().closedConnections(), 0);
assertEquals(pool.stats().pendingConnectionCount(), 0);
assertEquals(pool.stats().busyConnectionCount(), 1);
}).then(() -> pool.closeConnection(connection1)).then(() -> {
assertEquals(pool.stats().availableConnectionCount(), 1);
assertEquals(pool.stats().closedConnections(), 1);
assertEquals(pool.stats().pendingConnectionCount(), 0);
assertEquals(pool.stats().busyConnectionCount(), 0);
}).verifyComplete();
}
Aggregations