directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From seelm...@apache.org
Subject svn commit: r1510064 - in /directory/buildtools/junit-addons/trunk/src: main/java/org/apache/directory/junit/tools/ test/ test/java/ test/java/org/ test/java/org/apache/ test/java/org/apache/directory/ test/java/org/apache/directory/junit/ test/java/or...
Date Sat, 03 Aug 2013 20:21:12 GMT
Author: seelmann
Date: Sat Aug  3 20:21:11 2013
New Revision: 1510064

URL: http://svn.apache.org/r1510064
Log:
Use Callable/Future.get() instead of Runnable/Thread.sleep()

Added:
    directory/buildtools/junit-addons/trunk/src/test/
    directory/buildtools/junit-addons/trunk/src/test/java/
    directory/buildtools/junit-addons/trunk/src/test/java/org/
    directory/buildtools/junit-addons/trunk/src/test/java/org/apache/
    directory/buildtools/junit-addons/trunk/src/test/java/org/apache/directory/
    directory/buildtools/junit-addons/trunk/src/test/java/org/apache/directory/junit/
    directory/buildtools/junit-addons/trunk/src/test/java/org/apache/directory/junit/tools/
    directory/buildtools/junit-addons/trunk/src/test/java/org/apache/directory/junit/tools/MultiThreadedMultiInvokerNotThreadsafeTest.java
    directory/buildtools/junit-addons/trunk/src/test/java/org/apache/directory/junit/tools/MultiThreadedMultiInvokerThreadsafeTest.java
Modified:
    directory/buildtools/junit-addons/trunk/src/main/java/org/apache/directory/junit/tools/MultiThreadedMultiInvoker.java

Modified: directory/buildtools/junit-addons/trunk/src/main/java/org/apache/directory/junit/tools/MultiThreadedMultiInvoker.java
URL: http://svn.apache.org/viewvc/directory/buildtools/junit-addons/trunk/src/main/java/org/apache/directory/junit/tools/MultiThreadedMultiInvoker.java?rev=1510064&r1=1510063&r2=1510064&view=diff
==============================================================================
--- directory/buildtools/junit-addons/trunk/src/main/java/org/apache/directory/junit/tools/MultiThreadedMultiInvoker.java
(original)
+++ directory/buildtools/junit-addons/trunk/src/main/java/org/apache/directory/junit/tools/MultiThreadedMultiInvoker.java
Sat Aug  3 20:21:11 2013
@@ -23,10 +23,10 @@ package org.apache.directory.junit.tools
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.Future;
 
 import org.junit.rules.MethodRule;
 import org.junit.runners.model.FrameworkMethod;
@@ -123,9 +123,6 @@ public class MultiThreadedMultiInvoker i
      */
     public Statement apply( final Statement base, final FrameworkMethod method, final Object
target )
     {
-        final List<Throwable> throwables = Collections.synchronizedList( new ArrayList<Throwable>()
);
-        final List<Runnable> runnables = new ArrayList<Runnable>();
-
         return new Statement()
         {
             @Override
@@ -138,12 +135,14 @@ public class MultiThreadedMultiInvoker i
                 }
 
                 final long start = System.currentTimeMillis();
-                final AtomicInteger counter = new AtomicInteger();
+                final List<Throwable> throwables = Collections.synchronizedList( new
ArrayList<Throwable>() );
+
+                final List<Future<Void>> futures = new ArrayList<Future<Void>>();
                 for ( int threadNum = 0; threadNum < count; threadNum++ )
                 {
-                    Runnable r = new Runnable()
+                    Callable<Void> c = new Callable<Void>()
                     {
-                        public void run()
+                        public Void call() throws Exception
                         {
                             try
                             {
@@ -168,27 +167,18 @@ public class MultiThreadedMultiInvoker i
                                 }
                                 throwables.add( t );
                             }
-                            finally
-                            {
-                                counter.decrementAndGet();
-                            }
+                            return null;
                         }
                     };
 
-                    runnables.add( r );
-                    counter.incrementAndGet();
+                    Future<Void> future = pool.submit( c );
+                    futures.add( future );
                 }
 
-                for ( Runnable r : runnables )
+                for ( Future<Void> future : futures )
                 {
-                    pool.execute( r );
+                    future.get();
                 }
