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