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);
}
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 .*")));
}
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));
}
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")));
}
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()));
}
Aggregations