|
19 | 19 | import io.netty.bootstrap.ServerBootstrap; |
20 | 20 | import io.netty.buffer.ByteBuf; |
21 | 21 | import io.netty.buffer.ByteBufAllocator; |
| 22 | +import io.netty.buffer.ByteBufUtil; |
| 23 | +import io.netty.buffer.Unpooled; |
22 | 24 | import io.netty.channel.Channel; |
23 | 25 | import io.netty.channel.ChannelConfig; |
24 | 26 | import io.netty.channel.ChannelFuture; |
|
49 | 51 | import java.util.concurrent.atomic.AtomicReference; |
50 | 52 |
|
51 | 53 | import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 54 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
52 | 55 | import static org.junit.jupiter.api.Assertions.assertEquals; |
53 | 56 | import static org.junit.jupiter.api.Assertions.assertNull; |
54 | 57 | import static org.junit.jupiter.api.Assertions.assertTrue; |
@@ -802,6 +805,218 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
802 | 805 | } |
803 | 806 | } |
804 | 807 |
|
| 808 | + @Test |
| 809 | + public void testShutdownInputAllowsOutboundWrites(TestInfo testInfo) throws Throwable { |
| 810 | + run(testInfo, new Runner<ServerBootstrap, Bootstrap>() { |
| 811 | + @Override |
| 812 | + public void run(ServerBootstrap serverBootstrap, Bootstrap bootstrap) throws Throwable { |
| 813 | + testShutdownInputAllowsOutboundWrites(true, serverBootstrap, bootstrap); |
| 814 | + testShutdownInputAllowsOutboundWrites(false, serverBootstrap, bootstrap); |
| 815 | + } |
| 816 | + }); |
| 817 | + } |
| 818 | + |
| 819 | + private static void testShutdownInputAllowsOutboundWrites(final boolean clientInitiates, final ServerBootstrap sb, |
| 820 | + final Bootstrap cb) |
| 821 | + throws InterruptedException { |
| 822 | + final byte[] expectedBytes = new byte[100]; |
| 823 | + PlatformDependent.threadLocalRandom().nextBytes(expectedBytes); |
| 824 | + final CountDownLatch readDoneLatch = new CountDownLatch(1); |
| 825 | + |
| 826 | + Channel serverChannel = null; |
| 827 | + Channel clientChannel = null; |
| 828 | + try { |
| 829 | + final ShutdownInputWriter shutdownInputWriter = new ShutdownInputWriter(expectedBytes); |
| 830 | + final ShutdownInputReader shutdownInputReader = |
| 831 | + new ShutdownInputReader(expectedBytes.length, readDoneLatch); |
| 832 | + sb.childOption(ChannelOption.ALLOW_HALF_CLOSURE, true); |
| 833 | + cb.option(ChannelOption.ALLOW_HALF_CLOSURE, true); |
| 834 | + sb.childHandler(new ChannelInitializer<Channel>() { |
| 835 | + @Override |
| 836 | + protected void initChannel(Channel ch) throws Exception { |
| 837 | + ch.pipeline().addLast(clientInitiates? shutdownInputReader : shutdownInputWriter); |
| 838 | + } |
| 839 | + }); |
| 840 | + cb.handler(new ChannelInitializer<Channel>() { |
| 841 | + @Override |
| 842 | + protected void initChannel(Channel ch) throws Exception { |
| 843 | + ch.pipeline().addLast(clientInitiates? shutdownInputWriter : shutdownInputReader); |
| 844 | + } |
| 845 | + }); |
| 846 | + serverChannel = sb.bind().sync().channel(); |
| 847 | + clientChannel = cb.connect(serverChannel.localAddress()).sync().channel(); |
| 848 | + assertTrue(readDoneLatch.await(5, TimeUnit.SECONDS), |
| 849 | + "Timed out waiting for inbound data after input shutdown"); |
| 850 | + assertArrayEquals(expectedBytes, shutdownInputReader.receivedBytes()); |
| 851 | + } finally { |
| 852 | + if (serverChannel != null) { |
| 853 | + serverChannel.close().sync(); |
| 854 | + } |
| 855 | + if (clientChannel != null) { |
| 856 | + clientChannel.close().sync(); |
| 857 | + } |
| 858 | + } |
| 859 | + } |
| 860 | + |
| 861 | + private static final class ShutdownInputReader extends SimpleChannelInboundHandler<ByteBuf> { |
| 862 | + private final CountDownLatch readDoneLatch; |
| 863 | + private final int expectedBytesCount; |
| 864 | + private final ByteBuf receivedMsgs; |
| 865 | + private byte[] actualBytes; |
| 866 | + |
| 867 | + ShutdownInputReader(final int expectedBytesCount, |
| 868 | + final CountDownLatch readDoneLatch) { |
| 869 | + this.readDoneLatch = readDoneLatch; |
| 870 | + this.expectedBytesCount = expectedBytesCount; |
| 871 | + this.receivedMsgs = Unpooled.buffer(expectedBytesCount); |
| 872 | + } |
| 873 | + |
| 874 | + @Override |
| 875 | + protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception { |
| 876 | + receivedMsgs.writeBytes(msg); |
| 877 | + if (receivedMsgs.readableBytes() == expectedBytesCount) { |
| 878 | + actualBytes = ByteBufUtil.getBytes(receivedMsgs); |
| 879 | + readDoneLatch.countDown(); |
| 880 | + } |
| 881 | + } |
| 882 | + |
| 883 | + @Override |
| 884 | + public void handlerRemoved(ChannelHandlerContext ctx) { |
| 885 | + receivedMsgs.release(); |
| 886 | + } |
| 887 | + |
| 888 | + byte[] receivedBytes() { |
| 889 | + return actualBytes; |
| 890 | + } |
| 891 | + } |
| 892 | + |
| 893 | + private static final class ShutdownInputWriter extends ChannelInboundHandlerAdapter { |
| 894 | + private final byte[] expectedBytes; |
| 895 | + |
| 896 | + ShutdownInputWriter(final byte[] expectedBytes) { |
| 897 | + this.expectedBytes = expectedBytes; |
| 898 | + } |
| 899 | + |
| 900 | + @Override |
| 901 | + public void channelActive(final ChannelHandlerContext ctx) throws Exception { |
| 902 | + ((DuplexChannel) ctx.channel()).shutdownInput() |
| 903 | + .addListener(new ChannelFutureListener() { |
| 904 | + @Override |
| 905 | + public void operationComplete(ChannelFuture future) { |
| 906 | + ctx.writeAndFlush(Unpooled.wrappedBuffer(expectedBytes)); |
| 907 | + } |
| 908 | + }); |
| 909 | + } |
| 910 | + } |
| 911 | + |
| 912 | + @Test |
| 913 | + public void testShutdownOutputAllowsInboundReads(TestInfo testInfo) throws Throwable { |
| 914 | + run(testInfo, new Runner<ServerBootstrap, Bootstrap>() { |
| 915 | + @Override |
| 916 | + public void run(ServerBootstrap serverBootstrap, Bootstrap bootstrap) throws Throwable { |
| 917 | + testShutdownOutputAllowsInboundReads(true, serverBootstrap, bootstrap); |
| 918 | + testShutdownOutputAllowsInboundReads(false, serverBootstrap, bootstrap); |
| 919 | + } |
| 920 | + }); |
| 921 | + } |
| 922 | + |
| 923 | + private static void testShutdownOutputAllowsInboundReads(final boolean clientInitiates, final ServerBootstrap sb, |
| 924 | + final Bootstrap cb) throws InterruptedException { |
| 925 | + final byte[] expectedBytes = new byte[100]; |
| 926 | + PlatformDependent.threadLocalRandom().nextBytes(expectedBytes); |
| 927 | + final CountDownLatch readDoneLatch = new CountDownLatch(1); |
| 928 | + |
| 929 | + Channel serverChannel = null; |
| 930 | + Channel clientChannel = null; |
| 931 | + try { |
| 932 | + final ShutdownOutputWriter shutdownOutputWriter = new ShutdownOutputWriter(expectedBytes); |
| 933 | + final ShutdownOutputReader |
| 934 | + shutdownOutputReader = |
| 935 | + new ShutdownOutputReader(expectedBytes.length, readDoneLatch); |
| 936 | + sb.childOption(ChannelOption.ALLOW_HALF_CLOSURE, true); |
| 937 | + cb.option(ChannelOption.ALLOW_HALF_CLOSURE, true); |
| 938 | + sb.childHandler(new ChannelInitializer<Channel>() { |
| 939 | + @Override |
| 940 | + protected void initChannel(Channel ch) throws Exception { |
| 941 | + ch.pipeline().addLast(clientInitiates? shutdownOutputWriter : shutdownOutputReader); |
| 942 | + } |
| 943 | + }); |
| 944 | + cb.handler(new ChannelInitializer<Channel>() { |
| 945 | + |
| 946 | + @Override |
| 947 | + protected void initChannel(Channel ch) throws Exception { |
| 948 | + ch.pipeline().addLast(clientInitiates? shutdownOutputReader : shutdownOutputWriter); |
| 949 | + } |
| 950 | + }); |
| 951 | + |
| 952 | + serverChannel = sb.bind().sync().channel(); |
| 953 | + clientChannel = cb.connect(serverChannel.localAddress()).sync().channel(); |
| 954 | + assertTrue(readDoneLatch.await(5, TimeUnit.SECONDS), |
| 955 | + "Timed out waiting for inbound data after output shutdown"); |
| 956 | + assertArrayEquals(expectedBytes, shutdownOutputReader.receivedBytes()); |
| 957 | + } finally { |
| 958 | + if (serverChannel != null) { |
| 959 | + serverChannel.close().sync(); |
| 960 | + } |
| 961 | + if (clientChannel != null) { |
| 962 | + clientChannel.close().sync(); |
| 963 | + } |
| 964 | + } |
| 965 | + } |
| 966 | + |
| 967 | + private static final class ShutdownOutputWriter extends ChannelInboundHandlerAdapter { |
| 968 | + private final byte[] expectedBytes; |
| 969 | + |
| 970 | + ShutdownOutputWriter(final byte[] expectedBytes) { |
| 971 | + this.expectedBytes = expectedBytes; |
| 972 | + } |
| 973 | + |
| 974 | + @Override |
| 975 | + public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { |
| 976 | + if (evt == ChannelInputShutdownEvent.INSTANCE) { |
| 977 | + ctx.writeAndFlush(Unpooled.wrappedBuffer(expectedBytes)); |
| 978 | + } |
| 979 | + ctx.fireUserEventTriggered(evt); |
| 980 | + } |
| 981 | + } |
| 982 | + |
| 983 | + private static final class ShutdownOutputReader extends SimpleChannelInboundHandler<ByteBuf> { |
| 984 | + private final CountDownLatch readDoneLatch; |
| 985 | + private final int expectedBytesCount; |
| 986 | + private final ByteBuf receivedMsgs; |
| 987 | + private byte[] actualbytes; |
| 988 | + |
| 989 | + ShutdownOutputReader(final int expectedBytesCount, |
| 990 | + final CountDownLatch readDoneLatch) { |
| 991 | + this.readDoneLatch = readDoneLatch; |
| 992 | + this.expectedBytesCount = expectedBytesCount; |
| 993 | + this.receivedMsgs = Unpooled.buffer(expectedBytesCount); |
| 994 | + } |
| 995 | + |
| 996 | + @Override |
| 997 | + public void channelActive(ChannelHandlerContext ctx) { |
| 998 | + ((DuplexChannel) ctx.channel()).shutdownOutput(); |
| 999 | + } |
| 1000 | + |
| 1001 | + @Override |
| 1002 | + protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception { |
| 1003 | + receivedMsgs.writeBytes(msg); |
| 1004 | + if (receivedMsgs.readableBytes() == expectedBytesCount) { |
| 1005 | + this.actualbytes = ByteBufUtil.getBytes(receivedMsgs); |
| 1006 | + readDoneLatch.countDown(); |
| 1007 | + } |
| 1008 | + } |
| 1009 | + |
| 1010 | + @Override |
| 1011 | + public void handlerRemoved(ChannelHandlerContext ctx) { |
| 1012 | + receivedMsgs.release(); |
| 1013 | + } |
| 1014 | + |
| 1015 | + byte[] receivedBytes() { |
| 1016 | + return actualbytes; |
| 1017 | + } |
| 1018 | + } |
| 1019 | + |
805 | 1020 | /** |
806 | 1021 | * Designed to read a single byte at a time to control the number of reads done at a fine granularity. |
807 | 1022 | */ |
|