GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Socket: Add half-closure tests to SocketHalfClosedTest (#17394) (#17434) · netty/netty@b1f65ed

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • testsuite/src/main/java/io/netty/testsuite/transport/socket
    • SocketHalfClosedTest.java
  • transport/src/test/java/io/netty/channel/socket/nio
    • NioSocketChannelTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketHalfClosedTest.java‎

Copy file name to clipboardExpand all lines: testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketHalfClosedTest.java
+215Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import io.netty.bootstrap.ServerBootstrap;
2020
import io.netty.buffer.ByteBuf;
2121
import io.netty.buffer.ByteBufAllocator;
22+
import io.netty.buffer.ByteBufUtil;
23+
import io.netty.buffer.Unpooled;
2224
import io.netty.channel.Channel;
2325
import io.netty.channel.ChannelConfig;
2426
import io.netty.channel.ChannelFuture;
@@ -49,6 +51,7 @@
4951
import java.util.concurrent.atomic.AtomicReference;
5052

5153
import static java.util.concurrent.TimeUnit.MILLISECONDS;
54+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
5255
import static org.junit.jupiter.api.Assertions.assertEquals;
5356
import static org.junit.jupiter.api.Assertions.assertNull;
5457
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -802,6 +805,218 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
802805
}
803806
}
804807

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+
8051020
/**
8061021
* Designed to read a single byte at a time to control the number of reads done at a fine granularity.
8071022
*/
Collapse file

‎transport/src/test/java/io/netty/channel/socket/nio/NioSocketChannelTest.java‎

Copy file name to clipboardExpand all lines: transport/src/test/java/io/netty/channel/socket/nio/NioSocketChannelTest.java
-3Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,12 @@
5555
import java.util.Queue;
5656
import java.util.concurrent.CountDownLatch;
5757
import java.util.concurrent.LinkedBlockingQueue;
58-
import java.util.concurrent.TimeUnit;
5958

6059
import static org.junit.jupiter.api.Assertions.assertEquals;
6160
import static org.junit.jupiter.api.Assertions.assertFalse;
6261
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
63-
import static org.junit.jupiter.api.Assertions.assertNotSame;
6462
import static org.junit.jupiter.api.Assertions.assertTrue;
6563

66-
6764
public class NioSocketChannelTest extends AbstractNioChannelTest<NioSocketChannel> {
6865

6966
/**

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.