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 committed by GitHub
parent dae5d9d3f9
commit bce0784e5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 2 deletions

View File

@ -383,17 +383,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.
@ -436,7 +442,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();
}