use of com.hotels.styx.client.BackendServiceClient in project styx by ExpediaGroup.
the class StyxBackendServiceClientFactoryTest method usesTheOriginSpecifiedInTheOriginsRestrictionCookie.
@Test
public void usesTheOriginSpecifiedInTheOriginsRestrictionCookie() {
MapBackedConfiguration config = new MapBackedConfiguration();
config.set("originRestrictionCookie", ORIGINS_RESTRICTION_COOKIE);
environment = new Environment.Builder().registry(new MicrometerRegistry(new SimpleMeterRegistry())).configuration(new StyxConfig(config)).build();
BackendService backendService = newBackendServiceBuilder().origins(newOriginBuilder("localhost", 9091).id("x").build(), newOriginBuilder("localhost", 9092).id("y").build(), newOriginBuilder("localhost", 9093).id("z").build()).build();
BackendServiceClient styxBackendServiceClient = new StyxBackendServiceClientFactory(environment).createClient(backendService, newOriginsInventoryBuilder(environment.centralisedMetrics(), backendService).hostClientFactory((pool) -> {
if (pool.getOrigin().id().equals(id("x"))) {
return hostClient(response(OK).header("X-Origin-Id", "x").build());
} else if (pool.getOrigin().id().equals(id("y"))) {
return hostClient(response(OK).header("X-Origin-Id", "y").build());
} else {
return hostClient(response(OK).header("X-Origin-Id", "z").build());
}
}).build(), new CachingOriginStatsFactory(environment.centralisedMetrics()));
LiveHttpRequest requestz = get("/some-req").cookies(requestCookie(ORIGINS_RESTRICTION_COOKIE, id("z").toString())).build();
LiveHttpRequest requestx = get("/some-req").cookies(requestCookie(ORIGINS_RESTRICTION_COOKIE, id("x").toString())).build();
LiveHttpRequest requesty = get("/some-req").cookies(requestCookie(ORIGINS_RESTRICTION_COOKIE, id("y").toString())).build();
LiveHttpResponse responsez = Mono.from(styxBackendServiceClient.sendRequest(requestz, requestContext())).block();
LiveHttpResponse responsex = Mono.from(styxBackendServiceClient.sendRequest(requestx, requestContext())).block();
LiveHttpResponse responsey = Mono.from(styxBackendServiceClient.sendRequest(requesty, requestContext())).block();
assertThat(responsex.header("X-Origin-Id").get(), is("x"));
assertThat(responsey.header("X-Origin-Id").get(), is("y"));
assertThat(responsez.header("X-Origin-Id").get(), is("z"));
}
use of com.hotels.styx.client.BackendServiceClient in project styx by ExpediaGroup.
the class StyxBackendServiceClientFactoryTest method createsClients.
@Test
public void createsClients() {
StyxBackendServiceClientFactory factory = new StyxBackendServiceClientFactory(environment);
OriginsInventory originsInventory = newOriginsInventoryBuilder(backendService.id()).metrics(environment.centralisedMetrics()).connectionPoolFactory(simplePoolFactory()).initialOrigins(backendService.origins()).build();
OriginStatsFactory originStatsFactory = mock(OriginStatsFactory.class);
BackendServiceClient client = factory.createClient(backendService, originsInventory, originStatsFactory);
assertThat(client, is(instanceOf(StyxBackendServiceClient.class)));
// note: for more meaningful tests, perhaps we need to add package-private accessor methods to StyxBackendServiceClient
// there is also the issue that the Transport class assumes it will get a NettyConnection, so mocks can't be used
// these problems are both outside the scope of the current issue being implemented
// so for the time being, this test is mostly a placeholder
}
use of com.hotels.styx.client.BackendServiceClient in project styx by ExpediaGroup.
the class StyxBackendServiceClientFactoryTest method usesTheOriginSpecifiedInTheStickySessionCookie.
@Test
public void usesTheOriginSpecifiedInTheStickySessionCookie() {
BackendService backendService = newBackendServiceBuilder().origins(newOriginBuilder("localhost", 9091).id("x").build(), newOriginBuilder("localhost", 9092).id("y").build(), newOriginBuilder("localhost", 9093).id("z").build()).stickySessionConfig(newStickySessionConfigBuilder().enabled(true).build()).build();
BackendServiceClient styxBackendServiceClient = new StyxBackendServiceClientFactory(environment).createClient(backendService, newOriginsInventoryBuilder(environment.centralisedMetrics(), backendService).hostClientFactory((pool) -> {
if (pool.getOrigin().id().equals(id("x"))) {
return hostClient(response(OK).header("X-Origin-Id", "x").build());
} else if (pool.getOrigin().id().equals(id("y"))) {
return hostClient(response(OK).header("X-Origin-Id", "y").build());
} else {
return hostClient(response(OK).header("X-Origin-Id", "z").build());
}
}).build(), new CachingOriginStatsFactory(environment.centralisedMetrics()));
LiveHttpRequest requestz = get("/some-req").cookies(requestCookie(STICKY_COOKIE, id("z").toString())).build();
LiveHttpRequest requestx = get("/some-req").cookies(requestCookie(STICKY_COOKIE, id("x").toString())).build();
LiveHttpRequest requesty = get("/some-req").cookies(requestCookie(STICKY_COOKIE, id("y").toString())).build();
LiveHttpResponse responsez = Mono.from(styxBackendServiceClient.sendRequest(requestz, requestContext())).block();
LiveHttpResponse responsex = Mono.from(styxBackendServiceClient.sendRequest(requestx, requestContext())).block();
LiveHttpResponse responsey = Mono.from(styxBackendServiceClient.sendRequest(requesty, requestContext())).block();
assertThat(responsex.header("X-Origin-Id").get(), is("x"));
assertThat(responsey.header("X-Origin-Id").get(), is("y"));
assertThat(responsez.header("X-Origin-Id").get(), is("z"));
}
use of com.hotels.styx.client.BackendServiceClient in project styx by ExpediaGroup.
the class BackendServicesRouterTest method closesClientWhenBackendServicesAreUpdated.
@Test
public void closesClientWhenBackendServicesAreUpdated() {
BackendServiceClient firstClient = mock(BackendServiceClient.class);
BackendServiceClient secondClient = mock(BackendServiceClient.class);
BackendServiceClientFactory clientFactory = mock(BackendServiceClientFactory.class);
when(clientFactory.createClient(any(BackendService.class), any(OriginsInventory.class), any(OriginStatsFactory.class))).thenReturn(firstClient).thenReturn(secondClient);
BackendServicesRouter router = new BackendServicesRouter(clientFactory, environment, executor);
BackendService bookingApp = appB();
router.onChange(added(bookingApp));
ArgumentCaptor<OriginsInventory> originsInventory = forClass(OriginsInventory.class);
verify(clientFactory).createClient(eq(bookingApp), originsInventory.capture(), any(OriginStatsFactory.class));
BackendService bookingAppMinusOneOrigin = bookingAppMinusOneOrigin();
router.onChange(updated(bookingAppMinusOneOrigin));
assertThat(originsInventory.getValue().closed(), is(true));
verify(clientFactory).createClient(eq(bookingAppMinusOneOrigin), any(OriginsInventory.class), any(OriginStatsFactory.class));
}
use of com.hotels.styx.client.BackendServiceClient in project styx by ExpediaGroup.
the class BackendServicesRouterTest method closesClientWhenBackendServicesAreRemoved.
@Test
public void closesClientWhenBackendServicesAreRemoved() {
BackendServiceClient firstClient = mock(BackendServiceClient.class);
BackendServiceClient secondClient = mock(BackendServiceClient.class);
ArgumentCaptor<OriginsInventory> originsInventory = forClass(OriginsInventory.class);
BackendServiceClientFactory clientFactory = mock(BackendServiceClientFactory.class);
when(clientFactory.createClient(any(BackendService.class), any(OriginsInventory.class), any(OriginStatsFactory.class))).thenReturn(firstClient).thenReturn(secondClient);
BackendServicesRouter router = new BackendServicesRouter(clientFactory, environment, executor);
BackendService bookingApp = appB();
router.onChange(added(bookingApp));
verify(clientFactory).createClient(eq(bookingApp), originsInventory.capture(), any(OriginStatsFactory.class));
router.onChange(removed(bookingApp));
assertThat(originsInventory.getValue().closed(), is(true));
}
Aggregations