ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SynchronousQueueTest.java
Revision: 1.4
Committed: Sat Sep 20 18:20:08 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.3: +158 -59 lines
Log Message:
Documentation scaffolding

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     *
25     */
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     assertFalse(q.offer(new Integer(3)));
32     }
33    
34 dl 1.4 /**
35     *
36     */
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     *
47     */
48     public void testOffer() {
49 dl 1.1 SynchronousQueue q = new SynchronousQueue();
50     assertFalse(q.offer(new Integer(1)));
51     }
52    
53 dl 1.4 /**
54     *
55     */
56     public void testAdd() {
57 dl 1.1 try {
58     SynchronousQueue q = new SynchronousQueue();
59     assertEquals(0, q.remainingCapacity());
60     q.add(new Integer(0));
61     } catch (IllegalStateException success){
62     }
63     }
64    
65 dl 1.4 /**
66     *
67     */
68     public void testAddAll1() {
69 dl 1.1 try {
70     SynchronousQueue q = new SynchronousQueue();
71     q.addAll(null);
72 dl 1.4 shouldThrow();
73 dl 1.1 }
74     catch (NullPointerException success) {}
75     }
76 dl 1.4 /**
77     *
78     */
79     public void testAddAll2() {
80 dl 1.1 try {
81     SynchronousQueue q = new SynchronousQueue();
82 dl 1.3 Integer[] ints = new Integer[1];
83 dl 1.1 q.addAll(Arrays.asList(ints));
84 dl 1.4 shouldThrow();
85 dl 1.1 }
86     catch (NullPointerException success) {}
87     }
88 dl 1.4 /**
89     *
90     */
91     public void testAddAll4() {
92 dl 1.1 try {
93     SynchronousQueue q = new SynchronousQueue();
94 dl 1.3 Integer[] ints = new Integer[1];
95     for (int i = 0; i < 1; ++i)
96 dl 1.1 ints[i] = new Integer(i);
97     q.addAll(Arrays.asList(ints));
98 dl 1.4 shouldThrow();
99 dl 1.1 }
100     catch (IllegalStateException success) {}
101     }
102    
103 dl 1.4 /**
104     *
105     */
106 dl 1.1 public void testPutNull() {
107     try {
108     SynchronousQueue q = new SynchronousQueue();
109     q.put(null);
110 dl 1.4 shouldThrow();
111 dl 1.1 }
112     catch (NullPointerException success){
113     }
114     catch (InterruptedException ie) {
115 dl 1.4 unexpectedException();
116 dl 1.1 }
117     }
118    
119 dl 1.4 /**
120     *
121     */
122     public void testBlockingPut() {
123 dl 1.1 Thread t = new Thread(new Runnable() {
124     public void run() {
125     try {
126     SynchronousQueue q = new SynchronousQueue();
127     q.put(new Integer(0));
128 dl 1.4 threadShouldThrow();
129 dl 1.1 } catch (InterruptedException ie){
130     }
131     }});
132     t.start();
133     try {
134     Thread.sleep(SHORT_DELAY_MS);
135     t.interrupt();
136     t.join();
137     }
138     catch (InterruptedException ie) {
139 dl 1.4 unexpectedException();
140 dl 1.1 }
141     }
142    
143 dl 1.4 /**
144     *
145     */
146 dl 1.1 public void testPutWithTake() {
147     final SynchronousQueue q = new SynchronousQueue();
148     Thread t = new Thread(new Runnable() {
149 dl 1.4 public void run() {
150 dl 1.1 int added = 0;
151     try {
152     q.put(new Object());
153     ++added;
154     q.put(new Object());
155     ++added;
156     q.put(new Object());
157     ++added;
158     q.put(new Object());
159     ++added;
160 dl 1.4 threadShouldThrow();
161 dl 1.1 } catch (InterruptedException e){
162     assertTrue(added >= 1);
163     }
164     }
165     });
166     try {
167     t.start();
168     Thread.sleep(SHORT_DELAY_MS);
169     q.take();
170     Thread.sleep(SHORT_DELAY_MS);
171     t.interrupt();
172     t.join();
173     } catch (Exception e){
174 dl 1.4 unexpectedException();
175 dl 1.1 }
176     }
177    
178 dl 1.4 /**
179     *
180     */
181 dl 1.1 public void testTimedOffer() {
182     final SynchronousQueue q = new SynchronousQueue();
183     Thread t = new Thread(new Runnable() {
184 dl 1.4 public void run() {
185 dl 1.1 try {
186    
187 dl 1.3 threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
188 dl 1.1 q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
189 dl 1.4 threadShouldThrow();
190 dl 1.1 } catch (InterruptedException success){}
191     }
192     });
193    
194     try {
195     t.start();
196 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
197 dl 1.1 t.interrupt();
198     t.join();
199     } catch (Exception e){
200 dl 1.4 unexpectedException();
201 dl 1.1 }
202     }
203    
204    
205 dl 1.4 /**
206     *
207     */
208 dl 1.1 public void testTakeFromEmpty() {
209     final SynchronousQueue q = new SynchronousQueue();
210     Thread t = new Thread(new Runnable() {
211 dl 1.4 public void run() {
212 dl 1.1 try {
213     q.take();
214 dl 1.4 threadShouldThrow();
215 dl 1.1 } catch (InterruptedException success){ }
216     }
217     });
218     try {
219     t.start();
220     Thread.sleep(SHORT_DELAY_MS);
221     t.interrupt();
222     t.join();
223     } catch (Exception e){
224 dl 1.4 unexpectedException();
225 dl 1.1 }
226     }
227    
228 dl 1.4 /**
229     *
230     */
231     public void testPoll() {
232 dl 1.1 SynchronousQueue q = new SynchronousQueue();
233     assertNull(q.poll());
234     }
235    
236 dl 1.4 /**
237     *
238     */
239 dl 1.1 public void testTimedPoll0() {
240     try {
241     SynchronousQueue q = new SynchronousQueue();
242     assertNull(q.poll(0, TimeUnit.MILLISECONDS));
243     } catch (InterruptedException e){
244 dl 1.4 unexpectedException();
245 dl 1.1 }
246     }
247    
248 dl 1.4 /**
249     *
250     */
251 dl 1.1 public void testTimedPoll() {
252     try {
253     SynchronousQueue q = new SynchronousQueue();
254     assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
255     } catch (InterruptedException e){
256 dl 1.4 unexpectedException();
257 dl 1.1 }
258     }
259    
260 dl 1.4 /**
261     *
262     */
263     public void testInterruptedTimedPoll() {
264 dl 1.1 Thread t = new Thread(new Runnable() {
265     public void run() {
266     try {
267     SynchronousQueue q = new SynchronousQueue();
268     assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
269     } catch (InterruptedException success){
270     }
271     }});
272     t.start();
273     try {
274     Thread.sleep(SHORT_DELAY_MS);
275     t.interrupt();
276     t.join();
277     }
278     catch (InterruptedException ie) {
279 dl 1.4 unexpectedException();
280 dl 1.1 }
281     }
282    
283 dl 1.4 /**
284     *
285     */
286     public void testTimedPollWithOffer() {
287 dl 1.1 final SynchronousQueue q = new SynchronousQueue();
288     Thread t = new Thread(new Runnable() {
289 dl 1.4 public void run() {
290 dl 1.1 try {
291 dl 1.3 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
292 dl 1.1 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
293     q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
294 dl 1.4 threadShouldThrow();
295 dl 1.1 } catch (InterruptedException success) { }
296     }
297     });
298     try {
299     t.start();
300 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
301 dl 1.1 assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
302     t.interrupt();
303     t.join();
304     } catch (Exception e){
305 dl 1.4 unexpectedException();
306 dl 1.1 }
307     }
308    
309    
310 dl 1.4 /**
311     *
312     */
313     public void testPeek() {
314 dl 1.1 SynchronousQueue q = new SynchronousQueue();
315     assertNull(q.peek());
316     }
317    
318 dl 1.4 /**
319     *
320     */
321     public void testElement() {
322 dl 1.1 SynchronousQueue q = new SynchronousQueue();
323     try {
324     q.element();
325 dl 1.4 shouldThrow();
326 dl 1.1 }
327     catch (NoSuchElementException success) {}
328     }
329    
330 dl 1.4 /**
331     *
332     */
333     public void testRemove() {
334 dl 1.1 SynchronousQueue q = new SynchronousQueue();
335     try {
336     q.remove();
337 dl 1.4 shouldThrow();
338 dl 1.1 } catch (NoSuchElementException success){
339     }
340     }
341    
342 dl 1.4 /**
343     *
344     */
345     public void testRemoveElement() {
346 dl 1.1 SynchronousQueue q = new SynchronousQueue();
347 dl 1.3 assertFalse(q.remove(new Integer(0)));
348 dl 1.2 assertTrue(q.isEmpty());
349 dl 1.1 }
350    
351 dl 1.4 /**
352     *
353     */
354     public void testContains() {
355 dl 1.1 SynchronousQueue q = new SynchronousQueue();
356 dl 1.3 assertFalse(q.contains(new Integer(0)));
357 dl 1.1 }
358    
359 dl 1.4 /**
360     *
361     */
362     public void testClear() {
363 dl 1.1 SynchronousQueue q = new SynchronousQueue();
364     q.clear();
365     assertTrue(q.isEmpty());
366     }
367    
368 dl 1.4 /**
369     *
370     */
371     public void testContainsAll() {
372 dl 1.1 SynchronousQueue q = new SynchronousQueue();
373     Integer[] empty = new Integer[0];
374     Integer[] ints = new Integer[1]; ints[0] = new Integer(0);
375     assertFalse(q.containsAll(Arrays.asList(ints)));
376     }
377    
378 dl 1.4 /**
379     *
380     */
381     public void testRetainAll() {
382 dl 1.1 SynchronousQueue q = new SynchronousQueue();
383     Integer[] empty = new Integer[0];
384     Integer[] ints = new Integer[1]; ints[0] = new Integer(0);
385     q.retainAll(Arrays.asList(ints));
386     assertFalse(q.containsAll(Arrays.asList(ints)));
387     }
388    
389 dl 1.4 /**
390     *
391     */
392     public void testRemoveAll() {
393 dl 1.1 SynchronousQueue q = new SynchronousQueue();
394     Integer[] empty = new Integer[0];
395     Integer[] ints = new Integer[1]; ints[0] = new Integer(0);
396     q.removeAll(Arrays.asList(ints));
397     assertFalse(q.containsAll(Arrays.asList(ints)));
398     }
399    
400    
401 dl 1.4 /**
402     *
403     */
404     public void testToArray() {
405 dl 1.1 SynchronousQueue q = new SynchronousQueue();
406     Object[] o = q.toArray();
407     assertEquals(o.length, 0);
408     }
409    
410 dl 1.4 /**
411     *
412     */
413     public void testToArray2() {
414 dl 1.1 SynchronousQueue q = new SynchronousQueue();
415     Integer[] ints = new Integer[1];
416     assertNull(ints[0]);
417     }
418    
419 dl 1.4 /**
420     *
421     */
422     public void testIterator() {
423 dl 1.1 SynchronousQueue q = new SynchronousQueue();
424     Iterator it = q.iterator();
425     assertFalse(it.hasNext());
426     try {
427     Object x = it.next();
428 dl 1.4 shouldThrow();
429 dl 1.1 }
430     catch (NoSuchElementException success) {}
431     }
432    
433 dl 1.4 /**
434     *
435     */
436     public void testIteratorRemove() {
437 dl 1.1 SynchronousQueue q = new SynchronousQueue();
438     Iterator it = q.iterator();
439     try {
440     it.remove();
441 dl 1.4 shouldThrow();
442 dl 1.1 }
443     catch (IllegalStateException success) {}
444     }
445    
446 dl 1.4 /**
447     *
448     */
449     public void testToString() {
450 dl 1.1 SynchronousQueue q = new SynchronousQueue();
451     String s = q.toString();
452     assertTrue(s != null);
453     }
454    
455    
456 dl 1.4 /**
457     *
458     */
459 dl 1.1 public void testOfferInExecutor() {
460     final SynchronousQueue q = new SynchronousQueue();
461     ExecutorService executor = Executors.newFixedThreadPool(2);
462     final Integer one = new Integer(1);
463    
464     executor.execute(new Runnable() {
465     public void run() {
466 dl 1.3 threadAssertFalse(q.offer(one));
467 dl 1.1 try {
468 dl 1.3 threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
469     threadAssertEquals(0, q.remainingCapacity());
470 dl 1.1 }
471     catch (InterruptedException e) {
472 dl 1.4 threadUnexpectedException();
473 dl 1.1 }
474     }
475     });
476    
477     executor.execute(new Runnable() {
478     public void run() {
479     try {
480 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
481     threadAssertEquals(one, q.take());
482 dl 1.1 }
483     catch (InterruptedException e) {
484 dl 1.4 threadUnexpectedException();
485 dl 1.1 }
486     }
487     });
488    
489 dl 1.3 joinPool(executor);
490 dl 1.1
491     }
492    
493 dl 1.4 /**
494     *
495     */
496 dl 1.1 public void testPollInExecutor() {
497    
498     final SynchronousQueue q = new SynchronousQueue();
499    
500     ExecutorService executor = Executors.newFixedThreadPool(2);
501    
502     executor.execute(new Runnable() {
503     public void run() {
504 dl 1.3 threadAssertNull(q.poll());
505 dl 1.1 try {
506 dl 1.3 threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
507     threadAssertTrue(q.isEmpty());
508 dl 1.1 }
509     catch (InterruptedException e) {
510 dl 1.4 threadUnexpectedException();
511 dl 1.1 }
512     }
513     });
514    
515     executor.execute(new Runnable() {
516     public void run() {
517     try {
518 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
519 dl 1.1 q.put(new Integer(1));
520     }
521     catch (InterruptedException e) {
522 dl 1.4 threadUnexpectedException();
523 dl 1.1 }
524     }
525     });
526    
527 dl 1.3 joinPool(executor);
528 dl 1.1
529 dl 1.2 }
530    
531 dl 1.4 /**
532     *
533     */
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     }