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) · Raghuvardhan-Reddy78/netty@e8aaef5

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
+214Lines changed: 214 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.ChannelFutureListener;
@@ -44,10 +46,13 @@
4446
import org.junit.jupiter.api.Timeout;
4547

4648
import java.util.concurrent.CountDownLatch;
49+
import java.util.concurrent.ThreadLocalRandom;
50+
import java.util.concurrent.TimeUnit;
4751
import java.util.concurrent.atomic.AtomicInteger;
4852
import java.util.concurrent.atomic.AtomicReference;
4953

5054
import static java.util.concurrent.TimeUnit.MILLISECONDS;
55+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
5156
import static org.junit.jupiter.api.Assertions.assertEquals;
5257
import static org.junit.jupiter.api.Assertions.assertNull;
5358
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -790,6 +795,215 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
790795
}
791796
}
792797

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+
7931007
/**
7941008
* Designed to read a single byte at a time to control the number of reads done at a fine granularity.
7951009
*/
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
@@ -56,15 +56,12 @@
5656
import java.util.concurrent.CountDownLatch;
5757
import java.util.concurrent.LinkedBlockingQueue;
5858
import java.util.concurrent.ThreadLocalRandom;
59-
import java.util.concurrent.TimeUnit;
6059

6160
import static org.junit.jupiter.api.Assertions.assertEquals;
6261
import static org.junit.jupiter.api.Assertions.assertFalse;
6362
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
64-
import static org.junit.jupiter.api.Assertions.assertNotSame;
6563
import static org.junit.jupiter.api.Assertions.assertTrue;
6664

67-
6865
public class NioSocketChannelTest extends AbstractNioChannelTest<NioSocketChannel> {
6966

7067
/**

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.