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