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

Comparing jsr166/src/test/tck/ArrayBlockingQueueTest.java (file contents):
Revision 1.1 by dl, Sun Aug 31 19:24:52 2003 UTC vs.
Revision 1.5 by dl, Thu Sep 25 11:02:41 2003 UTC

# Line 5 | Line 5
5   * Jeffrey Hayes, Pat Fischer, Mike Judd.
6   */
7  
8 +
9   import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
12 + import java.io.*;
13  
14 < public class ArrayBlockingQueueTest extends TestCase {
13 <
14 <    private static int N = 10;
15 <    private static long SHORT_DELAY_MS = 100;
16 <    private static long MEDIUM_DELAY_MS = 1000;
17 <    private static long LONG_DELAY_MS = 10000;
18 <
14 > public class ArrayBlockingQueueTest extends JSR166TestCase {
15      public static void main(String[] args) {
16          junit.textui.TestRunner.run (suite());  
17      }
22
18      public static Test suite() {
19          return new TestSuite(ArrayBlockingQueueTest.class);
20      }
# Line 28 | Line 23 | public class ArrayBlockingQueueTest exte
23       * Create a queue of given size containing consecutive
24       * Integers 0 ... n.
25       */
26 <    private ArrayBlockingQueue fullQueue(int n) {
26 >    private ArrayBlockingQueue populatedQueue(int n) {
27          ArrayBlockingQueue q = new ArrayBlockingQueue(n);
28          assertTrue(q.isEmpty());
29          for(int i = 0; i < n; i++)
# Line 39 | Line 34 | public class ArrayBlockingQueueTest exte
34          return q;
35      }
36  
37 <    public void testConstructor1(){
38 <        assertEquals(N, new ArrayBlockingQueue(N).remainingCapacity());
37 >    /**
38 >     * A new queue has the indicated capacity
39 >     */
40 >    public void testConstructor1() {
41 >        assertEquals(SIZE, new ArrayBlockingQueue(SIZE).remainingCapacity());
42      }
43  
44 <    public void testConstructor2(){
44 >    /**
45 >     * Constructor throws IAE if  capacity argument nonpositive
46 >     */
47 >    public void testConstructor2() {
48          try {
49              ArrayBlockingQueue q = new ArrayBlockingQueue(0);
50 <            fail("Cannot make zero-sized");
50 >            shouldThrow();
51          }
52          catch (IllegalArgumentException success) {}
53      }
54  
55 <    public void testConstructor3(){
56 <
55 >    /**
56 >     * Initializing from null Collection throws NPE
57 >     */
58 >    public void testConstructor3() {
59          try {
60              ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null);
61 <            fail("Cannot make from null collection");
61 >            shouldThrow();
62          }
63          catch (NullPointerException success) {}
64      }
65  
66 <    public void testConstructor4(){
66 >    /**
67 >     * Initializing from Collection of null elements throws NPE
68 >     */
69 >    public void testConstructor4() {
70          try {
71 <            Integer[] ints = new Integer[N];
72 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N, false, Arrays.asList(ints));
73 <            fail("Cannot make with null elements");
71 >            Integer[] ints = new Integer[SIZE];
72 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
73 >            shouldThrow();
74          }
75          catch (NullPointerException success) {}
76      }
77  
78 <    public void testConstructor5(){
78 >    /**
79 >     * Initializing from Collection with some null elements throws NPE
80 >     */
81 >    public void testConstructor5() {
82          try {
83 <            Integer[] ints = new Integer[N];
84 <            for (int i = 0; i < N-1; ++i)
83 >            Integer[] ints = new Integer[SIZE];
84 >            for (int i = 0; i < SIZE-1; ++i)
85                  ints[i] = new Integer(i);
86 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N, false, Arrays.asList(ints));
87 <            fail("Cannot make with null elements");
86 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
87 >            shouldThrow();
88          }
89          catch (NullPointerException success) {}
90      }
91  
92 <    public void testConstructor6(){
92 >    /**
93 >     * Initializing from too large collection throws IAE
94 >     */
95 >    public void testConstructor6() {
96          try {
97 <            Integer[] ints = new Integer[N];
98 <            for (int i = 0; i < N; ++i)
97 >            Integer[] ints = new Integer[SIZE];
98 >            for (int i = 0; i < SIZE; ++i)
99                  ints[i] = new Integer(i);
100              ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints));
101 <            fail("Cannot make with insufficient capacity");
101 >            shouldThrow();
102          }
103          catch (IllegalArgumentException success) {}
104      }
105  
106 <    public void testConstructor7(){
106 >    /**
107 >     * Queue contains all elements of collection used to initialize
108 >     */
109 >    public void testConstructor7() {
110          try {
111 <            Integer[] ints = new Integer[N];
112 <            for (int i = 0; i < N; ++i)
111 >            Integer[] ints = new Integer[SIZE];
112 >            for (int i = 0; i < SIZE; ++i)
113                  ints[i] = new Integer(i);
114 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N, true, Arrays.asList(ints));
115 <            for (int i = 0; i < N; ++i)
114 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints));
115 >            for (int i = 0; i < SIZE; ++i)
116                  assertEquals(ints[i], q.poll());
117          }
118          finally {}
119      }
120  
121 +    /**
122 +     * Queue transitions from empty to full when elements added
123 +     */
124      public void testEmptyFull() {
125          ArrayBlockingQueue q = new ArrayBlockingQueue(2);
126          assertTrue(q.isEmpty());
127 <        assertEquals("should have room for 2", 2, q.remainingCapacity());
128 <        q.add(new Integer(1));
127 >        assertEquals(2, q.remainingCapacity());
128 >        q.add(one);
129          assertFalse(q.isEmpty());
130 <        q.add(new Integer(2));
130 >        q.add(two);
131          assertFalse(q.isEmpty());
132 <        assertEquals("queue should be full", 0, q.remainingCapacity());
133 <        assertFalse("offer should be rejected", q.offer(new Integer(3)));
132 >        assertEquals(0, q.remainingCapacity());
133 >        assertFalse(q.offer(three));
134      }
135  
136 <    public void testRemainingCapacity(){
137 <        ArrayBlockingQueue q = fullQueue(N);
138 <        for (int i = 0; i < N; ++i) {
136 >    /**
137 >     * remainingCapacity decreases on add, increases on remove
138 >     */
139 >    public void testRemainingCapacity() {
140 >        ArrayBlockingQueue q = populatedQueue(SIZE);
141 >        for (int i = 0; i < SIZE; ++i) {
142              assertEquals(i, q.remainingCapacity());
143 <            assertEquals(N-i, q.size());
143 >            assertEquals(SIZE-i, q.size());
144              q.remove();
145          }
146 <        for (int i = 0; i < N; ++i) {
147 <            assertEquals(N-i, q.remainingCapacity());
146 >        for (int i = 0; i < SIZE; ++i) {
147 >            assertEquals(SIZE-i, q.remainingCapacity());
148              assertEquals(i, q.size());
149              q.add(new Integer(i));
150          }
151      }
152  
153 <    public void testOfferNull(){
153 >    /**
154 >     *  offer(null) throws NPE
155 >     */
156 >    public void testOfferNull() {
157          try {
158              ArrayBlockingQueue q = new ArrayBlockingQueue(1);
159              q.offer(null);
160 <            fail("should throw NPE");
160 >            shouldThrow();
161          } catch (NullPointerException success) { }  
162      }
163  
164 <    public void testOffer(){
164 >    /**
165 >     * Offer succeeds if not full; fails if full
166 >     */
167 >    public void testOffer() {
168          ArrayBlockingQueue q = new ArrayBlockingQueue(1);
169 <        assertTrue(q.offer(new Integer(0)));
170 <        assertFalse(q.offer(new Integer(1)));
169 >        assertTrue(q.offer(zero));
170 >        assertFalse(q.offer(one));
171      }
172  
173 <    public void testAdd(){
173 >    /**
174 >     * add succeeds if not full; throws ISE if full
175 >     */
176 >    public void testAdd() {
177          try {
178 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N);
179 <            for (int i = 0; i < N; ++i) {
178 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
179 >            for (int i = 0; i < SIZE; ++i) {
180                  assertTrue(q.add(new Integer(i)));
181              }
182              assertEquals(0, q.remainingCapacity());
183 <            q.add(new Integer(N));
183 >            q.add(new Integer(SIZE));
184          } catch (IllegalStateException success){
185          }  
186      }
187  
188 <    public void testAddAll1(){
188 >    /**
189 >     *  addAll(null) throws NPE
190 >     */
191 >    public void testAddAll1() {
192          try {
193              ArrayBlockingQueue q = new ArrayBlockingQueue(1);
194              q.addAll(null);
195 <            fail("Cannot add null collection");
195 >            shouldThrow();
196          }
197          catch (NullPointerException success) {}
198      }
199 <    public void testAddAll2(){
199 >    /**
200 >     *  addAll of a collection with null elements throws NPE
201 >     */
202 >    public void testAddAll2() {
203          try {
204 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N);
205 <            Integer[] ints = new Integer[N];
204 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
205 >            Integer[] ints = new Integer[SIZE];
206              q.addAll(Arrays.asList(ints));
207 <            fail("Cannot add null elements");
207 >            shouldThrow();
208          }
209          catch (NullPointerException success) {}
210      }
211 <    public void testAddAll3(){
211 >    /**
212 >     * addAll of a collection with any null elements throws NPE after
213 >     * possibly adding some elements
214 >     */
215 >    public void testAddAll3() {
216          try {
217 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N);
218 <            Integer[] ints = new Integer[N];
219 <            for (int i = 0; i < N-1; ++i)
217 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
218 >            Integer[] ints = new Integer[SIZE];
219 >            for (int i = 0; i < SIZE-1; ++i)
220                  ints[i] = new Integer(i);
221              q.addAll(Arrays.asList(ints));
222 <            fail("Cannot add null elements");
222 >            shouldThrow();
223          }
224          catch (NullPointerException success) {}
225      }
226 <    public void testAddAll4(){
226 >    /**
227 >     * addAll throws ISE if not enough room
228 >     */
229 >    public void testAddAll4() {
230          try {
231              ArrayBlockingQueue q = new ArrayBlockingQueue(1);
232 <            Integer[] ints = new Integer[N];
233 <            for (int i = 0; i < N; ++i)
232 >            Integer[] ints = new Integer[SIZE];
233 >            for (int i = 0; i < SIZE; ++i)
234                  ints[i] = new Integer(i);
235              q.addAll(Arrays.asList(ints));
236 <            fail("Cannot add with insufficient capacity");
236 >            shouldThrow();
237          }
238          catch (IllegalStateException success) {}
239      }
240 <    public void testAddAll5(){
240 >    /**
241 >     * Queue contains all elements, in traversal order, of successful addAll
242 >     */
243 >    public void testAddAll5() {
244          try {
245              Integer[] empty = new Integer[0];
246 <            Integer[] ints = new Integer[N];
247 <            for (int i = 0; i < N; ++i)
246 >            Integer[] ints = new Integer[SIZE];
247 >            for (int i = 0; i < SIZE; ++i)
248                  ints[i] = new Integer(i);
249 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N);
249 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
250              assertFalse(q.addAll(Arrays.asList(empty)));
251              assertTrue(q.addAll(Arrays.asList(ints)));
252 <            for (int i = 0; i < N; ++i)
252 >            for (int i = 0; i < SIZE; ++i)
253                  assertEquals(ints[i], q.poll());
254          }
255          finally {}
256      }
257  
258 +    /**
259 +     *  put(null) throws NPE
260 +     */
261       public void testPutNull() {
262          try {
263 <            ArrayBlockingQueue q = new ArrayBlockingQueue(N);
263 >            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
264              q.put(null);
265 <            fail("put should throw NPE");
265 >            shouldThrow();
266          }
267          catch (NullPointerException success){
268          }  
269          catch (InterruptedException ie) {
270 <            fail("Unexpected exception");
270 >            unexpectedException();
271          }
272       }
273  
274 +    /**
275 +     * all elements successfully put are contained
276 +     */
277       public void testPut() {
278           try {
279 <             ArrayBlockingQueue q = new ArrayBlockingQueue(N);
280 <             for (int i = 0; i < N; ++i) {
279 >             ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
280 >             for (int i = 0; i < SIZE; ++i) {
281                   Integer I = new Integer(i);
282                   q.put(I);
283                   assertTrue(q.contains(I));
# Line 233 | Line 285 | public class ArrayBlockingQueueTest exte
285               assertEquals(0, q.remainingCapacity());
286           }
287          catch (InterruptedException ie) {
288 <            fail("Unexpected exception");
288 >            unexpectedException();
289          }
290      }
291  
292 <    public void testBlockingPut(){
292 >    /**
293 >     * put blocks interruptibly if full
294 >     */
295 >    public void testBlockingPut() {
296          Thread t = new Thread(new Runnable() {
297                  public void run() {
298                      int added = 0;
299                      try {
300 <                        ArrayBlockingQueue q = new ArrayBlockingQueue(N);
301 <                        for (int i = 0; i < N; ++i) {
300 >                        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
301 >                        for (int i = 0; i < SIZE; ++i) {
302                              q.put(new Integer(i));
303                              ++added;
304                          }
305 <                        q.put(new Integer(N));
306 <                        fail("put should block");
305 >                        q.put(new Integer(SIZE));
306 >                        threadShouldThrow();
307                      } catch (InterruptedException ie){
308 <                        assertEquals(added, N);
308 >                        threadAssertEquals(added, SIZE);
309                      }  
310                  }});
256        t.start();
311          try {
312 +            t.start();
313             Thread.sleep(SHORT_DELAY_MS);
314             t.interrupt();
315             t.join();
316          }
317          catch (InterruptedException ie) {
318 <            fail("Unexpected exception");
318 >            unexpectedException();
319          }
320      }
321  
322 +    /**
323 +     * put blocks waiting for take when full
324 +     */
325      public void testPutWithTake() {
326          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
327          Thread t = new Thread(new Runnable() {
328 <                public void run(){
328 >                public void run() {
329                      int added = 0;
330                      try {
331                          q.put(new Object());
# Line 278 | Line 336 | public class ArrayBlockingQueueTest exte
336                          ++added;
337                          q.put(new Object());
338                          ++added;
339 <                        fail("Should block");
339 >                        threadShouldThrow();
340                      } catch (InterruptedException e){
341 <                        assertTrue(added >= 2);
341 >                        threadAssertTrue(added >= 2);
342                      }
343                  }
344              });
# Line 291 | Line 349 | public class ArrayBlockingQueueTest exte
349              t.interrupt();
350              t.join();
351          } catch (Exception e){
352 <            fail("Unexpected exception");
352 >            unexpectedException();
353          }
354      }
355  
356 +    /**
357 +     * timed offer times out if full and elements not taken
358 +     */
359      public void testTimedOffer() {
360          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
361          Thread t = new Thread(new Runnable() {
362 <                public void run(){
362 >                public void run() {
363                      try {
364                          q.put(new Object());
365                          q.put(new Object());
366 <                        assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
366 >                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
367                          q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
368 <                        fail("Should block");
368 >                        threadShouldThrow();
369                      } catch (InterruptedException success){}
370                  }
371              });
# Line 315 | Line 376 | public class ArrayBlockingQueueTest exte
376              t.interrupt();
377              t.join();
378          } catch (Exception e){
379 <            fail("Unexpected exception");
379 >            unexpectedException();
380          }
381      }
382  
383 <    public void testTake(){
383 >    /**
384 >     * take retrieves elements in FIFO order
385 >     */
386 >    public void testTake() {
387          try {
388 <            ArrayBlockingQueue q = fullQueue(N);
389 <            for (int i = 0; i < N; ++i) {
388 >            ArrayBlockingQueue q = populatedQueue(SIZE);
389 >            for (int i = 0; i < SIZE; ++i) {
390                  assertEquals(i, ((Integer)q.take()).intValue());
391              }
392          } catch (InterruptedException e){
393 <            fail("Unexpected exception");
393 >            unexpectedException();
394          }  
395      }
396  
397 +    /**
398 +     * take blocks interruptibly when empty
399 +     */
400      public void testTakeFromEmpty() {
401          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
402          Thread t = new Thread(new Runnable() {
403 <                public void run(){
403 >                public void run() {
404                      try {
405                          q.take();
406 <                        fail("Should block");
406 >                        threadShouldThrow();
407                      } catch (InterruptedException success){ }                
408                  }
409              });
# Line 346 | Line 413 | public class ArrayBlockingQueueTest exte
413              t.interrupt();
414              t.join();
415          } catch (Exception e){
416 <            fail("Unexpected exception");
416 >            unexpectedException();
417          }
418      }
419  
420 <    public void testBlockingTake(){
420 >    /**
421 >     * Take removes existing elements until empty, then blocks interruptibly
422 >     */
423 >    public void testBlockingTake() {
424          Thread t = new Thread(new Runnable() {
425                  public void run() {
426                      try {
427 <                        ArrayBlockingQueue q = fullQueue(N);
428 <                        for (int i = 0; i < N; ++i) {
429 <                            assertEquals(i, ((Integer)q.take()).intValue());
427 >                        ArrayBlockingQueue q = populatedQueue(SIZE);
428 >                        for (int i = 0; i < SIZE; ++i) {
429 >                            threadAssertEquals(i, ((Integer)q.take()).intValue());
430                          }
431                          q.take();
432 <                        fail("take should block");
432 >                        threadShouldThrow();
433                      } catch (InterruptedException success){
434                      }  
435                  }});
366        t.start();
436          try {
437 <           Thread.sleep(SHORT_DELAY_MS);
438 <           t.interrupt();
439 <           t.join();
437 >            t.start();
438 >            Thread.sleep(SHORT_DELAY_MS);
439 >            t.interrupt();
440 >            t.join();
441          }
442          catch (InterruptedException ie) {
443 <            fail("Unexpected exception");
443 >            unexpectedException();
444          }
445      }
446  
447  
448 <    public void testPoll(){
449 <        ArrayBlockingQueue q = fullQueue(N);
450 <        for (int i = 0; i < N; ++i) {
448 >    /**
449 >     * poll succeeds unless empty
450 >     */
451 >    public void testPoll() {
452 >        ArrayBlockingQueue q = populatedQueue(SIZE);
453 >        for (int i = 0; i < SIZE; ++i) {
454              assertEquals(i, ((Integer)q.poll()).intValue());
455          }
456          assertNull(q.poll());
457      }
458  
459 +    /**
460 +     * timed pool with zero timeout succeeds when non-empty, else times out
461 +     */
462      public void testTimedPoll0() {
463          try {
464 <            ArrayBlockingQueue q = fullQueue(N);
465 <            for (int i = 0; i < N; ++i) {
464 >            ArrayBlockingQueue q = populatedQueue(SIZE);
465 >            for (int i = 0; i < SIZE; ++i) {
466                  assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
467              }
468              assertNull(q.poll(0, TimeUnit.MILLISECONDS));
469          } catch (InterruptedException e){
470 <            fail("Unexpected exception");
470 >            unexpectedException();
471          }  
472      }
473  
474 +    /**
475 +     * timed pool with nonzero timeout succeeds when non-empty, else times out
476 +     */
477      public void testTimedPoll() {
478          try {
479 <            ArrayBlockingQueue q = fullQueue(N);
480 <            for (int i = 0; i < N; ++i) {
479 >            ArrayBlockingQueue q = populatedQueue(SIZE);
480 >            for (int i = 0; i < SIZE; ++i) {
481                  assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
482              }
483              assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
484          } catch (InterruptedException e){
485 <            fail("Unexpected exception");
485 >            unexpectedException();
486          }  
487      }
488  
489 <    public void testInterruptedTimedPoll(){
489 >    /**
490 >     * Interrupted timed poll throws InterruptedException instead of
491 >     * returning timeout status
492 >     */
493 >    public void testInterruptedTimedPoll() {
494          Thread t = new Thread(new Runnable() {
495                  public void run() {
496                      try {
497 <                        ArrayBlockingQueue q = fullQueue(N);
498 <                        for (int i = 0; i < N; ++i) {
499 <                            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
497 >                        ArrayBlockingQueue q = populatedQueue(SIZE);
498 >                        for (int i = 0; i < SIZE; ++i) {
499 >                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
500                          }
501 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
501 >                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
502                      } catch (InterruptedException success){
503                      }  
504                  }});
422        t.start();
505          try {
506 <           Thread.sleep(SHORT_DELAY_MS);
507 <           t.interrupt();
508 <           t.join();
506 >            t.start();
507 >            Thread.sleep(SHORT_DELAY_MS);
508 >            t.interrupt();
509 >            t.join();
510          }
511          catch (InterruptedException ie) {
512 <            fail("Unexpected exception");
512 >            unexpectedException();
513          }
514      }
515  
516 <    public void testTimedPollWithOffer(){
516 >    /**
517 >     *  timed poll before a delayed offer fails; after offer succeeds;
518 >     *  on interruption throws
519 >     */
520 >    public void testTimedPollWithOffer() {
521          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
522          Thread t = new Thread(new Runnable() {
523 <                public void run(){
523 >                public void run() {
524                      try {
525 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
525 >                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
526                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
527                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
528 <                        fail("Should block");
528 >                        threadShouldThrow();
529                      } catch (InterruptedException success) { }                
530                  }
531              });
532          try {
533              t.start();
534 <            Thread.sleep(SHORT_DELAY_MS * 2);
535 <            assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
534 >            Thread.sleep(SMALL_DELAY_MS);
535 >            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
536              t.interrupt();
537              t.join();
538          } catch (Exception e){
539 <            fail("Unexpected exception");
539 >            unexpectedException();
540          }
541      }  
542  
543  
544 <    public void testPeek(){
545 <        ArrayBlockingQueue q = fullQueue(N);
546 <        for (int i = 0; i < N; ++i) {
544 >    /**
545 >     * peek returns next element, or null if empty
546 >     */
547 >    public void testPeek() {
548 >        ArrayBlockingQueue q = populatedQueue(SIZE);
549 >        for (int i = 0; i < SIZE; ++i) {
550              assertEquals(i, ((Integer)q.peek()).intValue());
551              q.poll();
552              assertTrue(q.peek() == null ||
# Line 465 | Line 555 | public class ArrayBlockingQueueTest exte
555          assertNull(q.peek());
556      }
557  
558 <    public void testElement(){
559 <        ArrayBlockingQueue q = fullQueue(N);
560 <        for (int i = 0; i < N; ++i) {
558 >    /**
559 >     * element returns next element, or throws NSEE if empty
560 >     */
561 >    public void testElement() {
562 >        ArrayBlockingQueue q = populatedQueue(SIZE);
563 >        for (int i = 0; i < SIZE; ++i) {
564              assertEquals(i, ((Integer)q.element()).intValue());
565              q.poll();
566          }
567          try {
568              q.element();
569 <            fail("no such element");
569 >            shouldThrow();
570          }
571          catch (NoSuchElementException success) {}
572      }
573  
574 <    public void testRemove(){
575 <        ArrayBlockingQueue q = fullQueue(N);
576 <        for (int i = 0; i < N; ++i) {
574 >    /**
575 >     * remove removes next element, or throws NSEE if empty
576 >     */
577 >    public void testRemove() {
578 >        ArrayBlockingQueue q = populatedQueue(SIZE);
579 >        for (int i = 0; i < SIZE; ++i) {
580              assertEquals(i, ((Integer)q.remove()).intValue());
581          }
582          try {
583              q.remove();
584 <            fail("remove should throw");
584 >            shouldThrow();
585          } catch (NoSuchElementException success){
586          }  
587      }
588  
589 <    public void testRemoveElement(){
590 <        ArrayBlockingQueue q = fullQueue(N);
591 <        for (int i = 1; i < N; i+=2) {
589 >    /**
590 >     * remove(x) removes x and returns true if present
591 >     */
592 >    public void testRemoveElement() {
593 >        ArrayBlockingQueue q = populatedQueue(SIZE);
594 >        for (int i = 1; i < SIZE; i+=2) {
595              assertTrue(q.remove(new Integer(i)));
596          }
597 <        for (int i = 0; i < N; i+=2) {
597 >        for (int i = 0; i < SIZE; i+=2) {
598              assertTrue(q.remove(new Integer(i)));
599              assertFalse(q.remove(new Integer(i+1)));
600          }
601 <        assert(q.isEmpty());
601 >        assertTrue(q.isEmpty());
602      }
603          
604 <    public void testContains(){
605 <        ArrayBlockingQueue q = fullQueue(N);
606 <        for (int i = 0; i < N; ++i) {
604 >    /**
605 >     * contains(x) reports true when elements added but not yet removed
606 >     */
607 >    public void testContains() {
608 >        ArrayBlockingQueue q = populatedQueue(SIZE);
609 >        for (int i = 0; i < SIZE; ++i) {
610              assertTrue(q.contains(new Integer(i)));
611              q.poll();
612              assertFalse(q.contains(new Integer(i)));
613          }
614      }
615  
616 <    public void testClear(){
617 <        ArrayBlockingQueue q = fullQueue(N);
616 >    /**
617 >     * clear removes all elements
618 >     */
619 >    public void testClear() {
620 >        ArrayBlockingQueue q = populatedQueue(SIZE);
621          q.clear();
622          assertTrue(q.isEmpty());
623          assertEquals(0, q.size());
624 <        assertEquals(N, q.remainingCapacity());
625 <        q.add(new Integer(1));
624 >        assertEquals(SIZE, q.remainingCapacity());
625 >        q.add(one);
626          assertFalse(q.isEmpty());
627          q.clear();
628          assertTrue(q.isEmpty());
629      }
630  
631 <    public void testContainsAll(){
632 <        ArrayBlockingQueue q = fullQueue(N);
633 <        ArrayBlockingQueue p = new ArrayBlockingQueue(N);
634 <        for (int i = 0; i < N; ++i) {
631 >    /**
632 >     * containsAll(c) is true when c contains a subset of elements
633 >     */
634 >    public void testContainsAll() {
635 >        ArrayBlockingQueue q = populatedQueue(SIZE);
636 >        ArrayBlockingQueue p = new ArrayBlockingQueue(SIZE);
637 >        for (int i = 0; i < SIZE; ++i) {
638              assertTrue(q.containsAll(p));
639              assertFalse(p.containsAll(q));
640              p.add(new Integer(i));
# Line 534 | Line 642 | public class ArrayBlockingQueueTest exte
642          assertTrue(p.containsAll(q));
643      }
644  
645 <    public void testRetainAll(){
646 <        ArrayBlockingQueue q = fullQueue(N);
647 <        ArrayBlockingQueue p = fullQueue(N);
648 <        for (int i = 0; i < N; ++i) {
645 >    /**
646 >     * retainAll(c) retains only those elements of c and reports true if changed
647 >     */
648 >    public void testRetainAll() {
649 >        ArrayBlockingQueue q = populatedQueue(SIZE);
650 >        ArrayBlockingQueue p = populatedQueue(SIZE);
651 >        for (int i = 0; i < SIZE; ++i) {
652              boolean changed = q.retainAll(p);
653              if (i == 0)
654                  assertFalse(changed);
# Line 545 | Line 656 | public class ArrayBlockingQueueTest exte
656                  assertTrue(changed);
657  
658              assertTrue(q.containsAll(p));
659 <            assertEquals(N-i, q.size());
659 >            assertEquals(SIZE-i, q.size());
660              p.remove();
661          }
662      }
663  
664 <    public void testRemoveAll(){
665 <        for (int i = 1; i < N; ++i) {
666 <            ArrayBlockingQueue q = fullQueue(N);
667 <            ArrayBlockingQueue p = fullQueue(i);
664 >    /**
665 >     * removeAll(c) removes only those elements of c and reports true if changed
666 >     */
667 >    public void testRemoveAll() {
668 >        for (int i = 1; i < SIZE; ++i) {
669 >            ArrayBlockingQueue q = populatedQueue(SIZE);
670 >            ArrayBlockingQueue p = populatedQueue(i);
671              assertTrue(q.removeAll(p));
672 <            assertEquals(N-i, q.size());
672 >            assertEquals(SIZE-i, q.size());
673              for (int j = 0; j < i; ++j) {
674                  Integer I = (Integer)(p.remove());
675                  assertFalse(q.contains(I));
# Line 563 | Line 677 | public class ArrayBlockingQueueTest exte
677          }
678      }
679  
680 <
681 <    public void testToArray(){
682 <        ArrayBlockingQueue q = fullQueue(N);
680 >    /**
681 >     *  toArray contains all elements
682 >     */
683 >    public void testToArray() {
684 >        ArrayBlockingQueue q = populatedQueue(SIZE);
685          Object[] o = q.toArray();
686          try {
687          for(int i = 0; i < o.length; i++)
688              assertEquals(o[i], q.take());
689          } catch (InterruptedException e){
690 <            fail("Unexpected exception");
690 >            unexpectedException();
691          }    
692      }
693  
694 <    public void testToArray2(){
695 <        ArrayBlockingQueue q = fullQueue(N);
696 <        Integer[] ints = new Integer[N];
694 >    /**
695 >     * toArray(a) contains all elements
696 >     */
697 >    public void testToArray2() {
698 >        ArrayBlockingQueue q = populatedQueue(SIZE);
699 >        Integer[] ints = new Integer[SIZE];
700          ints = (Integer[])q.toArray(ints);
701          try {
702              for(int i = 0; i < ints.length; i++)
703                  assertEquals(ints[i], q.take());
704          } catch (InterruptedException e){
705 <            fail("Unexpected exception");
705 >            unexpectedException();
706          }    
707      }
708      
709 <    public void testIterator(){
710 <        ArrayBlockingQueue q = fullQueue(N);
709 >    /**
710 >     * iterator iterates through all elements
711 >     */
712 >    public void testIterator() {
713 >        ArrayBlockingQueue q = populatedQueue(SIZE);
714          Iterator it = q.iterator();
715          try {
716              while(it.hasNext()){
717                  assertEquals(it.next(), q.take());
718              }
719          } catch (InterruptedException e){
720 <            fail("Unexpected exception");
720 >            unexpectedException();
721          }    
722      }
723  
724 <    public void testIteratorOrdering() {
725 <
724 >    /**
725 >     * iterator.remove removes current element
726 >     */
727 >    public void testIteratorRemove () {
728          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
729 +        q.add(two);
730 +        q.add(one);
731 +        q.add(three);
732 +
733 +        Iterator it = q.iterator();
734 +        it.next();
735 +        it.remove();
736 +        
737 +        it = q.iterator();
738 +        assertEquals(it.next(), one);
739 +        assertEquals(it.next(), three);
740 +        assertFalse(it.hasNext());
741 +    }
742  
743 <        q.add(new Integer(1));
744 <        q.add(new Integer(2));
745 <        q.add(new Integer(3));
743 >    /**
744 >     * iterator ordering is FIFO
745 >     */
746 >    public void testIteratorOrdering() {
747 >        final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
748 >        q.add(one);
749 >        q.add(two);
750 >        q.add(three);
751  
752          assertEquals("queue should be full", 0, q.remainingCapacity());
753  
754          int k = 0;
755          for (Iterator it = q.iterator(); it.hasNext();) {
756              int i = ((Integer)(it.next())).intValue();
757 <            assertEquals("items should come out in order", ++k, i);
757 >            assertEquals(++k, i);
758          }
759 <
618 <        assertEquals("should go through 3 elements", 3, k);
759 >        assertEquals(3, k);
760      }
761  
762 +    /**
763 +     * Modifications do not cause iterators to fail
764 +     */
765      public void testWeaklyConsistentIteration () {
622
766          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
767 <
768 <        q.add(new Integer(1));
769 <        q.add(new Integer(2));
627 <        q.add(new Integer(3));
628 <
767 >        q.add(one);
768 >        q.add(two);
769 >        q.add(three);
770          try {
771              for (Iterator it = q.iterator(); it.hasNext();) {
772                  q.remove();
# Line 633 | Line 774 | public class ArrayBlockingQueueTest exte
774              }
775          }
776          catch (ConcurrentModificationException e) {
777 <            fail("weakly consistent iterator; should not get CME");
777 >            unexpectedException();
778          }
779 <
639 <        assertEquals("queue should be empty again", 0, q.size());
779 >        assertEquals(0, q.size());
780      }
781  
782  
783 <    public void testToString(){
784 <        ArrayBlockingQueue q = fullQueue(N);
783 >    /**
784 >     * toString contains toStrings of elements
785 >     */
786 >    public void testToString() {
787 >        ArrayBlockingQueue q = populatedQueue(SIZE);
788          String s = q.toString();
789 <        for (int i = 0; i < N; ++i) {
789 >        for (int i = 0; i < SIZE; ++i) {
790              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
791          }
792      }        
793  
794  
795 +    /**
796 +     * offer transfers elements across Executor tasks
797 +     */
798      public void testOfferInExecutor() {
653
799          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
800 <
801 <        q.add(new Integer(1));
657 <        q.add(new Integer(2));
658 <
800 >        q.add(one);
801 >        q.add(two);
802          ExecutorService executor = Executors.newFixedThreadPool(2);
660
803          executor.execute(new Runnable() {
804              public void run() {
805 <                assertFalse("offer should be rejected", q.offer(new Integer(3)));
805 >                threadAssertFalse(q.offer(three));
806                  try {
807 <                    assertTrue("offer should be accepted", q.offer(new Integer(3), MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
808 <                    assertEquals(0, q.remainingCapacity());
807 >                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
808 >                    threadAssertEquals(0, q.remainingCapacity());
809                  }
810                  catch (InterruptedException e) {
811 <                    fail("should not be interrupted");
811 >                    threadUnexpectedException();
812                  }
813              }
814          });
# Line 674 | Line 816 | public class ArrayBlockingQueueTest exte
816          executor.execute(new Runnable() {
817              public void run() {
818                  try {
819 <                    Thread.sleep(MEDIUM_DELAY_MS);
820 <                    assertEquals("first item in queue should be 1", new Integer(1), q.take());
819 >                    Thread.sleep(SMALL_DELAY_MS);
820 >                    threadAssertEquals(one, q.take());
821                  }
822                  catch (InterruptedException e) {
823 <                    fail("should not be interrupted");
823 >                    threadUnexpectedException();
824                  }
825              }
826          });
827          
828 <        executor.shutdown();
828 >        joinPool(executor);
829  
830      }
831  
832 +    /**
833 +     * poll retrieves elements across Executor threads
834 +     */
835      public void testPollInExecutor() {
691
836          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
693
837          ExecutorService executor = Executors.newFixedThreadPool(2);
695
838          executor.execute(new Runnable() {
839              public void run() {
840 <                assertNull("poll should fail", q.poll());
840 >                threadAssertNull(q.poll());
841                  try {
842 <                    assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
843 <                    assertTrue(q.isEmpty());
842 >                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
843 >                    threadAssertTrue(q.isEmpty());
844                  }
845                  catch (InterruptedException e) {
846 <                    fail("should not be interrupted");
846 >                    threadUnexpectedException();
847                  }
848              }
849          });
# Line 709 | Line 851 | public class ArrayBlockingQueueTest exte
851          executor.execute(new Runnable() {
852              public void run() {
853                  try {
854 <                    Thread.sleep(MEDIUM_DELAY_MS);
855 <                    q.put(new Integer(1));
854 >                    Thread.sleep(SMALL_DELAY_MS);
855 >                    q.put(one);
856                  }
857                  catch (InterruptedException e) {
858 <                    fail("should not be interrupted");
858 >                    threadUnexpectedException();
859                  }
860              }
861          });
862          
863 <        executor.shutdown();
863 >        joinPool(executor);
864 >    }
865  
866 +    /**
867 +     * A deserialized serialized queue has same elements in same order
868 +     */
869 +    public void testSerialization() {
870 +        ArrayBlockingQueue q = populatedQueue(SIZE);
871 +
872 +        try {
873 +            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
874 +            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
875 +            out.writeObject(q);
876 +            out.close();
877 +
878 +            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
879 +            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
880 +            ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
881 +            assertEquals(q.size(), r.size());
882 +            while (!q.isEmpty())
883 +                assertEquals(q.remove(), r.remove());
884 +        } catch(Exception e){
885 +            unexpectedException();
886 +        }
887      }
888  
889 +
890   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines