Search in sources :

Example 11 with ActiveOrigins

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

Example 12 with ActiveOrigins

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

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

Example 14 with ActiveOrigins

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

the class RoundRobinStrategyTest method refreshesAtOriginsChange.

@Test
public void refreshesAtOriginsChange() {
    ActiveOrigins activeOrigins = mock(ActiveOrigins.class);
    when(activeOrigins.snapshot()).thenReturn(asList(HOST_1, HOST_2, HOST_3));
    strategy = new RoundRobinStrategy.Factory().create(environment, configuration, activeOrigins);
    assertThat(strategy.choose(null), is(Optional.of(HOST_1)));
    assertThat(strategy.choose(null), is(Optional.of(HOST_2)));
    assertThat(strategy.choose(null), is(Optional.of(HOST_3)));
    assertThat(strategy.choose(null), is(Optional.of(HOST_1)));
    assertThat(strategy.choose(null), is(Optional.of(HOST_2)));
    when(activeOrigins.snapshot()).thenReturn(asList(HOST_2, HOST_4, HOST_6));
    strategy.originsChanged(new OriginsSnapshot(APP_ID, asList(HOST_2, HOST_4, HOST_6), asList(HOST_1, HOST_3), asList(HOST_5)));
    assertThat(strategy.choose(null), is(Optional.of(HOST_6)));
    assertThat(strategy.choose(null), is(Optional.of(HOST_2)));
    assertThat(strategy.choose(null), is(Optional.of(HOST_4)));
    assertThat(strategy.choose(null), is(Optional.of(HOST_6)));
    assertThat(strategy.choose(null), is(Optional.of(HOST_2)));
    assertThat(strategy.choose(null), is(Optional.of(HOST_4)));
}
Also used : ActiveOrigins(com.hotels.styx.api.extension.ActiveOrigins) StubConnectionFactory(com.hotels.styx.client.connectionpool.stubs.StubConnectionFactory) OriginsSnapshot(com.hotels.styx.api.extension.OriginsSnapshot) 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