Fix wrong use of assertTrue in unit test.

Motivation:

My previous commit b88a980482 introduced a flawed unit test,
that executes an assertion in a different thread than the test thread.
If this assertion fails, the test doesn't fail.

Modifications:

Replace the assertion by a proper workaround.

Result:

More correct unit test
This commit is contained in:
buchgr 2016-03-15 15:40:09 +01:00 committed by Norman Maurer
parent acbca192bd
commit 83c349ffa9

View File

@ -26,6 +26,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.LockSupport; import java.util.concurrent.locks.LockSupport;
import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.MILLISECONDS;
@ -41,6 +42,8 @@ public class PooledByteBufAllocatorTest {
final PooledByteBufAllocator allocator = final PooledByteBufAllocator allocator =
new PooledByteBufAllocator(numArenas, numArenas, 8192, 1); new PooledByteBufAllocator(numArenas, numArenas, 8192, 1);
final AtomicBoolean threadCachesCreated = new AtomicBoolean(true);
for (int i = 0; i < numArenas; i++) { for (int i = 0; i < numArenas; i++) {
new FastThreadLocalThread(new Runnable() { new FastThreadLocalThread(new Runnable() {
@Override @Override
@ -50,7 +53,12 @@ public class PooledByteBufAllocatorTest {
buf.writeByte(0); buf.writeByte(0);
} }
assertTrue(allocator.numThreadLocalCaches() > 0); // Make sure that thread caches are actually created,
// so that down below we are not testing for zero
// thread caches without any of them ever having been initialized.
if (allocator.numThreadLocalCaches() == 0) {
threadCachesCreated.set(false);
}
buf.release(); buf.release();
} }
@ -61,6 +69,8 @@ public class PooledByteBufAllocatorTest {
while (allocator.numThreadLocalCaches() > 0) { while (allocator.numThreadLocalCaches() > 0) {
LockSupport.parkNanos(MILLISECONDS.toNanos(100)); LockSupport.parkNanos(MILLISECONDS.toNanos(100));
} }
assertTrue(threadCachesCreated.get());
} }
@Test @Test