Search in sources :

Example 1 with BackendServiceClient

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"));
}
Also used : BackendService(com.hotels.styx.api.extension.service.BackendService) MicrometerRegistry(com.hotels.styx.api.MicrometerRegistry) StyxConfig(com.hotels.styx.StyxConfig) StyxBackendServiceClient(com.hotels.styx.client.StyxBackendServiceClient) BackendServiceClient(com.hotels.styx.client.BackendServiceClient) CachingOriginStatsFactory(com.hotels.styx.client.OriginStatsFactory.CachingOriginStatsFactory) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) OriginsInventory.newOriginsInventoryBuilder(com.hotels.styx.client.OriginsInventory.newOriginsInventoryBuilder) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) BackendService.newBackendServiceBuilder(com.hotels.styx.api.extension.service.BackendService.newBackendServiceBuilder) StickySessionConfig.newStickySessionConfigBuilder(com.hotels.styx.api.extension.service.StickySessionConfig.newStickySessionConfigBuilder) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) MapBackedConfiguration(com.hotels.styx.api.configuration.Configuration.MapBackedConfiguration) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 2 with BackendServiceClient

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
}
Also used : StyxBackendServiceClient(com.hotels.styx.client.StyxBackendServiceClient) BackendServiceClient(com.hotels.styx.client.BackendServiceClient) CachingOriginStatsFactory(com.hotels.styx.client.OriginStatsFactory.CachingOriginStatsFactory) OriginStatsFactory(com.hotels.styx.client.OriginStatsFactory) OriginsInventory(com.hotels.styx.client.OriginsInventory) Test(org.junit.jupiter.api.Test)

Example 3 with BackendServiceClient

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"));
}
Also used : BackendService(com.hotels.styx.api.extension.service.BackendService) StyxBackendServiceClient(com.hotels.styx.client.StyxBackendServiceClient) BackendServiceClient(com.hotels.styx.client.BackendServiceClient) CachingOriginStatsFactory(com.hotels.styx.client.OriginStatsFactory.CachingOriginStatsFactory) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 4 with BackendServiceClient

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));
}
Also used : BackendService(com.hotels.styx.api.extension.service.BackendService) BackendServiceClient(com.hotels.styx.client.BackendServiceClient) OriginStatsFactory(com.hotels.styx.client.OriginStatsFactory) OriginsInventory(com.hotels.styx.client.OriginsInventory) Test(org.junit.jupiter.api.Test)

Example 5 with BackendServiceClient

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));
}
Also used : BackendService(com.hotels.styx.api.extension.service.BackendService) BackendServiceClient(com.hotels.styx.client.BackendServiceClient) OriginStatsFactory(com.hotels.styx.client.OriginStatsFactory) OriginsInventory(com.hotels.styx.client.OriginsInventory) Test(org.junit.jupiter.api.Test)

Aggregations

BackendServiceClient (com.hotels.styx.client.BackendServiceClient)5 Test (org.junit.jupiter.api.Test)5 BackendService (com.hotels.styx.api.extension.service.BackendService)4 OriginStatsFactory (com.hotels.styx.client.OriginStatsFactory)3 CachingOriginStatsFactory (com.hotels.styx.client.OriginStatsFactory.CachingOriginStatsFactory)3 OriginsInventory (com.hotels.styx.client.OriginsInventory)3 StyxBackendServiceClient (com.hotels.styx.client.StyxBackendServiceClient)3 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)2 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)2 StyxConfig (com.hotels.styx.StyxConfig)1 MicrometerRegistry (com.hotels.styx.api.MicrometerRegistry)1 MapBackedConfiguration (com.hotels.styx.api.configuration.Configuration.MapBackedConfiguration)1 Origin.newOriginBuilder (com.hotels.styx.api.extension.Origin.newOriginBuilder)1 BackendService.newBackendServiceBuilder (com.hotels.styx.api.extension.service.BackendService.newBackendServiceBuilder)1 StickySessionConfig.newStickySessionConfigBuilder (com.hotels.styx.api.extension.service.StickySessionConfig.newStickySessionConfigBuilder)1 OriginsInventory.newOriginsInventoryBuilder (com.hotels.styx.client.OriginsInventory.newOriginsInventoryBuilder)1 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)1