Search in sources :

Example 16 with RemoteHost

use of com.hotels.styx.api.extension.RemoteHost 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 17 with RemoteHost

use of com.hotels.styx.api.extension.RemoteHost in project styx by ExpediaGroup.

the class BusyConnectionsStrategyTest method tieBreaksOriginsWithEqualMetric.

@Test
public void tieBreaksOriginsWithEqualMetric() {
    RemoteHost hostOne = remoteHost(ORIGIN_ONE, mock(HttpHandler.class), lbMetrics(3));
    RemoteHost hostTwo = remoteHost(ORIGIN_TWO, mock(HttpHandler.class), lbMetrics(3));
    RemoteHost hostThree = remoteHost(ORIGIN_THREE, mock(HttpHandler.class), lbMetrics(3));
    when(activeOrigins.snapshot()).thenReturn(asList(hostOne, hostTwo, hostThree));
    List<RemoteHost> results = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        results.add(strategy.choose(null).get());
    }
    assertThat(count(hostOne, results), greaterThan(10L));
    assertThat(count(hostTwo, results), greaterThan(10L));
    assertThat(count(hostThree, results), greaterThan(10L));
}
Also used : HttpHandler(com.hotels.styx.api.HttpHandler) RemoteHost(com.hotels.styx.api.extension.RemoteHost) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 18 with RemoteHost

use of com.hotels.styx.api.extension.RemoteHost in project styx by ExpediaGroup.

the class BusyConnectionsStrategyTest method favoursOriginsWithLessBusyConnectionCount.

@Test
public void favoursOriginsWithLessBusyConnectionCount() {
    RemoteHost hostOne = remoteHost(ORIGIN_ONE, mock(HttpHandler.class), lbMetrics(4));
    RemoteHost hostTwo = remoteHost(ORIGIN_TWO, mock(HttpHandler.class), lbMetrics(3));
    RemoteHost hostThree = remoteHost(ORIGIN_THREE, mock(HttpHandler.class), lbMetrics(6));
    when(activeOrigins.snapshot()).thenReturn(asList(hostOne, hostTwo, hostThree));
    Optional<RemoteHost> sortedPool = strategy.choose(null);
    assertThat(sortedPool, is(Optional.of(hostTwo)));
}
Also used : HttpHandler(com.hotels.styx.api.HttpHandler) RemoteHost(com.hotels.styx.api.extension.RemoteHost) Test(org.junit.jupiter.api.Test)

Example 19 with RemoteHost

use of com.hotels.styx.api.extension.RemoteHost in project styx by ExpediaGroup.

the class PowerOfTwoStrategyTest method choosesBetterOfTwoRandomChoices.

@Test
public void choosesBetterOfTwoRandomChoices() {
    ActiveOrigins activeOrigins = mock(ActiveOrigins.class);
    when(activeOrigins.snapshot()).thenReturn(allOrigins);
    Random rng = new Random(RNG_SEED);
    int first = rng.nextInt(4);
    assert (first == 2);
    int second = rng.nextInt(4);
    assert (second == 0);
    PowerOfTwoStrategy loadBalancer = new PowerOfTwoStrategy(activeOrigins, new Random(RNG_SEED));
    Optional<RemoteHost> chosenOne = loadBalancer.choose(mock(LoadBalancer.Preferences.class));
    assertThat(chosenOne.get(), is(betterOf(allOrigins.get(first), allOrigins.get(second))));
}
Also used : ActiveOrigins(com.hotels.styx.api.extension.ActiveOrigins) RemoteHost(com.hotels.styx.api.extension.RemoteHost) Random(java.util.Random) Test(org.junit.jupiter.api.Test)

Example 20 with RemoteHost

use of com.hotels.styx.api.extension.RemoteHost in project styx by ExpediaGroup.

the class PowerOfTwoStrategyTest method choosesSoleOriginOutOfOne.

@Test
public void choosesSoleOriginOutOfOne() {
    ActiveOrigins activeOrigins = mock(ActiveOrigins.class);
    when(activeOrigins.snapshot()).thenReturn(asList(HOST_ONE));
    PowerOfTwoStrategy loadBalancer = new PowerOfTwoStrategy(activeOrigins, new Random(RNG_SEED));
    for (int i = 0; i < 10; i++) {
        Optional<RemoteHost> chosenOne = loadBalancer.choose(mock(LoadBalancer.Preferences.class));
        assertThat(chosenOne.get(), is(HOST_ONE));
    }
}
Also used : ActiveOrigins(com.hotels.styx.api.extension.ActiveOrigins) RemoteHost(com.hotels.styx.api.extension.RemoteHost) Random(java.util.Random) Test(org.junit.jupiter.api.Test)

Aggregations

RemoteHost (com.hotels.styx.api.extension.RemoteHost)20 Test (org.junit.jupiter.api.Test)19 Origin (com.hotels.styx.api.extension.Origin)14 ActiveOrigins (com.hotels.styx.api.extension.ActiveOrigins)13 Random (java.util.Random)12 HttpHandler (com.hotels.styx.api.HttpHandler)7 LoadBalancer (com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer)5 HttpInterceptor (com.hotels.styx.api.HttpInterceptor)4 Context (com.hotels.styx.api.HttpInterceptor.Context)4 Origin.newOriginBuilder (com.hotels.styx.api.extension.Origin.newOriginBuilder)4 RetryNTimes (com.hotels.styx.client.retry.RetryNTimes)4 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)3 BeforeEach (org.junit.jupiter.api.BeforeEach)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 TRANSFER_ENCODING (com.hotels.styx.api.HttpHeaderNames.TRANSFER_ENCODING)2 HttpResponseStatus (com.hotels.styx.api.HttpResponseStatus)2