20 |
|
return new TestSuite(SynchronousQueueTest.class); |
21 |
|
} |
22 |
|
|
23 |
+ |
/** |
24 |
+ |
* A SynchronousQueue is both empty and full |
25 |
+ |
*/ |
26 |
|
public void testEmptyFull() { |
27 |
|
SynchronousQueue q = new SynchronousQueue(); |
28 |
|
assertTrue(q.isEmpty()); |
29 |
|
assertEquals(0, q.size()); |
30 |
|
assertEquals(0, q.remainingCapacity()); |
31 |
< |
assertFalse(q.offer(new Integer(3))); |
31 |
> |
assertFalse(q.offer(zero)); |
32 |
|
} |
33 |
|
|
34 |
< |
public void testOfferNull(){ |
34 |
> |
/** |
35 |
> |
* offer(null) throws NPE |
36 |
> |
*/ |
37 |
> |
public void testOfferNull() { |
38 |
|
try { |
39 |
|
SynchronousQueue q = new SynchronousQueue(); |
40 |
|
q.offer(null); |
41 |
< |
fail("should throw NPE"); |
41 |
> |
shouldThrow(); |
42 |
|
} catch (NullPointerException success) { } |
43 |
|
} |
44 |
|
|
45 |
< |
public void testOffer(){ |
46 |
< |
SynchronousQueue q = new SynchronousQueue(); |
47 |
< |
assertFalse(q.offer(new Integer(1))); |
45 |
> |
/** |
46 |
> |
* offer fails if no active taker |
47 |
> |
*/ |
48 |
> |
public void testOffer() { |
49 |
> |
SynchronousQueue q = new SynchronousQueue(); |
50 |
> |
assertFalse(q.offer(one)); |
51 |
|
} |
52 |
|
|
53 |
< |
public void testAdd(){ |
53 |
> |
/** |
54 |
> |
* add throws ISE if no active taker |
55 |
> |
*/ |
56 |
> |
public void testAdd() { |
57 |
|
try { |
58 |
|
SynchronousQueue q = new SynchronousQueue(); |
59 |
|
assertEquals(0, q.remainingCapacity()); |
60 |
< |
q.add(new Integer(0)); |
60 |
> |
q.add(one); |
61 |
> |
shouldThrow(); |
62 |
|
} catch (IllegalStateException success){ |
63 |
|
} |
64 |
|
} |
65 |
|
|
66 |
< |
public void testAddAll1(){ |
66 |
> |
/** |
67 |
> |
* addAll(null) throws NPE |
68 |
> |
*/ |
69 |
> |
public void testAddAll1() { |
70 |
|
try { |
71 |
|
SynchronousQueue q = new SynchronousQueue(); |
72 |
|
q.addAll(null); |
73 |
< |
fail("Cannot add null collection"); |
73 |
> |
shouldThrow(); |
74 |
|
} |
75 |
|
catch (NullPointerException success) {} |
76 |
|
} |
77 |
< |
public void testAddAll2(){ |
77 |
> |
/** |
78 |
> |
* addAll of a collection with null elements throws NPE |
79 |
> |
*/ |
80 |
> |
public void testAddAll2() { |
81 |
|
try { |
82 |
|
SynchronousQueue q = new SynchronousQueue(); |
83 |
|
Integer[] ints = new Integer[1]; |
84 |
|
q.addAll(Arrays.asList(ints)); |
85 |
< |
fail("Cannot add null elements"); |
85 |
> |
shouldThrow(); |
86 |
|
} |
87 |
|
catch (NullPointerException success) {} |
88 |
|
} |
89 |
< |
public void testAddAll4(){ |
89 |
> |
/** |
90 |
> |
* addAll throws ISE if no active taker |
91 |
> |
*/ |
92 |
> |
public void testAddAll4() { |
93 |
|
try { |
94 |
|
SynchronousQueue q = new SynchronousQueue(); |
95 |
|
Integer[] ints = new Integer[1]; |
96 |
|
for (int i = 0; i < 1; ++i) |
97 |
|
ints[i] = new Integer(i); |
98 |
|
q.addAll(Arrays.asList(ints)); |
99 |
< |
fail("Cannot add with insufficient capacity"); |
99 |
> |
shouldThrow(); |
100 |
|
} |
101 |
|
catch (IllegalStateException success) {} |
102 |
|
} |
103 |
|
|
104 |
+ |
/** |
105 |
+ |
* put(null) throws NPE |
106 |
+ |
*/ |
107 |
|
public void testPutNull() { |
108 |
|
try { |
109 |
|
SynchronousQueue q = new SynchronousQueue(); |
110 |
|
q.put(null); |
111 |
< |
fail("put should throw NPE"); |
111 |
> |
shouldThrow(); |
112 |
|
} |
113 |
|
catch (NullPointerException success){ |
114 |
|
} |
115 |
|
catch (InterruptedException ie) { |
116 |
< |
fail("Unexpected exception"); |
116 |
> |
unexpectedException(); |
117 |
|
} |
118 |
|
} |
119 |
|
|
120 |
< |
public void testBlockingPut(){ |
120 |
> |
/** |
121 |
> |
* put blocks interruptibly if no active taker |
122 |
> |
*/ |
123 |
> |
public void testBlockingPut() { |
124 |
|
Thread t = new Thread(new Runnable() { |
125 |
|
public void run() { |
126 |
|
try { |
127 |
|
SynchronousQueue q = new SynchronousQueue(); |
128 |
< |
q.put(new Integer(0)); |
129 |
< |
threadFail("put should block"); |
128 |
> |
q.put(zero); |
129 |
> |
threadShouldThrow(); |
130 |
|
} catch (InterruptedException ie){ |
131 |
|
} |
132 |
|
}}); |
137 |
|
t.join(); |
138 |
|
} |
139 |
|
catch (InterruptedException ie) { |
140 |
< |
fail("Unexpected exception"); |
140 |
> |
unexpectedException(); |
141 |
|
} |
142 |
|
} |
143 |
|
|
144 |
+ |
/** |
145 |
+ |
* put blocks waiting for take |
146 |
+ |
*/ |
147 |
|
public void testPutWithTake() { |
148 |
|
final SynchronousQueue q = new SynchronousQueue(); |
149 |
|
Thread t = new Thread(new Runnable() { |
150 |
< |
public void run(){ |
150 |
> |
public void run() { |
151 |
|
int added = 0; |
152 |
|
try { |
153 |
|
q.put(new Object()); |
158 |
|
++added; |
159 |
|
q.put(new Object()); |
160 |
|
++added; |
161 |
< |
threadFail("Should block"); |
161 |
> |
threadShouldThrow(); |
162 |
|
} catch (InterruptedException e){ |
163 |
|
assertTrue(added >= 1); |
164 |
|
} |
172 |
|
t.interrupt(); |
173 |
|
t.join(); |
174 |
|
} catch (Exception e){ |
175 |
< |
fail("Unexpected exception"); |
175 |
> |
unexpectedException(); |
176 |
|
} |
177 |
|
} |
178 |
|
|
179 |
+ |
/** |
180 |
+ |
* timed offer times out if elements not taken |
181 |
+ |
*/ |
182 |
|
public void testTimedOffer() { |
183 |
|
final SynchronousQueue q = new SynchronousQueue(); |
184 |
|
Thread t = new Thread(new Runnable() { |
185 |
< |
public void run(){ |
185 |
> |
public void run() { |
186 |
|
try { |
187 |
|
|
188 |
|
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
189 |
|
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
190 |
< |
threadFail("Should block"); |
190 |
> |
threadShouldThrow(); |
191 |
|
} catch (InterruptedException success){} |
192 |
|
} |
193 |
|
}); |
198 |
|
t.interrupt(); |
199 |
|
t.join(); |
200 |
|
} catch (Exception e){ |
201 |
< |
fail("Unexpected exception"); |
201 |
> |
unexpectedException(); |
202 |
|
} |
203 |
|
} |
204 |
|
|
205 |
|
|
206 |
+ |
/** |
207 |
+ |
* take blocks interruptibly when empty |
208 |
+ |
*/ |
209 |
|
public void testTakeFromEmpty() { |
210 |
|
final SynchronousQueue q = new SynchronousQueue(); |
211 |
|
Thread t = new Thread(new Runnable() { |
212 |
< |
public void run(){ |
212 |
> |
public void run() { |
213 |
|
try { |
214 |
|
q.take(); |
215 |
< |
threadFail("Should block"); |
215 |
> |
threadShouldThrow(); |
216 |
|
} catch (InterruptedException success){ } |
217 |
|
} |
218 |
|
}); |
222 |
|
t.interrupt(); |
223 |
|
t.join(); |
224 |
|
} catch (Exception e){ |
225 |
< |
fail("Unexpected exception"); |
225 |
> |
unexpectedException(); |
226 |
|
} |
227 |
|
} |
228 |
|
|
229 |
< |
public void testPoll(){ |
229 |
> |
/** |
230 |
> |
* poll fails unless active taker |
231 |
> |
*/ |
232 |
> |
public void testPoll() { |
233 |
|
SynchronousQueue q = new SynchronousQueue(); |
234 |
|
assertNull(q.poll()); |
235 |
|
} |
236 |
|
|
237 |
+ |
/** |
238 |
+ |
* timed pool with zero timeout times out if no active taker |
239 |
+ |
*/ |
240 |
|
public void testTimedPoll0() { |
241 |
|
try { |
242 |
|
SynchronousQueue q = new SynchronousQueue(); |
243 |
|
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
244 |
|
} catch (InterruptedException e){ |
245 |
< |
fail("Unexpected exception"); |
245 |
> |
unexpectedException(); |
246 |
|
} |
247 |
|
} |
248 |
|
|
249 |
+ |
/** |
250 |
+ |
* timed pool with nonzero timeout times out if no active taker |
251 |
+ |
*/ |
252 |
|
public void testTimedPoll() { |
253 |
|
try { |
254 |
|
SynchronousQueue q = new SynchronousQueue(); |
255 |
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
256 |
|
} catch (InterruptedException e){ |
257 |
< |
fail("Unexpected exception"); |
257 |
> |
unexpectedException(); |
258 |
|
} |
259 |
|
} |
260 |
|
|
261 |
< |
public void testInterruptedTimedPoll(){ |
261 |
> |
/** |
262 |
> |
* Interrupted timed poll throws InterruptedException instead of |
263 |
> |
* returning timeout status |
264 |
> |
*/ |
265 |
> |
public void testInterruptedTimedPoll() { |
266 |
|
Thread t = new Thread(new Runnable() { |
267 |
|
public void run() { |
268 |
|
try { |
278 |
|
t.join(); |
279 |
|
} |
280 |
|
catch (InterruptedException ie) { |
281 |
< |
fail("Unexpected exception"); |
281 |
> |
unexpectedException(); |
282 |
|
} |
283 |
|
} |
284 |
|
|
285 |
< |
public void testTimedPollWithOffer(){ |
285 |
> |
/** |
286 |
> |
* timed poll before a delayed offer fails; after offer succeeds; |
287 |
> |
* on interruption throws |
288 |
> |
*/ |
289 |
> |
public void testTimedPollWithOffer() { |
290 |
|
final SynchronousQueue q = new SynchronousQueue(); |
291 |
|
Thread t = new Thread(new Runnable() { |
292 |
< |
public void run(){ |
292 |
> |
public void run() { |
293 |
|
try { |
294 |
|
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
295 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
296 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
297 |
< |
threadFail("Should block"); |
297 |
> |
threadShouldThrow(); |
298 |
|
} catch (InterruptedException success) { } |
299 |
|
} |
300 |
|
}); |
301 |
|
try { |
302 |
|
t.start(); |
303 |
|
Thread.sleep(SMALL_DELAY_MS); |
304 |
< |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
304 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
305 |
|
t.interrupt(); |
306 |
|
t.join(); |
307 |
|
} catch (Exception e){ |
308 |
< |
fail("Unexpected exception"); |
308 |
> |
unexpectedException(); |
309 |
|
} |
310 |
|
} |
311 |
|
|
312 |
|
|
313 |
< |
public void testPeek(){ |
313 |
> |
/** |
314 |
> |
* peek returns null |
315 |
> |
*/ |
316 |
> |
public void testPeek() { |
317 |
|
SynchronousQueue q = new SynchronousQueue(); |
318 |
|
assertNull(q.peek()); |
319 |
|
} |
320 |
|
|
321 |
< |
public void testElement(){ |
321 |
> |
/** |
322 |
> |
* element throws NSEE |
323 |
> |
*/ |
324 |
> |
public void testElement() { |
325 |
|
SynchronousQueue q = new SynchronousQueue(); |
326 |
|
try { |
327 |
|
q.element(); |
328 |
< |
fail("no such element"); |
328 |
> |
shouldThrow(); |
329 |
|
} |
330 |
|
catch (NoSuchElementException success) {} |
331 |
|
} |
332 |
|
|
333 |
< |
public void testRemove(){ |
333 |
> |
/** |
334 |
> |
* remove throws NSEE if no active taker |
335 |
> |
*/ |
336 |
> |
public void testRemove() { |
337 |
|
SynchronousQueue q = new SynchronousQueue(); |
338 |
|
try { |
339 |
|
q.remove(); |
340 |
< |
fail("remove should throw"); |
340 |
> |
shouldThrow(); |
341 |
|
} catch (NoSuchElementException success){ |
342 |
|
} |
343 |
|
} |
344 |
|
|
345 |
< |
public void testRemoveElement(){ |
345 |
> |
/** |
346 |
> |
* remove(x) returns false |
347 |
> |
*/ |
348 |
> |
public void testRemoveElement() { |
349 |
|
SynchronousQueue q = new SynchronousQueue(); |
350 |
< |
assertFalse(q.remove(new Integer(0))); |
350 |
> |
assertFalse(q.remove(zero)); |
351 |
|
assertTrue(q.isEmpty()); |
352 |
|
} |
353 |
|
|
354 |
< |
public void testContains(){ |
355 |
< |
SynchronousQueue q = new SynchronousQueue(); |
356 |
< |
assertFalse(q.contains(new Integer(0))); |
354 |
> |
/** |
355 |
> |
* contains returns false |
356 |
> |
*/ |
357 |
> |
public void testContains() { |
358 |
> |
SynchronousQueue q = new SynchronousQueue(); |
359 |
> |
assertFalse(q.contains(zero)); |
360 |
|
} |
361 |
|
|
362 |
< |
public void testClear(){ |
362 |
> |
/** |
363 |
> |
* clear ensures isEmpty |
364 |
> |
*/ |
365 |
> |
public void testClear() { |
366 |
|
SynchronousQueue q = new SynchronousQueue(); |
367 |
|
q.clear(); |
368 |
|
assertTrue(q.isEmpty()); |
369 |
|
} |
370 |
|
|
371 |
< |
public void testContainsAll(){ |
371 |
> |
/** |
372 |
> |
* containsAll returns false unless empty |
373 |
> |
*/ |
374 |
> |
public void testContainsAll() { |
375 |
|
SynchronousQueue q = new SynchronousQueue(); |
376 |
|
Integer[] empty = new Integer[0]; |
377 |
< |
Integer[] ints = new Integer[1]; ints[0] = new Integer(0); |
378 |
< |
// assertTrue(q.containsAll(Arrays.asList(empty))); |
377 |
> |
assertTrue(q.containsAll(Arrays.asList(empty))); |
378 |
> |
Integer[] ints = new Integer[1]; ints[0] = zero; |
379 |
|
assertFalse(q.containsAll(Arrays.asList(ints))); |
380 |
|
} |
381 |
|
|
382 |
< |
public void testRetainAll(){ |
382 |
> |
/** |
383 |
> |
* retainAll returns false |
384 |
> |
*/ |
385 |
> |
public void testRetainAll() { |
386 |
|
SynchronousQueue q = new SynchronousQueue(); |
387 |
|
Integer[] empty = new Integer[0]; |
388 |
< |
Integer[] ints = new Integer[1]; ints[0] = new Integer(0); |
389 |
< |
q.retainAll(Arrays.asList(ints)); |
390 |
< |
// assertTrue(q.containsAll(Arrays.asList(empty))); |
313 |
< |
assertFalse(q.containsAll(Arrays.asList(ints))); |
388 |
> |
assertFalse(q.retainAll(Arrays.asList(empty))); |
389 |
> |
Integer[] ints = new Integer[1]; ints[0] = zero; |
390 |
> |
assertFalse(q.retainAll(Arrays.asList(ints))); |
391 |
|
} |
392 |
|
|
393 |
< |
public void testRemoveAll(){ |
393 |
> |
/** |
394 |
> |
* removeAll returns false |
395 |
> |
*/ |
396 |
> |
public void testRemoveAll() { |
397 |
|
SynchronousQueue q = new SynchronousQueue(); |
398 |
|
Integer[] empty = new Integer[0]; |
399 |
< |
Integer[] ints = new Integer[1]; ints[0] = new Integer(0); |
400 |
< |
q.removeAll(Arrays.asList(ints)); |
321 |
< |
// assertTrue(q.containsAll(Arrays.asList(empty))); |
399 |
> |
assertFalse(q.removeAll(Arrays.asList(empty))); |
400 |
> |
Integer[] ints = new Integer[1]; ints[0] = zero; |
401 |
|
assertFalse(q.containsAll(Arrays.asList(ints))); |
402 |
|
} |
403 |
|
|
404 |
|
|
405 |
< |
public void testToArray(){ |
405 |
> |
/** |
406 |
> |
* toArray is empty |
407 |
> |
*/ |
408 |
> |
public void testToArray() { |
409 |
|
SynchronousQueue q = new SynchronousQueue(); |
410 |
|
Object[] o = q.toArray(); |
411 |
|
assertEquals(o.length, 0); |
412 |
|
} |
413 |
|
|
414 |
< |
public void testToArray2(){ |
414 |
> |
/** |
415 |
> |
* toArray(a) is nulled at position 0 |
416 |
> |
*/ |
417 |
> |
public void testToArray2() { |
418 |
|
SynchronousQueue q = new SynchronousQueue(); |
419 |
|
Integer[] ints = new Integer[1]; |
420 |
|
assertNull(ints[0]); |
421 |
|
} |
422 |
|
|
423 |
< |
public void testIterator(){ |
423 |
> |
/** |
424 |
> |
* iterator does not traverse any elements |
425 |
> |
*/ |
426 |
> |
public void testIterator() { |
427 |
|
SynchronousQueue q = new SynchronousQueue(); |
428 |
|
Iterator it = q.iterator(); |
429 |
|
assertFalse(it.hasNext()); |
430 |
|
try { |
431 |
|
Object x = it.next(); |
432 |
< |
fail("should throw"); |
432 |
> |
shouldThrow(); |
433 |
|
} |
434 |
|
catch (NoSuchElementException success) {} |
435 |
|
} |
436 |
|
|
437 |
< |
public void testIteratorRemove(){ |
437 |
> |
/** |
438 |
> |
* iterator remove throws ISE |
439 |
> |
*/ |
440 |
> |
public void testIteratorRemove() { |
441 |
|
SynchronousQueue q = new SynchronousQueue(); |
442 |
|
Iterator it = q.iterator(); |
443 |
|
try { |
444 |
|
it.remove(); |
445 |
< |
fail("should throw"); |
445 |
> |
shouldThrow(); |
446 |
|
} |
447 |
|
catch (IllegalStateException success) {} |
448 |
|
} |
449 |
|
|
450 |
< |
public void testToString(){ |
450 |
> |
/** |
451 |
> |
* toString returns a non-null string |
452 |
> |
*/ |
453 |
> |
public void testToString() { |
454 |
|
SynchronousQueue q = new SynchronousQueue(); |
455 |
|
String s = q.toString(); |
456 |
< |
assertTrue(s != null); |
456 |
> |
assertNotNull(s); |
457 |
|
} |
458 |
|
|
459 |
|
|
460 |
+ |
/** |
461 |
+ |
* offer transfers elements across Executor tasks |
462 |
+ |
*/ |
463 |
|
public void testOfferInExecutor() { |
464 |
|
final SynchronousQueue q = new SynchronousQueue(); |
465 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
473 |
|
threadAssertEquals(0, q.remainingCapacity()); |
474 |
|
} |
475 |
|
catch (InterruptedException e) { |
476 |
< |
threadFail("should not be interrupted"); |
476 |
> |
threadUnexpectedException(); |
477 |
|
} |
478 |
|
} |
479 |
|
}); |
485 |
|
threadAssertEquals(one, q.take()); |
486 |
|
} |
487 |
|
catch (InterruptedException e) { |
488 |
< |
fail("should not be interrupted"); |
488 |
> |
threadUnexpectedException(); |
489 |
|
} |
490 |
|
} |
491 |
|
}); |
494 |
|
|
495 |
|
} |
496 |
|
|
497 |
+ |
/** |
498 |
+ |
* poll retrieves elements across Executor threads |
499 |
+ |
*/ |
500 |
|
public void testPollInExecutor() { |
401 |
– |
|
501 |
|
final SynchronousQueue q = new SynchronousQueue(); |
403 |
– |
|
502 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
405 |
– |
|
503 |
|
executor.execute(new Runnable() { |
504 |
|
public void run() { |
505 |
|
threadAssertNull(q.poll()); |
508 |
|
threadAssertTrue(q.isEmpty()); |
509 |
|
} |
510 |
|
catch (InterruptedException e) { |
511 |
< |
threadFail("should not be interrupted"); |
511 |
> |
threadUnexpectedException(); |
512 |
|
} |
513 |
|
} |
514 |
|
}); |
520 |
|
q.put(new Integer(1)); |
521 |
|
} |
522 |
|
catch (InterruptedException e) { |
523 |
< |
threadFail("should not be interrupted"); |
523 |
> |
threadUnexpectedException(); |
524 |
|
} |
525 |
|
} |
526 |
|
}); |
527 |
|
|
528 |
|
joinPool(executor); |
432 |
– |
|
529 |
|
} |
530 |
|
|
531 |
+ |
/** |
532 |
+ |
* a deserialized serialized queue is usable |
533 |
+ |
*/ |
534 |
|
public void testSerialization() { |
535 |
|
SynchronousQueue q = new SynchronousQueue(); |
536 |
|
try { |
546 |
|
while (!q.isEmpty()) |
547 |
|
assertEquals(q.remove(), r.remove()); |
548 |
|
} catch(Exception e){ |
549 |
< |
fail("unexpected exception"); |
549 |
> |
unexpectedException(); |
550 |
|
} |
551 |
|
} |
552 |
|
|