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)));
}
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))));
}
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));
}
}
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)));
}
Aggregations