use of com.hotels.styx.api.extension.Origin 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.Origin in project styx by ExpediaGroup.
the class OriginsInventoryTest method updatesOriginPortNumber.
@Test
public void updatesOriginPortNumber() throws Exception {
Origin originV1 = newOriginBuilder("acme.com", 80).applicationId(GENERIC_APP).id("acme-01").build();
Origin originV2 = newOriginBuilder("acme.com", 443).applicationId(GENERIC_APP).id("acme-01").build();
inventory.setOrigins(originV1);
assertThat(inventory.originCount(ACTIVE), is(1));
verify(monitor).monitor(singleton(originV1));
assertThat(gaugeValue("generic-app", "acme-01"), isValue(1.0));
verify(eventBus).post(any(OriginsSnapshot.class));
inventory.setOrigins(originV2);
assertThat(inventory.originCount(ACTIVE), is(1));
verify(monitor).stopMonitoring(singleton(originV1));
verify(monitor).monitor(singleton(originV2));
assertThat(gaugeValue("generic-app", "acme-01"), isValue(1.0));
verify(eventBus, times(2)).post(any(OriginsSnapshot.class));
}
use of com.hotels.styx.api.extension.Origin in project styx by ExpediaGroup.
the class OriginsInventoryTest method shutsConnectionPoolForModifiedOrigin.
@Test
public void shutsConnectionPoolForModifiedOrigin() {
Origin originV1 = newOriginBuilder("acme01.com", 80).applicationId(GENERIC_APP).id("acme-01").build();
Origin originV2 = newOriginBuilder("acme02.com", 80).applicationId(GENERIC_APP).id("acme-01").build();
ConnectionPool.Factory connectionFactory = mock(ConnectionPool.Factory.class);
ConnectionPool pool1 = mock(ConnectionPool.class);
ConnectionPool pool2 = mock(ConnectionPool.class);
when(connectionFactory.create(eq(originV1))).thenReturn(pool1);
when(connectionFactory.create(eq(originV2))).thenReturn(pool2);
inventory = new OriginsInventory(eventBus, GENERIC_APP, monitor, connectionFactory, hostClientFactory, new CentralisedMetrics(meterRegistry));
inventory.setOrigins(originV1);
verify(connectionFactory).create(eq(originV1));
inventory.setOrigins(originV2);
verify(connectionFactory).create(eq(originV2));
verify(pool1).close();
}
use of com.hotels.styx.api.extension.Origin in project styx by ExpediaGroup.
the class StyxBackendServiceClientTest method updatesCountersWhenTransactionIsCancelled.
@Test
public void updatesCountersWhenTransactionIsCancelled() {
Origin origin = originWithId("localhost:234", "App-X", "Origin-Y");
Processor<LiveHttpResponse, LiveHttpResponse> processor = EmitterProcessor.create();
StyxHostHttpClient hostClient = mockHostClient(processor);
StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).loadBalancer(mockLoadBalancer(Optional.of(remoteHost(origin, toHandler(hostClient), hostClient)))).metrics(metrics).build();
StepVerifier.create(styxHttpClient.sendRequest(SOME_REQ, requestContext())).thenCancel().verify();
assertThat(meterRegistry.get("proxy.client.requests.cancelled").tags("appId", "App-X", "originId", "Origin-Y").counter().count(), is(1.0));
}
use of com.hotels.styx.api.extension.Origin in project styx by ExpediaGroup.
the class StyxBackendServiceClientTest method hostHeaderIsNotOverwrittenWhenOverrideHostHeaderIsFalse.
@Test
public void hostHeaderIsNotOverwrittenWhenOverrideHostHeaderIsFalse() {
HttpInterceptor.Context requestContext = requestContext();
StyxHostHttpClient hostClient = mock(StyxHostHttpClient.class);
HttpHandler httpHandler = mock(HttpHandler.class);
Origin origin = newOriginBuilder(incomingHostname, 9090).applicationId(GENERIC_APP).build();
RemoteHost remoteHost = remoteHost(origin, httpHandler, hostClient);
LoadBalancer loadBalancer = mockLoadBalancer(Optional.of(remoteHost));
when(httpHandler.handle(any(), any())).thenReturn(Eventual.of(testResponse));
StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).originStatsFactory(mock(OriginStatsFactory.class)).originsRestrictionCookieName("someCookie").originIdHeader("origin-id").loadBalancer(loadBalancer).retryPolicy(new RetryNTimes(0)).metrics(metrics).overrideHostHeader(false).build();
styxHttpClient.sendRequest(testRequest, requestContext);
verify(httpHandler).handle(testRequest, requestContext);
}
Aggregations