From 668368a17e943223e6c0b909447a77907f9943e7 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Sat, 19 Jan 2019 17:17:03 +0100 Subject: [PATCH] Fix racy ChannelOutboundBuffer.testWriteTaskRejected test. (#8735) Motivation: testWriteTaskRejected was racy as we did not ensure we dispatched all events to the executor before shutting it down. Modifications: Add a latch to ensure we dispatched everything. Result: Fix racy test that failed sometimes before. --- .../channel/ChannelOutboundBufferTest.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/transport/src/test/java/io/netty/channel/ChannelOutboundBufferTest.java b/transport/src/test/java/io/netty/channel/ChannelOutboundBufferTest.java index 0dfe3d7e45..90ffc9bca3 100644 --- a/transport/src/test/java/io/netty/channel/ChannelOutboundBufferTest.java +++ b/transport/src/test/java/io/netty/channel/ChannelOutboundBufferTest.java @@ -379,17 +379,23 @@ public class ChannelOutboundBufferTest { } }; final CountDownLatch handlerAddedLatch = new CountDownLatch(1); + final CountDownLatch handlerRemovedLatch = new CountDownLatch(1); EmbeddedChannel ch = new EmbeddedChannel(); - ch.pipeline().addLast(executor, new ChannelOutboundHandlerAdapter() { + ch.pipeline().addLast(executor, "handler", new ChannelOutboundHandlerAdapter() { @Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { promise.setFailure(new AssertionError("Should not be called")); } @Override - public void handlerAdded(ChannelHandlerContext ctx) throws Exception { + public void handlerAdded(ChannelHandlerContext ctx) { handlerAddedLatch.countDown(); } + + @Override + public void handlerRemoved(ChannelHandlerContext ctx) { + handlerRemovedLatch.countDown(); + } }); // Lets wait until we are sure the handler was added. @@ -432,7 +438,19 @@ public class ChannelOutboundBufferTest { assertEquals(0, ch.unsafe().outboundBuffer().totalPendingWriteBytes()); executeLatch.countDown(); + while (executor.pendingTasks() != 0) { + // Wait until there is no more pending task left. + Thread.sleep(10); + } + + ch.pipeline().remove("handler"); + + // Ensure we do not try to shutdown the executor before we handled everything for the Channel. Otherwise + // the Executor may reject when the Channel tries to add a task to it. + handlerRemovedLatch.await(); + safeClose(ch); + executor.shutdownGracefully(); }