Search in sources :

Example 56 with ChannelPromise

use of io.netty.channel.ChannelPromise in project netty by netty.

the class AbstractNioChannel method doClose.

@Override
protected void doClose() throws Exception {
    ChannelPromise promise = connectPromise;
    if (promise != null) {
        // Use tryFailure() instead of setFailure() to avoid the race against cancel().
        promise.tryFailure(new ClosedChannelException());
        connectPromise = null;
    }
    Future<?> future = connectTimeoutFuture;
    if (future != null) {
        future.cancel(false);
        connectTimeoutFuture = null;
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) ChannelPromise(io.netty.channel.ChannelPromise)

Example 57 with ChannelPromise

use of io.netty.channel.ChannelPromise in project netty by netty.

the class WebSocketClientProtocolHandshakeHandler method applyHandshakeTimeout.

private void applyHandshakeTimeout() {
    final ChannelPromise localHandshakePromise = handshakePromise;
    if (handshakeTimeoutMillis <= 0 || localHandshakePromise.isDone()) {
        return;
    }
    final Future<?> timeoutFuture = ctx.executor().schedule(new Runnable() {

        @Override
        public void run() {
            if (localHandshakePromise.isDone()) {
                return;
            }
            if (localHandshakePromise.tryFailure(new WebSocketClientHandshakeException("handshake timed out"))) {
                ctx.flush().fireUserEventTriggered(ClientHandshakeStateEvent.HANDSHAKE_TIMEOUT).close();
            }
        }
    }, handshakeTimeoutMillis, TimeUnit.MILLISECONDS);
    // Cancel the handshake timeout when handshake is finished.
    localHandshakePromise.addListener(new FutureListener<Void>() {

        @Override
        public void operationComplete(Future<Void> f) throws Exception {
            timeoutFuture.cancel(false);
        }
    });
}
Also used : ChannelPromise(io.netty.channel.ChannelPromise)

Example 58 with ChannelPromise

use of io.netty.channel.ChannelPromise in project netty by netty.

the class MessageToMessageEncoderTest method testIntermediateWriteFailures.

@Test
public void testIntermediateWriteFailures() {
    ChannelHandler encoder = new MessageToMessageEncoder<Object>() {

        @Override
        protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) {
            out.add(new Object());
            out.add(msg);
        }
    };
    final Exception firstWriteException = new Exception();
    ChannelHandler writeThrower = new ChannelOutboundHandlerAdapter() {

        private boolean firstWritten;

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
            if (firstWritten) {
                ctx.write(msg, promise);
            } else {
                firstWritten = true;
                promise.setFailure(firstWriteException);
            }
        }
    };
    EmbeddedChannel channel = new EmbeddedChannel(writeThrower, encoder);
    Object msg = new Object();
    ChannelFuture write = channel.writeAndFlush(msg);
    assertSame(firstWriteException, write.cause());
    assertSame(msg, channel.readOutbound());
    assertFalse(channel.finish());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) List(java.util.List) ChannelPromise(io.netty.channel.ChannelPromise) ChannelHandler(io.netty.channel.ChannelHandler) Test(org.junit.jupiter.api.Test)

Example 59 with ChannelPromise

use of io.netty.channel.ChannelPromise in project netty by netty.

the class AbstractHttp2StreamChannel method write0.

protected ChannelFuture write0(ChannelHandlerContext ctx, Object msg) {
    ChannelPromise promise = ctx.newPromise();
    ctx.write(msg, promise);
    return promise;
}
Also used : ChannelPromise(io.netty.channel.ChannelPromise) VoidChannelPromise(io.netty.channel.VoidChannelPromise)

Example 60 with ChannelPromise

use of io.netty.channel.ChannelPromise in project netty by netty.

the class SslHandlerTest method testNonApplicationDataFailureFailsQueuedWrites.

@Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testNonApplicationDataFailureFailsQueuedWrites() throws NoSuchAlgorithmException, InterruptedException {
    final CountDownLatch writeLatch = new CountDownLatch(1);
    final Queue<ChannelPromise> writesToFail = new ConcurrentLinkedQueue<ChannelPromise>();
    SSLEngine engine = newClientModeSSLEngine();
    SslHandler handler = new SslHandler(engine) {

        @Override
        public void write(final ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            super.write(ctx, msg, promise);
            writeLatch.countDown();
        }
    };
    EmbeddedChannel ch = new EmbeddedChannel(new ChannelDuplexHandler() {

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
            if (msg instanceof ByteBuf) {
                if (((ByteBuf) msg).isReadable()) {
                    writesToFail.add(promise);
                } else {
                    promise.setSuccess();
                }
            }
            ReferenceCountUtil.release(msg);
        }
    }, handler);
    try {
        final CountDownLatch writeCauseLatch = new CountDownLatch(1);
        final AtomicReference<Throwable> failureRef = new AtomicReference<Throwable>();
        ch.write(Unpooled.wrappedBuffer(new byte[] { 1 })).addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) {
                failureRef.compareAndSet(null, future.cause());
                writeCauseLatch.countDown();
            }
        });
        writeLatch.await();
        // Simulate failing the SslHandler non-application writes after there are applications writes queued.
        ChannelPromise promiseToFail;
        while ((promiseToFail = writesToFail.poll()) != null) {
            promiseToFail.setFailure(new RuntimeException("fake exception"));
        }
        writeCauseLatch.await();
        Throwable writeCause = failureRef.get();
        assertNotNull(writeCause);
        assertThat(writeCause, is(CoreMatchers.<Throwable>instanceOf(SSLException.class)));
        Throwable cause = handler.handshakeFuture().cause();
        assertNotNull(cause);
        assertThat(cause, is(CoreMatchers.<Throwable>instanceOf(SSLException.class)));
    } finally {
        assertFalse(ch.finishAndReleaseAll());
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SSLEngine(javax.net.ssl.SSLEngine) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelPromise(io.netty.channel.ChannelPromise) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Aggregations

ChannelPromise (io.netty.channel.ChannelPromise)218 Test (org.junit.jupiter.api.Test)87 ChannelFuture (io.netty.channel.ChannelFuture)62 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)59 DefaultChannelPromise (io.netty.channel.DefaultChannelPromise)57 ByteBuf (io.netty.buffer.ByteBuf)54 ChannelOutboundHandlerAdapter (io.netty.channel.ChannelOutboundHandlerAdapter)27 Test (org.junit.Test)24 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)22 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)22 Channel (io.netty.channel.Channel)21 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)21 ClosedChannelException (java.nio.channels.ClosedChannelException)20 ChannelFutureListener (io.netty.channel.ChannelFutureListener)19 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)18 InvocationOnMock (org.mockito.invocation.InvocationOnMock)18 AsciiString (io.netty.util.AsciiString)15 IOException (java.io.IOException)13 Bootstrap (io.netty.bootstrap.Bootstrap)12 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)12