Search in sources :

Example 1 with UNSUPPORTED_PROTOCOL_CLOSE_HANDLER

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

the class WriteStreamSubscriberOutOfEventloopTest method testTerminalOrder.

@Test
void testTerminalOrder() throws Exception {
    Processor subject = newCompletableProcessor();
    CompletableSource.Subscriber subscriber = new CompletableSource.Subscriber() {

        @Override
        public void onSubscribe(Cancellable cancellable) {
        // noop
        }

        @Override
        public void onComplete() {
            subject.onComplete();
        }

        @Override
        public void onError(Throwable t) {
            if (pendingFlush.contains(1)) {
                subject.onError(t);
            } else {
                subject.onError(new IllegalStateException("The expected object wasn't written before termination!", t));
            }
        }
    };
    WriteDemandEstimator demandEstimator = mock(WriteDemandEstimator.class);
    this.subscriber = new WriteStreamSubscriber(channel, demandEstimator, subscriber, UNSUPPORTED_PROTOCOL_CLOSE_HANDLER, NoopWriteObserver.INSTANCE, identity(), false, __ -> false);
    this.subscriber.onNext(1);
    this.subscriber.onError(DELIBERATE_EXCEPTION);
    try {
        fromSource(subject).toFuture().get();
        fail();
    } catch (ExecutionException cause) {
        assertSame(DELIBERATE_EXCEPTION, cause.getCause());
    }
}
Also used : UNSUPPORTED_PROTOCOL_CLOSE_HANDLER(io.servicetalk.transport.netty.internal.CloseHandler.UNSUPPORTED_PROTOCOL_CLOSE_HANDLER) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Matchers.empty(org.hamcrest.Matchers.empty) UnaryOperator.identity(java.util.function.UnaryOperator.identity) Cancellable(io.servicetalk.concurrent.Cancellable) EventLoop(io.netty.channel.EventLoop) CompletableSource(io.servicetalk.concurrent.CompletableSource) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) NoopWriteObserver(io.servicetalk.transport.netty.internal.NoopTransportObserver.NoopWriteObserver) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) SourceAdapters.fromSource(io.servicetalk.concurrent.api.SourceAdapters.fromSource) Processor(io.servicetalk.concurrent.CompletableSource.Processor) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) Processors.newCompletableProcessor(io.servicetalk.concurrent.api.Processors.newCompletableProcessor) Mockito.mock(org.mockito.Mockito.mock) Processor(io.servicetalk.concurrent.CompletableSource.Processor) Processors.newCompletableProcessor(io.servicetalk.concurrent.api.Processors.newCompletableProcessor) Cancellable(io.servicetalk.concurrent.Cancellable) CompletableSource(io.servicetalk.concurrent.CompletableSource) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 2 with UNSUPPORTED_PROTOCOL_CLOSE_HANDLER

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

the class TcpConnectorTest method testRegisteredAndActiveEventsFired.

@Test
void testRegisteredAndActiveEventsFired() throws Exception {
    final CountDownLatch registeredLatch = new CountDownLatch(1);
    final CountDownLatch activeLatch = new CountDownLatch(1);
    CloseHandler closeHandler = UNSUPPORTED_PROTOCOL_CLOSE_HANDLER;
    NettyConnection<Buffer, Buffer> connection = TcpConnector.<NettyConnection<Buffer, Buffer>>connect(null, serverContext.listenAddress(), new TcpClientConfig().asReadOnly(), false, CLIENT_CTX, (channel, connectionObserver) -> DefaultNettyConnection.initChannel(channel, CLIENT_CTX.bufferAllocator(), CLIENT_CTX.executor(), CLIENT_CTX.ioExecutor(), closeHandler, defaultFlushStrategy(), null, channel2 -> channel2.pipeline().addLast(new ChannelInboundHandlerAdapter() {

        @Override
        public void channelRegistered(ChannelHandlerContext ctx) {
            registeredLatch.countDown();
            ctx.fireChannelRegistered();
        }

        @Override
        public void channelActive(ChannelHandlerContext ctx) {
            activeLatch.countDown();
            ctx.fireChannelActive();
        }

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
            ctx.fireChannelRead(msg);
            closeHandler.protocolPayloadEndInbound(ctx);
        }
    }), CLIENT_CTX.executionStrategy(), mock(Protocol.class), connectionObserver, true, __ -> false), NoopTransportObserver.INSTANCE).toFuture().get();
    connection.closeAsync().toFuture().get();
    registeredLatch.await();
    activeLatch.await();
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) UNSUPPORTED_PROTOCOL_CLOSE_HANDLER(io.servicetalk.transport.netty.internal.CloseHandler.UNSUPPORTED_PROTOCOL_CLOSE_HANDLER) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BeforeEach(org.junit.jupiter.api.BeforeEach) Protocol(io.servicetalk.transport.api.ConnectionInfo.Protocol) Charset.defaultCharset(java.nio.charset.Charset.defaultCharset) Publisher(io.servicetalk.concurrent.api.Publisher) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Is.is(org.hamcrest.core.Is.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) RetryableConnectException(io.servicetalk.client.api.RetryableConnectException) ClosedChannelException(java.nio.channels.ClosedChannelException) DefaultNettyConnection(io.servicetalk.transport.netty.internal.DefaultNettyConnection) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) CountDownLatch(java.util.concurrent.CountDownLatch) Buffer(io.servicetalk.buffer.api.Buffer) FlushStrategies.defaultFlushStrategy(io.servicetalk.transport.netty.internal.FlushStrategies.defaultFlushStrategy) NettyConnection(io.servicetalk.transport.netty.internal.NettyConnection) Matchers.anyOf(org.hamcrest.Matchers.anyOf) CloseHandler(io.servicetalk.transport.netty.internal.CloseHandler) NoopTransportObserver(io.servicetalk.transport.netty.internal.NoopTransportObserver) Mockito.mock(org.mockito.Mockito.mock) DefaultNettyConnection(io.servicetalk.transport.netty.internal.DefaultNettyConnection) NettyConnection(io.servicetalk.transport.netty.internal.NettyConnection) CloseHandler(io.servicetalk.transport.netty.internal.CloseHandler) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 3 with UNSUPPORTED_PROTOCOL_CLOSE_HANDLER

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

