Search in sources :

Example 6 with Origin

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")));
}
Also used : Origin(com.hotels.styx.api.extension.Origin) ActiveOrigins(com.hotels.styx.api.extension.ActiveOrigins) RemoteHost(com.hotels.styx.api.extension.RemoteHost) Random(java.util.Random) Test(org.junit.jupiter.api.Test)

Example 7 with Origin

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));
}
Also used : DisableOrigin(com.hotels.styx.client.origincommands.DisableOrigin) EnableOrigin(com.hotels.styx.client.origincommands.EnableOrigin) Origin(com.hotels.styx.api.extension.Origin) OriginsSnapshot(com.hotels.styx.api.extension.OriginsSnapshot) Test(org.junit.jupiter.api.Test)

Example 8 with Origin

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();
}
Also used : DisableOrigin(com.hotels.styx.client.origincommands.DisableOrigin) EnableOrigin(com.hotels.styx.client.origincommands.EnableOrigin) Origin(com.hotels.styx.api.extension.Origin) ConnectionPool(com.hotels.styx.client.connectionpool.ConnectionPool) CentralisedMetrics(com.hotels.styx.metrics.CentralisedMetrics) Test(org.junit.jupiter.api.Test)

Example 9 with Origin

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));
}
Also used : Origin(com.hotels.styx.api.extension.Origin) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 10 with Origin

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);
}
Also used : Origin(com.hotels.styx.api.extension.Origin) RetryNTimes(com.hotels.styx.client.retry.RetryNTimes) Context(com.hotels.styx.api.HttpInterceptor.Context) HttpHandler(com.hotels.styx.api.HttpHandler) RemoteHost(com.hotels.styx.api.extension.RemoteHost) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) LoadBalancer(com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer) Test(org.junit.jupiter.api.Test)

Aggregations

Origin (com.hotels.styx.api.extension.Origin)49 Test (org.junit.jupiter.api.Test)40 RemoteHost (com.hotels.styx.api.extension.RemoteHost)15 ActiveOrigins (com.hotels.styx.api.extension.ActiveOrigins)11 Origin.newOriginBuilder (com.hotels.styx.api.extension.Origin.newOriginBuilder)11 Random (java.util.Random)9 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)8 LoadBalancer (com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer)8 HttpHandler (com.hotels.styx.api.HttpHandler)6 BackendService (com.hotels.styx.api.extension.service.BackendService)6 ConnectionSettings (com.hotels.styx.client.ConnectionSettings)6 Id (com.hotels.styx.api.Id)5 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)5 OriginUnreachableException (com.hotels.styx.api.exceptions.OriginUnreachableException)5 DisableOrigin (com.hotels.styx.client.origincommands.DisableOrigin)5 EnableOrigin (com.hotels.styx.client.origincommands.EnableOrigin)5 CentralisedMetrics (com.hotels.styx.metrics.CentralisedMetrics)5 EventBus (com.google.common.eventbus.EventBus)4 HttpInterceptor (com.hotels.styx.api.HttpInterceptor)4 Context (com.hotels.styx.api.HttpInterceptor.Context)4