1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.maven.surefire.junitcore;
21
22 import junit.framework.TestCase;
23 import org.junit.Ignore;
24 import org.junit.Test;
25 import org.junit.experimental.ParallelComputer;
26 import org.junit.runner.Computer;
27 import org.junit.runner.JUnitCore;
28 import org.junit.runner.Result;
29 import org.junit.runner.notification.RunListener;
30
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.concurrent.ExecutionException;
34
35
36
37
38
39
40 public class ConfigurableParallelComputerTest
41 extends TestCase
42 {
43 private static final int NUMTESTS = 1000;
44
45
46
47 @Test
48 public void testAnythingYouWantToPlayWith()
49 throws Exception
50 {
51 Result result = new Result();
52 Class[] realClasses = new Class[]{ Dummy.class, Dummy2.class };
53
54 DiagnosticRunListener diagnosticRunListener = new DiagnosticRunListener( false, result.createListener() );
55 JUnitCore jUnitCore = getJunitCore( diagnosticRunListener );
56 ConfigurableParallelComputer computer = new ConfigurableParallelComputer( true, false );
57 jUnitCore.run( computer, realClasses );
58 computer.close();
59 assertEquals( "All tests should succeed, right ?", 5, result.getRunCount() );
60 }
61
62 @Test
63 public void testOneMethod()
64 throws ExecutionException
65 {
66 JUnitCore jUnitCore = new JUnitCore();
67 ConfigurableParallelComputer computer = new ConfigurableParallelComputer( false, true );
68 jUnitCore.run( computer, new Class[]{ Dummy.class, Dummy.class, Dummy.class } );
69 computer.close();
70 }
71
72 @Test
73 public void testSerial()
74 throws Exception
75 {
76 Result result = new Result();
77 Class[] realClasses = getClassList();
78 JUnitCore jUnitCore = getJunitCore( result );
79 Computer computer = new Computer();
80 timedRun( NUMTESTS, result, realClasses, jUnitCore, computer );
81 }
82
83
84 @Test
85 public void testFullTestRunPC()
86 throws Exception
87 {
88 Result result = new Result();
89 Class[] realClasses = getClassList();
90 JUnitCore jUnitCore = getJunitCore( result );
91 Computer computer = new ConfigurableParallelComputer( true, true );
92 timedRun( NUMTESTS, result, realClasses, jUnitCore, computer );
93 }
94
95 @Test
96 public void testWithFailingAssertionCPC()
97 throws Exception
98 {
99 runWithFailingAssertion( new ConfigurableParallelComputer( false, true, 6, true ) );
100 runWithFailingAssertion( new ConfigurableParallelComputer( true, false, 12, false ) );
101 runWithFailingAssertion( new ConfigurableParallelComputer( true, true, 2, false ) );
102 }
103
104 @Test
105 public void testWithSlowTestJustAfew()
106 throws Exception
107 {
108 Result result = new Result();
109 final Computer computer = new ConfigurableParallelComputer( false, true, 3, false );
110 Class[] realClasses = getClassList( SlowTest.class, 5 );
111
112 JUnitCore jUnitCore = getJunitCore( result );
113 runIt( realClasses, jUnitCore, computer );
114 }
115
116
117 private void runWithFailingAssertion( Computer computer )
118 throws ExecutionException
119 {
120 Result result = new Result();
121 Class[] realClasses = getClassList( FailingAssertions.class );
122 JUnitCore jUnitCore = getJunitCore( result );
123 runIt( realClasses, jUnitCore, computer );
124 assertEquals( "No tests should fail, right ?", NUMTESTS, result.getFailures().size() );
125 assertEquals( "All tests should succeed, right ?", 0, result.getIgnoreCount() );
126 assertEquals( "All tests should succeed, right ?", NUMTESTS * 3, result.getRunCount() );
127 }
128
129 @Test
130 public void testWithFailure()
131 throws Exception
132 {
133 Computer computer = new ConfigurableParallelComputer( false, true, 4, true );
134 Result result = new Result();
135 Class[] realClasses = getClassList( Failure.class );
136 JUnitCore jUnitCore = getJunitCore( result );
137 runIt( realClasses, jUnitCore, computer );
138 assertEquals( "No tests should fail, right ?", NUMTESTS, result.getFailures().size() );
139 assertEquals( "All tests should succeed, right ?", 0, result.getIgnoreCount() );
140 assertEquals( "All tests should succeed, right ?", NUMTESTS * 3, result.getRunCount() );
141 }
142
143 @Test
144 public void testFixedThreadPool()
145 throws Exception
146 {
147 Result result = new Result();
148 Class[] realClasses = getClassList();
149 JUnitCore jUnitCore = getJunitCore( result );
150 ConfigurableParallelComputer computer = new ConfigurableParallelComputer( false, true, 2, false );
151 long resp = timedRun( NUMTESTS, result, realClasses, jUnitCore, computer );
152 }
153
154 @Test
155 public void testClassesUnlimited()
156 throws Exception
157 {
158 Result result = new Result();
159 Class[] realClasses = getClassList();
160 JUnitCore jUnitCore = getJunitCore( result );
161 ConfigurableParallelComputer computer = new ConfigurableParallelComputer( true, false );
162 timedRun( NUMTESTS, result, realClasses, jUnitCore, computer );
163 }
164
165 @Test
166 public void testBothUnlimited()
167 throws Exception
168 {
169 Result result = new Result();
170 Class[] realClasses = getClassList();
171 DiagnosticRunListener diagnosticRunListener = new DiagnosticRunListener( false, result.createListener() );
172 JUnitCore jUnitCore = getJunitCore( diagnosticRunListener );
173 ConfigurableParallelComputer computer = new ConfigurableParallelComputer( true, true );
174 timedRun( NUMTESTS, result, realClasses, jUnitCore, computer );
175 }
176
177 private JUnitCore getJunitCore( Result result )
178 {
179 RunListener listener = result.createListener();
180 JUnitCore jUnitCore = new JUnitCore();
181 jUnitCore.addListener( listener );
182 return jUnitCore;
183 }
184
185 private JUnitCore getJunitCore( RunListener listener )
186 {
187 JUnitCore jUnitCore = new JUnitCore();
188 jUnitCore.addListener( listener );
189 return jUnitCore;
190 }
191
192 private long runIt( Class[] realClasses, JUnitCore jUnitCore, Computer computer )
193 throws ExecutionException
194 {
195 long start = System.currentTimeMillis();
196 jUnitCore.run( computer, realClasses );
197 if ( computer instanceof ConfigurableParallelComputer )
198 {
199 ( (ConfigurableParallelComputer) computer ).close();
200 }
201 long rsult = System.currentTimeMillis() - start;
202 return rsult;
203 }
204
205 private long timedRun( int NUMTESTS, Result result, Class[] realClasses, JUnitCore jUnitCore, Computer computer )
206 throws ExecutionException
207 {
208 long time = runIt( realClasses, jUnitCore, computer );
209 assertEquals( "No tests should fail, right ?", 0, result.getFailures().size() );
210 assertEquals( "All tests should succeed, right ?", 0, result.getIgnoreCount() );
211 assertEquals( "All tests should succeed, right ?", NUMTESTS * 3, result.getRunCount() );
212 return time;
213 }
214
215 private Class[] getClassList()
216 {
217 return getClassList( Dummy.class, NUMTESTS );
218 }
219
220 private Class[] getClassList( Class testClass )
221 {
222 return getClassList( testClass, NUMTESTS );
223 }
224
225 private Class[] getClassList( Class testClass, int numItems )
226 {
227 List<Class> realClasses = new ArrayList<Class>();
228 for ( int i = 0; i < numItems; i++ )
229 {
230 realClasses.add( testClass );
231 }
232 return realClasses.toArray( new Class[realClasses.size()] );
233 }
234
235 static void sleepReallyEvenOnWindows(long ms)
236 throws InterruptedException
237 {
238 long endAt = System.currentTimeMillis() + ms;
239 Thread.sleep( ms);
240 while ( endAt > System.currentTimeMillis()){
241 Thread.sleep( ms/10);
242 Thread.yield();
243 }
244
245 }
246 public static class Dummy
247 {
248 @Test
249 public void testNotMuch()
250 {
251 }
252
253 @Test
254 public void testStub1()
255 {
256
257 }
258
259 @Test
260 public void testStub2()
261 {
262
263 }
264 }
265
266 public static class Dummy2
267 {
268 @Test
269 public void testNotMuch()
270 {
271
272 }
273
274 @Test
275 public void testDummy2()
276 {
277
278 }
279 }
280
281 public static class SlowTest
282 {
283 final int scaling = 100;
284
285 @Test
286 public void testNotMuch()
287 throws InterruptedException
288 {
289 sleepReallyEvenOnWindows( scaling );
290 }
291
292 @Test
293 public void testNotMuch2()
294 throws InterruptedException
295 {
296 sleepReallyEvenOnWindows( 3 * scaling );
297 }
298
299 @Test
300 public void testNotMuch3()
301 throws InterruptedException
302 {
303 sleepReallyEvenOnWindows( 2 * scaling );
304 }
305
306
307 }
308
309 public static class FailingAssertions
310 {
311 @Test
312 public void testNotMuch()
313 {
314 }
315
316 @Test
317 public void testNotMuch2()
318 {
319 }
320
321 @Test
322 public void testWithFail()
323 {
324 fail( "We excpect this" );
325 }
326 }
327
328 public static class Failure
329 {
330 @Test
331 public void testNotMuch()
332 {
333 }
334
335 @Test
336 public void testNotMuch2()
337 {
338 }
339
340
341 @Test
342 public void testWithException()
343 {
344 throw new RuntimeException( "We expect this" );
345 }
346
347
348 }
349 }