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