1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
10 |
– |
import java.util.*; |
11 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
< |
import java.io.*; |
10 |
> |
|
11 |
> |
import java.util.ArrayList; |
12 |
> |
import java.util.Arrays; |
13 |
> |
import java.util.Collection; |
14 |
> |
import java.util.Iterator; |
15 |
> |
import java.util.NoSuchElementException; |
16 |
> |
import java.util.Queue; |
17 |
> |
import java.util.concurrent.BlockingQueue; |
18 |
> |
import java.util.concurrent.CountDownLatch; |
19 |
> |
import java.util.concurrent.Executors; |
20 |
> |
import java.util.concurrent.ExecutorService; |
21 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
22 |
> |
|
23 |
> |
import junit.framework.Test; |
24 |
|
|
25 |
|
public class LinkedBlockingQueueTest extends JSR166TestCase { |
26 |
|
|
32 |
|
|
33 |
|
public static class Bounded extends BlockingQueueTest { |
34 |
|
protected BlockingQueue emptyCollection() { |
35 |
< |
return new LinkedBlockingQueue(20); |
35 |
> |
return new LinkedBlockingQueue(SIZE); |
36 |
|
} |
37 |
|
} |
38 |
|
|
46 |
|
new Bounded().testSuite()); |
47 |
|
} |
48 |
|
|
39 |
– |
|
49 |
|
/** |
50 |
< |
* Create a queue of given size containing consecutive |
50 |
> |
* Returns a new queue of given size containing consecutive |
51 |
|
* Integers 0 ... n. |
52 |
|
*/ |
53 |
< |
private LinkedBlockingQueue populatedQueue(int n) { |
54 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(n); |
53 |
> |
private LinkedBlockingQueue<Integer> populatedQueue(int n) { |
54 |
> |
LinkedBlockingQueue<Integer> q = |
55 |
> |
new LinkedBlockingQueue<Integer>(n); |
56 |
|
assertTrue(q.isEmpty()); |
57 |
|
for (int i = 0; i < n; i++) |
58 |
|
assertTrue(q.offer(new Integer(i))); |
72 |
|
} |
73 |
|
|
74 |
|
/** |
75 |
< |
* Constructor throws IAE if capacity argument nonpositive |
75 |
> |
* Constructor throws IllegalArgumentException if capacity argument nonpositive |
76 |
|
*/ |
77 |
|
public void testConstructor2() { |
78 |
|
try { |
79 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(0); |
79 |
> |
new LinkedBlockingQueue(0); |
80 |
|
shouldThrow(); |
81 |
|
} catch (IllegalArgumentException success) {} |
82 |
|
} |
83 |
|
|
84 |
|
/** |
85 |
< |
* Initializing from null Collection throws NPE |
85 |
> |
* Initializing from null Collection throws NullPointerException |
86 |
|
*/ |
87 |
|
public void testConstructor3() { |
88 |
|
try { |
89 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(null); |
89 |
> |
new LinkedBlockingQueue(null); |
90 |
|
shouldThrow(); |
91 |
|
} catch (NullPointerException success) {} |
92 |
|
} |
93 |
|
|
94 |
|
/** |
95 |
< |
* Initializing from Collection of null elements throws NPE |
95 |
> |
* Initializing from Collection of null elements throws NullPointerException |
96 |
|
*/ |
97 |
|
public void testConstructor4() { |
98 |
+ |
Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
99 |
|
try { |
100 |
< |
Integer[] ints = new Integer[SIZE]; |
90 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
100 |
> |
new LinkedBlockingQueue(elements); |
101 |
|
shouldThrow(); |
102 |
|
} catch (NullPointerException success) {} |
103 |
|
} |
104 |
|
|
105 |
|
/** |
106 |
< |
* Initializing from Collection with some null elements throws NPE |
106 |
> |
* Initializing from Collection with some null elements throws |
107 |
> |
* NullPointerException |
108 |
|
*/ |
109 |
|
public void testConstructor5() { |
110 |
+ |
Integer[] ints = new Integer[SIZE]; |
111 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
112 |
+ |
ints[i] = new Integer(i); |
113 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
114 |
|
try { |
115 |
< |
Integer[] ints = new Integer[SIZE]; |
101 |
< |
for (int i = 0; i < SIZE-1; ++i) |
102 |
< |
ints[i] = new Integer(i); |
103 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
115 |
> |
new LinkedBlockingQueue(elements); |
116 |
|
shouldThrow(); |
117 |
|
} catch (NullPointerException success) {} |
118 |
|
} |
162 |
|
} |
163 |
|
|
164 |
|
/** |
153 |
– |
* offer(null) throws NPE |
154 |
– |
*/ |
155 |
– |
public void testOfferNull() { |
156 |
– |
try { |
157 |
– |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
158 |
– |
q.offer(null); |
159 |
– |
shouldThrow(); |
160 |
– |
} catch (NullPointerException success) {} |
161 |
– |
} |
162 |
– |
|
163 |
– |
/** |
164 |
– |
* add(null) throws NPE |
165 |
– |
*/ |
166 |
– |
public void testAddNull() { |
167 |
– |
try { |
168 |
– |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
169 |
– |
q.add(null); |
170 |
– |
shouldThrow(); |
171 |
– |
} catch (NullPointerException success) {} |
172 |
– |
} |
173 |
– |
|
174 |
– |
/** |
165 |
|
* Offer succeeds if not full; fails if full |
166 |
|
*/ |
167 |
|
public void testOffer() { |
171 |
|
} |
172 |
|
|
173 |
|
/** |
174 |
< |
* add succeeds if not full; throws ISE if full |
174 |
> |
* add succeeds if not full; throws IllegalStateException if full |
175 |
|
*/ |
176 |
|
public void testAdd() { |
177 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
178 |
+ |
for (int i = 0; i < SIZE; ++i) |
179 |
+ |
assertTrue(q.add(new Integer(i))); |
180 |
+ |
assertEquals(0, q.remainingCapacity()); |
181 |
|
try { |
188 |
– |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
189 |
– |
for (int i = 0; i < SIZE; ++i) { |
190 |
– |
assertTrue(q.add(new Integer(i))); |
191 |
– |
} |
192 |
– |
assertEquals(0, q.remainingCapacity()); |
182 |
|
q.add(new Integer(SIZE)); |
183 |
|
shouldThrow(); |
184 |
|
} catch (IllegalStateException success) {} |
185 |
|
} |
186 |
|
|
187 |
|
/** |
188 |
< |
* addAll(null) throws NPE |
200 |
< |
*/ |
201 |
< |
public void testAddAll1() { |
202 |
< |
try { |
203 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
204 |
< |
q.addAll(null); |
205 |
< |
shouldThrow(); |
206 |
< |
} catch (NullPointerException success) {} |
207 |
< |
} |
208 |
< |
|
209 |
< |
/** |
210 |
< |
* addAll(this) throws IAE |
188 |
> |
* addAll(this) throws IllegalArgumentException |
189 |
|
*/ |
190 |
|
public void testAddAllSelf() { |
191 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
192 |
|
try { |
214 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
193 |
|
q.addAll(q); |
194 |
|
shouldThrow(); |
195 |
|
} catch (IllegalArgumentException success) {} |
196 |
|
} |
197 |
|
|
198 |
|
/** |
221 |
– |
* addAll of a collection with null elements throws NPE |
222 |
– |
*/ |
223 |
– |
public void testAddAll2() { |
224 |
– |
try { |
225 |
– |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
226 |
– |
Integer[] ints = new Integer[SIZE]; |
227 |
– |
q.addAll(Arrays.asList(ints)); |
228 |
– |
shouldThrow(); |
229 |
– |
} catch (NullPointerException success) {} |
230 |
– |
} |
231 |
– |
|
232 |
– |
/** |
199 |
|
* addAll of a collection with any null elements throws NPE after |
200 |
|
* possibly adding some elements |
201 |
|
*/ |
202 |
|
public void testAddAll3() { |
203 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
204 |
+ |
Integer[] ints = new Integer[SIZE]; |
205 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
206 |
+ |
ints[i] = new Integer(i); |
207 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
208 |
|
try { |
209 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
239 |
< |
Integer[] ints = new Integer[SIZE]; |
240 |
< |
for (int i = 0; i < SIZE-1; ++i) |
241 |
< |
ints[i] = new Integer(i); |
242 |
< |
q.addAll(Arrays.asList(ints)); |
209 |
> |
q.addAll(elements); |
210 |
|
shouldThrow(); |
211 |
|
} catch (NullPointerException success) {} |
212 |
|
} |
213 |
|
|
214 |
|
/** |
215 |
< |
* addAll throws ISE if not enough room |
215 |
> |
* addAll throws IllegalStateException if not enough room |
216 |
|
*/ |
217 |
|
public void testAddAll4() { |
218 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE - 1); |
219 |
+ |
Integer[] ints = new Integer[SIZE]; |
220 |
+ |
for (int i = 0; i < SIZE; ++i) |
221 |
+ |
ints[i] = new Integer(i); |
222 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
223 |
|
try { |
224 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
253 |
< |
Integer[] ints = new Integer[SIZE]; |
254 |
< |
for (int i = 0; i < SIZE; ++i) |
255 |
< |
ints[i] = new Integer(i); |
256 |
< |
q.addAll(Arrays.asList(ints)); |
224 |
> |
q.addAll(elements); |
225 |
|
shouldThrow(); |
226 |
|
} catch (IllegalStateException success) {} |
227 |
|
} |
242 |
|
} |
243 |
|
|
244 |
|
/** |
277 |
– |
* put(null) throws NPE |
278 |
– |
*/ |
279 |
– |
public void testPutNull() throws InterruptedException { |
280 |
– |
try { |
281 |
– |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
282 |
– |
q.put(null); |
283 |
– |
shouldThrow(); |
284 |
– |
} catch (NullPointerException success) {} |
285 |
– |
} |
286 |
– |
|
287 |
– |
/** |
245 |
|
* all elements successfully put are contained |
246 |
|
*/ |
247 |
|
public void testPut() throws InterruptedException { |
259 |
|
*/ |
260 |
|
public void testBlockingPut() throws InterruptedException { |
261 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
262 |
< |
Thread t = new Thread(new CheckedRunnable() { |
262 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
263 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
264 |
|
public void realRun() throws InterruptedException { |
265 |
|
for (int i = 0; i < SIZE; ++i) |
266 |
|
q.put(i); |
267 |
|
assertEquals(SIZE, q.size()); |
268 |
|
assertEquals(0, q.remainingCapacity()); |
269 |
+ |
|
270 |
+ |
Thread.currentThread().interrupt(); |
271 |
+ |
try { |
272 |
+ |
q.put(99); |
273 |
+ |
shouldThrow(); |
274 |
+ |
} catch (InterruptedException success) {} |
275 |
+ |
assertFalse(Thread.interrupted()); |
276 |
+ |
|
277 |
+ |
pleaseInterrupt.countDown(); |
278 |
|
try { |
279 |
|
q.put(99); |
280 |
|
shouldThrow(); |
281 |
|
} catch (InterruptedException success) {} |
282 |
+ |
assertFalse(Thread.interrupted()); |
283 |
|
}}); |
284 |
|
|
285 |
< |
t.start(); |
286 |
< |
Thread.sleep(SHORT_DELAY_MS); |
285 |
> |
await(pleaseInterrupt); |
286 |
> |
assertThreadStaysAlive(t); |
287 |
|
t.interrupt(); |
288 |
< |
t.join(); |
288 |
> |
awaitTermination(t); |
289 |
|
assertEquals(SIZE, q.size()); |
290 |
|
assertEquals(0, q.remainingCapacity()); |
291 |
|
} |
292 |
|
|
293 |
|
/** |
294 |
< |
* put blocks waiting for take when full |
294 |
> |
* put blocks interruptibly waiting for take when full |
295 |
|
*/ |
296 |
|
public void testPutWithTake() throws InterruptedException { |
297 |
|
final int capacity = 2; |
298 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
299 |
< |
Thread t = new Thread(new CheckedRunnable() { |
299 |
> |
final CountDownLatch pleaseTake = new CountDownLatch(1); |
300 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
301 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
302 |
|
public void realRun() throws InterruptedException { |
303 |
< |
for (int i = 0; i < capacity + 1; i++) |
303 |
> |
for (int i = 0; i < capacity; i++) |
304 |
|
q.put(i); |
305 |
+ |
pleaseTake.countDown(); |
306 |
+ |
q.put(86); |
307 |
+ |
|
308 |
+ |
pleaseInterrupt.countDown(); |
309 |
|
try { |
310 |
|
q.put(99); |
311 |
|
shouldThrow(); |
312 |
|
} catch (InterruptedException success) {} |
313 |
+ |
assertFalse(Thread.interrupted()); |
314 |
|
}}); |
315 |
|
|
316 |
< |
t.start(); |
317 |
< |
Thread.sleep(SHORT_DELAY_MS); |
343 |
< |
assertEquals(q.remainingCapacity(), 0); |
316 |
> |
await(pleaseTake); |
317 |
> |
assertEquals(0, q.remainingCapacity()); |
318 |
|
assertEquals(0, q.take()); |
319 |
< |
Thread.sleep(SHORT_DELAY_MS); |
319 |
> |
|
320 |
> |
await(pleaseInterrupt); |
321 |
> |
assertThreadStaysAlive(t); |
322 |
|
t.interrupt(); |
323 |
< |
t.join(); |
324 |
< |
assertEquals(q.remainingCapacity(), 0); |
323 |
> |
awaitTermination(t); |
324 |
> |
assertEquals(0, q.remainingCapacity()); |
325 |
|
} |
326 |
|
|
327 |
|
/** |
328 |
|
* timed offer times out if full and elements not taken |
329 |
|
*/ |
330 |
< |
public void testTimedOffer() throws InterruptedException { |
330 |
> |
public void testTimedOffer() { |
331 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
332 |
< |
Thread t = new Thread(new CheckedRunnable() { |
332 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
333 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
334 |
|
public void realRun() throws InterruptedException { |
335 |
|
q.put(new Object()); |
336 |
|
q.put(new Object()); |
337 |
< |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
337 |
> |
long startTime = System.nanoTime(); |
338 |
> |
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
339 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
340 |
> |
pleaseInterrupt.countDown(); |
341 |
|
try { |
342 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
342 |
> |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
343 |
|
shouldThrow(); |
344 |
|
} catch (InterruptedException success) {} |
345 |
|
}}); |
346 |
|
|
347 |
< |
t.start(); |
348 |
< |
Thread.sleep(SMALL_DELAY_MS); |
347 |
> |
await(pleaseInterrupt); |
348 |
> |
assertThreadStaysAlive(t); |
349 |
|
t.interrupt(); |
350 |
< |
t.join(); |
350 |
> |
awaitTermination(t); |
351 |
|
} |
352 |
|
|
353 |
|
/** |
364 |
|
* Take removes existing elements until empty, then blocks interruptibly |
365 |
|
*/ |
366 |
|
public void testBlockingTake() throws InterruptedException { |
367 |
< |
final LinkedBlockingQueue q = populatedQueue(SIZE); |
368 |
< |
Thread t = new Thread(new CheckedRunnable() { |
367 |
> |
final BlockingQueue q = populatedQueue(SIZE); |
368 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
369 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
370 |
|
public void realRun() throws InterruptedException { |
371 |
|
for (int i = 0; i < SIZE; ++i) { |
372 |
|
assertEquals(i, q.take()); |
373 |
|
} |
374 |
+ |
|
375 |
+ |
Thread.currentThread().interrupt(); |
376 |
|
try { |
377 |
|
q.take(); |
378 |
|
shouldThrow(); |
379 |
|
} catch (InterruptedException success) {} |
380 |
+ |
assertFalse(Thread.interrupted()); |
381 |
+ |
|
382 |
+ |
pleaseInterrupt.countDown(); |
383 |
+ |
try { |
384 |
+ |
q.take(); |
385 |
+ |
shouldThrow(); |
386 |
+ |
} catch (InterruptedException success) {} |
387 |
+ |
assertFalse(Thread.interrupted()); |
388 |
|
}}); |
389 |
|
|
390 |
< |
t.start(); |
391 |
< |
Thread.sleep(SHORT_DELAY_MS); |
390 |
> |
await(pleaseInterrupt); |
391 |
> |
assertThreadStaysAlive(t); |
392 |
|
t.interrupt(); |
393 |
< |
t.join(); |
393 |
> |
awaitTermination(t); |
394 |
|
} |
395 |
|
|
396 |
|
/** |
405 |
|
} |
406 |
|
|
407 |
|
/** |
408 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
408 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
409 |
|
*/ |
410 |
|
public void testTimedPoll0() throws InterruptedException { |
411 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
416 |
|
} |
417 |
|
|
418 |
|
/** |
419 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
419 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
420 |
|
*/ |
421 |
|
public void testTimedPoll() throws InterruptedException { |
422 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
422 |
> |
LinkedBlockingQueue<Integer> q = populatedQueue(SIZE); |
423 |
|
for (int i = 0; i < SIZE; ++i) { |
424 |
< |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
425 |
< |
} |
426 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
424 |
> |
long startTime = System.nanoTime(); |
425 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
426 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
427 |
> |
} |
428 |
> |
long startTime = System.nanoTime(); |
429 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
430 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
431 |
> |
checkEmpty(q); |
432 |
|
} |
433 |
|
|
434 |
|
/** |
436 |
|
* returning timeout status |
437 |
|
*/ |
438 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
439 |
< |
Thread t = new Thread(new CheckedRunnable() { |
439 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
440 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
441 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
442 |
|
public void realRun() throws InterruptedException { |
445 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
443 |
|
for (int i = 0; i < SIZE; ++i) { |
444 |
< |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
444 |
> |
long t0 = System.nanoTime(); |
445 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
446 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
447 |
|
} |
448 |
+ |
long t0 = System.nanoTime(); |
449 |
+ |
aboutToWait.countDown(); |
450 |
|
try { |
451 |
< |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
451 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
452 |
|
shouldThrow(); |
453 |
< |
} catch (InterruptedException success) {} |
453 |
> |
} catch (InterruptedException success) { |
454 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
455 |
> |
} |
456 |
|
}}); |
457 |
|
|
458 |
< |
t.start(); |
459 |
< |
Thread.sleep(SHORT_DELAY_MS); |
458 |
> |
aboutToWait.await(); |
459 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
460 |
|
t.interrupt(); |
461 |
< |
t.join(); |
461 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
462 |
> |
checkEmpty(q); |
463 |
|
} |
464 |
|
|
465 |
|
/** |
506 |
|
} |
507 |
|
|
508 |
|
/** |
505 |
– |
* remove(x) removes x and returns true if present |
506 |
– |
*/ |
507 |
– |
public void testRemoveElement() { |
508 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
509 |
– |
for (int i = 1; i < SIZE; i+=2) { |
510 |
– |
assertTrue(q.remove(new Integer(i))); |
511 |
– |
} |
512 |
– |
for (int i = 0; i < SIZE; i+=2) { |
513 |
– |
assertTrue(q.remove(new Integer(i))); |
514 |
– |
assertFalse(q.remove(new Integer(i+1))); |
515 |
– |
} |
516 |
– |
assertTrue(q.isEmpty()); |
517 |
– |
} |
518 |
– |
|
519 |
– |
/** |
509 |
|
* An add following remove(x) succeeds |
510 |
|
*/ |
511 |
|
public void testRemoveElementAndAdd() throws InterruptedException { |
515 |
|
assertTrue(q.remove(new Integer(1))); |
516 |
|
assertTrue(q.remove(new Integer(2))); |
517 |
|
assertTrue(q.add(new Integer(3))); |
518 |
< |
assertTrue(q.take() != null); |
518 |
> |
assertNotNull(q.take()); |
519 |
|
} |
520 |
|
|
521 |
|
/** |
596 |
|
} |
597 |
|
|
598 |
|
/** |
599 |
< |
* toArray contains all elements |
599 |
> |
* toArray contains all elements in FIFO order |
600 |
|
*/ |
601 |
< |
public void testToArray() throws InterruptedException { |
601 |
> |
public void testToArray() { |
602 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
603 |
|
Object[] o = q.toArray(); |
604 |
|
for (int i = 0; i < o.length; i++) |
605 |
< |
assertEquals(o[i], q.take()); |
605 |
> |
assertSame(o[i], q.poll()); |
606 |
|
} |
607 |
|
|
608 |
|
/** |
609 |
< |
* toArray(a) contains all elements |
609 |
> |
* toArray(a) contains all elements in FIFO order |
610 |
|
*/ |
611 |
|
public void testToArray2() throws InterruptedException { |
612 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
612 |
> |
LinkedBlockingQueue<Integer> q = populatedQueue(SIZE); |
613 |
|
Integer[] ints = new Integer[SIZE]; |
614 |
< |
ints = (Integer[])q.toArray(ints); |
614 |
> |
Integer[] array = q.toArray(ints); |
615 |
> |
assertSame(ints, array); |
616 |
|
for (int i = 0; i < ints.length; i++) |
617 |
< |
assertEquals(ints[i], q.take()); |
628 |
< |
} |
629 |
< |
|
630 |
< |
/** |
631 |
< |
* toArray(null) throws NPE |
632 |
< |
*/ |
633 |
< |
public void testToArray_BadArg() { |
634 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
635 |
< |
try { |
636 |
< |
Object o[] = q.toArray(null); |
637 |
< |
shouldThrow(); |
638 |
< |
} catch (NullPointerException success) {} |
617 |
> |
assertSame(ints[i], q.poll()); |
618 |
|
} |
619 |
|
|
620 |
|
/** |
621 |
< |
* toArray with incompatible array type throws CCE |
621 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
622 |
|
*/ |
623 |
|
public void testToArray1_BadArg() { |
624 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
625 |
|
try { |
626 |
< |
Object o[] = q.toArray(new String[10]); |
626 |
> |
q.toArray(new String[10]); |
627 |
|
shouldThrow(); |
628 |
|
} catch (ArrayStoreException success) {} |
629 |
|
} |
630 |
|
|
652 |
– |
|
631 |
|
/** |
632 |
|
* iterator iterates through all elements |
633 |
|
*/ |
658 |
|
assertFalse(it.hasNext()); |
659 |
|
} |
660 |
|
|
683 |
– |
|
661 |
|
/** |
662 |
|
* iterator ordering is FIFO |
663 |
|
*/ |
689 |
|
assertEquals(0, q.size()); |
690 |
|
} |
691 |
|
|
715 |
– |
|
692 |
|
/** |
693 |
|
* toString contains toStrings of elements |
694 |
|
*/ |
696 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
697 |
|
String s = q.toString(); |
698 |
|
for (int i = 0; i < SIZE; ++i) { |
699 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
699 |
> |
assertTrue(s.contains(String.valueOf(i))); |
700 |
|
} |
701 |
|
} |
702 |
|
|
727 |
– |
|
703 |
|
/** |
704 |
|
* offer transfers elements across Executor tasks |
705 |
|
*/ |
708 |
|
q.add(one); |
709 |
|
q.add(two); |
710 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
711 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
712 |
|
executor.execute(new CheckedRunnable() { |
713 |
|
public void realRun() throws InterruptedException { |
714 |
|
assertFalse(q.offer(three)); |
715 |
< |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
715 |
> |
threadsStarted.await(); |
716 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
717 |
|
assertEquals(0, q.remainingCapacity()); |
718 |
|
}}); |
719 |
|
|
720 |
|
executor.execute(new CheckedRunnable() { |
721 |
|
public void realRun() throws InterruptedException { |
722 |
< |
Thread.sleep(SMALL_DELAY_MS); |
722 |
> |
threadsStarted.await(); |
723 |
|
assertSame(one, q.take()); |
724 |
|
}}); |
725 |
|
|
727 |
|
} |
728 |
|
|
729 |
|
/** |
730 |
< |
* poll retrieves elements across Executor threads |
730 |
> |
* timed poll retrieves elements across Executor threads |
731 |
|
*/ |
732 |
|
public void testPollInExecutor() { |
733 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
734 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
735 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
736 |
|
executor.execute(new CheckedRunnable() { |
737 |
|
public void realRun() throws InterruptedException { |
738 |
|
assertNull(q.poll()); |
739 |
< |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
740 |
< |
assertTrue(q.isEmpty()); |
739 |
> |
threadsStarted.await(); |
740 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
741 |
> |
checkEmpty(q); |
742 |
|
}}); |
743 |
|
|
744 |
|
executor.execute(new CheckedRunnable() { |
745 |
|
public void realRun() throws InterruptedException { |
746 |
< |
Thread.sleep(SMALL_DELAY_MS); |
746 |
> |
threadsStarted.await(); |
747 |
|
q.put(one); |
748 |
|
}}); |
749 |
|
|
754 |
|
* A deserialized serialized queue has same elements in same order |
755 |
|
*/ |
756 |
|
public void testSerialization() throws Exception { |
757 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
758 |
< |
|
780 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
781 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
782 |
< |
out.writeObject(q); |
783 |
< |
out.close(); |
757 |
> |
Queue x = populatedQueue(SIZE); |
758 |
> |
Queue y = serialClone(x); |
759 |
|
|
760 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
761 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
762 |
< |
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
763 |
< |
assertEquals(q.size(), r.size()); |
764 |
< |
while (!q.isEmpty()) |
765 |
< |
assertEquals(q.remove(), r.remove()); |
766 |
< |
} |
767 |
< |
|
768 |
< |
/** |
794 |
< |
* drainTo(null) throws NPE |
795 |
< |
*/ |
796 |
< |
public void testDrainToNull() { |
797 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
798 |
< |
try { |
799 |
< |
q.drainTo(null); |
800 |
< |
shouldThrow(); |
801 |
< |
} catch (NullPointerException success) {} |
802 |
< |
} |
803 |
< |
|
804 |
< |
/** |
805 |
< |
* drainTo(this) throws IAE |
806 |
< |
*/ |
807 |
< |
public void testDrainToSelf() { |
808 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
809 |
< |
try { |
810 |
< |
q.drainTo(q); |
811 |
< |
shouldThrow(); |
812 |
< |
} catch (IllegalArgumentException success) {} |
760 |
> |
assertNotSame(x, y); |
761 |
> |
assertEquals(x.size(), y.size()); |
762 |
> |
assertEquals(x.toString(), y.toString()); |
763 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
764 |
> |
while (!x.isEmpty()) { |
765 |
> |
assertFalse(y.isEmpty()); |
766 |
> |
assertEquals(x.remove(), y.remove()); |
767 |
> |
} |
768 |
> |
assertTrue(y.isEmpty()); |
769 |
|
} |
770 |
|
|
771 |
|
/** |
775 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
776 |
|
ArrayList l = new ArrayList(); |
777 |
|
q.drainTo(l); |
778 |
< |
assertEquals(q.size(), 0); |
779 |
< |
assertEquals(l.size(), SIZE); |
778 |
> |
assertEquals(0, q.size()); |
779 |
> |
assertEquals(SIZE, l.size()); |
780 |
|
for (int i = 0; i < SIZE; ++i) |
781 |
|
assertEquals(l.get(i), new Integer(i)); |
782 |
|
q.add(zero); |
786 |
|
assertTrue(q.contains(one)); |
787 |
|
l.clear(); |
788 |
|
q.drainTo(l); |
789 |
< |
assertEquals(q.size(), 0); |
790 |
< |
assertEquals(l.size(), 2); |
789 |
> |
assertEquals(0, q.size()); |
790 |
> |
assertEquals(2, l.size()); |
791 |
|
for (int i = 0; i < 2; ++i) |
792 |
|
assertEquals(l.get(i), new Integer(i)); |
793 |
|
} |
813 |
|
} |
814 |
|
|
815 |
|
/** |
860 |
– |
* drainTo(null, n) throws NPE |
861 |
– |
*/ |
862 |
– |
public void testDrainToNullN() { |
863 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
864 |
– |
try { |
865 |
– |
q.drainTo(null, 0); |
866 |
– |
shouldThrow(); |
867 |
– |
} catch (NullPointerException success) {} |
868 |
– |
} |
869 |
– |
|
870 |
– |
/** |
871 |
– |
* drainTo(this, n) throws IAE |
872 |
– |
*/ |
873 |
– |
public void testDrainToSelfN() { |
874 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
875 |
– |
try { |
876 |
– |
q.drainTo(q, 0); |
877 |
– |
shouldThrow(); |
878 |
– |
} catch (IllegalArgumentException success) {} |
879 |
– |
} |
880 |
– |
|
881 |
– |
/** |
816 |
|
* drainTo(c, n) empties first min(n, size) elements of queue into c |
817 |
|
*/ |
818 |
|
public void testDrainToN() { |
823 |
|
ArrayList l = new ArrayList(); |
824 |
|
q.drainTo(l, i); |
825 |
|
int k = (i < SIZE) ? i : SIZE; |
826 |
< |
assertEquals(l.size(), k); |
827 |
< |
assertEquals(q.size(), SIZE-k); |
826 |
> |
assertEquals(k, l.size()); |
827 |
> |
assertEquals(SIZE-k, q.size()); |
828 |
|
for (int j = 0; j < k; ++j) |
829 |
|
assertEquals(l.get(j), new Integer(j)); |
830 |
< |
while (q.poll() != null) ; |
830 |
> |
do {} while (q.poll() != null); |
831 |
> |
} |
832 |
> |
} |
833 |
> |
|
834 |
> |
/** |
835 |
> |
* remove(null), contains(null) always return false |
836 |
> |
*/ |
837 |
> |
public void testNeverContainsNull() { |
838 |
> |
Collection<?>[] qs = { |
839 |
> |
new LinkedBlockingQueue<Object>(), |
840 |
> |
populatedQueue(2), |
841 |
> |
}; |
842 |
> |
|
843 |
> |
for (Collection<?> q : qs) { |
844 |
> |
assertFalse(q.contains(null)); |
845 |
> |
assertFalse(q.remove(null)); |
846 |
|
} |
847 |
|
} |
848 |
|
|