Coverage Report - org.apache.maven.surefire.junitcore.pc.AbstractThreadPoolStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractThreadPoolStrategy
93%
30/32
70%
7/10
1,5
 
 1  
 package org.apache.maven.surefire.junitcore.pc;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *     http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.util.Collection;
 23  
 import java.util.concurrent.ExecutorService;
 24  
 import java.util.concurrent.Future;
 25  
 import java.util.concurrent.ThreadPoolExecutor;
 26  
 import java.util.concurrent.atomic.AtomicBoolean;
 27  
 
 28  
 /**
 29  
  * Abstract parallel scheduling strategy in private package.
 30  
  * The remaining abstract methods have to be implemented differently
 31  
  * depending if the thread pool is shared with other strategies or not.
 32  
  *
 33  
  * @author Tibor Digana (tibor17)
 34  
  * @since 2.16
 35  
  *
 36  
  * @see SchedulingStrategy
 37  
  * @see SharedThreadPoolStrategy
 38  
  * @see NonSharedThreadPoolStrategy
 39  
  */
 40  
 abstract class AbstractThreadPoolStrategy extends SchedulingStrategy {
 41  
     private final ExecutorService threadPool;
 42  
     private final Collection<Future<?>> futureResults;
 43  102
     private final AtomicBoolean canSchedule = new AtomicBoolean(true);
 44  
 
 45  
     AbstractThreadPoolStrategy(ExecutorService threadPool) {
 46  15
         this(threadPool, null);
 47  15
     }
 48  
 
 49  102
     AbstractThreadPoolStrategy(ExecutorService threadPool, Collection<Future<?>> futureResults) {
 50  102
         this.threadPool = threadPool;
 51  102
         this.futureResults = futureResults;
 52  102
     }
 53  
 
 54  
     protected final ExecutorService getThreadPool() {
 55  36
         return threadPool;
 56  
     }
 57  
 
 58  
     protected final Collection<Future<?>> getFutureResults() {
 59  185
         return futureResults;
 60  
     }
 61  
 
 62  
     protected final void disable() {
 63  203
         canSchedule.set(false);
 64  203
     }
 65  
 
 66  
     @Override
 67  
     public void schedule(Runnable task) {
 68  184
         if (canSchedule()) {
 69  181
             Future<?> futureResult = threadPool.submit(task);
 70  181
             if (futureResults != null) {
 71  154
                 futureResults.add(futureResult);
 72  
             }
 73  
         }
 74  184
     }
 75  
 
 76  
     @Override
 77  
     protected boolean stop() {
 78  1
         boolean wasRunning = canSchedule.getAndSet(false);
 79  1
         if (threadPool.isShutdown()) {
 80  1
             wasRunning = false;
 81  
         } else {
 82  0
             threadPool.shutdown();
 83  
         }
 84  1
         return wasRunning;
 85  
     }
 86  
 
 87  
     @Override
 88  
     protected boolean stopNow() {
 89  1
         boolean wasRunning = canSchedule.getAndSet(false);
 90  1
         if (threadPool.isShutdown()) {
 91  1
             wasRunning = false;
 92  
         } else {
 93  0
             threadPool.shutdownNow();
 94  
         }
 95  1
         return wasRunning;
 96  
     }
 97  
 
 98  
     @Override
 99  
     protected void setDefaultShutdownHandler(Scheduler.ShutdownHandler handler) {
 100  186
         if (threadPool instanceof ThreadPoolExecutor) {
 101  186
             ThreadPoolExecutor pool = (ThreadPoolExecutor) threadPool;
 102  186
             handler.setRejectedExecutionHandler(pool.getRejectedExecutionHandler());
 103  186
             pool.setRejectedExecutionHandler(handler);
 104  
         }
 105  186
     }
 106  
 
 107  
     @Override
 108  
     public final boolean canSchedule() {
 109  575
         return canSchedule.get();
 110  
     }
 111  
 }