Search in sources :

Example 26 with ChannelPromise

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

the class StreamBufferingEncoderTest method rstStreamClosesBufferedStream.

@Test
public void rstStreamClosesBufferedStream() {
    encoder.writeSettingsAck(ctx, newPromise());
    setMaxConcurrentStreams(0);
    encoderWriteHeaders(3, newPromise());
    assertEquals(1, encoder.numBufferedStreams());
    ChannelPromise rstStreamPromise = newPromise();
    encoder.writeRstStream(ctx, 3, CANCEL.code(), rstStreamPromise);
    assertTrue(rstStreamPromise.isSuccess());
    assertEquals(0, encoder.numBufferedStreams());
}
Also used : ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Test(org.junit.Test)

Example 27 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.Test)

Example 28 with ChannelPromise

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

the class BootstrapTest method testLateRegisterSuccessBindFailed.

@Test
public void testLateRegisterSuccessBindFailed() throws Exception {
    TestEventLoopGroup group = new TestEventLoopGroup();
    try {
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(group);
        bootstrap.channelFactory(new ChannelFactory<ServerChannel>() {

            @Override
            public ServerChannel newChannel() {
                return new LocalServerChannel() {

                    @Override
                    public ChannelFuture bind(SocketAddress localAddress) {
                        // Close the Channel to emulate what NIO and others impl do on bind failure
                        // See https://github.com/netty/netty/issues/2586
                        close();
                        return newFailedFuture(new SocketException());
                    }

                    @Override
                    public ChannelFuture bind(SocketAddress localAddress, ChannelPromise promise) {
                        // Close the Channel to emulate what NIO and others impl do on bind failure
                        // See https://github.com/netty/netty/issues/2586
                        close();
                        return promise.setFailure(new SocketException());
                    }
                };
            }
        });
        bootstrap.childHandler(new DummyHandler());
        bootstrap.localAddress(new LocalAddress("1"));
        ChannelFuture future = bootstrap.bind();
        assertFalse(future.isDone());
        group.promise.setSuccess();
        final BlockingQueue<Boolean> queue = new LinkedBlockingQueue<Boolean>();
        future.addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                queue.add(future.channel().eventLoop().inEventLoop(Thread.currentThread()));
                queue.add(future.isSuccess());
            }
        });
        assertTrue(queue.take());
        assertFalse(queue.take());
    } finally {
        group.shutdownGracefully();
        group.terminationFuture().sync();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SocketException(java.net.SocketException) LocalAddress(io.netty.channel.local.LocalAddress) ChannelPromise(io.netty.channel.ChannelPromise) LocalServerChannel(io.netty.channel.local.LocalServerChannel) ServerChannel(io.netty.channel.ServerChannel) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ChannelFutureListener(io.netty.channel.ChannelFutureListener) SocketException(java.net.SocketException) ConnectException(java.net.ConnectException) UnknownHostException(java.net.UnknownHostException) LocalServerChannel(io.netty.channel.local.LocalServerChannel) SocketAddress(java.net.SocketAddress) Test(org.junit.Test)

Example 29 with ChannelPromise

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

the class LocalChannel method doClose.

@Override
protected void doClose() throws Exception {
    final LocalChannel peer = this.peer;
    if (state != State.CLOSED) {
        // Update all internal state before the closeFuture is notified.
        if (localAddress != null) {
            if (parent() == null) {
                LocalChannelRegistry.unregister(localAddress);
            }
            localAddress = null;
        }
        // State change must happen before finishPeerRead to ensure writes are released either in doWrite or
        // channelRead.
        state = State.CLOSED;
        // Preserve order of event and force a read operation now before the close operation is processed.
        finishPeerRead(this);
        ChannelPromise promise = connectPromise;
        if (promise != null) {
            // Use tryFailure() instead of setFailure() to avoid the race against cancel().
            promise.tryFailure(DO_CLOSE_CLOSED_CHANNEL_EXCEPTION);
            connectPromise = null;
        }
    }
    if (peer != null) {
        this.peer = null;
        // Need to execute the close in the correct EventLoop (see https://github.com/netty/netty/issues/1777).
        // Also check if the registration was not done yet. In this case we submit the close to the EventLoop
        // to make sure its run after the registration completes (see https://github.com/netty/netty/issues/2144).
        EventLoop peerEventLoop = peer.eventLoop();
        final boolean peerIsActive = peer.isActive();
        if (peerEventLoop.inEventLoop() && !registerInProgress) {
            peer.tryClose(peerIsActive);
        } else {
            try {
                peerEventLoop.execute(new Runnable() {

                    @Override
                    public void run() {
                        peer.tryClose(peerIsActive);
                    }
                });
            } catch (Throwable cause) {
                logger.warn("Releasing Inbound Queues for channels {}-{} because exception occurred!", this, peer, cause);
                releaseInboundBuffers();
                if (peerEventLoop.inEventLoop()) {
                    peer.releaseInboundBuffers();
                } else {
                    // inboundBuffers is a SPSC so we may leak if the event loop is shutdown prematurely or rejects
                    // the close Runnable but give a best effort.
                    peer.close();
                }
                PlatformDependent.throwException(cause);
            }
        }
    }
}
Also used : EventLoop(io.netty.channel.EventLoop) SingleThreadEventLoop(io.netty.channel.SingleThreadEventLoop) ChannelPromise(io.netty.channel.ChannelPromise)

Example 30 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(DO_CLOSE_CLOSED_CHANNEL_EXCEPTION);
        connectPromise = null;
    }
    ScheduledFuture<?> future = connectTimeoutFuture;
    if (future != null) {
        future.cancel(false);
        connectTimeoutFuture = null;
    }
}
Also used : ChannelPromise(io.netty.channel.ChannelPromise)

Aggregations

ChannelPromise (io.netty.channel.ChannelPromise)97 Test (org.junit.Test)56 DefaultChannelPromise (io.netty.channel.DefaultChannelPromise)34 ByteBuf (io.netty.buffer.ByteBuf)29 ChannelFuture (io.netty.channel.ChannelFuture)23 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)20 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)17 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)16 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)15 AsciiString (io.netty.util.AsciiString)14 Channel (io.netty.channel.Channel)10 ClosedChannelException (java.nio.channels.ClosedChannelException)10 InvocationOnMock (org.mockito.invocation.InvocationOnMock)10 ChannelFutureListener (io.netty.channel.ChannelFutureListener)9 Bootstrap (io.netty.bootstrap.Bootstrap)7 ConnectException (java.net.ConnectException)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)6 AbstractChannel (io.netty.channel.AbstractChannel)6 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)6