7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.*; |
11 |
< |
import java.util.concurrent.*; |
10 |
> |
import java.util.Arrays; |
11 |
> |
import java.util.ArrayList; |
12 |
> |
import java.util.Collection; |
13 |
> |
import java.util.Iterator; |
14 |
> |
import java.util.NoSuchElementException; |
15 |
> |
import java.util.concurrent.BlockingQueue; |
16 |
> |
import java.util.concurrent.CountDownLatch; |
17 |
> |
import java.util.concurrent.Executors; |
18 |
> |
import java.util.concurrent.ExecutorService; |
19 |
> |
import java.util.concurrent.SynchronousQueue; |
20 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
– |
import java.io.*; |
21 |
|
|
22 |
|
public class SynchronousQueueTest extends JSR166TestCase { |
23 |
|
|
57 |
|
} |
58 |
|
|
59 |
|
/** |
53 |
– |
* offer(null) throws NPE |
54 |
– |
*/ |
55 |
– |
public void testOfferNull() { |
56 |
– |
try { |
57 |
– |
SynchronousQueue q = new SynchronousQueue(); |
58 |
– |
q.offer(null); |
59 |
– |
shouldThrow(); |
60 |
– |
} catch (NullPointerException success) {} |
61 |
– |
} |
62 |
– |
|
63 |
– |
/** |
64 |
– |
* add(null) throws NPE |
65 |
– |
*/ |
66 |
– |
public void testAddNull() { |
67 |
– |
try { |
68 |
– |
SynchronousQueue q = new SynchronousQueue(); |
69 |
– |
q.add(null); |
70 |
– |
shouldThrow(); |
71 |
– |
} catch (NullPointerException success) {} |
72 |
– |
} |
73 |
– |
|
74 |
– |
/** |
60 |
|
* offer fails if no active taker |
61 |
|
*/ |
62 |
< |
public void testOffer() { |
63 |
< |
SynchronousQueue q = new SynchronousQueue(); |
62 |
> |
public void testOffer() { testOffer(false); } |
63 |
> |
public void testOffer_fair() { testOffer(true); } |
64 |
> |
public void testOffer(boolean fair) { |
65 |
> |
SynchronousQueue q = new SynchronousQueue(fair); |
66 |
|
assertFalse(q.offer(one)); |
67 |
|
} |
68 |
|
|
69 |
|
/** |
70 |
< |
* add throws ISE if no active taker |
70 |
> |
* add throws IllegalStateException if no active taker |
71 |
|
*/ |
72 |
< |
public void testAdd() { |
72 |
> |
public void testAdd() { testAdd(false); } |
73 |
> |
public void testAdd_fair() { testAdd(true); } |
74 |
> |
public void testAdd(boolean fair) { |
75 |
> |
SynchronousQueue q = new SynchronousQueue(fair); |
76 |
> |
assertEquals(0, q.remainingCapacity()); |
77 |
|
try { |
87 |
– |
SynchronousQueue q = new SynchronousQueue(); |
88 |
– |
assertEquals(0, q.remainingCapacity()); |
78 |
|
q.add(one); |
79 |
|
shouldThrow(); |
80 |
|
} catch (IllegalStateException success) {} |
81 |
|
} |
82 |
|
|
83 |
|
/** |
84 |
< |
* addAll(null) throws NPE |
84 |
> |
* addAll(this) throws IllegalArgumentException |
85 |
|
*/ |
86 |
< |
public void testAddAll1() { |
86 |
> |
public void testAddAll_self() { testAddAll_self(false); } |
87 |
> |
public void testAddAll_self_fair() { testAddAll_self(true); } |
88 |
> |
public void testAddAll_self(boolean fair) { |
89 |
> |
SynchronousQueue q = new SynchronousQueue(fair); |
90 |
|
try { |
99 |
– |
SynchronousQueue q = new SynchronousQueue(); |
100 |
– |
q.addAll(null); |
101 |
– |
shouldThrow(); |
102 |
– |
} catch (NullPointerException success) {} |
103 |
– |
} |
104 |
– |
|
105 |
– |
/** |
106 |
– |
* addAll(this) throws IAE |
107 |
– |
*/ |
108 |
– |
public void testAddAllSelf() { |
109 |
– |
try { |
110 |
– |
SynchronousQueue q = new SynchronousQueue(); |
91 |
|
q.addAll(q); |
92 |
|
shouldThrow(); |
93 |
|
} catch (IllegalArgumentException success) {} |
94 |
|
} |
95 |
|
|
96 |
|
/** |
117 |
– |
* addAll of a collection with null elements throws NPE |
118 |
– |
*/ |
119 |
– |
public void testAddAll2() { |
120 |
– |
try { |
121 |
– |
SynchronousQueue q = new SynchronousQueue(); |
122 |
– |
Integer[] ints = new Integer[1]; |
123 |
– |
q.addAll(Arrays.asList(ints)); |
124 |
– |
shouldThrow(); |
125 |
– |
} catch (NullPointerException success) {} |
126 |
– |
} |
127 |
– |
|
128 |
– |
/** |
97 |
|
* addAll throws ISE if no active taker |
98 |
|
*/ |
99 |
< |
public void testAddAll4() { |
99 |
> |
public void testAddAll_ISE() { testAddAll_ISE(false); } |
100 |
> |
public void testAddAll_ISE_fair() { testAddAll_ISE(true); } |
101 |
> |
public void testAddAll_ISE(boolean fair) { |
102 |
> |
SynchronousQueue q = new SynchronousQueue(fair); |
103 |
> |
Integer[] ints = new Integer[1]; |
104 |
> |
for (int i = 0; i < ints.length; i++) |
105 |
> |
ints[i] = i; |
106 |
> |
Collection<Integer> coll = Arrays.asList(ints); |
107 |
|
try { |
108 |
< |
SynchronousQueue q = new SynchronousQueue(); |
134 |
< |
Integer[] ints = new Integer[1]; |
135 |
< |
for (int i = 0; i < 1; ++i) |
136 |
< |
ints[i] = new Integer(i); |
137 |
< |
q.addAll(Arrays.asList(ints)); |
108 |
> |
q.addAll(coll); |
109 |
|
shouldThrow(); |
110 |
|
} catch (IllegalStateException success) {} |
111 |
|
} |
112 |
|
|
113 |
|
/** |
143 |
– |
* put(null) throws NPE |
144 |
– |
*/ |
145 |
– |
public void testPutNull() throws InterruptedException { |
146 |
– |
try { |
147 |
– |
SynchronousQueue q = new SynchronousQueue(); |
148 |
– |
q.put(null); |
149 |
– |
shouldThrow(); |
150 |
– |
} catch (NullPointerException success) {} |
151 |
– |
} |
152 |
– |
|
153 |
– |
/** |
114 |
|
* put blocks interruptibly if no active taker |
115 |
|
*/ |
116 |
|
public void testBlockingPut() { testBlockingPut(false); } |
165 |
|
}}); |
166 |
|
|
167 |
|
await(pleaseTake); |
168 |
< |
assertEquals(q.remainingCapacity(), 0); |
168 |
> |
assertEquals(0, q.remainingCapacity()); |
169 |
|
try { assertSame(one, q.take()); } |
170 |
|
catch (InterruptedException e) { threadUnexpectedException(e); } |
171 |
|
|
173 |
|
assertThreadStaysAlive(t); |
174 |
|
t.interrupt(); |
175 |
|
awaitTermination(t); |
176 |
< |
assertEquals(q.remainingCapacity(), 0); |
176 |
> |
assertEquals(0, q.remainingCapacity()); |
177 |
|
} |
178 |
|
|
179 |
|
/** |
205 |
|
/** |
206 |
|
* poll return null if no active putter |
207 |
|
*/ |
208 |
< |
public void testPoll() { |
209 |
< |
SynchronousQueue q = new SynchronousQueue(); |
208 |
> |
public void testPoll() { testPoll(false); } |
209 |
> |
public void testPoll_fair() { testPoll(true); } |
210 |
> |
public void testPoll(boolean fair) { |
211 |
> |
final SynchronousQueue q = new SynchronousQueue(fair); |
212 |
|
assertNull(q.poll()); |
213 |
|
} |
214 |
|
|
215 |
|
/** |
216 |
|
* timed poll with zero timeout times out if no active putter |
217 |
|
*/ |
218 |
< |
public void testTimedPoll0() throws InterruptedException { |
219 |
< |
SynchronousQueue q = new SynchronousQueue(); |
220 |
< |
assertNull(q.poll(0, MILLISECONDS)); |
218 |
> |
public void testTimedPoll0() { testTimedPoll0(false); } |
219 |
> |
public void testTimedPoll0_fair() { testTimedPoll0(true); } |
220 |
> |
public void testTimedPoll0(boolean fair) { |
221 |
> |
final SynchronousQueue q = new SynchronousQueue(fair); |
222 |
> |
try { assertNull(q.poll(0, MILLISECONDS)); } |
223 |
> |
catch (InterruptedException e) { threadUnexpectedException(e); } |
224 |
|
} |
225 |
|
|
226 |
|
/** |
227 |
|
* timed poll with nonzero timeout times out if no active putter |
228 |
|
*/ |
229 |
< |
public void testTimedPoll() throws InterruptedException { |
230 |
< |
SynchronousQueue q = new SynchronousQueue(); |
229 |
> |
public void testTimedPoll() { testTimedPoll(false); } |
230 |
> |
public void testTimedPoll_fair() { testTimedPoll(true); } |
231 |
> |
public void testTimedPoll(boolean fair) { |
232 |
> |
final SynchronousQueue q = new SynchronousQueue(fair); |
233 |
|
long startTime = System.nanoTime(); |
234 |
< |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
234 |
> |
try { assertNull(q.poll(timeoutMillis(), MILLISECONDS)); } |
235 |
> |
catch (InterruptedException e) { threadUnexpectedException(e); } |
236 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
237 |
|
} |
238 |
|
|
240 |
|
* timed poll before a delayed offer times out, returning null; |
241 |
|
* after offer succeeds; on interruption throws |
242 |
|
*/ |
243 |
< |
public void testFairTimedPollWithOffer() throws InterruptedException { |
244 |
< |
final SynchronousQueue q = new SynchronousQueue(true); |
243 |
> |
public void testTimedPollWithOffer() { testTimedPollWithOffer(false); } |
244 |
> |
public void testTimedPollWithOffer_fair() { testTimedPollWithOffer(true); } |
245 |
> |
public void testTimedPollWithOffer(boolean fair) { |
246 |
> |
final SynchronousQueue q = new SynchronousQueue(fair); |
247 |
|
final CountDownLatch pleaseOffer = new CountDownLatch(1); |
248 |
+ |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
249 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
250 |
|
public void realRun() throws InterruptedException { |
251 |
< |
long t0 = System.nanoTime(); |
252 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
253 |
< |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
251 |
> |
long startTime = System.nanoTime(); |
252 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
253 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
254 |
|
|
255 |
|
pleaseOffer.countDown(); |
256 |
< |
t0 = System.nanoTime(); |
256 |
> |
startTime = System.nanoTime(); |
257 |
|
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
258 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
258 |
> |
assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
259 |
|
|
260 |
< |
t0 = System.nanoTime(); |
260 |
> |
Thread.currentThread().interrupt(); |
261 |
|
try { |
262 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
263 |
|
shouldThrow(); |
264 |
|
} catch (InterruptedException success) {} |
265 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
265 |
> |
assertFalse(Thread.interrupted()); |
266 |
> |
|
267 |
> |
pleaseInterrupt.countDown(); |
268 |
> |
try { |
269 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
270 |
> |
shouldThrow(); |
271 |
> |
} catch (InterruptedException success) {} |
272 |
> |
assertFalse(Thread.interrupted()); |
273 |
|
}}); |
274 |
|
|
275 |
< |
assertTrue(pleaseOffer.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
276 |
< |
long t0 = System.nanoTime(); |
277 |
< |
assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); |
278 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
275 |
> |
await(pleaseOffer); |
276 |
> |
long startTime = System.nanoTime(); |
277 |
> |
try { assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); } |
278 |
> |
catch (InterruptedException e) { threadUnexpectedException(e); } |
279 |
> |
assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
280 |
|
|
281 |
+ |
await(pleaseInterrupt); |
282 |
+ |
assertThreadStaysAlive(t); |
283 |
|
t.interrupt(); |
284 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
284 |
> |
awaitTermination(t); |
285 |
|
} |
286 |
|
|
287 |
|
/** |
288 |
|
* peek() returns null if no active putter |
289 |
|
*/ |
290 |
< |
public void testPeek() { |
291 |
< |
SynchronousQueue q = new SynchronousQueue(); |
290 |
> |
public void testPeek() { testPeek(false); } |
291 |
> |
public void testPeek_fair() { testPeek(true); } |
292 |
> |
public void testPeek(boolean fair) { |
293 |
> |
final SynchronousQueue q = new SynchronousQueue(fair); |
294 |
|
assertNull(q.peek()); |
295 |
|
} |
296 |
|
|
297 |
|
/** |
298 |
< |
* element() throws NSEE if no active putter |
298 |
> |
* element() throws NoSuchElementException if no active putter |
299 |
|
*/ |
300 |
< |
public void testElement() { |
301 |
< |
SynchronousQueue q = new SynchronousQueue(); |
300 |
> |
public void testElement() { testElement(false); } |
301 |
> |
public void testElement_fair() { testElement(true); } |
302 |
> |
public void testElement(boolean fair) { |
303 |
> |
final SynchronousQueue q = new SynchronousQueue(fair); |
304 |
|
try { |
305 |
|
q.element(); |
306 |
|
shouldThrow(); |
308 |
|
} |
309 |
|
|
310 |
|
/** |
311 |
< |
* remove() throws NSEE if no active putter |
311 |
> |
* remove() throws NoSuchElementException if no active putter |
312 |
|
*/ |
313 |
< |
public void testRemove() { |
314 |
< |
SynchronousQueue q = new SynchronousQueue(); |
313 |
> |
public void testRemove() { testRemove(false); } |
314 |
> |
public void testRemove_fair() { testRemove(true); } |
315 |
> |
public void testRemove(boolean fair) { |
316 |
> |
final SynchronousQueue q = new SynchronousQueue(fair); |
317 |
|
try { |
318 |
|
q.remove(); |
319 |
|
shouldThrow(); |
321 |
|
} |
322 |
|
|
323 |
|
/** |
337 |
– |
* remove(x) returns false |
338 |
– |
*/ |
339 |
– |
public void testRemoveElement() { |
340 |
– |
SynchronousQueue q = new SynchronousQueue(); |
341 |
– |
assertFalse(q.remove(zero)); |
342 |
– |
assertTrue(q.isEmpty()); |
343 |
– |
} |
344 |
– |
|
345 |
– |
/** |
324 |
|
* contains returns false |
325 |
|
*/ |
326 |
< |
public void testContains() { |
327 |
< |
SynchronousQueue q = new SynchronousQueue(); |
326 |
> |
public void testContains() { testContains(false); } |
327 |
> |
public void testContains_fair() { testContains(true); } |
328 |
> |
public void testContains(boolean fair) { |
329 |
> |
final SynchronousQueue q = new SynchronousQueue(fair); |
330 |
|
assertFalse(q.contains(zero)); |
331 |
|
} |
332 |
|
|
333 |
|
/** |
334 |
|
* clear ensures isEmpty |
335 |
|
*/ |
336 |
< |
public void testClear() { |
337 |
< |
SynchronousQueue q = new SynchronousQueue(); |
336 |
> |
public void testClear() { testClear(false); } |
337 |
> |
public void testClear_fair() { testClear(true); } |
338 |
> |
public void testClear(boolean fair) { |
339 |
> |
final SynchronousQueue q = new SynchronousQueue(fair); |
340 |
|
q.clear(); |
341 |
|
assertTrue(q.isEmpty()); |
342 |
|
} |
344 |
|
/** |
345 |
|
* containsAll returns false unless empty |
346 |
|
*/ |
347 |
< |
public void testContainsAll() { |
348 |
< |
SynchronousQueue q = new SynchronousQueue(); |
347 |
> |
public void testContainsAll() { testContainsAll(false); } |
348 |
> |
public void testContainsAll_fair() { testContainsAll(true); } |
349 |
> |
public void testContainsAll(boolean fair) { |
350 |
> |
final SynchronousQueue q = new SynchronousQueue(fair); |
351 |
|
Integer[] empty = new Integer[0]; |
352 |
|
assertTrue(q.containsAll(Arrays.asList(empty))); |
353 |
|
Integer[] ints = new Integer[1]; ints[0] = zero; |
357 |
|
/** |
358 |
|
* retainAll returns false |
359 |
|
*/ |
360 |
< |
public void testRetainAll() { |
361 |
< |
SynchronousQueue q = new SynchronousQueue(); |
360 |
> |
public void testRetainAll() { testRetainAll(false); } |
361 |
> |
public void testRetainAll_fair() { testRetainAll(true); } |
362 |
> |
public void testRetainAll(boolean fair) { |
363 |
> |
final SynchronousQueue q = new SynchronousQueue(fair); |
364 |
|
Integer[] empty = new Integer[0]; |
365 |
|
assertFalse(q.retainAll(Arrays.asList(empty))); |
366 |
|
Integer[] ints = new Integer[1]; ints[0] = zero; |
370 |
|
/** |
371 |
|
* removeAll returns false |
372 |
|
*/ |
373 |
< |
public void testRemoveAll() { |
374 |
< |
SynchronousQueue q = new SynchronousQueue(); |
373 |
> |
public void testRemoveAll() { testRemoveAll(false); } |
374 |
> |
public void testRemoveAll_fair() { testRemoveAll(true); } |
375 |
> |
public void testRemoveAll(boolean fair) { |
376 |
> |
final SynchronousQueue q = new SynchronousQueue(fair); |
377 |
|
Integer[] empty = new Integer[0]; |
378 |
|
assertFalse(q.removeAll(Arrays.asList(empty))); |
379 |
|
Integer[] ints = new Integer[1]; ints[0] = zero; |
383 |
|
/** |
384 |
|
* toArray is empty |
385 |
|
*/ |
386 |
< |
public void testToArray() { |
387 |
< |
SynchronousQueue q = new SynchronousQueue(); |
386 |
> |
public void testToArray() { testToArray(false); } |
387 |
> |
public void testToArray_fair() { testToArray(true); } |
388 |
> |
public void testToArray(boolean fair) { |
389 |
> |
final SynchronousQueue q = new SynchronousQueue(fair); |
390 |
|
Object[] o = q.toArray(); |
391 |
< |
assertEquals(o.length, 0); |
391 |
> |
assertEquals(0, o.length); |
392 |
|
} |
393 |
|
|
394 |
|
/** |
395 |
< |
* toArray(a) is nulled at position 0 |
395 |
> |
* toArray(Integer array) returns its argument with the first |
396 |
> |
* element (if present) nulled out |
397 |
|
*/ |
398 |
< |
public void testToArray2() { |
399 |
< |
SynchronousQueue q = new SynchronousQueue(); |
400 |
< |
Integer[] ints = new Integer[1]; |
401 |
< |
assertNull(ints[0]); |
398 |
> |
public void testToArray2() { testToArray2(false); } |
399 |
> |
public void testToArray2_fair() { testToArray2(true); } |
400 |
> |
public void testToArray2(boolean fair) { |
401 |
> |
final SynchronousQueue<Integer> q |
402 |
> |
= new SynchronousQueue<Integer>(fair); |
403 |
> |
Integer[] a; |
404 |
> |
|
405 |
> |
a = new Integer[0]; |
406 |
> |
assertSame(a, q.toArray(a)); |
407 |
> |
|
408 |
> |
a = new Integer[3]; |
409 |
> |
Arrays.fill(a, 42); |
410 |
> |
assertSame(a, q.toArray(a)); |
411 |
> |
assertNull(a[0]); |
412 |
> |
for (int i = 1; i < a.length; i++) |
413 |
> |
assertEquals(42, (int) a[i]); |
414 |
|
} |
415 |
|
|
416 |
|
/** |
417 |
|
* toArray(null) throws NPE |
418 |
|
*/ |
419 |
< |
public void testToArray_BadArg() { |
420 |
< |
SynchronousQueue q = new SynchronousQueue(); |
419 |
> |
public void testToArray_null() { testToArray_null(false); } |
420 |
> |
public void testToArray_null_fair() { testToArray_null(true); } |
421 |
> |
public void testToArray_null(boolean fair) { |
422 |
> |
final SynchronousQueue q = new SynchronousQueue(fair); |
423 |
|
try { |
424 |
|
Object o[] = q.toArray(null); |
425 |
|
shouldThrow(); |
429 |
|
/** |
430 |
|
* iterator does not traverse any elements |
431 |
|
*/ |
432 |
< |
public void testIterator() { |
433 |
< |
SynchronousQueue q = new SynchronousQueue(); |
432 |
> |
public void testIterator() { testIterator(false); } |
433 |
> |
public void testIterator_fair() { testIterator(true); } |
434 |
> |
public void testIterator(boolean fair) { |
435 |
> |
final SynchronousQueue q = new SynchronousQueue(fair); |
436 |
|
Iterator it = q.iterator(); |
437 |
|
assertFalse(it.hasNext()); |
438 |
|
try { |
444 |
|
/** |
445 |
|
* iterator remove throws ISE |
446 |
|
*/ |
447 |
< |
public void testIteratorRemove() { |
448 |
< |
SynchronousQueue q = new SynchronousQueue(); |
447 |
> |
public void testIteratorRemove() { testIteratorRemove(false); } |
448 |
> |
public void testIteratorRemove_fair() { testIteratorRemove(true); } |
449 |
> |
public void testIteratorRemove(boolean fair) { |
450 |
> |
final SynchronousQueue q = new SynchronousQueue(fair); |
451 |
|
Iterator it = q.iterator(); |
452 |
|
try { |
453 |
|
it.remove(); |
458 |
|
/** |
459 |
|
* toString returns a non-null string |
460 |
|
*/ |
461 |
< |
public void testToString() { |
462 |
< |
SynchronousQueue q = new SynchronousQueue(); |
461 |
> |
public void testToString() { testToString(false); } |
462 |
> |
public void testToString_fair() { testToString(true); } |
463 |
> |
public void testToString(boolean fair) { |
464 |
> |
final SynchronousQueue q = new SynchronousQueue(fair); |
465 |
|
String s = q.toString(); |
466 |
|
assertNotNull(s); |
467 |
|
} |
469 |
|
/** |
470 |
|
* offer transfers elements across Executor tasks |
471 |
|
*/ |
472 |
< |
public void testOfferInExecutor() { |
473 |
< |
final SynchronousQueue q = new SynchronousQueue(); |
472 |
> |
public void testOfferInExecutor() { testOfferInExecutor(false); } |
473 |
> |
public void testOfferInExecutor_fair() { testOfferInExecutor(true); } |
474 |
> |
public void testOfferInExecutor(boolean fair) { |
475 |
> |
final SynchronousQueue q = new SynchronousQueue(fair); |
476 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
477 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
478 |
|
|
496 |
|
/** |
497 |
|
* timed poll retrieves elements across Executor threads |
498 |
|
*/ |
499 |
< |
public void testPollInExecutor() { |
500 |
< |
final SynchronousQueue q = new SynchronousQueue(); |
499 |
> |
public void testPollInExecutor() { testPollInExecutor(false); } |
500 |
> |
public void testPollInExecutor_fair() { testPollInExecutor(true); } |
501 |
> |
public void testPollInExecutor(boolean fair) { |
502 |
> |
final SynchronousQueue q = new SynchronousQueue(fair); |
503 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
504 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
505 |
|
executor.execute(new CheckedRunnable() { |
522 |
|
/** |
523 |
|
* a deserialized serialized queue is usable |
524 |
|
*/ |
525 |
< |
public void testSerialization() throws Exception { |
526 |
< |
SynchronousQueue q = new SynchronousQueue(); |
527 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
528 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
529 |
< |
out.writeObject(q); |
530 |
< |
out.close(); |
531 |
< |
|
532 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
533 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
534 |
< |
SynchronousQueue r = (SynchronousQueue)in.readObject(); |
535 |
< |
assertEquals(q.size(), r.size()); |
536 |
< |
while (!q.isEmpty()) |
537 |
< |
assertEquals(q.remove(), r.remove()); |
538 |
< |
} |
539 |
< |
|
540 |
< |
/** |
526 |
< |
* drainTo(null) throws NPE |
527 |
< |
*/ |
528 |
< |
public void testDrainToNull() { |
529 |
< |
SynchronousQueue q = new SynchronousQueue(); |
530 |
< |
try { |
531 |
< |
q.drainTo(null); |
532 |
< |
shouldThrow(); |
533 |
< |
} catch (NullPointerException success) {} |
534 |
< |
} |
535 |
< |
|
536 |
< |
/** |
537 |
< |
* drainTo(this) throws IAE |
538 |
< |
*/ |
539 |
< |
public void testDrainToSelf() { |
540 |
< |
SynchronousQueue q = new SynchronousQueue(); |
541 |
< |
try { |
542 |
< |
q.drainTo(q); |
543 |
< |
shouldThrow(); |
544 |
< |
} catch (IllegalArgumentException success) {} |
525 |
> |
public void testSerialization() { |
526 |
> |
final SynchronousQueue x = new SynchronousQueue(); |
527 |
> |
final SynchronousQueue y = new SynchronousQueue(false); |
528 |
> |
final SynchronousQueue z = new SynchronousQueue(true); |
529 |
> |
assertSerialEquals(x, y); |
530 |
> |
assertNotSerialEquals(x, z); |
531 |
> |
SynchronousQueue[] qs = { x, y, z }; |
532 |
> |
for (SynchronousQueue q : qs) { |
533 |
> |
SynchronousQueue clone = serialClone(q); |
534 |
> |
assertNotSame(q, clone); |
535 |
> |
assertSerialEquals(q, clone); |
536 |
> |
assertTrue(clone.isEmpty()); |
537 |
> |
assertEquals(0, clone.size()); |
538 |
> |
assertEquals(0, clone.remainingCapacity()); |
539 |
> |
assertFalse(clone.offer(zero)); |
540 |
> |
} |
541 |
|
} |
542 |
|
|
543 |
|
/** |
544 |
|
* drainTo(c) of empty queue doesn't transfer elements |
545 |
|
*/ |
546 |
< |
public void testDrainTo() { |
547 |
< |
SynchronousQueue q = new SynchronousQueue(); |
546 |
> |
public void testDrainTo() { testDrainTo(false); } |
547 |
> |
public void testDrainTo_fair() { testDrainTo(true); } |
548 |
> |
public void testDrainTo(boolean fair) { |
549 |
> |
final SynchronousQueue q = new SynchronousQueue(fair); |
550 |
|
ArrayList l = new ArrayList(); |
551 |
|
q.drainTo(l); |
552 |
< |
assertEquals(q.size(), 0); |
553 |
< |
assertEquals(l.size(), 0); |
552 |
> |
assertEquals(0, q.size()); |
553 |
> |
assertEquals(0, l.size()); |
554 |
|
} |
555 |
|
|
556 |
|
/** |
557 |
|
* drainTo empties queue, unblocking a waiting put. |
558 |
|
*/ |
559 |
< |
public void testDrainToWithActivePut() throws InterruptedException { |
560 |
< |
final SynchronousQueue q = new SynchronousQueue(); |
559 |
> |
public void testDrainToWithActivePut() { testDrainToWithActivePut(false); } |
560 |
> |
public void testDrainToWithActivePut_fair() { testDrainToWithActivePut(true); } |
561 |
> |
public void testDrainToWithActivePut(boolean fair) { |
562 |
> |
final SynchronousQueue q = new SynchronousQueue(fair); |
563 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
564 |
|
public void realRun() throws InterruptedException { |
565 |
|
q.put(one); |
579 |
|
} |
580 |
|
|
581 |
|
/** |
582 |
– |
* drainTo(null, n) throws NPE |
583 |
– |
*/ |
584 |
– |
public void testDrainToNullN() { |
585 |
– |
SynchronousQueue q = new SynchronousQueue(); |
586 |
– |
try { |
587 |
– |
q.drainTo(null, 0); |
588 |
– |
shouldThrow(); |
589 |
– |
} catch (NullPointerException success) {} |
590 |
– |
} |
591 |
– |
|
592 |
– |
/** |
593 |
– |
* drainTo(this, n) throws IAE |
594 |
– |
*/ |
595 |
– |
public void testDrainToSelfN() { |
596 |
– |
SynchronousQueue q = new SynchronousQueue(); |
597 |
– |
try { |
598 |
– |
q.drainTo(q, 0); |
599 |
– |
shouldThrow(); |
600 |
– |
} catch (IllegalArgumentException success) {} |
601 |
– |
} |
602 |
– |
|
603 |
– |
/** |
582 |
|
* drainTo(c, n) empties up to n elements of queue into c |
583 |
|
*/ |
584 |
|
public void testDrainToN() throws InterruptedException { |
605 |
|
awaitTermination(t2); |
606 |
|
} |
607 |
|
|
608 |
+ |
/** |
609 |
+ |
* remove(null), contains(null) always return false |
610 |
+ |
*/ |
611 |
+ |
public void testNeverContainsNull() { |
612 |
+ |
Collection<?> q = new SynchronousQueue(); |
613 |
+ |
assertFalse(q.contains(null)); |
614 |
+ |
assertFalse(q.remove(null)); |
615 |
+ |
} |
616 |
+ |
|
617 |
|
} |