ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SynchronousQueueTest.java
Revision: 1.5
Committed: Thu Sep 25 11:02:41 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.4: +52 -52 lines
Log Message:
improve tck javadocs; rename and add a few tests

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by members of JCP JSR-166 Expert Group and released to the
3     * public domain. Use, modify, and redistribute this code in any way
4     * without acknowledgement. Other contributors include Andrew Wright,
5     * Jeffrey Hayes, Pat Fischer, Mike Judd.
6     */
7    
8     import junit.framework.*;
9     import java.util.*;
10     import java.util.concurrent.*;
11 dl 1.2 import java.io.*;
12 dl 1.1
13 dl 1.3 public class SynchronousQueueTest extends JSR166TestCase {
14 dl 1.1
15     public static void main(String[] args) {
16     junit.textui.TestRunner.run (suite());
17     }
18    
19     public static Test suite() {
20     return new TestSuite(SynchronousQueueTest.class);
21     }
22    
23 dl 1.4 /**
24 dl 1.5 * A SynchronousQueue is both empty and full
25 dl 1.4 */
26 dl 1.1 public void testEmptyFull() {
27     SynchronousQueue q = new SynchronousQueue();
28     assertTrue(q.isEmpty());
29     assertEquals(0, q.size());
30     assertEquals(0, q.remainingCapacity());
31 dl 1.5 assertFalse(q.offer(zero));
32 dl 1.1 }
33    
34 dl 1.4 /**
35 dl 1.5 * offer(null) throws NPE
36 dl 1.4 */
37     public void testOfferNull() {
38 dl 1.1 try {
39     SynchronousQueue q = new SynchronousQueue();
40     q.offer(null);
41 dl 1.4 shouldThrow();
42 dl 1.1 } catch (NullPointerException success) { }
43     }
44    
45 dl 1.4 /**
46 dl 1.5 * offer fails if no active taker
47 dl 1.4 */
48     public void testOffer() {
49 dl 1.1 SynchronousQueue q = new SynchronousQueue();
50 dl 1.5 assertFalse(q.offer(one));
51 dl 1.1 }
52    
53 dl 1.4 /**
54 dl 1.5 * add throws ISE if no active taker
55 dl 1.4 */
56     public void testAdd() {
57 dl 1.1 try {
58     SynchronousQueue q = new SynchronousQueue();
59     assertEquals(0, q.remainingCapacity());
60 dl 1.5 q.add(one);
61     shouldThrow();
62 dl 1.1 } catch (IllegalStateException success){
63     }
64     }
65    
66 dl 1.4 /**
67 dl 1.5 * addAll(null) throws NPE
68 dl 1.4 */
69     public void testAddAll1() {
70 dl 1.1 try {
71     SynchronousQueue q = new SynchronousQueue();
72     q.addAll(null);
73 dl 1.4 shouldThrow();
74 dl 1.1 }
75     catch (NullPointerException success) {}
76     }
77 dl 1.4 /**
78 dl 1.5 * addAll of a collection with null elements throws NPE
79 dl 1.4 */
80     public void testAddAll2() {
81 dl 1.1 try {
82     SynchronousQueue q = new SynchronousQueue();
83 dl 1.3 Integer[] ints = new Integer[1];
84 dl 1.1 q.addAll(Arrays.asList(ints));
85 dl 1.4 shouldThrow();
86 dl 1.1 }
87     catch (NullPointerException success) {}
88     }
89 dl 1.4 /**
90 dl 1.5 * addAll throws ISE if no active taker
91 dl 1.4 */
92     public void testAddAll4() {
93 dl 1.1 try {
94     SynchronousQueue q = new SynchronousQueue();
95 dl 1.3 Integer[] ints = new Integer[1];
96     for (int i = 0; i < 1; ++i)
97 dl 1.1 ints[i] = new Integer(i);
98     q.addAll(Arrays.asList(ints));
99 dl 1.4 shouldThrow();
100 dl 1.1 }
101     catch (IllegalStateException success) {}
102     }
103    
104 dl 1.4 /**
105 dl 1.5 * put(null) throws NPE
106 dl 1.4 */
107 dl 1.1 public void testPutNull() {
108     try {
109     SynchronousQueue q = new SynchronousQueue();
110     q.put(null);
111 dl 1.4 shouldThrow();
112 dl 1.1 }
113     catch (NullPointerException success){
114     }
115     catch (InterruptedException ie) {
116 dl 1.4 unexpectedException();
117 dl 1.1 }
118     }
119    
120 dl 1.4 /**
121 dl 1.5 * put blocks interruptibly if no active taker
122 dl 1.4 */
123     public void testBlockingPut() {
124 dl 1.1 Thread t = new Thread(new Runnable() {
125     public void run() {
126     try {
127     SynchronousQueue q = new SynchronousQueue();
128 dl 1.5 q.put(zero);
129 dl 1.4 threadShouldThrow();
130 dl 1.1 } catch (InterruptedException ie){
131     }
132     }});
133     t.start();
134     try {
135     Thread.sleep(SHORT_DELAY_MS);
136     t.interrupt();
137     t.join();
138     }
139     catch (InterruptedException ie) {
140 dl 1.4 unexpectedException();
141 dl 1.1 }
142     }
143    
144 dl 1.4 /**
145 dl 1.5 * put blocks waiting for take
146 dl 1.4 */
147 dl 1.1 public void testPutWithTake() {
148     final SynchronousQueue q = new SynchronousQueue();
149     Thread t = new Thread(new Runnable() {
150 dl 1.4 public void run() {
151 dl 1.1 int added = 0;
152     try {
153     q.put(new Object());
154     ++added;
155     q.put(new Object());
156     ++added;
157     q.put(new Object());
158     ++added;
159     q.put(new Object());
160     ++added;
161 dl 1.4 threadShouldThrow();
162 dl 1.1 } catch (InterruptedException e){
163     assertTrue(added >= 1);
164     }
165     }
166     });
167     try {
168     t.start();
169     Thread.sleep(SHORT_DELAY_MS);
170     q.take();
171     Thread.sleep(SHORT_DELAY_MS);
172     t.interrupt();
173     t.join();
174     } catch (Exception e){
175 dl 1.4 unexpectedException();
176 dl 1.1 }
177     }
178    
179 dl 1.4 /**
180 dl 1.5 * timed offer times out if elements not taken
181 dl 1.4 */
182 dl 1.1 public void testTimedOffer() {
183     final SynchronousQueue q = new SynchronousQueue();
184     Thread t = new Thread(new Runnable() {
185 dl 1.4 public void run() {
186 dl 1.1 try {
187    
188 dl 1.3 threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
189 dl 1.1 q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
190 dl 1.4 threadShouldThrow();
191 dl 1.1 } catch (InterruptedException success){}
192     }
193     });
194    
195     try {
196     t.start();
197 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
198 dl 1.1 t.interrupt();
199     t.join();
200     } catch (Exception e){
201 dl 1.4 unexpectedException();
202 dl 1.1 }
203     }
204    
205    
206 dl 1.4 /**
207 dl 1.5 * take blocks interruptibly when empty
208 dl 1.4 */
209 dl 1.1 public void testTakeFromEmpty() {
210     final SynchronousQueue q = new SynchronousQueue();
211     Thread t = new Thread(new Runnable() {
212 dl 1.4 public void run() {
213 dl 1.1 try {
214     q.take();
215 dl 1.4 threadShouldThrow();
216 dl 1.1 } catch (InterruptedException success){ }
217     }
218     });
219     try {
220     t.start();
221     Thread.sleep(SHORT_DELAY_MS);
222     t.interrupt();
223     t.join();
224     } catch (Exception e){
225 dl 1.4 unexpectedException();
226 dl 1.1 }
227     }
228    
229 dl 1.4 /**
230 dl 1.5 * poll fails unless active taker
231 dl 1.4 */
232     public void testPoll() {
233 dl 1.1 SynchronousQueue q = new SynchronousQueue();
234     assertNull(q.poll());
235     }
236    
237 dl 1.4 /**
238 dl 1.5 * timed pool with zero timeout times out if no active taker
239 dl 1.4 */
240 dl 1.1 public void testTimedPoll0() {
241     try {
242     SynchronousQueue q = new SynchronousQueue();
243     assertNull(q.poll(0, TimeUnit.MILLISECONDS));
244     } catch (InterruptedException e){
245 dl 1.4 unexpectedException();
246 dl 1.1 }
247     }
248    
249 dl 1.4 /**
250 dl 1.5 * timed pool with nonzero timeout times out if no active taker
251 dl 1.4 */
252 dl 1.1 public void testTimedPoll() {
253     try {
254     SynchronousQueue q = new SynchronousQueue();
255     assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
256     } catch (InterruptedException e){
257 dl 1.4 unexpectedException();
258 dl 1.1 }
259     }
260    
261 dl 1.4 /**
262 dl 1.5 * Interrupted timed poll throws InterruptedException instead of
263     * returning timeout status
264 dl 1.4 */
265     public void testInterruptedTimedPoll() {
266 dl 1.1 Thread t = new Thread(new Runnable() {
267     public void run() {
268     try {
269     SynchronousQueue q = new SynchronousQueue();
270     assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
271     } catch (InterruptedException success){
272     }
273     }});
274     t.start();
275     try {
276     Thread.sleep(SHORT_DELAY_MS);
277     t.interrupt();
278     t.join();
279     }
280     catch (InterruptedException ie) {
281 dl 1.4 unexpectedException();
282 dl 1.1 }
283     }
284    
285 dl 1.4 /**
286 dl 1.5 * timed poll before a delayed offer fails; after offer succeeds;
287     * on interruption throws
288 dl 1.4 */
289     public void testTimedPollWithOffer() {
290 dl 1.1 final SynchronousQueue q = new SynchronousQueue();
291     Thread t = new Thread(new Runnable() {
292 dl 1.4 public void run() {
293 dl 1.1 try {
294 dl 1.3 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
295 dl 1.1 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
296     q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
297 dl 1.4 threadShouldThrow();
298 dl 1.1 } catch (InterruptedException success) { }
299     }
300     });
301     try {
302     t.start();
303 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
304 dl 1.5 assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
305 dl 1.1 t.interrupt();
306     t.join();
307     } catch (Exception e){
308 dl 1.4 unexpectedException();
309 dl 1.1 }
310     }
311    
312    
313 dl 1.4 /**
314 dl 1.5 * peek returns null
315 dl 1.4 */
316     public void testPeek() {
317 dl 1.1 SynchronousQueue q = new SynchronousQueue();
318     assertNull(q.peek());
319     }
320    
321 dl 1.4 /**
322 dl 1.5 * element throws NSEE
323 dl 1.4 */
324     public void testElement() {
325 dl 1.1 SynchronousQueue q = new SynchronousQueue();
326     try {
327     q.element();
328 dl 1.4 shouldThrow();
329 dl 1.1 }
330     catch (NoSuchElementException success) {}
331     }
332    
333 dl 1.4 /**
334 dl 1.5 * remove throws NSEE if no active taker
335 dl 1.4 */
336     public void testRemove() {
337 dl 1.1 SynchronousQueue q = new SynchronousQueue();
338     try {
339     q.remove();
340 dl 1.4 shouldThrow();
341 dl 1.1 } catch (NoSuchElementException success){
342     }
343     }
344    
345 dl 1.4 /**
346 dl 1.5 * remove(x) returns false
347 dl 1.4 */
348     public void testRemoveElement() {
349 dl 1.1 SynchronousQueue q = new SynchronousQueue();
350 dl 1.5 assertFalse(q.remove(zero));
351 dl 1.2 assertTrue(q.isEmpty());
352 dl 1.1 }
353    
354 dl 1.4 /**
355 dl 1.5 * contains returns false
356 dl 1.4 */
357     public void testContains() {
358 dl 1.1 SynchronousQueue q = new SynchronousQueue();
359 dl 1.5 assertFalse(q.contains(zero));
360 dl 1.1 }
361    
362 dl 1.4 /**
363 dl 1.5 * clear ensures isEmpty
364 dl 1.4 */
365     public void testClear() {
366 dl 1.1 SynchronousQueue q = new SynchronousQueue();
367     q.clear();
368     assertTrue(q.isEmpty());
369     }
370    
371 dl 1.4 /**
372 dl 1.5 * containsAll returns false unless empty
373 dl 1.4 */
374     public void testContainsAll() {
375 dl 1.1 SynchronousQueue q = new SynchronousQueue();
376     Integer[] empty = new Integer[0];
377 dl 1.5 assertTrue(q.containsAll(Arrays.asList(empty)));
378     Integer[] ints = new Integer[1]; ints[0] = zero;
379 dl 1.1 assertFalse(q.containsAll(Arrays.asList(ints)));
380     }
381    
382 dl 1.4 /**
383 dl 1.5 * retainAll returns false
384 dl 1.4 */
385     public void testRetainAll() {
386 dl 1.1 SynchronousQueue q = new SynchronousQueue();
387     Integer[] empty = new Integer[0];
388 dl 1.5 assertFalse(q.retainAll(Arrays.asList(empty)));
389     Integer[] ints = new Integer[1]; ints[0] = zero;
390     assertFalse(q.retainAll(Arrays.asList(ints)));
391 dl 1.1 }
392    
393 dl 1.4 /**
394 dl 1.5 * removeAll returns false
395 dl 1.4 */
396     public void testRemoveAll() {
397 dl 1.1 SynchronousQueue q = new SynchronousQueue();
398     Integer[] empty = new Integer[0];
399 dl 1.5 assertFalse(q.removeAll(Arrays.asList(empty)));
400     Integer[] ints = new Integer[1]; ints[0] = zero;
401 dl 1.1 assertFalse(q.containsAll(Arrays.asList(ints)));
402     }
403    
404    
405 dl 1.4 /**
406 dl 1.5 * toArray is empty
407 dl 1.4 */
408     public void testToArray() {
409 dl 1.1 SynchronousQueue q = new SynchronousQueue();
410     Object[] o = q.toArray();
411     assertEquals(o.length, 0);
412     }
413    
414 dl 1.4 /**
415 dl 1.5 * toArray(a) is nulled at position 0
416 dl 1.4 */
417     public void testToArray2() {
418 dl 1.1 SynchronousQueue q = new SynchronousQueue();
419     Integer[] ints = new Integer[1];
420     assertNull(ints[0]);
421     }
422    
423 dl 1.4 /**
424 dl 1.5 * iterator does not traverse any elements
425 dl 1.4 */
426     public void testIterator() {
427 dl 1.1 SynchronousQueue q = new SynchronousQueue();
428     Iterator it = q.iterator();
429     assertFalse(it.hasNext());
430     try {
431     Object x = it.next();
432 dl 1.4 shouldThrow();
433 dl 1.1 }
434     catch (NoSuchElementException success) {}
435     }
436    
437 dl 1.4 /**
438 dl 1.5 * iterator remove throws ISE
439 dl 1.4 */
440     public void testIteratorRemove() {
441 dl 1.1 SynchronousQueue q = new SynchronousQueue();
442     Iterator it = q.iterator();
443     try {
444     it.remove();
445 dl 1.4 shouldThrow();
446 dl 1.1 }
447     catch (IllegalStateException success) {}
448     }
449    
450 dl 1.4 /**
451 dl 1.5 * toString returns a non-null string
452 dl 1.4 */
453     public void testToString() {
454 dl 1.1 SynchronousQueue q = new SynchronousQueue();
455     String s = q.toString();
456 dl 1.5 assertNotNull(s);
457 dl 1.1 }
458    
459    
460 dl 1.4 /**
461 dl 1.5 * offer transfers elements across Executor tasks
462 dl 1.4 */
463 dl 1.1 public void testOfferInExecutor() {
464     final SynchronousQueue q = new SynchronousQueue();
465     ExecutorService executor = Executors.newFixedThreadPool(2);
466     final Integer one = new Integer(1);
467    
468     executor.execute(new Runnable() {
469     public void run() {
470 dl 1.3 threadAssertFalse(q.offer(one));
471 dl 1.1 try {
472 dl 1.3 threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
473     threadAssertEquals(0, q.remainingCapacity());
474 dl 1.1 }
475     catch (InterruptedException e) {
476 dl 1.4 threadUnexpectedException();
477 dl 1.1 }
478     }
479     });
480    
481     executor.execute(new Runnable() {
482     public void run() {
483     try {
484 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
485     threadAssertEquals(one, q.take());
486 dl 1.1 }
487     catch (InterruptedException e) {
488 dl 1.4 threadUnexpectedException();
489 dl 1.1 }
490     }
491     });
492    
493 dl 1.3 joinPool(executor);
494 dl 1.1
495     }
496    
497 dl 1.4 /**
498 dl 1.5 * poll retrieves elements across Executor threads
499 dl 1.4 */
500 dl 1.1 public void testPollInExecutor() {
501     final SynchronousQueue q = new SynchronousQueue();
502     ExecutorService executor = Executors.newFixedThreadPool(2);
503     executor.execute(new Runnable() {
504     public void run() {
505 dl 1.3 threadAssertNull(q.poll());
506 dl 1.1 try {
507 dl 1.3 threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
508     threadAssertTrue(q.isEmpty());
509 dl 1.1 }
510     catch (InterruptedException e) {
511 dl 1.4 threadUnexpectedException();
512 dl 1.1 }
513     }
514     });
515    
516     executor.execute(new Runnable() {
517     public void run() {
518     try {
519 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
520 dl 1.1 q.put(new Integer(1));
521     }
522     catch (InterruptedException e) {
523 dl 1.4 threadUnexpectedException();
524 dl 1.1 }
525     }
526     });
527    
528 dl 1.3 joinPool(executor);
529 dl 1.2 }
530    
531 dl 1.4 /**
532 dl 1.5 * a deserialized serialized queue is usable
533 dl 1.4 */
534 dl 1.2 public void testSerialization() {
535     SynchronousQueue q = new SynchronousQueue();
536     try {
537     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
538     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
539     out.writeObject(q);
540     out.close();
541    
542     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
543     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
544     SynchronousQueue r = (SynchronousQueue)in.readObject();
545     assertEquals(q.size(), r.size());
546     while (!q.isEmpty())
547     assertEquals(q.remove(), r.remove());
548     } catch(Exception e){
549 dl 1.4 unexpectedException();
550 dl 1.2 }
551 dl 1.1 }
552    
553     }