Search in sources :

Example 1 with OriginsInventory

use of com.hotels.styx.client.OriginsInventory 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 2 with OriginsInventory

use of com.hotels.styx.client.OriginsInventory 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 3 with OriginsInventory

use of com.hotels.styx.client.OriginsInventory 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 4 with OriginsInventory

use of com.hotels.styx.client.OriginsInventory in project styx by ExpediaGroup.

the class BackendServicesRouter method onChange.

@Override
public void onChange(Registry.Changes<BackendService> changes) {
    changes.removed().forEach(backendService -> routes.remove(backendService.path()).close());
    concatenatedForEach(changes.added(), changes.updated(), backendService -> {
        ProxyToClientPipeline pipeline = routes.get(backendService.path());
        if (pipeline != null) {
            pipeline.close();
        }
        boolean requestLoggingEnabled = environment.styxConfig().get("request-logging.outbound.enabled", Boolean.class).orElse(false);
        boolean longFormat = environment.styxConfig().get("request-logging.outbound.longFormat", Boolean.class).orElse(false);
        OriginStatsFactory originStatsFactory = new CachingOriginStatsFactory(environment.centralisedMetrics());
        ConnectionPoolSettings poolSettings = backendService.connectionPoolConfig();
        Connection.Factory connectionFactory = connectionFactory(backendService, requestLoggingEnabled, longFormat, originStatsFactory, poolSettings.connectionExpirationSeconds());
        ConnectionPool.Factory connectionPoolFactory = new SimpleConnectionPoolFactory.Builder().connectionFactory(connectionFactory).connectionPoolSettings(backendService.connectionPoolConfig()).metrics(environment.centralisedMetrics()).build();
        OriginHealthStatusMonitor healthStatusMonitor = healthStatusMonitor(backendService);
        OriginsInventory inventory = new OriginsInventory.Builder(backendService.id()).eventBus(environment.eventBus()).metrics(environment.centralisedMetrics()).connectionPoolFactory(connectionPoolFactory).originHealthMonitor(healthStatusMonitor).initialOrigins(backendService.origins()).hostClientFactory(StyxHostHttpClient::create).build();
        pipeline = new ProxyToClientPipeline(newClientHandler(backendService, inventory, originStatsFactory), () -> {
            inventory.close();
            healthStatusMonitor.stop();
        });
        routes.put(backendService.path(), pipeline);
        LOG.info("added path={} current routes={}", backendService.path(), routes.keySet());
    });
}
Also used : ConnectionPool(com.hotels.styx.client.connectionpool.ConnectionPool) CachingOriginStatsFactory(com.hotels.styx.client.OriginStatsFactory.CachingOriginStatsFactory) OriginsInventory(com.hotels.styx.client.OriginsInventory) HttpConfig.newHttpConfigBuilder(com.hotels.styx.client.HttpConfig.newHttpConfigBuilder) Builder.httpRequestOperationFactoryBuilder(com.hotels.styx.client.HttpRequestOperationFactory.Builder.httpRequestOperationFactoryBuilder) Connection(com.hotels.styx.client.Connection) OriginHealthStatusMonitor(com.hotels.styx.client.healthcheck.OriginHealthStatusMonitor) CachingOriginStatsFactory(com.hotels.styx.client.OriginStatsFactory.CachingOriginStatsFactory) OriginStatsFactory(com.hotels.styx.client.OriginStatsFactory) ConnectionPoolSettings(com.hotels.styx.api.extension.service.ConnectionPoolSettings)

Aggregations

OriginStatsFactory (com.hotels.styx.client.OriginStatsFactory)4 OriginsInventory (com.hotels.styx.client.OriginsInventory)4 BackendServiceClient (com.hotels.styx.client.BackendServiceClient)3 Test (org.junit.jupiter.api.Test)3 BackendService (com.hotels.styx.api.extension.service.BackendService)2 CachingOriginStatsFactory (com.hotels.styx.client.OriginStatsFactory.CachingOriginStatsFactory)2 ConnectionPoolSettings (com.hotels.styx.api.extension.service.ConnectionPoolSettings)1 Connection (com.hotels.styx.client.Connection)1 HttpConfig.newHttpConfigBuilder (com.hotels.styx.client.HttpConfig.newHttpConfigBuilder)1 Builder.httpRequestOperationFactoryBuilder (com.hotels.styx.client.HttpRequestOperationFactory.Builder.httpRequestOperationFactoryBuilder)1 StyxBackendServiceClient (com.hotels.styx.client.StyxBackendServiceClient)1 ConnectionPool (com.hotels.styx.client.connectionpool.ConnectionPool)1 OriginHealthStatusMonitor (com.hotels.styx.client.healthcheck.OriginHealthStatusMonitor)1