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.6 by dl, Fri May 6 11:22:07 2011 UTC vs.
Revision 1.19 by jsr166, Sat May 13 22:38:09 2017 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines