Search in sources :

Example 1 with ActiveOrigins

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

the class OriginRestrictionLoadBalancingStrategyTest method setUp.

@BeforeEach
public void setUp() {
    delegate = mock(LoadBalancer.class);
    when(delegate.choose(any(LoadBalancer.Preferences.class))).thenReturn(Optional.of(origins.get(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);
    log = new LoggingTestSupport(OriginRestrictionLoadBalancingStrategy.class);
}
Also used : Origin(com.hotels.styx.api.extension.Origin) ActiveOrigins(com.hotels.styx.api.extension.ActiveOrigins) RemoteHost(com.hotels.styx.api.extension.RemoteHost) LoadBalancer(com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer) LoggingTestSupport(com.hotels.styx.support.matchers.LoggingTestSupport) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with ActiveOrigins

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

the class OriginRestrictionLoadBalancingStrategyTest method logsInvalidPatterns.

@Test
public void logsInvalidPatterns() {
    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);
    strategy.choose(lbPreference(Optional.of("*-01")));
    assertThat(log.lastMessage(), is(loggingEvent(ERROR, "Invalid origin restriction cookie value=.*, Cause=Dangling meta character .*")));
}
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 3 with ActiveOrigins

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

the class OriginRestrictionLoadBalancingStrategyTest method usesAllOriginsWhenRegularExpressionMatchesAll.

@Test
public void usesAllOriginsWhenRegularExpressionMatchesAll() {
    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(".*")));
    assertThat(chosenOne.get(), isOneOf(origins.get(3)));
    verify(mockRandom).nextInt(eq(7));
}
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 4 with ActiveOrigins

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

the class OriginRestrictionLoadBalancingStrategyTest method randomlyChoosesOneOfTheMatchingOrigins.

@Test
public void randomlyChoosesOneOfTheMatchingOrigins() {
    Random mockRandom = mock(Random.class);
    when(mockRandom.nextInt(5)).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("origin-[1-3], origin-(5|6)")));
    // The preferred origins are [1, 2, 3, 5, 6]
    // The index 3 (as chosen by rng) contains:
    assertThat(chosenOne.get().id(), is(id("origin-5")));
}
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 5 with ActiveOrigins

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

the class PowerOfTwoStrategyTest method returnsEmptyWhenNoOriginsAreAvailable.

@Test
public void returnsEmptyWhenNoOriginsAreAvailable() {
    ActiveOrigins activeOrigins = mock(ActiveOrigins.class);
    when(activeOrigins.snapshot()).thenReturn(asList());
    PowerOfTwoStrategy loadBalancer = new PowerOfTwoStrategy(activeOrigins, new Random(RNG_SEED));
    Optional<RemoteHost> chosenOne = loadBalancer.choose(mock(LoadBalancer.Preferences.class));
    assertThat(chosenOne, is(Optional.empty()));
}
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

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