Coverage Report - org.apache.maven.plugin.surefire.runorder.ThreadedExecutionScheduler
 
Classes in this File Line Coverage Branch Coverage Complexity
ThreadedExecutionScheduler
100%
28/28
100%
12/12
2,5
 
 1  
 package org.apache.maven.plugin.surefire.runorder;
 2  
 /*
 3  
  * Licensed to the Apache Software Foundation (ASF) under one
 4  
  * or more contributor license agreements.  See the NOTICE file
 5  
  * distributed with this work for additional information
 6  
  * regarding copyright ownership.  The ASF licenses this file
 7  
  * to you under the Apache License, Version 2.0 (the
 8  
  * "License"); you may not use this file except in compliance
 9  
  * with the License.  You may obtain a copy of the License at
 10  
  *
 11  
  *     http://www.apache.org/licenses/LICENSE-2.0
 12  
  *
 13  
  * Unless required by applicable law or agreed to in writing,
 14  
  * software distributed under the License is distributed on an
 15  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 16  
  * KIND, either express or implied.  See the License for the
 17  
  * specific language governing permissions and limitations
 18  
  * under the License.
 19  
  */
 20  
 
 21  
 import java.util.ArrayList;
 22  
 import java.util.List;
 23  
 
 24  
 
 25  
 /**
 26  
  * @author Kristian Rosenvold
 27  
  */
 28  
 public class ThreadedExecutionScheduler
 29  
 {
 30  
     private final int numThreads;
 31  
 
 32  
     private final int runTime[];
 33  
 
 34  
     private final List[] lists;
 35  
 
 36  
     public ThreadedExecutionScheduler( int numThreads )
 37  2
     {
 38  2
         this.numThreads = numThreads;
 39  2
         runTime = new int[numThreads];
 40  2
         lists = new List[numThreads];
 41  8
         for ( int i = 0; i < numThreads; i++ )
 42  
         {
 43  6
             lists[i] = new ArrayList<Class>();
 44  
         }
 45  2
     }
 46  
 
 47  
     public void addTest( PrioritizedTest prioritizedTest )
 48  
     {
 49  10
         final int leastBusySlot = findLeastBusySlot();
 50  10
         runTime[leastBusySlot] += prioritizedTest.getTotalRuntime();
 51  
         //noinspection unchecked
 52  10
         lists[leastBusySlot].add( prioritizedTest.getClazz() );
 53  10
     }
 54  
 
 55  
     public List<Class> getResult()
 56  
     {
 57  2
         List<Class> result = new ArrayList<Class>();
 58  2
         int index = 0;
 59  
         boolean added;
 60  
         do
 61  
         {
 62  7
             added = false;
 63  27
             for ( int i = 0; i < numThreads; i++ )
 64  
             {
 65  20
                 if ( lists[i].size() > index )
 66  
                 {
 67  10
                     result.add( (Class) lists[i].get( index ) );
 68  10
                     added = true;
 69  
                 }
 70  
             }
 71  7
             index++;
 72  
         }
 73  7
         while ( added );
 74  2
         return result;
 75  
     }
 76  
 
 77  
     private int findLeastBusySlot()
 78  
     {
 79  10
         int leastBusy = 0;
 80  10
         int minRuntime = runTime[0];
 81  30
         for ( int i = 1; i < numThreads; i++ )
 82  
         {
 83  20
             if ( runTime[i] < minRuntime )
 84  
             {
 85  12
                 leastBusy = i;
 86  12
                 minRuntime = runTime[i];
 87  
             }
 88  
         }
 89  10
         return leastBusy;
 90  
     }
 91  
 }