ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/BlockingQueueTest.java
Revision: 1.25
Committed: Tue Jan 26 13:33:05 2021 UTC (3 years, 3 months ago) by dl
Branch: MAIN
CVS Tags: HEAD
Changes since 1.24: +12 -11 lines
Log Message:
Replace Integer with Item class

File Contents

# User Rev Content
1 jsr166 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 jsr166 1.5 * explained at http://creativecommons.org/publicdomain/zero/1.0/
5 jsr166 1.1 *
6     * Other contributors include Andrew Wright, Jeffrey Hayes,
7     * Pat Fisher, Mike Judd.
8     */
9    
10 jsr166 1.13 import static java.util.concurrent.TimeUnit.MILLISECONDS;
11    
12 jsr166 1.10 import java.util.ArrayList;
13 jsr166 1.8 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 jsr166 1.13
19     import junit.framework.Test;
20     import junit.framework.TestSuite;
21 jsr166 1.1
22     /**
23 jsr166 1.8 * Contains "contract" tests applicable to all BlockingQueue implementations.
24 jsr166 1.1 */
25     public abstract class BlockingQueueTest extends JSR166TestCase {
26     /*
27     * This is the start of an attempt to refactor the tests for the
28     * various related implementations of related interfaces without
29     * too much duplicated code. junit does not really support such
30     * testing. Here subclasses of TestCase not only contain tests,
31     * but also configuration information that describes the
32     * implementation class, most importantly how to instantiate
33     * instances.
34     */
35    
36     /** Like suite(), but non-static */
37     public Test testSuite() {
38     // TODO: filter the returned tests using the configuration
39     // information provided by the subclass via protected methods.
40     return new TestSuite(this.getClass());
41     }
42    
43 jsr166 1.8 //----------------------------------------------------------------
44     // Configuration methods
45     //----------------------------------------------------------------
46 jsr166 1.9
47 jsr166 1.1 /** Returns an empty instance of the implementation class. */
48     protected abstract BlockingQueue emptyCollection();
49    
50     /**
51 jsr166 1.8 * 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 dl 1.25 return JSR166TestCase.itemFor(i);
56 jsr166 1.8 }
57    
58     //----------------------------------------------------------------
59     // Tests
60     //----------------------------------------------------------------
61 jsr166 1.9
62 jsr166 1.8 /**
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 jsr166 1.20 * addAll(null) throws NullPointerException
110 jsr166 1.8 */
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 dl 1.25 final Collection elements = Arrays.asList(new Item[SIZE]);
125 jsr166 1.8 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 jsr166 1.22 q.toArray((Object[])null);
138 jsr166 1.8 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 jsr166 1.11 * drainTo(c, n) returns 0 and does nothing when n <= 0
188 jsr166 1.10 */
189 jsr166 1.11 public void testDrainToNonPositiveMaxElements() {
190 jsr166 1.10 final BlockingQueue q = emptyCollection();
191 jsr166 1.11 final int[] ns = { 0, -1, -42, Integer.MIN_VALUE };
192 jsr166 1.21 final ArrayList sink = new ArrayList();
193     for (int n : ns) {
194     assertEquals(0, q.drainTo(sink, n));
195     assertTrue(sink.isEmpty());
196     }
197 jsr166 1.11 if (q.remainingCapacity() > 0) {
198     // Not SynchronousQueue, that is
199 dl 1.25 Object e = makeElement(1);
200     if (q.add(e)) {
201     for (int n : ns)
202     assertEquals(0, q.drainTo(sink, n));
203     assertEquals(1, q.size());
204     assertEquals(e, q.poll());
205     assertTrue(sink.isEmpty());
206     }
207 jsr166 1.10 }
208     }
209    
210     /**
211 jsr166 1.7 * timed poll before a delayed offer times out; after offer succeeds;
212 jsr166 1.1 * on interruption throws
213     */
214     public void testTimedPollWithOffer() throws InterruptedException {
215     final BlockingQueue q = emptyCollection();
216     final CheckedBarrier barrier = new CheckedBarrier(2);
217 jsr166 1.8 final Object zero = makeElement(0);
218 jsr166 1.1 Thread t = newStartedThread(new CheckedRunnable() {
219     public void realRun() throws InterruptedException {
220 jsr166 1.7 long startTime = System.nanoTime();
221     assertNull(q.poll(timeoutMillis(), MILLISECONDS));
222     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
223 jsr166 1.1
224     barrier.await();
225 jsr166 1.7
226     assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
227 jsr166 1.1
228     Thread.currentThread().interrupt();
229     try {
230 jsr166 1.23 q.poll(randomTimeout(), randomTimeUnit());
231 jsr166 1.1 shouldThrow();
232     } catch (InterruptedException success) {}
233 jsr166 1.7 assertFalse(Thread.interrupted());
234 jsr166 1.1
235     barrier.await();
236     try {
237 jsr166 1.7 q.poll(LONG_DELAY_MS, MILLISECONDS);
238 jsr166 1.1 shouldThrow();
239     } catch (InterruptedException success) {}
240 jsr166 1.7 assertFalse(Thread.interrupted());
241 jsr166 1.17
242     assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
243 jsr166 1.1 }});
244    
245     barrier.await();
246 jsr166 1.7 long startTime = System.nanoTime();
247     assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS));
248     assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
249    
250 jsr166 1.1 barrier.await();
251 jsr166 1.23 if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
252 jsr166 1.1 t.interrupt();
253 jsr166 1.7 awaitTermination(t);
254 jsr166 1.1 }
255    
256 jsr166 1.2 /**
257 jsr166 1.3 * take() blocks interruptibly when empty
258 jsr166 1.2 */
259 jsr166 1.7 public void testTakeFromEmptyBlocksInterruptibly() {
260 jsr166 1.2 final BlockingQueue q = emptyCollection();
261     final CountDownLatch threadStarted = new CountDownLatch(1);
262     Thread t = newStartedThread(new CheckedRunnable() {
263     public void realRun() {
264     threadStarted.countDown();
265     try {
266     q.take();
267     shouldThrow();
268     } catch (InterruptedException success) {}
269 jsr166 1.7 assertFalse(Thread.interrupted());
270 jsr166 1.2 }});
271 jsr166 1.3
272 jsr166 1.7 await(threadStarted);
273 jsr166 1.23 if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
274 jsr166 1.2 t.interrupt();
275 jsr166 1.7 awaitTermination(t);
276 jsr166 1.3 }
277    
278     /**
279     * take() throws InterruptedException immediately if interrupted
280     * before waiting
281     */
282 jsr166 1.7 public void testTakeFromEmptyAfterInterrupt() {
283 jsr166 1.3 final BlockingQueue q = emptyCollection();
284     Thread t = newStartedThread(new CheckedRunnable() {
285     public void realRun() {
286     Thread.currentThread().interrupt();
287     try {
288     q.take();
289     shouldThrow();
290     } catch (InterruptedException success) {}
291 jsr166 1.7 assertFalse(Thread.interrupted());
292     }});
293    
294     awaitTermination(t);
295     }
296    
297     /**
298     * timed poll() blocks interruptibly when empty
299     */
300     public void testTimedPollFromEmptyBlocksInterruptibly() {
301     final BlockingQueue q = emptyCollection();
302 jsr166 1.24 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
303 jsr166 1.7 Thread t = newStartedThread(new CheckedRunnable() {
304     public void realRun() {
305 jsr166 1.24 pleaseInterrupt.countDown();
306 jsr166 1.7 try {
307 jsr166 1.24 q.poll(LONGER_DELAY_MS, MILLISECONDS);
308 jsr166 1.7 shouldThrow();
309     } catch (InterruptedException success) {}
310     assertFalse(Thread.interrupted());
311     }});
312    
313 jsr166 1.24 await(pleaseInterrupt);
314     if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
315 jsr166 1.7 t.interrupt();
316     awaitTermination(t);
317     }
318    
319     /**
320     * timed poll() throws InterruptedException immediately if
321     * interrupted before waiting
322     */
323     public void testTimedPollFromEmptyAfterInterrupt() {
324     final BlockingQueue q = emptyCollection();
325     Thread t = newStartedThread(new CheckedRunnable() {
326     public void realRun() {
327     Thread.currentThread().interrupt();
328     try {
329 jsr166 1.24 q.poll(LONGER_DELAY_MS, MILLISECONDS);
330 jsr166 1.7 shouldThrow();
331     } catch (InterruptedException success) {}
332     assertFalse(Thread.interrupted());
333 jsr166 1.3 }});
334    
335 jsr166 1.7 awaitTermination(t);
336 jsr166 1.2 }
337    
338 jsr166 1.12 /**
339     * remove(x) removes x and returns true if present
340     * TODO: move to superclass CollectionTest.java
341     */
342     public void testRemoveElement() {
343     final BlockingQueue q = emptyCollection();
344     final int size = Math.min(q.remainingCapacity(), SIZE);
345     final Object[] elts = new Object[size];
346     assertFalse(q.contains(makeElement(99)));
347     assertFalse(q.remove(makeElement(99)));
348     checkEmpty(q);
349     for (int i = 0; i < size; i++)
350     q.add(elts[i] = makeElement(i));
351 jsr166 1.14 for (int i = 1; i < size; i += 2) {
352 jsr166 1.12 for (int pass = 0; pass < 2; pass++) {
353 dl 1.25 mustEqual((pass == 0), q.contains(elts[i]));
354     mustEqual((pass == 0), q.remove(elts[i]));
355 jsr166 1.12 assertFalse(q.contains(elts[i]));
356 jsr166 1.16 assertTrue(q.contains(elts[i - 1]));
357 jsr166 1.12 if (i < size - 1)
358 jsr166 1.16 assertTrue(q.contains(elts[i + 1]));
359 jsr166 1.12 }
360     }
361     if (size > 0)
362     assertTrue(q.contains(elts[0]));
363 jsr166 1.15 for (int i = size - 2; i >= 0; i -= 2) {
364 jsr166 1.12 assertTrue(q.contains(elts[i]));
365 jsr166 1.16 assertFalse(q.contains(elts[i + 1]));
366 jsr166 1.12 assertTrue(q.remove(elts[i]));
367     assertFalse(q.contains(elts[i]));
368 jsr166 1.16 assertFalse(q.remove(elts[i + 1]));
369     assertFalse(q.contains(elts[i + 1]));
370 jsr166 1.12 }
371     checkEmpty(q);
372     }
373    
374 jsr166 1.1 /** For debugging. */
375     public void XXXXtestFails() {
376     fail(emptyCollection().getClass().toString());
377     }
378    
379     }