use of com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer in project styx by ExpediaGroup.
the class StyxBackendServiceClientTest method mockLoadBalancer.
private LoadBalancer mockLoadBalancer(Optional<RemoteHost> first, Optional<RemoteHost>... remoteHostWrappers) {
LoadBalancer lbStategy = mock(LoadBalancer.class);
when(lbStategy.choose(any(LoadBalancer.Preferences.class))).thenReturn(first, remoteHostWrappers);
return lbStategy;
}
use of com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer in project styx by ExpediaGroup.
the class StickySessionLoadBalancingStrategyTest method dontDelegateWhenPreferredOriginIsFound.
@Test
public void dontDelegateWhenPreferredOriginIsFound() {
LoadBalancer delegate = mock(LoadBalancer.class);
StickySessionLoadBalancingStrategy aStrategy = new StickySessionLoadBalancingStrategy(activeOrigins, delegate);
when(activeOrigins.snapshot()).thenReturn(asList(ORIGIN_0, ORIGIN_1, ORIGIN_2));
aStrategy.choose(lbPreferences(Optional.of(ORIGIN_1.id())));
verify(delegate, never()).choose(any(LoadBalancer.Preferences.class));
}
use of com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer in project styx by ExpediaGroup.
the class StyxBackendServiceClientTest method hostHeaderIsOverwrittenWhenOverrideHostHeaderIsTrue.
@Test
public void hostHeaderIsOverwrittenWhenOverrideHostHeaderIsTrue() {
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();
styxHttpClient.sendRequest(testRequest, requestContext);
ArgumentCaptor<LiveHttpRequest> updatedRequest = ArgumentCaptor.forClass(LiveHttpRequest.class);
verify(httpHandler).handle(updatedRequest.capture(), eq(requestContext));
assertThat(updatedRequest.getValue().header(HttpHeaderNames.HOST).get(), is(updatedHostName));
}
use of com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer in project styx by ExpediaGroup.
the class StyxBackendServiceClientTest method prefersStickyOrigins.
@Test
public void prefersStickyOrigins() {
Origin origin = originWithId("localhost:234", "App-X", "Origin-Y");
StyxHostHttpClient hostClient = mockHostClient(Flux.just(response(OK).build()));
LoadBalancer loadBalancer = mockLoadBalancer(Optional.of(remoteHost(origin, toHandler(hostClient), hostClient)));
StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).loadBalancer(loadBalancer).metrics(metrics).build();
LiveHttpResponse response = Mono.from(styxHttpClient.sendRequest(get("/foo").cookies(requestCookie("styx_origin_" + Id.GENERIC_APP, "Origin-Y")).build(), requestContext())).block();
assertThat(response.status(), is(OK));
ArgumentCaptor<LoadBalancer.Preferences> argCaptor = ArgumentCaptor.forClass(LoadBalancer.Preferences.class);
verify(loadBalancer).choose(argCaptor.capture());
assertThat(argCaptor.getValue().preferredOrigins(), is(Optional.of("Origin-Y")));
}
use of com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer in project styx by ExpediaGroup.
the class StyxBackendServiceClientTest method prefersRestrictedOrigins.
@Test
public void prefersRestrictedOrigins() {
Origin origin = originWithId("localhost:234", "App-X", "Origin-Y");
StyxHostHttpClient hostClient = mockHostClient(Flux.just(response(OK).build()));
LoadBalancer loadBalancer = mockLoadBalancer(Optional.of(remoteHost(origin, toHandler(hostClient), hostClient)));
StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).loadBalancer(loadBalancer).metrics(metrics).originsRestrictionCookieName("restrictedOrigin").build();
LiveHttpResponse response = Mono.from(styxHttpClient.sendRequest(get("/foo").cookies(requestCookie("restrictedOrigin", "Origin-Y")).build(), requestContext())).block();
assertThat(response.status(), is(OK));
ArgumentCaptor<LoadBalancer.Preferences> argCaptor = ArgumentCaptor.forClass(LoadBalancer.Preferences.class);
verify(loadBalancer).choose(argCaptor.capture());
assertThat(argCaptor.getValue().preferredOrigins(), is(Optional.of("Origin-Y")));
}
Aggregations