Search in sources :

Example 1 with PartitionAttributesBuilder

use of io.servicetalk.client.api.partition.PartitionAttributesBuilder 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));
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Matchers.hasToString(org.hamcrest.Matchers.hasToString) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) HttpRequestMethod(io.servicetalk.http.api.HttpRequestMethod) Publisher(io.servicetalk.concurrent.api.Publisher) ClientGroup(io.servicetalk.client.api.ClientGroup) DefaultPartitionAttributesBuilder(io.servicetalk.client.api.internal.partition.DefaultPartitionAttributesBuilder) Function(java.util.function.Function) AfterAll(org.junit.jupiter.api.AfterAll) AVAILABLE(io.servicetalk.client.api.ServiceDiscovererEvent.Status.AVAILABLE) HttpExecutionStrategies.defaultStrategy(io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) BeforeAll(org.junit.jupiter.api.BeforeAll) HttpSerializers.textSerializerUtf8(io.servicetalk.http.api.HttpSerializers.textSerializerUtf8) Objects.requireNonNull(java.util.Objects.requireNonNull) HttpClient(io.servicetalk.http.api.HttpClient) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) InetAddress.getLoopbackAddress(java.net.InetAddress.getLoopbackAddress) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) StreamingHttpRequestFactory(io.servicetalk.http.api.StreamingHttpRequestFactory) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) PartitionAttributesBuilder(io.servicetalk.client.api.partition.PartitionAttributesBuilder) Single(io.servicetalk.concurrent.api.Single) HttpResponse(io.servicetalk.http.api.HttpResponse) ServiceDiscoverer(io.servicetalk.client.api.ServiceDiscoverer) AsyncCloseables.newCompositeCloseable(io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable) Mockito.when(org.mockito.Mockito.when) OK(io.servicetalk.http.api.HttpResponseStatus.OK) InetSocketAddress(java.net.InetSocketAddress) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) StandardCharsets(java.nio.charset.StandardCharsets) PartitionedServiceDiscovererEvent(io.servicetalk.client.api.partition.PartitionedServiceDiscovererEvent) DefaultHttpHeadersFactory(io.servicetalk.http.api.DefaultHttpHeadersFactory) HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData) Test(org.junit.jupiter.api.Test) List(java.util.List) PartitionAttributes(io.servicetalk.client.api.partition.PartitionAttributes) MatcherAssert(org.hamcrest.MatcherAssert) ExecutionContext(io.servicetalk.transport.api.ExecutionContext) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) HTTP_1_1(io.servicetalk.http.api.HttpProtocolVersion.HTTP_1_1) DefaultStreamingHttpRequestResponseFactory(io.servicetalk.http.api.DefaultStreamingHttpRequestResponseFactory) HostAndPort(io.servicetalk.transport.api.HostAndPort) Mockito.mock(org.mockito.Mockito.mock) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) InetSocketAddress(java.net.InetSocketAddress) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) HttpResponse(io.servicetalk.http.api.HttpResponse) DefaultPartitionAttributesBuilder(io.servicetalk.client.api.internal.partition.DefaultPartitionAttributesBuilder) DefaultPartitionAttributesBuilder(io.servicetalk.client.api.internal.partition.DefaultPartitionAttributesBuilder) PartitionAttributesBuilder(io.servicetalk.client.api.partition.PartitionAttributesBuilder) Test(org.junit.jupiter.api.Test)

Example 2 with PartitionAttributesBuilder

use of io.servicetalk.client.api.partition.PartitionAttributesBuilder in project servicetalk by apple.

the class PowerSetPartitionMapTest method testWildCardResolveTwoElements.

@Test
void testWildCardResolveTwoElements() {
    PowerSetPartitionMap<ListenableAsyncCloseable> map = oneTwoThreeMap();
    PartitionAttributesBuilder builder = new DefaultPartitionAttributesBuilder(2);
    builder.add(DC_ID, 1);
    builder.add(SHARD_ID, 10);
    assertEquals(VALUE, map.get(builder.build()));
    builder = new DefaultPartitionAttributesBuilder(2);
    builder.add(DC_ID, 1);
    builder.add(SHARD_ID, 9);
    assertEquals(VALUE, map.get(builder.build()));
    builder = new DefaultPartitionAttributesBuilder(2);
    builder.add(DC_ID, 1);
    builder.add(APP_ID, "myapp");
    assertEquals(VALUE, map.get(builder.build()));
    builder = new DefaultPartitionAttributesBuilder(2);
    builder.add(APP_ID, "myapp");
    builder.add(IS_MAIN, true);
    assertEquals(VALUE, map.get(builder.build()));
    builder = new DefaultPartitionAttributesBuilder(2);
    builder.add(APP_ID, "myapp");
    builder.add(IS_MAIN, false);
    assertEquals(VALUE, map.get(builder.build()));
    builder = new DefaultPartitionAttributesBuilder(2);
    builder.add(DC_ID, 1);
    builder.add(IS_MAIN, false);
    assertEquals(VALUE, map.get(builder.build()));
    builder = new DefaultPartitionAttributesBuilder(2);
    builder.add(DC_ID, 1);
    builder.add(IS_MAIN, true);
    assertEquals(VALUE, map.get(builder.build()));
    builder = new DefaultPartitionAttributesBuilder(2);
    builder.add(DC_ID, 2);
    builder.add(IS_MAIN, true);
    assertNull(map.get(builder.build()));
}
Also used : ListenableAsyncCloseable(io.servicetalk.concurrent.api.ListenableAsyncCloseable) PartitionAttributesBuilder(io.servicetalk.client.api.partition.PartitionAttributesBuilder) Test(org.junit.jupiter.api.Test)

