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 |
* |
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))); |
32 |
} |
33 |
|
34 |
/** |
35 |
* |
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 |
* |
47 |
*/ |
48 |
public void testOffer() { |
49 |
SynchronousQueue q = new SynchronousQueue(); |
50 |
assertFalse(q.offer(new Integer(1))); |
51 |
} |
52 |
|
53 |
/** |
54 |
* |
55 |
*/ |
56 |
public void testAdd() { |
57 |
try { |
58 |
SynchronousQueue q = new SynchronousQueue(); |
59 |
assertEquals(0, q.remainingCapacity()); |
60 |
q.add(new Integer(0)); |
61 |
} catch (IllegalStateException success){ |
62 |
} |
63 |
} |
64 |
|
65 |
/** |
66 |
* |
67 |
*/ |
68 |
public void testAddAll1() { |
69 |
try { |
70 |
SynchronousQueue q = new SynchronousQueue(); |
71 |
q.addAll(null); |
72 |
shouldThrow(); |
73 |
} |
74 |
catch (NullPointerException success) {} |
75 |
} |
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 |
shouldThrow(); |
85 |
} |
86 |
catch (NullPointerException success) {} |
87 |
} |
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 |
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 |
shouldThrow(); |
111 |
} |
112 |
catch (NullPointerException success){ |
113 |
} |
114 |
catch (InterruptedException ie) { |
115 |
unexpectedException(); |
116 |
} |
117 |
} |
118 |
|
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 |
threadShouldThrow(); |
129 |
} catch (InterruptedException ie){ |
130 |
} |
131 |
}}); |
132 |
t.start(); |
133 |
try { |
134 |
Thread.sleep(SHORT_DELAY_MS); |
135 |
t.interrupt(); |
136 |
t.join(); |
137 |
} |
138 |
catch (InterruptedException ie) { |
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() { |
150 |
int added = 0; |
151 |
try { |
152 |
q.put(new Object()); |
153 |
++added; |
154 |
q.put(new Object()); |
155 |
++added; |
156 |
q.put(new Object()); |
157 |
++added; |
158 |
q.put(new Object()); |
159 |
++added; |
160 |
threadShouldThrow(); |
161 |
} catch (InterruptedException e){ |
162 |
assertTrue(added >= 1); |
163 |
} |
164 |
} |
165 |
}); |
166 |
try { |
167 |
t.start(); |
168 |
Thread.sleep(SHORT_DELAY_MS); |
169 |
q.take(); |
170 |
Thread.sleep(SHORT_DELAY_MS); |
171 |
t.interrupt(); |
172 |
t.join(); |
173 |
} catch (Exception e){ |
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() { |
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 |
threadShouldThrow(); |
190 |
} catch (InterruptedException success){} |
191 |
} |
192 |
}); |
193 |
|
194 |
try { |
195 |
t.start(); |
196 |
Thread.sleep(SMALL_DELAY_MS); |
197 |
t.interrupt(); |
198 |
t.join(); |
199 |
} catch (Exception e){ |
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() { |
212 |
try { |
213 |
q.take(); |
214 |
threadShouldThrow(); |
215 |
} catch (InterruptedException success){ } |
216 |
} |
217 |
}); |
218 |
try { |
219 |
t.start(); |
220 |
Thread.sleep(SHORT_DELAY_MS); |
221 |
t.interrupt(); |
222 |
t.join(); |
223 |
} catch (Exception e){ |
224 |
unexpectedException(); |
225 |
} |
226 |
} |
227 |
|
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 |
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 |
unexpectedException(); |
257 |
} |
258 |
} |
259 |
|
260 |
/** |
261 |
* |
262 |
*/ |
263 |
public void testInterruptedTimedPoll() { |
264 |
Thread t = new Thread(new Runnable() { |
265 |
public void run() { |
266 |
try { |
267 |
SynchronousQueue q = new SynchronousQueue(); |
268 |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
269 |
} catch (InterruptedException success){ |
270 |
} |
271 |
}}); |
272 |
t.start(); |
273 |
try { |
274 |
Thread.sleep(SHORT_DELAY_MS); |
275 |
t.interrupt(); |
276 |
t.join(); |
277 |
} |
278 |
catch (InterruptedException ie) { |
279 |
unexpectedException(); |
280 |
} |
281 |
} |
282 |
|
283 |
/** |
284 |
* |
285 |
*/ |
286 |
public void testTimedPollWithOffer() { |
287 |
final SynchronousQueue q = new SynchronousQueue(); |
288 |
Thread t = new Thread(new Runnable() { |
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 |
threadShouldThrow(); |
295 |
} catch (InterruptedException success) { } |
296 |
} |
297 |
}); |
298 |
try { |
299 |
t.start(); |
300 |
Thread.sleep(SMALL_DELAY_MS); |
301 |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
302 |
t.interrupt(); |
303 |
t.join(); |
304 |
} catch (Exception e){ |
305 |
unexpectedException(); |
306 |
} |
307 |
} |
308 |
|
309 |
|
310 |
/** |
311 |
* |
312 |
*/ |
313 |
public void testPeek() { |
314 |
SynchronousQueue q = new SynchronousQueue(); |
315 |
assertNull(q.peek()); |
316 |
} |
317 |
|
318 |
/** |
319 |
* |
320 |
*/ |
321 |
public void testElement() { |
322 |
SynchronousQueue q = new SynchronousQueue(); |
323 |
try { |
324 |
q.element(); |
325 |
shouldThrow(); |
326 |
} |
327 |
catch (NoSuchElementException success) {} |
328 |
} |
329 |
|
330 |
/** |
331 |
* |
332 |
*/ |
333 |
public void testRemove() { |
334 |
SynchronousQueue q = new SynchronousQueue(); |
335 |
try { |
336 |
q.remove(); |
337 |
shouldThrow(); |
338 |
} catch (NoSuchElementException success){ |
339 |
} |
340 |
} |
341 |
|
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 |
/** |
352 |
* |
353 |
*/ |
354 |
public void testContains() { |
355 |
SynchronousQueue q = new SynchronousQueue(); |
356 |
assertFalse(q.contains(new Integer(0))); |
357 |
} |
358 |
|
359 |
/** |
360 |
* |
361 |
*/ |
362 |
public void testClear() { |
363 |
SynchronousQueue q = new SynchronousQueue(); |
364 |
q.clear(); |
365 |
assertTrue(q.isEmpty()); |
366 |
} |
367 |
|
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); |
375 |
assertFalse(q.containsAll(Arrays.asList(ints))); |
376 |
} |
377 |
|
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)); |
386 |
assertFalse(q.containsAll(Arrays.asList(ints))); |
387 |
} |
388 |
|
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)); |
397 |
assertFalse(q.containsAll(Arrays.asList(ints))); |
398 |
} |
399 |
|
400 |
|
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 |
/** |
411 |
* |
412 |
*/ |
413 |
public void testToArray2() { |
414 |
SynchronousQueue q = new SynchronousQueue(); |
415 |
Integer[] ints = new Integer[1]; |
416 |
assertNull(ints[0]); |
417 |
} |
418 |
|
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 |
shouldThrow(); |
429 |
} |
430 |
catch (NoSuchElementException success) {} |
431 |
} |
432 |
|
433 |
/** |
434 |
* |
435 |
*/ |
436 |
public void testIteratorRemove() { |
437 |
SynchronousQueue q = new SynchronousQueue(); |
438 |
Iterator it = q.iterator(); |
439 |
try { |
440 |
it.remove(); |
441 |
shouldThrow(); |
442 |
} |
443 |
catch (IllegalStateException success) {} |
444 |
} |
445 |
|
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); |
462 |
final Integer one = new Integer(1); |
463 |
|
464 |
executor.execute(new Runnable() { |
465 |
public void run() { |
466 |
threadAssertFalse(q.offer(one)); |
467 |
try { |
468 |
threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
469 |
threadAssertEquals(0, q.remainingCapacity()); |
470 |
} |
471 |
catch (InterruptedException e) { |
472 |
threadUnexpectedException(); |
473 |
} |
474 |
} |
475 |
}); |
476 |
|
477 |
executor.execute(new Runnable() { |
478 |
public void run() { |
479 |
try { |
480 |
Thread.sleep(SMALL_DELAY_MS); |
481 |
threadAssertEquals(one, q.take()); |
482 |
} |
483 |
catch (InterruptedException e) { |
484 |
threadUnexpectedException(); |
485 |
} |
486 |
} |
487 |
}); |
488 |
|
489 |
joinPool(executor); |
490 |
|
491 |
} |
492 |
|
493 |
/** |
494 |
* |
495 |
*/ |
496 |
public void testPollInExecutor() { |
497 |
|
498 |
final SynchronousQueue q = new SynchronousQueue(); |
499 |
|
500 |
ExecutorService executor = Executors.newFixedThreadPool(2); |
501 |
|
502 |
executor.execute(new Runnable() { |
503 |
public void run() { |
504 |
threadAssertNull(q.poll()); |
505 |
try { |
506 |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
507 |
threadAssertTrue(q.isEmpty()); |
508 |
} |
509 |
catch (InterruptedException e) { |
510 |
threadUnexpectedException(); |
511 |
} |
512 |
} |
513 |
}); |
514 |
|
515 |
executor.execute(new Runnable() { |
516 |
public void run() { |
517 |
try { |
518 |
Thread.sleep(SMALL_DELAY_MS); |
519 |
q.put(new Integer(1)); |
520 |
} |
521 |
catch (InterruptedException e) { |
522 |
threadUnexpectedException(); |
523 |
} |
524 |
} |
525 |
}); |
526 |
|
527 |
joinPool(executor); |
528 |
|
529 |
} |
530 |
|
531 |
/** |
532 |
* |
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 |
} |