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