Search in sources :

Example 21 with TestSubscription

use of io.servicetalk.concurrent.api.TestSubscription in project servicetalk by apple.

the class CompletableConcatWithPublisherTest method setUp.

@BeforeEach
void setUp() {
    subscriber = new TestPublisherSubscriber<>();
    cancellable = new TestCancellable();
    source = new TestCompletable.Builder().disableAutoOnSubscribe().build();
    next = new TestPublisher.Builder<Integer>().disableAutoOnSubscribe().build();
    subscription = new TestSubscription();
    toSource(source.concat(next)).subscribe(subscriber);
    source.onSubscribe(cancellable);
    subscriber.awaitSubscription();
}
Also used : TestSubscription(io.servicetalk.concurrent.api.TestSubscription) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) TestCompletable(io.servicetalk.concurrent.api.TestCompletable) TestCancellable(io.servicetalk.concurrent.api.TestCancellable) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 22 with TestSubscription

use of io.servicetalk.concurrent.api.TestSubscription in project servicetalk by apple.

the class RetryTest method testCancel.

@Test
void testCancel() {
    final TestSubscription subscription = new TestSubscription();
    source.onSubscribe(subscription);
    subscriber.awaitSubscription().request(2);
    source.onNext(1, 2);
    assertThat(subscriber.takeOnNext(2), contains(1, 2));
    subscriber.awaitSubscription().cancel();
    source.onError(DELIBERATE_EXCEPTION);
    assertTrue(subscription.isCancelled());
}
Also used : TestSubscription(io.servicetalk.concurrent.api.TestSubscription) Test(org.junit.jupiter.api.Test)

Example 23 with TestSubscription

use of io.servicetalk.concurrent.api.TestSubscription 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));
    }
}
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 24 with TestSubscription

use of io.servicetalk.concurrent.api.TestSubscription 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));
    }
}
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 25 with TestSubscription

use of io.servicetalk.concurrent.api.TestSubscription in project servicetalk by apple.

the class NettyPipelinedConnectionTest method channelCloseErrorsPendingReadCancelsPendingWrite.

@Test
void channelCloseErrorsPendingReadCancelsPendingWrite() throws Exception {
    TestSubscription writePublisher1Subscription = new TestSubscription();
    toSource(requester.write(writePublisher1.afterSubscription(() -> writePublisher1Subscription))).subscribe(readSubscriber);
    Subscription readSubscription = readSubscriber.awaitSubscription();
    readSubscription.request(1);
    toSource(requester.write(writePublisher2)).subscribe(readSubscriber2);
    assertTrue(writePublisher1.isSubscribed());
    assertFalse(writePublisher2.isSubscribed());
    channel.close();
    assertThat(readSubscriber.awaitOnError(), is(instanceOf(ClosedChannelException.class)));
    assertThat(readSubscriber2.awaitOnError(), is(instanceOf(ClosedChannelException.class)));
    writePublisher1Subscription.awaitCancelled();
    assertFalse(writePublisher2.isSubscribed());
}
Also used : TestSubscription(io.servicetalk.concurrent.api.TestSubscription) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) Test(org.junit.jupiter.api.Test)

Aggregations

TestSubscription (io.servicetalk.concurrent.api.TestSubscription)26 Test (org.junit.jupiter.api.Test)19 TestPublisher (io.servicetalk.concurrent.api.TestPublisher)7 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)7 Buffer (io.servicetalk.buffer.api.Buffer)4 Subscription (io.servicetalk.concurrent.PublisherSource.Subscription)4 AsyncCloseables.newCompositeCloseable (io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable)4 ScalarValueSubscription (io.servicetalk.concurrent.internal.ScalarValueSubscription)4 HttpExecutionStrategies.defaultStrategy (io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy)4 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)4 ServerContext (io.servicetalk.transport.api.ServerContext)4 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)4 AddressUtils.serverHostAndPort (io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort)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 PartitionAttributes (io.servicetalk.client.api.partition.PartitionAttributes)3 PartitionAttributesBuilder (io.servicetalk.client.api.partition.PartitionAttributesBuilder)3 PartitionedServiceDiscovererEvent (io.servicetalk.client.api.partition.PartitionedServiceDiscovererEvent)3