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.8 by jsr166, Mon May 30 22:43:20 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.
8   */
9  
10   import junit.framework.*;
11 < import java.util.*;
12 < import java.util.concurrent.*;
11 > import java.util.Arrays;
12 > import java.util.Collection;
13 > import java.util.Queue;
14 > import java.util.concurrent.BlockingQueue;
15 > import java.util.concurrent.CountDownLatch;
16   import static java.util.concurrent.TimeUnit.MILLISECONDS;
17  
18   /**
19 < * Contains tests generally applicable to BlockingQueue implementations.
19 > * Contains "contract" tests applicable to all BlockingQueue implementations.
20   */
21   public abstract class BlockingQueueTest extends JSR166TestCase {
22      /*
# Line 33 | Line 36 | public abstract class BlockingQueueTest
36          return new TestSuite(this.getClass());
37      }
38  
39 +    //----------------------------------------------------------------
40 +    // Configuration methods
41 +    //----------------------------------------------------------------
42 +    
43      /** Returns an empty instance of the implementation class. */
44      protected abstract BlockingQueue emptyCollection();
45  
46      /**
47 <     * timed poll before a delayed offer fails; after offer succeeds;
47 >     * Returns an element suitable for insertion in the collection.
48 >     * Override for collections with unusual element types.
49 >     */
50 >    protected Object makeElement(int i) {
51 >        return Integer.valueOf(i);
52 >    }
53 >
54 >    //----------------------------------------------------------------
55 >    // Tests
56 >    //----------------------------------------------------------------
57 >    
58 >    /**
59 >     * offer(null) throws NullPointerException
60 >     */
61 >    public void testOfferNull() {
62 >        final Queue q = emptyCollection();
63 >        try {
64 >            q.offer(null);
65 >            shouldThrow();
66 >        } catch (NullPointerException success) {}
67 >    }
68 >
69 >    /**
70 >     * add(null) throws NullPointerException
71 >     */
72 >    public void testAddNull() {
73 >        final Collection q = emptyCollection();
74 >        try {
75 >            q.add(null);
76 >            shouldThrow();
77 >        } catch (NullPointerException success) {}
78 >    }
79 >
80 >    /**
81 >     * timed offer(null) throws NullPointerException
82 >     */
83 >    public void testTimedOfferNull() throws InterruptedException {
84 >        final BlockingQueue q = emptyCollection();
85 >        long startTime = System.nanoTime();
86 >        try {
87 >            q.offer(null, LONG_DELAY_MS, MILLISECONDS);
88 >            shouldThrow();
89 >        } catch (NullPointerException success) {}
90 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
91 >    }
92 >
93 >    /**
94 >     * put(null) throws NullPointerException
95 >     */
96 >    public void testPutNull() throws InterruptedException {
97 >        final BlockingQueue q = emptyCollection();
98 >        try {
99 >            q.put(null);
100 >            shouldThrow();
101 >        } catch (NullPointerException success) {}
102 >    }
103 >
104 >    /**
105 >     * put(null) throws NullPointerException
106 >     */
107 >    public void testAddAllNull() throws InterruptedException {
108 >        final Collection q = emptyCollection();
109 >        try {
110 >            q.addAll(null);
111 >            shouldThrow();
112 >        } catch (NullPointerException success) {}
113 >    }
114 >
115 >    /**
116 >     * addAll of a collection with null elements throws NullPointerException
117 >     */
118 >    public void testAddAllNullElements() {
119 >        final Collection q = emptyCollection();
120 >        final Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
121 >        try {
122 >            q.addAll(elements);
123 >            shouldThrow();
124 >        } catch (NullPointerException success) {}
125 >    }
126 >
127 >    /**
128 >     * toArray(null) throws NullPointerException
129 >     */
130 >    public void testToArray_NullArray() {
131 >        final Collection q = emptyCollection();
132 >        try {
133 >            q.toArray(null);
134 >            shouldThrow();
135 >        } catch (NullPointerException success) {}
136 >    }
137 >
138 >    /**
139 >     * drainTo(null) throws NullPointerException
140 >     */
141 >    public void testDrainToNull() {
142 >        final BlockingQueue q = emptyCollection();
143 >        try {
144 >            q.drainTo(null);
145 >            shouldThrow();
146 >        } catch (NullPointerException success) {}
147 >    }
148 >
149 >    /**
150 >     * drainTo(this) throws IllegalArgumentException
151 >     */
152 >    public void testDrainToSelf() {
153 >        final BlockingQueue q = emptyCollection();
154 >        try {
155 >            q.drainTo(q);
156 >            shouldThrow();
157 >        } catch (IllegalArgumentException success) {}
158 >    }
159 >
160 >    /**
161 >     * drainTo(null, n) throws NullPointerException
162 >     */
163 >    public void testDrainToNullN() {
164 >        final BlockingQueue q = emptyCollection();
165 >        try {
166 >            q.drainTo(null, 0);
167 >            shouldThrow();
168 >        } catch (NullPointerException success) {}
169 >    }
170 >
171 >    /**
172 >     * drainTo(this, n) throws IllegalArgumentException
173 >     */
174 >    public void testDrainToSelfN() {
175 >        final BlockingQueue q = emptyCollection();
176 >        try {
177 >            q.drainTo(q, 0);
178 >            shouldThrow();
179 >        } catch (IllegalArgumentException success) {}
180 >    }
181 >
182 >    /**
183 >     * timed poll before a delayed offer times out; after offer succeeds;
184       * on interruption throws
185       */
186      public void testTimedPollWithOffer() throws InterruptedException {
187          final BlockingQueue q = emptyCollection();
188          final CheckedBarrier barrier = new CheckedBarrier(2);
189 +        final Object zero = makeElement(0);
190          Thread t = newStartedThread(new CheckedRunnable() {
191              public void realRun() throws InterruptedException {
192 <                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
192 >                long startTime = System.nanoTime();
193 >                assertNull(q.poll(timeoutMillis(), MILLISECONDS));
194 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
195  
196                  barrier.await();
197 <                assertSame(zero, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
197 >
198 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
199  
200                  Thread.currentThread().interrupt();
201                  try {
202 <                    q.poll(SHORT_DELAY_MS, MILLISECONDS);
202 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
203                      shouldThrow();
204                  } catch (InterruptedException success) {}
205 +                assertFalse(Thread.interrupted());
206  
207                  barrier.await();
208                  try {
209 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
209 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
210                      shouldThrow();
211                  } catch (InterruptedException success) {}
212 +                assertFalse(Thread.interrupted());
213              }});
214  
215          barrier.await();
216 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
216 >        long startTime = System.nanoTime();
217 >        assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS));
218 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
219 >
220          barrier.await();
221 <        sleep(SHORT_DELAY_MS);
221 >        assertThreadStaysAlive(t);
222 >        t.interrupt();
223 >        awaitTermination(t);
224 >    }
225 >
226 >    /**
227 >     * take() blocks interruptibly when empty
228 >     */
229 >    public void testTakeFromEmptyBlocksInterruptibly() {
230 >        final BlockingQueue q = emptyCollection();
231 >        final CountDownLatch threadStarted = new CountDownLatch(1);
232 >        Thread t = newStartedThread(new CheckedRunnable() {
233 >            public void realRun() {
234 >                threadStarted.countDown();
235 >                try {
236 >                    q.take();
237 >                    shouldThrow();
238 >                } catch (InterruptedException success) {}
239 >                assertFalse(Thread.interrupted());
240 >            }});
241 >
242 >        await(threadStarted);
243 >        assertThreadStaysAlive(t);
244 >        t.interrupt();
245 >        awaitTermination(t);
246 >    }
247 >
248 >    /**
249 >     * take() throws InterruptedException immediately if interrupted
250 >     * before waiting
251 >     */
252 >    public void testTakeFromEmptyAfterInterrupt() {
253 >        final BlockingQueue q = emptyCollection();
254 >        Thread t = newStartedThread(new CheckedRunnable() {
255 >            public void realRun() {
256 >                Thread.currentThread().interrupt();
257 >                try {
258 >                    q.take();
259 >                    shouldThrow();
260 >                } catch (InterruptedException success) {}
261 >                assertFalse(Thread.interrupted());
262 >            }});
263 >
264 >        awaitTermination(t);
265 >    }
266 >
267 >    /**
268 >     * timed poll() blocks interruptibly when empty
269 >     */
270 >    public void testTimedPollFromEmptyBlocksInterruptibly() {
271 >        final BlockingQueue q = emptyCollection();
272 >        final CountDownLatch threadStarted = new CountDownLatch(1);
273 >        Thread t = newStartedThread(new CheckedRunnable() {
274 >            public void realRun() {
275 >                threadStarted.countDown();
276 >                try {
277 >                    q.poll(2 * LONG_DELAY_MS, MILLISECONDS);
278 >                    shouldThrow();
279 >                } catch (InterruptedException success) {}
280 >                assertFalse(Thread.interrupted());
281 >            }});
282 >
283 >        await(threadStarted);
284 >        assertThreadStaysAlive(t);
285          t.interrupt();
286 <        awaitTermination(t, MEDIUM_DELAY_MS);
286 >        awaitTermination(t);
287 >    }
288 >
289 >    /**
290 >     * timed poll() throws InterruptedException immediately if
291 >     * interrupted before waiting
292 >     */
293 >    public void testTimedPollFromEmptyAfterInterrupt() {
294 >        final BlockingQueue q = emptyCollection();
295 >        Thread t = newStartedThread(new CheckedRunnable() {
296 >            public void realRun() {
297 >                Thread.currentThread().interrupt();
298 >                try {
299 >                    q.poll(2 * LONG_DELAY_MS, MILLISECONDS);
300 >                    shouldThrow();
301 >                } catch (InterruptedException success) {}
302 >                assertFalse(Thread.interrupted());
303 >            }});
304 >
305 >        awaitTermination(t);
306      }
307  
308      /** For debugging. */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines