20 |
|
return new TestSuite(SynchronousQueueTest.class); |
21 |
|
} |
22 |
|
|
23 |
+ |
/** |
24 |
+ |
* |
25 |
+ |
*/ |
26 |
|
public void testEmptyFull() { |
27 |
|
SynchronousQueue q = new SynchronousQueue(); |
28 |
|
assertTrue(q.isEmpty()); |
31 |
|
assertFalse(q.offer(new Integer(3))); |
32 |
|
} |
33 |
|
|
34 |
< |
public void testOfferNull(){ |
34 |
> |
/** |
35 |
> |
* |
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(){ |
45 |
> |
/** |
46 |
> |
* |
47 |
> |
*/ |
48 |
> |
public void testOffer() { |
49 |
|
SynchronousQueue q = new SynchronousQueue(); |
50 |
|
assertFalse(q.offer(new Integer(1))); |
51 |
|
} |
52 |
|
|
53 |
< |
public void testAdd(){ |
53 |
> |
/** |
54 |
> |
* |
55 |
> |
*/ |
56 |
> |
public void testAdd() { |
57 |
|
try { |
58 |
|
SynchronousQueue q = new SynchronousQueue(); |
59 |
|
assertEquals(0, q.remainingCapacity()); |
62 |
|
} |
63 |
|
} |
64 |
|
|
65 |
< |
public void testAddAll1(){ |
65 |
> |
/** |
66 |
> |
* |
67 |
> |
*/ |
68 |
> |
public void testAddAll1() { |
69 |
|
try { |
70 |
|
SynchronousQueue q = new SynchronousQueue(); |
71 |
|
q.addAll(null); |
72 |
< |
fail("Cannot add null collection"); |
72 |
> |
shouldThrow(); |
73 |
|
} |
74 |
|
catch (NullPointerException success) {} |
75 |
|
} |
76 |
< |
public void testAddAll2(){ |
76 |
> |
/** |
77 |
> |
* |
78 |
> |
*/ |
79 |
> |
public void testAddAll2() { |
80 |
|
try { |
81 |
|
SynchronousQueue q = new SynchronousQueue(); |
82 |
|
Integer[] ints = new Integer[1]; |
83 |
|
q.addAll(Arrays.asList(ints)); |
84 |
< |
fail("Cannot add null elements"); |
84 |
> |
shouldThrow(); |
85 |
|
} |
86 |
|
catch (NullPointerException success) {} |
87 |
|
} |
88 |
< |
public void testAddAll4(){ |
88 |
> |
/** |
89 |
> |
* |
90 |
> |
*/ |
91 |
> |
public void testAddAll4() { |
92 |
|
try { |
93 |
|
SynchronousQueue q = new SynchronousQueue(); |
94 |
|
Integer[] ints = new Integer[1]; |
95 |
|
for (int i = 0; i < 1; ++i) |
96 |
|
ints[i] = new Integer(i); |
97 |
|
q.addAll(Arrays.asList(ints)); |
98 |
< |
fail("Cannot add with insufficient capacity"); |
98 |
> |
shouldThrow(); |
99 |
|
} |
100 |
|
catch (IllegalStateException success) {} |
101 |
|
} |
102 |
|
|
103 |
+ |
/** |
104 |
+ |
* |
105 |
+ |
*/ |
106 |
|
public void testPutNull() { |
107 |
|
try { |
108 |
|
SynchronousQueue q = new SynchronousQueue(); |
109 |
|
q.put(null); |
110 |
< |
fail("put should throw NPE"); |
110 |
> |
shouldThrow(); |
111 |
|
} |
112 |
|
catch (NullPointerException success){ |
113 |
|
} |
114 |
|
catch (InterruptedException ie) { |
115 |
< |
fail("Unexpected exception"); |
115 |
> |
unexpectedException(); |
116 |
|
} |
117 |
|
} |
118 |
|
|
119 |
< |
public void testBlockingPut(){ |
119 |
> |
/** |
120 |
> |
* |
121 |
> |
*/ |
122 |
> |
public void testBlockingPut() { |
123 |
|
Thread t = new Thread(new Runnable() { |
124 |
|
public void run() { |
125 |
|
try { |
126 |
|
SynchronousQueue q = new SynchronousQueue(); |
127 |
|
q.put(new Integer(0)); |
128 |
< |
threadFail("put should block"); |
128 |
> |
threadShouldThrow(); |
129 |
|
} catch (InterruptedException ie){ |
130 |
|
} |
131 |
|
}}); |
136 |
|
t.join(); |
137 |
|
} |
138 |
|
catch (InterruptedException ie) { |
139 |
< |
fail("Unexpected exception"); |
139 |
> |
unexpectedException(); |
140 |
|
} |
141 |
|
} |
142 |
|
|
143 |
+ |
/** |
144 |
+ |
* |
145 |
+ |
*/ |
146 |
|
public void testPutWithTake() { |
147 |
|
final SynchronousQueue q = new SynchronousQueue(); |
148 |
|
Thread t = new Thread(new Runnable() { |
149 |
< |
public void run(){ |
149 |
> |
public void run() { |
150 |
|
int added = 0; |
151 |
|
try { |
152 |
|
q.put(new Object()); |
157 |
|
++added; |
158 |
|
q.put(new Object()); |
159 |
|
++added; |
160 |
< |
threadFail("Should block"); |
160 |
> |
threadShouldThrow(); |
161 |
|
} catch (InterruptedException e){ |
162 |
|
assertTrue(added >= 1); |
163 |
|
} |
171 |
|
t.interrupt(); |
172 |
|
t.join(); |
173 |
|
} catch (Exception e){ |
174 |
< |
fail("Unexpected exception"); |
174 |
> |
unexpectedException(); |
175 |
|
} |
176 |
|
} |
177 |
|
|
178 |
+ |
/** |
179 |
+ |
* |
180 |
+ |
*/ |
181 |
|
public void testTimedOffer() { |
182 |
|
final SynchronousQueue q = new SynchronousQueue(); |
183 |
|
Thread t = new Thread(new Runnable() { |
184 |
< |
public void run(){ |
184 |
> |
public void run() { |
185 |
|
try { |
186 |
|
|
187 |
|
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
188 |
|
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
189 |
< |
threadFail("Should block"); |
189 |
> |
threadShouldThrow(); |
190 |
|
} catch (InterruptedException success){} |
191 |
|
} |
192 |
|
}); |
197 |
|
t.interrupt(); |
198 |
|
t.join(); |
199 |
|
} catch (Exception e){ |
200 |
< |
fail("Unexpected exception"); |
200 |
> |
unexpectedException(); |
201 |
|
} |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
+ |
/** |
206 |
+ |
* |
207 |
+ |
*/ |
208 |
|
public void testTakeFromEmpty() { |
209 |
|
final SynchronousQueue q = new SynchronousQueue(); |
210 |
|
Thread t = new Thread(new Runnable() { |
211 |
< |
public void run(){ |
211 |
> |
public void run() { |
212 |
|
try { |
213 |
|
q.take(); |
214 |
< |
threadFail("Should block"); |
214 |
> |
threadShouldThrow(); |
215 |
|
} catch (InterruptedException success){ } |
216 |
|
} |
217 |
|
}); |
221 |
|
t.interrupt(); |
222 |
|
t.join(); |
223 |
|
} catch (Exception e){ |
224 |
< |
fail("Unexpected exception"); |
224 |
> |
unexpectedException(); |
225 |
|
} |
226 |
|
} |
227 |
|
|
228 |
< |
public void testPoll(){ |
228 |
> |
/** |
229 |
> |
* |
230 |
> |
*/ |
231 |
> |
public void testPoll() { |
232 |
|
SynchronousQueue q = new SynchronousQueue(); |
233 |
|
assertNull(q.poll()); |
234 |
|
} |
235 |
|
|
236 |
+ |
/** |
237 |
+ |
* |
238 |
+ |
*/ |
239 |
|
public void testTimedPoll0() { |
240 |
|
try { |
241 |
|
SynchronousQueue q = new SynchronousQueue(); |
242 |
|
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
243 |
|
} catch (InterruptedException e){ |
244 |
< |
fail("Unexpected exception"); |
244 |
> |
unexpectedException(); |
245 |
|
} |
246 |
|
} |
247 |
|
|
248 |
+ |
/** |
249 |
+ |
* |
250 |
+ |
*/ |
251 |
|
public void testTimedPoll() { |
252 |
|
try { |
253 |
|
SynchronousQueue q = new SynchronousQueue(); |
254 |
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
255 |
|
} catch (InterruptedException e){ |
256 |
< |
fail("Unexpected exception"); |
256 |
> |
unexpectedException(); |
257 |
|
} |
258 |
|
} |
259 |
|
|
260 |
< |
public void testInterruptedTimedPoll(){ |
260 |
> |
/** |
261 |
> |
* |
262 |
> |
*/ |
263 |
> |
public void testInterruptedTimedPoll() { |
264 |
|
Thread t = new Thread(new Runnable() { |
265 |
|
public void run() { |
266 |
|
try { |
276 |
|
t.join(); |
277 |
|
} |
278 |
|
catch (InterruptedException ie) { |
279 |
< |
fail("Unexpected exception"); |
279 |
> |
unexpectedException(); |
280 |
|
} |
281 |
|
} |
282 |
|
|
283 |
< |
public void testTimedPollWithOffer(){ |
283 |
> |
/** |
284 |
> |
* |
285 |
> |
*/ |
286 |
> |
public void testTimedPollWithOffer() { |
287 |
|
final SynchronousQueue q = new SynchronousQueue(); |
288 |
|
Thread t = new Thread(new Runnable() { |
289 |
< |
public void run(){ |
289 |
> |
public void run() { |
290 |
|
try { |
291 |
|
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
292 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
293 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
294 |
< |
threadFail("Should block"); |
294 |
> |
threadShouldThrow(); |
295 |
|
} catch (InterruptedException success) { } |
296 |
|
} |
297 |
|
}); |
302 |
|
t.interrupt(); |
303 |
|
t.join(); |
304 |
|
} catch (Exception e){ |
305 |
< |
fail("Unexpected exception"); |
305 |
> |
unexpectedException(); |
306 |
|
} |
307 |
|
} |
308 |
|
|
309 |
|
|
310 |
< |
public void testPeek(){ |
310 |
> |
/** |
311 |
> |
* |
312 |
> |
*/ |
313 |
> |
public void testPeek() { |
314 |
|
SynchronousQueue q = new SynchronousQueue(); |
315 |
|
assertNull(q.peek()); |
316 |
|
} |
317 |
|
|
318 |
< |
public void testElement(){ |
318 |
> |
/** |
319 |
> |
* |
320 |
> |
*/ |
321 |
> |
public void testElement() { |
322 |
|
SynchronousQueue q = new SynchronousQueue(); |
323 |
|
try { |
324 |
|
q.element(); |
325 |
< |
fail("no such element"); |
325 |
> |
shouldThrow(); |
326 |
|
} |
327 |
|
catch (NoSuchElementException success) {} |
328 |
|
} |
329 |
|
|
330 |
< |
public void testRemove(){ |
330 |
> |
/** |
331 |
> |
* |
332 |
> |
*/ |
333 |
> |
public void testRemove() { |
334 |
|
SynchronousQueue q = new SynchronousQueue(); |
335 |
|
try { |
336 |
|
q.remove(); |
337 |
< |
fail("remove should throw"); |
337 |
> |
shouldThrow(); |
338 |
|
} catch (NoSuchElementException success){ |
339 |
|
} |
340 |
|
} |
341 |
|
|
342 |
< |
public void testRemoveElement(){ |
342 |
> |
/** |
343 |
> |
* |
344 |
> |
*/ |
345 |
> |
public void testRemoveElement() { |
346 |
|
SynchronousQueue q = new SynchronousQueue(); |
347 |
|
assertFalse(q.remove(new Integer(0))); |
348 |
|
assertTrue(q.isEmpty()); |
349 |
|
} |
350 |
|
|
351 |
< |
public void testContains(){ |
351 |
> |
/** |
352 |
> |
* |
353 |
> |
*/ |
354 |
> |
public void testContains() { |
355 |
|
SynchronousQueue q = new SynchronousQueue(); |
356 |
|
assertFalse(q.contains(new Integer(0))); |
357 |
|
} |
358 |
|
|
359 |
< |
public void testClear(){ |
359 |
> |
/** |
360 |
> |
* |
361 |
> |
*/ |
362 |
> |
public void testClear() { |
363 |
|
SynchronousQueue q = new SynchronousQueue(); |
364 |
|
q.clear(); |
365 |
|
assertTrue(q.isEmpty()); |
366 |
|
} |
367 |
|
|
368 |
< |
public void testContainsAll(){ |
368 |
> |
/** |
369 |
> |
* |
370 |
> |
*/ |
371 |
> |
public void testContainsAll() { |
372 |
|
SynchronousQueue q = new SynchronousQueue(); |
373 |
|
Integer[] empty = new Integer[0]; |
374 |
|
Integer[] ints = new Integer[1]; ints[0] = new Integer(0); |
303 |
– |
// assertTrue(q.containsAll(Arrays.asList(empty))); |
375 |
|
assertFalse(q.containsAll(Arrays.asList(ints))); |
376 |
|
} |
377 |
|
|
378 |
< |
public void testRetainAll(){ |
378 |
> |
/** |
379 |
> |
* |
380 |
> |
*/ |
381 |
> |
public void testRetainAll() { |
382 |
|
SynchronousQueue q = new SynchronousQueue(); |
383 |
|
Integer[] empty = new Integer[0]; |
384 |
|
Integer[] ints = new Integer[1]; ints[0] = new Integer(0); |
385 |
|
q.retainAll(Arrays.asList(ints)); |
312 |
– |
// assertTrue(q.containsAll(Arrays.asList(empty))); |
386 |
|
assertFalse(q.containsAll(Arrays.asList(ints))); |
387 |
|
} |
388 |
|
|
389 |
< |
public void testRemoveAll(){ |
389 |
> |
/** |
390 |
> |
* |
391 |
> |
*/ |
392 |
> |
public void testRemoveAll() { |
393 |
|
SynchronousQueue q = new SynchronousQueue(); |
394 |
|
Integer[] empty = new Integer[0]; |
395 |
|
Integer[] ints = new Integer[1]; ints[0] = new Integer(0); |
396 |
|
q.removeAll(Arrays.asList(ints)); |
321 |
– |
// assertTrue(q.containsAll(Arrays.asList(empty))); |
397 |
|
assertFalse(q.containsAll(Arrays.asList(ints))); |
398 |
|
} |
399 |
|
|
400 |
|
|
401 |
< |
public void testToArray(){ |
401 |
> |
/** |
402 |
> |
* |
403 |
> |
*/ |
404 |
> |
public void testToArray() { |
405 |
|
SynchronousQueue q = new SynchronousQueue(); |
406 |
|
Object[] o = q.toArray(); |
407 |
|
assertEquals(o.length, 0); |
408 |
|
} |
409 |
|
|
410 |
< |
public void testToArray2(){ |
410 |
> |
/** |
411 |
> |
* |
412 |
> |
*/ |
413 |
> |
public void testToArray2() { |
414 |
|
SynchronousQueue q = new SynchronousQueue(); |
415 |
|
Integer[] ints = new Integer[1]; |
416 |
|
assertNull(ints[0]); |
417 |
|
} |
418 |
|
|
419 |
< |
public void testIterator(){ |
419 |
> |
/** |
420 |
> |
* |
421 |
> |
*/ |
422 |
> |
public void testIterator() { |
423 |
|
SynchronousQueue q = new SynchronousQueue(); |
424 |
|
Iterator it = q.iterator(); |
425 |
|
assertFalse(it.hasNext()); |
426 |
|
try { |
427 |
|
Object x = it.next(); |
428 |
< |
fail("should throw"); |
428 |
> |
shouldThrow(); |
429 |
|
} |
430 |
|
catch (NoSuchElementException success) {} |
431 |
|
} |
432 |
|
|
433 |
< |
public void testIteratorRemove(){ |
433 |
> |
/** |
434 |
> |
* |
435 |
> |
*/ |
436 |
> |
public void testIteratorRemove() { |
437 |
|
SynchronousQueue q = new SynchronousQueue(); |
438 |
|
Iterator it = q.iterator(); |
439 |
|
try { |
440 |
|
it.remove(); |
441 |
< |
fail("should throw"); |
441 |
> |
shouldThrow(); |
442 |
|
} |
443 |
|
catch (IllegalStateException success) {} |
444 |
|
} |
445 |
|
|
446 |
< |
public void testToString(){ |
446 |
> |
/** |
447 |
> |
* |
448 |
> |
*/ |
449 |
> |
public void testToString() { |
450 |
|
SynchronousQueue q = new SynchronousQueue(); |
451 |
|
String s = q.toString(); |
452 |
|
assertTrue(s != null); |
453 |
|
} |
454 |
|
|
455 |
|
|
456 |
+ |
/** |
457 |
+ |
* |
458 |
+ |
*/ |
459 |
|
public void testOfferInExecutor() { |
460 |
|
final SynchronousQueue q = new SynchronousQueue(); |
461 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
469 |
|
threadAssertEquals(0, q.remainingCapacity()); |
470 |
|
} |
471 |
|
catch (InterruptedException e) { |
472 |
< |
threadFail("should not be interrupted"); |
472 |
> |
threadUnexpectedException(); |
473 |
|
} |
474 |
|
} |
475 |
|
}); |
481 |
|
threadAssertEquals(one, q.take()); |
482 |
|
} |
483 |
|
catch (InterruptedException e) { |
484 |
< |
fail("should not be interrupted"); |
484 |
> |
threadUnexpectedException(); |
485 |
|
} |
486 |
|
} |
487 |
|
}); |
490 |
|
|
491 |
|
} |
492 |
|
|
493 |
+ |
/** |
494 |
+ |
* |
495 |
+ |
*/ |
496 |
|
public void testPollInExecutor() { |
497 |
|
|
498 |
|
final SynchronousQueue q = new SynchronousQueue(); |
507 |
|
threadAssertTrue(q.isEmpty()); |
508 |
|
} |
509 |
|
catch (InterruptedException e) { |
510 |
< |
threadFail("should not be interrupted"); |
510 |
> |
threadUnexpectedException(); |
511 |
|
} |
512 |
|
} |
513 |
|
}); |
519 |
|
q.put(new Integer(1)); |
520 |
|
} |
521 |
|
catch (InterruptedException e) { |
522 |
< |
threadFail("should not be interrupted"); |
522 |
> |
threadUnexpectedException(); |
523 |
|
} |
524 |
|
} |
525 |
|
}); |
528 |
|
|
529 |
|
} |
530 |
|
|
531 |
+ |
/** |
532 |
+ |
* |
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 |
|
|