21 |
|
} |
22 |
|
|
23 |
|
/** |
24 |
< |
* |
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 |
|
/** |
35 |
< |
* |
35 |
> |
* offer(null) throws NPE |
36 |
|
*/ |
37 |
|
public void testOfferNull() { |
38 |
|
try { |
43 |
|
} |
44 |
|
|
45 |
|
/** |
46 |
< |
* |
46 |
> |
* offer fails if no active taker |
47 |
|
*/ |
48 |
|
public void testOffer() { |
49 |
|
SynchronousQueue q = new SynchronousQueue(); |
50 |
< |
assertFalse(q.offer(new Integer(1))); |
50 |
> |
assertFalse(q.offer(one)); |
51 |
|
} |
52 |
|
|
53 |
|
/** |
54 |
< |
* |
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 |
|
/** |
67 |
< |
* |
67 |
> |
* addAll(null) throws NPE |
68 |
|
*/ |
69 |
|
public void testAddAll1() { |
70 |
|
try { |
75 |
|
catch (NullPointerException success) {} |
76 |
|
} |
77 |
|
/** |
78 |
< |
* |
78 |
> |
* addAll of a collection with null elements throws NPE |
79 |
|
*/ |
80 |
|
public void testAddAll2() { |
81 |
|
try { |
87 |
|
catch (NullPointerException success) {} |
88 |
|
} |
89 |
|
/** |
90 |
< |
* |
90 |
> |
* addAll throws ISE if no active taker |
91 |
|
*/ |
92 |
|
public void testAddAll4() { |
93 |
|
try { |
102 |
|
} |
103 |
|
|
104 |
|
/** |
105 |
< |
* |
105 |
> |
* put(null) throws NPE |
106 |
|
*/ |
107 |
|
public void testPutNull() { |
108 |
|
try { |
118 |
|
} |
119 |
|
|
120 |
|
/** |
121 |
< |
* |
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)); |
128 |
> |
q.put(zero); |
129 |
|
threadShouldThrow(); |
130 |
|
} catch (InterruptedException ie){ |
131 |
|
} |
142 |
|
} |
143 |
|
|
144 |
|
/** |
145 |
< |
* |
145 |
> |
* put blocks waiting for take |
146 |
|
*/ |
147 |
|
public void testPutWithTake() { |
148 |
|
final SynchronousQueue q = new SynchronousQueue(); |
177 |
|
} |
178 |
|
|
179 |
|
/** |
180 |
< |
* |
180 |
> |
* timed offer times out if elements not taken |
181 |
|
*/ |
182 |
|
public void testTimedOffer() { |
183 |
|
final SynchronousQueue q = new SynchronousQueue(); |
204 |
|
|
205 |
|
|
206 |
|
/** |
207 |
< |
* |
207 |
> |
* take blocks interruptibly when empty |
208 |
|
*/ |
209 |
|
public void testTakeFromEmpty() { |
210 |
|
final SynchronousQueue q = new SynchronousQueue(); |
227 |
|
} |
228 |
|
|
229 |
|
/** |
230 |
< |
* |
230 |
> |
* poll fails unless active taker |
231 |
|
*/ |
232 |
|
public void testPoll() { |
233 |
|
SynchronousQueue q = new SynchronousQueue(); |
235 |
|
} |
236 |
|
|
237 |
|
/** |
238 |
< |
* |
238 |
> |
* timed pool with zero timeout times out if no active taker |
239 |
|
*/ |
240 |
|
public void testTimedPoll0() { |
241 |
|
try { |
247 |
|
} |
248 |
|
|
249 |
|
/** |
250 |
< |
* |
250 |
> |
* timed pool with nonzero timeout times out if no active taker |
251 |
|
*/ |
252 |
|
public void testTimedPoll() { |
253 |
|
try { |
259 |
|
} |
260 |
|
|
261 |
|
/** |
262 |
< |
* |
262 |
> |
* Interrupted timed poll throws InterruptedException instead of |
263 |
> |
* returning timeout status |
264 |
|
*/ |
265 |
|
public void testInterruptedTimedPoll() { |
266 |
|
Thread t = new Thread(new Runnable() { |
283 |
|
} |
284 |
|
|
285 |
|
/** |
286 |
< |
* |
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(); |
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){ |
311 |
|
|
312 |
|
|
313 |
|
/** |
314 |
< |
* |
314 |
> |
* peek returns null |
315 |
|
*/ |
316 |
|
public void testPeek() { |
317 |
|
SynchronousQueue q = new SynchronousQueue(); |
319 |
|
} |
320 |
|
|
321 |
|
/** |
322 |
< |
* |
322 |
> |
* element throws NSEE |
323 |
|
*/ |
324 |
|
public void testElement() { |
325 |
|
SynchronousQueue q = new SynchronousQueue(); |
331 |
|
} |
332 |
|
|
333 |
|
/** |
334 |
< |
* |
334 |
> |
* remove throws NSEE if no active taker |
335 |
|
*/ |
336 |
|
public void testRemove() { |
337 |
|
SynchronousQueue q = new SynchronousQueue(); |
343 |
|
} |
344 |
|
|
345 |
|
/** |
346 |
< |
* |
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 |
|
/** |
355 |
< |
* |
355 |
> |
* contains returns false |
356 |
|
*/ |
357 |
|
public void testContains() { |
358 |
|
SynchronousQueue q = new SynchronousQueue(); |
359 |
< |
assertFalse(q.contains(new Integer(0))); |
359 |
> |
assertFalse(q.contains(zero)); |
360 |
|
} |
361 |
|
|
362 |
|
/** |
363 |
< |
* |
363 |
> |
* clear ensures isEmpty |
364 |
|
*/ |
365 |
|
public void testClear() { |
366 |
|
SynchronousQueue q = new SynchronousQueue(); |
369 |
|
} |
370 |
|
|
371 |
|
/** |
372 |
< |
* |
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); |
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 |
|
/** |
383 |
< |
* |
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 |
< |
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 |
|
/** |
394 |
< |
* |
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)); |
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 |
|
/** |
406 |
< |
* |
406 |
> |
* toArray is empty |
407 |
|
*/ |
408 |
|
public void testToArray() { |
409 |
|
SynchronousQueue q = new SynchronousQueue(); |
412 |
|
} |
413 |
|
|
414 |
|
/** |
415 |
< |
* |
415 |
> |
* toArray(a) is nulled at position 0 |
416 |
|
*/ |
417 |
|
public void testToArray2() { |
418 |
|
SynchronousQueue q = new SynchronousQueue(); |
421 |
|
} |
422 |
|
|
423 |
|
/** |
424 |
< |
* |
424 |
> |
* iterator does not traverse any elements |
425 |
|
*/ |
426 |
|
public void testIterator() { |
427 |
|
SynchronousQueue q = new SynchronousQueue(); |
435 |
|
} |
436 |
|
|
437 |
|
/** |
438 |
< |
* |
438 |
> |
* iterator remove throws ISE |
439 |
|
*/ |
440 |
|
public void testIteratorRemove() { |
441 |
|
SynchronousQueue q = new SynchronousQueue(); |
448 |
|
} |
449 |
|
|
450 |
|
/** |
451 |
< |
* |
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 |
< |
* |
461 |
> |
* offer transfers elements across Executor tasks |
462 |
|
*/ |
463 |
|
public void testOfferInExecutor() { |
464 |
|
final SynchronousQueue q = new SynchronousQueue(); |
495 |
|
} |
496 |
|
|
497 |
|
/** |
498 |
< |
* |
498 |
> |
* poll retrieves elements across Executor threads |
499 |
|
*/ |
500 |
|
public void testPollInExecutor() { |
497 |
– |
|
501 |
|
final SynchronousQueue q = new SynchronousQueue(); |
499 |
– |
|
502 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
501 |
– |
|
503 |
|
executor.execute(new Runnable() { |
504 |
|
public void run() { |
505 |
|
threadAssertNull(q.poll()); |
526 |
|
}); |
527 |
|
|
528 |
|
joinPool(executor); |
528 |
– |
|
529 |
|
} |
530 |
|
|
531 |
|
/** |
532 |
< |
* |
532 |
> |
* a deserialized serialized queue is usable |
533 |
|
*/ |
534 |
|
public void testSerialization() { |
535 |
|
SynchronousQueue q = new SynchronousQueue(); |