maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tibordig...@apache.org
Subject [02/19] maven-surefire git commit: [SUREFIRE] refactoring + improve: synchronization, use only daemon threads, using interrupted threads
Date Thu, 23 Jul 2015 21:29:06 GMT
[SUREFIRE] refactoring + improve: synchronization,use only daemon threads,using interrupted
threads


Project: http://git-wip-us.apache.org/repos/asf/maven-surefire/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven-surefire/commit/e53ab122
Tree: http://git-wip-us.apache.org/repos/asf/maven-surefire/tree/e53ab122
Diff: http://git-wip-us.apache.org/repos/asf/maven-surefire/diff/e53ab122

Branch: refs/heads/master
Commit: e53ab1229add11b89532ba054312c870ef77ab3c
Parents: 7d9c438
Author: Tibor17 <tibor17@lycos.com>
Authored: Sat Jul 11 16:39:44 2015 +0200
Committer: Tibor17 <tibor17@lycos.com>
Committed: Thu Jul 23 23:28:01 2015 +0200

----------------------------------------------------------------------
 .../surefire/booterclient/ForkStarter.java      |  20 +++-
 .../TestProvidingInputStream.java               | 102 +++++++++++------
 .../booterclient/output/ForkClient.java         |  23 ++--
 .../output/ThreadedStreamConsumer.java          |  10 +-
 .../booterclient/ForkingRunListenerTest.java    |   4 +-
 .../surefire/booter/ForkingRunListener.java     |   8 +-
 .../util/internal/DaemonThreadFactory.java      | 112 +++++++++++++++++++
 .../maven/surefire/booter/ForkedBooter.java     |   8 +-
 .../surefire/report/RunnableTestClass1.java     |   5 +-
 .../report/SmartStackTraceParserTest.java       |   6 +-
 .../common/junit4/JUnit4RunListenerTest.java    |   3 +-
 .../junitcore/ConfigurableParallelComputer.java |  15 ++-
 .../surefire/junitcore/pc/ParallelComputer.java |   6 +-
 .../junitcore/pc/ParallelComputerBuilder.java   |  12 +-
 .../junitcore/pc/SchedulingStrategies.java      |   8 +-
 .../junitcore/pc/SingleThreadScheduler.java     |  11 +-
 .../junitcore/pc/SchedulingStrategiesTest.java  |   6 +-
 17 files changed, 263 insertions(+), 96 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e53ab122/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkStarter.java
----------------------------------------------------------------------
diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkStarter.java
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkStarter.java
index 7022bf8..0514e47 100644
--- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkStarter.java
+++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkStarter.java
@@ -48,6 +48,7 @@ import org.apache.maven.surefire.report.StackTraceWriter;
 import org.apache.maven.surefire.suite.RunResult;
 import org.apache.maven.surefire.testset.TestRequest;
 import org.apache.maven.surefire.util.DefaultScanResult;
+import org.apache.maven.surefire.util.internal.DaemonThreadFactory;
 import org.apache.maven.surefire.util.internal.StringUtils;
 
 import java.io.File;
