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.35 by jsr166, Fri May 27 20:07:24 2011 UTC vs.
Revision 1.37 by jsr166, Mon May 30 22:43:20 2011 UTC

# Line 7 | Line 7
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.concurrent.BlockingQueue;
16 > import java.util.concurrent.CountDownLatch;
17 > import java.util.concurrent.Executors;
18 > import java.util.concurrent.ExecutorService;
19 > import java.util.concurrent.SynchronousQueue;
20   import static java.util.concurrent.TimeUnit.MILLISECONDS;
21   import java.io.*;
22  
# Line 50 | Line 58 | public class SynchronousQueueTest extend
58      }
59  
60      /**
53     * offer(null) throws NPE
54     */
55    public void testOfferNull() {
56        try {
57            SynchronousQueue q = new SynchronousQueue();
58            q.offer(null);
59            shouldThrow();
60        } catch (NullPointerException success) {}
61    }
62
63    /**
64     * add(null) throws NPE
65     */
66    public void testAddNull() {
67        try {
68            SynchronousQueue q = new SynchronousQueue();
69            q.add(null);
70            shouldThrow();
71        } catch (NullPointerException success) {}
72    }
73
74    /**
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 {
87            SynchronousQueue q = new SynchronousQueue();
88            assertEquals(0, q.remainingCapacity());
79              q.add(one);
80              shouldThrow();
81          } catch (IllegalStateException success) {}
82      }
83  
84      /**
85 <     * addAll(null) throws NPE
85 >     * addAll(this) throws IllegalArgumentException
86       */
87 <    public void testAddAll1() {
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 {
99            SynchronousQueue q = new SynchronousQueue();
100            q.addAll(null);
101            shouldThrow();
102        } catch (NullPointerException success) {}
103    }
104
105    /**
106     * addAll(this) throws IAE
107     */
108    public void testAddAllSelf() {
109        try {
110            SynchronousQueue q = new SynchronousQueue();
92              q.addAll(q);
93              shouldThrow();
94          } catch (IllegalArgumentException success) {}
95      }
96  
97      /**
117     * addAll of a collection with null elements throws NPE
118     */
119    public void testAddAll2() {
120        try {
121            SynchronousQueue q = new SynchronousQueue();
122            Integer[] ints = new Integer[1];
123            q.addAll(Arrays.asList(ints));
124            shouldThrow();
125        } catch (NullPointerException success) {}
126    }
127
128    /**
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();
134 <            Integer[] ints = new Integer[1];
135 <            for (int i = 0; i < 1; ++i)
136 <                ints[i] = new Integer(i);
137 <            q.addAll(Arrays.asList(ints));
109 >            q.addAll(coll);
110              shouldThrow();
111          } catch (IllegalStateException success) {}
112      }
113  
114      /**
143     * put(null) throws NPE
144     */
145    public void testPutNull() throws InterruptedException {
146        try {
147            SynchronousQueue q = new SynchronousQueue();
148            q.put(null);
149            shouldThrow();
150        } catch (NullPointerException success) {}
151    }
152
153    /**
115       * put blocks interruptibly if no active taker
116       */
117      public void testBlockingPut()      { testBlockingPut(false); }
# Line 245 | Line 206 | public class SynchronousQueueTest extend
206      /**
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 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 poll with nonzero timeout times out if no active putter
229       */
230 <    public void testTimedPoll() throws InterruptedException {
231 <        SynchronousQueue q = new SynchronousQueue();
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 <        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
235 >        try { assertNull(q.poll(timeoutMillis(), MILLISECONDS)); }
236 >        catch (InterruptedException e) { threadUnexpectedException(e); }
237          assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
238      }
239  
# Line 272 | Line 241 | public class SynchronousQueueTest extend
241       * timed poll before a delayed offer times out, returning null;
242       * after offer succeeds; on interruption throws
243       */
244 <    public void testFairTimedPollWithOffer() throws InterruptedException {
245 <        final SynchronousQueue q = new SynchronousQueue(true);
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 <                long t0 = System.nanoTime();
253 <                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
254 <                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
252 >                long startTime = System.nanoTime();
253 >                assertNull(q.poll(timeoutMillis(), MILLISECONDS));
254 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
255  
256                  pleaseOffer.countDown();
257 <                t0 = System.nanoTime();
257 >                startTime = System.nanoTime();
258                  assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
259 <                assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
259 >                assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
260  
261 <                t0 = System.nanoTime();
261 >                Thread.currentThread().interrupt();
262 >                try {
263 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
264 >                    shouldThrow();
265 >                } catch (InterruptedException success) {}
266 >                assertFalse(Thread.interrupted());
267 >
268 >                pleaseInterrupt.countDown();
269                  try {
270                      q.poll(LONG_DELAY_MS, MILLISECONDS);
271                      shouldThrow();
272                  } catch (InterruptedException success) {}
273 <                assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
273 >                assertFalse(Thread.interrupted());
274              }});
275  
276 <        assertTrue(pleaseOffer.await(MEDIUM_DELAY_MS, MILLISECONDS));
277 <        long t0 = System.nanoTime();
278 <        assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS));
279 <        assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
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 <        awaitTermination(t, MEDIUM_DELAY_MS);
285 >        awaitTermination(t);
286      }
287  
288      /**
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 if no active putter
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 323 | Line 309 | public class SynchronousQueueTest extend
309      }
310  
311      /**
312 <     * remove() throws NSEE if no active putter
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();
# Line 336 | Line 324 | public class SynchronousQueueTest extend
324      /**
325       * remove(x) returns false
326       */
327 <    public void testRemoveElement() {
328 <        SynchronousQueue q = new SynchronousQueue();
327 >    public void testRemoveElement()      { testRemoveElement(false); }
328 >    public void testRemoveElement_fair() { testRemoveElement(true); }
329 >    public void testRemoveElement(boolean fair) {
330 >        final SynchronousQueue q = new SynchronousQueue(fair);
331          assertFalse(q.remove(zero));
332          assertTrue(q.isEmpty());
333      }
# Line 345 | Line 335 | public class SynchronousQueueTest extend
335      /**
336       * contains returns false
337       */
338 <    public void testContains() {
339 <        SynchronousQueue q = new SynchronousQueue();
338 >    public void testContains()      { testContains(false); }
339 >    public void testContains_fair() { testContains(true); }
340 >    public void testContains(boolean fair) {
341 >        final SynchronousQueue q = new SynchronousQueue(fair);
342          assertFalse(q.contains(zero));
343      }
344  
345      /**
346       * clear ensures isEmpty
347       */
348 <    public void testClear() {
349 <        SynchronousQueue q = new SynchronousQueue();
348 >    public void testClear()      { testClear(false); }
349 >    public void testClear_fair() { testClear(true); }
350 >    public void testClear(boolean fair) {
351 >        final SynchronousQueue q = new SynchronousQueue(fair);
352          q.clear();
353          assertTrue(q.isEmpty());
354      }
# Line 362 | Line 356 | public class SynchronousQueueTest extend
356      /**
357       * containsAll returns false unless empty
358       */
359 <    public void testContainsAll() {
360 <        SynchronousQueue q = new SynchronousQueue();
359 >    public void testContainsAll()      { testContainsAll(false); }
360 >    public void testContainsAll_fair() { testContainsAll(true); }
361 >    public void testContainsAll(boolean fair) {
362 >        final SynchronousQueue q = new SynchronousQueue(fair);
363          Integer[] empty = new Integer[0];
364          assertTrue(q.containsAll(Arrays.asList(empty)));
365          Integer[] ints = new Integer[1]; ints[0] = zero;
# Line 373 | Line 369 | public class SynchronousQueueTest extend
369      /**
370       * retainAll returns false
371       */
372 <    public void testRetainAll() {
373 <        SynchronousQueue q = new SynchronousQueue();
372 >    public void testRetainAll()      { testRetainAll(false); }
373 >    public void testRetainAll_fair() { testRetainAll(true); }
374 >    public void testRetainAll(boolean fair) {
375 >        final SynchronousQueue q = new SynchronousQueue(fair);
376          Integer[] empty = new Integer[0];
377          assertFalse(q.retainAll(Arrays.asList(empty)));
378          Integer[] ints = new Integer[1]; ints[0] = zero;
# Line 384 | Line 382 | public class SynchronousQueueTest extend
382      /**
383       * removeAll returns false
384       */
385 <    public void testRemoveAll() {
386 <        SynchronousQueue q = new SynchronousQueue();
385 >    public void testRemoveAll()      { testRemoveAll(false); }
386 >    public void testRemoveAll_fair() { testRemoveAll(true); }
387 >    public void testRemoveAll(boolean fair) {
388 >        final SynchronousQueue q = new SynchronousQueue(fair);
389          Integer[] empty = new Integer[0];
390          assertFalse(q.removeAll(Arrays.asList(empty)));
391          Integer[] ints = new Integer[1]; ints[0] = zero;
# Line 395 | Line 395 | public class SynchronousQueueTest extend
395      /**
396       * toArray is empty
397       */
398 <    public void testToArray() {
399 <        SynchronousQueue q = new SynchronousQueue();
398 >    public void testToArray()      { testToArray(false); }
399 >    public void testToArray_fair() { testToArray(true); }
400 >    public void testToArray(boolean fair) {
401 >        final SynchronousQueue q = new SynchronousQueue(fair);
402          Object[] o = q.toArray();
403          assertEquals(o.length, 0);
404      }
# Line 404 | Line 406 | public class SynchronousQueueTest extend
406      /**
407       * toArray(a) is nulled at position 0
408       */
409 <    public void testToArray2() {
410 <        SynchronousQueue q = new SynchronousQueue();
409 >    public void testToArray2()      { testToArray2(false); }
410 >    public void testToArray2_fair() { testToArray2(true); }
411 >    public void testToArray2(boolean fair) {
412 >        final SynchronousQueue q = new SynchronousQueue(fair);
413          Integer[] ints = new Integer[1];
414          assertNull(ints[0]);
415      }
# Line 413 | Line 417 | public class SynchronousQueueTest extend
417      /**
418       * toArray(null) throws NPE
419       */
420 <    public void testToArray_BadArg() {
421 <        SynchronousQueue q = new SynchronousQueue();
420 >    public void testToArray_null()      { testToArray_null(false); }
421 >    public void testToArray_null_fair() { testToArray_null(true); }
422 >    public void testToArray_null(boolean fair) {
423 >        final SynchronousQueue q = new SynchronousQueue(fair);
424          try {
425              Object o[] = q.toArray(null);
426              shouldThrow();
# Line 424 | Line 430 | public class SynchronousQueueTest extend
430      /**
431       * iterator does not traverse any elements
432       */
433 <    public void testIterator() {
434 <        SynchronousQueue q = new SynchronousQueue();
433 >    public void testIterator()      { testIterator(false); }
434 >    public void testIterator_fair() { testIterator(true); }
435 >    public void testIterator(boolean fair) {
436 >        final SynchronousQueue q = new SynchronousQueue(fair);
437          Iterator it = q.iterator();
438          assertFalse(it.hasNext());
439          try {
# Line 437 | Line 445 | public class SynchronousQueueTest extend
445      /**
446       * iterator remove throws ISE
447       */
448 <    public void testIteratorRemove() {
449 <        SynchronousQueue q = new SynchronousQueue();
448 >    public void testIteratorRemove()      { testIteratorRemove(false); }
449 >    public void testIteratorRemove_fair() { testIteratorRemove(true); }
450 >    public void testIteratorRemove(boolean fair) {
451 >        final SynchronousQueue q = new SynchronousQueue(fair);
452          Iterator it = q.iterator();
453          try {
454              it.remove();
# Line 449 | Line 459 | public class SynchronousQueueTest extend
459      /**
460       * toString returns a non-null string
461       */
462 <    public void testToString() {
463 <        SynchronousQueue q = new SynchronousQueue();
462 >    public void testToString()      { testToString(false); }
463 >    public void testToString_fair() { testToString(true); }
464 >    public void testToString(boolean fair) {
465 >        final SynchronousQueue q = new SynchronousQueue(fair);
466          String s = q.toString();
467          assertNotNull(s);
468      }
# Line 458 | Line 470 | public class SynchronousQueueTest extend
470      /**
471       * offer transfers elements across Executor tasks
472       */
473 <    public void testOfferInExecutor() {
474 <        final SynchronousQueue q = new SynchronousQueue();
473 >    public void testOfferInExecutor()      { testOfferInExecutor(false); }
474 >    public void testOfferInExecutor_fair() { testOfferInExecutor(true); }
475 >    public void testOfferInExecutor(boolean fair) {
476 >        final SynchronousQueue q = new SynchronousQueue(fair);
477          ExecutorService executor = Executors.newFixedThreadPool(2);
478          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
479  
# Line 483 | Line 497 | public class SynchronousQueueTest extend
497      /**
498       * timed poll retrieves elements across Executor threads
499       */
500 <    public void testPollInExecutor() {
501 <        final SynchronousQueue q = new SynchronousQueue();
500 >    public void testPollInExecutor()      { testPollInExecutor(false); }
501 >    public void testPollInExecutor_fair() { testPollInExecutor(true); }
502 >    public void testPollInExecutor(boolean fair) {
503 >        final SynchronousQueue q = new SynchronousQueue(fair);
504          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
505          ExecutorService executor = Executors.newFixedThreadPool(2);
506          executor.execute(new CheckedRunnable() {
# Line 507 | Line 523 | public class SynchronousQueueTest extend
523      /**
524       * a deserialized serialized queue is usable
525       */
526 <    public void testSerialization() throws Exception {
527 <        SynchronousQueue q = new SynchronousQueue();
528 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
529 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
530 <        out.writeObject(q);
531 <        out.close();
516 <
517 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
518 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
519 <        SynchronousQueue r = (SynchronousQueue)in.readObject();
526 >    public void testSerialization()      { testSerialization(false); }
527 >    public void testSerialization_fair() { testSerialization(true); }
528 >    public void testSerialization(boolean fair) {
529 >        final SynchronousQueue q = new SynchronousQueue(fair);
530 >        final SynchronousQueue r = serialClone(q);
531 >        assertTrue(q != r);
532          assertEquals(q.size(), r.size());
533          while (!q.isEmpty())
534              assertEquals(q.remove(), r.remove());
535      }
536  
537      /**
526     * drainTo(null) throws NPE
527     */
528    public void testDrainToNull() {
529        SynchronousQueue q = new SynchronousQueue();
530        try {
531            q.drainTo(null);
532            shouldThrow();
533        } catch (NullPointerException success) {}
534    }
535
536    /**
537     * drainTo(this) throws IAE
538     */
539    public void testDrainToSelf() {
540        SynchronousQueue q = new SynchronousQueue();
541        try {
542            q.drainTo(q);
543            shouldThrow();
544        } catch (IllegalArgumentException success) {}
545    }
546
547    /**
538       * drainTo(c) of empty queue doesn't transfer elements
539       */
540 <    public void testDrainTo() {
541 <        SynchronousQueue q = new SynchronousQueue();
540 >    public void testDrainTo()      { testDrainTo(false); }
541 >    public void testDrainTo_fair() { testDrainTo(true); }
542 >    public void testDrainTo(boolean fair) {
543 >        final SynchronousQueue q = new SynchronousQueue(fair);
544          ArrayList l = new ArrayList();
545          q.drainTo(l);
546          assertEquals(q.size(), 0);
# Line 558 | Line 550 | public class SynchronousQueueTest extend
550      /**
551       * drainTo empties queue, unblocking a waiting put.
552       */
553 <    public void testDrainToWithActivePut() throws InterruptedException {
554 <        final SynchronousQueue q = new SynchronousQueue();
553 >    public void testDrainToWithActivePut()      { testDrainToWithActivePut(false); }
554 >    public void testDrainToWithActivePut_fair() { testDrainToWithActivePut(true); }
555 >    public void testDrainToWithActivePut(boolean fair) {
556 >        final SynchronousQueue q = new SynchronousQueue(fair);
557          Thread t = newStartedThread(new CheckedRunnable() {
558              public void realRun() throws InterruptedException {
559                  q.put(one);
# Line 579 | Line 573 | public class SynchronousQueueTest extend
573      }
574  
575      /**
582     * drainTo(null, n) throws NPE
583     */
584    public void testDrainToNullN() {
585        SynchronousQueue q = new SynchronousQueue();
586        try {
587            q.drainTo(null, 0);
588            shouldThrow();
589        } catch (NullPointerException success) {}
590    }
591
592    /**
593     * drainTo(this, n) throws IAE
594     */
595    public void testDrainToSelfN() {
596        SynchronousQueue q = new SynchronousQueue();
597        try {
598            q.drainTo(q, 0);
599            shouldThrow();
600        } catch (IllegalArgumentException success) {}
601    }
602
603    /**
576       * drainTo(c, n) empties up to n elements of queue into c
577       */
578      public void testDrainToN() throws InterruptedException {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines