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