the class WriteStreamSubscriberFutureListenersTest method synchronousCompleteWrite.

@Test
void synchronousCompleteWrite() throws Exception {
    Channel mockChannel = mock(Channel.class);
    EventLoop mockEventLoop = mock(EventLoop.class);
    when(mockEventLoop.inEventLoop()).thenReturn(true);
    when(mockChannel.eventLoop()).thenReturn(mockEventLoop);
    when(mockChannel.newSucceededFuture()).thenReturn(channel.newSucceededFuture());
    doAnswer((Answer<Void>) invocation -> {
        ReferenceCountUtil.release(invocation.getArgument(0));
        ChannelFutureListener listener = mock(ChannelFutureListener.class);
        listeners.add(listener);
        ChannelPromise promise = invocation.getArgument(1);
        promise.addListener(listener);
        promise.setSuccess();
        return null;
    }).when(mockChannel).write(any(), any());
    WriteDemandEstimator estimator = WriteDemandEstimators.newDefaultEstimator();
    TestCompletableSubscriber completableSubscriber = new TestCompletableSubscriber();
    WriteStreamSubscriber subscriber = new WriteStreamSubscriber(mockChannel, estimator, completableSubscriber, UNSUPPORTED_PROTOCOL_CLOSE_HANDLER, NoopWriteObserver.INSTANCE, identity(), false, __ -> false);
    subscriber.onNext(1);
    verifyListenerInvokedWithSuccess(listeners.take());
    subscriber.onNext(2);
    verifyListenerInvokedWithSuccess(listeners.take());
}
Also used : UNSUPPORTED_PROTOCOL_CLOSE_HANDLER(io.servicetalk.transport.netty.internal.CloseHandler.UNSUPPORTED_PROTOCOL_CLOSE_HANDLER) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) UnaryOperator.identity(java.util.function.UnaryOperator.identity) TestCompletableSubscriber(io.servicetalk.concurrent.test.internal.TestCompletableSubscriber) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) NoopWriteObserver(io.servicetalk.transport.netty.internal.NoopTransportObserver.NoopWriteObserver) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Answer(org.mockito.stubbing.Answer) ArgumentCaptor(org.mockito.ArgumentCaptor) ChannelPromise(io.netty.channel.ChannelPromise) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Mockito.doAnswer(org.mockito.Mockito.doAnswer) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) BlockingQueue(java.util.concurrent.BlockingQueue) Mockito.when(org.mockito.Mockito.when) EventLoop(io.netty.channel.EventLoop) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) ChannelFuture(io.netty.channel.ChannelFuture) Mockito.verify(org.mockito.Mockito.verify) Channel(io.netty.channel.Channel) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) Mockito.mock(org.mockito.Mockito.mock) EventLoop(io.netty.channel.EventLoop) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) ChannelPromise(io.netty.channel.ChannelPromise) TestCompletableSubscriber(io.servicetalk.concurrent.test.internal.TestCompletableSubscriber) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Test(org.junit.jupiter.api.Test)

Aggregations

UNSUPPORTED_PROTOCOL_CLOSE_HANDLER (io.servicetalk.transport.netty.internal.CloseHandler.UNSUPPORTED_PROTOCOL_CLOSE_HANDLER)3 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)3 Test (org.junit.jupiter.api.Test)3 Mockito.mock (org.mockito.Mockito.mock)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 EventLoop (io.netty.channel.EventLoop)2 DELIBERATE_EXCEPTION (io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION)2 NoopWriteObserver (io.servicetalk.transport.netty.internal.NoopTransportObserver.NoopWriteObserver)2 ExecutionException (java.util.concurrent.ExecutionException)2 UnaryOperator.identity (java.util.function.UnaryOperator.identity)2 Matchers.is (org.hamcrest.Matchers.is)2 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 ChannelOutboundHandlerAdapter (io.netty.channel.ChannelOutboundHandlerAdapter)1 ChannelPromise (io.netty.channel.ChannelPromise)1 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1 ReferenceCountUtil (io.netty.util.ReferenceCountUtil)1 Buffer (io.servicetalk.buffer.api.Buffer)1