Search in sources :

Example 1 with OioSocketChannel

use of io.netty.channel.socket.oio.OioSocketChannel in project netty by netty.

the class SocketShutdownOutputBySelfTest method testWriteAfterShutdownOutputNoWritabilityChange.

public void testWriteAfterShutdownOutputNoWritabilityChange(Bootstrap cb) throws Throwable {
    final TestHandler h = new TestHandler();
    ServerSocket ss = new ServerSocket();
    Socket s = null;
    SocketChannel ch = null;
    try {
        ss.bind(newSocketAddress());
        cb.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(2, 4));
        ch = (SocketChannel) cb.handler(h).connect(ss.getLocalSocketAddress()).sync().channel();
        assumeFalse(ch instanceof OioSocketChannel);
        assertTrue(ch.isActive());
        assertFalse(ch.isOutputShutdown());
        s = ss.accept();
        byte[] expectedBytes = new byte[] { 1, 2, 3, 4, 5, 6 };
        ChannelFuture writeFuture = ch.write(Unpooled.wrappedBuffer(expectedBytes));
        h.assertWritability(false);
        ch.flush();
        writeFuture.sync();
        h.assertWritability(true);
        for (int i = 0; i < expectedBytes.length; ++i) {
            assertEquals(expectedBytes[i], s.getInputStream().read());
        }
        assertTrue(h.ch.isOpen());
        assertTrue(h.ch.isActive());
        assertFalse(h.ch.isInputShutdown());
        assertFalse(h.ch.isOutputShutdown());
        // Make the connection half-closed and ensure read() returns -1.
        ch.shutdownOutput().sync();
        assertEquals(-1, s.getInputStream().read());
        assertTrue(h.ch.isOpen());
        assertTrue(h.ch.isActive());
        assertFalse(h.ch.isInputShutdown());
        assertTrue(h.ch.isOutputShutdown());
        try {
            // If half-closed, the local endpoint shouldn't be able to write
            ch.writeAndFlush(Unpooled.wrappedBuffer(new byte[] { 2 })).sync();
            fail();
        } catch (Throwable cause) {
            checkThrowable(cause);
        }
        assertNull(h.writabilityQueue.poll());
    } finally {
        if (s != null) {
            s.close();
        }
        if (ch != null) {
            ch.close();
        }
        ss.close();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) OioSocketChannel(io.netty.channel.socket.oio.OioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) OioSocketChannel(io.netty.channel.socket.oio.OioSocketChannel) ServerSocket(java.net.ServerSocket) WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Aggregations

ChannelFuture (io.netty.channel.ChannelFuture)1 WriteBufferWaterMark (io.netty.channel.WriteBufferWaterMark)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 OioSocketChannel (io.netty.channel.socket.oio.OioSocketChannel)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1