Search in sources :

Example 86 with ChannelPromise

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

the class StreamBufferingEncoderTest method closedBufferedStreamReleasesByteBuf.

@Test
public void closedBufferedStreamReleasesByteBuf() {
    encoder.writeSettingsAck(ctx, newPromise());
    setMaxConcurrentStreams(0);
    ByteBuf data = mock(ByteBuf.class);
    ChannelFuture f1 = encoderWriteHeaders(3, newPromise());
    assertEquals(1, encoder.numBufferedStreams());
    ChannelFuture f2 = encoder.writeData(ctx, 3, data, 0, false, newPromise());
    ChannelPromise rstPromise = mock(ChannelPromise.class);
    encoder.writeRstStream(ctx, 3, CANCEL.code(), rstPromise);
    assertEquals(0, encoder.numBufferedStreams());
    verify(rstPromise).setSuccess();
    assertTrue(f1.isSuccess());
    assertTrue(f2.isSuccess());
    verify(data).release();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 87 with ChannelPromise

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

the class Http2ConnectionHandler method onConnectionError.

/**
 * Handler for a connection error. Sends a GO_AWAY frame to the remote endpoint. Once all
 * streams are closed, the connection is shut down.
 *
 * @param ctx the channel context
 * @param outbound {@code true} if the error was caused by an outbound operation.
 * @param cause the exception that was caught
 * @param http2Ex the {@link Http2Exception} that is embedded in the causality chain. This may
 *            be {@code null} if it's an unknown exception.
 */
protected void onConnectionError(ChannelHandlerContext ctx, boolean outbound, Throwable cause, Http2Exception http2Ex) {
    if (http2Ex == null) {
        http2Ex = new Http2Exception(INTERNAL_ERROR, cause.getMessage(), cause);
    }
    ChannelPromise promise = ctx.newPromise();
    ChannelFuture future = goAway(ctx, http2Ex, ctx.newPromise());
    if (http2Ex.shutdownHint() == Http2Exception.ShutdownHint.GRACEFUL_SHUTDOWN) {
        doGracefulShutdown(ctx, future, promise);
    } else {
        future.addListener(newClosingChannelFutureListener(ctx, promise));
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Http2CodecUtil.getEmbeddedHttp2Exception(io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception) ChannelPromise(io.netty.channel.ChannelPromise)

Example 88 with ChannelPromise

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

the class AbstractBootstrap method doBind.

private ChannelFuture doBind(final SocketAddress localAddress) {
    final ChannelFuture regFuture = initAndRegister();
    final Channel channel = regFuture.channel();
    if (regFuture.cause() != null) {
        return regFuture;
    }
    if (regFuture.isDone()) {
        // At this point we know that the registration was complete and successful.
        ChannelPromise promise = channel.newPromise();
        doBind0(regFuture, channel, localAddress, promise);
        return promise;
    } else {
        // Registration future is almost always fulfilled already, but just in case it's not.
        final PendingRegistrationPromise promise = new PendingRegistrationPromise(channel);
        regFuture.addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                Throwable cause = future.cause();
                if (cause != null) {
                    // Registration on the EventLoop failed so fail the ChannelPromise directly to not cause an
                    // IllegalStateException once we try to access the EventLoop of the Channel.
                    promise.setFailure(cause);
                } else {
                    // Registration was successful, so set the correct executor to use.
                    // See https://github.com/netty/netty/issues/2586
                    promise.registered();
                    doBind0(regFuture, channel, localAddress, promise);
                }
            }
        });
        return promise;
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 89 with ChannelPromise

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

the class WebSocketProtocolHandlerTest method testTimeout.

@Test
public void testTimeout() throws Exception {
    final AtomicReference<ChannelPromise> ref = new AtomicReference<ChannelPromise>();
    WebSocketProtocolHandler handler = new WebSocketProtocolHandler(false, WebSocketCloseStatus.NORMAL_CLOSURE, 1) {
    };
    EmbeddedChannel channel = new EmbeddedChannel(new ChannelOutboundHandlerAdapter() {

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
            ref.set(promise);
            ReferenceCountUtil.release(msg);
        }
    }, handler);
    ChannelFuture future = channel.writeAndFlush(new CloseWebSocketFrame());
    ChannelHandlerContext ctx = channel.pipeline().context(WebSocketProtocolHandler.class);
    handler.close(ctx, ctx.newPromise());
    do {
        Thread.sleep(10);
        channel.runPendingTasks();
    } while (!future.isDone());
    assertThat(future.cause(), Matchers.instanceOf(WebSocketHandshakeException.class));
    assertFalse(ref.get().isDone());
    assertFalse(channel.finish());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) ChannelPromise(io.netty.channel.ChannelPromise) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Test(org.junit.jupiter.api.Test)

Example 90 with ChannelPromise

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

the class WebSocketServerExtensionHandlerTest method testExtensionHandlerNotRemovedByFailureWritePromise.

@Test
public void testExtensionHandlerNotRemovedByFailureWritePromise() {
    // initialize
    when(mainHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("main"))).thenReturn(mainExtensionMock);
    when(mainExtensionMock.newReponseData()).thenReturn(new WebSocketExtensionData("main", Collections.<String, String>emptyMap()));
    // execute
    WebSocketServerExtensionHandler extensionHandler = new WebSocketServerExtensionHandler(mainHandshakerMock);
    EmbeddedChannel ch = new EmbeddedChannel(extensionHandler);
    HttpRequest req = newUpgradeRequest("main");
    ch.writeInbound(req);
    HttpResponse res = newUpgradeResponse(null);
    ChannelPromise failurePromise = ch.newPromise();
    ch.writeOneOutbound(res, failurePromise);
    failurePromise.setFailure(new IOException("Cannot write response"));
    // test
    assertNull(ch.readOutbound());
    assertNotNull(ch.pipeline().context(extensionHandler));
    assertTrue(ch.finish());
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpResponse(io.netty.handler.codec.http.HttpResponse) ChannelPromise(io.netty.channel.ChannelPromise) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

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