View Javadoc
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 org.apache.maven.surefire.junitcore.JUnitCoreParameters;
23  import org.apache.maven.surefire.report.ConsoleStream;
24  import org.apache.maven.surefire.report.DefaultDirectConsoleReporter;
25  import org.apache.maven.surefire.testset.TestSetFailedException;
26  import org.junit.AfterClass;
27  import org.junit.BeforeClass;
28  import org.junit.Before;
29  import org.junit.Rule;
30  import org.junit.Test;
31  import org.junit.experimental.theories.DataPoint;
32  import org.junit.experimental.theories.Theories;
33  import org.junit.experimental.theories.Theory;
34  import org.junit.rules.ExpectedException;
35  import org.junit.runner.JUnitCore;
36  import org.junit.runner.Result;
37  import org.junit.runner.RunWith;
38  
39  import java.util.Collections;
40  import java.util.HashMap;
41  import java.util.Map;
42  import java.util.concurrent.TimeUnit;
43  
44  import static org.apache.maven.surefire.junitcore.pc.ParallelComputerUtil.*;
45  import static org.apache.maven.surefire.junitcore.JUnitCoreParameters.*;
46  import static org.hamcrest.core.Is.is;
47  import static org.junit.Assert.*;
48  
49  /**
50   * Testing an algorithm in {@link ParallelComputerUtil} which configures
51   * allocated thread resources in ParallelComputer by given {@link org.apache.maven.surefire.junitcore.JUnitCoreParameters}.
52   *
53   * @author Tibor Digana (tibor17)
54   * @see ParallelComputerUtil
55   * @since 2.16
56   */
57  @RunWith( Theories.class )
58  public final class ParallelComputerUtilTest
59  {
60      private final ConsoleStream logger = new DefaultDirectConsoleReporter( System.out );
61  
62      @DataPoint
63      public static final int CPU_1 = 1;
64  
65      @DataPoint
66      public static final int CPU_4 = 4;
67  
68      @Rule
69      public final ExpectedException exception = ExpectedException.none();
70  
71      @BeforeClass
72      public static void beforeClass()
73      {
74          ParallelComputerUtil.overrideAvailableProcessors( 1 );
75      }
76  
77      @AfterClass
78      public static void afterClass()
79      {
80          ParallelComputerUtil.setDefaultAvailableProcessors();
81      }
82  
83      @Before
84      public void beforeTest()
85          throws InterruptedException
86      {
87          System.gc();
88          Thread.sleep( 50L );
89          assertFalse( Thread.currentThread().isInterrupted() );
90      }
91  
92      private static Map<String, String> parallel( String parallel )
93      {
94          return Collections.singletonMap( PARALLEL_KEY, parallel );
95      }
96  
97      @Test
98      public void unknownParallel()
99          throws TestSetFailedException
100     {
101         Map<String, String> properties = new HashMap<String, String>();
102         exception.expect( TestSetFailedException.class );
103         resolveConcurrency( new JUnitCoreParameters( properties ), null );
104     }
105 
106     @Test
107     public void unknownThreadCountSuites()
108         throws TestSetFailedException
109     {
110         JUnitCoreParameters params = new JUnitCoreParameters( parallel( "suites" ) );
111         assertTrue( params.isParallelSuites() );
112         assertFalse( params.isParallelClasses() );
113         assertFalse( params.isParallelMethods() );
114         exception.expect( TestSetFailedException.class );
115         resolveConcurrency( params, null );
116     }
117 
118     @Test
119     public void unknownThreadCountClasses()
120         throws TestSetFailedException
121     {
122         JUnitCoreParameters params = new JUnitCoreParameters( parallel( "classes" ) );
123         assertFalse( params.isParallelSuites() );
124         assertTrue( params.isParallelClasses() );
125         assertFalse( params.isParallelMethods() );
126         exception.expect( TestSetFailedException.class );
127         resolveConcurrency( params, null );
128     }
129 
130     @Test
131     public void unknownThreadCountMethods()
132         throws TestSetFailedException
133     {
134         JUnitCoreParameters params = new JUnitCoreParameters( parallel( "methods" ) );
135         assertFalse( params.isParallelSuites() );
136         assertFalse( params.isParallelClasses() );
137         assertTrue( params.isParallelMethods() );
138         exception.expect( TestSetFailedException.class );
139         resolveConcurrency( params, null );
140     }
141 
142     @Test
143     public void unknownThreadCountBoth()
144         throws TestSetFailedException
145     {
146         JUnitCoreParameters params = new JUnitCoreParameters( parallel( "both" ) );
147         assertFalse( params.isParallelSuites() );
148         assertTrue( params.isParallelClasses() );
149         assertTrue( params.isParallelMethods() );
150         exception.expect( TestSetFailedException.class );
151         resolveConcurrency( params, null );
152     }
153 
154     @Test
155     public void unknownThreadCountAll()
156         throws TestSetFailedException
157     {
158         JUnitCoreParameters params = new JUnitCoreParameters( parallel( "all" ) );
159         assertTrue( params.isParallelSuites() );
160         assertTrue( params.isParallelClasses() );
161         assertTrue( params.isParallelMethods() );
162         exception.expect( TestSetFailedException.class );
163         resolveConcurrency( params, null );
164     }
165 
166     @Test
167     public void unknownThreadCountSuitesAndClasses()
168         throws TestSetFailedException
169     {
170         JUnitCoreParameters params = new JUnitCoreParameters( parallel( "suitesAndClasses" ) );
171         assertTrue( params.isParallelSuites() );
172         assertTrue( params.isParallelClasses() );
173         assertFalse( params.isParallelMethods() );
174         exception.expect( TestSetFailedException.class );
175         resolveConcurrency( params, null );
176     }
177 
178     @Test
179     public void unknownThreadCountSuitesAndMethods()
180         throws TestSetFailedException
181     {
182         JUnitCoreParameters params = new JUnitCoreParameters( parallel( "suitesAndMethods" ) );
183         assertTrue( params.isParallelSuites() );
184         assertFalse( params.isParallelClasses() );
185         assertTrue( params.isParallelMethods() );
186         exception.expect( TestSetFailedException.class );
187         resolveConcurrency( params, null );
188     }
189 
190     @Test
191     public void unknownThreadCountClassesAndMethods()
192         throws TestSetFailedException
193     {
194         JUnitCoreParameters params = new JUnitCoreParameters( parallel( "classesAndMethods" ) );
195         assertFalse( params.isParallelSuites() );
196         assertTrue( params.isParallelClasses() );
197         assertTrue( params.isParallelMethods() );
198         exception.expect( TestSetFailedException.class );
199         resolveConcurrency( params, null );
200     }
201 
202     @Theory
203     public void useUnlimitedThreadsSuites( int cpu )
204         throws TestSetFailedException
205     {
206         ParallelComputerUtil.overrideAvailableProcessors( cpu );
207         Map<String, String> properties = new HashMap<String, String>();
208         properties.put(PARALLEL_KEY, "suites");
209         properties.put(USEUNLIMITEDTHREADS_KEY, "true");
210         JUnitCoreParameters params = new JUnitCoreParameters( properties );
211         Concurrency concurrency = resolveConcurrency( params, null );
212         assertTrue( params.isParallelSuites() );
213         assertFalse( params.isParallelClasses() );
214         assertFalse( params.isParallelMethods() );
215         assertThat( concurrency.capacity, is( Integer.MAX_VALUE ) );
216         assertThat( concurrency.suites, is( Integer.MAX_VALUE ) );
217         assertThat( concurrency.classes, is( 0 ) );
218         assertThat( concurrency.methods, is( 0 ) );
219 
220         properties.put(THREADCOUNTSUITES_KEY, "5");
221         params = new JUnitCoreParameters( properties );
222         concurrency = resolveConcurrency( params, null );
223         assertTrue( params.isParallelSuites() );
224         assertFalse( params.isParallelClasses() );
225         assertFalse( params.isParallelMethods() );
226         assertThat( concurrency.capacity, is( Integer.MAX_VALUE ) );
227         assertThat( concurrency.suites, is( 5 * cpu ) );
228         assertThat( concurrency.classes, is( 0 ) );
229         assertThat( concurrency.methods, is( 0 ) );
230     }
231 
232     @Theory
233     public void useUnlimitedThreadsClasses( int cpu )
234         throws TestSetFailedException
235     {
236         ParallelComputerUtil.overrideAvailableProcessors( cpu );
237         Map<String, String> properties = new HashMap<String, String>();
238         properties.put(PARALLEL_KEY, "classes");
239         properties.put(USEUNLIMITEDTHREADS_KEY, "true");
240         JUnitCoreParameters params = new JUnitCoreParameters( properties );
241         Concurrency concurrency = resolveConcurrency( params, null );
242         assertFalse( params.isParallelSuites() );
243         assertTrue( params.isParallelClasses() );
244         assertFalse( params.isParallelMethods() );
245         assertThat( concurrency.capacity, is( Integer.MAX_VALUE ) );
246         assertThat( concurrency.suites, is( 0 ) );
247         assertThat( concurrency.classes, is( Integer.MAX_VALUE ) );
248         assertThat( concurrency.methods, is( 0 ) );
249 
250         properties.put(THREADCOUNTCLASSES_KEY, "5");
251         params = new JUnitCoreParameters( properties );
252         concurrency = resolveConcurrency( params, null );
253         assertFalse( params.isParallelSuites() );
254         assertTrue( params.isParallelClasses() );
255         assertFalse( params.isParallelMethods() );
256         assertThat( concurrency.capacity, is( Integer.MAX_VALUE ) );
257         assertThat( concurrency.suites, is( 0 ) );
258         assertThat( concurrency.classes, is( 5 * cpu ) );
259         assertThat( concurrency.methods, is( 0 ) );
260     }
261 
262     @Theory
263     public void unlimitedThreadsMethods( int cpu )
264         throws TestSetFailedException
265     {
266         ParallelComputerUtil.overrideAvailableProcessors( cpu );
267         Map<String, String> properties = new HashMap<String, String>();
268         properties.put(PARALLEL_KEY, "methods");
269         properties.put(USEUNLIMITEDTHREADS_KEY, "true");
270         JUnitCoreParameters params = new JUnitCoreParameters( properties );
271         Concurrency concurrency = resolveConcurrency( params, null );
272         assertFalse( params.isParallelSuites() );
273         assertFalse( params.isParallelClasses() );
274         assertTrue( params.isParallelMethods() );
275         assertThat( concurrency.capacity, is( Integer.MAX_VALUE ) );
276         assertThat( concurrency.suites, is( 0 ) );
277         assertThat( concurrency.classes, is( 0 ) );
278         assertThat( concurrency.methods, is( Integer.MAX_VALUE ) );
279 
280         properties.put(THREADCOUNTMETHODS_KEY, "5");
281         params = new JUnitCoreParameters( properties );
282         concurrency = resolveConcurrency( params, null );
283         assertFalse( params.isParallelSuites() );
284         assertFalse( params.isParallelClasses() );
285         assertTrue( params.isParallelMethods() );
286         assertThat( concurrency.capacity, is( Integer.MAX_VALUE ) );
287         assertThat( concurrency.suites, is( 0 ) );
288         assertThat( concurrency.classes, is( 0 ) );
289         assertThat( concurrency.methods, is( 5 * cpu ) );
290     }
291 
292     @Theory
293     public void unlimitedThreadsSuitesAndClasses( int cpu )
294         throws TestSetFailedException
295     {
296         ParallelComputerUtil.overrideAvailableProcessors( cpu );
297         Map<String, String> properties = new HashMap<String, String>();
298         properties.put(PARALLEL_KEY, "suitesAndClasses");
299         properties.put(USEUNLIMITEDTHREADS_KEY, "true");
300         JUnitCoreParameters params = new JUnitCoreParameters( properties );
301         Concurrency concurrency = resolveConcurrency( params, null );
302         assertTrue( params.isParallelSuites() );
303         assertTrue( params.isParallelClasses() );
304         assertFalse( params.isParallelMethods() );
305         assertThat( concurrency.capacity, is( Integer.MAX_VALUE ) );
306         assertThat( concurrency.suites, is( Integer.MAX_VALUE ) );
307         assertThat( concurrency.classes, is( Integer.MAX_VALUE ) );
308         assertThat( concurrency.methods, is( 0 ) );
309 
310         properties.put(THREADCOUNTSUITES_KEY, "5");
311         properties.put(THREADCOUNTCLASSES_KEY, "15");
312         params = new JUnitCoreParameters( properties );
313         concurrency = resolveConcurrency( params, null );
314         assertTrue( params.isParallelSuites() );
315         assertTrue( params.isParallelClasses() );
316         assertFalse( params.isParallelMethods() );
317         assertThat( concurrency.capacity, is( Integer.MAX_VALUE ) );
318         assertThat( concurrency.suites, is( 5 * cpu ) );
319         assertThat( concurrency.classes, is( 15 * cpu ) );
320         assertThat( concurrency.methods, is( 0 ) );
321     }
322 
323     @Theory
324     public void unlimitedThreadsSuitesAndMethods( int cpu )
325         throws TestSetFailedException
326     {
327         ParallelComputerUtil.overrideAvailableProcessors( cpu );
328         Map<String, String> properties = new HashMap<String, String>();
329         properties.put(PARALLEL_KEY, "suitesAndMethods");
330         properties.put(USEUNLIMITEDTHREADS_KEY, "true");
331         JUnitCoreParameters params = new JUnitCoreParameters( properties );
332         Concurrency concurrency = resolveConcurrency( params, null );
333         assertTrue( params.isParallelSuites() );
334         assertFalse( params.isParallelClasses() );
335         assertTrue( params.isParallelMethods() );
336         assertThat( concurrency.capacity, is( Integer.MAX_VALUE ) );
337         assertThat( concurrency.suites, is( Integer.MAX_VALUE ) );
338         assertThat( concurrency.classes, is( 0 ) );
339         assertThat( concurrency.methods, is( Integer.MAX_VALUE ) );
340 
341         properties.put(THREADCOUNTSUITES_KEY, "5");
342         properties.put(THREADCOUNTMETHODS_KEY, "15");
343         params = new JUnitCoreParameters( properties );
344         concurrency = resolveConcurrency( params, null );
345         assertTrue( params.isParallelSuites() );
346         assertFalse( params.isParallelClasses() );
347         assertTrue( params.isParallelMethods() );
348         assertThat( concurrency.capacity, is( Integer.MAX_VALUE ) );
349         assertThat( concurrency.suites, is( 5 * cpu ) );
350         assertThat( concurrency.classes, is( 0 ) );
351         assertThat( concurrency.methods, is( 15 * cpu ) );
352     }
353 
354     @Theory
355     public void unlimitedThreadsClassesAndMethods( int cpu )
356         throws TestSetFailedException
357     {
358         ParallelComputerUtil.overrideAvailableProcessors( cpu );
359         Map<String, String> properties = new HashMap<String, String>();
360         properties.put(PARALLEL_KEY, "classesAndMethods");
361         properties.put(USEUNLIMITEDTHREADS_KEY, "true");
362         JUnitCoreParameters params = new JUnitCoreParameters( properties );
363         Concurrency concurrency = resolveConcurrency( params, null );
364         assertFalse( params.isParallelSuites() );
365         assertTrue( params.isParallelClasses() );
366         assertTrue( params.isParallelMethods() );
367         assertThat( concurrency.capacity, is( Integer.MAX_VALUE ) );
368         assertThat( concurrency.suites, is( 0 ) );
369         assertThat( concurrency.classes, is( Integer.MAX_VALUE ) );
370         assertThat( concurrency.methods, is( Integer.MAX_VALUE ) );
371 
372         properties.put(THREADCOUNTCLASSES_KEY, "5");
373         properties.put(THREADCOUNTMETHODS_KEY, "15");
374         params = new JUnitCoreParameters( properties );
375         concurrency = resolveConcurrency( params, null );
376         assertFalse( params.isParallelSuites() );
377         assertTrue( params.isParallelClasses() );
378         assertTrue( params.isParallelMethods() );
379         assertThat( concurrency.capacity, is( Integer.MAX_VALUE ) );
380         assertThat( concurrency.suites, is( 0 ) );
381         assertThat( concurrency.classes, is( 5 * cpu ) );
382         assertThat( concurrency.methods, is( 15 * cpu ) );
383     }
384 
385     @Theory
386     public void unlimitedThreadsAll( int cpu )
387         throws TestSetFailedException
388     {
389         ParallelComputerUtil.overrideAvailableProcessors( cpu );
390         Map<String, String> properties = new HashMap<String, String>();
391         properties.put(PARALLEL_KEY, "all");
392         properties.put(USEUNLIMITEDTHREADS_KEY, "true");
393         JUnitCoreParameters params = new JUnitCoreParameters( properties );
394         Concurrency concurrency = resolveConcurrency( params, null );
395         assertTrue( params.isParallelSuites() );
396         assertTrue( params.isParallelClasses() );
397         assertTrue( params.isParallelMethods() );
398         assertThat( concurrency.capacity, is( Integer.MAX_VALUE ) );
399         assertThat( concurrency.suites, is( Integer.MAX_VALUE ) );
400         assertThat( concurrency.classes, is( Integer.MAX_VALUE ) );
401         assertThat( concurrency.methods, is( Integer.MAX_VALUE ) );
402 
403         properties.put(THREADCOUNTSUITES_KEY, "5");
404         properties.put(THREADCOUNTCLASSES_KEY, "15");
405         properties.put(THREADCOUNTMETHODS_KEY, "30");
406         params = new JUnitCoreParameters( properties );
407         concurrency = resolveConcurrency( params, null );
408         assertTrue( params.isParallelSuites() );
409         assertTrue( params.isParallelClasses() );
410         assertTrue( params.isParallelMethods() );
411         assertThat( concurrency.capacity, is( Integer.MAX_VALUE ) );
412         assertThat( concurrency.suites, is( 5 * cpu ) );
413         assertThat( concurrency.classes, is( 15 * cpu ) );
414         assertThat( concurrency.methods, is( 30 * cpu ) );
415     }
416 
417     @Theory
418     public void threadCountSuites( int cpu )
419         throws TestSetFailedException
420     {
421         ParallelComputerUtil.overrideAvailableProcessors( cpu );
422         Map<String, String> properties = new HashMap<String, String>();
423         properties.put(PARALLEL_KEY, "suites");
424         properties.put(THREADCOUNT_KEY, "3");
425         JUnitCoreParameters params = new JUnitCoreParameters( properties );
426         Concurrency concurrency = resolveConcurrency( params, null );
427         assertTrue( params.isParallelSuites() );
428         assertFalse( params.isParallelClasses() );
429         assertFalse( params.isParallelMethods() );
430         assertThat( concurrency.capacity, is( 0 ) );
431         assertThat( concurrency.suites, is( 3 * cpu ) );
432         assertThat( concurrency.classes, is( 0 ) );
433         assertThat( concurrency.methods, is( 0 ) );
434     }
435 
436     @Theory
437     public void threadCountClasses( int cpu )
438         throws TestSetFailedException
439     {
440         ParallelComputerUtil.overrideAvailableProcessors( cpu );
441         Map<String, String> properties = new HashMap<String, String>();
442         properties.put(PARALLEL_KEY, "classes");
443         properties.put(THREADCOUNT_KEY, "3");
444         JUnitCoreParameters params = new JUnitCoreParameters( properties );
445         Concurrency concurrency = resolveConcurrency( params, null );
446         assertFalse( params.isParallelSuites() );
447         assertTrue( params.isParallelClasses() );
448         assertFalse( params.isParallelMethods() );
449         assertThat( concurrency.capacity, is( 0 ) );
450         assertThat( concurrency.suites, is( 0 ) );
451         assertThat( concurrency.classes, is( 3 * cpu ) );
452         assertThat( concurrency.methods, is( 0 ) );
453     }
454 
455     @Theory
456     public void threadCountMethods( int cpu )
457         throws TestSetFailedException
458     {
459         ParallelComputerUtil.overrideAvailableProcessors( cpu );
460         Map<String, String> properties = new HashMap<String, String>();
461         properties.put(PARALLEL_KEY, "methods");
462         properties.put(THREADCOUNT_KEY, "3");
463         JUnitCoreParameters params = new JUnitCoreParameters( properties );
464         Concurrency concurrency = resolveConcurrency( params, null );
465         assertFalse( params.isParallelSuites() );
466         assertFalse( params.isParallelClasses() );
467         assertTrue( params.isParallelMethods() );
468         assertThat( concurrency.capacity, is( 0 ) );
469         assertThat( concurrency.suites, is( 0 ) );
470         assertThat( concurrency.classes, is( 0 ) );
471         assertThat( concurrency.methods, is( 3 * cpu ) );
472     }
473 
474     @Theory
475     public void threadCountBoth( int cpu )
476         throws TestSetFailedException
477     {
478         ParallelComputerUtil.overrideAvailableProcessors( cpu );
479         Map<String, String> properties = new HashMap<String, String>();
480         properties.put(PARALLEL_KEY, "both");
481         properties.put(THREADCOUNT_KEY, "3");
482         JUnitCoreParameters params = new JUnitCoreParameters( properties );
483         Concurrency concurrency = resolveConcurrency( params, null );
484         assertFalse( params.isParallelSuites() );
485         assertTrue( params.isParallelClasses() );
486         assertTrue( params.isParallelMethods() );
487         assertThat( concurrency.capacity, is( 3 * cpu ) );
488         assertThat( concurrency.suites, is( 0 ) );
489         assertThat( concurrency.classes, is( (int) ( ( 3d / 2 ) * cpu ) ) );
490         assertThat( concurrency.methods, is( Integer.MAX_VALUE ) );
491     }
492 
493     @Theory
494     public void threadCountClassesAndMethods( int cpu )
495         throws TestSetFailedException
496     {
497         ParallelComputerUtil.overrideAvailableProcessors( cpu );
498         Map<String, String> properties = new HashMap<String, String>();
499         properties.put(PARALLEL_KEY, "classesAndMethods");
500         properties.put(THREADCOUNT_KEY, "3");
501         JUnitCoreParameters params = new JUnitCoreParameters( properties );
502         Concurrency concurrency = resolveConcurrency( params, null );
503         assertFalse( params.isParallelSuites() );
504         assertTrue( params.isParallelClasses() );
505         assertTrue( params.isParallelMethods() );
506         assertThat( concurrency.capacity, is( 3 * cpu ) );
507         assertThat( concurrency.suites, is( 0 ) );
508         assertThat( concurrency.classes, is( (int) ( ( 3d / 2 ) * cpu ) ) );
509         assertThat( concurrency.methods, is( Integer.MAX_VALUE ) );
510     }
511 
512     @Theory
513     public void threadCountSuitesAndMethods( int cpu )
514         throws TestSetFailedException
515     {
516         ParallelComputerUtil.overrideAvailableProcessors( cpu );
517         Map<String, String> properties = new HashMap<String, String>();
518         properties.put(PARALLEL_KEY, "suitesAndMethods");
519         properties.put(THREADCOUNT_KEY, "3");
520         JUnitCoreParameters params = new JUnitCoreParameters( properties );
521         Concurrency concurrency = resolveConcurrency( params, null );
522         assertTrue( params.isParallelSuites() );
523         assertFalse( params.isParallelClasses() );
524         assertTrue( params.isParallelMethods() );
525         assertThat( concurrency.capacity, is( 3 * cpu ) );
526         assertThat( concurrency.suites, is( (int) ( ( 3d / 2 ) * cpu ) ) );
527         assertThat( concurrency.classes, is( 0 ) );
528         assertThat( concurrency.methods, is( Integer.MAX_VALUE ) );
529     }
530 
531     @Theory
532     public void threadCountSuitesAndClasses( int cpu )
533         throws TestSetFailedException
534     {
535         ParallelComputerUtil.overrideAvailableProcessors( cpu );
536         Map<String, String> properties = new HashMap<String, String>();
537         properties.put(PARALLEL_KEY, "suitesAndClasses");
538         properties.put(THREADCOUNT_KEY, "3");
539         JUnitCoreParameters params = new JUnitCoreParameters( properties );
540         Concurrency concurrency = resolveConcurrency( params, null );
541         assertTrue( params.isParallelSuites() );
542         assertTrue( params.isParallelClasses() );
543         assertFalse( params.isParallelMethods() );
544         assertThat( concurrency.capacity, is( 3 * cpu ) );
545         assertThat( concurrency.suites, is( (int) ( ( 3d / 2 ) * cpu ) ) );
546         assertThat( concurrency.classes, is( Integer.MAX_VALUE ) );
547         assertThat( concurrency.methods, is( 0 ) );
548     }
549 
550     @Theory
551     public void threadCountAll( int cpu )
552         throws TestSetFailedException
553     {
554         ParallelComputerUtil.overrideAvailableProcessors( cpu );
555         Map<String, String> properties = new HashMap<String, String>();
556         properties.put(PARALLEL_KEY, "all");
557         properties.put(THREADCOUNT_KEY, "3");
558         JUnitCoreParameters params = new JUnitCoreParameters( properties );
559         Concurrency concurrency = resolveConcurrency( params, null );
560         assertTrue( params.isParallelSuites() );
561         assertTrue( params.isParallelClasses() );
562         assertTrue( params.isParallelMethods() );
563         assertThat( concurrency.capacity, is( 3 * cpu ) );
564         assertThat( concurrency.suites, is( cpu ) );
565         assertThat( concurrency.classes, is( cpu ) );
566         assertThat( concurrency.methods, is( Integer.MAX_VALUE ) );
567     }
568 
569     @Theory
570     public void everyThreadCountSuitesAndClasses( int cpu )
571         throws TestSetFailedException
572     {
573         ParallelComputerUtil.overrideAvailableProcessors( cpu );
574         Map<String, String> properties = new HashMap<String, String>();
575         properties.put(PARALLEL_KEY, "suitesAndClasses");
576         properties.put(THREADCOUNT_KEY, "3");
577         // % percentage ratio
578         properties.put(THREADCOUNTSUITES_KEY, "34");
579         properties.put(THREADCOUNTCLASSES_KEY, "66");
580         JUnitCoreParameters params = new JUnitCoreParameters( properties );
581         Concurrency concurrency = resolveConcurrency( params, null );
582         assertTrue( params.isParallelSuites() );
583         assertTrue( params.isParallelClasses() );
584         assertFalse( params.isParallelMethods() );
585         assertThat( concurrency.capacity, is( 3 * cpu ) );
586         int concurrentSuites = (int) ( 0.34d * concurrency.capacity );
587         assertThat( concurrency.suites, is( concurrentSuites ) );
588         assertThat( concurrency.classes, is( concurrency.capacity - concurrentSuites ) );
589         assertThat( concurrency.methods, is( 0 ) );
590     }
591 
592     @Theory
593     public void everyThreadCountSuitesAndMethods( int cpu )
594         throws TestSetFailedException
595     {
596         ParallelComputerUtil.overrideAvailableProcessors( cpu );
597         Map<String, String> properties = new HashMap<String, String>();
598         properties.put(PARALLEL_KEY, "suitesAndMethods");
599         properties.put(THREADCOUNT_KEY, "3");
600         // % percentage ratio
601         properties.put(THREADCOUNTSUITES_KEY, "34");
602         properties.put(THREADCOUNTMETHODS_KEY, "66");
603         JUnitCoreParameters params = new JUnitCoreParameters( properties );
604         Concurrency concurrency = resolveConcurrency( params, null );
605         assertTrue( params.isParallelSuites() );
606         assertFalse( params.isParallelClasses() );
607         assertTrue( params.isParallelMethods() );
608         assertThat( concurrency.capacity, is( 3 * cpu ) );
609         int concurrentSuites = (int) ( 0.34d * concurrency.capacity );
610         assertThat( concurrency.suites, is( concurrentSuites ) );
611         assertThat( concurrency.classes, is( 0 ) );
612         assertThat( concurrency.methods, is( concurrency.capacity - concurrentSuites ) );
613     }
614 
615     @Theory
616     public void everyThreadCountClassesAndMethods( int cpu )
617         throws TestSetFailedException
618     {
619         ParallelComputerUtil.overrideAvailableProcessors( cpu );
620         Map<String, String> properties = new HashMap<String, String>();
621         properties.put(PARALLEL_KEY, "classesAndMethods");
622         properties.put(THREADCOUNT_KEY, "3");
623         // % percentage ratio
624         properties.put(THREADCOUNTCLASSES_KEY, "34");
625         properties.put(THREADCOUNTMETHODS_KEY, "66");
626         JUnitCoreParameters params = new JUnitCoreParameters( properties );
627         Concurrency concurrency = resolveConcurrency( params, null );
628         assertFalse( params.isParallelSuites() );
629         assertTrue( params.isParallelClasses() );
630         assertTrue( params.isParallelMethods() );
631         assertThat( concurrency.capacity, is( 3 * cpu ) );
632         assertThat( concurrency.suites, is( 0 ) );
633         int concurrentClasses = (int) ( 0.34d * concurrency.capacity );
634         assertThat( concurrency.classes, is( concurrentClasses ) );
635         assertThat( concurrency.methods, is( concurrency.capacity - concurrentClasses ) );
636     }
637 
638     @Theory
639     public void everyThreadCountAll( int cpu )
640         throws TestSetFailedException
641     {
642         ParallelComputerUtil.overrideAvailableProcessors( cpu );
643         Map<String, String> properties = new HashMap<String, String>();
644         properties.put(PARALLEL_KEY, "all");
645         properties.put(THREADCOUNT_KEY, "3");
646         // % percentage ratio
647         properties.put(THREADCOUNTSUITES_KEY, "17");
648         properties.put(THREADCOUNTCLASSES_KEY, "34");
649         properties.put(THREADCOUNTMETHODS_KEY, "49");
650         JUnitCoreParameters params = new JUnitCoreParameters( properties );
651         Concurrency concurrency = resolveConcurrency( params, null );
652         assertTrue( params.isParallelSuites() );
653         assertTrue( params.isParallelClasses() );
654         assertTrue( params.isParallelMethods() );
655         assertThat( concurrency.capacity, is( 3 * cpu ) );
656         int concurrentSuites = (int) ( 0.17d * concurrency.capacity );
657         int concurrentClasses = (int) ( 0.34d * concurrency.capacity );
658         assertThat( concurrency.suites, is( concurrentSuites ) );
659         assertThat( concurrency.classes, is( concurrentClasses ) );
660         assertThat( concurrency.methods, is( concurrency.capacity - concurrentSuites - concurrentClasses ) );
661     }
662 
663     @Theory
664     public void reusableThreadCountSuitesAndClasses( int cpu )
665         throws TestSetFailedException
666     {
667         // 4 * cpu to 5 * cpu threads to run test classes
668         ParallelComputerUtil.overrideAvailableProcessors( cpu );
669         Map<String, String> properties = new HashMap<String, String>();
670         properties.put(PARALLEL_KEY, "suitesAndClasses");
671         properties.put(THREADCOUNT_KEY, "6");
672         properties.put(THREADCOUNTSUITES_KEY, "2");
673         JUnitCoreParameters params = new JUnitCoreParameters( properties );
674         Concurrency concurrency = resolveConcurrency( params, null );
675         assertTrue( params.isParallelSuites() );
676         assertTrue( params.isParallelClasses() );
677         assertFalse( params.isParallelMethods() );
678         assertThat( concurrency.capacity, is( 6 * cpu ) );
679         assertThat( concurrency.suites, is( 2 * cpu ) );
680         assertThat( concurrency.classes, is( Integer.MAX_VALUE ) );
681         assertThat( concurrency.methods, is( 0 ) );
682     }
683 
684     @Theory
685     public void reusableThreadCountSuitesAndMethods( int cpu )
686         throws TestSetFailedException
687     {
688         // 4 * cpu to 5 * cpu threads to run test methods
689         ParallelComputerUtil.overrideAvailableProcessors( cpu );
690         Map<String, String> properties = new HashMap<String, String>();
691         properties.put(PARALLEL_KEY, "suitesAndMethods");
692         properties.put(THREADCOUNT_KEY, "6");
693         properties.put(THREADCOUNTSUITES_KEY, "2");
694         JUnitCoreParameters params = new JUnitCoreParameters( properties );
695         Concurrency concurrency = resolveConcurrency( params, null );
696         assertTrue( params.isParallelSuites() );
697         assertFalse( params.isParallelClasses() );
698         assertTrue( params.isParallelMethods() );
699         assertThat( concurrency.capacity, is( 6 * cpu ) );
700         assertThat( concurrency.suites, is( 2 * cpu ) );
701         assertThat( concurrency.classes, is( 0 ) );
702         assertThat( concurrency.methods, is( Integer.MAX_VALUE ) );
703     }
704 
705     @Theory
706     public void reusableThreadCountClassesAndMethods( int cpu )
707         throws TestSetFailedException
708     {
709         // 4 * cpu to 5 * cpu threads to run test methods
710         ParallelComputerUtil.overrideAvailableProcessors( cpu );
711         Map<String, String> properties = new HashMap<String, String>();
712         properties.put(PARALLEL_KEY, "classesAndMethods");
713         properties.put(THREADCOUNT_KEY, "6");
714         properties.put(THREADCOUNTCLASSES_KEY, "2");
715         JUnitCoreParameters params = new JUnitCoreParameters( properties );
716         Concurrency concurrency = resolveConcurrency( params, null );
717         assertFalse( params.isParallelSuites() );
718         assertTrue( params.isParallelClasses() );
719         assertTrue( params.isParallelMethods() );
720         assertThat( concurrency.capacity, is( 6 * cpu ) );
721         assertThat( concurrency.suites, is( 0 ) );
722         assertThat( concurrency.classes, is( 2 * cpu ) );
723         assertThat( concurrency.methods, is( Integer.MAX_VALUE ) );
724     }
725 
726     @Theory
727     public void reusableThreadCountAll( int cpu )
728         throws TestSetFailedException
729     {
730         // 8 * cpu to 13 * cpu threads to run test methods
731         ParallelComputerUtil.overrideAvailableProcessors( cpu );
732         Map<String, String> properties = new HashMap<String, String>();
733         properties.put(PARALLEL_KEY, "all");
734         properties.put(THREADCOUNT_KEY, "14");
735         properties.put(THREADCOUNTSUITES_KEY, "2");
736         properties.put(THREADCOUNTCLASSES_KEY, "4");
737         JUnitCoreParameters params = new JUnitCoreParameters( properties );
738         Concurrency concurrency = resolveConcurrency( params, null );
739         assertTrue( params.isParallelSuites() );
740         assertTrue( params.isParallelClasses() );
741         assertTrue( params.isParallelMethods() );
742         assertThat( concurrency.capacity, is( 14 * cpu ) );
743         assertThat( concurrency.suites, is( 2 * cpu ) );
744         assertThat( concurrency.classes, is( 4 * cpu ) );
745         assertThat( concurrency.methods, is( Integer.MAX_VALUE ) );
746     }
747 
748     @Theory
749     public void suites( int cpu )
750         throws TestSetFailedException
751     {
752         ParallelComputerUtil.overrideAvailableProcessors( cpu );
753         Map<String, String> properties = new HashMap<String, String>();
754         properties.put(PARALLEL_KEY, "suites");
755         properties.put(THREADCOUNTSUITES_KEY, "5");
756         JUnitCoreParameters params = new JUnitCoreParameters( properties );
757         Concurrency concurrency = resolveConcurrency( params, null );
758         assertTrue( params.isParallelSuites() );
759         assertFalse( params.isParallelClasses() );
760         assertFalse( params.isParallelMethods() );
761         assertThat( concurrency.capacity, is( 5 * cpu ) );
762         assertThat( concurrency.suites, is( 5 * cpu ) );
763         assertThat( concurrency.classes, is( 0 ) );
764         assertThat( concurrency.methods, is( 0 ) );
765     }
766 
767     @Theory
768     public void classes( int cpu )
769         throws TestSetFailedException
770     {
771         ParallelComputerUtil.overrideAvailableProcessors( cpu );
772         Map<String, String> properties = new HashMap<String, String>();
773         properties.put(PARALLEL_KEY, "classes");
774         properties.put(THREADCOUNTCLASSES_KEY, "5");
775         JUnitCoreParameters params = new JUnitCoreParameters( properties );
776         Concurrency concurrency = resolveConcurrency( params, null );
777         assertFalse( params.isParallelSuites() );
778         assertTrue( params.isParallelClasses() );
779         assertFalse( params.isParallelMethods() );
780         assertThat( concurrency.capacity, is( 5 * cpu ) );
781         assertThat( concurrency.suites, is( 0 ) );
782         assertThat( concurrency.classes, is( 5 * cpu ) );
783         assertThat( concurrency.methods, is( 0 ) );
784     }
785 
786     @Theory
787     public void methods( int cpu )
788         throws TestSetFailedException
789     {
790         ParallelComputerUtil.overrideAvailableProcessors( cpu );
791         Map<String, String> properties = new HashMap<String, String>();
792         properties.put(PARALLEL_KEY, "methods");
793         properties.put(THREADCOUNTMETHODS_KEY, "5");
794         JUnitCoreParameters params = new JUnitCoreParameters( properties );
795         Concurrency concurrency = resolveConcurrency( params, null );
796         assertFalse( params.isParallelSuites() );
797         assertFalse( params.isParallelClasses() );
798         assertTrue( params.isParallelMethods() );
799         assertThat( concurrency.capacity, is( 5 * cpu ) );
800         assertThat( concurrency.suites, is( 0 ) );
801         assertThat( concurrency.classes, is( 0 ) );
802         assertThat( concurrency.methods, is( 5 * cpu ) );
803     }
804 
805     @Theory
806     public void suitesAndClasses( int cpu )
807         throws TestSetFailedException
808     {
809         ParallelComputerUtil.overrideAvailableProcessors( cpu );
810         Map<String, String> properties = new HashMap<String, String>();
811 
812         properties.put(PARALLEL_KEY, "suitesAndClasses");
813         properties.put(THREADCOUNTSUITES_KEY, "5");
814         properties.put(THREADCOUNTCLASSES_KEY, "15");
815         JUnitCoreParameters params = new JUnitCoreParameters( properties );
816         Concurrency concurrency = resolveConcurrency( params, null );
817         assertTrue( params.isParallelSuites() );
818         assertTrue( params.isParallelClasses() );
819         assertFalse( params.isParallelMethods() );
820         assertThat( concurrency.capacity, is( 20 * cpu ) );
821         assertThat( concurrency.suites, is( 5 * cpu ) );
822         assertThat( concurrency.classes, is( 15 * cpu ) );
823         assertThat( concurrency.methods, is( 0 ) );
824 
825         // Warning: this case works but is not enabled in AbstractSurefireMojo
826         // Instead use the 'useUnlimitedThreads' parameter.
827         properties = new HashMap<String, String>();
828         properties.put(PARALLEL_KEY, "suitesAndClasses");
829         properties.put(THREADCOUNTSUITES_KEY, "5");
830         params = new JUnitCoreParameters( properties );
831         concurrency = resolveConcurrency( params, null );
832         assertTrue( params.isParallelSuites() );
833         assertTrue( params.isParallelClasses() );
834         assertFalse( params.isParallelMethods() );
835         assertThat( concurrency.capacity, is( Integer.MAX_VALUE ) );
836         assertThat( concurrency.suites, is( 5 * cpu ) );
837         assertThat( concurrency.classes, is( Integer.MAX_VALUE ) );
838         assertThat( concurrency.methods, is( 0 ) );
839     }
840 
841     @Theory
842     public void suitesAndMethods( int cpu )
843         throws TestSetFailedException
844     {
845         ParallelComputerUtil.overrideAvailableProcessors( cpu );
846         Map<String, String> properties = new HashMap<String, String>();
847 
848         properties.put(PARALLEL_KEY, "suitesAndMethods");
849         properties.put(THREADCOUNTSUITES_KEY, "5");
850         properties.put(THREADCOUNTMETHODS_KEY, "15");
851         JUnitCoreParameters params = new JUnitCoreParameters( properties );
852         Concurrency concurrency = resolveConcurrency( params, null );
853         assertTrue( params.isParallelSuites() );
854         assertFalse( params.isParallelClasses() );
855         assertTrue( params.isParallelMethods() );
856         assertThat( concurrency.capacity, is( 20 * cpu ) );
857         assertThat( concurrency.suites, is( 5 * cpu ) );
858         assertThat( concurrency.classes, is( 0 ) );
859         assertThat( concurrency.methods, is( 15 * cpu ) );
860 
861         // Warning: this case works but is not enabled in AbstractSurefireMojo
862         // Instead use the 'useUnlimitedThreads' parameter.
863         properties = new HashMap<String, String>();
864         properties.put(PARALLEL_KEY, "suitesAndMethods");
865         properties.put(THREADCOUNTSUITES_KEY, "5");
866         params = new JUnitCoreParameters( properties );
867         concurrency = resolveConcurrency( params, null );
868         assertTrue( params.isParallelSuites() );
869         assertFalse( params.isParallelClasses() );
870         assertTrue( params.isParallelMethods() );
871         assertThat( concurrency.capacity, is( Integer.MAX_VALUE ) );
872         assertThat( concurrency.suites, is( 5 * cpu ) );
873         assertThat( concurrency.classes, is( 0 ) );
874         assertThat( concurrency.methods, is( Integer.MAX_VALUE ) );
875     }
876 
877     @Theory
878     public void classesAndMethods( int cpu )
879         throws TestSetFailedException
880     {
881         ParallelComputerUtil.overrideAvailableProcessors( cpu );
882         Map<String, String> properties = new HashMap<String, String>();
883 
884         properties.put(PARALLEL_KEY, "classesAndMethods");
885         properties.put(THREADCOUNTCLASSES_KEY, "5");
886         properties.put(THREADCOUNTMETHODS_KEY, "15");
887         JUnitCoreParameters params = new JUnitCoreParameters( properties );
888         Concurrency concurrency = resolveConcurrency( params, null );
889         assertFalse( params.isParallelSuites() );
890         assertTrue( params.isParallelClasses() );
891         assertTrue( params.isParallelMethods() );
892         assertThat( concurrency.capacity, is( 20 * cpu ) );
893         assertThat( concurrency.suites, is( 0 ) );
894         assertThat( concurrency.classes, is( 5 * cpu ) );
895         assertThat( concurrency.methods, is( 15 * cpu ) );
896 
897         // Warning: this case works but is not enabled in AbstractSurefireMojo
898         // Instead use the 'useUnlimitedThreads' parameter.
899         properties = new HashMap<String, String>();
900         properties.put(PARALLEL_KEY, "classesAndMethods");
901         properties.put(THREADCOUNTCLASSES_KEY, "5");
902         params = new JUnitCoreParameters( properties );
903         concurrency = resolveConcurrency( params, null );
904         assertFalse( params.isParallelSuites() );
905         assertTrue( params.isParallelClasses() );
906         assertTrue( params.isParallelMethods() );
907         assertThat( concurrency.capacity, is( Integer.MAX_VALUE ) );
908         assertThat( concurrency.suites, is( 0 ) );
909         assertThat( concurrency.classes, is( 5 * cpu ) );
910         assertThat( concurrency.methods, is( Integer.MAX_VALUE ) );
911     }
912 
913     @Theory
914     public void all( int cpu )
915         throws TestSetFailedException
916     {
917         ParallelComputerUtil.overrideAvailableProcessors( cpu );
918         Map<String, String> properties = new HashMap<String, String>();
919 
920         properties.put(PARALLEL_KEY, "all");
921         properties.put(THREADCOUNTSUITES_KEY, "5");
922         properties.put(THREADCOUNTCLASSES_KEY, "15");
923         properties.put(THREADCOUNTMETHODS_KEY, "30");
924         JUnitCoreParameters params = new JUnitCoreParameters( properties );
925         Concurrency concurrency = resolveConcurrency( params, null );
926         assertTrue( params.isParallelSuites() );
927         assertTrue( params.isParallelClasses() );
928         assertTrue( params.isParallelMethods() );
929         assertThat( concurrency.capacity, is( 50 * cpu ) );
930         assertThat( concurrency.suites, is( 5 * cpu ) );
931         assertThat( concurrency.classes, is( 15 * cpu ) );
932         assertThat( concurrency.methods, is( 30 * cpu ) );
933 
934         // Warning: these cases work but they are not enabled in AbstractSurefireMojo
935         // Instead use the 'useUnlimitedThreads' parameter.
936         properties = new HashMap<String, String>();
937         properties.put(PARALLEL_KEY, "all");
938         properties.put(THREADCOUNTSUITES_KEY, "5");
939         properties.put(THREADCOUNTCLASSES_KEY, "15");
940         params = new JUnitCoreParameters( properties );
941         concurrency = resolveConcurrency( params, null );
942         assertTrue( params.isParallelSuites() );
943         assertTrue( params.isParallelClasses() );
944         assertTrue( params.isParallelMethods() );
945         assertThat( concurrency.capacity, is( Integer.MAX_VALUE ) );
946         assertThat( concurrency.suites, is( 5 * cpu ) );
947         assertThat( concurrency.classes, is( 15 * cpu ) );
948         assertThat( concurrency.methods, is( Integer.MAX_VALUE ) );
949 
950         properties = new HashMap<String, String>();
951         properties.put(PARALLEL_KEY, "all");
952         properties.put(THREADCOUNTCLASSES_KEY, "15");
953         params = new JUnitCoreParameters( properties );
954         concurrency = resolveConcurrency( params, null );
955         assertTrue( params.isParallelSuites() );
956         assertTrue( params.isParallelClasses() );
957         assertTrue( params.isParallelMethods() );
958         assertThat( concurrency.capacity, is( Integer.MAX_VALUE ) );
959         assertThat( concurrency.suites, is( Integer.MAX_VALUE ) );
960         assertThat( concurrency.classes, is( 15 * cpu ) );
961         assertThat( concurrency.methods, is( Integer.MAX_VALUE ) );
962     }
963 
964     @Test
965     public void withoutShutdown()
966     {
967         Map<String, String> properties = new HashMap<String, String>();
968         properties.put(PARALLEL_KEY, "methods");
969         properties.put(THREADCOUNTMETHODS_KEY, "2");
970         JUnitCoreParameters params = new JUnitCoreParameters( properties );
971         ParallelComputerBuilder pcBuilder = new ParallelComputerBuilder( logger, params );
972         ParallelComputer pc = pcBuilder.buildComputer();
973         final JUnitCore core = new JUnitCore();
974         final long t1 = systemMillis();
975         final Result result = core.run( pc, TestClass.class );
976         final long t2 = systemMillis();
977         long timeSpent = t2 - t1;
978         final long deltaTime = 500L;
979 
980         assertTrue( result.wasSuccessful() );
981         assertThat( result.getRunCount(), is( 3 ) );
982         assertThat( result.getFailureCount(), is( 0 ) );
983         assertThat( result.getIgnoreCount(), is( 0 ) );
984         //assertThat( timeSpent, between (timeSpent - deltaTime, timeSpent + deltaTime + 2000L ) );
985         assertEquals( 10000L, timeSpent, deltaTime );
986     }
987 
988     @Test
989     public void shutdown()
990         throws TestSetFailedException
991     {
992         // The JUnitCore returns after 2.5s.
993         // The test-methods in TestClass are NOT interrupted, and return normally after 5s.
994         Map<String, String> properties = new HashMap<String, String>();
995         properties.put(PARALLEL_KEY, "methods");
996         properties.put(THREADCOUNTMETHODS_KEY, "2");
997         properties.put(PARALLEL_TIMEOUT_KEY, Double.toString( 2.5d ));
998         JUnitCoreParameters params = new JUnitCoreParameters( properties );
999         ParallelComputerBuilder pcBuilder = new ParallelComputerBuilder( logger, params );
1000         ParallelComputer pc = pcBuilder.buildComputer();
1001         final JUnitCore core = new JUnitCore();
1002         final long t1 = systemMillis();
1003         core.run( pc, TestClass.class );
1004         final long t2 = systemMillis();
1005         final long timeSpent = t2 - t1;
1006         final long deltaTime = 500L;
1007 
1008         assertEquals( 5000L, timeSpent, deltaTime );
1009         String description = pc.describeElapsedTimeout();
1010         assertTrue( description.contains( "The test run has finished abruptly after timeout of 2.5 seconds.") );
1011         assertTrue( description.contains( "These tests were executed in prior to the shutdown operation:\n"
1012                 + TestClass.class.getName() ) );
1013     }
1014 
1015     @Test
1016     public void forcedShutdown()
1017         throws TestSetFailedException
1018     {
1019         // The JUnitCore returns after 2.5s, and the test-methods in TestClass are interrupted.
1020         Map<String, String> properties = new HashMap<String, String>();
1021         properties.put(PARALLEL_KEY, "methods");
1022         properties.put(THREADCOUNTMETHODS_KEY, "2");
1023         properties.put(PARALLEL_TIMEOUTFORCED_KEY, Double.toString( 2.5d ));
1024         JUnitCoreParameters params = new JUnitCoreParameters( properties );
1025         ParallelComputerBuilder pcBuilder = new ParallelComputerBuilder( logger, params );
1026         ParallelComputer pc = pcBuilder.buildComputer();
1027         final JUnitCore core = new JUnitCore();
1028         final long t1 = systemMillis();
1029         core.run( pc, TestClass.class );
1030         final long t2 = systemMillis();
1031         final long timeSpent = t2 - t1;
1032         final long deltaTime = 500L;
1033 
1034         assertEquals( 2500L, timeSpent, deltaTime );
1035         String description = pc.describeElapsedTimeout();
1036         assertTrue( description.contains( "The test run has finished abruptly after timeout of 2.5 seconds.") );
1037         assertTrue( description.contains( "These tests were executed in prior to the shutdown operation:\n"
1038                 + TestClass.class.getName() ) );
1039     }
1040 
1041     @Test
1042     public void timeoutAndForcedShutdown()
1043         throws TestSetFailedException
1044     {
1045         // The JUnitCore returns after 3.5s and the test-methods in TestClass are timed out after 2.5s.
1046         // No new test methods are scheduled for execution after 2.5s.
1047         // Interruption of test methods after 3.5s.
1048         Map<String, String> properties = new HashMap<String, String>();
1049         properties.put(PARALLEL_KEY, "methods");
1050         properties.put(THREADCOUNTMETHODS_KEY, "2");
1051         properties.put(PARALLEL_TIMEOUT_KEY, Double.toString( 2.5d ));
1052         properties.put(PARALLEL_TIMEOUTFORCED_KEY, Double.toString( 3.5d ));
1053         JUnitCoreParameters params = new JUnitCoreParameters( properties );
1054         ParallelComputerBuilder pcBuilder = new ParallelComputerBuilder( logger, params );
1055         ParallelComputer pc = pcBuilder.buildComputer();
1056         final JUnitCore core = new JUnitCore();
1057         final long t1 = systemMillis();
1058         core.run( pc, TestClass.class );
1059         final long t2 = systemMillis();
1060         final long timeSpent = t2 - t1;
1061         final long deltaTime = 500L;
1062 
1063         assertEquals( 3500L, timeSpent, deltaTime );
1064         String description = pc.describeElapsedTimeout();
1065         assertTrue( description.contains( "The test run has finished abruptly after timeout of 2.5 seconds.") );
1066         assertTrue( description.contains( "These tests were executed in prior to the shutdown operation:\n"
1067                                               + TestClass.class.getName() ) );
1068     }
1069 
1070     @Test
1071     public void forcedTimeoutAndShutdown()
1072         throws Exception
1073     {
1074         // The JUnitCore returns after 3.5s and the test-methods in TestClass are interrupted after 3.5s.
1075         Map<String, String> properties = new HashMap<String, String>();
1076         properties.put(PARALLEL_KEY, "methods");
1077         properties.put(THREADCOUNTMETHODS_KEY, "2");
1078         properties.put(PARALLEL_TIMEOUTFORCED_KEY, Double.toString( 3.5d ) );
1079         properties.put(PARALLEL_TIMEOUT_KEY, Double.toString( 4.0d ) );
1080         JUnitCoreParameters params = new JUnitCoreParameters( properties );
1081         ParallelComputerBuilder pcBuilder = new ParallelComputerBuilder( logger, params );
1082         ParallelComputer pc = pcBuilder.buildComputer();
1083         final JUnitCore core = new JUnitCore();
1084         final long t1 = systemMillis();
1085         core.run( pc, TestClass.class );
1086         final long t2 = systemMillis();
1087         final long timeSpent = t2 - t1;
1088         final long deltaTime = 500L;
1089 
1090         assertEquals( 3500L, timeSpent, deltaTime );
1091         String description = pc.describeElapsedTimeout();
1092         assertTrue( description.contains( "The test run has finished abruptly after timeout of 3.5 seconds.") );
1093         assertTrue( description.contains( "These tests were executed in prior to the shutdown operation:\n"
1094                                               + TestClass.class.getName() ) );
1095     }
1096 
1097     public static class TestClass
1098     {
1099         @Test
1100         public void a()
1101             throws InterruptedException
1102         {
1103             long t1 = systemMillis();
1104             try
1105             {
1106                 Thread.sleep( 5000L );
1107             }
1108             finally
1109             {
1110                 System.out.println( getClass().getSimpleName() + "#a() spent " + ( systemMillis() - t1 ) );
1111             }
1112         }
1113 
1114         @Test
1115         public void b()
1116             throws InterruptedException
1117         {
1118             long t1 = systemMillis();
1119             try
1120             {
1121                 Thread.sleep( 5000L );
1122             }
1123             finally
1124             {
1125                 System.out.println( getClass().getSimpleName() + "#b() spent " + ( systemMillis() - t1 ) );
1126             }
1127         }
1128 
1129         @Test
1130         public void c()
1131             throws InterruptedException
1132         {
1133             long t1 = systemMillis();
1134             try
1135             {
1136                 Thread.sleep( 5000L );
1137             }
1138             finally
1139             {
1140                 System.out.println( getClass().getSimpleName() + "#c() spent " + ( systemMillis() - t1 ) );
1141             }
1142         }
1143     }
1144 
1145     private static long systemMillis()
1146     {
1147         return TimeUnit.NANOSECONDS.toMillis( System.nanoTime() );
1148     }
1149 }