ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/BlockingQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/BlockingQueueTest.java (file contents):
Revision 1.1 by jsr166, Wed Oct 6 07:49:22 2010 UTC vs.
Revision 1.7 by jsr166, Fri May 27 19:32:04 2011 UTC

# Line 1 | Line 1
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/licenses/publicdomain
4 > * explained at http://creativecommons.org/publicdomain/zero/1.0/
5   *
6   * Other contributors include Andrew Wright, Jeffrey Hayes,
7   * Pat Fisher, Mike Judd.
# Line 37 | Line 37 | public abstract class BlockingQueueTest
37      protected abstract BlockingQueue emptyCollection();
38  
39      /**
40 <     * timed poll before a delayed offer fails; after offer succeeds;
40 >     * timed poll before a delayed offer times out; after offer succeeds;
41       * on interruption throws
42       */
43      public void testTimedPollWithOffer() throws InterruptedException {
# Line 45 | Line 45 | public abstract class BlockingQueueTest
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));
48 >                long startTime = System.nanoTime();
49 >                assertNull(q.poll(timeoutMillis(), MILLISECONDS));
50 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
51  
52                  barrier.await();
53 <                assertSame(zero, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
53 >
54 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
55  
56                  Thread.currentThread().interrupt();
57                  try {
58 <                    q.poll(SHORT_DELAY_MS, MILLISECONDS);
58 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
59                      shouldThrow();
60                  } catch (InterruptedException success) {}
61 +                assertFalse(Thread.interrupted());
62  
63                  barrier.await();
64                  try {
65 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
65 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
66                      shouldThrow();
67                  } catch (InterruptedException success) {}
68 +                assertFalse(Thread.interrupted());
69              }});
70  
71          barrier.await();
72 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
72 >        long startTime = System.nanoTime();
73 >        assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS));
74 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
75 >
76          barrier.await();
77 <        sleep(SHORT_DELAY_MS);
77 >        assertThreadStaysAlive(t);
78 >        t.interrupt();
79 >        awaitTermination(t);
80 >    }
81 >
82 >    /**
83 >     * take() blocks interruptibly when empty
84 >     */
85 >    public void testTakeFromEmptyBlocksInterruptibly() {
86 >        final BlockingQueue q = emptyCollection();
87 >        final CountDownLatch threadStarted = new CountDownLatch(1);
88 >        Thread t = newStartedThread(new CheckedRunnable() {
89 >            public void realRun() {
90 >                threadStarted.countDown();
91 >                try {
92 >                    q.take();
93 >                    shouldThrow();
94 >                } catch (InterruptedException success) {}
95 >                assertFalse(Thread.interrupted());
96 >            }});
97 >
98 >        await(threadStarted);
99 >        assertThreadStaysAlive(t);
100 >        t.interrupt();
101 >        awaitTermination(t);
102 >    }
103 >
104 >    /**
105 >     * take() throws InterruptedException immediately if interrupted
106 >     * before waiting
107 >     */
108 >    public void testTakeFromEmptyAfterInterrupt() {
109 >        final BlockingQueue q = emptyCollection();
110 >        Thread t = newStartedThread(new CheckedRunnable() {
111 >            public void realRun() {
112 >                Thread.currentThread().interrupt();
113 >                try {
114 >                    q.take();
115 >                    shouldThrow();
116 >                } catch (InterruptedException success) {}
117 >                assertFalse(Thread.interrupted());
118 >            }});
119 >
120 >        awaitTermination(t);
121 >    }
122 >
123 >    /**
124 >     * timed poll() blocks interruptibly when empty
125 >     */
126 >    public void testTimedPollFromEmptyBlocksInterruptibly() {
127 >        final BlockingQueue q = emptyCollection();
128 >        final CountDownLatch threadStarted = new CountDownLatch(1);
129 >        Thread t = newStartedThread(new CheckedRunnable() {
130 >            public void realRun() {
131 >                threadStarted.countDown();
132 >                try {
133 >                    q.poll(2 * LONG_DELAY_MS, MILLISECONDS);
134 >                    shouldThrow();
135 >                } catch (InterruptedException success) {}
136 >                assertFalse(Thread.interrupted());
137 >            }});
138 >
139 >        await(threadStarted);
140 >        assertThreadStaysAlive(t);
141          t.interrupt();
142 <        awaitTermination(t, MEDIUM_DELAY_MS);
142 >        awaitTermination(t);
143 >    }
144 >
145 >    /**
146 >     * timed poll() throws InterruptedException immediately if
147 >     * interrupted before waiting
148 >     */
149 >    public void testTimedPollFromEmptyAfterInterrupt() {
150 >        final BlockingQueue q = emptyCollection();
151 >        Thread t = newStartedThread(new CheckedRunnable() {
152 >            public void realRun() {
153 >                Thread.currentThread().interrupt();
154 >                try {
155 >                    q.poll(2 * LONG_DELAY_MS, MILLISECONDS);
156 >                    shouldThrow();
157 >                } catch (InterruptedException success) {}
158 >                assertFalse(Thread.interrupted());
159 >            }});
160 >
161 >        awaitTermination(t);
162      }
163  
164      /** For debugging. */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines