Search in sources :

Example 1 with WriteDemandEstimators

use of io.servicetalk.transport.netty.internal.WriteDemandEstimators in project servicetalk by apple.

the class NettyPipelinedConnectionTest method flushStrategy.

@Test
void flushStrategy() {
    FlushStrategy flushStrategy1 = mock(FlushStrategy.class);
    FlushStrategy.WriteEventsListener writeEventsListener1 = mock(FlushStrategy.WriteEventsListener.class);
    AtomicReference<FlushStrategy.FlushSender> sender1Ref = new AtomicReference<>();
    doAnswer((Answer<FlushStrategy.WriteEventsListener>) invocation -> {
        sender1Ref.compareAndSet(null, invocation.getArgument(0, FlushStrategy.FlushSender.class));
        return writeEventsListener1;
    }).when(flushStrategy1).apply(any());
    FlushStrategy flushStrategy2 = mock(FlushStrategy.class);
    FlushStrategy.WriteEventsListener writeEventsListener2 = mock(FlushStrategy.WriteEventsListener.class);
    AtomicReference<FlushStrategy.FlushSender> sender2Ref = new AtomicReference<>();
    doAnswer((Answer<FlushStrategy.WriteEventsListener>) invocation -> {
        sender2Ref.compareAndSet(null, invocation.getArgument(0, FlushStrategy.FlushSender.class));
        return writeEventsListener2;
    }).when(flushStrategy2).apply(any());
    toSource(requester.write(writePublisher1, () -> flushStrategy1, WriteDemandEstimators::newDefaultEstimator)).subscribe(readSubscriber);
    toSource(requester.write(writePublisher2, () -> flushStrategy2, WriteDemandEstimators::newDefaultEstimator)).subscribe(readSubscriber2);
    readSubscriber.awaitSubscription().request(1);
    assertTrue(writePublisher1.isSubscribed());
    assertFalse(writePublisher2.isSubscribed());
    verify(writeEventsListener1).writeStarted();
    FlushStrategy.FlushSender sender1 = sender1Ref.get();
    assertNotNull(sender1);
    writePublisher1.onNext(1);
    sender1.flush();
    verify(writeEventsListener1).itemWritten(eq(1));
    writePublisher1.onComplete();
    verify(writeEventsListener1).writeTerminated();
    assertTrue(writePublisher2.isSubscribed());
    verify(writeEventsListener2).writeStarted();
    FlushStrategy.FlushSender sender2 = sender1Ref.get();
    assertNotNull(sender2);
    writePublisher2.onNext(2);
    sender2.flush();
    verify(writeEventsListener2).itemWritten(eq(2));
    writePublisher2.onComplete();
    verify(writeEventsListener2).writeTerminated();
}
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) FlushStrategies.defaultFlushStrategy(io.servicetalk.transport.netty.internal.FlushStrategies.defaultFlushStrategy) FlushStrategy(io.servicetalk.transport.netty.internal.FlushStrategy) WriteDemandEstimators(io.servicetalk.transport.netty.internal.WriteDemandEstimators) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Aggregations

ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 DEFAULT_ALLOCATOR (io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR)1 CompletableSource (io.servicetalk.concurrent.CompletableSource)1 PublisherSource (io.servicetalk.concurrent.PublisherSource)1 Subscription (io.servicetalk.concurrent.PublisherSource.Subscription)1 Completable (io.servicetalk.concurrent.api.Completable)1 Completable.completed (io.servicetalk.concurrent.api.Completable.completed)1 Executor (io.servicetalk.concurrent.api.Executor)1 Executors (io.servicetalk.concurrent.api.Executors)1 Executors.immediate (io.servicetalk.concurrent.api.Executors.immediate)1 Publisher (io.servicetalk.concurrent.api.Publisher)1 Single (io.servicetalk.concurrent.api.Single)1 SourceAdapters.toSource (io.servicetalk.concurrent.api.SourceAdapters.toSource)1 TestPublisher (io.servicetalk.concurrent.api.TestPublisher)1 TestSubscription (io.servicetalk.concurrent.api.TestSubscription)1 DELIBERATE_EXCEPTION (io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION)1 SubscriberUtils.deliverCompleteFromSource (io.servicetalk.concurrent.internal.SubscriberUtils.deliverCompleteFromSource)1 TestPublisherSubscriber (io.servicetalk.concurrent.test.internal.TestPublisherSubscriber)1 HttpExecutionStrategies.defaultStrategy (io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy)1