ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SynchronousQueueTest.java
Revision: 1.2
Committed: Sun Sep 7 20:39:11 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.1: +21 -1 lines
Log Message:
Added serialization and lock tests

File Contents

# Content
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 import java.io.*;
12
13 public class SynchronousQueueTest extends TestCase {
14
15 private final static int N = 1;
16 private static long SHORT_DELAY_MS = 100;
17 private static long MEDIUM_DELAY_MS = 1000;
18 private static long LONG_DELAY_MS = 10000;
19
20 public static void main(String[] args) {
21 junit.textui.TestRunner.run (suite());
22 }
23
24 public static Test suite() {
25 return new TestSuite(SynchronousQueueTest.class);
26 }
27
28 public void testEmptyFull() {
29 SynchronousQueue q = new SynchronousQueue();
30 assertTrue(q.isEmpty());
31 assertEquals(0, q.size());
32 assertEquals(0, q.remainingCapacity());
33 assertFalse(q.offer(new Integer(3)));
34 }
35
36 public void testOfferNull(){
37 try {
38 SynchronousQueue q = new SynchronousQueue();
39 q.offer(null);
40 fail("should throw NPE");
41 } catch (NullPointerException success) { }
42 }
43
44 public void testOffer(){
45 SynchronousQueue q = new SynchronousQueue();
46 assertFalse(q.offer(new Integer(1)));
47 }
48
49 public void testAdd(){
50 try {
51 SynchronousQueue q = new SynchronousQueue();
52 assertEquals(0, q.remainingCapacity());
53 q.add(new Integer(0));
54 } catch (IllegalStateException success){
55 }
56 }
57
58 public void testAddAll1(){
59 try {
60 SynchronousQueue q = new SynchronousQueue();
61 q.addAll(null);
62 fail("Cannot add null collection");
63 }
64 catch (NullPointerException success) {}
65 }
66 public void testAddAll2(){
67 try {
68 SynchronousQueue q = new SynchronousQueue();
69 Integer[] ints = new Integer[N];
70 q.addAll(Arrays.asList(ints));
71 fail("Cannot add null elements");
72 }
73 catch (NullPointerException success) {}
74 }
75 public void testAddAll4(){
76 try {
77 SynchronousQueue q = new SynchronousQueue();
78 Integer[] ints = new Integer[N];
79 for (int i = 0; i < N; ++i)
80 ints[i] = new Integer(i);
81 q.addAll(Arrays.asList(ints));
82 fail("Cannot add with insufficient capacity");
83 }
84 catch (IllegalStateException success) {}
85 }
86
87 public void testPutNull() {
88 try {
89 SynchronousQueue q = new SynchronousQueue();
90 q.put(null);
91 fail("put should throw NPE");
92 }
93 catch (NullPointerException success){
94 }
95 catch (InterruptedException ie) {
96 fail("Unexpected exception");
97 }
98 }
99
100 public void testBlockingPut(){
101 Thread t = new Thread(new Runnable() {
102 public void run() {
103 try {
104 SynchronousQueue q = new SynchronousQueue();
105 q.put(new Integer(0));
106 fail("put should block");
107 } catch (InterruptedException ie){
108 }
109 }});
110 t.start();
111 try {
112 Thread.sleep(SHORT_DELAY_MS);
113 t.interrupt();
114 t.join();
115 }
116 catch (InterruptedException ie) {
117 fail("Unexpected exception");
118 }
119 }
120
121 public void testPutWithTake() {
122 final SynchronousQueue q = new SynchronousQueue();
123 Thread t = new Thread(new Runnable() {
124 public void run(){
125 int added = 0;
126 try {
127 q.put(new Object());
128 ++added;
129 q.put(new Object());
130 ++added;
131 q.put(new Object());
132 ++added;
133 q.put(new Object());
134 ++added;
135 fail("Should block");
136 } catch (InterruptedException e){
137 assertTrue(added >= 1);
138 }
139 }
140 });
141 try {
142 t.start();
143 Thread.sleep(SHORT_DELAY_MS);
144 q.take();
145 Thread.sleep(SHORT_DELAY_MS);
146 t.interrupt();
147 t.join();
148 } catch (Exception e){
149 fail("Unexpected exception");
150 }
151 }
152
153 public void testTimedOffer() {
154 final SynchronousQueue q = new SynchronousQueue();
155 Thread t = new Thread(new Runnable() {
156 public void run(){
157 try {
158
159 assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
160 q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
161 fail("Should block");
162 } catch (InterruptedException success){}
163 }
164 });
165
166 try {
167 t.start();
168 Thread.sleep(SHORT_DELAY_MS);
169 t.interrupt();
170 t.join();
171 } catch (Exception e){
172 fail("Unexpected exception");
173 }
174 }
175
176
177 public void testTakeFromEmpty() {
178 final SynchronousQueue q = new SynchronousQueue();
179 Thread t = new Thread(new Runnable() {
180 public void run(){
181 try {
182 q.take();
183 fail("Should block");
184 } catch (InterruptedException success){ }
185 }
186 });
187 try {
188 t.start();
189 Thread.sleep(SHORT_DELAY_MS);
190 t.interrupt();
191 t.join();
192 } catch (Exception e){
193 fail("Unexpected exception");
194 }
195 }
196
197 public void testPoll(){
198 SynchronousQueue q = new SynchronousQueue();
199 assertNull(q.poll());
200 }
201
202 public void testTimedPoll0() {
203 try {
204 SynchronousQueue q = new SynchronousQueue();
205 assertNull(q.poll(0, TimeUnit.MILLISECONDS));
206 } catch (InterruptedException e){
207 fail("Unexpected exception");
208 }
209 }
210
211 public void testTimedPoll() {
212 try {
213 SynchronousQueue q = new SynchronousQueue();
214 assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
215 } catch (InterruptedException e){
216 fail("Unexpected exception");
217 }
218 }
219
220 public void testInterruptedTimedPoll(){
221 Thread t = new Thread(new Runnable() {
222 public void run() {
223 try {
224 SynchronousQueue q = new SynchronousQueue();
225 assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
226 } catch (InterruptedException success){
227 }
228 }});
229 t.start();
230 try {
231 Thread.sleep(SHORT_DELAY_MS);
232 t.interrupt();
233 t.join();
234 }
235 catch (InterruptedException ie) {
236 fail("Unexpected exception");
237 }
238 }
239
240 public void testTimedPollWithOffer(){
241 final SynchronousQueue q = new SynchronousQueue();
242 Thread t = new Thread(new Runnable() {
243 public void run(){
244 try {
245 assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
246 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
247 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
248 fail("Should block");
249 } catch (InterruptedException success) { }
250 }
251 });
252 try {
253 t.start();
254 Thread.sleep(SHORT_DELAY_MS * 2);
255 assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
256 t.interrupt();
257 t.join();
258 } catch (Exception e){
259 fail("Unexpected exception");
260 }
261 }
262
263
264 public void testPeek(){
265 SynchronousQueue q = new SynchronousQueue();
266 assertNull(q.peek());
267 }
268
269 public void testElement(){
270 SynchronousQueue q = new SynchronousQueue();
271 try {
272 q.element();
273 fail("no such element");
274 }
275 catch (NoSuchElementException success) {}
276 }
277
278 public void testRemove(){
279 SynchronousQueue q = new SynchronousQueue();
280 try {
281 q.remove();
282 fail("remove should throw");
283 } catch (NoSuchElementException success){
284 }
285 }
286
287 public void testRemoveElement(){
288 SynchronousQueue q = new SynchronousQueue();
289 for (int i = 1; i < N; i+=2) {
290 assertFalse(q.remove(new Integer(i)));
291 }
292 assertTrue(q.isEmpty());
293 }
294
295 public void testContains(){
296 SynchronousQueue q = new SynchronousQueue();
297 for (int i = 0; i < N; ++i) {
298 assertFalse(q.contains(new Integer(i)));
299 }
300 }
301
302 public void testClear(){
303 SynchronousQueue q = new SynchronousQueue();
304 q.clear();
305 assertTrue(q.isEmpty());
306 }
307
308 public void testContainsAll(){
309 SynchronousQueue q = new SynchronousQueue();
310 Integer[] empty = new Integer[0];
311 Integer[] ints = new Integer[1]; ints[0] = new Integer(0);
312 // assertTrue(q.containsAll(Arrays.asList(empty)));
313 assertFalse(q.containsAll(Arrays.asList(ints)));
314 }
315
316 public void testRetainAll(){
317 SynchronousQueue q = new SynchronousQueue();
318 Integer[] empty = new Integer[0];
319 Integer[] ints = new Integer[1]; ints[0] = new Integer(0);
320 q.retainAll(Arrays.asList(ints));
321 // assertTrue(q.containsAll(Arrays.asList(empty)));
322 assertFalse(q.containsAll(Arrays.asList(ints)));
323 }
324
325 public void testRemoveAll(){
326 SynchronousQueue q = new SynchronousQueue();
327 Integer[] empty = new Integer[0];
328 Integer[] ints = new Integer[1]; ints[0] = new Integer(0);
329 q.removeAll(Arrays.asList(ints));
330 // assertTrue(q.containsAll(Arrays.asList(empty)));
331 assertFalse(q.containsAll(Arrays.asList(ints)));
332 }
333
334
335 public void testToArray(){
336 SynchronousQueue q = new SynchronousQueue();
337 Object[] o = q.toArray();
338 assertEquals(o.length, 0);
339 }
340
341 public void testToArray2(){
342 SynchronousQueue q = new SynchronousQueue();
343 Integer[] ints = new Integer[1];
344 assertNull(ints[0]);
345 }
346
347 public void testIterator(){
348 SynchronousQueue q = new SynchronousQueue();
349 Iterator it = q.iterator();
350 assertFalse(it.hasNext());
351 try {
352 Object x = it.next();
353 fail("should throw");
354 }
355 catch (NoSuchElementException success) {}
356 }
357
358 public void testIteratorRemove(){
359 SynchronousQueue q = new SynchronousQueue();
360 Iterator it = q.iterator();
361 try {
362 it.remove();
363 fail("should throw");
364 }
365 catch (IllegalStateException success) {}
366 }
367
368 public void testToString(){
369 SynchronousQueue q = new SynchronousQueue();
370 String s = q.toString();
371 assertTrue(s != null);
372 }
373
374
375 public void testOfferInExecutor() {
376 final SynchronousQueue q = new SynchronousQueue();
377 ExecutorService executor = Executors.newFixedThreadPool(2);
378 final Integer one = new Integer(1);
379
380 executor.execute(new Runnable() {
381 public void run() {
382 assertFalse(q.offer(one));
383 try {
384 assertTrue(q.offer(one, MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
385 assertEquals(0, q.remainingCapacity());
386 }
387 catch (InterruptedException e) {
388 fail("should not be interrupted");
389 }
390 }
391 });
392
393 executor.execute(new Runnable() {
394 public void run() {
395 try {
396 Thread.sleep(MEDIUM_DELAY_MS);
397 assertEquals(one, q.take());
398 }
399 catch (InterruptedException e) {
400 fail("should not be interrupted");
401 }
402 }
403 });
404
405 executor.shutdown();
406
407 }
408
409 public void testPollInExecutor() {
410
411 final SynchronousQueue q = new SynchronousQueue();
412
413 ExecutorService executor = Executors.newFixedThreadPool(2);
414
415 executor.execute(new Runnable() {
416 public void run() {
417 assertNull(q.poll());
418 try {
419 assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
420 assertTrue(q.isEmpty());
421 }
422 catch (InterruptedException e) {
423 fail("should not be interrupted");
424 }
425 }
426 });
427
428 executor.execute(new Runnable() {
429 public void run() {
430 try {
431 Thread.sleep(MEDIUM_DELAY_MS);
432 q.put(new Integer(1));
433 }
434 catch (InterruptedException e) {
435 fail("should not be interrupted");
436 }
437 }
438 });
439
440 executor.shutdown();
441
442 }
443
444 public void testSerialization() {
445 SynchronousQueue q = new SynchronousQueue();
446 try {
447 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
448 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
449 out.writeObject(q);
450 out.close();
451
452 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
453 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
454 SynchronousQueue r = (SynchronousQueue)in.readObject();
455 assertEquals(q.size(), r.size());
456 while (!q.isEmpty())
457 assertEquals(q.remove(), r.remove());
458 } catch(Exception e){
459 fail("unexpected exception");
460 }
461 }
462
463 }