Search in sources :

Example 6 with LoadBalancer

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;
}
Also used : LoadBalancer(com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer)

Example 7 with LoadBalancer

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

Example 8 with LoadBalancer

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));
}
Also used : Origin(com.hotels.styx.api.extension.Origin) RetryNTimes(com.hotels.styx.client.retry.RetryNTimes) Context(com.hotels.styx.api.HttpInterceptor.Context) HttpHandler(com.hotels.styx.api.HttpHandler) RemoteHost(com.hotels.styx.api.extension.RemoteHost) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) LoadBalancer(com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer) Test(org.junit.jupiter.api.Test)

Example 9 with LoadBalancer

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")));
}
Also used : Origin(com.hotels.styx.api.extension.Origin) LoadBalancer(com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 10 with LoadBalancer

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")));
}
Also used : Origin(com.hotels.styx.api.extension.Origin) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) LoadBalancer(com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

LoadBalancer (com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer)11 Test (org.junit.jupiter.api.Test)10 Origin.newOriginBuilder (com.hotels.styx.api.extension.Origin.newOriginBuilder)8 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)7 Origin (com.hotels.styx.api.extension.Origin)7 Context (com.hotels.styx.api.HttpInterceptor.Context)6 HttpHandler (com.hotels.styx.api.HttpHandler)4 HttpInterceptor (com.hotels.styx.api.HttpInterceptor)4 RemoteHost (com.hotels.styx.api.extension.RemoteHost)4 RetryPolicy (com.hotels.styx.api.extension.retrypolicy.spi.RetryPolicy)4 RetryNTimes (com.hotels.styx.client.retry.RetryNTimes)4 Support.requestContext (com.hotels.styx.support.Support.requestContext)4 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)3 OriginUnreachableException (com.hotels.styx.api.exceptions.OriginUnreachableException)3 InOrder (org.mockito.InOrder)3 HostAndPort (com.google.common.net.HostAndPort)2 Eventual (com.hotels.styx.api.Eventual)2 HttpHeaderNames (com.hotels.styx.api.HttpHeaderNames)2 CHUNKED (com.hotels.styx.api.HttpHeaderNames.CHUNKED)2 CONTENT_LENGTH (com.hotels.styx.api.HttpHeaderNames.CONTENT_LENGTH)2