Example 3 with PartitionAttributesBuilder

use of io.servicetalk.client.api.partition.PartitionAttributesBuilder in project servicetalk by apple.

the class PowerSetPartitionMapTest method testRemoveOfOverlappingAttributesPreservesValue.

@Test
void testRemoveOfOverlappingAttributesPreservesValue() {
    PowerSetPartitionMap<ListenableAsyncCloseable> map = oneTwoThreeMap();
    // Test remove
    List<ListenableAsyncCloseable> result = map.remove(host3Attributes());
    assertEquals(VALUE, map.get(host1Attributes()));
    assertEquals(VALUE, map.get(host2Attributes()));
    assertNull(map.get(host3Attributes()));
    assertEquals(15, result.size());
    // Test a wild card with a unique attribute to host3 removed no longer returns any results.
    PartitionAttributesBuilder builder = new DefaultPartitionAttributesBuilder(1);
    builder.add(SHARD_ID, 9);
    PartitionAttributes partitionAttributes = builder.build();
    assertNull(map.get(partitionAttributes));
    // Test the same key, but different value still returns results.
    builder = new DefaultPartitionAttributesBuilder(1);
    builder.add(SHARD_ID, 10);
    partitionAttributes = builder.build();
    assertNotNull(map.get(partitionAttributes));
}
Also used : PartitionAttributes(io.servicetalk.client.api.partition.PartitionAttributes) ListenableAsyncCloseable(io.servicetalk.concurrent.api.ListenableAsyncCloseable) PartitionAttributesBuilder(io.servicetalk.client.api.partition.PartitionAttributesBuilder) Test(org.junit.jupiter.api.Test)

Example 4 with PartitionAttributesBuilder

use of io.servicetalk.client.api.partition.PartitionAttributesBuilder in project servicetalk by apple.

the class PowerSetPartitionMapTest method host3Attributes.

private static PartitionAttributes host3Attributes() {
    PartitionAttributesBuilder builder = new DefaultPartitionAttributesBuilder(4);
    builder.add(DC_ID, 1);
    builder.add(SHARD_ID, 9);
    builder.add(APP_ID, "myapp");
    builder.add(IS_MAIN, true);
    return builder.build();
}
Also used : PartitionAttributesBuilder(io.servicetalk.client.api.partition.PartitionAttributesBuilder)

Example 5 with PartitionAttributesBuilder

use of io.servicetalk.client.api.partition.PartitionAttributesBuilder in project servicetalk by apple.

the class PowerSetPartitionMapTest method host4Attributes.

private static PartitionAttributes host4Attributes() {
    PartitionAttributesBuilder builder = new DefaultPartitionAttributesBuilder(4);
    builder.add(DC_ID, 1);
    builder.add(SHARD_ID, 8);
    builder.add(APP_ID, "myapp");
    builder.add(IS_MAIN, true);
    return builder.build();
}
Also used : PartitionAttributesBuilder(io.servicetalk.client.api.partition.PartitionAttributesBuilder)

Aggregations

PartitionAttributesBuilder (io.servicetalk.client.api.partition.PartitionAttributesBuilder)14 Test (org.junit.jupiter.api.Test)9 PartitionAttributes (io.servicetalk.client.api.partition.PartitionAttributes)6 ListenableAsyncCloseable (io.servicetalk.concurrent.api.ListenableAsyncCloseable)6 List (java.util.List)4 Objects.requireNonNull (java.util.Objects.requireNonNull)4 ClientGroup (io.servicetalk.client.api.ClientGroup)3 ServiceDiscoverer (io.servicetalk.client.api.ServiceDiscoverer)3 AVAILABLE (io.servicetalk.client.api.ServiceDiscovererEvent.Status.AVAILABLE)3 DefaultPartitionAttributesBuilder (io.servicetalk.client.api.internal.partition.DefaultPartitionAttributesBuilder)3 PartitionedServiceDiscovererEvent (io.servicetalk.client.api.partition.PartitionedServiceDiscovererEvent)3 AsyncCloseables.newCompositeCloseable (io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable)3 Publisher (io.servicetalk.concurrent.api.Publisher)3 Single (io.servicetalk.concurrent.api.Single)3 TestPublisher (io.servicetalk.concurrent.api.TestPublisher)3 TestSubscription (io.servicetalk.concurrent.api.TestSubscription)3 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)3 DefaultHttpHeadersFactory (io.servicetalk.http.api.DefaultHttpHeadersFactory)3 DefaultStreamingHttpRequestResponseFactory (io.servicetalk.http.api.DefaultStreamingHttpRequestResponseFactory)3 HttpClient (io.servicetalk.http.api.HttpClient)3