You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by se...@apache.org on 2010/10/22 15:23:05 UTC

svn commit: r1026316 - in /directory/buildtools/trunk/junit-addons/src/main/java/org/apache/directory/junit/tools: MultiThreadedMultiInvoker.java NoMultiThreadedInvocation.java

Author: seelmann
Date: Fri Oct 22 13:23:05 2010
New Revision: 1026316

URL: http://svn.apache.org/viewvc?rev=1026316&view=rev
Log:
o Added annotation to prevent multi-threaded invocation
o Better thread names
o Configurable trace information
o Configuration via system property

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

Modified: directory/buildtools/trunk/junit-addons/src/main/java/org/apache/directory/junit/tools/MultiThreadedMultiInvoker.java
URL: http://svn.apache.org/viewvc/directory/buildtools/trunk/junit-addons/src/main/java/org/apache/directory/junit/tools/MultiThreadedMultiInvoker.java?rev=1026316&r1=1026315&r2=1026316&view=diff
==============================================================================
--- directory/buildtools/trunk/junit-addons/src/main/java/org/apache/directory/junit/tools/MultiThreadedMultiInvoker.java (original)
+++ directory/buildtools/trunk/junit-addons/src/main/java/org/apache/directory/junit/tools/MultiThreadedMultiInvoker.java Fri Oct 22 13:23:05 2010
@@ -23,6 +23,7 @@ package org.apache.directory.junit.tools
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.junit.rules.MethodRule;
 import org.junit.runners.model.FrameworkMethod;
@@ -43,19 +44,39 @@ import org.junit.runners.model.Statement
  */
 public class MultiThreadedMultiInvoker implements MethodRule
 {
-
-    private int numThreads = 10;
-    private int numInvocationsPerThread = 100;
+    private static AtomicInteger threadCounter = new AtomicInteger();
+    private int numThreads;
+    private int numInvocationsPerThread;
+    private boolean trace;
 
 
     /**
      * Instantiates a new multi threaded invoker.
      * 
-     * Using this default constructor each test method is invoked 100 times
-     * by 10 thread.
+     * The number of threads and invocations per thread are derived from 
+     * system properties 'threads' and 'invocations'.
      */
     public MultiThreadedMultiInvoker()
     {
+        this.numThreads = getSystemIntProperty( "mtmi.threads", 1 );
+        this.numInvocationsPerThread = getSystemIntProperty( "mtmi.invocations", 1 );
+        this.trace = getSystemBoolProperty( "mtmi.trace", false );
+    }
+
+
+    private int getSystemIntProperty( String key, int def )
+    {
+        String property = System.getProperty( key, "" + def );
+        int value = Integer.parseInt( property );
+        return value;
+    }
+
+
+    private boolean getSystemBoolProperty( String key, boolean def )
+    {
+        String property = System.getProperty( key, "" + def );
+        boolean value = Boolean.parseBoolean( property );
+        return value;
     }
 
 
@@ -70,6 +91,23 @@ public class MultiThreadedMultiInvoker i
         super();
         this.numThreads = numThreads;
         this.numInvocationsPerThread = numInvocationsPerThread;
+        this.trace = false;
+    }
+
+
+    /**
+     * Instantiates a new multi threaded multi invoker.
+     *
+     * @param numThreads the number of threads
+     * @param numInvocationsPerThread the number of method invocations per thread
+     * @param trace the trace flag
+     */
+    public MultiThreadedMultiInvoker( int numThreads, int numInvocationsPerThread, boolean trace )
+    {
+        super();
+        this.numThreads = numThreads;
+        this.numInvocationsPerThread = numInvocationsPerThread;
+        this.trace = trace;
     }
 
 
@@ -86,8 +124,14 @@ public class MultiThreadedMultiInvoker i
             @Override
             public void evaluate() throws Throwable
             {
+                int count = numThreads;
+                if ( method.getAnnotation( NoMultiThreadedInvocation.class ) != null )
+                {
+                    count = 1;
+                }
 
-                for ( int threadNum = 0; threadNum < numThreads; threadNum++ )
+                final long start = System.currentTimeMillis();
+                for ( int threadNum = 0; threadNum < count; threadNum++ )
                 {
                     Runnable r = new Runnable()
                     {
@@ -97,17 +141,31 @@ public class MultiThreadedMultiInvoker i
                             {
                                 for ( int invocationNum = 0; invocationNum < numInvocationsPerThread; invocationNum++ )
                                 {
+                                    if ( trace )
+                                    {
+                                        long t = System.currentTimeMillis() - start;
+                                        System.out.println( t + " - " + method.getName() + " - "
+                                            + Thread.currentThread().getName() + " - Invocation "
+                                            + ( invocationNum + 1 ) + "/" + numInvocationsPerThread );
+                                    }
+
                                     base.evaluate();
                                 }
                             }
                             catch ( Throwable t )
                             {
+                                if ( trace )
+                                {
+                                    t.printStackTrace();
+                                }
                                 throwables.add( t );
                             }
                         }
                     };
 
-                    Thread t = new Thread( r );
+                    String name = MultiThreadedMultiInvoker.class.getSimpleName() + "-Thread-"
+                        + threadCounter.incrementAndGet();
+                    Thread t = new Thread( r, name );
                     threads.add( t );
                 }
 

Added: directory/buildtools/trunk/junit-addons/src/main/java/org/apache/directory/junit/tools/NoMultiThreadedInvocation.java
URL: http://svn.apache.org/viewvc/directory/buildtools/trunk/junit-addons/src/main/java/org/apache/directory/junit/tools/NoMultiThreadedInvocation.java?rev=1026316&view=auto
==============================================================================
--- directory/buildtools/trunk/junit-addons/src/main/java/org/apache/directory/junit/tools/NoMultiThreadedInvocation.java (added)
+++ directory/buildtools/trunk/junit-addons/src/main/java/org/apache/directory/junit/tools/NoMultiThreadedInvocation.java Fri Oct 22 13:23:05 2010
@@ -0,0 +1,43 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.junit.tools;
+
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+
+/**
+ * Indicates that a test shouldn't be invoked by multiple threads.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(
+    { ElementType.METHOD })
+public @interface NoMultiThreadedInvocation
+{
+    /**
+     * The optional reason why the test shouldn't be invoked by multiple threads.
+     */
+    String value() default "";
+}