Search in sources :

Example 6 with TestSubscription

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

the class RepeatTest 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.onComplete();
    assertTrue(subscription.isCancelled());
}
Also used : TestSubscription(io.servicetalk.concurrent.api.TestSubscription) Test(org.junit.jupiter.api.Test)

Example 7 with TestSubscription

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

the class AbstractBlockingStreamingHttpRequesterTest method asyncToSyncCancelPropagated.

@Test
void asyncToSyncCancelPropagated() throws Exception {
    StreamingHttpRequester asyncRequester = newAsyncRequester(reqRespFactory, mockExecutionCtx, req -> succeeded(reqRespFactory.ok().payloadBody(publisher)));
    TestSubscription subscription = new TestSubscription();
    BlockingStreamingHttpRequester syncRequester = toBlockingStreamingRequester(asyncRequester);
    BlockingStreamingHttpResponse syncResponse = syncRequester.request(syncRequester.get("/"));
    assertEquals(HTTP_1_1, syncResponse.version());
    assertEquals(OK, syncResponse.status());
    BlockingIterator iterator = syncResponse.payloadBody().iterator();
    publisher.onSubscribe(subscription);
    publisher.onNext(allocator.fromAscii("hello"));
    assertTrue(iterator.hasNext());
    iterator.close();
    assertTrue(subscription.isCancelled());
}
Also used : TestSubscription(io.servicetalk.concurrent.api.TestSubscription) BlockingIterator(io.servicetalk.concurrent.BlockingIterator) Test(org.junit.jupiter.api.Test)

Example 8 with TestSubscription

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

the class NettyHttpServerConnectionTest method updateFlushStrategy.

@ParameterizedTest(name = "server={0} client={1}")
@MethodSource("executionStrategies")
void updateFlushStrategy(HttpExecutionStrategy serverExecutionStrategy, HttpExecutionStrategy clientExecutionStrategy) throws Exception {
    customStrategy = new MockFlushStrategy();
    AtomicReference<Cancellable> customCancellableRef = new AtomicReference<>();
    AtomicBoolean handledFirstRequest = new AtomicBoolean();
    serverContext = HttpServers.forAddress(localAddress(0)).ioExecutor(contextRule.ioExecutor()).appendConnectionAcceptorFilter(original -> original.append(ctx -> {
        customCancellableRef.set(((NettyConnectionContext) ctx).updateFlushStrategy((__, ___) -> customStrategy));
        return completed();
    })).executionStrategy(serverExecutionStrategy).listenStreaming((ctx, request, responseFactory) -> {
        if (handledFirstRequest.compareAndSet(false, true)) {
            customStrategy.afterFirstWrite(FlushStrategy.FlushSender::flush);
            return succeeded(responseFactory.ok().payloadBody(responsePublisher));
        }
        return succeeded(responseFactory.ok().payloadBody(responsePublisher2));
    }).toFuture().get();
    client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).executionStrategy(clientExecutionStrategy).buildStreaming();
    StreamingHttpResponse response = client.request(client.newRequest(GET, "/1")).toFuture().get();
    FlushStrategy.FlushSender customFlushSender = customStrategy.verifyApplied();
    Cancellable customCancellable = customCancellableRef.get();
    assertNotNull(customCancellable);
    // Verify that the custom strategy is applied and used for flushing.
    customStrategy.verifyWriteStarted();
    customStrategy.verifyItemWritten(1);
    customStrategy.verifyNoMoreInteractions();
    String payloadBodyString = "foo";
    TestSubscription testSubscription1 = new TestSubscription();
    responsePublisher.onSubscribe(testSubscription1);
    testSubscription1.awaitRequestN(1);
    responsePublisher.onNext(DEFAULT_ALLOCATOR.fromAscii(payloadBodyString));
    responsePublisher.onComplete();
    customFlushSender.flush();
    Buffer responsePayload = response.payloadBody().collect(DEFAULT_ALLOCATOR::newBuffer, (results, current) -> {
        results.writeBytes(current);
        return results;
    }).toFuture().get();
    assertEquals(payloadBodyString, responsePayload.toString(US_ASCII));
    customStrategy.verifyItemWritten(2);
    customStrategy.verifyWriteTerminated();
    // Restore the default flush strategy, which should flush on each
    customCancellable.cancel();
    StreamingHttpResponse response2 = client.request(client.newRequest(GET, "/2")).toFuture().get();
    TestSubscription testSubscription2 = new TestSubscription();
    responsePublisher2.onSubscribe(testSubscription2);
    responsePublisher2.onNext(DEFAULT_ALLOCATOR.fromAscii(payloadBodyString));
    responsePublisher2.onComplete();
    responsePayload = response2.payloadBody().collect(DEFAULT_ALLOCATOR::newBuffer, (results, current) -> {
        results.writeBytes(current);
        return results;
    }).toFuture().get();
    assertEquals(payloadBodyString, responsePayload.toString(US_ASCII));
}
Also used : FlushStrategy(io.servicetalk.transport.netty.internal.FlushStrategy) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) DEFAULT_ALLOCATOR(io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Cancellable(io.servicetalk.concurrent.Cancellable) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpExecutionStrategies.defaultStrategy(io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) NettyConnectionContext(io.servicetalk.transport.netty.internal.NettyConnectionContext) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MethodSource(org.junit.jupiter.params.provider.MethodSource) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ExecutionContextExtension.immediate(io.servicetalk.transport.netty.internal.ExecutionContextExtension.immediate) ServerContext(io.servicetalk.transport.api.ServerContext) ExecutionContextExtension(io.servicetalk.transport.netty.internal.ExecutionContextExtension) AsyncCloseables.newCompositeCloseable(io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable) Arguments(org.junit.jupiter.params.provider.Arguments) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) GET(io.servicetalk.http.api.HttpRequestMethod.GET) US_ASCII(java.nio.charset.StandardCharsets.US_ASCII) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Buffer(io.servicetalk.buffer.api.Buffer) Stream(java.util.stream.Stream) Completable.completed(io.servicetalk.concurrent.api.Completable.completed) MockFlushStrategy(io.servicetalk.transport.netty.internal.MockFlushStrategy) HttpExecutionStrategies.offloadNever(io.servicetalk.http.api.HttpExecutionStrategies.offloadNever) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) Buffer(io.servicetalk.buffer.api.Buffer) Cancellable(io.servicetalk.concurrent.Cancellable) MockFlushStrategy(io.servicetalk.transport.netty.internal.MockFlushStrategy) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DEFAULT_ALLOCATOR(io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR) FlushStrategy(io.servicetalk.transport.netty.internal.FlushStrategy) MockFlushStrategy(io.servicetalk.transport.netty.internal.MockFlushStrategy) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 9 with TestSubscription

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

the class NettyPipelinedConnectionTest method readCancelErrorsPendingReadCancelsPendingWrite.

@Test
void readCancelErrorsPendingReadCancelsPendingWrite() 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());
    // cancelling an active read will close the connection.
    readSubscription.cancel();
    // readSubscriber was cancelled, so it may or may not terminate, but other sources that have not terminated
    // should be terminated, cancelled, or not subscribed.
    assertThat(readSubscriber2.awaitOnError(), is(instanceOf(ClosedChannelException.class)));
    writePublisher1Subscription.awaitCancelled();
    assertFalse(writePublisher2.isSubscribed());
    assertFalse(channel.isOpen());
}
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)

Example 10 with TestSubscription

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

the class NettyPipelinedConnectionTest method readCancelClosesConnectionThenWriteDoesNotSubscribe.

@Test
void readCancelClosesConnectionThenWriteDoesNotSubscribe() throws Exception {
    TestSubscription writePublisher1Subscription = new TestSubscription();
    toSource(requester.write(writePublisher1.afterSubscription(() -> writePublisher1Subscription))).subscribe(readSubscriber);
    Subscription readSubscription = readSubscriber.awaitSubscription();
    readSubscription.request(1);
    assertTrue(writePublisher1.isSubscribed());
    // cancelling an active read will close the connection.
    readSubscription.cancel();
    // readSubscriber was cancelled, so it may or may not terminate, but other sources that have not terminated
    // should be terminated, cancelled, or not subscribed.
    writePublisher1Subscription.awaitCancelled();
    assertFalse(channel.isOpen());
    toSource(requester.write(writePublisher2)).subscribe(readSubscriber2);
    assertThat(readSubscriber2.awaitOnError(), is(instanceOf(ClosedChannelException.class)));
    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