ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/BlockingQueueTest.java
Revision: 1.7
Committed: Fri May 27 19:32:04 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.6: +65 -21 lines
Log Message:
add testTimedPollFromEmpty tests

File Contents

# Content
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/publicdomain/zero/1.0/
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 times out; 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 long startTime = System.nanoTime();
49 assertNull(q.poll(timeoutMillis(), MILLISECONDS));
50 assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
51
52 barrier.await();
53
54 assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
55
56 Thread.currentThread().interrupt();
57 try {
58 q.poll(LONG_DELAY_MS, MILLISECONDS);
59 shouldThrow();
60 } catch (InterruptedException success) {}
61 assertFalse(Thread.interrupted());
62
63 barrier.await();
64 try {
65 q.poll(LONG_DELAY_MS, MILLISECONDS);
66 shouldThrow();
67 } catch (InterruptedException success) {}
68 assertFalse(Thread.interrupted());
69 }});
70
71 barrier.await();
72 long startTime = System.nanoTime();
73 assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS));
74 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
75
76 barrier.await();
77 assertThreadStaysAlive(t);
78 t.interrupt();
79 awaitTermination(t);
80 }
81
82 /**
83 * take() blocks interruptibly when empty
84 */
85 public void testTakeFromEmptyBlocksInterruptibly() {
86 final BlockingQueue q = emptyCollection();
87 final CountDownLatch threadStarted = new CountDownLatch(1);
88 Thread t = newStartedThread(new CheckedRunnable() {
89 public void realRun() {
90 threadStarted.countDown();
91 try {
92 q.take();
93 shouldThrow();
94 } catch (InterruptedException success) {}
95 assertFalse(Thread.interrupted());
96 }});
97
98 await(threadStarted);
99 assertThreadStaysAlive(t);
100 t.interrupt();
101 awaitTermination(t);
102 }
103
104 /**
105 * take() throws InterruptedException immediately if interrupted
106 * before waiting
107 */
108 public void testTakeFromEmptyAfterInterrupt() {
109 final BlockingQueue q = emptyCollection();
110 Thread t = newStartedThread(new CheckedRunnable() {
111 public void realRun() {
112 Thread.currentThread().interrupt();
113 try {
114 q.take();
115 shouldThrow();
116 } catch (InterruptedException success) {}
117 assertFalse(Thread.interrupted());
118 }});
119
120 awaitTermination(t);
121 }
122
123 /**
124 * timed poll() blocks interruptibly when empty
125 */
126 public void testTimedPollFromEmptyBlocksInterruptibly() {
127 final BlockingQueue q = emptyCollection();
128 final CountDownLatch threadStarted = new CountDownLatch(1);
129 Thread t = newStartedThread(new CheckedRunnable() {
130 public void realRun() {
131 threadStarted.countDown();
132 try {
133 q.poll(2 * LONG_DELAY_MS, MILLISECONDS);
134 shouldThrow();
135 } catch (InterruptedException success) {}
136 assertFalse(Thread.interrupted());
137 }});
138
139 await(threadStarted);
140 assertThreadStaysAlive(t);
141 t.interrupt();
142 awaitTermination(t);
143 }
144
145 /**
146 * timed poll() throws InterruptedException immediately if
147 * interrupted before waiting
148 */
149 public void testTimedPollFromEmptyAfterInterrupt() {
150 final BlockingQueue q = emptyCollection();
151 Thread t = newStartedThread(new CheckedRunnable() {
152 public void realRun() {
153 Thread.currentThread().interrupt();
154 try {
155 q.poll(2 * LONG_DELAY_MS, MILLISECONDS);
156 shouldThrow();
157 } catch (InterruptedException success) {}
158 assertFalse(Thread.interrupted());
159 }});
160
161 awaitTermination(t);
162 }
163
164 /** For debugging. */
165 public void XXXXtestFails() {
166 fail(emptyCollection().getClass().toString());
167 }
168
169 }