-
Notifications
You must be signed in to change notification settings - Fork 532
/
DefaultRunOrderCalculator.java
172 lines (152 loc) · 5.7 KB
/
DefaultRunOrderCalculator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package org.apache.maven.surefire.api.util;
/*
* 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 org.apache.maven.surefire.api.runorder.RunEntryStatisticsMap;
import org.apache.maven.surefire.api.testset.ResolvedTest;
import org.apache.maven.surefire.api.testset.RunOrderParameters;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;
/**
* Applies the final runorder of the tests
*
* @author Kristian Rosenvold
*/
public class DefaultRunOrderCalculator
implements RunOrderCalculator
{
private final Comparator<Class> sortOrder;
private final RunOrder[] runOrder;
private final List<ResolvedTest> resolvedTests;
private final RunOrderParameters runOrderParameters;
private final int threadCount;
private final Random random;
public DefaultRunOrderCalculator( RunOrderParameters runOrderParameters, int threadCount,
List<ResolvedTest> resolvedTests )
{
this.runOrderParameters = runOrderParameters;
this.threadCount = threadCount;
this.runOrder = runOrderParameters.getRunOrder();
this.resolvedTests = resolvedTests;
this.sortOrder = this.runOrder.length > 0 ? getSortOrderComparator( this.runOrder[0] ) : null;
Long runOrderRandomSeed = runOrderParameters.getRunOrderRandomSeed();
if ( runOrderRandomSeed == null )
{
runOrderRandomSeed = System.nanoTime();
runOrderParameters.setRunOrderRandomSeed( runOrderRandomSeed );
}
this.random = new Random( runOrderRandomSeed );
}
@Override
@SuppressWarnings( "checkstyle:magicnumber" )
public TestsToRun orderTestClasses( TestsToRun scannedClasses )
{
List<Class<?>> result = new ArrayList<>( 512 );
for ( Class<?> scannedClass : scannedClasses )
{
result.add( scannedClass );
}
orderTestClasses( result, runOrder.length != 0 ? runOrder[0] : null );
return new TestsToRun( new LinkedHashSet<>( result ) );
}
private void orderTestClasses( List<Class<?>> testClasses, RunOrder runOrder )
{
if ( RunOrder.RANDOM.equals( runOrder ) )
{
Collections.shuffle( testClasses, random );
}
else if ( RunOrder.FAILEDFIRST.equals( runOrder ) )
{
RunEntryStatisticsMap stat = RunEntryStatisticsMap.fromFile( runOrderParameters.getRunStatisticsFile() );
List<Class<?>> prioritized = stat.getPrioritizedTestsByFailureFirst( testClasses );
testClasses.clear();
testClasses.addAll( prioritized );
}
else if ( RunOrder.BALANCED.equals( runOrder ) )
{
RunEntryStatisticsMap stat = RunEntryStatisticsMap.fromFile( runOrderParameters.getRunStatisticsFile() );
List<Class<?>> prioritized = stat.getPrioritizedTestsClassRunTime( testClasses, threadCount );
testClasses.clear();
testClasses.addAll( prioritized );
}
else if ( sortOrder != null )
{
Collections.sort( testClasses, sortOrder );
}
}
private Comparator<Class> getSortOrderComparator( RunOrder runOrder )
{
if ( RunOrder.ALPHABETICAL.equals( runOrder ) )
{
return getAlphabeticalComparator();
}
else if ( RunOrder.REVERSE_ALPHABETICAL.equals( runOrder ) )
{
return getReverseAlphabeticalComparator();
}
else if ( RunOrder.HOURLY.equals( runOrder ) )
{
final int hour = Calendar.getInstance().get( Calendar.HOUR_OF_DAY );
return ( ( hour % 2 ) == 0 ) ? getAlphabeticalComparator() : getReverseAlphabeticalComparator();
}
else if ( RunOrder.TEST.equals( runOrder ) )
{
return getTestOrderComparator( resolvedTests );
}
else
{
return null;
}
}
private Comparator<Class> getTestOrderComparator( List<ResolvedTest> resolvedTests )
{
return Comparator.comparing( testClass -> IntStream.range( 0, resolvedTests.size() )
.filter( i -> resolvedTests.get( i ).matchClass( testClass.getName().replace( '.', '/' ) ) )
.findFirst()
.orElse( -1 )
);
}
private Comparator<Class> getReverseAlphabeticalComparator()
{
return new Comparator<Class>()
{
@Override
public int compare( Class o1, Class o2 )
{
return o2.getName().compareTo( o1.getName() );
}
};
}
private Comparator<Class> getAlphabeticalComparator()
{
return new Comparator<Class>()
{
@Override
public int compare( Class o1, Class o2 )
{
return o1.getName().compareTo( o2.getName() );
}
};
}
}