1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
14 |
|
|
15 |
|
public class SynchronousQueueTest extends JSR166TestCase { |
16 |
|
|
17 |
+ |
public static class Fair extends BlockingQueueTest { |
18 |
+ |
protected BlockingQueue emptyCollection() { |
19 |
+ |
return new SynchronousQueue(true); |
20 |
+ |
} |
21 |
+ |
} |
22 |
+ |
|
23 |
+ |
public static class NonFair extends BlockingQueueTest { |
24 |
+ |
protected BlockingQueue emptyCollection() { |
25 |
+ |
return new SynchronousQueue(false); |
26 |
+ |
} |
27 |
+ |
} |
28 |
+ |
|
29 |
|
public static void main(String[] args) { |
30 |
< |
junit.textui.TestRunner.run (suite()); |
30 |
> |
junit.textui.TestRunner.run(suite()); |
31 |
|
} |
32 |
|
|
33 |
|
public static Test suite() { |
34 |
< |
return new TestSuite(SynchronousQueueTest.class); |
34 |
> |
return newTestSuite(SynchronousQueueTest.class, |
35 |
> |
new Fair().testSuite(), |
36 |
> |
new NonFair().testSuite()); |
37 |
|
} |
38 |
|
|
39 |
|
/** |
40 |
< |
* A SynchronousQueue is both empty and full |
40 |
> |
* Any SynchronousQueue is both empty and full |
41 |
|
*/ |
42 |
< |
public void testEmptyFull() { |
29 |
< |
SynchronousQueue q = new SynchronousQueue(); |
42 |
> |
public void testEmptyFull(SynchronousQueue q) { |
43 |
|
assertTrue(q.isEmpty()); |
44 |
|
assertEquals(0, q.size()); |
45 |
|
assertEquals(0, q.remainingCapacity()); |
47 |
|
} |
48 |
|
|
49 |
|
/** |
50 |
+ |
* A non-fair SynchronousQueue is both empty and full |
51 |
+ |
*/ |
52 |
+ |
public void testEmptyFull() { |
53 |
+ |
testEmptyFull(new SynchronousQueue()); |
54 |
+ |
} |
55 |
+ |
|
56 |
+ |
/** |
57 |
|
* A fair SynchronousQueue is both empty and full |
58 |
|
*/ |
59 |
|
public void testFairEmptyFull() { |
60 |
< |
SynchronousQueue q = new SynchronousQueue(true); |
41 |
< |
assertTrue(q.isEmpty()); |
42 |
< |
assertEquals(0, q.size()); |
43 |
< |
assertEquals(0, q.remainingCapacity()); |
44 |
< |
assertFalse(q.offer(zero)); |
60 |
> |
testEmptyFull(new SynchronousQueue(true)); |
61 |
|
} |
62 |
|
|
63 |
|
/** |
135 |
|
shouldThrow(); |
136 |
|
} catch (NullPointerException success) {} |
137 |
|
} |
138 |
+ |
|
139 |
|
/** |
140 |
|
* addAll throws ISE if no active taker |
141 |
|
*/ |
159 |
|
q.put(null); |
160 |
|
shouldThrow(); |
161 |
|
} catch (NullPointerException success) {} |
162 |
< |
} |
162 |
> |
} |
163 |
|
|
164 |
|
/** |
165 |
|
* put blocks interruptibly if no active taker |
172 |
|
}}); |
173 |
|
|
174 |
|
t.start(); |
175 |
< |
Thread.sleep(SHORT_DELAY_MS); |
175 |
> |
delay(SHORT_DELAY_MS); |
176 |
|
t.interrupt(); |
177 |
|
t.join(); |
178 |
|
} |
186 |
|
public void realRun() throws InterruptedException { |
187 |
|
int added = 0; |
188 |
|
try { |
189 |
< |
q.put(new Object()); |
190 |
< |
++added; |
191 |
< |
q.put(new Object()); |
192 |
< |
++added; |
176 |
< |
q.put(new Object()); |
177 |
< |
++added; |
178 |
< |
q.put(new Object()); |
179 |
< |
++added; |
180 |
< |
threadShouldThrow(); |
189 |
> |
while (true) { |
190 |
> |
q.put(added); |
191 |
> |
++added; |
192 |
> |
} |
193 |
|
} catch (InterruptedException success) { |
194 |
< |
assertTrue(added >= 1); |
194 |
> |
assertEquals(1, added); |
195 |
|
} |
196 |
|
}}); |
197 |
|
|
198 |
|
t.start(); |
199 |
< |
Thread.sleep(SHORT_DELAY_MS); |
200 |
< |
q.take(); |
201 |
< |
Thread.sleep(SHORT_DELAY_MS); |
199 |
> |
delay(SHORT_DELAY_MS); |
200 |
> |
assertEquals(0, q.take()); |
201 |
> |
delay(SHORT_DELAY_MS); |
202 |
|
t.interrupt(); |
203 |
|
t.join(); |
204 |
|
} |
206 |
|
/** |
207 |
|
* timed offer times out if elements not taken |
208 |
|
*/ |
209 |
< |
public void testTimedOffer() throws InterruptedException { |
210 |
< |
final SynchronousQueue q = new SynchronousQueue(); |
211 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
212 |
< |
public void realRun() throws InterruptedException { |
213 |
< |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
214 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
209 |
> |
public void testTimedOffer(final SynchronousQueue q) |
210 |
> |
throws InterruptedException { |
211 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
212 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
213 |
> |
public void realRun() throws InterruptedException { |
214 |
> |
long startTime = System.nanoTime(); |
215 |
> |
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
216 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
217 |
> |
pleaseInterrupt.countDown(); |
218 |
> |
try { |
219 |
> |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
220 |
> |
shouldThrow(); |
221 |
> |
} catch (InterruptedException success) {} |
222 |
|
}}); |
223 |
|
|
224 |
< |
t.start(); |
206 |
< |
Thread.sleep(SMALL_DELAY_MS); |
224 |
> |
await(pleaseInterrupt); |
225 |
|
t.interrupt(); |
226 |
< |
t.join(); |
226 |
> |
awaitTermination(t); |
227 |
|
} |
228 |
|
|
211 |
– |
|
229 |
|
/** |
230 |
< |
* take blocks interruptibly when empty |
230 |
> |
* timed offer times out if elements not taken |
231 |
|
*/ |
232 |
< |
public void testTakeFromEmpty() throws InterruptedException { |
233 |
< |
final SynchronousQueue q = new SynchronousQueue(); |
217 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
218 |
< |
public void realRun() throws InterruptedException { |
219 |
< |
q.take(); |
220 |
< |
}}); |
221 |
< |
|
222 |
< |
t.start(); |
223 |
< |
Thread.sleep(SHORT_DELAY_MS); |
224 |
< |
t.interrupt(); |
225 |
< |
t.join(); |
232 |
> |
public void testTimedOffer() throws InterruptedException { |
233 |
> |
testTimedOffer(new SynchronousQueue()); |
234 |
|
} |
235 |
|
|
236 |
+ |
/** |
237 |
+ |
* timed offer times out if elements not taken |
238 |
+ |
*/ |
239 |
+ |
public void testFairTimedOffer() throws InterruptedException { |
240 |
+ |
testTimedOffer(new SynchronousQueue(true)); |
241 |
+ |
} |
242 |
|
|
243 |
|
/** |
244 |
|
* put blocks interruptibly if no active taker |
251 |
|
}}); |
252 |
|
|
253 |
|
t.start(); |
254 |
< |
Thread.sleep(SHORT_DELAY_MS); |
254 |
> |
delay(SHORT_DELAY_MS); |
255 |
|
t.interrupt(); |
256 |
|
t.join(); |
257 |
|
} |
265 |
|
public void realRun() throws InterruptedException { |
266 |
|
int added = 0; |
267 |
|
try { |
268 |
< |
q.put(new Object()); |
269 |
< |
++added; |
270 |
< |
q.put(new Object()); |
271 |
< |
++added; |
258 |
< |
q.put(new Object()); |
259 |
< |
++added; |
260 |
< |
q.put(new Object()); |
261 |
< |
++added; |
262 |
< |
threadShouldThrow(); |
268 |
> |
while (true) { |
269 |
> |
q.put(added); |
270 |
> |
++added; |
271 |
> |
} |
272 |
|
} catch (InterruptedException success) { |
273 |
< |
assertTrue(added >= 1); |
273 |
> |
assertEquals(1, added); |
274 |
|
} |
275 |
|
}}); |
276 |
|
|
277 |
|
t.start(); |
278 |
< |
Thread.sleep(SHORT_DELAY_MS); |
279 |
< |
q.take(); |
280 |
< |
Thread.sleep(SHORT_DELAY_MS); |
272 |
< |
t.interrupt(); |
273 |
< |
t.join(); |
274 |
< |
} |
275 |
< |
|
276 |
< |
/** |
277 |
< |
* timed offer times out if elements not taken |
278 |
< |
*/ |
279 |
< |
public void testFairTimedOffer() throws InterruptedException { |
280 |
< |
final SynchronousQueue q = new SynchronousQueue(true); |
281 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
282 |
< |
public void realRun() throws InterruptedException { |
283 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
284 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
285 |
< |
}}); |
286 |
< |
|
287 |
< |
t.start(); |
288 |
< |
Thread.sleep(SMALL_DELAY_MS); |
278 |
> |
delay(SHORT_DELAY_MS); |
279 |
> |
assertEquals(0, q.take()); |
280 |
> |
delay(SHORT_DELAY_MS); |
281 |
|
t.interrupt(); |
282 |
|
t.join(); |
283 |
|
} |
284 |
|
|
293 |
– |
|
285 |
|
/** |
286 |
|
* take blocks interruptibly when empty |
287 |
|
*/ |
293 |
|
}}); |
294 |
|
|
295 |
|
t.start(); |
296 |
< |
Thread.sleep(SHORT_DELAY_MS); |
296 |
> |
delay(SHORT_DELAY_MS); |
297 |
|
t.interrupt(); |
298 |
|
t.join(); |
299 |
|
} |
300 |
|
|
301 |
|
/** |
302 |
< |
* poll fails unless active taker |
302 |
> |
* poll return null if no active putter |
303 |
|
*/ |
304 |
|
public void testPoll() { |
305 |
|
SynchronousQueue q = new SynchronousQueue(); |
307 |
|
} |
308 |
|
|
309 |
|
/** |
310 |
< |
* timed pool with zero timeout times out if no active taker |
310 |
> |
* timed poll with zero timeout times out if no active putter |
311 |
|
*/ |
312 |
|
public void testTimedPoll0() throws InterruptedException { |
313 |
|
SynchronousQueue q = new SynchronousQueue(); |
315 |
|
} |
316 |
|
|
317 |
|
/** |
318 |
< |
* timed pool with nonzero timeout times out if no active taker |
318 |
> |
* timed poll with nonzero timeout times out if no active putter |
319 |
|
*/ |
320 |
|
public void testTimedPoll() throws InterruptedException { |
321 |
|
SynchronousQueue q = new SynchronousQueue(); |
322 |
+ |
long t0 = System.nanoTime(); |
323 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
324 |
+ |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
325 |
|
} |
326 |
|
|
327 |
|
/** |
328 |
|
* Interrupted timed poll throws InterruptedException instead of |
329 |
|
* returning timeout status |
330 |
|
*/ |
331 |
< |
public void testInterruptedTimedPoll() throws InterruptedException { |
332 |
< |
final SynchronousQueue q = new SynchronousQueue(); |
333 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
334 |
< |
public void realRun() throws InterruptedException { |
342 |
< |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
343 |
< |
}}); |
344 |
< |
|
345 |
< |
t.start(); |
346 |
< |
Thread.sleep(SHORT_DELAY_MS); |
347 |
< |
t.interrupt(); |
348 |
< |
t.join(); |
349 |
< |
} |
350 |
< |
|
351 |
< |
/** |
352 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
353 |
< |
* on interruption throws |
354 |
< |
*/ |
355 |
< |
public void testTimedPollWithOffer() throws InterruptedException { |
356 |
< |
final SynchronousQueue q = new SynchronousQueue(); |
357 |
< |
Thread t = new Thread(new CheckedRunnable() { |
331 |
> |
public void testInterruptedTimedPoll(final SynchronousQueue q) |
332 |
> |
throws InterruptedException { |
333 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
334 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
335 |
|
public void realRun() throws InterruptedException { |
336 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
337 |
< |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
336 |
> |
long t0 = System.nanoTime(); |
337 |
> |
threadStarted.countDown(); |
338 |
|
try { |
339 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
340 |
|
shouldThrow(); |
341 |
|
} catch (InterruptedException success) {} |
342 |
+ |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
343 |
+ |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
344 |
|
}}); |
345 |
|
|
346 |
< |
t.start(); |
347 |
< |
Thread.sleep(SMALL_DELAY_MS); |
369 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
346 |
> |
threadStarted.await(); |
347 |
> |
delay(SHORT_DELAY_MS); |
348 |
|
t.interrupt(); |
349 |
< |
t.join(); |
349 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
350 |
|
} |
351 |
|
|
352 |
|
/** |
353 |
|
* Interrupted timed poll throws InterruptedException instead of |
354 |
|
* returning timeout status |
355 |
|
*/ |
356 |
< |
public void testFairInterruptedTimedPoll() throws InterruptedException { |
357 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
358 |
< |
public void realRun() throws InterruptedException { |
381 |
< |
SynchronousQueue q = new SynchronousQueue(true); |
382 |
< |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
383 |
< |
}}); |
356 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
357 |
> |
testInterruptedTimedPoll(new SynchronousQueue()); |
358 |
> |
} |
359 |
|
|
360 |
< |
t.start(); |
361 |
< |
Thread.sleep(SHORT_DELAY_MS); |
362 |
< |
t.interrupt(); |
363 |
< |
t.join(); |
360 |
> |
/** |
361 |
> |
* Interrupted timed poll throws InterruptedException instead of |
362 |
> |
* returning timeout status |
363 |
> |
*/ |
364 |
> |
public void testFairInterruptedTimedPoll() throws InterruptedException { |
365 |
> |
testInterruptedTimedPoll(new SynchronousQueue(true)); |
366 |
|
} |
367 |
|
|
368 |
|
/** |
369 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
370 |
< |
* on interruption throws |
369 |
> |
* timed poll before a delayed offer times out, returning null; |
370 |
> |
* after offer succeeds; on interruption throws |
371 |
|
*/ |
372 |
|
public void testFairTimedPollWithOffer() throws InterruptedException { |
373 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
374 |
< |
Thread t = new Thread(new CheckedRunnable() { |
374 |
> |
final CountDownLatch pleaseOffer = new CountDownLatch(1); |
375 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
376 |
|
public void realRun() throws InterruptedException { |
377 |
+ |
long t0 = System.nanoTime(); |
378 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
379 |
+ |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
380 |
+ |
|
381 |
+ |
pleaseOffer.countDown(); |
382 |
+ |
t0 = System.nanoTime(); |
383 |
|
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
384 |
+ |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
385 |
+ |
|
386 |
+ |
t0 = System.nanoTime(); |
387 |
|
try { |
388 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
389 |
< |
threadShouldThrow(); |
389 |
> |
shouldThrow(); |
390 |
|
} catch (InterruptedException success) {} |
391 |
+ |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
392 |
|
}}); |
393 |
|
|
394 |
< |
t.start(); |
395 |
< |
Thread.sleep(SMALL_DELAY_MS); |
396 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
394 |
> |
assertTrue(pleaseOffer.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
395 |
> |
long t0 = System.nanoTime(); |
396 |
> |
assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); |
397 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
398 |
> |
|
399 |
|
t.interrupt(); |
400 |
< |
t.join(); |
400 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
401 |
|
} |
402 |
|
|
414 |
– |
|
403 |
|
/** |
404 |
< |
* peek returns null |
404 |
> |
* peek() returns null if no active putter |
405 |
|
*/ |
406 |
|
public void testPeek() { |
407 |
|
SynchronousQueue q = new SynchronousQueue(); |
409 |
|
} |
410 |
|
|
411 |
|
/** |
412 |
< |
* element throws NSEE |
412 |
> |
* element() throws NSEE if no active putter |
413 |
|
*/ |
414 |
|
public void testElement() { |
415 |
|
SynchronousQueue q = new SynchronousQueue(); |
420 |
|
} |
421 |
|
|
422 |
|
/** |
423 |
< |
* remove throws NSEE if no active taker |
423 |
> |
* remove() throws NSEE if no active putter |
424 |
|
*/ |
425 |
|
public void testRemove() { |
426 |
|
SynchronousQueue q = new SynchronousQueue(); |
561 |
|
public void testOfferInExecutor() { |
562 |
|
final SynchronousQueue q = new SynchronousQueue(); |
563 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
576 |
– |
final Integer one = new Integer(1); |
564 |
|
|
565 |
|
executor.execute(new CheckedRunnable() { |
566 |
|
public void realRun() throws InterruptedException { |
567 |
< |
threadAssertFalse(q.offer(one)); |
568 |
< |
threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); |
569 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
567 |
> |
assertFalse(q.offer(one)); |
568 |
> |
assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); |
569 |
> |
assertEquals(0, q.remainingCapacity()); |
570 |
|
}}); |
571 |
|
|
572 |
|
executor.execute(new CheckedRunnable() { |
573 |
|
public void realRun() throws InterruptedException { |
574 |
< |
Thread.sleep(SMALL_DELAY_MS); |
575 |
< |
threadAssertEquals(one, q.take()); |
574 |
> |
delay(SMALL_DELAY_MS); |
575 |
> |
assertSame(one, q.take()); |
576 |
|
}}); |
577 |
|
|
578 |
|
joinPool(executor); |
592 |
– |
|
579 |
|
} |
580 |
|
|
581 |
|
/** |
586 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
587 |
|
executor.execute(new CheckedRunnable() { |
588 |
|
public void realRun() throws InterruptedException { |
589 |
< |
threadAssertNull(q.poll()); |
590 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
591 |
< |
threadAssertTrue(q.isEmpty()); |
589 |
> |
assertNull(q.poll()); |
590 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
591 |
> |
assertTrue(q.isEmpty()); |
592 |
|
}}); |
593 |
|
|
594 |
|
executor.execute(new CheckedRunnable() { |
595 |
|
public void realRun() throws InterruptedException { |
596 |
< |
Thread.sleep(SMALL_DELAY_MS); |
597 |
< |
q.put(new Integer(1)); |
596 |
> |
delay(SHORT_DELAY_MS); |
597 |
> |
q.put(one); |
598 |
|
}}); |
599 |
|
|
600 |
|
joinPool(executor); |
663 |
|
|
664 |
|
t.start(); |
665 |
|
ArrayList l = new ArrayList(); |
666 |
< |
Thread.sleep(SHORT_DELAY_MS); |
666 |
> |
delay(SHORT_DELAY_MS); |
667 |
|
q.drainTo(l); |
668 |
|
assertTrue(l.size() <= 1); |
669 |
|
if (l.size() > 0) |
712 |
|
t1.start(); |
713 |
|
t2.start(); |
714 |
|
ArrayList l = new ArrayList(); |
715 |
< |
Thread.sleep(SHORT_DELAY_MS); |
715 |
> |
delay(SHORT_DELAY_MS); |
716 |
|
q.drainTo(l, 1); |
717 |
< |
assertTrue(l.size() == 1); |
717 |
> |
assertEquals(1, l.size()); |
718 |
|
q.drainTo(l, 1); |
719 |
< |
assertTrue(l.size() == 2); |
719 |
> |
assertEquals(2, l.size()); |
720 |
|
assertTrue(l.contains(one)); |
721 |
|
assertTrue(l.contains(two)); |
722 |
|
t1.join(); |