X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=common%2Futil%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Futil%2Fconcurrent%2FThreadPoolExecutorTest.java;h=44f90f9884d927df4a20fb939a4ecb4e000334c1;hb=178b951cbabbc81d3bcf00749a7c97964ed20296;hp=8270e45d357ca6fffd6ecdb407d84d37115155cc;hpb=c5e58b6841e2f9d9bcd7808d4f3073b1c4792d26;p=yangtools.git diff --git a/common/util/src/test/java/org/opendaylight/yangtools/util/concurrent/ThreadPoolExecutorTest.java b/common/util/src/test/java/org/opendaylight/yangtools/util/concurrent/ThreadPoolExecutorTest.java index 8270e45d35..44f90f9884 100644 --- a/common/util/src/test/java/org/opendaylight/yangtools/util/concurrent/ThreadPoolExecutorTest.java +++ b/common/util/src/test/java/org/opendaylight/yangtools/util/concurrent/ThreadPoolExecutorTest.java @@ -10,6 +10,7 @@ package org.opendaylight.yangtools.util.concurrent; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; +import com.google.common.base.Stopwatch; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -19,12 +20,9 @@ import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; - import org.junit.After; import org.junit.Test; -import com.google.common.base.Stopwatch; - /** * Tests various ThreadPoolExecutor implementations. * @@ -36,7 +34,7 @@ public class ThreadPoolExecutorTest { @After public void tearDown() { - if( executor != null ) { + if (executor != null) { executor.shutdownNow(); } } @@ -49,12 +47,12 @@ public class ThreadPoolExecutorTest { 100000, "TestPool", 0 ); } - @Test(expected=RejectedExecutionException.class) + @Test(expected = RejectedExecutionException.class) public void testFastThreadPoolRejectingTask() throws Exception { executor = SpecialExecutors.newBoundedFastThreadPool( 1, 1, "TestPool" ); - for( int i = 0; i < 5; i++ ) { + for (int i = 0; i < 5; i++) { executor.execute( new Task( null, null, null, null, TimeUnit.MICROSECONDS.convert( 5, TimeUnit.SECONDS ) ) ); } @@ -77,12 +75,12 @@ public class ThreadPoolExecutorTest { 100000, "TestPool", 0 ); } - @Test(expected=RejectedExecutionException.class) + @Test(expected = RejectedExecutionException.class) public void testCachedThreadRejectingTask() throws Exception { ExecutorService executor = SpecialExecutors.newBoundedCachedThreadPool( 1, 1, "TestPool" ); - for( int i = 0; i < 5; i++ ) { + for (int i = 0; i < 5; i++) { executor.execute( new Task( null, null, null, null, TimeUnit.MICROSECONDS.convert( 5, TimeUnit.SECONDS ) ) ); } @@ -101,21 +99,19 @@ public class ThreadPoolExecutorTest { this.executor = executor; - System.out.println( "\nTesting " + executor.getClass().getSimpleName() + " with " + - numTasksToRun + " tasks." ); + System.out.println("\nTesting " + executor.getClass().getSimpleName() + " with " + numTasksToRun + " tasks."); final CountDownLatch tasksRunLatch = new CountDownLatch( numTasksToRun ); final ConcurrentMap taskCountPerThread = new ConcurrentHashMap<>(); final AtomicReference threadError = new AtomicReference<>(); - Stopwatch stopWatch = new Stopwatch(); - stopWatch.start(); + Stopwatch stopWatch = Stopwatch.createStarted(); new Thread() { @Override public void run() { - for( int i = 0; i < numTasksToRun; i++ ) { -// if(i%100 == 0) { + for (int i = 0; i < numTasksToRun; i++) { +// if (i%100 == 0) { // Uninterruptibles.sleepUninterruptibly( 20, TimeUnit.MICROSECONDS ); // } @@ -129,17 +125,16 @@ public class ThreadPoolExecutorTest { stopWatch.stop(); - if( !done ) { - fail( (numTasksToRun - tasksRunLatch.getCount()) + " tasks out of " + - numTasksToRun + " executed" ); + if (!done) { + fail((numTasksToRun - tasksRunLatch.getCount()) + " tasks out of " + numTasksToRun + " executed"); } - if( threadError.get() != null ) { + if (threadError.get() != null) { throw threadError.get(); } System.out.println( taskCountPerThread.size() + " threads used:" ); - for( Map.Entry e : taskCountPerThread.entrySet() ) { + for (Map.Entry e : taskCountPerThread.entrySet()) { System.out.println( " " + e.getKey().getName() + " - " + e.getValue() + " tasks" ); } @@ -148,8 +143,9 @@ public class ThreadPoolExecutorTest { System.out.println(); } - private static class Task implements Runnable { + static class Task implements Runnable { final CountDownLatch tasksRunLatch; + final CountDownLatch blockLatch; final ConcurrentMap taskCountPerThread; final AtomicReference threadError; final String expThreadPrefix; @@ -162,28 +158,41 @@ public class ThreadPoolExecutorTest { this.threadError = threadError; this.expThreadPrefix = expThreadPrefix; this.delay = delay; + blockLatch = null; + } + + Task( CountDownLatch tasksRunLatch, CountDownLatch blockLatch ) { + this.tasksRunLatch = tasksRunLatch; + this.blockLatch = blockLatch; + this.taskCountPerThread = null; + this.threadError = null; + this.expThreadPrefix = null; + this.delay = 0; } @Override public void run() { try { - if( delay > 0 ) { - try { + try { + if (delay > 0) { TimeUnit.MICROSECONDS.sleep( delay ); - } catch( InterruptedException e ) {} + } else if (blockLatch != null) { + blockLatch.await(); + } + } catch (InterruptedException e) { } - if( expThreadPrefix != null ) { + if (expThreadPrefix != null) { assertEquals( "Thread name starts with " + expThreadPrefix, true, Thread.currentThread().getName().startsWith( expThreadPrefix ) ); } - if( taskCountPerThread != null ) { + if (taskCountPerThread != null) { AtomicLong count = taskCountPerThread.get( Thread.currentThread() ); - if( count == null ) { + if (count == null) { count = new AtomicLong( 0 ); AtomicLong prev = taskCountPerThread.putIfAbsent( Thread.currentThread(), count ); - if( prev != null ) { + if (prev != null) { count = prev; } } @@ -191,12 +200,12 @@ public class ThreadPoolExecutorTest { count.incrementAndGet(); } - } catch( AssertionError e ) { - if( threadError != null ) { + } catch (AssertionError e) { + if (threadError != null) { threadError.set( e ); } } finally { - if( tasksRunLatch != null ) { + if (tasksRunLatch != null) { tasksRunLatch.countDown(); } }