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 static java.util.concurrent.TimeUnit.MILLISECONDS; |
11 |
|
12 |
import java.util.ArrayList; |
13 |
import java.util.Arrays; |
14 |
import java.util.Collection; |
15 |
import java.util.Queue; |
16 |
import java.util.concurrent.BlockingQueue; |
17 |
import java.util.concurrent.CountDownLatch; |
18 |
|
19 |
import junit.framework.Test; |
20 |
import junit.framework.TestSuite; |
21 |
|
22 |
/** |
23 |
* Contains "contract" tests applicable to all BlockingQueue implementations. |
24 |
*/ |
25 |
public abstract class BlockingQueueTest extends JSR166TestCase { |
26 |
/* |
27 |
* This is the start of an attempt to refactor the tests for the |
28 |
* various related implementations of related interfaces without |
29 |
* too much duplicated code. junit does not really support such |
30 |
* testing. Here subclasses of TestCase not only contain tests, |
31 |
* but also configuration information that describes the |
32 |
* implementation class, most importantly how to instantiate |
33 |
* instances. |
34 |
*/ |
35 |
|
36 |
/** Like suite(), but non-static */ |
37 |
public Test testSuite() { |
38 |
// TODO: filter the returned tests using the configuration |
39 |
// information provided by the subclass via protected methods. |
40 |
return new TestSuite(this.getClass()); |
41 |
} |
42 |
|
43 |
//---------------------------------------------------------------- |
44 |
// Configuration methods |
45 |
//---------------------------------------------------------------- |
46 |
|
47 |
/** Returns an empty instance of the implementation class. */ |
48 |
protected abstract BlockingQueue emptyCollection(); |
49 |
|
50 |
/** |
51 |
* Returns an element suitable for insertion in the collection. |
52 |
* Override for collections with unusual element types. |
53 |
*/ |
54 |
protected Object makeElement(int i) { |
55 |
return Integer.valueOf(i); |
56 |
} |
57 |
|
58 |
//---------------------------------------------------------------- |
59 |
// Tests |
60 |
//---------------------------------------------------------------- |
61 |
|
62 |
/** |
63 |
* offer(null) throws NullPointerException |
64 |
*/ |
65 |
public void testOfferNull() { |
66 |
final Queue q = emptyCollection(); |
67 |
try { |
68 |
q.offer(null); |
69 |
shouldThrow(); |
70 |
} catch (NullPointerException success) {} |
71 |
} |
72 |
|
73 |
/** |
74 |
* add(null) throws NullPointerException |
75 |
*/ |
76 |
public void testAddNull() { |
77 |
final Collection q = emptyCollection(); |
78 |
try { |
79 |
q.add(null); |
80 |
shouldThrow(); |
81 |
} catch (NullPointerException success) {} |
82 |
} |
83 |
|
84 |
/** |
85 |
* timed offer(null) throws NullPointerException |
86 |
*/ |
87 |
public void testTimedOfferNull() throws InterruptedException { |
88 |
final BlockingQueue q = emptyCollection(); |
89 |
long startTime = System.nanoTime(); |
90 |
try { |
91 |
q.offer(null, LONG_DELAY_MS, MILLISECONDS); |
92 |
shouldThrow(); |
93 |
} catch (NullPointerException success) {} |
94 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
95 |
} |
96 |
|
97 |
/** |
98 |
* put(null) throws NullPointerException |
99 |
*/ |
100 |
public void testPutNull() throws InterruptedException { |
101 |
final BlockingQueue q = emptyCollection(); |
102 |
try { |
103 |
q.put(null); |
104 |
shouldThrow(); |
105 |
} catch (NullPointerException success) {} |
106 |
} |
107 |
|
108 |
/** |
109 |
* addAll(null) throws NullPointerException |
110 |
*/ |
111 |
public void testAddAllNull() throws InterruptedException { |
112 |
final Collection q = emptyCollection(); |
113 |
try { |
114 |
q.addAll(null); |
115 |
shouldThrow(); |
116 |
} catch (NullPointerException success) {} |
117 |
} |
118 |
|
119 |
/** |
120 |
* addAll of a collection with null elements throws NullPointerException |
121 |
*/ |
122 |
public void testAddAllNullElements() { |
123 |
final Collection q = emptyCollection(); |
124 |
final Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
125 |
try { |
126 |
q.addAll(elements); |
127 |
shouldThrow(); |
128 |
} catch (NullPointerException success) {} |
129 |
} |
130 |
|
131 |
/** |
132 |
* toArray(null) throws NullPointerException |
133 |
*/ |
134 |
public void testToArray_NullArray() { |
135 |
final Collection q = emptyCollection(); |
136 |
try { |
137 |
q.toArray((Object[])null); |
138 |
shouldThrow(); |
139 |
} catch (NullPointerException success) {} |
140 |
} |
141 |
|
142 |
/** |
143 |
* drainTo(null) throws NullPointerException |
144 |
*/ |
145 |
public void testDrainToNull() { |
146 |
final BlockingQueue q = emptyCollection(); |
147 |
try { |
148 |
q.drainTo(null); |
149 |
shouldThrow(); |
150 |
} catch (NullPointerException success) {} |
151 |
} |
152 |
|
153 |
/** |
154 |
* drainTo(this) throws IllegalArgumentException |
155 |
*/ |
156 |
public void testDrainToSelf() { |
157 |
final BlockingQueue q = emptyCollection(); |
158 |
try { |
159 |
q.drainTo(q); |
160 |
shouldThrow(); |
161 |
} catch (IllegalArgumentException success) {} |
162 |
} |
163 |
|
164 |
/** |
165 |
* drainTo(null, n) throws NullPointerException |
166 |
*/ |
167 |
public void testDrainToNullN() { |
168 |
final BlockingQueue q = emptyCollection(); |
169 |
try { |
170 |
q.drainTo(null, 0); |
171 |
shouldThrow(); |
172 |
} catch (NullPointerException success) {} |
173 |
} |
174 |
|
175 |
/** |
176 |
* drainTo(this, n) throws IllegalArgumentException |
177 |
*/ |
178 |
public void testDrainToSelfN() { |
179 |
final BlockingQueue q = emptyCollection(); |
180 |
try { |
181 |
q.drainTo(q, 0); |
182 |
shouldThrow(); |
183 |
} catch (IllegalArgumentException success) {} |
184 |
} |
185 |
|
186 |
/** |
187 |
* drainTo(c, n) returns 0 and does nothing when n <= 0 |
188 |
*/ |
189 |
public void testDrainToNonPositiveMaxElements() { |
190 |
final BlockingQueue q = emptyCollection(); |
191 |
final int[] ns = { 0, -1, -42, Integer.MIN_VALUE }; |
192 |
final ArrayList sink = new ArrayList(); |
193 |
for (int n : ns) { |
194 |
assertEquals(0, q.drainTo(sink, n)); |
195 |
assertTrue(sink.isEmpty()); |
196 |
} |
197 |
if (q.remainingCapacity() > 0) { |
198 |
// Not SynchronousQueue, that is |
199 |
Object one = makeElement(1); |
200 |
q.add(one); |
201 |
for (int n : ns) |
202 |
assertEquals(0, q.drainTo(sink, n)); |
203 |
assertEquals(1, q.size()); |
204 |
assertSame(one, q.poll()); |
205 |
assertTrue(sink.isEmpty()); |
206 |
} |
207 |
} |
208 |
|
209 |
/** |
210 |
* timed poll before a delayed offer times out; after offer succeeds; |
211 |
* on interruption throws |
212 |
*/ |
213 |
public void testTimedPollWithOffer() throws InterruptedException { |
214 |
final BlockingQueue q = emptyCollection(); |
215 |
final CheckedBarrier barrier = new CheckedBarrier(2); |
216 |
final Object zero = makeElement(0); |
217 |
Thread t = newStartedThread(new CheckedRunnable() { |
218 |
public void realRun() throws InterruptedException { |
219 |
long startTime = System.nanoTime(); |
220 |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
221 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
222 |
|
223 |
barrier.await(); |
224 |
|
225 |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
226 |
|
227 |
Thread.currentThread().interrupt(); |
228 |
try { |
229 |
q.poll(randomTimeout(), randomTimeUnit()); |
230 |
shouldThrow(); |
231 |
} catch (InterruptedException success) {} |
232 |
assertFalse(Thread.interrupted()); |
233 |
|
234 |
barrier.await(); |
235 |
try { |
236 |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
237 |
shouldThrow(); |
238 |
} catch (InterruptedException success) {} |
239 |
assertFalse(Thread.interrupted()); |
240 |
|
241 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
242 |
}}); |
243 |
|
244 |
barrier.await(); |
245 |
long startTime = System.nanoTime(); |
246 |
assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); |
247 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
248 |
|
249 |
barrier.await(); |
250 |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
251 |
t.interrupt(); |
252 |
awaitTermination(t); |
253 |
} |
254 |
|
255 |
/** |
256 |
* take() blocks interruptibly when empty |
257 |
*/ |
258 |
public void testTakeFromEmptyBlocksInterruptibly() { |
259 |
final BlockingQueue q = emptyCollection(); |
260 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
261 |
Thread t = newStartedThread(new CheckedRunnable() { |
262 |
public void realRun() { |
263 |
threadStarted.countDown(); |
264 |
try { |
265 |
q.take(); |
266 |
shouldThrow(); |
267 |
} catch (InterruptedException success) {} |
268 |
assertFalse(Thread.interrupted()); |
269 |
}}); |
270 |
|
271 |
await(threadStarted); |
272 |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
273 |
t.interrupt(); |
274 |
awaitTermination(t); |
275 |
} |
276 |
|
277 |
/** |
278 |
* take() throws InterruptedException immediately if interrupted |
279 |
* before waiting |
280 |
*/ |
281 |
public void testTakeFromEmptyAfterInterrupt() { |
282 |
final BlockingQueue q = emptyCollection(); |
283 |
Thread t = newStartedThread(new CheckedRunnable() { |
284 |
public void realRun() { |
285 |
Thread.currentThread().interrupt(); |
286 |
try { |
287 |
q.take(); |
288 |
shouldThrow(); |
289 |
} catch (InterruptedException success) {} |
290 |
assertFalse(Thread.interrupted()); |
291 |
}}); |
292 |
|
293 |
awaitTermination(t); |
294 |
} |
295 |
|
296 |
/** |
297 |
* timed poll() blocks interruptibly when empty |
298 |
*/ |
299 |
public void testTimedPollFromEmptyBlocksInterruptibly() { |
300 |
final BlockingQueue q = emptyCollection(); |
301 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
302 |
Thread t = newStartedThread(new CheckedRunnable() { |
303 |
public void realRun() { |
304 |
threadStarted.countDown(); |
305 |
long startTime = System.nanoTime(); |
306 |
|
307 |
try { |
308 |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
309 |
shouldThrow(); |
310 |
} catch (InterruptedException success) {} |
311 |
assertFalse(Thread.interrupted()); |
312 |
|
313 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
314 |
}}); |
315 |
|
316 |
await(threadStarted); |
317 |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
318 |
t.interrupt(); |
319 |
awaitTermination(t); |
320 |
} |
321 |
|
322 |
/** |
323 |
* timed poll() throws InterruptedException immediately if |
324 |
* interrupted before waiting |
325 |
*/ |
326 |
public void testTimedPollFromEmptyAfterInterrupt() { |
327 |
final BlockingQueue q = emptyCollection(); |
328 |
Thread t = newStartedThread(new CheckedRunnable() { |
329 |
public void realRun() { |
330 |
long startTime = System.nanoTime(); |
331 |
|
332 |
Thread.currentThread().interrupt(); |
333 |
try { |
334 |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
335 |
shouldThrow(); |
336 |
} catch (InterruptedException success) {} |
337 |
assertFalse(Thread.interrupted()); |
338 |
|
339 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
340 |
}}); |
341 |
|
342 |
awaitTermination(t); |
343 |
} |
344 |
|
345 |
/** |
346 |
* remove(x) removes x and returns true if present |
347 |
* TODO: move to superclass CollectionTest.java |
348 |
*/ |
349 |
public void testRemoveElement() { |
350 |
final BlockingQueue q = emptyCollection(); |
351 |
final int size = Math.min(q.remainingCapacity(), SIZE); |
352 |
final Object[] elts = new Object[size]; |
353 |
assertFalse(q.contains(makeElement(99))); |
354 |
assertFalse(q.remove(makeElement(99))); |
355 |
checkEmpty(q); |
356 |
for (int i = 0; i < size; i++) |
357 |
q.add(elts[i] = makeElement(i)); |
358 |
for (int i = 1; i < size; i += 2) { |
359 |
for (int pass = 0; pass < 2; pass++) { |
360 |
assertEquals((pass == 0), q.contains(elts[i])); |
361 |
assertEquals((pass == 0), q.remove(elts[i])); |
362 |
assertFalse(q.contains(elts[i])); |
363 |
assertTrue(q.contains(elts[i - 1])); |
364 |
if (i < size - 1) |
365 |
assertTrue(q.contains(elts[i + 1])); |
366 |
} |
367 |
} |
368 |
if (size > 0) |
369 |
assertTrue(q.contains(elts[0])); |
370 |
for (int i = size - 2; i >= 0; i -= 2) { |
371 |
assertTrue(q.contains(elts[i])); |
372 |
assertFalse(q.contains(elts[i + 1])); |
373 |
assertTrue(q.remove(elts[i])); |
374 |
assertFalse(q.contains(elts[i])); |
375 |
assertFalse(q.remove(elts[i + 1])); |
376 |
assertFalse(q.contains(elts[i + 1])); |
377 |
} |
378 |
checkEmpty(q); |
379 |
} |
380 |
|
381 |
/** For debugging. */ |
382 |
public void XXXXtestFails() { |
383 |
fail(emptyCollection().getClass().toString()); |
384 |
} |
385 |
|
386 |
} |