From 30bb3094c16f8c3bf0f570f34f62a4a098cdb9b6 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Wed, 11 May 2016 11:53:31 +0200 Subject: [PATCH] [#5227] Fix race-condition in PooledByteBufAllocatorTest Motivation: PooledByteBufAllocatorTest.testNumThreadCachesWithNoDirrectArenas() had a race as it just used LockSupport.parkNanos(). We should better use a CountdownLatch and so be sure we really have init everything. Modifications: Replace LockSupport.parkNanos(...) with CountdownLatch usage Result: No more race in test. --- .../buffer/PooledByteBufAllocatorTest.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/buffer/src/test/java/io/netty/buffer/PooledByteBufAllocatorTest.java b/buffer/src/test/java/io/netty/buffer/PooledByteBufAllocatorTest.java index 9bdd2431c0..b3ab27e948 100644 --- a/buffer/src/test/java/io/netty/buffer/PooledByteBufAllocatorTest.java +++ b/buffer/src/test/java/io/netty/buffer/PooledByteBufAllocatorTest.java @@ -126,7 +126,7 @@ public class PooledByteBufAllocatorTest { } @Test - public void testNumThreadCachesWithNoDirectArenas() { + public void testNumThreadCachesWithNoDirectArenas() throws InterruptedException { int numHeapArenas = 1; final PooledByteBufAllocator allocator = new PooledByteBufAllocator(numHeapArenas, 0, 8192, 1); @@ -188,17 +188,20 @@ public class PooledByteBufAllocatorTest { LockSupport.parkNanos(MILLISECONDS.toNanos(100)); } - private static CountDownLatch createNewThreadCache(final PooledByteBufAllocator allocator) { + private static CountDownLatch createNewThreadCache(final PooledByteBufAllocator allocator) + throws InterruptedException { final CountDownLatch latch = new CountDownLatch(1); - + final CountDownLatch cacheLatch = new CountDownLatch(1); Thread t = new FastThreadLocalThread(new Runnable() { @Override public void run() { ByteBuf buf = allocator.newHeapBuffer(1024, 1024); - for (int i = 0; i < buf.capacity(); i++) { - buf.writeByte(0); - } + + // Countdown the latch after we allocated a buffer. At this point the cache must exists. + cacheLatch.countDown(); + + buf.writeZero(buf.capacity()); try { latch.await(); @@ -213,8 +216,8 @@ public class PooledByteBufAllocatorTest { }); t.start(); - // Wait a bit for the thread & thread cache to be created. - LockSupport.parkNanos(MILLISECONDS.toNanos(100)); + // Wait until we allocated a buffer and so be sure the thread was started and the cache exists. + cacheLatch.await(); return latch; }