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.Logger;
23  import org.junit.Test;
24  
25  import java.util.concurrent.ExecutorService;
26  import java.util.concurrent.Executors;
27  
28  import static org.junit.Assert.assertFalse;
29  import static org.junit.Assert.assertTrue;
30  
31  /**
32   * Tests the factories in SchedulingStrategy.
33   * <p/>
34   * Th point of these tests is to check {@link Task#result} if changed
35   * from <code>false</code> to <code>true</code> after all scheduled tasks
36   * have finished.
37   * The call {@link SchedulingStrategy#finished()} is waiting until the
38   * strategy has finished.
39   * Then {@link Task#result} should be asserted that is <code>true</code>.
40   *
41   * @author Tibor Digana (tibor17)
42   * @see SchedulingStrategy
43   * @since 2.16
44   */
45  public class SchedulingStrategiesTest
46  {
47      @Test
48      public void invokerStrategy()
49          throws InterruptedException
50      {
51          SchedulingStrategy strategy = SchedulingStrategies.createInvokerStrategy( new Logger() );
52          assertFalse( strategy.hasSharedThreadPool() );
53          assertTrue( strategy.canSchedule() );
54  
55          Task task = new Task();
56  
57          strategy.schedule( task );
58  
59          assertTrue( strategy.canSchedule() );
60  
61          assertTrue( task.result );
62  
63          assertTrue( strategy.finished() );
64          assertFalse( strategy.canSchedule() );
65      }
66  
67      @Test
68      public void nonSharedPoolStrategy()
69          throws InterruptedException
70      {
71          SchedulingStrategy strategy = SchedulingStrategies.createParallelStrategy( new Logger(),  2 );
72          assertFalse( strategy.hasSharedThreadPool() );
73          assertTrue( strategy.canSchedule() );
74  
75          Task task1 = new Task();
76          Task task2 = new Task();
77  
78          strategy.schedule( task1 );
79          strategy.schedule( task2 );
80  
81          assertTrue( strategy.canSchedule() );
82  
83          assertTrue( strategy.finished() );
84          assertFalse( strategy.canSchedule() );
85  
86          assertTrue( task1.result );
87          assertTrue( task2.result );
88      }
89  
90      @Test(expected = NullPointerException.class)
91      public void sharedPoolStrategyNullPool()
92      {
93          SchedulingStrategies.createParallelSharedStrategy( new Logger(), null );
94      }
95  
96      @Test
97      public void sharedPoolStrategy()
98          throws InterruptedException
99      {
100         ExecutorService sharedPool = Executors.newCachedThreadPool();
101 
102         SchedulingStrategy strategy1 = SchedulingStrategies.createParallelSharedStrategy( new Logger(), sharedPool );
103         assertTrue( strategy1.hasSharedThreadPool() );
104         assertTrue( strategy1.canSchedule() );
105 
106         SchedulingStrategy strategy2 = SchedulingStrategies.createParallelSharedStrategy( new Logger(), sharedPool );
107         assertTrue( strategy2.hasSharedThreadPool() );
108         assertTrue( strategy2.canSchedule() );
109 
110         Task task1 = new Task();
111         Task task2 = new Task();
112         Task task3 = new Task();
113         Task task4 = new Task();
114 
115         strategy1.schedule( task1 );
116         strategy2.schedule( task2 );
117         strategy1.schedule( task3 );
118         strategy2.schedule( task4 );
119 
120         assertTrue( strategy1.canSchedule() );
121         assertTrue( strategy2.canSchedule() );
122 
123         assertTrue( strategy1.finished() );
124         assertFalse( strategy1.canSchedule() );
125 
126         assertTrue( strategy2.finished() );
127         assertFalse( strategy2.canSchedule() );
128 
129         assertTrue( task1.result );
130         assertTrue( task2.result );
131         assertTrue( task3.result );
132         assertTrue( task4.result );
133     }
134 
135     @Test
136     public void infinitePoolStrategy()
137         throws InterruptedException
138     {
139         SchedulingStrategy strategy = SchedulingStrategies.createParallelStrategyUnbounded( new Logger() );
140         assertFalse( strategy.hasSharedThreadPool() );
141         assertTrue( strategy.canSchedule() );
142 
143         Task task1 = new Task();
144         Task task2 = new Task();
145 
146         strategy.schedule( task1 );
147         strategy.schedule( task2 );
148 
149         assertTrue( strategy.canSchedule() );
150 
151         assertTrue( strategy.finished() );
152         assertFalse( strategy.canSchedule() );
153 
154         assertTrue( task1.result );
155         assertTrue( task2.result );
156     }
157 
158     static class Task
159         implements Runnable
160     {
161         volatile boolean result = false;
162 
163         public void run()
164         {
165             result = true;
166         }
167     }
168 }