Search in sources :

Example 6 with ActiveOrigins

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

the class OriginRestrictionLoadBalancingStrategyTest method dontDelegateWhenOriginRestrictionCookieMatches.

@Test
public void dontDelegateWhenOriginRestrictionCookieMatches() {
    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);
    strategy.choose(lbPreference(Optional.of("origin-1")));
    strategy.choose(lbPreference(Optional.of("origin-1")));
    strategy.choose(lbPreference(Optional.of("origin-1")));
    strategy.choose(lbPreference(Optional.of("origin-1")));
    strategy.choose(lbPreference(Optional.of("origin-1")));
    strategy.choose(lbPreference(Optional.of("origin-1")));
    verify(delegate, never()).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 7 with ActiveOrigins

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

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

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

use of com.hotels.styx.api.extension.ActiveOrigins 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)

Aggregations

ActiveOrigins (com.hotels.styx.api.extension.ActiveOrigins)14 RemoteHost (com.hotels.styx.api.extension.RemoteHost)13 Test (org.junit.jupiter.api.Test)13 Random (java.util.Random)12 Origin (com.hotels.styx.api.extension.Origin)10 OriginsSnapshot (com.hotels.styx.api.extension.OriginsSnapshot)1 LoadBalancer (com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer)1 StubConnectionFactory (com.hotels.styx.client.connectionpool.stubs.StubConnectionFactory)1 LoggingTestSupport (com.hotels.styx.support.matchers.LoggingTestSupport)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1