-                
-                while(counter.get() > 0)
-                {
-                    Thread.sleep( 1000 );
-                }
-
                 if ( !throwables.isEmpty() )
                 {
                     throw throwables.get( 0 );

Added: directory/buildtools/junit-addons/trunk/src/test/java/org/apache/directory/junit/tools/MultiThreadedMultiInvokerNotThreadsafeTest.java
URL: http://svn.apache.org/viewvc/directory/buildtools/junit-addons/trunk/src/test/java/org/apache/directory/junit/tools/MultiThreadedMultiInvokerNotThreadsafeTest.java?rev=1510064&view=auto
==============================================================================
--- directory/buildtools/junit-addons/trunk/src/test/java/org/apache/directory/junit/tools/MultiThreadedMultiInvokerNotThreadsafeTest.java
(added)
+++ directory/buildtools/junit-addons/trunk/src/test/java/org/apache/directory/junit/tools/MultiThreadedMultiInvokerNotThreadsafeTest.java
Sat Aug  3 20:21:11 2013
@@ -0,0 +1,128 @@
+package org.apache.directory.junit.tools;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+
+
+public class MultiThreadedMultiInvokerNotThreadsafeTest
+{
+    private static final int NUM_THREADS = 1;
+    private static final int NUM_INVOCATIONS = 1000000;
+    private static final int NUM_METHODS = 10;
+
+    @Rule
+    public MultiThreadedMultiInvoker i = new MultiThreadedMultiInvoker( NUM_THREADS, NUM_INVOCATIONS
);
+
+    private static long counter;
+    private static long startTime;
+
+
+    @BeforeClass
+    public static void initCounter()
+    {
+        counter = 0l;
+    }
+
+
+    @BeforeClass
+    public static void initStartTime()
+    {
+        startTime = System.currentTimeMillis();
+    }
+
+
+    @AfterClass
+    public static void checkCounter()
+    {
+        assertEquals( 1L * NUM_THREADS * NUM_INVOCATIONS * NUM_METHODS, counter );
+    }
+
+
+    @AfterClass
+    public static void checkRuntime()
+    {
+        long endTime = System.currentTimeMillis();
+        long runTime = endTime - startTime;
+        if ( runTime > 10000 )
+        {
+            fail( "Test shut run in less than 10 seconds." );
+        }
+    }
+
+
+    @Test
+    public void test0()
+    {
+        counter++;
+    }
+
+
+    @Test
+    public void test1()
+    {
+        counter++;
+    }
+
+
+    @Test
+    public void test2()
+    {
+        counter++;
+    }
+
+
+    @Test
+    public void test3()
+    {
+        counter++;
+    }
+
+
+    @Test
+    public void test4()
+    {
+        counter++;
+    }
+
+
+    @Test
+    public void test5()
+    {
+        counter++;
+    }
+
+
+    @Test
+    public void test6()
+    {
+        counter++;
+    }
+
+
+    @Test
+    public void test7()
+    {
+        counter++;
+    }
+
+
+    @Test
+    public void test8()
+    {
+        counter++;
+    }
+
+
+    @Test
+    public void test9()
+    {
+        counter++;
+    }
+
+}

Added: directory/buildtools/junit-addons/trunk/src/test/java/org/apache/directory/junit/tools/MultiThreadedMultiInvokerThreadsafeTest.java
URL: http://svn.apache.org/viewvc/directory/buildtools/junit-addons/trunk/src/test/java/org/apache/directory/junit/tools/MultiThreadedMultiInvokerThreadsafeTest.java?rev=1510064&view=auto
==============================================================================
--- directory/buildtools/junit-addons/trunk/src/test/java/org/apache/directory/junit/tools/MultiThreadedMultiInvokerThreadsafeTest.java
(added)
+++ directory/buildtools/junit-addons/trunk/src/test/java/org/apache/directory/junit/tools/MultiThreadedMultiInvokerThreadsafeTest.java
Sat Aug  3 20:21:11 2013
@@ -0,0 +1,130 @@
+package org.apache.directory.junit.tools;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+
+
+public class MultiThreadedMultiInvokerThreadsafeTest
+{
+    private static final int NUM_THREADS = 100;
+    private static final int NUM_INVOCATIONS = 10000;
+    private static final int NUM_METHODS = 10;
+
+    @Rule
+    public MultiThreadedMultiInvoker i = new MultiThreadedMultiInvoker( NUM_THREADS, NUM_INVOCATIONS
);
+
+    private static AtomicLong counter;
+    private static long startTime;
+
+
+    @BeforeClass
+    public static void initCounter()
+    {
+        counter = new AtomicLong( 0L );
+    }
+
+
+    @BeforeClass
+    public static void initStartTime()
+    {
+        startTime = System.currentTimeMillis();
+    }
+
+
+    @AfterClass
+    public static void checkCounter()
+    {
+        assertEquals( 1L * NUM_THREADS * NUM_INVOCATIONS * NUM_METHODS, counter.get() );
+    }
+
+
+    @AfterClass
+    public static void checkRuntime()
+    {
+        long endTime = System.currentTimeMillis();
+        long runTime = endTime - startTime;
+        if ( runTime > 10000 )
+        {
+            fail( "Test shut run in less than 10 seconds." );
+        }
+    }
+
+
+    @Test
+    public void test0()
+    {
+        counter.incrementAndGet();
+    }
+
+
+    @Test
+    public void test1()
+    {
+        counter.incrementAndGet();
+    }
+
+
+    @Test
+    public void test2()
+    {
+        counter.incrementAndGet();
+    }
+
+
+    @Test
+    public void test3()
+    {
+        counter.incrementAndGet();
+    }
+
+
+    @Test
+    public void test4()
+    {
+        counter.incrementAndGet();
+    }
+
+
+    @Test
+    public void test5()
+    {
+        counter.incrementAndGet();
+    }
+
+
+    @Test
+    public void test6()
+    {
+        counter.incrementAndGet();
+    }
+
+
+    @Test
+    public void test7()
+    {
+        counter.incrementAndGet();
+    }
+
+
+    @Test
+    public void test8()
+    {
+        counter.incrementAndGet();
+    }
+
+
+    @Test
+    public void test9()
+    {
+        counter.incrementAndGet();
+    }
+
+}



Mime
View raw message