jihoonson closed pull request #5975: [Backport] Increase timeout for BlockingPoolTest URL: https://github.com/apache/incubator-druid/pull/5975
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/common/src/test/java/io/druid/collections/BlockingPoolTest.java b/common/src/test/java/io/druid/collections/BlockingPoolTest.java index f76339ca99c..8e13cb56b27 100644 --- a/common/src/test/java/io/druid/collections/BlockingPoolTest.java +++ b/common/src/test/java/io/druid/collections/BlockingPoolTest.java @@ -21,7 +21,9 @@ import com.google.common.base.Suppliers; import com.google.common.collect.Lists; -import org.junit.AfterClass; +import io.druid.java.util.common.concurrent.Execs; +import org.junit.After; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -30,7 +32,6 @@ import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.Future; import static org.junit.Assert.assertEquals; @@ -40,26 +41,34 @@ public class BlockingPoolTest { - private static final ExecutorService SERVICE = Executors.newFixedThreadPool(2); + private ExecutorService service; - private static final DefaultBlockingPool<Integer> POOL = new DefaultBlockingPool<>(Suppliers.ofInstance(1), 10); - private static final BlockingPool<Integer> EMPTY_POOL = new DefaultBlockingPool<>(Suppliers.ofInstance(1), 0); + private DefaultBlockingPool<Integer> pool; + private BlockingPool<Integer> emptyPool; - @AfterClass - public static void teardown() + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Before + public void setup() { - SERVICE.shutdown(); + service = Execs.multiThreaded(2, "blocking-pool-test"); + pool = new DefaultBlockingPool<>(Suppliers.ofInstance(1), 10); + emptyPool = new DefaultBlockingPool<>(Suppliers.ofInstance(1), 0); } - @Rule - public ExpectedException expectedException = ExpectedException.none(); + @After + public void teardown() + { + service.shutdownNow(); + } @Test public void testTakeFromEmptyPool() { expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Pool was initialized with limit = 0, there are no objects to take."); - EMPTY_POOL.take(0); + emptyPool.take(0); } @Test @@ -67,54 +76,54 @@ public void testDrainFromEmptyPool() { expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Pool was initialized with limit = 0, there are no objects to take."); - EMPTY_POOL.takeBatch(1, 0); + emptyPool.takeBatch(1, 0); } - @Test(timeout = 1000) + @Test(timeout = 5000) public void testTake() { - final ReferenceCountingResourceHolder<Integer> holder = POOL.take(100); + final ReferenceCountingResourceHolder<Integer> holder = pool.take(100); assertNotNull(holder); - assertEquals(9, POOL.getPoolSize()); + assertEquals(9, pool.getPoolSize()); holder.close(); - assertEquals(10, POOL.getPoolSize()); + assertEquals(10, pool.getPoolSize()); } - @Test(timeout = 1000) + @Test(timeout = 5000) public void testTakeTimeout() { - final ReferenceCountingResourceHolder<List<Integer>> batchHolder = POOL.takeBatch(10, 100L); - final ReferenceCountingResourceHolder<Integer> holder = POOL.take(100); + final ReferenceCountingResourceHolder<List<Integer>> batchHolder = pool.takeBatch(10, 100L); + final ReferenceCountingResourceHolder<Integer> holder = pool.take(100); assertNull(holder); batchHolder.close(); } - @Test(timeout = 1000) + @Test(timeout = 5000) public void testTakeBatch() { - final ReferenceCountingResourceHolder<List<Integer>> holder = POOL.takeBatch(6, 100L); + final ReferenceCountingResourceHolder<List<Integer>> holder = pool.takeBatch(6, 100L); assertNotNull(holder); assertEquals(6, holder.get().size()); - assertEquals(4, POOL.getPoolSize()); + assertEquals(4, pool.getPoolSize()); holder.close(); - assertEquals(10, POOL.getPoolSize()); + assertEquals(10, pool.getPoolSize()); } - @Test(timeout = 1000) + @Test(timeout = 5000) public void testWaitAndTakeBatch() throws InterruptedException, ExecutionException { - ReferenceCountingResourceHolder<List<Integer>> batchHolder = POOL.takeBatch(10, 10); + ReferenceCountingResourceHolder<List<Integer>> batchHolder = pool.takeBatch(10, 10); assertNotNull(batchHolder); assertEquals(10, batchHolder.get().size()); - assertEquals(0, POOL.getPoolSize()); + assertEquals(0, pool.getPoolSize()); - final Future<ReferenceCountingResourceHolder<List<Integer>>> future = SERVICE.submit( + final Future<ReferenceCountingResourceHolder<List<Integer>>> future = service.submit( new Callable<ReferenceCountingResourceHolder<List<Integer>>>() { @Override public ReferenceCountingResourceHolder<List<Integer>> call() throws Exception { - return POOL.takeBatch(8, 100); + return pool.takeBatch(8, 100); } } ); @@ -124,26 +133,26 @@ public void testWaitAndTakeBatch() throws InterruptedException, ExecutionExcepti batchHolder = future.get(); assertNotNull(batchHolder); assertEquals(8, batchHolder.get().size()); - assertEquals(2, POOL.getPoolSize()); + assertEquals(2, pool.getPoolSize()); batchHolder.close(); - assertEquals(10, POOL.getPoolSize()); + assertEquals(10, pool.getPoolSize()); } - @Test(timeout = 1000) + @Test(timeout = 5000) public void testTakeBatchTooManyObjects() { - final ReferenceCountingResourceHolder<List<Integer>> holder = POOL.takeBatch(100, 100L); + final ReferenceCountingResourceHolder<List<Integer>> holder = pool.takeBatch(100, 100L); assertNull(holder); } - @Test(timeout = 1000) + @Test(timeout = 5000) public void testConcurrentTake() throws ExecutionException, InterruptedException { - final int limit1 = POOL.maxSize() / 2; - final int limit2 = POOL.maxSize() - limit1 + 1; + final int limit1 = pool.maxSize() / 2; + final int limit2 = pool.maxSize() - limit1 + 1; - final Future<List<ReferenceCountingResourceHolder<Integer>>> f1 = SERVICE.submit( + final Future<List<ReferenceCountingResourceHolder<Integer>>> f1 = service.submit( new Callable<List<ReferenceCountingResourceHolder<Integer>>>() { @Override @@ -151,13 +160,13 @@ public void testConcurrentTake() throws ExecutionException, InterruptedException { List<ReferenceCountingResourceHolder<Integer>> result = Lists.newArrayList(); for (int i = 0; i < limit1; i++) { - result.add(POOL.take(10)); + result.add(pool.take(10)); } return result; } } ); - final Future<List<ReferenceCountingResourceHolder<Integer>>> f2 = SERVICE.submit( + final Future<List<ReferenceCountingResourceHolder<Integer>>> f2 = service.submit( new Callable<List<ReferenceCountingResourceHolder<Integer>>>() { @Override @@ -165,7 +174,7 @@ public void testConcurrentTake() throws ExecutionException, InterruptedException { List<ReferenceCountingResourceHolder<Integer>> result = Lists.newArrayList(); for (int i = 0; i < limit2; i++) { - result.add(POOL.take(10)); + result.add(pool.take(10)); } return result; } @@ -175,7 +184,7 @@ public void testConcurrentTake() throws ExecutionException, InterruptedException final List<ReferenceCountingResourceHolder<Integer>> r1 = f1.get(); final List<ReferenceCountingResourceHolder<Integer>> r2 = f2.get(); - assertEquals(0, POOL.getPoolSize()); + assertEquals(0, pool.getPoolSize()); assertTrue(r1.contains(null) || r2.contains(null)); int nonNullCount = 0; @@ -190,9 +199,9 @@ public void testConcurrentTake() throws ExecutionException, InterruptedException nonNullCount++; } } - assertEquals(POOL.maxSize(), nonNullCount); + assertEquals(pool.maxSize(), nonNullCount); - final Future future1 = SERVICE.submit(new Runnable() + final Future future1 = service.submit(new Runnable() { @Override public void run() @@ -204,7 +213,7 @@ public void run() } } }); - final Future future2 = SERVICE.submit(new Runnable() + final Future future2 = service.submit(new Runnable() { @Override public void run() @@ -220,82 +229,82 @@ public void run() future1.get(); future2.get(); - assertEquals(POOL.maxSize(), POOL.getPoolSize()); + assertEquals(pool.maxSize(), pool.getPoolSize()); } - @Test(timeout = 1000) + @Test(timeout = 5000) public void testConcurrentTakeBatch() throws ExecutionException, InterruptedException { - final int batch1 = POOL.maxSize() / 2; + final int batch1 = pool.maxSize() / 2; final Callable<ReferenceCountingResourceHolder<List<Integer>>> c1 = new Callable<ReferenceCountingResourceHolder<List<Integer>>>() { @Override public ReferenceCountingResourceHolder<List<Integer>> call() throws Exception { - return POOL.takeBatch(batch1, 10); + return pool.takeBatch(batch1, 10); } }; - final int batch2 = POOL.maxSize() - batch1 + 1; + final int batch2 = pool.maxSize() - batch1 + 1; final Callable<ReferenceCountingResourceHolder<List<Integer>>> c2 = new Callable<ReferenceCountingResourceHolder<List<Integer>>>() { @Override public ReferenceCountingResourceHolder<List<Integer>> call() throws Exception { - return POOL.takeBatch(batch2, 10); + return pool.takeBatch(batch2, 10); } }; - final Future<ReferenceCountingResourceHolder<List<Integer>>> f1 = SERVICE.submit(c1); - final Future<ReferenceCountingResourceHolder<List<Integer>>> f2 = SERVICE.submit(c2); + final Future<ReferenceCountingResourceHolder<List<Integer>>> f1 = service.submit(c1); + final Future<ReferenceCountingResourceHolder<List<Integer>>> f2 = service.submit(c2); final ReferenceCountingResourceHolder<List<Integer>> r1 = f1.get(); final ReferenceCountingResourceHolder<List<Integer>> r2 = f2.get(); if (r1 != null) { assertNull(r2); - assertEquals(POOL.maxSize() - batch1, POOL.getPoolSize()); + assertEquals(pool.maxSize() - batch1, pool.getPoolSize()); assertEquals(batch1, r1.get().size()); r1.close(); } else { assertNotNull(r2); - assertEquals(POOL.maxSize() - batch2, POOL.getPoolSize()); + assertEquals(pool.maxSize() - batch2, pool.getPoolSize()); assertEquals(batch2, r2.get().size()); r2.close(); } - assertEquals(POOL.maxSize(), POOL.getPoolSize()); + assertEquals(pool.maxSize(), pool.getPoolSize()); } - @Test(timeout = 1000) + @Test(timeout = 5000) public void testConcurrentBatchClose() throws ExecutionException, InterruptedException { - final int batch1 = POOL.maxSize() / 2; + final int batch1 = pool.maxSize() / 2; final Callable<ReferenceCountingResourceHolder<List<Integer>>> c1 = new Callable<ReferenceCountingResourceHolder<List<Integer>>>() { @Override public ReferenceCountingResourceHolder<List<Integer>> call() throws Exception { - return POOL.takeBatch(batch1, 10); + return pool.takeBatch(batch1, 10); } }; - final int batch2 = POOL.maxSize() - batch1; + final int batch2 = pool.maxSize() - batch1; final Callable<ReferenceCountingResourceHolder<List<Integer>>> c2 = new Callable<ReferenceCountingResourceHolder<List<Integer>>>() { @Override public ReferenceCountingResourceHolder<List<Integer>> call() throws Exception { - return POOL.takeBatch(batch2, 10); + return pool.takeBatch(batch2, 10); } }; - final Future<ReferenceCountingResourceHolder<List<Integer>>> f1 = SERVICE.submit(c1); - final Future<ReferenceCountingResourceHolder<List<Integer>>> f2 = SERVICE.submit(c2); + final Future<ReferenceCountingResourceHolder<List<Integer>>> f1 = service.submit(c1); + final Future<ReferenceCountingResourceHolder<List<Integer>>> f2 = service.submit(c2); final ReferenceCountingResourceHolder<List<Integer>> r1 = f1.get(); final ReferenceCountingResourceHolder<List<Integer>> r2 = f2.get(); @@ -304,9 +313,9 @@ public void testConcurrentBatchClose() throws ExecutionException, InterruptedExc assertNotNull(r2); assertEquals(batch1, r1.get().size()); assertEquals(batch2, r2.get().size()); - assertEquals(0, POOL.getPoolSize()); + assertEquals(0, pool.getPoolSize()); - final Future future1 = SERVICE.submit(new Runnable() + final Future future1 = service.submit(new Runnable() { @Override public void run() @@ -314,7 +323,7 @@ public void run() r1.close(); } }); - final Future future2 = SERVICE.submit(new Runnable() + final Future future2 = service.submit(new Runnable() { @Override public void run() @@ -326,13 +335,13 @@ public void run() future1.get(); future2.get(); - assertEquals(POOL.maxSize(), POOL.getPoolSize()); + assertEquals(pool.maxSize(), pool.getPoolSize()); } - @Test(timeout = 1000) + @Test(timeout = 5000) public void testConcurrentTakeBatchClose() throws ExecutionException, InterruptedException { - final ReferenceCountingResourceHolder<List<Integer>> r1 = POOL.takeBatch(1, 10); + final ReferenceCountingResourceHolder<List<Integer>> r1 = pool.takeBatch(1, 10); final Callable<ReferenceCountingResourceHolder<List<Integer>>> c2 = new Callable<ReferenceCountingResourceHolder<List<Integer>>>() @@ -340,12 +349,12 @@ public void testConcurrentTakeBatchClose() throws ExecutionException, Interrupte @Override public ReferenceCountingResourceHolder<List<Integer>> call() throws Exception { - return POOL.takeBatch(10, 100); + return pool.takeBatch(10, 100); } }; - final Future<ReferenceCountingResourceHolder<List<Integer>>> f2 = SERVICE.submit(c2); - final Future f1 = SERVICE.submit(new Runnable() + final Future<ReferenceCountingResourceHolder<List<Integer>>> f2 = service.submit(c2); + final Future f1 = service.submit(new Runnable() { @Override public void run() @@ -364,9 +373,9 @@ public void run() f1.get(); assertNotNull(r2); assertEquals(10, r2.get().size()); - assertEquals(0, POOL.getPoolSize()); + assertEquals(0, pool.getPoolSize()); r2.close(); - assertEquals(POOL.maxSize(), POOL.getPoolSize()); + assertEquals(pool.maxSize(), pool.getPoolSize()); } } ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@druid.apache.org For additional commands, e-mail: dev-h...@druid.apache.org