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 |
|
|
* explained at http://creativecommons.org/licenses/publicdomain |
5 |
|
|
* |
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 |
|
|
/** For debugging. */ |
75 |
|
|
public void XXXXtestFails() { |
76 |
|
|
fail(emptyCollection().getClass().toString()); |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
} |