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.5 by jsr166, Tue Mar 15 19:47:06 2011 UTC vs.
Revision 1.10 by jsr166, Tue Jun 14 03:22:38 2011 UTC

# Line 8 | Line 8
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
185 >     */
186 >    public void testDrainToNegativeMaxElements() {
187 >        final BlockingQueue q = emptyCollection();
188 >        assertEquals(0, q.drainTo(new ArrayList(), -42));
189 >    }
190 >
191 >    /**
192 >     * drainTo(c, 0) returns 0 and does nothing
193 >     */
194 >    public void testDrainToZeroMaxElements() {
195 >        final BlockingQueue q = emptyCollection();
196 >        if (q.remainingCapacity() == 0) {
197 >            // SynchronousQueue, for example
198 >            assertEquals(0, q.drainTo(new ArrayList(), 0));
199 >        } else {
200 >            Object one = makeElement(1);
201 >            q.add(one);
202 >            ArrayList c = new ArrayList();
203 >            assertEquals(0, q.drainTo(c, 0));
204 >            assertEquals(1, q.size());
205 >            assertSame(one, q.poll());
206 >            assertTrue(c.isEmpty());
207 >        }
208 >    }
209 >
210 >    /**
211 >     * timed poll before a delayed offer times out; after offer succeeds;
212       * on interruption throws
213       */
214      public void testTimedPollWithOffer() throws InterruptedException {
215          final BlockingQueue q = emptyCollection();
216          final CheckedBarrier barrier = new CheckedBarrier(2);
217 +        final Object zero = makeElement(0);
218          Thread t = newStartedThread(new CheckedRunnable() {
219              public void realRun() throws InterruptedException {
220 <                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
220 >                long startTime = System.nanoTime();
221 >                assertNull(q.poll(timeoutMillis(), MILLISECONDS));
222 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
223  
224                  barrier.await();
225 <                assertSame(zero, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
225 >
226 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
227  
228                  Thread.currentThread().interrupt();
229                  try {
230 <                    q.poll(SHORT_DELAY_MS, MILLISECONDS);
230 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
231                      shouldThrow();
232                  } catch (InterruptedException success) {}
233 +                assertFalse(Thread.interrupted());
234  
235                  barrier.await();
236                  try {
237 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
237 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
238                      shouldThrow();
239                  } catch (InterruptedException success) {}
240 +                assertFalse(Thread.interrupted());
241              }});
242  
243          barrier.await();
244 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
244 >        long startTime = System.nanoTime();
245 >        assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS));
246 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
247 >
248          barrier.await();
249 <        sleep(SHORT_DELAY_MS);
249 >        assertThreadStaysAlive(t);
250          t.interrupt();
251 <        awaitTermination(t, MEDIUM_DELAY_MS);
251 >        awaitTermination(t);
252      }
253  
254      /**
255       * take() blocks interruptibly when empty
256       */
257 <    public void testTakeFromEmptyBlocksInterruptibly()
78 <            throws InterruptedException {
257 >    public void testTakeFromEmptyBlocksInterruptibly() {
258          final BlockingQueue q = emptyCollection();
259          final CountDownLatch threadStarted = new CountDownLatch(1);
260          Thread t = newStartedThread(new CheckedRunnable() {
261              public void realRun() {
83                long t0 = System.nanoTime();
262                  threadStarted.countDown();
263                  try {
264                      q.take();
265                      shouldThrow();
266                  } catch (InterruptedException success) {}
267 <                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
267 >                assertFalse(Thread.interrupted());
268              }});
269  
270 <        threadStarted.await();
271 <        Thread.sleep(SHORT_DELAY_MS);
94 <        assertTrue(t.isAlive());
270 >        await(threadStarted);
271 >        assertThreadStaysAlive(t);
272          t.interrupt();
273 <        awaitTermination(t, MEDIUM_DELAY_MS);
273 >        awaitTermination(t);
274      }
275  
276      /**
277       * take() throws InterruptedException immediately if interrupted
278       * before waiting
279       */
280 <    public void testTakeFromEmptyAfterInterrupt()
104 <            throws InterruptedException {
280 >    public void testTakeFromEmptyAfterInterrupt() {
281          final BlockingQueue q = emptyCollection();
282          Thread t = newStartedThread(new CheckedRunnable() {
283              public void realRun() {
108                long t0 = System.nanoTime();
284                  Thread.currentThread().interrupt();
285                  try {
286                      q.take();
287                      shouldThrow();
288                  } catch (InterruptedException success) {}
289 <                assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
289 >                assertFalse(Thread.interrupted());
290 >            }});
291 >
292 >        awaitTermination(t);
293 >    }
294 >
295 >    /**
296 >     * timed poll() blocks interruptibly when empty
297 >     */
298 >    public void testTimedPollFromEmptyBlocksInterruptibly() {
299 >        final BlockingQueue q = emptyCollection();
300 >        final CountDownLatch threadStarted = new CountDownLatch(1);
301 >        Thread t = newStartedThread(new CheckedRunnable() {
302 >            public void realRun() {
303 >                threadStarted.countDown();
304 >                try {
305 >                    q.poll(2 * LONG_DELAY_MS, MILLISECONDS);
306 >                    shouldThrow();
307 >                } catch (InterruptedException success) {}
308 >                assertFalse(Thread.interrupted());
309 >            }});
310 >
311 >        await(threadStarted);
312 >        assertThreadStaysAlive(t);
313 >        t.interrupt();
314 >        awaitTermination(t);
315 >    }
316 >
317 >    /**
318 >     * timed poll() throws InterruptedException immediately if
319 >     * interrupted before waiting
320 >     */
321 >    public void testTimedPollFromEmptyAfterInterrupt() {
322 >        final BlockingQueue q = emptyCollection();
323 >        Thread t = newStartedThread(new CheckedRunnable() {
324 >            public void realRun() {
325 >                Thread.currentThread().interrupt();
326 >                try {
327 >                    q.poll(2 * LONG_DELAY_MS, MILLISECONDS);
328 >                    shouldThrow();
329 >                } catch (InterruptedException success) {}
330 >                assertFalse(Thread.interrupted());
331              }});
332  
333 <        awaitTermination(t, MEDIUM_DELAY_MS);
333 >        awaitTermination(t);
334      }
335  
336      /** For debugging. */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines