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