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

Comparing jsr166/src/test/tck/BlockingQueueTest.java (file contents):
Revision 1.3 by jsr166, Fri Oct 29 07:00:32 2010 UTC vs.
Revision 1.12 by jsr166, Fri Jul 15 18:49:31 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea and Martin Buchholz with assistance from members
3   * of JCP JSR-166 Expert Group and released to the public domain, as
4 < * explained at http://creativecommons.org/licenses/publicdomain
4 > * explained at http://creativecommons.org/publicdomain/zero/1.0/
5   *
6   * Other contributors include Andrew Wright, Jeffrey Hayes,
7   * Pat Fisher, Mike Judd.
8   */
9  
10   import junit.framework.*;
11 < import java.util.*;
12 < import java.util.concurrent.*;
11 > import java.util.ArrayList;
12 > import java.util.Arrays;
13 > import java.util.Collection;
14 > import java.util.Queue;
15 > import java.util.concurrent.BlockingQueue;
16 > import java.util.concurrent.CountDownLatch;
17   import static java.util.concurrent.TimeUnit.MILLISECONDS;
18  
19   /**
20 < * Contains tests generally applicable to BlockingQueue implementations.
20 > * Contains "contract" tests applicable to all BlockingQueue implementations.
21   */
22   public abstract class BlockingQueueTest extends JSR166TestCase {
23      /*
# Line 33 | Line 37 | public abstract class BlockingQueueTest
37          return new TestSuite(this.getClass());
38      }
39  
40 +    //----------------------------------------------------------------
41 +    // Configuration methods
42 +    //----------------------------------------------------------------
43 +
44      /** Returns an empty instance of the implementation class. */
45      protected abstract BlockingQueue emptyCollection();
46  
47      /**
48 <     * timed poll before a delayed offer fails; after offer succeeds;
48 >     * Returns an element suitable for insertion in the collection.
49 >     * Override for collections with unusual element types.
50 >     */
51 >    protected Object makeElement(int i) {
52 >        return Integer.valueOf(i);
53 >    }
54 >
55 >    //----------------------------------------------------------------
56 >    // Tests
57 >    //----------------------------------------------------------------
58 >
59 >    /**
60 >     * offer(null) throws NullPointerException
61 >     */
62 >    public void testOfferNull() {
63 >        final Queue q = emptyCollection();
64 >        try {
65 >            q.offer(null);
66 >            shouldThrow();
67 >        } catch (NullPointerException success) {}
68 >    }
69 >
70 >    /**
71 >     * add(null) throws NullPointerException
72 >     */
73 >    public void testAddNull() {
74 >        final Collection q = emptyCollection();
75 >        try {
76 >            q.add(null);
77 >            shouldThrow();
78 >        } catch (NullPointerException success) {}
79 >    }
80 >
81 >    /**
82 >     * timed offer(null) throws NullPointerException
83 >     */
84 >    public void testTimedOfferNull() throws InterruptedException {
85 >        final BlockingQueue q = emptyCollection();
86 >        long startTime = System.nanoTime();
87 >        try {
88 >            q.offer(null, LONG_DELAY_MS, MILLISECONDS);
89 >            shouldThrow();
90 >        } catch (NullPointerException success) {}
91 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
92 >    }
93 >
94 >    /**
95 >     * put(null) throws NullPointerException
96 >     */
97 >    public void testPutNull() throws InterruptedException {
98 >        final BlockingQueue q = emptyCollection();
99 >        try {
100 >            q.put(null);
101 >            shouldThrow();
102 >        } catch (NullPointerException success) {}
103 >    }
104 >
105 >    /**
106 >     * put(null) throws NullPointerException
107 >     */
108 >    public void testAddAllNull() throws InterruptedException {
109 >        final Collection q = emptyCollection();
110 >        try {
111 >            q.addAll(null);
112 >            shouldThrow();
113 >        } catch (NullPointerException success) {}
114 >    }
115 >
116 >    /**
117 >     * addAll of a collection with null elements throws NullPointerException
118 >     */
119 >    public void testAddAllNullElements() {
120 >        final Collection q = emptyCollection();
121 >        final Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
122 >        try {
123 >            q.addAll(elements);
124 >            shouldThrow();
125 >        } catch (NullPointerException success) {}
126 >    }
127 >
128 >    /**
129 >     * toArray(null) throws NullPointerException
130 >     */
131 >    public void testToArray_NullArray() {
132 >        final Collection q = emptyCollection();
133 >        try {
134 >            q.toArray(null);
135 >            shouldThrow();
136 >        } catch (NullPointerException success) {}
137 >    }
138 >
139 >    /**
140 >     * drainTo(null) throws NullPointerException
141 >     */
142 >    public void testDrainToNull() {
143 >        final BlockingQueue q = emptyCollection();
144 >        try {
145 >            q.drainTo(null);
146 >            shouldThrow();
147 >        } catch (NullPointerException success) {}
148 >    }
149 >
150 >    /**
151 >     * drainTo(this) throws IllegalArgumentException
152 >     */
153 >    public void testDrainToSelf() {
154 >        final BlockingQueue q = emptyCollection();
155 >        try {
156 >            q.drainTo(q);
157 >            shouldThrow();
158 >        } catch (IllegalArgumentException success) {}
159 >    }
160 >
161 >    /**
162 >     * drainTo(null, n) throws NullPointerException
163 >     */
164 >    public void testDrainToNullN() {
165 >        final BlockingQueue q = emptyCollection();
166 >        try {
167 >            q.drainTo(null, 0);
168 >            shouldThrow();
169 >        } catch (NullPointerException success) {}
170 >    }
171 >
172 >    /**
173 >     * drainTo(this, n) throws IllegalArgumentException
174 >     */
175 >    public void testDrainToSelfN() {
176 >        final BlockingQueue q = emptyCollection();
177 >        try {
178 >            q.drainTo(q, 0);
179 >            shouldThrow();
180 >        } catch (IllegalArgumentException success) {}
181 >    }
182 >
183 >    /**
184 >     * drainTo(c, n) returns 0 and does nothing when n <= 0
185 >     */
186 >    public void testDrainToNonPositiveMaxElements() {
187 >        final BlockingQueue q = emptyCollection();
188 >        final int[] ns = { 0, -1, -42, Integer.MIN_VALUE };
189 >        for (int n : ns)
190 >            assertEquals(0, q.drainTo(new ArrayList(), n));
191 >        if (q.remainingCapacity() > 0) {
192 >            // Not SynchronousQueue, that is
193 >            Object one = makeElement(1);
194 >            q.add(one);
195 >            ArrayList c = new ArrayList();
196 >            for (int n : ns)
197 >                assertEquals(0, q.drainTo(new ArrayList(), n));
198 >            assertEquals(1, q.size());
199 >            assertSame(one, q.poll());
200 >            assertTrue(c.isEmpty());
201 >        }
202 >    }
203 >
204 >    /**
205 >     * timed poll before a delayed offer times out; after offer succeeds;
206       * on interruption throws
207       */
208      public void testTimedPollWithOffer() throws InterruptedException {
209          final BlockingQueue q = emptyCollection();
210          final CheckedBarrier barrier = new CheckedBarrier(2);
211 +        final Object zero = makeElement(0);
212          Thread t = newStartedThread(new CheckedRunnable() {
213              public void realRun() throws InterruptedException {
214 <                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
214 >                long startTime = System.nanoTime();
215 >                assertNull(q.poll(timeoutMillis(), MILLISECONDS));
216 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
217  
218                  barrier.await();
219 <                assertSame(zero, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
219 >
220 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
221  
222                  Thread.currentThread().interrupt();
223                  try {
224 <                    q.poll(SHORT_DELAY_MS, MILLISECONDS);
224 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
225                      shouldThrow();
226                  } catch (InterruptedException success) {}
227 +                assertFalse(Thread.interrupted());
228  
229                  barrier.await();
230                  try {
231 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
231 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
232                      shouldThrow();
233                  } catch (InterruptedException success) {}
234 +                assertFalse(Thread.interrupted());
235              }});
236  
237          barrier.await();
238 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
238 >        long startTime = System.nanoTime();
239 >        assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS));
240 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
241 >
242          barrier.await();
243 <        sleep(SHORT_DELAY_MS);
243 >        assertThreadStaysAlive(t);
244          t.interrupt();
245 <        awaitTermination(t, MEDIUM_DELAY_MS);
245 >        awaitTermination(t);
246      }
247  
248      /**
249       * take() blocks interruptibly when empty
250       */
251 <    public void testTakeFromEmptyBlocksInterruptibly()
78 <            throws InterruptedException {
251 >    public void testTakeFromEmptyBlocksInterruptibly() {
252          final BlockingQueue q = emptyCollection();
253          final CountDownLatch threadStarted = new CountDownLatch(1);
254          Thread t = newStartedThread(new CheckedRunnable() {
255              public void realRun() {
83                long t0 = System.nanoTime();
256                  threadStarted.countDown();
257                  try {
258                      q.take();
259                      shouldThrow();
260                  } catch (InterruptedException success) {}
261 <                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
261 >                assertFalse(Thread.interrupted());
262              }});
263  
264 <        threadStarted.await();
265 <        Thread.sleep(SHORT_DELAY_MS);
94 <        assertTrue(t.isAlive());
264 >        await(threadStarted);
265 >        assertThreadStaysAlive(t);
266          t.interrupt();
267 <        awaitTermination(t, MEDIUM_DELAY_MS);
267 >        awaitTermination(t);
268      }
269  
270      /**
271       * take() throws InterruptedException immediately if interrupted
272       * before waiting
273       */
274 <    public void testTakeFromEmptyAfterInterrupt()
104 <            throws InterruptedException {
274 >    public void testTakeFromEmptyAfterInterrupt() {
275          final BlockingQueue q = emptyCollection();
276          Thread t = newStartedThread(new CheckedRunnable() {
277              public void realRun() {
108                long t0 = System.nanoTime();
278                  Thread.currentThread().interrupt();
279                  try {
280                      q.take();
281                      shouldThrow();
282                  } catch (InterruptedException success) {}
283 <                assertTrue(millisElapsedSince(t0) < SHORT_DELAY_MS);
283 >                assertFalse(Thread.interrupted());
284 >            }});
285 >
286 >        awaitTermination(t);
287 >    }
288 >
289 >    /**
290 >     * timed poll() blocks interruptibly when empty
291 >     */
292 >    public void testTimedPollFromEmptyBlocksInterruptibly() {
293 >        final BlockingQueue q = emptyCollection();
294 >        final CountDownLatch threadStarted = new CountDownLatch(1);
295 >        Thread t = newStartedThread(new CheckedRunnable() {
296 >            public void realRun() {
297 >                threadStarted.countDown();
298 >                try {
299 >                    q.poll(2 * LONG_DELAY_MS, MILLISECONDS);
300 >                    shouldThrow();
301 >                } catch (InterruptedException success) {}
302 >                assertFalse(Thread.interrupted());
303 >            }});
304 >
305 >        await(threadStarted);
306 >        assertThreadStaysAlive(t);
307 >        t.interrupt();
308 >        awaitTermination(t);
309 >    }
310 >
311 >    /**
312 >     * timed poll() throws InterruptedException immediately if
313 >     * interrupted before waiting
314 >     */
315 >    public void testTimedPollFromEmptyAfterInterrupt() {
316 >        final BlockingQueue q = emptyCollection();
317 >        Thread t = newStartedThread(new CheckedRunnable() {
318 >            public void realRun() {
319 >                Thread.currentThread().interrupt();
320 >                try {
321 >                    q.poll(2 * LONG_DELAY_MS, MILLISECONDS);
322 >                    shouldThrow();
323 >                } catch (InterruptedException success) {}
324 >                assertFalse(Thread.interrupted());
325              }});
326  
327 <        awaitTermination(t, MEDIUM_DELAY_MS);
327 >        awaitTermination(t);
328 >    }
329 >
330 >    /**
331 >     * remove(x) removes x and returns true if present
332 >     * TODO: move to superclass CollectionTest.java
333 >     */
334 >    public void testRemoveElement() {
335 >        final BlockingQueue q = emptyCollection();
336 >        final int size = Math.min(q.remainingCapacity(), SIZE);
337 >        final Object[] elts = new Object[size];
338 >        assertFalse(q.contains(makeElement(99)));
339 >        assertFalse(q.remove(makeElement(99)));
340 >        checkEmpty(q);
341 >        for (int i = 0; i < size; i++)
342 >            q.add(elts[i] = makeElement(i));
343 >        for (int i = 1; i < size; i+=2) {
344 >            for (int pass = 0; pass < 2; pass++) {
345 >                assertEquals((pass == 0), q.contains(elts[i]));
346 >                assertEquals((pass == 0), q.remove(elts[i]));
347 >                assertFalse(q.contains(elts[i]));
348 >                assertTrue(q.contains(elts[i-1]));
349 >                if (i < size - 1)
350 >                    assertTrue(q.contains(elts[i+1]));
351 >            }
352 >        }
353 >        if (size > 0)
354 >            assertTrue(q.contains(elts[0]));
355 >        for (int i = size-2; i >= 0; i-=2) {
356 >            assertTrue(q.contains(elts[i]));
357 >            assertFalse(q.contains(elts[i+1]));
358 >            assertTrue(q.remove(elts[i]));
359 >            assertFalse(q.contains(elts[i]));
360 >            assertFalse(q.remove(elts[i+1]));
361 >            assertFalse(q.contains(elts[i+1]));
362 >        }
363 >        checkEmpty(q);
364      }
365  
366      /** For debugging. */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines