ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/BlockingQueueTest.java
Revision: 1.6
Committed: Fri May 6 11:22:07 2011 UTC (13 years ago) by dl
Branch: MAIN
CVS Tags: release-1_7_0
Changes since 1.5: +1 -1 lines
Log Message:
Add/use delay() instead of Thread.sleep to ensure sleeps are long enough

File Contents

# Content
1 /*
2 * Written by Doug Lea and Martin Buchholz with assistance from members
3 * of JCP JSR-166 Expert Group and released to the public domain, as
4 * explained at http://creativecommons.org/publicdomain/zero/1.0/
5 *
6 * Other contributors include Andrew Wright, Jeffrey Hayes,
7 * Pat Fisher, Mike Judd.
8 */
9
10 import junit.framework.*;
11 import java.util.*;
12 import java.util.concurrent.*;
13 import static java.util.concurrent.TimeUnit.MILLISECONDS;
14
15 /**
16 * Contains tests generally applicable to BlockingQueue implementations.
17 */
18 public abstract class BlockingQueueTest extends JSR166TestCase {
19 /*
20 * This is the start of an attempt to refactor the tests for the
21 * various related implementations of related interfaces without
22 * too much duplicated code. junit does not really support such
23 * testing. Here subclasses of TestCase not only contain tests,
24 * but also configuration information that describes the
25 * implementation class, most importantly how to instantiate
26 * instances.
27 */
28
29 /** Like suite(), but non-static */
30 public Test testSuite() {
31 // TODO: filter the returned tests using the configuration
32 // information provided by the subclass via protected methods.
33 return new TestSuite(this.getClass());
34 }
35
36 /** Returns an empty instance of the implementation class. */
37 protected abstract BlockingQueue emptyCollection();
38
39 /**
40 * timed poll before a delayed offer fails; after offer succeeds;
41 * on interruption throws
42 */
43 public void testTimedPollWithOffer() throws InterruptedException {
44 final BlockingQueue q = emptyCollection();
45 final CheckedBarrier barrier = new CheckedBarrier(2);
46 Thread t = newStartedThread(new CheckedRunnable() {
47 public void realRun() throws InterruptedException {
48 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
49
50 barrier.await();
51 assertSame(zero, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
52
53 Thread.currentThread().interrupt();
54 try {
55 q.poll(SHORT_DELAY_MS, MILLISECONDS);
56 shouldThrow();
57 } catch (InterruptedException success) {}
58
59 barrier.await();
60 try {
61 q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
62 shouldThrow();
63 } catch (InterruptedException success) {}
64 }});
65
66 barrier.await();
67 assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
68 barrier.await();
69 sleep(SHORT_DELAY_MS);
70 t.interrupt();
71 awaitTermination(t, MEDIUM_DELAY_MS);
72 }
73
74 /**
75 * take() blocks interruptibly when empty
76 */
77 public void testTakeFromEmptyBlocksInterruptibly()
78 throws InterruptedException {
79 final BlockingQueue q = emptyCollection();
80 final CountDownLatch threadStarted = new CountDownLatch(1);
81 Thread t = newStartedThread(new CheckedRunnable() {
82 public void realRun() {
83 long t0 = System.nanoTime();
84 threadStarted.countDown();
85 try {
86 q.take();
87 shouldThrow();
88 } catch (InterruptedException success) {}
89 assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
90 }});
91
92 threadStarted.await();
93 delay(SHORT_DELAY_MS);
94 assertTrue(t.isAlive());
95 t.interrupt();
96 awaitTermination(t, MEDIUM_DELAY_MS);
97 }
98
99 /**
100 * take() throws InterruptedException immediately if interrupted
101 * before waiting
102 */
103 public void testTakeFromEmptyAfterInterrupt()
104 throws InterruptedException {
105 final BlockingQueue q = emptyCollection();
106 Thread t = newStartedThread(new CheckedRunnable() {
107 public void realRun() {
108 long t0 = System.nanoTime();
109 Thread.currentThread().interrupt();
110 try {
111 q.take();
112 shouldThrow();
113 } catch (InterruptedException success) {}
114 assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
115 }});
116
117 awaitTermination(t, MEDIUM_DELAY_MS);
118 }
119
120 /** For debugging. */
121 public void XXXXtestFails() {
122 fail(emptyCollection().getClass().toString());
123 }
124
125 }