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