Repository: netty/netty
File: testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketHalfClosedTest.java (Java)

Code before:
                            } else {
                                ctx.read();
                            }
                        }

                        @Override
                        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                            ctx.close();
                            ctx.fireExceptionCaught(cause);
                        }
                    });
                    ch.read();
                }
            });

            serverChannel = sb.bind().sync().channel();
            clientChannel = cb.connect(serverChannel.localAddress()).sync().channel();
            clientChannel.read();

            clientReadAllDataLatch.await();

            // Now we need to trigger server half-close
            ((DuplexChannel) serverChildChannel.get()).shutdownOutput();

            clientHalfClosedLatch.await();
            clientHalfClosedAllBytesRead.await(); // failing here.
        } finally {
            if (clientChannel != null) {
                clientChannel.close().sync();
            }
            if (serverChannel != null) {
                serverChannel.close().sync();
            }
        }
    }

    @Test
    public void testHalfClosureReceiveDataOnFinalWait2StateWhenSoLingerSet(TestInfo testInfo) throws Throwable {
        run(testInfo, new Runner<ServerBootstrap, Bootstrap>() {
            @Override
            public void run(ServerBootstrap serverBootstrap, Bootstrap bootstrap) throws Throwable {
                testHalfClosureReceiveDataOnFinalWait2StateWhenSoLingerSet(serverBootstrap, bootstrap);
            }
        });
    }

    private void testHalfClosureReceiveDataOnFinalWait2StateWhenSoLingerSet(ServerBootstrap sb, Bootstrap cb)
            throws Throwable {
        Channel serverChannel = null;
        Channel clientChannel = null;


Review comment (suggestion):
```suggestion
            clientHalfClosedAllBytesRead.await();
```

Code after:
                            if (bytesRead == totalServerBytesWritten) {
                                // Bounce this through the event loop to make sure it happens after we're done
                                // with the read operation.
                                ch.eventLoop().execute(new Runnable() {
                                    @Override
                                    public void run() {
                                        clientReadAllDataLatch.countDown();
                                    }
                                });
                            } else {
                                ctx.read();
                            }
                        }

                        @Override
                        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                            ctx.fireExceptionCaught(cause);
                            ctx.close();
                        }
                    });
                    ch.read();
                }
            });

            serverChannel = sb.bind().sync().channel();
            clientChannel = cb.connect(serverChannel.localAddress()).sync().channel();
            clientChannel.read();

            clientReadAllDataLatch.await();

            // Now we need to trigger server half-close
            ((DuplexChannel) serverChildChannel.get()).shutdownOutput();

            clientHalfClosedLatch.await();
            clientHalfClosedAllBytesRead.await();
        } finally {
            if (clientChannel != null) {
                clientChannel.close().sync();
            }
            if (serverChannel != null) {
                serverChannel.close().sync();
            }
        }
    }

    @Test
    public void testHalfClosureReceiveDataOnFinalWait2StateWhenSoLingerSet(TestInfo testInfo) throws Throwable {
        run(testInfo, new Runner<ServerBootstrap, Bootstrap>() {
            @Override
            public void run(ServerBootstrap serverBootstrap, Bootstrap bootstrap) throws Throwable {
                testHalfClosureReceiveDataOnFinalWait2StateWhenSoLingerSet(serverBootstrap, bootstrap);