use of com.hotels.styx.api.LiveHttpResponse in project styx by ExpediaGroup.
the class StyxBackendServiceClientTest method incrementsResponseStatusMetricsForBadResponse.
@Test
public void incrementsResponseStatusMetricsForBadResponse() {
StyxHostHttpClient hostClient = mockHostClient(Flux.just(response(BAD_REQUEST).build()));
StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).metrics(metrics).loadBalancer(mockLoadBalancer(Optional.of(remoteHost(SOME_ORIGIN, toHandler(hostClient), hostClient)))).build();
LiveHttpResponse response = Mono.from(styxHttpClient.sendRequest(SOME_REQ, requestContext())).block();
assertThat(response.status(), is(BAD_REQUEST));
verify(hostClient).sendRequest(eq(SOME_REQ), any(Context.class));
assertThat(meterRegistry.get("proxy.client.responseCode.errorStatus").tag("statusCode", "400").counter(), is(notNullValue()));
}
use of com.hotels.styx.api.LiveHttpResponse in project styx by ExpediaGroup.
the class StyxBackendServiceClientTest method sendsRequestToHostChosenByLoadBalancer.
@Test
public void sendsRequestToHostChosenByLoadBalancer() {
StyxHostHttpClient hostClient = mockHostClient(Flux.just(response(OK).build()));
StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).metrics(metrics).loadBalancer(mockLoadBalancer(Optional.of(remoteHost(SOME_ORIGIN, toHandler(hostClient), hostClient)))).build();
LiveHttpResponse response = Mono.from(styxHttpClient.sendRequest(SOME_REQ, requestContext())).block();
assertThat(response.status(), is(OK));
verify(hostClient).sendRequest(eq(SOME_REQ), any(Context.class));
}
use of com.hotels.styx.api.LiveHttpResponse in project styx by ExpediaGroup.
the class StyxBackendServiceClientTest method updatesCountersWhenTransactionIsCancelled.
@Test
public void updatesCountersWhenTransactionIsCancelled() {
Origin origin = originWithId("localhost:234", "App-X", "Origin-Y");
Processor<LiveHttpResponse, LiveHttpResponse> processor = EmitterProcessor.create();
StyxHostHttpClient hostClient = mockHostClient(processor);
StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).loadBalancer(mockLoadBalancer(Optional.of(remoteHost(origin, toHandler(hostClient), hostClient)))).metrics(metrics).build();
StepVerifier.create(styxHttpClient.sendRequest(SOME_REQ, requestContext())).thenCancel().verify();
assertThat(meterRegistry.get("proxy.client.requests.cancelled").tags("appId", "App-X", "originId", "Origin-Y").counter().count(), is(1.0));
}
use of com.hotels.styx.api.LiveHttpResponse 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.LiveHttpResponse 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));
}
Aggregations