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.
This commit is contained in:
Norman Maurer 2019-01-19 17:17:03 +01:00
parent af8e17f8a2
commit 668368a17e

View File

@ -379,17 +379,23 @@ public class ChannelOutboundBufferTest {
} }
}; };
final CountDownLatch handlerAddedLatch = new CountDownLatch(1); final CountDownLatch handlerAddedLatch = new CountDownLatch(1);
final CountDownLatch handlerRemovedLatch = new CountDownLatch(1);
EmbeddedChannel ch = new EmbeddedChannel(); EmbeddedChannel ch = new EmbeddedChannel();
ch.pipeline().addLast(executor, new ChannelOutboundHandlerAdapter() { ch.pipeline().addLast(executor, "handler", new ChannelOutboundHandlerAdapter() {
@Override @Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
promise.setFailure(new AssertionError("Should not be called")); promise.setFailure(new AssertionError("Should not be called"));
} }
@Override @Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception { public void handlerAdded(ChannelHandlerContext ctx) {
handlerAddedLatch.countDown(); handlerAddedLatch.countDown();
} }
@Override
public void handlerRemoved(ChannelHandlerContext ctx) {
handlerRemovedLatch.countDown();
}
}); });
// Lets wait until we are sure the handler was added. // Lets wait until we are sure the handler was added.
@ -432,7 +438,19 @@ public class ChannelOutboundBufferTest {
assertEquals(0, ch.unsafe().outboundBuffer().totalPendingWriteBytes()); assertEquals(0, ch.unsafe().outboundBuffer().totalPendingWriteBytes());
executeLatch.countDown(); 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); safeClose(ch);
executor.shutdownGracefully(); executor.shutdownGracefully();
} }