Search in sources :

Example 16 with Completable

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

the class NettyPipelinedConnectionTest method readSubscribeThrowsWritesStillProcessed.

@Test
void readSubscribeThrowsWritesStillProcessed() {
    AtomicBoolean thrownError = new AtomicBoolean();
    Publisher<Integer> mockReadPublisher = new Publisher<Integer>() {

        @Override
        protected void handleSubscribe(final PublisherSource.Subscriber<? super Integer> subscriber) {
            if (thrownError.compareAndSet(false, true)) {
                throw DELIBERATE_EXCEPTION;
            } else {
                deliverCompleteFromSource(subscriber);
            }
        }
    };
    @SuppressWarnings("unchecked") NettyConnection<Integer, Integer> mockConnection = mock(NettyConnection.class);
    when(mockConnection.read()).thenReturn(mockReadPublisher);
    doAnswer((Answer<Completable>) invocation -> {
        Publisher<Integer> writePub = invocation.getArgument(0);
        return writePub.ignoreElements();
    }).when(mockConnection).write(any(), any(), any());
    requester = new NettyPipelinedConnection<>(mockConnection, 2);
    toSource(requester.write(writePublisher1)).subscribe(readSubscriber);
    toSource(requester.write(writePublisher2)).subscribe(readSubscriber2);
    Subscription readSubscription = readSubscriber.awaitSubscription();
    readSubscription.request(1);
    assertTrue(writePublisher1.isSubscribed());
    writePublisher1.onError(newSecondException());
    assertThat(readSubscriber.awaitOnError(), is(DELIBERATE_EXCEPTION));
    readSubscriber2.awaitSubscription();
    assertTrue(writePublisher2.isSubscribed());
    writePublisher2.onComplete();
    readSubscriber2.awaitOnComplete();
    verify(mockConnection, never()).closeAsync();
}
Also used : UNSUPPORTED_PROTOCOL_CLOSE_HANDLER(io.servicetalk.transport.netty.internal.CloseHandler.UNSUPPORTED_PROTOCOL_CLOSE_HANDLER) BeforeEach(org.junit.jupiter.api.BeforeEach) Protocol(io.servicetalk.transport.api.ConnectionInfo.Protocol) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) MAX_VALUE(java.lang.Integer.MAX_VALUE) Future(java.util.concurrent.Future) HttpExecutionStrategies.defaultStrategy(io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Executor(io.servicetalk.concurrent.api.Executor) Executors.immediate(io.servicetalk.concurrent.api.Executors.immediate) TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) CyclicBarrier(java.util.concurrent.CyclicBarrier) SynchronousQueue(java.util.concurrent.SynchronousQueue) PublisherSource(io.servicetalk.concurrent.PublisherSource) Collection(java.util.Collection) DefaultNettyConnection(io.servicetalk.transport.netty.internal.DefaultNettyConnection) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) SubscriberUtils.deliverCompleteFromSource(io.servicetalk.concurrent.internal.SubscriberUtils.deliverCompleteFromSource) RetryableException(io.servicetalk.transport.api.RetryableException) WriteDemandEstimators(io.servicetalk.transport.netty.internal.WriteDemandEstimators) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) FlushStrategies.defaultFlushStrategy(io.servicetalk.transport.netty.internal.FlushStrategies.defaultFlushStrategy) Matchers.is(org.hamcrest.Matchers.is) CloseHandler(io.servicetalk.transport.netty.internal.CloseHandler) NoopConnectionObserver(io.servicetalk.transport.netty.internal.NoopTransportObserver.NoopConnectionObserver) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) FlushStrategy(io.servicetalk.transport.netty.internal.FlushStrategy) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) DEFAULT_ALLOCATOR(io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Publisher(io.servicetalk.concurrent.api.Publisher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Answer(org.mockito.stubbing.Answer) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) ExecutorService(java.util.concurrent.ExecutorService) ClosedChannelException(java.nio.channels.ClosedChannelException) Single(io.servicetalk.concurrent.api.Single) Completable(io.servicetalk.concurrent.api.Completable) EmbeddedDuplexChannel(io.servicetalk.transport.netty.internal.EmbeddedDuplexChannel) Mockito.when(org.mockito.Mockito.when) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) SourceAdapters.toSource(io.servicetalk.concurrent.api.SourceAdapters.toSource) CompletableSource(io.servicetalk.concurrent.CompletableSource) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) WriteDemandEstimator(io.servicetalk.transport.netty.internal.WriteDemandEstimator) Completable.completed(io.servicetalk.concurrent.api.Completable.completed) Executors(io.servicetalk.concurrent.api.Executors) NettyConnection(io.servicetalk.transport.netty.internal.NettyConnection) SECONDS(java.util.concurrent.TimeUnit.SECONDS) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Completable(io.servicetalk.concurrent.api.Completable) TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) Publisher(io.servicetalk.concurrent.api.Publisher) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) Test(org.junit.jupiter.api.Test)

Example 17 with Completable

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

the class NettyPipelinedConnectionTest method multiThreadedWritesAllComplete.

@Test
void multiThreadedWritesAllComplete() throws Exception {
    // Avoid using EmbeddedChannel because it is not thread safe. This test writes/reads from multiple threads.
    @SuppressWarnings("unchecked") NettyConnection<Integer, Integer> connection = mock(NettyConnection.class);
    Executor connectionExecutor = Executors.newCachedThreadExecutor();
    try {
        doAnswer((Answer<Completable>) invocation -> {
            Publisher<Integer> writeStream = invocation.getArgument(0);
            return writeStream.ignoreElements().concat(connectionExecutor.submit(() -> {
            }));
        }).when(connection).write(any(), any(), any());
        doAnswer((Answer<Publisher<Integer>>) invocation -> connectionExecutor.submit(() -> {
        }).concat(Publisher.from(1))).when(connection).read();
        final int concurrentRequestCount = 300;
        NettyPipelinedConnection<Integer, Integer> pipelinedConnection = new NettyPipelinedConnection<>(connection, concurrentRequestCount);
        CyclicBarrier requestStartBarrier = new CyclicBarrier(concurrentRequestCount);
        List<Future<Collection<Integer>>> futures = new ArrayList<>(concurrentRequestCount);
        ExecutorService executor = new ThreadPoolExecutor(0, concurrentRequestCount, 1, SECONDS, new SynchronousQueue<>());
        try {
            for (int i = 0; i < concurrentRequestCount; ++i) {
                final int finalI = i;
                futures.add(executor.submit(() -> {
                    try {
                        requestStartBarrier.await();
                    } catch (Exception e) {
                        return Single.<Collection<Integer>>failed(new AssertionError("failure during request " + finalI, e)).toFuture().get();
                    }
                    return pipelinedConnection.write(Publisher.from(finalI)).toFuture().get();
                }));
            }
            for (Future<Collection<Integer>> future : futures) {
                assertThat(future.get(), hasSize(1));
            }
        } finally {
            executor.shutdown();
        }
    } finally {
        connectionExecutor.closeAsync().subscribe();
    }
}
Also used : UNSUPPORTED_PROTOCOL_CLOSE_HANDLER(io.servicetalk.transport.netty.internal.CloseHandler.UNSUPPORTED_PROTOCOL_CLOSE_HANDLER) BeforeEach(org.junit.jupiter.api.BeforeEach) Protocol(io.servicetalk.transport.api.ConnectionInfo.Protocol) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) MAX_VALUE(java.lang.Integer.MAX_VALUE) Future(java.util.concurrent.Future) HttpExecutionStrategies.defaultStrategy(io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Executor(io.servicetalk.concurrent.api.Executor) Executors.immediate(io.servicetalk.concurrent.api.Executors.immediate) TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) CyclicBarrier(java.util.concurrent.CyclicBarrier) SynchronousQueue(java.util.concurrent.SynchronousQueue) PublisherSource(io.servicetalk.concurrent.PublisherSource) Collection(java.util.Collection) DefaultNettyConnection(io.servicetalk.transport.netty.internal.DefaultNettyConnection) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) SubscriberUtils.deliverCompleteFromSource(io.servicetalk.concurrent.internal.SubscriberUtils.deliverCompleteFromSource) RetryableException(io.servicetalk.transport.api.RetryableException) WriteDemandEstimators(io.servicetalk.transport.netty.internal.WriteDemandEstimators) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) FlushStrategies.defaultFlushStrategy(io.servicetalk.transport.netty.internal.FlushStrategies.defaultFlushStrategy) Matchers.is(org.hamcrest.Matchers.is) CloseHandler(io.servicetalk.transport.netty.internal.CloseHandler) NoopConnectionObserver(io.servicetalk.transport.netty.internal.NoopTransportObserver.NoopConnectionObserver) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) FlushStrategy(io.servicetalk.transport.netty.internal.FlushStrategy) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) DEFAULT_ALLOCATOR(io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Publisher(io.servicetalk.concurrent.api.Publisher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Answer(org.mockito.stubbing.Answer) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) ExecutorService(java.util.concurrent.ExecutorService) ClosedChannelException(java.nio.channels.ClosedChannelException) Single(io.servicetalk.concurrent.api.Single) Completable(io.servicetalk.concurrent.api.Completable) EmbeddedDuplexChannel(io.servicetalk.transport.netty.internal.EmbeddedDuplexChannel) Mockito.when(org.mockito.Mockito.when) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) SourceAdapters.toSource(io.servicetalk.concurrent.api.SourceAdapters.toSource) CompletableSource(io.servicetalk.concurrent.CompletableSource) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) WriteDemandEstimator(io.servicetalk.transport.netty.internal.WriteDemandEstimator) Completable.completed(io.servicetalk.concurrent.api.Completable.completed) Executors(io.servicetalk.concurrent.api.Executors) NettyConnection(io.servicetalk.transport.netty.internal.NettyConnection) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Completable(io.servicetalk.concurrent.api.Completable) ArrayList(java.util.ArrayList) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) Publisher(io.servicetalk.concurrent.api.Publisher) RetryableException(io.servicetalk.transport.api.RetryableException) ClosedChannelException(java.nio.channels.ClosedChannelException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Executor(io.servicetalk.concurrent.api.Executor) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Collection(java.util.Collection) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Test(org.junit.jupiter.api.Test)

Example 18 with Completable

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

the class ChannelSetTest method closeAsyncGracefullyWithoutNettyConnectionChannelHandler.

@Test
void closeAsyncGracefullyWithoutNettyConnectionChannelHandler() {
    when(mockClosableAttribute.getAndSet(any())).thenReturn(null);
    Completable completable = closeAsyncGracefully(fixture, 100, SECONDS);
    verify(channel, never()).close();
    TestCompletableSubscriber subscriber = new TestCompletableSubscriber();
    toSource(completable).subscribe(subscriber);
    verify(channel).close();
    subscriber.awaitOnComplete();
}
Also used : Completable(io.servicetalk.concurrent.api.Completable) TestCompletableSubscriber(io.servicetalk.concurrent.test.internal.TestCompletableSubscriber) Test(org.junit.jupiter.api.Test)

Example 19 with Completable

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

the class ChannelSetTest method testCloseAsyncGracefullyTwice.

@Test
void testCloseAsyncGracefullyTwice() throws Exception {
    Completable gracefulCompletable1 = closeAsyncGracefully(fixture, 60, SECONDS);
    Completable gracefulCompletable2 = closeAsyncGracefully(fixture, 60, SECONDS);
    TestCompletableSubscriber subscriber = new TestCompletableSubscriber();
    toSource(gracefulCompletable1).subscribe(subscriber);
    verify(nettyConnection).closeAsyncGracefullyNoOffload();
    TestCompletableSubscriber subscriber2 = new TestCompletableSubscriber();
    toSource(gracefulCompletable2).subscribe(subscriber2);
    verify(nettyConnection, times(1)).closeAsyncGracefullyNoOffload();
    assertThat(subscriber.pollTerminal(10, MILLISECONDS), is(nullValue()));
    closeAsyncGracefullyCompletable.onComplete();
    assertThat(subscriber.pollTerminal(10, MILLISECONDS), is(nullValue()));
    listener.operationComplete(channelCloseFuture);
    fixture.onClose().toFuture().get();
    subscriber.awaitOnComplete();
    subscriber2.awaitOnComplete();
}
Also used : Completable(io.servicetalk.concurrent.api.Completable) TestCompletableSubscriber(io.servicetalk.concurrent.test.internal.TestCompletableSubscriber) Test(org.junit.jupiter.api.Test)

Example 20 with Completable

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

the class ChannelSetTest method testCloseAsyncThenCloseAsyncGracefully.

@Test
void testCloseAsyncThenCloseAsyncGracefully() throws Exception {
    Completable closeCompletable = fixture.closeAsync();
    Completable gracefulCompletable = closeAsyncGracefully(fixture, 100, SECONDS);
    TestCompletableSubscriber subscriber = new TestCompletableSubscriber();
    toSource(closeCompletable).subscribe(subscriber);
    verify(channel).close();
    TestCompletableSubscriber subscriber2 = new TestCompletableSubscriber();
    toSource(gracefulCompletable).subscribe(subscriber2);
    fixture.onClose().toFuture().get();
    subscriber.awaitOnComplete();
    subscriber2.awaitOnComplete();
}
Also used : Completable(io.servicetalk.concurrent.api.Completable) TestCompletableSubscriber(io.servicetalk.concurrent.test.internal.TestCompletableSubscriber) Test(org.junit.jupiter.api.Test)

Aggregations

Completable (io.servicetalk.concurrent.api.Completable)58 Test (org.junit.jupiter.api.Test)45 TestCompletable (io.servicetalk.concurrent.api.TestCompletable)17 TestCompletableSubscriber (io.servicetalk.concurrent.test.internal.TestCompletableSubscriber)15 ContextAwareRetryingHttpClientFilter (io.servicetalk.http.netty.RetryingHttpRequesterFilter.ContextAwareRetryingHttpClientFilter)14 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)14 Matchers.is (org.hamcrest.Matchers.is)13 SourceAdapters.toSource (io.servicetalk.concurrent.api.SourceAdapters.toSource)12 Single (io.servicetalk.concurrent.api.Single)11 DELIBERATE_EXCEPTION (io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION)11 CompletableSource (io.servicetalk.concurrent.CompletableSource)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)10 Mockito.mock (org.mockito.Mockito.mock)10 DEFAULT_ALLOCATOR (io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR)9 Completable.completed (io.servicetalk.concurrent.api.Completable.completed)9 Executor (io.servicetalk.concurrent.api.Executor)9 BeforeEach (org.junit.jupiter.api.BeforeEach)9 Answer (org.mockito.stubbing.Answer)9 Executors.immediate (io.servicetalk.concurrent.api.Executors.immediate)8