@@ -66,6 +67,7 @@ import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Future;
+import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.ThreadPoolExecutor;
@@ -89,6 +91,8 @@ import static org.apache.maven.surefire.booter.Classpath.join;
  */
 public class ForkStarter
 {
+    private final ThreadFactory threadFactory = DaemonThreadFactory.newDaemonThreadFactory();
+
     /**
      * Closes an InputStream
      */
@@ -206,9 +210,9 @@ public class ForkStarter
     {
 
         ArrayList<Future<RunResult>> results = new ArrayList<Future<RunResult>>(
forkCount );
-        ExecutorService executorService = new ThreadPoolExecutor( forkCount, forkCount, 60,
TimeUnit.SECONDS,
+        ThreadPoolExecutor executorService = new ThreadPoolExecutor( forkCount, forkCount,
60, TimeUnit.SECONDS,
                                                                   new ArrayBlockingQueue<Runnable>(
forkCount ) );
-
+        executorService.setThreadFactory( threadFactory );
         try
         {
             // Ask to the executorService to run all tasks
@@ -267,6 +271,8 @@ public class ForkStarter
                 }
                 catch ( InterruptedException e )
                 {
+                    executorService.shutdownNow();
+                    Thread.currentThread().interrupt();
                     throw new SurefireBooterForkException( "Interrupted", e );
                 }
                 catch ( ExecutionException e )
@@ -288,11 +294,10 @@ public class ForkStarter
     private RunResult runSuitesForkPerTestSet( final SurefireProperties effectiveSystemProperties,
final int forkCount )
         throws SurefireBooterForkException
     {
-
         ArrayList<Future<RunResult>> results = new ArrayList<Future<RunResult>>(
500 );
-        ExecutorService executorService =
+        ThreadPoolExecutor executorService =
             new ThreadPoolExecutor( forkCount, forkCount, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()
);
-
+        executorService.setThreadFactory( threadFactory );
         try
         {
             // Ask to the executorService to run all tasks
@@ -335,6 +340,8 @@ public class ForkStarter
                 }
                 catch ( InterruptedException e )
                 {
+                    executorService.shutdownNow();
+                    Thread.currentThread().interrupt();
                     throw new SurefireBooterForkException( "Interrupted", e );
                 }
                 catch ( ExecutionException e )
@@ -364,6 +371,7 @@ public class ForkStarter
         }
         catch ( InterruptedException e )
         {
+            Thread.currentThread().interrupt();
             throw new SurefireBooterForkException( "Interrupted", e );
         }
     }
@@ -439,7 +447,7 @@ public class ForkStarter
         {
             testProvidingInputStream.setFlushReceiverProvider( cli );
             inputStreamCloser = new InputStreamCloser( testProvidingInputStream );
-            inputStreamCloserHook = new Thread( inputStreamCloser );
+            inputStreamCloserHook = DaemonThreadFactory.newDaemonThread( inputStreamCloser,
"input-stream-closer" );
             ShutdownHookUtils.addShutDownHook( inputStreamCloserHook );
         }
         else

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e53ab122/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/lazytestprovider/TestProvidingInputStream.java
----------------------------------------------------------------------
diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/lazytestprovider/TestProvidingInputStream.java
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/lazytestprovider/TestProvidingInputStream.java
index e7a5032..9f40bf7 100644
--- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/lazytestprovider/TestProvidingInputStream.java
+++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/lazytestprovider/TestProvidingInputStream.java
@@ -19,10 +19,12 @@ package org.apache.maven.plugin.surefire.booterclient.lazytestprovider;
  * under the License.
  */
 
+import java.io.EOFException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Queue;
-import java.util.concurrent.Semaphore;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.ReentrantLock;
 
 import static org.apache.maven.surefire.util.internal.StringUtils.encodeStringForForkCommunication;
 
@@ -40,17 +42,19 @@ import static org.apache.maven.surefire.util.internal.StringUtils.encodeStringFo
 public class TestProvidingInputStream
     extends InputStream
 {
+    private final ReentrantLock lock = new ReentrantLock();
+
+    private final Condition lockCondition = lock.newCondition();
+
     private final Queue<String> testItemQueue;
 
     private byte[] currentBuffer;
 
     private int currentPos;
 
-    private final Semaphore semaphore = new Semaphore( 0 );
-
-    private FlushReceiverProvider flushReceiverProvider;
+    private volatile FlushReceiverProvider flushReceiverProvider;
 
-    private volatile boolean closed = false;
+    private volatile boolean closed;
 
     /**
      * C'tor
@@ -72,43 +76,58 @@ public class TestProvidingInputStream
 
     @SuppressWarnings( "checkstyle:magicnumber" )
     @Override
-    public synchronized int read()
+    public int read()
         throws IOException
     {
-        if ( null == currentBuffer )
+        lock.lock();
+        try
         {
-            if ( null != flushReceiverProvider && null != flushReceiverProvider.getFlushReceiver()
)
-            {
-                flushReceiverProvider.getFlushReceiver().flush();
-            }
-
-            semaphore.acquireUninterruptibly();
-
             if ( closed )
             {
-                return -1;
-            }
-
-            String currentElement = testItemQueue.poll();
-            if ( currentElement != null )
-            {
-                currentBuffer = encodeStringForForkCommunication( currentElement );
-                currentPos = 0;
+                throw new EOFException( "closed unexpectedly" );
             }
             else
             {
-                return -1;
+                if ( null == currentBuffer )
+                {
+                    if ( null != flushReceiverProvider && null != flushReceiverProvider.getFlushReceiver()
)
+                    {
+                        flushReceiverProvider.getFlushReceiver().flush();
+                    }
+
+                    lockCondition.awaitUninterruptibly();
+
+                    if ( closed )
+                    {
+                        throw new EOFException( "closed unexpectedly" );
+                    }
+
+                    String currentElement = testItemQueue.poll();
+                    if ( currentElement != null )
+                    {
+                        currentBuffer = encodeStringForForkCommunication( currentElement
);
+                        currentPos = 0;
+                    }
+                    else
+                    {
+                        return -1;
+                    }
+                }
+
+                if ( currentPos < currentBuffer.length )
+                {
+                    return currentBuffer[currentPos++] & 0xff;
+                }
+                else
+                {
+                    currentBuffer = null;
+                    return '\n' & 0xff;
+                }
             }
         }
-
-        if ( currentPos < currentBuffer.length )
+        finally
         {
-            return currentBuffer[currentPos++] & 0xff;
-        }
-        else
-        {
-            currentBuffer = null;
-            return '\n' & 0xff;
+            lock.unlock();
         }
     }
 
@@ -117,13 +136,30 @@ public class TestProvidingInputStream
      */
     public void provideNewTest()
     {
-        semaphore.release();
+        lock.lock();
+        try
+        {
+            lockCondition.signalAll();
+        }
+        finally
+        {
+            lock.unlock();
+        }
     }
 
     @Override
     public void close()
     {
         closed = true;
-        semaphore.release();
+        lock.lock();
+        try
+        {
+            currentBuffer = null;
+            lockCondition.signalAll();
+        }
+        finally
+        {
+            lock.unlock();
+        }
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e53ab122/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/ForkClient.java
----------------------------------------------------------------------
diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/ForkClient.java
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/ForkClient.java
index cea1824..54ee5e4 100644
--- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/ForkClient.java
+++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/ForkClient.java
@@ -23,12 +23,11 @@ import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.StringReader;
 import java.nio.ByteBuffer;
-import java.util.Collections;
-import java.util.HashMap;
 import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.Properties;
 import java.util.StringTokenizer;
+import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.maven.plugin.surefire.booterclient.lazytestprovider.TestProvidingInputStream;
 import org.apache.maven.plugin.surefire.report.DefaultReporterFactory;
@@ -51,19 +50,17 @@ import org.apache.maven.surefire.util.internal.StringUtils;
 public class ForkClient
     implements StreamConsumer
 {
-
     private final DefaultReporterFactory defaultReporterFactory;
 
     private final TestProvidingInputStream testProvidingInputStream;
 
-    private final Map<Integer, RunListener> testSetReporters =
-        Collections.synchronizedMap( new HashMap<Integer, RunListener>() );
+    private final Map<Integer, RunListener> testSetReporters = new ConcurrentHashMap<Integer,
RunListener>();
 
     private final Properties testVmSystemProperties;
 
-    private volatile boolean saidGoodBye = false;
+    private volatile boolean saidGoodBye;
 
-    private volatile StackTraceWriter errorInFork = null;
+    private volatile StackTraceWriter errorInFork;
 
     public ForkClient( DefaultReporterFactory defaultReporterFactory, Properties testVmSystemProperties
)
     {
@@ -98,7 +95,7 @@ public class ForkClient
                 System.out.println( s );
                 return;
             }
-            final Integer channelNumber = Integer.parseInt( s.substring( 2, commma ), 16
);
+            final int channelNumber = Integer.parseInt( s.substring( 2, commma ), 16 );
             int rest = s.indexOf( ",", commma );
             final String remaining = s.substring( rest + 1 );
 
@@ -181,7 +178,7 @@ public class ForkClient
         }
     }
 
-    private void writeTestOutput( final Integer channelNumber, final String remaining, boolean
isStdout )
+    private void writeTestOutput( final int channelNumber, final String remaining, boolean
isStdout )
     {
         int csNameEnd = remaining.indexOf( ',' );
         String charsetName = remaining.substring( 0, csNameEnd );
@@ -277,12 +274,12 @@ public class ForkClient
      * @param channelNumber The logical channel number
      * @return A mock provider reporter
      */
-    public RunListener getReporter( Integer channelNumber )
+    public RunListener getReporter( int channelNumber )
     {
         return testSetReporters.get( channelNumber );
     }
 
-    private RunListener getOrCreateReporter( Integer channelNumber )
+    private RunListener getOrCreateReporter( int channelNumber )
     {
         RunListener reporter = testSetReporters.get( channelNumber );
         if ( reporter == null )
@@ -293,12 +290,12 @@ public class ForkClient
         return reporter;
     }
 
-    private ConsoleOutputReceiver getOrCreateConsoleOutputReceiver( Integer channelNumber
)
+    private ConsoleOutputReceiver getOrCreateConsoleOutputReceiver( int channelNumber )
     {
         return (ConsoleOutputReceiver) getOrCreateReporter( channelNumber );
     }
 
-    private ConsoleLogger getOrCreateConsoleLogger( Integer channelNumber )
+    private ConsoleLogger getOrCreateConsoleLogger( int channelNumber )
     {
         return (ConsoleLogger) getOrCreateReporter( channelNumber );
     }

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e53ab122/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/ThreadedStreamConsumer.java
----------------------------------------------------------------------
diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/ThreadedStreamConsumer.java
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/ThreadedStreamConsumer.java
index 7574eec..c7b4e66 100644
--- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/ThreadedStreamConsumer.java
+++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/ThreadedStreamConsumer.java
@@ -20,7 +20,9 @@ package org.apache.maven.plugin.surefire.booterclient.output;
  */
 
 import org.apache.maven.shared.utils.cli.StreamConsumer;
+import org.apache.maven.surefire.util.internal.DaemonThreadFactory;
 
+import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.LinkedBlockingQueue;
 
 /**
@@ -31,7 +33,7 @@ import java.util.concurrent.LinkedBlockingQueue;
 public class ThreadedStreamConsumer
     implements StreamConsumer
 {
-    private final java.util.concurrent.BlockingQueue<String> items = new LinkedBlockingQueue<String>();
+    private final BlockingQueue<String> items = new LinkedBlockingQueue<String>();
 
     private static final String POISON = "Pioson";
     private static final int ITEM_LIMIT_BEFORE_SLEEP = 10000;
@@ -43,14 +45,14 @@ public class ThreadedStreamConsumer
     static class Pumper
         implements Runnable
     {
-        private final java.util.concurrent.BlockingQueue<String> queue;
+        private final BlockingQueue<String> queue;
 
         private final StreamConsumer target;
 
         private volatile Throwable throwable;
 
 
-        Pumper( java.util.concurrent.BlockingQueue<String> queue, StreamConsumer target
)
+        Pumper( BlockingQueue<String> queue, StreamConsumer target )
         {
             this.queue = queue;
             this.target = target;
@@ -85,7 +87,7 @@ public class ThreadedStreamConsumer
     public ThreadedStreamConsumer( StreamConsumer target )
     {
         pumper = new Pumper( items, target );
-        thread = new Thread( pumper, "ThreadedStreamConsumer" );
+        thread = DaemonThreadFactory.newDaemonThread( pumper, "ThreadedStreamConsumer" );
         thread.start();
     }
 

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e53ab122/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkingRunListenerTest.java
----------------------------------------------------------------------
diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkingRunListenerTest.java
b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkingRunListenerTest.java
index fd85b44..31e511c 100644
--- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkingRunListenerTest.java
+++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkingRunListenerTest.java
@@ -53,9 +53,9 @@ public class ForkingRunListenerTest
 
     private final PrintStream printStream;
 
-    final Integer defaultChannel = 17;
+    final int defaultChannel = 17;
 
-    final Integer anotherChannel = 18;
+    final int anotherChannel = 18;
 
     public ForkingRunListenerTest()
     {

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e53ab122/surefire-api/src/main/java/org/apache/maven/surefire/booter/ForkingRunListener.java
----------------------------------------------------------------------
diff --git a/surefire-api/src/main/java/org/apache/maven/surefire/booter/ForkingRunListener.java
b/surefire-api/src/main/java/org/apache/maven/surefire/booter/ForkingRunListener.java
index 719679f..203651e 100644
--- a/surefire-api/src/main/java/org/apache/maven/surefire/booter/ForkingRunListener.java
+++ b/surefire-api/src/main/java/org/apache/maven/surefire/booter/ForkingRunListener.java
@@ -155,14 +155,8 @@ public class ForkingRunListener
             while ( propertyKeys.hasMoreElements() )
             {
                 String key = (String) propertyKeys.nextElement();
-
                 String value = systemProperties.getProperty( key );
-
-                if ( value == null )
-                {
-                    value = "null";
-                }
-                encodeAndWriteToTarget( toPropertyString( key, value ) );
+                encodeAndWriteToTarget( toPropertyString( key, value == null ? "null" : value
) );
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e53ab122/surefire-api/src/main/java/org/apache/maven/surefire/util/internal/DaemonThreadFactory.java
----------------------------------------------------------------------
diff --git a/surefire-api/src/main/java/org/apache/maven/surefire/util/internal/DaemonThreadFactory.java
b/surefire-api/src/main/java/org/apache/maven/surefire/util/internal/DaemonThreadFactory.java
new file mode 100644
index 0000000..29eb18d
--- /dev/null
+++ b/surefire-api/src/main/java/org/apache/maven/surefire/util/internal/DaemonThreadFactory.java
@@ -0,0 +1,112 @@
+package org.apache.maven.surefire.util.internal;
+
+/*
+ * 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.
+ */
+
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * Creates new daemon Thread.
+ */
+public final class DaemonThreadFactory
+    implements ThreadFactory
+{
+    private static final AtomicInteger POOL_NUMBER = new AtomicInteger( 1 );
+
+    private final AtomicInteger threadNumber = new AtomicInteger( 1 );
+
+    private final ThreadGroup group;
+
+    private final String namePrefix;
+
+    private DaemonThreadFactory()
+    {
+        SecurityManager s = System.getSecurityManager();
+        group = s != null ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
+        namePrefix = "pool-" + POOL_NUMBER.getAndIncrement() + "-thread-";
+    }
+
+    public Thread newThread( Runnable r )
+    {
+        Thread t = new Thread( group, r, namePrefix + threadNumber.getAndIncrement() );
+        if ( t.getPriority() != Thread.NORM_PRIORITY )
+        {
+            t.setPriority( Thread.NORM_PRIORITY );
+        }
+        t.setDaemon( true );
+        return t;
+    }
+
+    /**
+     * Should be used by thread pools.
+     */
+    public static ThreadFactory newDaemonThreadFactory()
+    {
+        return new DaemonThreadFactory();
+    }
+
+    public static ThreadFactory newDaemonThreadFactory( String name )
+    {
+        return new NamedThreadFactory( name );
+    }
+
+    public static Thread newDaemonThread( Runnable r )
+    {
+        SecurityManager s = System.getSecurityManager();
+        ThreadGroup group = s == null ? Thread.currentThread().getThreadGroup() : s.getThreadGroup();
+        Thread t = new Thread( group, r );
+        if ( t.getPriority() != Thread.NORM_PRIORITY )
+        {
+            t.setPriority( Thread.NORM_PRIORITY );
+        }
+        t.setDaemon( true );
+        return t;
+    }
+
+    public static Thread newDaemonThread( Runnable r, String name )
+    {
+        SecurityManager s = System.getSecurityManager();
+        ThreadGroup group = s == null ? Thread.currentThread().getThreadGroup() : s.getThreadGroup();
+        Thread t = new Thread( group, r, name );
+        if ( t.getPriority() != Thread.NORM_PRIORITY )
+        {
+            t.setPriority( Thread.NORM_PRIORITY );
+        }
+        t.setDaemon( true );
+        return t;
+    }
+
+    private static class NamedThreadFactory
+        implements ThreadFactory
+    {
+
+        private final String name;
+
+        private NamedThreadFactory( String name )
+        {
+            this.name = name;
+        }
+
+        public Thread newThread( Runnable r )
+        {
+            return newDaemonThread( r, name );
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e53ab122/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkedBooter.java
----------------------------------------------------------------------
diff --git a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkedBooter.java
b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkedBooter.java
index f59461a..ac73564 100644
--- a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkedBooter.java
+++ b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkedBooter.java
@@ -33,6 +33,7 @@ import org.apache.maven.surefire.report.StackTraceWriter;
 import org.apache.maven.surefire.suite.RunResult;
 import org.apache.maven.surefire.testset.TestSetFailedException;
 import org.apache.maven.surefire.util.ReflectionUtils;
+import org.apache.maven.surefire.util.internal.DaemonThreadFactory;
 
 import static org.apache.maven.surefire.util.internal.StringUtils.encodeStringForForkCommunication;
 
@@ -48,7 +49,6 @@ import static org.apache.maven.surefire.util.internal.StringUtils.encodeStringFo
  */
 public class ForkedBooter
 {
-
     /**
      * This method is invoked when Surefire is forked - this method parses and organizes
the arguments passed to it and
      * then calls the Surefire class' run method. <p/> The system exit code will be
1 if an exception is thrown.
@@ -174,7 +174,7 @@ public class ForkedBooter
     @SuppressWarnings( "checkstyle:emptyblock" )
     private static void launchLastDitchDaemonShutdownThread( final int returnCode )
     {
-        Thread lastExit = new Thread( new Runnable()
+        DaemonThreadFactory.newDaemonThread( new Runnable()
         {
             public void run()
             {
@@ -187,9 +187,7 @@ public class ForkedBooter
                 {
                 }
             }
-        } );
-        lastExit.setDaemon( true );
-        lastExit.start();
+        }, "last-ditch-daemon-shutdown-thread-" + SYSTEM_EXIT_TIMEOUT ).start();
     }
 
     public static RunResult invokeProviderInSameClassLoader( Object testSet, Object factory,

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e53ab122/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/RunnableTestClass1.java
----------------------------------------------------------------------
diff --git a/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/RunnableTestClass1.java
b/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/RunnableTestClass1.java
index 58ed78d..8414903 100644
--- a/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/RunnableTestClass1.java
+++ b/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/RunnableTestClass1.java
@@ -1,5 +1,7 @@
 package org.apache.maven.surefire.report;
 
+import org.apache.maven.surefire.util.internal.DaemonThreadFactory;
+
 import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.FutureTask;
@@ -24,7 +26,6 @@ import java.util.concurrent.FutureTask;
 */
 class RunnableTestClass1
     implements Callable<Object>
-
 {
     public Object call()
         throws Exception
@@ -38,7 +39,7 @@ class RunnableTestClass1
     {
         RunnableTestClass2 rt2 = new RunnableTestClass2();
         FutureTask<Object> futureTask = new FutureTask<Object>( rt2 );
-        new Thread( futureTask ).start();
+        DaemonThreadFactory.newDaemonThread( futureTask ).start();
         try
         {
             futureTask.get();

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e53ab122/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/SmartStackTraceParserTest.java
----------------------------------------------------------------------
diff --git a/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/SmartStackTraceParserTest.java
b/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/SmartStackTraceParserTest.java
index 4d39184..e600718 100644
--- a/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/SmartStackTraceParserTest.java
+++ b/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/SmartStackTraceParserTest.java
@@ -27,6 +27,7 @@ import java.util.concurrent.FutureTask;
 import junit.framework.AssertionFailedError;
 import junit.framework.ComparisonFailure;
 import junit.framework.TestCase;
+import org.apache.maven.surefire.util.internal.DaemonThreadFactory;
 
 import static org.apache.maven.surefire.report.SmartStackTraceParser.*;
 
@@ -34,7 +35,6 @@ import static org.apache.maven.surefire.report.SmartStackTraceParser.*;
 public class SmartStackTraceParserTest
     extends TestCase
 {
-
     public void testGetString()
         throws Exception
     {
@@ -306,7 +306,7 @@ public class SmartStackTraceParserTest
     public ExecutionException getSingleNested()
     {
         FutureTask<Object> futureTask = new FutureTask<Object>( new RunnableTestClass2()
);
-        new Thread( futureTask ).start();
+        DaemonThreadFactory.newDaemonThread( futureTask ).start();
         try
         {
             futureTask.get();
@@ -326,7 +326,7 @@ public class SmartStackTraceParserTest
     private ExecutionException getDoubleNestedException()
     {
         FutureTask<Object> futureTask = new FutureTask<Object>( new RunnableTestClass1()
);
-        new Thread( futureTask ).start();
+        DaemonThreadFactory.newDaemonThread( futureTask ).start();
         try
         {
             futureTask.get();

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e53ab122/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/common/junit4/JUnit4RunListenerTest.java
----------------------------------------------------------------------
diff --git a/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/common/junit4/JUnit4RunListenerTest.java
b/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/common/junit4/JUnit4RunListenerTest.java
index 1bd27cb..c4765e9 100644
--- a/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/common/junit4/JUnit4RunListenerTest.java
+++ b/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/common/junit4/JUnit4RunListenerTest.java
@@ -23,6 +23,7 @@ import java.util.concurrent.CountDownLatch;
 import org.apache.maven.surefire.junit4.MockReporter;
 
 import junit.framework.Assert;
+import org.apache.maven.surefire.util.internal.DaemonThreadFactory;
 import org.junit.Test;
 import org.junit.runner.Description;
 import org.junit.runner.Request;
@@ -59,7 +60,7 @@ public class JUnit4RunListenerTest
 
         jUnit4TestSetReporter.testStarted( testSomething );
 
-        new Thread( new Runnable()
+        DaemonThreadFactory.newDaemonThread( new Runnable()
         {
             public void run()
             {

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e53ab122/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/ConfigurableParallelComputer.java
----------------------------------------------------------------------
diff --git a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/ConfigurableParallelComputer.java
b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/ConfigurableParallelComputer.java
index d664edc..0c1898b 100644
--- a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/ConfigurableParallelComputer.java
+++ b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/ConfigurableParallelComputer.java
@@ -25,7 +25,9 @@ import java.util.List;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadFactory;
 
+import org.apache.maven.surefire.util.internal.DaemonThreadFactory;
 import org.junit.runner.Computer;
 import org.junit.runner.Runner;
 import org.junit.runners.ParentRunner;
@@ -44,6 +46,8 @@ import org.junit.runners.model.RunnerScheduler;
 public class ConfigurableParallelComputer
     extends Computer
 {
+    private static final ThreadFactory DAEMON_THREAD_FACTORY = DaemonThreadFactory.newDaemonThreadFactory();
+
     private final boolean fClasses;
 
     private final boolean fMethods;
@@ -58,18 +62,21 @@ public class ConfigurableParallelComputer
 
     public ConfigurableParallelComputer()
     {
-        this( true, true, Executors.newCachedThreadPool(), false );
+        this( true, true, Executors.newCachedThreadPool( DAEMON_THREAD_FACTORY ), false );
     }
 
     public ConfigurableParallelComputer( boolean fClasses, boolean fMethods )
     {
-        this( fClasses, fMethods, Executors.newCachedThreadPool(), false );
+        this( fClasses, fMethods, Executors.newCachedThreadPool( DAEMON_THREAD_FACTORY ),
false );
     }
 
     public ConfigurableParallelComputer( boolean fClasses, boolean fMethods, Integer numberOfThreads,
boolean perCore )
     {
-        this( fClasses, fMethods, Executors.newFixedThreadPool(
-            numberOfThreads * ( perCore ? Runtime.getRuntime().availableProcessors() : 1
) ), true );
+        this( fClasses, fMethods,
+              Executors.newFixedThreadPool(
+                  numberOfThreads * ( perCore ? Runtime.getRuntime().availableProcessors()
: 1 ),
+                  DAEMON_THREAD_FACTORY ),
+              true );
     }
 
     private ConfigurableParallelComputer( boolean fClasses, boolean fMethods, ExecutorService
executorService,

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e53ab122/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/pc/ParallelComputer.java
----------------------------------------------------------------------
diff --git a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/pc/ParallelComputer.java
b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/pc/ParallelComputer.java
index 5491a68..afec8a3 100644
--- a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/pc/ParallelComputer.java
+++ b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/pc/ParallelComputer.java
@@ -20,6 +20,7 @@ package org.apache.maven.surefire.junitcore.pc;
  */
 
 import org.apache.maven.surefire.testset.TestSetFailedException;
+import org.apache.maven.surefire.util.internal.DaemonThreadFactory;
 import org.junit.runner.Computer;
 import org.junit.runner.Description;
 
@@ -30,6 +31,7 @@ import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadFactory;
 
 import static java.util.concurrent.TimeUnit.NANOSECONDS;
 
@@ -43,6 +45,8 @@ import static java.util.concurrent.TimeUnit.NANOSECONDS;
 public abstract class ParallelComputer
     extends Computer
 {
+    private static final ThreadFactory DAEMON_THREAD_FACTORY = DaemonThreadFactory.newDaemonThreadFactory();
+
     private static final double NANOS_IN_A_SECOND = 1E9;
 
     private final ShutdownStatus shutdownStatus = new ShutdownStatus();
@@ -169,7 +173,7 @@ public abstract class ParallelComputer
     {
         if ( shutdownScheduler == null )
         {
-            shutdownScheduler = Executors.newScheduledThreadPool( 2 );
+            shutdownScheduler = Executors.newScheduledThreadPool( 2, DAEMON_THREAD_FACTORY
);
         }
         return shutdownScheduler;
     }

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e53ab122/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/pc/ParallelComputerBuilder.java
----------------------------------------------------------------------
diff --git a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/pc/ParallelComputerBuilder.java
b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/pc/ParallelComputerBuilder.java
index 2ef3fd1..fdc4473 100644
--- a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/pc/ParallelComputerBuilder.java
+++ b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/pc/ParallelComputerBuilder.java
@@ -32,10 +32,12 @@ import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadFactory;
 
 import org.apache.maven.surefire.junitcore.JUnitCoreParameters;
 import org.apache.maven.surefire.report.ConsoleLogger;
 import org.apache.maven.surefire.testset.TestSetFailedException;
+import org.apache.maven.surefire.util.internal.DaemonThreadFactory;
 import org.junit.internal.runners.ErrorReportingRunner;
 import org.junit.runner.Description;
 import org.junit.runner.Runner;
@@ -78,6 +80,8 @@ import static org.apache.maven.surefire.junitcore.pc.Type.SUITES;
  */
 public final class ParallelComputerBuilder
 {
+    private static final ThreadFactory DAEMON_THREAD_FACTORY = DaemonThreadFactory.newDaemonThreadFactory();
+
     private static final Class<? extends Annotation> JCIP_NOT_THREAD_SAFE = loadNotThreadSafeAnnotations();
 
     private static final Set<?> NULL_SINGLETON = Collections.singleton( null );
@@ -403,8 +407,8 @@ public final class ParallelComputerBuilder
         private ExecutorService createPool( int poolSize )
         {
             return poolSize < Integer.MAX_VALUE
-                ? Executors.newFixedThreadPool( poolSize )
-                : Executors.newCachedThreadPool();
+                ? Executors.newFixedThreadPool( poolSize, DAEMON_THREAD_FACTORY )
+                : Executors.newCachedThreadPool( DAEMON_THREAD_FACTORY );
         }
 
         private Scheduler createMaster( ExecutorService pool, int poolSize )
@@ -574,11 +578,11 @@ public final class ParallelComputerBuilder
                 ExecutorService pool = null;
                 if ( poolSize == Integer.MAX_VALUE )
                 {
-                    pool = Executors.newCachedThreadPool();
+                    pool = Executors.newCachedThreadPool( DAEMON_THREAD_FACTORY );
                 }
                 else if ( poolSize > 0 )
                 {
-                    pool = Executors.newFixedThreadPool( poolSize );
+                    pool = Executors.newFixedThreadPool( poolSize, DAEMON_THREAD_FACTORY
);
                 }
                 boolean doParallel = pool != null;
                 for ( ParentRunner runner : runners )

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e53ab122/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/pc/SchedulingStrategies.java
----------------------------------------------------------------------
diff --git a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/pc/SchedulingStrategies.java
b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/pc/SchedulingStrategies.java
index a7f45f8..6706951 100644
--- a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/pc/SchedulingStrategies.java
+++ b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/pc/SchedulingStrategies.java
@@ -20,9 +20,11 @@ package org.apache.maven.surefire.junitcore.pc;
  */
 
 import org.apache.maven.surefire.report.ConsoleLogger;
+import org.apache.maven.surefire.util.internal.DaemonThreadFactory;
 
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadFactory;
 
 /**
  * The factory of {@link SchedulingStrategy}.
@@ -32,6 +34,7 @@ import java.util.concurrent.Executors;
  */
 public class SchedulingStrategies
 {
+    private static final ThreadFactory DAEMON_THREAD_FACTORY = DaemonThreadFactory.newDaemonThreadFactory();
 
     /**
      * @param logger current error logger
@@ -49,7 +52,8 @@ public class SchedulingStrategies
      */
     public static SchedulingStrategy createParallelStrategy( ConsoleLogger logger, int nThreads
)
     {
-        return new NonSharedThreadPoolStrategy( logger, Executors.newFixedThreadPool( nThreads
) );
+        return new NonSharedThreadPoolStrategy( logger,
+                                                Executors.newFixedThreadPool( nThreads, DAEMON_THREAD_FACTORY
) );
     }
 
     /**
@@ -58,7 +62,7 @@ public class SchedulingStrategies
      */
     public static SchedulingStrategy createParallelStrategyUnbounded( ConsoleLogger logger
)
     {
-        return new NonSharedThreadPoolStrategy( logger, Executors.newCachedThreadPool() );
+        return new NonSharedThreadPoolStrategy( logger, Executors.newCachedThreadPool( DAEMON_THREAD_FACTORY
) );
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e53ab122/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/pc/SingleThreadScheduler.java
----------------------------------------------------------------------
diff --git a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/pc/SingleThreadScheduler.java
b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/pc/SingleThreadScheduler.java
index c235a15..88ae625 100644
--- a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/pc/SingleThreadScheduler.java
+++ b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/pc/SingleThreadScheduler.java
@@ -20,6 +20,7 @@ package org.apache.maven.surefire.junitcore.pc;
  */
 
 import org.apache.maven.surefire.report.ConsoleLogger;
+import org.apache.maven.surefire.util.internal.DaemonThreadFactory;
 import org.junit.runner.Description;
 import org.junit.runners.model.RunnerScheduler;
 
@@ -49,14 +50,8 @@ final class SingleThreadScheduler
 
     private static ExecutorService newPool()
     {
-        final ThreadFactory factory = new ThreadFactory()
-        {
-            public Thread newThread( Runnable r )
-            {
-                return new Thread( r, "maven-surefire-plugin@NotThreadSafe" );
-            }
-        };
-        return new ThreadPoolExecutor( 1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(),
factory );
+        ThreadFactory tf = DaemonThreadFactory.newDaemonThreadFactory( "maven-surefire-plugin@NotThreadSafe"
);
+        return new ThreadPoolExecutor( 1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(),
tf );
     }
 
     SingleThreadScheduler( ConsoleLogger logger )

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/e53ab122/surefire-providers/surefire-junit47/src/test/java/org/apache/maven/surefire/junitcore/pc/SchedulingStrategiesTest.java
----------------------------------------------------------------------
diff --git a/surefire-providers/surefire-junit47/src/test/java/org/apache/maven/surefire/junitcore/pc/SchedulingStrategiesTest.java
b/surefire-providers/surefire-junit47/src/test/java/org/apache/maven/surefire/junitcore/pc/SchedulingStrategiesTest.java
index 10bb3bc..a47030c 100644
--- a/surefire-providers/surefire-junit47/src/test/java/org/apache/maven/surefire/junitcore/pc/SchedulingStrategiesTest.java
+++ b/surefire-providers/surefire-junit47/src/test/java/org/apache/maven/surefire/junitcore/pc/SchedulingStrategiesTest.java
@@ -20,10 +20,12 @@ package org.apache.maven.surefire.junitcore.pc;
  */
 
 import org.apache.maven.surefire.junitcore.Logger;
+import org.apache.maven.surefire.util.internal.DaemonThreadFactory;
 import org.junit.Test;
 
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadFactory;
 
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
@@ -44,6 +46,8 @@ import static org.junit.Assert.assertTrue;
  */
 public class SchedulingStrategiesTest
 {
+    private static final ThreadFactory DAEMON_THREAD_FACTORY = DaemonThreadFactory.newDaemonThreadFactory();
+
     @Test
     public void invokerStrategy()
         throws InterruptedException
@@ -97,7 +101,7 @@ public class SchedulingStrategiesTest
     public void sharedPoolStrategy()
         throws InterruptedException
     {
-        ExecutorService sharedPool = Executors.newCachedThreadPool();
+        ExecutorService sharedPool = Executors.newCachedThreadPool( DAEMON_THREAD_FACTORY
);
 
         SchedulingStrategy strategy1 = SchedulingStrategies.createParallelSharedStrategy(
new Logger(), sharedPool );
         assertTrue( strategy1.hasSharedThreadPool() );


Mime
View raw message