use of io.servicetalk.client.api.internal.partition.DefaultPartitionAttributesBuilder in project servicetalk by apple.
the class PartitionedHttpClientTest method testPartitionByTarget.
@Test
void testPartitionByTarget() throws Exception {
final Function<HttpRequestMetaData, PartitionAttributesBuilder> selector = req -> new DefaultPartitionAttributesBuilder(1).add(SRV_NAME, req.requestTarget().substring(1));
try (BlockingHttpClient clt = HttpClients.forPartitionedAddress(psd, "test-cluster", selector).initializer((pa, builder) -> builder.unresolvedAddressToHost(addr -> pa.get(SRV_NAME))).buildBlocking()) {
sdPublisher.onSubscribe(new TestSubscription());
sdPublisher.onNext(new TestPSDE(SRV_1, (InetSocketAddress) srv1.listenAddress()), new TestPSDE(SRV_2, (InetSocketAddress) srv2.listenAddress()));
final HttpResponse httpResponse1 = clt.request(clt.get("/" + SRV_2));
final HttpResponse httpResponse2 = clt.request(clt.get("/" + SRV_1));
MatcherAssert.assertThat(httpResponse1.headers().get(X_SERVER), hasToString(SRV_2));
MatcherAssert.assertThat(httpResponse2.headers().get(X_SERVER), hasToString(SRV_1));
}
}
use of io.servicetalk.client.api.internal.partition.DefaultPartitionAttributesBuilder in project servicetalk by apple.
the class PartitionedHttpClientTest method testPartitionByLeader.
@Test
void testPartitionByLeader() throws Exception {
final Function<HttpRequestMetaData, PartitionAttributesBuilder> selector = req -> new DefaultPartitionAttributesBuilder(1).add(SRV_LEADER, true);
try (BlockingHttpClient clt = HttpClients.forPartitionedAddress(psd, "test-cluster", selector).buildBlocking()) {
sdPublisher.onSubscribe(new TestSubscription());
sdPublisher.onNext(new TestPSDE(SRV_1, false, (InetSocketAddress) srv1.listenAddress()), new TestPSDE(SRV_2, true, (InetSocketAddress) srv2.listenAddress()));
final HttpResponse httpResponse1 = clt.request(clt.get("/foo"));
final HttpResponse httpResponse2 = clt.request(clt.get("/bar"));
MatcherAssert.assertThat(httpResponse1.headers().get(X_SERVER), hasToString(SRV_2));
MatcherAssert.assertThat(httpResponse2.headers().get(X_SERVER), hasToString(SRV_2));
}
}
use of io.servicetalk.client.api.internal.partition.DefaultPartitionAttributesBuilder in project servicetalk by apple.
the class PartitionedHttpClientTest method testPartitionByHeader.
@Test
void testPartitionByHeader() throws Exception {
final Function<HttpRequestMetaData, PartitionAttributesBuilder> selector = req -> new DefaultPartitionAttributesBuilder(1).add(SRV_NAME, requireNonNull(req.headers().get(X_SERVER)).toString());
try (BlockingHttpClient clt = HttpClients.forPartitionedAddress(psd, "test-cluster", selector).initializer((pa, builder) -> builder.unresolvedAddressToHost(addr -> pa.get(SRV_NAME))).buildBlocking()) {
sdPublisher.onSubscribe(new TestSubscription());
sdPublisher.onNext(new TestPSDE(SRV_1, (InetSocketAddress) srv1.listenAddress()), new TestPSDE(SRV_2, (InetSocketAddress) srv2.listenAddress()));
final HttpResponse httpResponse1 = clt.request(clt.get("/").addHeader(X_SERVER, SRV_2));
final HttpResponse httpResponse2 = clt.request(clt.get("/").addHeader(X_SERVER, SRV_1));
MatcherAssert.assertThat(httpResponse1.headers().get(X_SERVER), hasToString(SRV_2));
MatcherAssert.assertThat(httpResponse2.headers().get(X_SERVER), hasToString(SRV_1));
}
}
Aggregations