1 |
jsr166 |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea and Martin Buchholz with assistance from members |
3 |
|
|
* of JCP JSR-166 Expert Group and released to the public domain, as |
4 |
jsr166 |
1.5 |
* explained at http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.1 |
* |
6 |
|
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
7 |
|
|
* Pat Fisher, Mike Judd. |
8 |
|
|
*/ |
9 |
|
|
|
10 |
|
|
import junit.framework.*; |
11 |
|
|
import java.util.*; |
12 |
|
|
import java.util.concurrent.*; |
13 |
|
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
|
|
|
15 |
|
|
/** |
16 |
|
|
* Contains tests generally applicable to BlockingQueue implementations. |
17 |
|
|
*/ |
18 |
|
|
public abstract class BlockingQueueTest extends JSR166TestCase { |
19 |
|
|
/* |
20 |
|
|
* This is the start of an attempt to refactor the tests for the |
21 |
|
|
* various related implementations of related interfaces without |
22 |
|
|
* too much duplicated code. junit does not really support such |
23 |
|
|
* testing. Here subclasses of TestCase not only contain tests, |
24 |
|
|
* but also configuration information that describes the |
25 |
|
|
* implementation class, most importantly how to instantiate |
26 |
|
|
* instances. |
27 |
|
|
*/ |
28 |
|
|
|
29 |
|
|
/** Like suite(), but non-static */ |
30 |
|
|
public Test testSuite() { |
31 |
|
|
// TODO: filter the returned tests using the configuration |
32 |
|
|
// information provided by the subclass via protected methods. |
33 |
|
|
return new TestSuite(this.getClass()); |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
/** Returns an empty instance of the implementation class. */ |
37 |
|
|
protected abstract BlockingQueue emptyCollection(); |
38 |
|
|
|
39 |
|
|
/** |
40 |
|
|
* timed poll before a delayed offer fails; after offer succeeds; |
41 |
|
|
* on interruption throws |
42 |
|
|
*/ |
43 |
|
|
public void testTimedPollWithOffer() throws InterruptedException { |
44 |
|
|
final BlockingQueue q = emptyCollection(); |
45 |
|
|
final CheckedBarrier barrier = new CheckedBarrier(2); |
46 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
47 |
|
|
public void realRun() throws InterruptedException { |
48 |
|
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
49 |
|
|
|
50 |
|
|
barrier.await(); |
51 |
|
|
assertSame(zero, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
52 |
|
|
|
53 |
|
|
Thread.currentThread().interrupt(); |
54 |
|
|
try { |
55 |
|
|
q.poll(SHORT_DELAY_MS, MILLISECONDS); |
56 |
|
|
shouldThrow(); |
57 |
|
|
} catch (InterruptedException success) {} |
58 |
|
|
|
59 |
|
|
barrier.await(); |
60 |
|
|
try { |
61 |
|
|
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
62 |
|
|
shouldThrow(); |
63 |
|
|
} catch (InterruptedException success) {} |
64 |
|
|
}}); |
65 |
|
|
|
66 |
|
|
barrier.await(); |
67 |
|
|
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
68 |
|
|
barrier.await(); |
69 |
|
|
sleep(SHORT_DELAY_MS); |
70 |
|
|
t.interrupt(); |
71 |
|
|
awaitTermination(t, MEDIUM_DELAY_MS); |
72 |
|
|
} |
73 |
|
|
|
74 |
jsr166 |
1.2 |
/** |
75 |
jsr166 |
1.3 |
* take() blocks interruptibly when empty |
76 |
jsr166 |
1.2 |
*/ |
77 |
|
|
public void testTakeFromEmptyBlocksInterruptibly() |
78 |
|
|
throws InterruptedException { |
79 |
|
|
final BlockingQueue q = emptyCollection(); |
80 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
81 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
82 |
|
|
public void realRun() { |
83 |
|
|
long t0 = System.nanoTime(); |
84 |
|
|
threadStarted.countDown(); |
85 |
|
|
try { |
86 |
|
|
q.take(); |
87 |
|
|
shouldThrow(); |
88 |
|
|
} catch (InterruptedException success) {} |
89 |
|
|
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
90 |
|
|
}}); |
91 |
jsr166 |
1.3 |
|
92 |
jsr166 |
1.2 |
threadStarted.await(); |
93 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
94 |
|
|
assertTrue(t.isAlive()); |
95 |
|
|
t.interrupt(); |
96 |
|
|
awaitTermination(t, MEDIUM_DELAY_MS); |
97 |
jsr166 |
1.3 |
} |
98 |
|
|
|
99 |
|
|
/** |
100 |
|
|
* take() throws InterruptedException immediately if interrupted |
101 |
|
|
* before waiting |
102 |
|
|
*/ |
103 |
|
|
public void testTakeFromEmptyAfterInterrupt() |
104 |
|
|
throws InterruptedException { |
105 |
|
|
final BlockingQueue q = emptyCollection(); |
106 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
107 |
|
|
public void realRun() { |
108 |
|
|
long t0 = System.nanoTime(); |
109 |
|
|
Thread.currentThread().interrupt(); |
110 |
|
|
try { |
111 |
|
|
q.take(); |
112 |
|
|
shouldThrow(); |
113 |
|
|
} catch (InterruptedException success) {} |
114 |
jsr166 |
1.4 |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
115 |
jsr166 |
1.3 |
}}); |
116 |
|
|
|
117 |
|
|
awaitTermination(t, MEDIUM_DELAY_MS); |
118 |
jsr166 |
1.2 |
} |
119 |
|
|
|
120 |
jsr166 |
1.1 |
/** For debugging. */ |
121 |
|
|
public void XXXXtestFails() { |
122 |
|
|
fail(emptyCollection().getClass().toString()); |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
} |