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

Comparing jsr166/src/test/tck/SynchronousQueueTest.java (file contents):
Revision 1.14 by jsr166, Sat Nov 21 02:07:27 2009 UTC vs.
Revision 1.39 by jsr166, Fri Jul 15 18:49:31 2011 UTC

# Line 1 | Line 1
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   */
8  
9   import junit.framework.*;
10 < import java.util.*;
11 < import java.util.concurrent.*;
10 > import java.util.Arrays;
11 > import java.util.ArrayList;
12 > import java.util.Collection;
13 > import java.util.Iterator;
14 > import java.util.NoSuchElementException;
15 > import java.util.Queue;
16 > import java.util.concurrent.BlockingQueue;
17 > import java.util.concurrent.CountDownLatch;
18 > import java.util.concurrent.Executors;
19 > import java.util.concurrent.ExecutorService;
20 > import java.util.concurrent.SynchronousQueue;
21   import static java.util.concurrent.TimeUnit.MILLISECONDS;
13 import java.io.*;
22  
23   public class SynchronousQueueTest extends JSR166TestCase {
24  
25 <    public static void main(String[] args) {
26 <        junit.textui.TestRunner.run (suite());
25 >    public static class Fair extends BlockingQueueTest {
26 >        protected BlockingQueue emptyCollection() {
27 >            return new SynchronousQueue(true);
28 >        }
29      }
30  
31 <    public static Test suite() {
32 <        return new TestSuite(SynchronousQueueTest.class);
31 >    public static class NonFair extends BlockingQueueTest {
32 >        protected BlockingQueue emptyCollection() {
33 >            return new SynchronousQueue(false);
34 >        }
35      }
36  
37 <    /**
38 <     * A SynchronousQueue is both empty and full
39 <     */
40 <    public void testEmptyFull() {
41 <        SynchronousQueue q = new SynchronousQueue();
42 <        assertTrue(q.isEmpty());
43 <        assertEquals(0, q.size());
44 <        assertEquals(0, q.remainingCapacity());
33 <        assertFalse(q.offer(zero));
37 >    public static void main(String[] args) {
38 >        junit.textui.TestRunner.run(suite());
39 >    }
40 >
41 >    public static Test suite() {
42 >        return newTestSuite(SynchronousQueueTest.class,
43 >                            new Fair().testSuite(),
44 >                            new NonFair().testSuite());
45      }
46  
47      /**
48 <     * A fair SynchronousQueue is both empty and full
48 >     * Any SynchronousQueue is both empty and full
49       */
50 <    public void testFairEmptyFull() {
51 <        SynchronousQueue q = new SynchronousQueue(true);
50 >    public void testEmptyFull()      { testEmptyFull(false); }
51 >    public void testEmptyFull_fair() { testEmptyFull(true); }
52 >    public void testEmptyFull(boolean fair) {
53 >        final SynchronousQueue q = new SynchronousQueue(fair);
54          assertTrue(q.isEmpty());
55          assertEquals(0, q.size());
56          assertEquals(0, q.remainingCapacity());
# Line 45 | Line 58 | public class SynchronousQueueTest extend
58      }
59  
60      /**
48     * offer(null) throws NPE
49     */
50    public void testOfferNull() {
51        try {
52            SynchronousQueue q = new SynchronousQueue();
53            q.offer(null);
54            shouldThrow();
55        } catch (NullPointerException success) {}
56    }
57
58    /**
59     * add(null) throws NPE
60     */
61    public void testAddNull() {
62        try {
63            SynchronousQueue q = new SynchronousQueue();
64            q.add(null);
65            shouldThrow();
66        } catch (NullPointerException success) {}
67    }
68
69    /**
61       * offer fails if no active taker
62       */
63 <    public void testOffer() {
64 <        SynchronousQueue q = new SynchronousQueue();
63 >    public void testOffer()      { testOffer(false); }
64 >    public void testOffer_fair() { testOffer(true); }
65 >    public void testOffer(boolean fair) {
66 >        SynchronousQueue q = new SynchronousQueue(fair);
67          assertFalse(q.offer(one));
68      }
69  
70      /**
71 <     * add throws ISE if no active taker
71 >     * add throws IllegalStateException if no active taker
72       */
73 <    public void testAdd() {
73 >    public void testAdd()      { testAdd(false); }
74 >    public void testAdd_fair() { testAdd(true); }
75 >    public void testAdd(boolean fair) {
76 >        SynchronousQueue q = new SynchronousQueue(fair);
77 >        assertEquals(0, q.remainingCapacity());
78          try {
82            SynchronousQueue q = new SynchronousQueue();
83            assertEquals(0, q.remainingCapacity());
79              q.add(one);
80              shouldThrow();
81          } catch (IllegalStateException success) {}
82      }
83  
84      /**
85 <     * addAll(null) throws NPE
91 <     */
92 <    public void testAddAll1() {
93 <        try {
94 <            SynchronousQueue q = new SynchronousQueue();
95 <            q.addAll(null);
96 <            shouldThrow();
97 <        } catch (NullPointerException success) {}
98 <    }
99 <
100 <    /**
101 <     * addAll(this) throws IAE
85 >     * addAll(this) throws IllegalArgumentException
86       */
87 <    public void testAddAllSelf() {
87 >    public void testAddAll_self()      { testAddAll_self(false); }
88 >    public void testAddAll_self_fair() { testAddAll_self(true); }
89 >    public void testAddAll_self(boolean fair) {
90 >        SynchronousQueue q = new SynchronousQueue(fair);
91          try {
105            SynchronousQueue q = new SynchronousQueue();
92              q.addAll(q);
93              shouldThrow();
94          } catch (IllegalArgumentException success) {}
95      }
96  
97      /**
112     * addAll of a collection with null elements throws NPE
113     */
114    public void testAddAll2() {
115        try {
116            SynchronousQueue q = new SynchronousQueue();
117            Integer[] ints = new Integer[1];
118            q.addAll(Arrays.asList(ints));
119            shouldThrow();
120        } catch (NullPointerException success) {}
121    }
122    /**
98       * addAll throws ISE if no active taker
99       */
100 <    public void testAddAll4() {
100 >    public void testAddAll_ISE()      { testAddAll_ISE(false); }
101 >    public void testAddAll_ISE_fair() { testAddAll_ISE(true); }
102 >    public void testAddAll_ISE(boolean fair) {
103 >        SynchronousQueue q = new SynchronousQueue(fair);
104 >        Integer[] ints = new Integer[1];
105 >        for (int i = 0; i < ints.length; i++)
106 >            ints[i] = i;
107 >        Collection<Integer> coll = Arrays.asList(ints);
108          try {
109 <            SynchronousQueue q = new SynchronousQueue();
128 <            Integer[] ints = new Integer[1];
129 <            for (int i = 0; i < 1; ++i)
130 <                ints[i] = new Integer(i);
131 <            q.addAll(Arrays.asList(ints));
109 >            q.addAll(coll);
110              shouldThrow();
111          } catch (IllegalStateException success) {}
112      }
113  
114      /**
137     * put(null) throws NPE
138     */
139    public void testPutNull() throws InterruptedException {
140        try {
141            SynchronousQueue q = new SynchronousQueue();
142            q.put(null);
143            shouldThrow();
144        } catch (NullPointerException success) {}
145     }
146
147    /**
115       * put blocks interruptibly if no active taker
116       */
117 <    public void testBlockingPut() throws InterruptedException {
118 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
119 <            public void realRun() throws InterruptedException {
120 <                SynchronousQueue q = new SynchronousQueue();
121 <                q.put(zero);
122 <            }});
156 <
157 <        t.start();
158 <        Thread.sleep(SHORT_DELAY_MS);
159 <        t.interrupt();
160 <        t.join();
161 <    }
162 <
163 <    /**
164 <     * put blocks waiting for take
165 <     */
166 <    public void testPutWithTake() throws InterruptedException {
167 <        final SynchronousQueue q = new SynchronousQueue();
168 <        Thread t = new Thread(new CheckedRunnable() {
117 >    public void testBlockingPut()      { testBlockingPut(false); }
118 >    public void testBlockingPut_fair() { testBlockingPut(true); }
119 >    public void testBlockingPut(boolean fair) {
120 >        final SynchronousQueue q = new SynchronousQueue(fair);
121 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
122 >        Thread t = newStartedThread(new CheckedRunnable() {
123              public void realRun() throws InterruptedException {
124 <                int added = 0;
124 >                Thread.currentThread().interrupt();
125                  try {
126 <                    q.put(new Object());
127 <                    ++added;
128 <                    q.put(new Object());
129 <                    ++added;
176 <                    q.put(new Object());
177 <                    ++added;
178 <                    q.put(new Object());
179 <                    ++added;
180 <                    threadShouldThrow();
181 <                } catch (InterruptedException success) {
182 <                    assertTrue(added >= 1);
183 <                }
184 <            }});
185 <
186 <        t.start();
187 <        Thread.sleep(SHORT_DELAY_MS);
188 <        q.take();
189 <        Thread.sleep(SHORT_DELAY_MS);
190 <        t.interrupt();
191 <        t.join();
192 <    }
126 >                    q.put(99);
127 >                    shouldThrow();
128 >                } catch (InterruptedException success) {}
129 >                assertFalse(Thread.interrupted());
130  
131 <    /**
132 <     * timed offer times out if elements not taken
133 <     */
134 <    public void testTimedOffer() throws InterruptedException {
135 <        final SynchronousQueue q = new SynchronousQueue();
136 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
200 <            public void realRun() throws InterruptedException {
201 <                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
202 <                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
131 >                pleaseInterrupt.countDown();
132 >                try {
133 >                    q.put(99);
134 >                    shouldThrow();
135 >                } catch (InterruptedException success) {}
136 >                assertFalse(Thread.interrupted());
137              }});
138  
139 <        t.start();
140 <        Thread.sleep(SMALL_DELAY_MS);
139 >        await(pleaseInterrupt);
140 >        assertThreadStaysAlive(t);
141          t.interrupt();
142 <        t.join();
142 >        awaitTermination(t);
143 >        assertEquals(0, q.remainingCapacity());
144      }
145  
211
146      /**
147 <     * take blocks interruptibly when empty
147 >     * put blocks interruptibly waiting for take
148       */
149 <    public void testTakeFromEmpty() throws InterruptedException {
150 <        final SynchronousQueue q = new SynchronousQueue();
151 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
149 >    public void testPutWithTake()      { testPutWithTake(false); }
150 >    public void testPutWithTake_fair() { testPutWithTake(true); }
151 >    public void testPutWithTake(boolean fair) {
152 >        final SynchronousQueue q = new SynchronousQueue(fair);
153 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
154 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
155 >        Thread t = newStartedThread(new CheckedRunnable() {
156              public void realRun() throws InterruptedException {
157 <                q.take();
158 <            }});
221 <
222 <        t.start();
223 <        Thread.sleep(SHORT_DELAY_MS);
224 <        t.interrupt();
225 <        t.join();
226 <    }
227 <
157 >                pleaseTake.countDown();
158 >                q.put(one);
159  
160 <    /**
161 <     * put blocks interruptibly if no active taker
162 <     */
163 <    public void testFairBlockingPut() throws InterruptedException {
164 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
165 <            public void realRun() throws InterruptedException {
235 <                SynchronousQueue q = new SynchronousQueue(true);
236 <                q.put(zero);
160 >                pleaseInterrupt.countDown();
161 >                try {
162 >                    q.put(99);
163 >                    shouldThrow();
164 >                } catch (InterruptedException success) {}
165 >                assertFalse(Thread.interrupted());
166              }});
167  
168 <        t.start();
169 <        Thread.sleep(SHORT_DELAY_MS);
170 <        t.interrupt();
171 <        t.join();
243 <    }
168 >        await(pleaseTake);
169 >        assertEquals(q.remainingCapacity(), 0);
170 >        try { assertSame(one, q.take()); }
171 >        catch (InterruptedException e) { threadUnexpectedException(e); }
172  
173 <    /**
174 <     * put blocks waiting for take
247 <     */
248 <    public void testFairPutWithTake() throws InterruptedException {
249 <        final SynchronousQueue q = new SynchronousQueue(true);
250 <        Thread t = new Thread(new CheckedRunnable() {
251 <            public void realRun() throws InterruptedException {
252 <                int added = 0;
253 <                try {
254 <                    q.put(new Object());
255 <                    ++added;
256 <                    q.put(new Object());
257 <                    ++added;
258 <                    q.put(new Object());
259 <                    ++added;
260 <                    q.put(new Object());
261 <                    ++added;
262 <                    threadShouldThrow();
263 <                } catch (InterruptedException success) {
264 <                    assertTrue(added >= 1);
265 <                }
266 <            }});
267 <
268 <        t.start();
269 <        Thread.sleep(SHORT_DELAY_MS);
270 <        q.take();
271 <        Thread.sleep(SHORT_DELAY_MS);
173 >        await(pleaseInterrupt);
174 >        assertThreadStaysAlive(t);
175          t.interrupt();
176 <        t.join();
176 >        awaitTermination(t);
177 >        assertEquals(q.remainingCapacity(), 0);
178      }
179  
180      /**
181       * timed offer times out if elements not taken
182       */
183 <    public void testFairTimedOffer() throws InterruptedException {
184 <        final SynchronousQueue q = new SynchronousQueue(true);
185 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
186 <            public void realRun() throws InterruptedException {
187 <                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
188 <                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
189 <            }});
190 <
191 <        t.start();
192 <        Thread.sleep(SMALL_DELAY_MS);
193 <        t.interrupt();
194 <        t.join();
195 <    }
196 <
197 <
294 <    /**
295 <     * take blocks interruptibly when empty
296 <     */
297 <    public void testFairTakeFromEmpty() throws InterruptedException {
298 <        final SynchronousQueue q = new SynchronousQueue(true);
299 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
300 <            public void realRun() throws InterruptedException {
301 <                q.take();
183 >    public void testTimedOffer()      { testTimedOffer(false); }
184 >    public void testTimedOffer_fair() { testTimedOffer(true); }
185 >    public void testTimedOffer(boolean fair) {
186 >        final SynchronousQueue q = new SynchronousQueue(fair);
187 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
188 >        Thread t = newStartedThread(new CheckedRunnable() {
189 >            public void realRun() throws InterruptedException {
190 >                long startTime = System.nanoTime();
191 >                assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
192 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
193 >                pleaseInterrupt.countDown();
194 >                try {
195 >                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
196 >                    shouldThrow();
197 >                } catch (InterruptedException success) {}
198              }});
199  
200 <        t.start();
201 <        Thread.sleep(SHORT_DELAY_MS);
200 >        await(pleaseInterrupt);
201 >        assertThreadStaysAlive(t);
202          t.interrupt();
203 <        t.join();
203 >        awaitTermination(t);
204      }
205  
206      /**
207 <     * poll fails unless active taker
207 >     * poll return null if no active putter
208       */
209 <    public void testPoll() {
210 <        SynchronousQueue q = new SynchronousQueue();
209 >    public void testPoll()      { testPoll(false); }
210 >    public void testPoll_fair() { testPoll(true); }
211 >    public void testPoll(boolean fair) {
212 >        final SynchronousQueue q = new SynchronousQueue(fair);
213          assertNull(q.poll());
214      }
215  
216      /**
217 <     * timed pool with zero timeout times out if no active taker
217 >     * timed poll with zero timeout times out if no active putter
218       */
219 <    public void testTimedPoll0() throws InterruptedException {
220 <        SynchronousQueue q = new SynchronousQueue();
221 <        assertNull(q.poll(0, MILLISECONDS));
219 >    public void testTimedPoll0()      { testTimedPoll0(false); }
220 >    public void testTimedPoll0_fair() { testTimedPoll0(true); }
221 >    public void testTimedPoll0(boolean fair) {
222 >        final SynchronousQueue q = new SynchronousQueue(fair);
223 >        try { assertNull(q.poll(0, MILLISECONDS)); }
224 >        catch (InterruptedException e) { threadUnexpectedException(e); }
225      }
226  
227      /**
228 <     * timed pool with nonzero timeout times out if no active taker
228 >     * timed poll with nonzero timeout times out if no active putter
229       */
230 <    public void testTimedPoll() throws InterruptedException {
231 <        SynchronousQueue q = new SynchronousQueue();
232 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
230 >    public void testTimedPoll()      { testTimedPoll(false); }
231 >    public void testTimedPoll_fair() { testTimedPoll(true); }
232 >    public void testTimedPoll(boolean fair) {
233 >        final SynchronousQueue q = new SynchronousQueue(fair);
234 >        long startTime = System.nanoTime();
235 >        try { assertNull(q.poll(timeoutMillis(), MILLISECONDS)); }
236 >        catch (InterruptedException e) { threadUnexpectedException(e); }
237 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
238      }
239  
240      /**
241 <     * Interrupted timed poll throws InterruptedException instead of
242 <     * returning timeout status
241 >     * timed poll before a delayed offer times out, returning null;
242 >     * after offer succeeds; on interruption throws
243       */
244 <    public void testInterruptedTimedPoll() throws InterruptedException {
245 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
244 >    public void testTimedPollWithOffer()      { testTimedPollWithOffer(false); }
245 >    public void testTimedPollWithOffer_fair() { testTimedPollWithOffer(true); }
246 >    public void testTimedPollWithOffer(boolean fair) {
247 >        final SynchronousQueue q = new SynchronousQueue(fair);
248 >        final CountDownLatch pleaseOffer = new CountDownLatch(1);
249 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
250 >        Thread t = newStartedThread(new CheckedRunnable() {
251              public void realRun() throws InterruptedException {
252 <                SynchronousQueue q = new SynchronousQueue();
253 <                q.poll(SMALL_DELAY_MS, MILLISECONDS);
254 <            }});
252 >                long startTime = System.nanoTime();
253 >                assertNull(q.poll(timeoutMillis(), MILLISECONDS));
254 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
255  
256 <        t.start();
257 <        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() {
358 <            public void realRun() throws InterruptedException {
359 <                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
256 >                pleaseOffer.countDown();
257 >                startTime = System.nanoTime();
258                  assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
259 +                assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
260 +
261 +                Thread.currentThread().interrupt();
262                  try {
263                      q.poll(LONG_DELAY_MS, MILLISECONDS);
264 <                    threadShouldThrow();
264 >                    shouldThrow();
265                  } catch (InterruptedException success) {}
266 <            }});
366 <
367 <        t.start();
368 <        Thread.sleep(SMALL_DELAY_MS);
369 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
370 <        t.interrupt();
371 <        t.join();
372 <    }
373 <
374 <    /**
375 <     * Interrupted timed poll throws InterruptedException instead of
376 <     * returning timeout status
377 <     */
378 <    public void testFairInterruptedTimedPoll() throws InterruptedException {
379 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
380 <            public void realRun() throws InterruptedException {
381 <                SynchronousQueue q = new SynchronousQueue(true);
382 <                q.poll(SMALL_DELAY_MS, MILLISECONDS);
383 <            }});
384 <
385 <        t.start();
386 <        Thread.sleep(SHORT_DELAY_MS);
387 <        t.interrupt();
388 <        t.join();
389 <    }
266 >                assertFalse(Thread.interrupted());
267  
268 <    /**
392 <     *  timed poll before a delayed offer fails; after offer succeeds;
393 <     *  on interruption throws
394 <     */
395 <    public void testFairTimedPollWithOffer() throws InterruptedException {
396 <        final SynchronousQueue q = new SynchronousQueue(true);
397 <        Thread t = new Thread(new CheckedRunnable() {
398 <            public void realRun() throws InterruptedException {
399 <                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
400 <                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
268 >                pleaseInterrupt.countDown();
269                  try {
270                      q.poll(LONG_DELAY_MS, MILLISECONDS);
271 <                    threadShouldThrow();
271 >                    shouldThrow();
272                  } catch (InterruptedException success) {}
273 +                assertFalse(Thread.interrupted());
274              }});
275  
276 <        t.start();
277 <        Thread.sleep(SMALL_DELAY_MS);
278 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
276 >        await(pleaseOffer);
277 >        long startTime = System.nanoTime();
278 >        try { assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); }
279 >        catch (InterruptedException e) { threadUnexpectedException(e); }
280 >        assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
281 >
282 >        await(pleaseInterrupt);
283 >        assertThreadStaysAlive(t);
284          t.interrupt();
285 <        t.join();
285 >        awaitTermination(t);
286      }
287  
414
288      /**
289 <     * peek returns null
289 >     * peek() returns null if no active putter
290       */
291 <    public void testPeek() {
292 <        SynchronousQueue q = new SynchronousQueue();
291 >    public void testPeek()      { testPeek(false); }
292 >    public void testPeek_fair() { testPeek(true); }
293 >    public void testPeek(boolean fair) {
294 >        final SynchronousQueue q = new SynchronousQueue(fair);
295          assertNull(q.peek());
296      }
297  
298      /**
299 <     * element throws NSEE
299 >     * element() throws NoSuchElementException if no active putter
300       */
301 <    public void testElement() {
302 <        SynchronousQueue q = new SynchronousQueue();
301 >    public void testElement()      { testElement(false); }
302 >    public void testElement_fair() { testElement(true); }
303 >    public void testElement(boolean fair) {
304 >        final SynchronousQueue q = new SynchronousQueue(fair);
305          try {
306              q.element();
307              shouldThrow();
# Line 432 | Line 309 | public class SynchronousQueueTest extend
309      }
310  
311      /**
312 <     * remove throws NSEE if no active taker
312 >     * remove() throws NoSuchElementException if no active putter
313       */
314 <    public void testRemove() {
315 <        SynchronousQueue q = new SynchronousQueue();
314 >    public void testRemove()      { testRemove(false); }
315 >    public void testRemove_fair() { testRemove(true); }
316 >    public void testRemove(boolean fair) {
317 >        final SynchronousQueue q = new SynchronousQueue(fair);
318          try {
319              q.remove();
320              shouldThrow();
321 <        } catch (NoSuchElementException success) {
443 <        }
444 <    }
445 <
446 <    /**
447 <     * remove(x) returns false
448 <     */
449 <    public void testRemoveElement() {
450 <        SynchronousQueue q = new SynchronousQueue();
451 <        assertFalse(q.remove(zero));
452 <        assertTrue(q.isEmpty());
321 >        } catch (NoSuchElementException success) {}
322      }
323  
324      /**
325       * contains returns false
326       */
327 <    public void testContains() {
328 <        SynchronousQueue q = new SynchronousQueue();
327 >    public void testContains()      { testContains(false); }
328 >    public void testContains_fair() { testContains(true); }
329 >    public void testContains(boolean fair) {
330 >        final SynchronousQueue q = new SynchronousQueue(fair);
331          assertFalse(q.contains(zero));
332      }
333  
334      /**
335       * clear ensures isEmpty
336       */
337 <    public void testClear() {
338 <        SynchronousQueue q = new SynchronousQueue();
337 >    public void testClear()      { testClear(false); }
338 >    public void testClear_fair() { testClear(true); }
339 >    public void testClear(boolean fair) {
340 >        final SynchronousQueue q = new SynchronousQueue(fair);
341          q.clear();
342          assertTrue(q.isEmpty());
343      }
# Line 472 | Line 345 | public class SynchronousQueueTest extend
345      /**
346       * containsAll returns false unless empty
347       */
348 <    public void testContainsAll() {
349 <        SynchronousQueue q = new SynchronousQueue();
348 >    public void testContainsAll()      { testContainsAll(false); }
349 >    public void testContainsAll_fair() { testContainsAll(true); }
350 >    public void testContainsAll(boolean fair) {
351 >        final SynchronousQueue q = new SynchronousQueue(fair);
352          Integer[] empty = new Integer[0];
353          assertTrue(q.containsAll(Arrays.asList(empty)));
354          Integer[] ints = new Integer[1]; ints[0] = zero;
# Line 483 | Line 358 | public class SynchronousQueueTest extend
358      /**
359       * retainAll returns false
360       */
361 <    public void testRetainAll() {
362 <        SynchronousQueue q = new SynchronousQueue();
361 >    public void testRetainAll()      { testRetainAll(false); }
362 >    public void testRetainAll_fair() { testRetainAll(true); }
363 >    public void testRetainAll(boolean fair) {
364 >        final SynchronousQueue q = new SynchronousQueue(fair);
365          Integer[] empty = new Integer[0];
366          assertFalse(q.retainAll(Arrays.asList(empty)));
367          Integer[] ints = new Integer[1]; ints[0] = zero;
# Line 494 | Line 371 | public class SynchronousQueueTest extend
371      /**
372       * removeAll returns false
373       */
374 <    public void testRemoveAll() {
375 <        SynchronousQueue q = new SynchronousQueue();
374 >    public void testRemoveAll()      { testRemoveAll(false); }
375 >    public void testRemoveAll_fair() { testRemoveAll(true); }
376 >    public void testRemoveAll(boolean fair) {
377 >        final SynchronousQueue q = new SynchronousQueue(fair);
378          Integer[] empty = new Integer[0];
379          assertFalse(q.removeAll(Arrays.asList(empty)));
380          Integer[] ints = new Integer[1]; ints[0] = zero;
381          assertFalse(q.containsAll(Arrays.asList(ints)));
382      }
383  
505
384      /**
385       * toArray is empty
386       */
387 <    public void testToArray() {
388 <        SynchronousQueue q = new SynchronousQueue();
387 >    public void testToArray()      { testToArray(false); }
388 >    public void testToArray_fair() { testToArray(true); }
389 >    public void testToArray(boolean fair) {
390 >        final SynchronousQueue q = new SynchronousQueue(fair);
391          Object[] o = q.toArray();
392          assertEquals(o.length, 0);
393      }
# Line 515 | Line 395 | public class SynchronousQueueTest extend
395      /**
396       * toArray(a) is nulled at position 0
397       */
398 <    public void testToArray2() {
399 <        SynchronousQueue q = new SynchronousQueue();
398 >    public void testToArray2()      { testToArray2(false); }
399 >    public void testToArray2_fair() { testToArray2(true); }
400 >    public void testToArray2(boolean fair) {
401 >        final SynchronousQueue q = new SynchronousQueue(fair);
402          Integer[] ints = new Integer[1];
403          assertNull(ints[0]);
404      }
# Line 524 | Line 406 | public class SynchronousQueueTest extend
406      /**
407       * toArray(null) throws NPE
408       */
409 <    public void testToArray_BadArg() {
409 >    public void testToArray_null()      { testToArray_null(false); }
410 >    public void testToArray_null_fair() { testToArray_null(true); }
411 >    public void testToArray_null(boolean fair) {
412 >        final SynchronousQueue q = new SynchronousQueue(fair);
413          try {
529            SynchronousQueue q = new SynchronousQueue();
414              Object o[] = q.toArray(null);
415              shouldThrow();
416          } catch (NullPointerException success) {}
417      }
418  
535
419      /**
420       * iterator does not traverse any elements
421       */
422 <    public void testIterator() {
423 <        SynchronousQueue q = new SynchronousQueue();
422 >    public void testIterator()      { testIterator(false); }
423 >    public void testIterator_fair() { testIterator(true); }
424 >    public void testIterator(boolean fair) {
425 >        final SynchronousQueue q = new SynchronousQueue(fair);
426          Iterator it = q.iterator();
427          assertFalse(it.hasNext());
428          try {
# Line 549 | Line 434 | public class SynchronousQueueTest extend
434      /**
435       * iterator remove throws ISE
436       */
437 <    public void testIteratorRemove() {
438 <        SynchronousQueue q = new SynchronousQueue();
437 >    public void testIteratorRemove()      { testIteratorRemove(false); }
438 >    public void testIteratorRemove_fair() { testIteratorRemove(true); }
439 >    public void testIteratorRemove(boolean fair) {
440 >        final SynchronousQueue q = new SynchronousQueue(fair);
441          Iterator it = q.iterator();
442          try {
443              it.remove();
# Line 561 | Line 448 | public class SynchronousQueueTest extend
448      /**
449       * toString returns a non-null string
450       */
451 <    public void testToString() {
452 <        SynchronousQueue q = new SynchronousQueue();
451 >    public void testToString()      { testToString(false); }
452 >    public void testToString_fair() { testToString(true); }
453 >    public void testToString(boolean fair) {
454 >        final SynchronousQueue q = new SynchronousQueue(fair);
455          String s = q.toString();
456          assertNotNull(s);
457      }
458  
570
459      /**
460       * offer transfers elements across Executor tasks
461       */
462 <    public void testOfferInExecutor() {
463 <        final SynchronousQueue q = new SynchronousQueue();
462 >    public void testOfferInExecutor()      { testOfferInExecutor(false); }
463 >    public void testOfferInExecutor_fair() { testOfferInExecutor(true); }
464 >    public void testOfferInExecutor(boolean fair) {
465 >        final SynchronousQueue q = new SynchronousQueue(fair);
466          ExecutorService executor = Executors.newFixedThreadPool(2);
467 <        final Integer one = new Integer(1);
467 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
468  
469          executor.execute(new CheckedRunnable() {
470              public void realRun() throws InterruptedException {
471 <                threadAssertFalse(q.offer(one));
472 <                threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
473 <                threadAssertEquals(0, q.remainingCapacity());
471 >                assertFalse(q.offer(one));
472 >                threadsStarted.await();
473 >                assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
474 >                assertEquals(0, q.remainingCapacity());
475              }});
476  
477          executor.execute(new CheckedRunnable() {
478              public void realRun() throws InterruptedException {
479 <                Thread.sleep(SMALL_DELAY_MS);
480 <                threadAssertEquals(one, q.take());
479 >                threadsStarted.await();
480 >                assertSame(one, q.take());
481              }});
482  
483          joinPool(executor);
593
484      }
485  
486      /**
487 <     * poll retrieves elements across Executor threads
487 >     * timed poll retrieves elements across Executor threads
488       */
489 <    public void testPollInExecutor() {
490 <        final SynchronousQueue q = new SynchronousQueue();
489 >    public void testPollInExecutor()      { testPollInExecutor(false); }
490 >    public void testPollInExecutor_fair() { testPollInExecutor(true); }
491 >    public void testPollInExecutor(boolean fair) {
492 >        final SynchronousQueue q = new SynchronousQueue(fair);
493 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
494          ExecutorService executor = Executors.newFixedThreadPool(2);
495          executor.execute(new CheckedRunnable() {
496              public void realRun() throws InterruptedException {
497 <                threadAssertNull(q.poll());
498 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
499 <                threadAssertTrue(q.isEmpty());
497 >                assertNull(q.poll());
498 >                threadsStarted.await();
499 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
500 >                assertTrue(q.isEmpty());
501              }});
502  
503          executor.execute(new CheckedRunnable() {
504              public void realRun() throws InterruptedException {
505 <                Thread.sleep(SMALL_DELAY_MS);
506 <                q.put(new Integer(1));
505 >                threadsStarted.await();
506 >                q.put(one);
507              }});
508  
509          joinPool(executor);
# Line 618 | Line 512 | public class SynchronousQueueTest extend
512      /**
513       * a deserialized serialized queue is usable
514       */
515 <    public void testSerialization() throws Exception {
516 <        SynchronousQueue q = new SynchronousQueue();
517 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
518 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
519 <        out.writeObject(q);
520 <        out.close();
521 <
522 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
629 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
630 <        SynchronousQueue r = (SynchronousQueue)in.readObject();
631 <        assertEquals(q.size(), r.size());
632 <        while (!q.isEmpty())
633 <            assertEquals(q.remove(), r.remove());
634 <    }
635 <
636 <    /**
637 <     * drainTo(null) throws NPE
638 <     */
639 <    public void testDrainToNull() {
640 <        SynchronousQueue q = new SynchronousQueue();
641 <        try {
642 <            q.drainTo(null);
643 <            shouldThrow();
644 <        } catch (NullPointerException success) {}
645 <    }
646 <
647 <    /**
648 <     * drainTo(this) throws IAE
649 <     */
650 <    public void testDrainToSelf() {
651 <        SynchronousQueue q = new SynchronousQueue();
652 <        try {
653 <            q.drainTo(q);
654 <            shouldThrow();
655 <        } catch (IllegalArgumentException success) {}
515 >    public void testSerialization()      { testSerialization(false); }
516 >    public void testSerialization_fair() { testSerialization(true); }
517 >    public void testSerialization(boolean fair) {
518 >        final SynchronousQueue x = new SynchronousQueue(fair);
519 >        final SynchronousQueue y = serialClone(x);
520 >        assertTrue(x != y);
521 >        assertTrue(x.isEmpty());
522 >        assertTrue(y.isEmpty());
523      }
524  
525      /**
526       * drainTo(c) of empty queue doesn't transfer elements
527       */
528 <    public void testDrainTo() {
529 <        SynchronousQueue q = new SynchronousQueue();
528 >    public void testDrainTo()      { testDrainTo(false); }
529 >    public void testDrainTo_fair() { testDrainTo(true); }
530 >    public void testDrainTo(boolean fair) {
531 >        final SynchronousQueue q = new SynchronousQueue(fair);
532          ArrayList l = new ArrayList();
533          q.drainTo(l);
534          assertEquals(q.size(), 0);
# Line 669 | Line 538 | public class SynchronousQueueTest extend
538      /**
539       * drainTo empties queue, unblocking a waiting put.
540       */
541 <    public void testDrainToWithActivePut() throws InterruptedException {
542 <        final SynchronousQueue q = new SynchronousQueue();
543 <        Thread t = new Thread(new CheckedRunnable() {
541 >    public void testDrainToWithActivePut()      { testDrainToWithActivePut(false); }
542 >    public void testDrainToWithActivePut_fair() { testDrainToWithActivePut(true); }
543 >    public void testDrainToWithActivePut(boolean fair) {
544 >        final SynchronousQueue q = new SynchronousQueue(fair);
545 >        Thread t = newStartedThread(new CheckedRunnable() {
546              public void realRun() throws InterruptedException {
547 <                q.put(new Integer(1));
547 >                q.put(one);
548              }});
549  
679        t.start();
550          ArrayList l = new ArrayList();
551 <        Thread.sleep(SHORT_DELAY_MS);
552 <        q.drainTo(l);
553 <        assertTrue(l.size() <= 1);
554 <        if (l.size() > 0)
555 <            assertEquals(l.get(0), new Integer(1));
556 <        t.join();
687 <        assertTrue(l.size() <= 1);
688 <    }
689 <
690 <    /**
691 <     * drainTo(null, n) throws NPE
692 <     */
693 <    public void testDrainToNullN() {
694 <        SynchronousQueue q = new SynchronousQueue();
695 <        try {
696 <            q.drainTo(null, 0);
697 <            shouldThrow();
698 <        } catch (NullPointerException success) {}
699 <    }
700 <
701 <    /**
702 <     * drainTo(this, n) throws IAE
703 <     */
704 <    public void testDrainToSelfN() {
705 <        SynchronousQueue q = new SynchronousQueue();
706 <        try {
707 <            q.drainTo(q, 0);
708 <            shouldThrow();
709 <        } catch (IllegalArgumentException success) {
551 >        long startTime = System.nanoTime();
552 >        while (l.isEmpty()) {
553 >            q.drainTo(l);
554 >            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
555 >                fail("timed out");
556 >            Thread.yield();
557          }
558 +        assertTrue(l.size() == 1);
559 +        assertSame(one, l.get(0));
560 +        awaitTermination(t);
561      }
562  
563      /**
# Line 715 | Line 565 | public class SynchronousQueueTest extend
565       */
566      public void testDrainToN() throws InterruptedException {
567          final SynchronousQueue q = new SynchronousQueue();
568 <        Thread t1 = new Thread(new CheckedRunnable() {
568 >        Thread t1 = newStartedThread(new CheckedRunnable() {
569              public void realRun() throws InterruptedException {
570                  q.put(one);
571              }});
572  
573 <        Thread t2 = new Thread(new CheckedRunnable() {
573 >        Thread t2 = newStartedThread(new CheckedRunnable() {
574              public void realRun() throws InterruptedException {
575                  q.put(two);
576              }});
577  
728        t1.start();
729        t2.start();
578          ArrayList l = new ArrayList();
579 <        Thread.sleep(SHORT_DELAY_MS);
579 >        delay(SHORT_DELAY_MS);
580          q.drainTo(l, 1);
581 <        assertTrue(l.size() == 1);
581 >        assertEquals(1, l.size());
582          q.drainTo(l, 1);
583 <        assertTrue(l.size() == 2);
583 >        assertEquals(2, l.size());
584          assertTrue(l.contains(one));
585          assertTrue(l.contains(two));
586 <        t1.join();
587 <        t2.join();
586 >        awaitTermination(t1);
587 >        awaitTermination(t2);
588      }
589  
590   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines