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