Search in sources :

Example 21 with BackendService

use of com.hotels.styx.api.extension.service.BackendService in project styx by ExpediaGroup.

the class BackendServicesRouterTest method selectsUsingSingleSlashPath.

@Test
public void selectsUsingSingleSlashPath() throws Exception {
    Registry.Changes<BackendService> changes = added(appA().newCopy().path("/").build(), appB().newCopy().path("/appB/hotel/details.html").build());
    BackendServicesRouter router = new BackendServicesRouter(serviceClientFactory, environment, executor);
    router.onChange(changes);
    LiveHttpRequest request = get("/").build();
    Optional<HttpHandler> route = router.route(request, context);
    assertThat(proxyTo(route, request).header(ORIGIN_ID_DEFAULT), isValue(APP_A));
}
Also used : BackendService(com.hotels.styx.api.extension.service.BackendService) HttpHandler(com.hotels.styx.api.HttpHandler) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) Registry(com.hotels.styx.api.extension.service.spi.Registry) MicrometerRegistry(com.hotels.styx.api.MicrometerRegistry) MeterRegistry(com.hotels.styx.api.MeterRegistry) Test(org.junit.jupiter.api.Test)

Example 22 with BackendService

use of com.hotels.styx.api.extension.service.BackendService in project styx by ExpediaGroup.

the class BackendServicesRouterTest method deregistersAndReregistersMetricsAppropriately.

// This test exists due to a real bug we had when reloading in prod
@Test
public void deregistersAndReregistersMetricsAppropriately() {
    MeterRegistry metrics = new MicrometerRegistry(new SimpleMeterRegistry());
    Environment environment = new Environment.Builder().registry(metrics).build();
    MeterRegistry meterRegistry = environment.meterRegistry();
    BackendServicesRouter router = new BackendServicesRouter(new StyxBackendServiceClientFactory(environment), environment, executor);
    router.onChange(added(backendService(APP_B, "/appB/", 9094, "appB-01", 9095, "appB-02")));
    Tags tags01 = Tags.of(APPID_TAG, APP_B, ORIGINID_TAG, "appB-01");
    Tags tags02 = Tags.of(APPID_TAG, APP_B, ORIGINID_TAG, "appB-02");
    assertThat(meterRegistry.find("proxy.client.originHealthStatus").tags(tags01).gauge().value(), is(1.0));
    assertThat(meterRegistry.find("proxy.client.originHealthStatus").tags(tags02).gauge().value(), is(1.0));
    BackendService appMinusOneOrigin = backendService(APP_B, "/appB/", 9094, "appB-01");
    router.onChange(updated(appMinusOneOrigin));
    assertThat(meterRegistry.find("proxy.client.originHealthStatus").tags(tags01).gauge().value(), is(1.0));
    assertThat(meterRegistry.find("proxy.client.originHealthStatus").tags(tags02).gauge(), is(nullValue()));
}
Also used : BackendService(com.hotels.styx.api.extension.service.BackendService) MicrometerRegistry(com.hotels.styx.api.MicrometerRegistry) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) BackendService.newBackendServiceBuilder(com.hotels.styx.api.extension.service.BackendService.newBackendServiceBuilder) Environment(com.hotels.styx.Environment) Tags(io.micrometer.core.instrument.Tags) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) MeterRegistry(com.hotels.styx.api.MeterRegistry) Test(org.junit.jupiter.api.Test)

Example 23 with BackendService

use of com.hotels.styx.api.extension.service.BackendService 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 24 with BackendService

use of com.hotels.styx.api.extension.service.BackendService 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)

Example 25 with BackendService

use of com.hotels.styx.api.extension.service.BackendService in project styx by ExpediaGroup.

the class DashboardDataTest method unsubscribesFromEventBus.

@Test
public void unsubscribesFromEventBus() {
    EventBus eventBus = mock(EventBus.class);
    MemoryBackedRegistry<BackendService> backendServicesRegistry = new MemoryBackedRegistry<>();
    backendServicesRegistry.add(application("app", origin("app-01", "localhost", 9090)));
    backendServicesRegistry.add(application("test", origin("test-01", "localhost", 9090)));
    DashboardData dashbaord = new DashboardData(metricRegistry, backendServicesRegistry, "styx-prod1-presentation-01", new Version("releaseTag"), eventBus);
    // Twice for each backend. One during backend construction, another from BackendServicesRegistry listener callback.
    verify(eventBus, times(4)).register(any(DashboardData.Origin.class));
    dashbaord.unregister();
    verify(eventBus, times(4)).unregister(any(DashboardData.Origin.class));
}
Also used : Origin(com.hotels.styx.api.extension.Origin) BackendService(com.hotels.styx.api.extension.service.BackendService) Version(com.hotels.styx.Version) MemoryBackedRegistry(com.hotels.styx.infrastructure.MemoryBackedRegistry) EventBus(com.google.common.eventbus.EventBus) Test(org.junit.jupiter.api.Test)

Aggregations

BackendService (com.hotels.styx.api.extension.service.BackendService)37 Test (org.junit.jupiter.api.Test)36 Registry (com.hotels.styx.api.extension.service.spi.Registry)11 JustATestException (com.hotels.styx.support.JustATestException)8 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)7 MicrometerRegistry (com.hotels.styx.api.MicrometerRegistry)7 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)7 MeterRegistry (com.hotels.styx.api.MeterRegistry)6 Resource (com.hotels.styx.api.Resource)6 MemoryBackedRegistry (com.hotels.styx.infrastructure.MemoryBackedRegistry)6 IOException (java.io.IOException)6 HttpHandler (com.hotels.styx.api.HttpHandler)5 ReloadResult (com.hotels.styx.api.extension.service.spi.Registry.ReloadResult)5 ReloadResult.reloaded (com.hotels.styx.api.extension.service.spi.Registry.ReloadResult.reloaded)5 ReloadResult.unchanged (com.hotels.styx.api.extension.service.spi.Registry.ReloadResult.unchanged)5 StyxFutures.await (com.hotels.styx.common.StyxFutures.await)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)5 List (java.util.List)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5