use of com.hotels.styx.api.extension.retrypolicy.spi.RetryPolicy 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.extension.retrypolicy.spi.RetryPolicy in project styx by ExpediaGroup.
the class StyxBackendServiceClientTest method constructsRetryContextWhenLoadBalancerDoesNotFindAvailableOrigins.
@Test
public void constructsRetryContextWhenLoadBalancerDoesNotFindAvailableOrigins() {
RetryPolicy retryPolicy = mockRetryPolicy(true, true, true);
StyxHostHttpClient hostClient = mockHostClient(Flux.just(response(OK).build()));
StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).metrics(metrics).loadBalancer(mockLoadBalancer(Optional.empty(), Optional.of(remoteHost(SOME_ORIGIN, toHandler(hostClient), hostClient)))).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(), is(Optional.empty()));
assertThat(lbPreference.getValue(), notNullValue());
assertThat(lbContext.getValue().avoidOrigins(), is(empty()));
assertThat(lbContext.getValue().preferredOrigins(), is(Optional.empty()));
assertThat(response.status(), is(OK));
}
use of com.hotels.styx.api.extension.retrypolicy.spi.RetryPolicy in project styx by ExpediaGroup.
the class StyxBackendServiceClientTest method updatedRequestWithUpdatedHostHeaderIsPresentInResponseWhenOverrideHostHeaderIsTrue.
@Test
public void updatedRequestWithUpdatedHostHeaderIsPresentInResponseWhenOverrideHostHeaderIsTrue() {
HttpInterceptor.Context requestContext = requestContext();
StyxHostHttpClient hostClient = mock(StyxHostHttpClient.class);
HttpHandler httpHandler = mock(HttpHandler.class);
Origin origin = newOriginBuilder(updatedHostName, 9090).applicationId(GENERIC_APP).build();
RemoteHost remoteHost = remoteHost(origin, httpHandler, hostClient);
LoadBalancer loadBalancer = mockLoadBalancer(Optional.of(remoteHost));
when(httpHandler.handle(any(), any())).thenReturn(Eventual.of(testResponse));
StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).originStatsFactory(mock(OriginStatsFactory.class)).originsRestrictionCookieName("someCookie").originIdHeader("origin-id").loadBalancer(loadBalancer).retryPolicy(new RetryNTimes(0)).metrics(metrics).overrideHostHeader(true).build();
Publisher<LiveHttpResponse> responsePublisher = styxHttpClient.sendRequest(testRequest, requestContext);
StepVerifier.create(responsePublisher).expectNextMatches(it -> it.request().header(HttpHeaderNames.HOST).isPresent() && it.request().header(HttpHeaderNames.HOST).get().equals(updatedHostName)).verifyComplete();
}
use of com.hotels.styx.api.extension.retrypolicy.spi.RetryPolicy in project styx by ExpediaGroup.
the class StyxBackendServiceClientTest method originalRequestIsPresentInResponseWhenOverrideHostHeaderIsFalse.
@Test
public void originalRequestIsPresentInResponseWhenOverrideHostHeaderIsFalse() {
HttpInterceptor.Context requestContext = requestContext();
StyxHostHttpClient hostClient = mock(StyxHostHttpClient.class);
HttpHandler httpHandler = mock(HttpHandler.class);
Origin origin = newOriginBuilder(incomingHostname, 9090).applicationId(GENERIC_APP).build();
RemoteHost remoteHost = remoteHost(origin, httpHandler, hostClient);
LoadBalancer loadBalancer = mockLoadBalancer(Optional.of(remoteHost));
when(httpHandler.handle(any(), any())).thenReturn(Eventual.of(testResponse));
StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).originStatsFactory(mock(OriginStatsFactory.class)).originsRestrictionCookieName("someCookie").originIdHeader("origin-id").loadBalancer(loadBalancer).retryPolicy(new RetryNTimes(0)).metrics(metrics).overrideHostHeader(false).build();
Publisher<LiveHttpResponse> responsePublisher = styxHttpClient.sendRequest(testRequest, requestContext);
StepVerifier.create(responsePublisher).expectNextMatches(it -> it.request().header(HttpHeaderNames.HOST).isPresent() && it.request().header(HttpHeaderNames.HOST).get().equals(incomingHostname)).verifyComplete();
}
use of com.hotels.styx.api.extension.retrypolicy.spi.RetryPolicy in project styx by ExpediaGroup.
the class RetryPolicyFactoryTest method usesDefaultCountOf1IfNotSpecified.
@Test
public void usesDefaultCountOf1IfNotSpecified() {
RetryPolicy retryPolicy = new RetryPolicyFactory().create(environment, EMPTY_CONFIGURATION);
assertThat(retryPolicy, is(instanceOf(RetryNTimes.class)));
assertThat(((RetryNTimes) retryPolicy).maxAttempts(), is(1));
}
Aggregations