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