Search in sources :

Example 11 with RemoteHost

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

the class OriginRestrictionLoadBalancingStrategyTest method usesAllOriginsWhenCookieIsAbsent.

@Test
public void usesAllOriginsWhenCookieIsAbsent() {
    Random mockRandom = mock(Random.class);
    strategy = new OriginRestrictionLoadBalancingStrategy(new ActiveOrigins() {

        @Override
        public Iterable<RemoteHost> snapshot() {
            return origins;
        }

        @Override
        public String getApplicationId() {
            return null;
        }

        @Override
        public List<Origin> origins() {
            return null;
        }
    }, delegate, mockRandom);
    Optional<RemoteHost> chosenOne = strategy.choose(lbPreference(empty()));
    assertThat(chosenOne.get(), is(origins.get(0)));
    verify(mockRandom, never()).nextInt(any(Integer.class));
    verify(delegate).choose(any(LoadBalancer.Preferences.class));
}
Also used : Origin(com.hotels.styx.api.extension.Origin) 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 12 with RemoteHost

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

the class OriginRestrictionLoadBalancingStrategyTest method randomlyChoosesOneOfTheOriginsWithInvalidPreferences.

@Test
public void randomlyChoosesOneOfTheOriginsWithInvalidPreferences() {
    Random mockRandom = mock(Random.class);
    when(mockRandom.nextInt(any(Integer.class))).thenReturn(3);
    strategy = new OriginRestrictionLoadBalancingStrategy(new ActiveOrigins() {

        @Override
        public Iterable<RemoteHost> snapshot() {
            return origins;
        }

        @Override
        public String getApplicationId() {
            return null;
        }

        @Override
        public List<Origin> origins() {
            return null;
        }
    }, delegate, mockRandom);
    Optional<RemoteHost> chosenOne = strategy.choose(lbPreference(Optional.of("*-01")));
    // Chooses one from *all (= 7)* origins:
    verify(mockRandom).nextInt(eq(7));
    assertThat(chosenOne.get().id(), is(id("origin-3")));
}
Also used : Origin(com.hotels.styx.api.extension.Origin) 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 13 with RemoteHost

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

the class OriginRestrictionLoadBalancingStrategyTest method usesSingleOriginMatchingRegularExpression.

@Test
public void usesSingleOriginMatchingRegularExpression() {
    Random mockRandom = mock(Random.class);
    when(mockRandom.nextInt(eq(1))).thenReturn(0);
    strategy = new OriginRestrictionLoadBalancingStrategy(new ActiveOrigins() {

        @Override
        public Iterable<RemoteHost> snapshot() {
            return origins;
        }

        @Override
        public String getApplicationId() {
            return null;
        }

        @Override
        public List<Origin> origins() {
            return null;
        }
    }, delegate, mockRandom);
    Optional<RemoteHost> chosenOne = strategy.choose(lbPreference(Optional.of("origin-1")));
    assertThat(chosenOne.get(), is(origins.get(1)));
}
Also used : Origin(com.hotels.styx.api.extension.Origin) 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 14 with RemoteHost

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

the class OriginRestrictionLoadBalancingStrategyTest method usesNoOriginsWhenRegularExpressionMatchesNone.

@Test
public void usesNoOriginsWhenRegularExpressionMatchesNone() {
    Random mockRandom = mock(Random.class);
    strategy = new OriginRestrictionLoadBalancingStrategy(new ActiveOrigins() {

        @Override
        public Iterable<RemoteHost> snapshot() {
            return origins;
        }

        @Override
        public String getApplicationId() {
            return null;
        }

        @Override
        public List<Origin> origins() {
            return null;
        }
    }, delegate, mockRandom);
    Optional<RemoteHost> chosenOne = strategy.choose(lbPreference(Optional.of("foo")));
    assertThat(chosenOne, is(empty()));
    verify(mockRandom, never()).nextInt(any(Integer.class));
}
Also used : Origin(com.hotels.styx.api.extension.Origin) 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 15 with RemoteHost

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

the class OriginRestrictionLoadBalancingStrategyTest method usesMultipleOriginsMatchingRegularExpression.

@Test
public void usesMultipleOriginsMatchingRegularExpression() {
    Random mockRandom = mock(Random.class);
    when(mockRandom.nextInt(eq(3))).thenReturn(1);
    strategy = new OriginRestrictionLoadBalancingStrategy(new ActiveOrigins() {

        @Override
        public Iterable<RemoteHost> snapshot() {
            return origins;
        }

        @Override
        public String getApplicationId() {
            return null;
        }

        @Override
        public List<Origin> origins() {
            return null;
        }
    }, delegate, mockRandom);
    Optional<RemoteHost> chosenOne = strategy.choose(lbPreference(Optional.of("origin-[2-4]")));
    verify(mockRandom).nextInt(eq(3));
    assertThat(chosenOne.get(), is(origins.get(3)));
}
Also used : Origin(com.hotels.styx.api.extension.Origin) 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