Search in sources :

Example 1 with OriginUnreachableException

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));
}
Also used : Support.requestContext(com.hotels.styx.support.Support.requestContext) Context(com.hotels.styx.api.HttpInterceptor.Context) OriginUnreachableException(com.hotels.styx.api.exceptions.OriginUnreachableException) InOrder(org.mockito.InOrder) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) LoadBalancer(com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) RetryPolicy(com.hotels.styx.api.extension.retrypolicy.spi.RetryPolicy) Test(org.junit.jupiter.api.Test)

Example 2 with OriginUnreachableException

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));
}
Also used : Support.requestContext(com.hotels.styx.support.Support.requestContext) Context(com.hotels.styx.api.HttpInterceptor.Context) OriginUnreachableException(com.hotels.styx.api.exceptions.OriginUnreachableException) InOrder(org.mockito.InOrder) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) Test(org.junit.jupiter.api.Test)

Example 3 with OriginUnreachableException

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);
}
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 4 with OriginUnreachableException

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));
}
Also used : Support.requestContext(com.hotels.styx.support.Support.requestContext) Context(com.hotels.styx.api.HttpInterceptor.Context) OriginUnreachableException(com.hotels.styx.api.exceptions.OriginUnreachableException) InOrder(org.mockito.InOrder) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) Test(org.junit.jupiter.api.Test)

Example 5 with OriginUnreachableException

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();
}
Also used : OriginUnreachableException(com.hotels.styx.api.exceptions.OriginUnreachableException) Test(org.junit.jupiter.api.Test)

Aggregations

OriginUnreachableException (com.hotels.styx.api.exceptions.OriginUnreachableException)9 Test (org.junit.jupiter.api.Test)7 Context (com.hotels.styx.api.HttpInterceptor.Context)3 Origin (com.hotels.styx.api.extension.Origin)3 Origin.newOriginBuilder (com.hotels.styx.api.extension.Origin.newOriginBuilder)3 Support.requestContext (com.hotels.styx.support.Support.requestContext)3 InOrder (org.mockito.InOrder)3 ConnectionSettings (com.hotels.styx.client.ConnectionSettings)2 Id (com.hotels.styx.api.Id)1 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)1 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)1 NoAvailableHostsException (com.hotels.styx.api.exceptions.NoAvailableHostsException)1 ResponseTimeoutException (com.hotels.styx.api.exceptions.ResponseTimeoutException)1 LoadBalancer (com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer)1 RetryPolicy (com.hotels.styx.api.extension.retrypolicy.spi.RetryPolicy)1 PluginException (com.hotels.styx.api.plugins.spi.PluginException)1 BadHttpResponseException (com.hotels.styx.client.BadHttpResponseException)1 MaxPendingConnectionTimeoutException (com.hotels.styx.client.connectionpool.MaxPendingConnectionTimeoutException)1 MaxPendingConnectionsExceededException (com.hotels.styx.client.connectionpool.MaxPendingConnectionsExceededException)1 ChannelFuture (io.netty.channel.ChannelFuture)1