|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| package org.codehaus.prometheus.testsupport; |
|
7 |
| |
|
8 |
| import junit.framework.TestCase; |
|
9 |
| |
|
10 |
| import java.util.concurrent.TimeUnit; |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| public abstract class ConcurrentTestCase extends TestCase { |
|
18 |
| |
|
19 |
| public static final boolean START_INTERRUPTED = true; |
|
20 |
| public static final boolean START_UNINTERRUPTED = false; |
|
21 |
| |
|
22 |
| public static long DELAY_TINY_MS = 50; |
|
23 |
| public static long DELAY_SMALL_MS = DELAY_TINY_MS * 5; |
|
24 |
| public static long DELAY_MEDIUM_MS = DELAY_TINY_MS * 10; |
|
25 |
| public static long DELAY_LONG_MS = DELAY_TINY_MS * 50; |
|
26 |
| public static long DELAY_EON_MS = 100000000000L; |
|
27 |
| |
|
28 |
| public volatile Stopwatch stopwatch; |
|
29 |
| |
|
30 |
461
| public ConcurrentTestCase() {
|
|
31 |
| } |
|
32 |
| |
|
33 |
0
| public ConcurrentTestCase(String fixture) {
|
|
34 |
0
| super(fixture);
|
|
35 |
| } |
|
36 |
| |
|
37 |
461
| @Override
|
|
38 |
| public void setUp() throws Exception { |
|
39 |
461
| stopwatch = new Stopwatch();
|
|
40 |
| } |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
864
| public void joinAll(Thread... threads) {
|
|
53 |
864
| joinAll(2 * DELAY_LONG_MS,threads);
|
|
54 |
| } |
|
55 |
| |
|
56 |
864
| public void joinAll(long delayMs, Thread... threads) {
|
|
57 |
864
| for (int k = 0; k < threads.length; k++) {
|
|
58 |
920
| Thread t = threads[k];
|
|
59 |
920
| try {
|
|
60 |
920
| long startNs = System.nanoTime();
|
|
61 |
920
| t.join(delayMs);
|
|
62 |
920
| long endNs = System.nanoTime();
|
|
63 |
920
| delayMs -= TimeUnit.NANOSECONDS.toMillis(endNs - startNs);
|
|
64 |
| |
|
65 |
920
| if (t.isAlive()) {
|
|
66 |
0
| fail(String.format("thread #%s is still running", k));
|
|
67 |
| } |
|
68 |
| } catch (InterruptedException e) { |
|
69 |
0
| fail(String.format("join on thread #%s was interrupted", k));
|
|
70 |
| } |
|
71 |
| } |
|
72 |
| } |
|
73 |
| } |
|
74 |
| |