1 |
/* |
2 |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
* public domain. Use, modify, and redistribute this code in any way |
4 |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
6 |
*/ |
7 |
|
8 |
import junit.framework.*; |
9 |
import java.util.*; |
10 |
import java.util.concurrent.*; |
11 |
import java.io.*; |
12 |
|
13 |
public class SynchronousQueueTest extends JSR166TestCase { |
14 |
|
15 |
public static void main(String[] args) { |
16 |
junit.textui.TestRunner.run (suite()); |
17 |
} |
18 |
|
19 |
public static Test suite() { |
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(zero)); |
32 |
} |
33 |
|
34 |
/** |
35 |
* offer(null) throws NPE |
36 |
*/ |
37 |
public void testOfferNull() { |
38 |
try { |
39 |
SynchronousQueue q = new SynchronousQueue(); |
40 |
q.offer(null); |
41 |
shouldThrow(); |
42 |
} catch (NullPointerException success) { } |
43 |
} |
44 |
|
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 |
/** |
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(one); |
61 |
shouldThrow(); |
62 |
} catch (IllegalStateException success){ |
63 |
} |
64 |
} |
65 |
|
66 |
/** |
67 |
* addAll(null) throws NPE |
68 |
*/ |
69 |
public void testAddAll1() { |
70 |
try { |
71 |
SynchronousQueue q = new SynchronousQueue(); |
72 |
q.addAll(null); |
73 |
shouldThrow(); |
74 |
} |
75 |
catch (NullPointerException success) {} |
76 |
} |
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 |
shouldThrow(); |
86 |
} |
87 |
catch (NullPointerException success) {} |
88 |
} |
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 |
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 |
shouldThrow(); |
112 |
} |
113 |
catch (NullPointerException success){ |
114 |
} |
115 |
catch (InterruptedException ie) { |
116 |
unexpectedException(); |
117 |
} |
118 |
} |
119 |
|
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(zero); |
129 |
threadShouldThrow(); |
130 |
} catch (InterruptedException ie){ |
131 |
} |
132 |
}}); |
133 |
t.start(); |
134 |
try { |
135 |
Thread.sleep(SHORT_DELAY_MS); |
136 |
t.interrupt(); |
137 |
t.join(); |
138 |
} |
139 |
catch (InterruptedException ie) { |
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() { |
151 |
int added = 0; |
152 |
try { |
153 |
q.put(new Object()); |
154 |
++added; |
155 |
q.put(new Object()); |
156 |
++added; |
157 |
q.put(new Object()); |
158 |
++added; |
159 |
q.put(new Object()); |
160 |
++added; |
161 |
threadShouldThrow(); |
162 |
} catch (InterruptedException e){ |
163 |
assertTrue(added >= 1); |
164 |
} |
165 |
} |
166 |
}); |
167 |
try { |
168 |
t.start(); |
169 |
Thread.sleep(SHORT_DELAY_MS); |
170 |
q.take(); |
171 |
Thread.sleep(SHORT_DELAY_MS); |
172 |
t.interrupt(); |
173 |
t.join(); |
174 |
} catch (Exception e){ |
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() { |
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 |
threadShouldThrow(); |
191 |
} catch (InterruptedException success){} |
192 |
} |
193 |
}); |
194 |
|
195 |
try { |
196 |
t.start(); |
197 |
Thread.sleep(SMALL_DELAY_MS); |
198 |
t.interrupt(); |
199 |
t.join(); |
200 |
} catch (Exception e){ |
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() { |
213 |
try { |
214 |
q.take(); |
215 |
threadShouldThrow(); |
216 |
} catch (InterruptedException success){ } |
217 |
} |
218 |
}); |
219 |
try { |
220 |
t.start(); |
221 |
Thread.sleep(SHORT_DELAY_MS); |
222 |
t.interrupt(); |
223 |
t.join(); |
224 |
} catch (Exception e){ |
225 |
unexpectedException(); |
226 |
} |
227 |
} |
228 |
|
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 |
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 |
unexpectedException(); |
258 |
} |
259 |
} |
260 |
|
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 { |
269 |
SynchronousQueue q = new SynchronousQueue(); |
270 |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
271 |
} catch (InterruptedException success){ |
272 |
} |
273 |
}}); |
274 |
t.start(); |
275 |
try { |
276 |
Thread.sleep(SHORT_DELAY_MS); |
277 |
t.interrupt(); |
278 |
t.join(); |
279 |
} |
280 |
catch (InterruptedException ie) { |
281 |
unexpectedException(); |
282 |
} |
283 |
} |
284 |
|
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() { |
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 |
threadShouldThrow(); |
298 |
} catch (InterruptedException success) { } |
299 |
} |
300 |
}); |
301 |
try { |
302 |
t.start(); |
303 |
Thread.sleep(SMALL_DELAY_MS); |
304 |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
305 |
t.interrupt(); |
306 |
t.join(); |
307 |
} catch (Exception e){ |
308 |
unexpectedException(); |
309 |
} |
310 |
} |
311 |
|
312 |
|
313 |
/** |
314 |
* peek returns null |
315 |
*/ |
316 |
public void testPeek() { |
317 |
SynchronousQueue q = new SynchronousQueue(); |
318 |
assertNull(q.peek()); |
319 |
} |
320 |
|
321 |
/** |
322 |
* element throws NSEE |
323 |
*/ |
324 |
public void testElement() { |
325 |
SynchronousQueue q = new SynchronousQueue(); |
326 |
try { |
327 |
q.element(); |
328 |
shouldThrow(); |
329 |
} |
330 |
catch (NoSuchElementException success) {} |
331 |
} |
332 |
|
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 |
shouldThrow(); |
341 |
} catch (NoSuchElementException success){ |
342 |
} |
343 |
} |
344 |
|
345 |
/** |
346 |
* remove(x) returns false |
347 |
*/ |
348 |
public void testRemoveElement() { |
349 |
SynchronousQueue q = new SynchronousQueue(); |
350 |
assertFalse(q.remove(zero)); |
351 |
assertTrue(q.isEmpty()); |
352 |
} |
353 |
|
354 |
/** |
355 |
* contains returns false |
356 |
*/ |
357 |
public void testContains() { |
358 |
SynchronousQueue q = new SynchronousQueue(); |
359 |
assertFalse(q.contains(zero)); |
360 |
} |
361 |
|
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 |
/** |
372 |
* containsAll returns false unless empty |
373 |
*/ |
374 |
public void testContainsAll() { |
375 |
SynchronousQueue q = new SynchronousQueue(); |
376 |
Integer[] empty = 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 |
* retainAll returns false |
384 |
*/ |
385 |
public void testRetainAll() { |
386 |
SynchronousQueue q = new SynchronousQueue(); |
387 |
Integer[] empty = new Integer[0]; |
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 |
* removeAll returns false |
395 |
*/ |
396 |
public void testRemoveAll() { |
397 |
SynchronousQueue q = new SynchronousQueue(); |
398 |
Integer[] empty = new Integer[0]; |
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 |
* 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 |
/** |
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 |
/** |
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 |
shouldThrow(); |
433 |
} |
434 |
catch (NoSuchElementException success) {} |
435 |
} |
436 |
|
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 |
shouldThrow(); |
446 |
} |
447 |
catch (IllegalStateException success) {} |
448 |
} |
449 |
|
450 |
/** |
451 |
* toString returns a non-null string |
452 |
*/ |
453 |
public void testToString() { |
454 |
SynchronousQueue q = new SynchronousQueue(); |
455 |
String s = q.toString(); |
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); |
466 |
final Integer one = new Integer(1); |
467 |
|
468 |
executor.execute(new Runnable() { |
469 |
public void run() { |
470 |
threadAssertFalse(q.offer(one)); |
471 |
try { |
472 |
threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
473 |
threadAssertEquals(0, q.remainingCapacity()); |
474 |
} |
475 |
catch (InterruptedException e) { |
476 |
threadUnexpectedException(); |
477 |
} |
478 |
} |
479 |
}); |
480 |
|
481 |
executor.execute(new Runnable() { |
482 |
public void run() { |
483 |
try { |
484 |
Thread.sleep(SMALL_DELAY_MS); |
485 |
threadAssertEquals(one, q.take()); |
486 |
} |
487 |
catch (InterruptedException e) { |
488 |
threadUnexpectedException(); |
489 |
} |
490 |
} |
491 |
}); |
492 |
|
493 |
joinPool(executor); |
494 |
|
495 |
} |
496 |
|
497 |
/** |
498 |
* poll retrieves elements across Executor threads |
499 |
*/ |
500 |
public void testPollInExecutor() { |
501 |
final SynchronousQueue q = new SynchronousQueue(); |
502 |
ExecutorService executor = Executors.newFixedThreadPool(2); |
503 |
executor.execute(new Runnable() { |
504 |
public void run() { |
505 |
threadAssertNull(q.poll()); |
506 |
try { |
507 |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
508 |
threadAssertTrue(q.isEmpty()); |
509 |
} |
510 |
catch (InterruptedException e) { |
511 |
threadUnexpectedException(); |
512 |
} |
513 |
} |
514 |
}); |
515 |
|
516 |
executor.execute(new Runnable() { |
517 |
public void run() { |
518 |
try { |
519 |
Thread.sleep(SMALL_DELAY_MS); |
520 |
q.put(new Integer(1)); |
521 |
} |
522 |
catch (InterruptedException e) { |
523 |
threadUnexpectedException(); |
524 |
} |
525 |
} |
526 |
}); |
527 |
|
528 |
joinPool(executor); |
529 |
} |
530 |
|
531 |
/** |
532 |
* a deserialized serialized queue is usable |
533 |
*/ |
534 |
public void testSerialization() { |
535 |
SynchronousQueue q = new SynchronousQueue(); |
536 |
try { |
537 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
538 |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
539 |
out.writeObject(q); |
540 |
out.close(); |
541 |
|
542 |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
543 |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
544 |
SynchronousQueue r = (SynchronousQueue)in.readObject(); |
545 |
assertEquals(q.size(), r.size()); |
546 |
while (!q.isEmpty()) |
547 |
assertEquals(q.remove(), r.remove()); |
548 |
} catch(Exception e){ |
549 |
unexpectedException(); |
550 |
} |
551 |
} |
552 |
|
553 |
} |