9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
|
import java.io.*; |
14 |
|
|
15 |
|
public class LinkedBlockingQueueTest extends JSR166TestCase { |
16 |
|
|
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
20 |
|
|
21 |
|
public static Test suite() { |
22 |
< |
return new TestSuite(LinkedBlockingQueueTest.class); |
22 |
> |
return new TestSuite(LinkedBlockingQueueTest.class); |
23 |
|
} |
24 |
|
|
25 |
|
|
30 |
|
private LinkedBlockingQueue populatedQueue(int n) { |
31 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(n); |
32 |
|
assertTrue(q.isEmpty()); |
33 |
< |
for (int i = 0; i < n; i++) |
34 |
< |
assertTrue(q.offer(new Integer(i))); |
33 |
> |
for (int i = 0; i < n; i++) |
34 |
> |
assertTrue(q.offer(new Integer(i))); |
35 |
|
assertFalse(q.isEmpty()); |
36 |
|
assertEquals(0, q.remainingCapacity()); |
37 |
< |
assertEquals(n, q.size()); |
37 |
> |
assertEquals(n, q.size()); |
38 |
|
return q; |
39 |
|
} |
40 |
|
|
48 |
|
} |
49 |
|
|
50 |
|
/** |
51 |
< |
* Constructor throws IAE if capacity argument nonpositive |
51 |
> |
* Constructor throws IAE if capacity argument nonpositive |
52 |
|
*/ |
53 |
|
public void testConstructor2() { |
54 |
|
try { |
55 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(0); |
56 |
|
shouldThrow(); |
57 |
< |
} |
57 |
< |
catch (IllegalArgumentException success) {} |
57 |
> |
} catch (IllegalArgumentException success) {} |
58 |
|
} |
59 |
|
|
60 |
|
/** |
64 |
|
try { |
65 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(null); |
66 |
|
shouldThrow(); |
67 |
< |
} |
68 |
< |
catch (NullPointerException success) {} |
67 |
> |
} catch (NullPointerException success) {} |
68 |
|
} |
69 |
|
|
70 |
|
/** |
75 |
|
Integer[] ints = new Integer[SIZE]; |
76 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
77 |
|
shouldThrow(); |
78 |
< |
} |
80 |
< |
catch (NullPointerException success) {} |
78 |
> |
} catch (NullPointerException success) {} |
79 |
|
} |
80 |
|
|
81 |
|
/** |
88 |
|
ints[i] = new Integer(i); |
89 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
90 |
|
shouldThrow(); |
91 |
< |
} |
94 |
< |
catch (NullPointerException success) {} |
91 |
> |
} catch (NullPointerException success) {} |
92 |
|
} |
93 |
|
|
94 |
|
/** |
95 |
|
* Queue contains all elements of collection used to initialize |
96 |
|
*/ |
97 |
|
public void testConstructor6() { |
98 |
< |
try { |
99 |
< |
Integer[] ints = new Integer[SIZE]; |
100 |
< |
for (int i = 0; i < SIZE; ++i) |
101 |
< |
ints[i] = new Integer(i); |
102 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
103 |
< |
for (int i = 0; i < SIZE; ++i) |
107 |
< |
assertEquals(ints[i], q.poll()); |
108 |
< |
} |
109 |
< |
finally {} |
98 |
> |
Integer[] ints = new Integer[SIZE]; |
99 |
> |
for (int i = 0; i < SIZE; ++i) |
100 |
> |
ints[i] = new Integer(i); |
101 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
102 |
> |
for (int i = 0; i < SIZE; ++i) |
103 |
> |
assertEquals(ints[i], q.poll()); |
104 |
|
} |
105 |
|
|
106 |
|
/** |
139 |
|
* offer(null) throws NPE |
140 |
|
*/ |
141 |
|
public void testOfferNull() { |
142 |
< |
try { |
142 |
> |
try { |
143 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
144 |
|
q.offer(null); |
145 |
|
shouldThrow(); |
146 |
< |
} catch (NullPointerException success) { } |
146 |
> |
} catch (NullPointerException success) {} |
147 |
|
} |
148 |
|
|
149 |
|
/** |
150 |
|
* add(null) throws NPE |
151 |
|
*/ |
152 |
|
public void testAddNull() { |
153 |
< |
try { |
153 |
> |
try { |
154 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
155 |
|
q.add(null); |
156 |
|
shouldThrow(); |
157 |
< |
} catch (NullPointerException success) { } |
157 |
> |
} catch (NullPointerException success) {} |
158 |
|
} |
159 |
|
|
160 |
|
/** |
170 |
|
* add succeeds if not full; throws ISE if full |
171 |
|
*/ |
172 |
|
public void testAdd() { |
173 |
< |
try { |
173 |
> |
try { |
174 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
175 |
|
for (int i = 0; i < SIZE; ++i) { |
176 |
|
assertTrue(q.add(new Integer(i))); |
177 |
|
} |
178 |
|
assertEquals(0, q.remainingCapacity()); |
179 |
|
q.add(new Integer(SIZE)); |
180 |
< |
} catch (IllegalStateException success){ |
181 |
< |
} |
180 |
> |
shouldThrow(); |
181 |
> |
} catch (IllegalStateException success) {} |
182 |
|
} |
183 |
|
|
184 |
|
/** |
189 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
190 |
|
q.addAll(null); |
191 |
|
shouldThrow(); |
192 |
< |
} |
199 |
< |
catch (NullPointerException success) {} |
192 |
> |
} catch (NullPointerException success) {} |
193 |
|
} |
194 |
|
|
195 |
|
/** |
200 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
201 |
|
q.addAll(q); |
202 |
|
shouldThrow(); |
203 |
< |
} |
211 |
< |
catch (IllegalArgumentException success) {} |
203 |
> |
} catch (IllegalArgumentException success) {} |
204 |
|
} |
205 |
|
|
206 |
|
/** |
212 |
|
Integer[] ints = new Integer[SIZE]; |
213 |
|
q.addAll(Arrays.asList(ints)); |
214 |
|
shouldThrow(); |
215 |
< |
} |
224 |
< |
catch (NullPointerException success) {} |
215 |
> |
} catch (NullPointerException success) {} |
216 |
|
} |
217 |
+ |
|
218 |
|
/** |
219 |
|
* addAll of a collection with any null elements throws NPE after |
220 |
|
* possibly adding some elements |
227 |
|
ints[i] = new Integer(i); |
228 |
|
q.addAll(Arrays.asList(ints)); |
229 |
|
shouldThrow(); |
230 |
< |
} |
239 |
< |
catch (NullPointerException success) {} |
230 |
> |
} catch (NullPointerException success) {} |
231 |
|
} |
232 |
+ |
|
233 |
|
/** |
234 |
|
* addAll throws ISE if not enough room |
235 |
|
*/ |
241 |
|
ints[i] = new Integer(i); |
242 |
|
q.addAll(Arrays.asList(ints)); |
243 |
|
shouldThrow(); |
244 |
< |
} |
253 |
< |
catch (IllegalStateException success) {} |
244 |
> |
} catch (IllegalStateException success) {} |
245 |
|
} |
246 |
+ |
|
247 |
|
/** |
248 |
|
* Queue contains all elements, in traversal order, of successful addAll |
249 |
|
*/ |
250 |
|
public void testAddAll5() { |
251 |
< |
try { |
252 |
< |
Integer[] empty = new Integer[0]; |
253 |
< |
Integer[] ints = new Integer[SIZE]; |
254 |
< |
for (int i = 0; i < SIZE; ++i) |
255 |
< |
ints[i] = new Integer(i); |
256 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
257 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
258 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
259 |
< |
for (int i = 0; i < SIZE; ++i) |
268 |
< |
assertEquals(ints[i], q.poll()); |
269 |
< |
} |
270 |
< |
finally {} |
251 |
> |
Integer[] empty = new Integer[0]; |
252 |
> |
Integer[] ints = new Integer[SIZE]; |
253 |
> |
for (int i = 0; i < SIZE; ++i) |
254 |
> |
ints[i] = new Integer(i); |
255 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
256 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
257 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
258 |
> |
for (int i = 0; i < SIZE; ++i) |
259 |
> |
assertEquals(ints[i], q.poll()); |
260 |
|
} |
261 |
|
|
262 |
|
/** |
263 |
|
* put(null) throws NPE |
264 |
|
*/ |
265 |
< |
public void testPutNull() { |
266 |
< |
try { |
265 |
> |
public void testPutNull() throws InterruptedException { |
266 |
> |
try { |
267 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
268 |
|
q.put(null); |
269 |
|
shouldThrow(); |
270 |
< |
} |
282 |
< |
catch (NullPointerException success){ |
283 |
< |
} |
284 |
< |
catch (InterruptedException ie) { |
285 |
< |
unexpectedException(); |
286 |
< |
} |
270 |
> |
} catch (NullPointerException success) {} |
271 |
|
} |
272 |
|
|
273 |
|
/** |
274 |
|
* all elements successfully put are contained |
275 |
|
*/ |
276 |
< |
public void testPut() { |
277 |
< |
try { |
278 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
279 |
< |
for (int i = 0; i < SIZE; ++i) { |
280 |
< |
Integer I = new Integer(i); |
281 |
< |
q.put(I); |
298 |
< |
assertTrue(q.contains(I)); |
299 |
< |
} |
300 |
< |
assertEquals(0, q.remainingCapacity()); |
301 |
< |
} |
302 |
< |
catch (InterruptedException ie) { |
303 |
< |
unexpectedException(); |
276 |
> |
public void testPut() throws InterruptedException { |
277 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
278 |
> |
for (int i = 0; i < SIZE; ++i) { |
279 |
> |
Integer I = new Integer(i); |
280 |
> |
q.put(I); |
281 |
> |
assertTrue(q.contains(I)); |
282 |
|
} |
283 |
+ |
assertEquals(0, q.remainingCapacity()); |
284 |
|
} |
285 |
|
|
286 |
|
/** |
287 |
|
* put blocks interruptibly if full |
288 |
|
*/ |
289 |
< |
public void testBlockingPut() { |
290 |
< |
Thread t = new Thread(new Runnable() { |
291 |
< |
public void run() { |
292 |
< |
int added = 0; |
293 |
< |
try { |
294 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
295 |
< |
for (int i = 0; i < SIZE; ++i) { |
296 |
< |
q.put(new Integer(i)); |
297 |
< |
++added; |
298 |
< |
} |
299 |
< |
q.put(new Integer(SIZE)); |
300 |
< |
threadShouldThrow(); |
301 |
< |
} catch (InterruptedException ie){ |
302 |
< |
threadAssertEquals(added, SIZE); |
324 |
< |
} |
325 |
< |
}}); |
289 |
> |
public void testBlockingPut() throws InterruptedException { |
290 |
> |
final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
291 |
> |
Thread t = new Thread(new CheckedRunnable() { |
292 |
> |
public void realRun() throws InterruptedException { |
293 |
> |
for (int i = 0; i < SIZE; ++i) |
294 |
> |
q.put(i); |
295 |
> |
assertEquals(SIZE, q.size()); |
296 |
> |
assertEquals(0, q.remainingCapacity()); |
297 |
> |
try { |
298 |
> |
q.put(99); |
299 |
> |
shouldThrow(); |
300 |
> |
} catch (InterruptedException success) {} |
301 |
> |
}}); |
302 |
> |
|
303 |
|
t.start(); |
304 |
< |
try { |
305 |
< |
Thread.sleep(SHORT_DELAY_MS); |
306 |
< |
t.interrupt(); |
307 |
< |
t.join(); |
308 |
< |
} |
332 |
< |
catch (InterruptedException ie) { |
333 |
< |
unexpectedException(); |
334 |
< |
} |
304 |
> |
Thread.sleep(SHORT_DELAY_MS); |
305 |
> |
t.interrupt(); |
306 |
> |
t.join(); |
307 |
> |
assertEquals(SIZE, q.size()); |
308 |
> |
assertEquals(0, q.remainingCapacity()); |
309 |
|
} |
310 |
|
|
311 |
|
/** |
312 |
|
* put blocks waiting for take when full |
313 |
|
*/ |
314 |
< |
public void testPutWithTake() { |
314 |
> |
public void testPutWithTake() throws InterruptedException { |
315 |
> |
final int capacity = 2; |
316 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
317 |
< |
Thread t = new Thread(new Runnable() { |
318 |
< |
public void run() { |
319 |
< |
int added = 0; |
320 |
< |
try { |
321 |
< |
q.put(new Object()); |
322 |
< |
++added; |
323 |
< |
q.put(new Object()); |
324 |
< |
++added; |
325 |
< |
q.put(new Object()); |
326 |
< |
++added; |
327 |
< |
q.put(new Object()); |
328 |
< |
++added; |
329 |
< |
threadShouldThrow(); |
330 |
< |
} catch (InterruptedException e){ |
331 |
< |
threadAssertTrue(added >= 2); |
332 |
< |
} |
333 |
< |
} |
334 |
< |
}); |
360 |
< |
try { |
361 |
< |
t.start(); |
362 |
< |
Thread.sleep(SHORT_DELAY_MS); |
363 |
< |
q.take(); |
364 |
< |
t.interrupt(); |
365 |
< |
t.join(); |
366 |
< |
} catch (Exception e){ |
367 |
< |
unexpectedException(); |
368 |
< |
} |
317 |
> |
Thread t = new Thread(new CheckedRunnable() { |
318 |
> |
public void realRun() throws InterruptedException { |
319 |
> |
for (int i = 0; i < capacity + 1; i++) |
320 |
> |
q.put(i); |
321 |
> |
try { |
322 |
> |
q.put(99); |
323 |
> |
shouldThrow(); |
324 |
> |
} catch (InterruptedException success) {} |
325 |
> |
}}); |
326 |
> |
|
327 |
> |
t.start(); |
328 |
> |
Thread.sleep(SHORT_DELAY_MS); |
329 |
> |
assertEquals(q.remainingCapacity(), 0); |
330 |
> |
assertEquals(0, q.take()); |
331 |
> |
Thread.sleep(SHORT_DELAY_MS); |
332 |
> |
t.interrupt(); |
333 |
> |
t.join(); |
334 |
> |
assertEquals(q.remainingCapacity(), 0); |
335 |
|
} |
336 |
|
|
337 |
|
/** |
338 |
|
* timed offer times out if full and elements not taken |
339 |
|
*/ |
340 |
< |
public void testTimedOffer() { |
340 |
> |
public void testTimedOffer() throws InterruptedException { |
341 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
342 |
< |
Thread t = new Thread(new Runnable() { |
343 |
< |
public void run() { |
344 |
< |
try { |
345 |
< |
q.put(new Object()); |
346 |
< |
q.put(new Object()); |
347 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
348 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
349 |
< |
threadShouldThrow(); |
350 |
< |
} catch (InterruptedException success){} |
351 |
< |
} |
386 |
< |
}); |
342 |
> |
Thread t = new Thread(new CheckedRunnable() { |
343 |
> |
public void realRun() throws InterruptedException { |
344 |
> |
q.put(new Object()); |
345 |
> |
q.put(new Object()); |
346 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
347 |
> |
try { |
348 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
349 |
> |
shouldThrow(); |
350 |
> |
} catch (InterruptedException success) {} |
351 |
> |
}}); |
352 |
|
|
353 |
< |
try { |
354 |
< |
t.start(); |
355 |
< |
Thread.sleep(SMALL_DELAY_MS); |
356 |
< |
t.interrupt(); |
392 |
< |
t.join(); |
393 |
< |
} catch (Exception e){ |
394 |
< |
unexpectedException(); |
395 |
< |
} |
353 |
> |
t.start(); |
354 |
> |
Thread.sleep(SMALL_DELAY_MS); |
355 |
> |
t.interrupt(); |
356 |
> |
t.join(); |
357 |
|
} |
358 |
|
|
359 |
|
/** |
360 |
|
* take retrieves elements in FIFO order |
361 |
|
*/ |
362 |
< |
public void testTake() { |
363 |
< |
try { |
364 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
365 |
< |
for (int i = 0; i < SIZE; ++i) { |
366 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
406 |
< |
} |
407 |
< |
} catch (InterruptedException e){ |
408 |
< |
unexpectedException(); |
409 |
< |
} |
362 |
> |
public void testTake() throws InterruptedException { |
363 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
364 |
> |
for (int i = 0; i < SIZE; ++i) { |
365 |
> |
assertEquals(i, q.take()); |
366 |
> |
} |
367 |
|
} |
368 |
|
|
369 |
|
/** |
370 |
|
* take blocks interruptibly when empty |
371 |
|
*/ |
372 |
< |
public void testTakeFromEmpty() { |
372 |
> |
public void testTakeFromEmpty() throws InterruptedException { |
373 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
374 |
< |
Thread t = new Thread(new Runnable() { |
375 |
< |
public void run() { |
376 |
< |
try { |
377 |
< |
q.take(); |
378 |
< |
threadShouldThrow(); |
379 |
< |
} catch (InterruptedException success){ } |
380 |
< |
} |
381 |
< |
}); |
382 |
< |
try { |
426 |
< |
t.start(); |
427 |
< |
Thread.sleep(SHORT_DELAY_MS); |
428 |
< |
t.interrupt(); |
429 |
< |
t.join(); |
430 |
< |
} catch (Exception e){ |
431 |
< |
unexpectedException(); |
432 |
< |
} |
374 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
375 |
> |
public void realRun() throws InterruptedException { |
376 |
> |
q.take(); |
377 |
> |
}}; |
378 |
> |
|
379 |
> |
t.start(); |
380 |
> |
Thread.sleep(SHORT_DELAY_MS); |
381 |
> |
t.interrupt(); |
382 |
> |
t.join(); |
383 |
|
} |
384 |
|
|
385 |
|
/** |
386 |
|
* Take removes existing elements until empty, then blocks interruptibly |
387 |
|
*/ |
388 |
< |
public void testBlockingTake() { |
389 |
< |
Thread t = new Thread(new Runnable() { |
390 |
< |
public void run() { |
391 |
< |
try { |
392 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
393 |
< |
for (int i = 0; i < SIZE; ++i) { |
394 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
395 |
< |
} |
396 |
< |
q.take(); |
397 |
< |
threadShouldThrow(); |
398 |
< |
} catch (InterruptedException success){ |
399 |
< |
} |
400 |
< |
}}); |
388 |
> |
public void testBlockingTake() throws InterruptedException { |
389 |
> |
final LinkedBlockingQueue q = populatedQueue(SIZE); |
390 |
> |
Thread t = new Thread(new CheckedRunnable() { |
391 |
> |
public void realRun() throws InterruptedException { |
392 |
> |
for (int i = 0; i < SIZE; ++i) { |
393 |
> |
assertEquals(i, q.take()); |
394 |
> |
} |
395 |
> |
try { |
396 |
> |
q.take(); |
397 |
> |
shouldThrow(); |
398 |
> |
} catch (InterruptedException success) {} |
399 |
> |
}}); |
400 |
> |
|
401 |
|
t.start(); |
402 |
< |
try { |
403 |
< |
Thread.sleep(SHORT_DELAY_MS); |
404 |
< |
t.interrupt(); |
455 |
< |
t.join(); |
456 |
< |
} |
457 |
< |
catch (InterruptedException ie) { |
458 |
< |
unexpectedException(); |
459 |
< |
} |
402 |
> |
Thread.sleep(SHORT_DELAY_MS); |
403 |
> |
t.interrupt(); |
404 |
> |
t.join(); |
405 |
|
} |
406 |
|
|
462 |
– |
|
407 |
|
/** |
408 |
|
* poll succeeds unless empty |
409 |
|
*/ |
410 |
|
public void testPoll() { |
411 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
412 |
|
for (int i = 0; i < SIZE; ++i) { |
413 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
413 |
> |
assertEquals(i, q.poll()); |
414 |
|
} |
415 |
< |
assertNull(q.poll()); |
415 |
> |
assertNull(q.poll()); |
416 |
|
} |
417 |
|
|
418 |
|
/** |
419 |
|
* timed pool with zero timeout succeeds when non-empty, else times out |
420 |
|
*/ |
421 |
< |
public void testTimedPoll0() { |
422 |
< |
try { |
423 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
424 |
< |
for (int i = 0; i < SIZE; ++i) { |
425 |
< |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
426 |
< |
} |
483 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
484 |
< |
} catch (InterruptedException e){ |
485 |
< |
unexpectedException(); |
486 |
< |
} |
421 |
> |
public void testTimedPoll0() throws InterruptedException { |
422 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
423 |
> |
for (int i = 0; i < SIZE; ++i) { |
424 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
425 |
> |
} |
426 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
427 |
|
} |
428 |
|
|
429 |
|
/** |
430 |
|
* timed pool with nonzero timeout succeeds when non-empty, else times out |
431 |
|
*/ |
432 |
< |
public void testTimedPoll() { |
433 |
< |
try { |
434 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
435 |
< |
for (int i = 0; i < SIZE; ++i) { |
436 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
437 |
< |
} |
498 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
499 |
< |
} catch (InterruptedException e){ |
500 |
< |
unexpectedException(); |
501 |
< |
} |
432 |
> |
public void testTimedPoll() throws InterruptedException { |
433 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
434 |
> |
for (int i = 0; i < SIZE; ++i) { |
435 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
436 |
> |
} |
437 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
438 |
|
} |
439 |
|
|
440 |
|
/** |
441 |
|
* Interrupted timed poll throws InterruptedException instead of |
442 |
|
* returning timeout status |
443 |
|
*/ |
444 |
< |
public void testInterruptedTimedPoll() { |
445 |
< |
Thread t = new Thread(new Runnable() { |
446 |
< |
public void run() { |
447 |
< |
try { |
448 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
449 |
< |
for (int i = 0; i < SIZE; ++i) { |
450 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
451 |
< |
} |
452 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
453 |
< |
} catch (InterruptedException success){ |
454 |
< |
} |
455 |
< |
}}); |
444 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
445 |
> |
Thread t = new Thread(new CheckedRunnable() { |
446 |
> |
public void realRun() throws InterruptedException { |
447 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
448 |
> |
for (int i = 0; i < SIZE; ++i) { |
449 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
450 |
> |
} |
451 |
> |
try { |
452 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
453 |
> |
shouldThrow(); |
454 |
> |
} catch (InterruptedException success) {} |
455 |
> |
}}); |
456 |
> |
|
457 |
|
t.start(); |
458 |
< |
try { |
459 |
< |
Thread.sleep(SHORT_DELAY_MS); |
460 |
< |
t.interrupt(); |
524 |
< |
t.join(); |
525 |
< |
} |
526 |
< |
catch (InterruptedException ie) { |
527 |
< |
unexpectedException(); |
528 |
< |
} |
458 |
> |
Thread.sleep(SHORT_DELAY_MS); |
459 |
> |
t.interrupt(); |
460 |
> |
t.join(); |
461 |
|
} |
462 |
|
|
463 |
|
/** |
464 |
|
* timed poll before a delayed offer fails; after offer succeeds; |
465 |
|
* on interruption throws |
466 |
|
*/ |
467 |
< |
public void testTimedPollWithOffer() { |
467 |
> |
public void testTimedPollWithOffer() throws InterruptedException { |
468 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
469 |
< |
Thread t = new Thread(new Runnable() { |
470 |
< |
public void run() { |
471 |
< |
try { |
472 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
473 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
474 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
475 |
< |
threadShouldThrow(); |
476 |
< |
} catch (InterruptedException success) { } |
477 |
< |
} |
478 |
< |
}); |
479 |
< |
try { |
480 |
< |
t.start(); |
481 |
< |
Thread.sleep(SMALL_DELAY_MS); |
482 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
483 |
< |
t.interrupt(); |
552 |
< |
t.join(); |
553 |
< |
} catch (Exception e){ |
554 |
< |
unexpectedException(); |
555 |
< |
} |
469 |
> |
Thread t = new Thread(new CheckedRunnable() { |
470 |
> |
public void realRun() throws InterruptedException { |
471 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
472 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
473 |
> |
try { |
474 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
475 |
> |
shouldThrow(); |
476 |
> |
} catch (InterruptedException success) {} |
477 |
> |
}}); |
478 |
> |
|
479 |
> |
t.start(); |
480 |
> |
Thread.sleep(SMALL_DELAY_MS); |
481 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
482 |
> |
t.interrupt(); |
483 |
> |
t.join(); |
484 |
|
} |
485 |
|
|
486 |
|
/** |
489 |
|
public void testPeek() { |
490 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
491 |
|
for (int i = 0; i < SIZE; ++i) { |
492 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
493 |
< |
q.poll(); |
492 |
> |
assertEquals(i, q.peek()); |
493 |
> |
assertEquals(i, q.poll()); |
494 |
|
assertTrue(q.peek() == null || |
495 |
< |
i != ((Integer)q.peek()).intValue()); |
495 |
> |
!q.peek().equals(i)); |
496 |
|
} |
497 |
< |
assertNull(q.peek()); |
497 |
> |
assertNull(q.peek()); |
498 |
|
} |
499 |
|
|
500 |
|
/** |
503 |
|
public void testElement() { |
504 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
505 |
|
for (int i = 0; i < SIZE; ++i) { |
506 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
507 |
< |
q.poll(); |
506 |
> |
assertEquals(i, q.element()); |
507 |
> |
assertEquals(i, q.poll()); |
508 |
|
} |
509 |
|
try { |
510 |
|
q.element(); |
511 |
|
shouldThrow(); |
512 |
< |
} |
585 |
< |
catch (NoSuchElementException success) {} |
512 |
> |
} catch (NoSuchElementException success) {} |
513 |
|
} |
514 |
|
|
515 |
|
/** |
518 |
|
public void testRemove() { |
519 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
520 |
|
for (int i = 0; i < SIZE; ++i) { |
521 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
521 |
> |
assertEquals(i, q.remove()); |
522 |
|
} |
523 |
|
try { |
524 |
|
q.remove(); |
525 |
|
shouldThrow(); |
526 |
< |
} catch (NoSuchElementException success){ |
600 |
< |
} |
526 |
> |
} catch (NoSuchElementException success) {} |
527 |
|
} |
528 |
|
|
529 |
|
/** |
544 |
|
/** |
545 |
|
* An add following remove(x) succeeds |
546 |
|
*/ |
547 |
< |
public void testRemoveElementAndAdd() { |
548 |
< |
try { |
549 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
550 |
< |
assertTrue(q.add(new Integer(1))); |
551 |
< |
assertTrue(q.add(new Integer(2))); |
552 |
< |
assertTrue(q.remove(new Integer(1))); |
553 |
< |
assertTrue(q.remove(new Integer(2))); |
554 |
< |
assertTrue(q.add(new Integer(3))); |
629 |
< |
assertTrue(q.take() != null); |
630 |
< |
} catch (Exception e){ |
631 |
< |
unexpectedException(); |
632 |
< |
} |
547 |
> |
public void testRemoveElementAndAdd() throws InterruptedException { |
548 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
549 |
> |
assertTrue(q.add(new Integer(1))); |
550 |
> |
assertTrue(q.add(new Integer(2))); |
551 |
> |
assertTrue(q.remove(new Integer(1))); |
552 |
> |
assertTrue(q.remove(new Integer(2))); |
553 |
> |
assertTrue(q.add(new Integer(3))); |
554 |
> |
assertTrue(q.take() != null); |
555 |
|
} |
556 |
|
|
557 |
|
/** |
634 |
|
/** |
635 |
|
* toArray contains all elements |
636 |
|
*/ |
637 |
< |
public void testToArray() { |
637 |
> |
public void testToArray() throws InterruptedException { |
638 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
639 |
< |
Object[] o = q.toArray(); |
640 |
< |
try { |
641 |
< |
for (int i = 0; i < o.length; i++) |
720 |
< |
assertEquals(o[i], q.take()); |
721 |
< |
} catch (InterruptedException e){ |
722 |
< |
unexpectedException(); |
723 |
< |
} |
639 |
> |
Object[] o = q.toArray(); |
640 |
> |
for (int i = 0; i < o.length; i++) |
641 |
> |
assertEquals(o[i], q.take()); |
642 |
|
} |
643 |
|
|
644 |
|
/** |
645 |
|
* toArray(a) contains all elements |
646 |
|
*/ |
647 |
< |
public void testToArray2() { |
647 |
> |
public void testToArray2() throws InterruptedException { |
648 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
649 |
< |
Integer[] ints = new Integer[SIZE]; |
650 |
< |
ints = (Integer[])q.toArray(ints); |
651 |
< |
try { |
652 |
< |
for (int i = 0; i < ints.length; i++) |
735 |
< |
assertEquals(ints[i], q.take()); |
736 |
< |
} catch (InterruptedException e){ |
737 |
< |
unexpectedException(); |
738 |
< |
} |
649 |
> |
Integer[] ints = new Integer[SIZE]; |
650 |
> |
ints = (Integer[])q.toArray(ints); |
651 |
> |
for (int i = 0; i < ints.length; i++) |
652 |
> |
assertEquals(ints[i], q.take()); |
653 |
|
} |
654 |
|
|
655 |
|
/** |
656 |
|
* toArray(null) throws NPE |
657 |
|
*/ |
658 |
|
public void testToArray_BadArg() { |
659 |
< |
try { |
660 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
661 |
< |
Object o[] = q.toArray(null); |
662 |
< |
shouldThrow(); |
663 |
< |
} catch (NullPointerException success){} |
659 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
660 |
> |
try { |
661 |
> |
Object o[] = q.toArray(null); |
662 |
> |
shouldThrow(); |
663 |
> |
} catch (NullPointerException success) {} |
664 |
|
} |
665 |
|
|
666 |
|
/** |
667 |
|
* toArray with incompatible array type throws CCE |
668 |
|
*/ |
669 |
|
public void testToArray1_BadArg() { |
670 |
< |
try { |
671 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
672 |
< |
Object o[] = q.toArray(new String[10] ); |
673 |
< |
shouldThrow(); |
674 |
< |
} catch (ArrayStoreException success){} |
670 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
671 |
> |
try { |
672 |
> |
Object o[] = q.toArray(new String[10]); |
673 |
> |
shouldThrow(); |
674 |
> |
} catch (ArrayStoreException success) {} |
675 |
|
} |
676 |
|
|
677 |
|
|
678 |
|
/** |
679 |
|
* iterator iterates through all elements |
680 |
|
*/ |
681 |
< |
public void testIterator() { |
681 |
> |
public void testIterator() throws InterruptedException { |
682 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
683 |
< |
Iterator it = q.iterator(); |
684 |
< |
try { |
685 |
< |
while (it.hasNext()){ |
686 |
< |
assertEquals(it.next(), q.take()); |
773 |
< |
} |
774 |
< |
} catch (InterruptedException e){ |
775 |
< |
unexpectedException(); |
776 |
< |
} |
683 |
> |
Iterator it = q.iterator(); |
684 |
> |
while (it.hasNext()) { |
685 |
> |
assertEquals(it.next(), q.take()); |
686 |
> |
} |
687 |
|
} |
688 |
|
|
689 |
|
/** |
690 |
|
* iterator.remove removes current element |
691 |
|
*/ |
692 |
< |
public void testIteratorRemove () { |
692 |
> |
public void testIteratorRemove() { |
693 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
694 |
|
q.add(two); |
695 |
|
q.add(one); |
700 |
|
it.remove(); |
701 |
|
|
702 |
|
it = q.iterator(); |
703 |
< |
assertEquals(it.next(), one); |
704 |
< |
assertEquals(it.next(), three); |
703 |
> |
assertSame(it.next(), one); |
704 |
> |
assertSame(it.next(), three); |
705 |
|
assertFalse(it.hasNext()); |
706 |
|
} |
707 |
|
|
717 |
|
assertEquals(0, q.remainingCapacity()); |
718 |
|
int k = 0; |
719 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
720 |
< |
int i = ((Integer)(it.next())).intValue(); |
811 |
< |
assertEquals(++k, i); |
720 |
> |
assertEquals(++k, it.next()); |
721 |
|
} |
722 |
|
assertEquals(3, k); |
723 |
|
} |
725 |
|
/** |
726 |
|
* Modifications do not cause iterators to fail |
727 |
|
*/ |
728 |
< |
public void testWeaklyConsistentIteration () { |
728 |
> |
public void testWeaklyConsistentIteration() { |
729 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
730 |
|
q.add(one); |
731 |
|
q.add(two); |
732 |
|
q.add(three); |
733 |
< |
try { |
734 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
735 |
< |
q.remove(); |
827 |
< |
it.next(); |
828 |
< |
} |
829 |
< |
} |
830 |
< |
catch (ConcurrentModificationException e) { |
831 |
< |
unexpectedException(); |
733 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
734 |
> |
q.remove(); |
735 |
> |
it.next(); |
736 |
|
} |
737 |
|
assertEquals(0, q.size()); |
738 |
|
} |
758 |
|
q.add(one); |
759 |
|
q.add(two); |
760 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
761 |
< |
executor.execute(new Runnable() { |
762 |
< |
public void run() { |
763 |
< |
threadAssertFalse(q.offer(three)); |
764 |
< |
try { |
765 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
766 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
767 |
< |
} |
768 |
< |
catch (InterruptedException e) { |
769 |
< |
threadUnexpectedException(); |
770 |
< |
} |
771 |
< |
} |
772 |
< |
}); |
869 |
< |
|
870 |
< |
executor.execute(new Runnable() { |
871 |
< |
public void run() { |
872 |
< |
try { |
873 |
< |
Thread.sleep(SMALL_DELAY_MS); |
874 |
< |
threadAssertEquals(one, q.take()); |
875 |
< |
} |
876 |
< |
catch (InterruptedException e) { |
877 |
< |
threadUnexpectedException(); |
878 |
< |
} |
879 |
< |
} |
880 |
< |
}); |
761 |
> |
executor.execute(new CheckedRunnable() { |
762 |
> |
public void realRun() throws InterruptedException { |
763 |
> |
assertFalse(q.offer(three)); |
764 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
765 |
> |
assertEquals(0, q.remainingCapacity()); |
766 |
> |
}}); |
767 |
> |
|
768 |
> |
executor.execute(new CheckedRunnable() { |
769 |
> |
public void realRun() throws InterruptedException { |
770 |
> |
Thread.sleep(SMALL_DELAY_MS); |
771 |
> |
assertSame(one, q.take()); |
772 |
> |
}}); |
773 |
|
|
774 |
|
joinPool(executor); |
775 |
|
} |
780 |
|
public void testPollInExecutor() { |
781 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
782 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
783 |
< |
executor.execute(new Runnable() { |
784 |
< |
public void run() { |
785 |
< |
threadAssertNull(q.poll()); |
786 |
< |
try { |
787 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
788 |
< |
threadAssertTrue(q.isEmpty()); |
789 |
< |
} |
790 |
< |
catch (InterruptedException e) { |
791 |
< |
threadUnexpectedException(); |
792 |
< |
} |
793 |
< |
} |
794 |
< |
}); |
903 |
< |
|
904 |
< |
executor.execute(new Runnable() { |
905 |
< |
public void run() { |
906 |
< |
try { |
907 |
< |
Thread.sleep(SMALL_DELAY_MS); |
908 |
< |
q.put(one); |
909 |
< |
} |
910 |
< |
catch (InterruptedException e) { |
911 |
< |
threadUnexpectedException(); |
912 |
< |
} |
913 |
< |
} |
914 |
< |
}); |
783 |
> |
executor.execute(new CheckedRunnable() { |
784 |
> |
public void realRun() throws InterruptedException { |
785 |
> |
assertNull(q.poll()); |
786 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
787 |
> |
assertTrue(q.isEmpty()); |
788 |
> |
}}); |
789 |
> |
|
790 |
> |
executor.execute(new CheckedRunnable() { |
791 |
> |
public void realRun() throws InterruptedException { |
792 |
> |
Thread.sleep(SMALL_DELAY_MS); |
793 |
> |
q.put(one); |
794 |
> |
}}); |
795 |
|
|
796 |
|
joinPool(executor); |
797 |
|
} |
799 |
|
/** |
800 |
|
* A deserialized serialized queue has same elements in same order |
801 |
|
*/ |
802 |
< |
public void testSerialization() { |
802 |
> |
public void testSerialization() throws Exception { |
803 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
804 |
|
|
805 |
< |
try { |
806 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
807 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
808 |
< |
out.writeObject(q); |
809 |
< |
out.close(); |
810 |
< |
|
811 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
812 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
813 |
< |
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
814 |
< |
assertEquals(q.size(), r.size()); |
815 |
< |
while (!q.isEmpty()) |
936 |
< |
assertEquals(q.remove(), r.remove()); |
937 |
< |
} catch (Exception e){ |
938 |
< |
unexpectedException(); |
939 |
< |
} |
805 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
806 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
807 |
> |
out.writeObject(q); |
808 |
> |
out.close(); |
809 |
> |
|
810 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
811 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
812 |
> |
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
813 |
> |
assertEquals(q.size(), r.size()); |
814 |
> |
while (!q.isEmpty()) |
815 |
> |
assertEquals(q.remove(), r.remove()); |
816 |
|
} |
817 |
|
|
818 |
|
/** |
823 |
|
try { |
824 |
|
q.drainTo(null); |
825 |
|
shouldThrow(); |
826 |
< |
} catch (NullPointerException success) { |
951 |
< |
} |
826 |
> |
} catch (NullPointerException success) {} |
827 |
|
} |
828 |
|
|
829 |
|
/** |
834 |
|
try { |
835 |
|
q.drainTo(q); |
836 |
|
shouldThrow(); |
837 |
< |
} catch (IllegalArgumentException success) { |
963 |
< |
} |
837 |
> |
} catch (IllegalArgumentException success) {} |
838 |
|
} |
839 |
|
|
840 |
|
/** |
864 |
|
/** |
865 |
|
* drainTo empties full queue, unblocking a waiting put. |
866 |
|
*/ |
867 |
< |
public void testDrainToWithActivePut() { |
867 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
868 |
|
final LinkedBlockingQueue q = populatedQueue(SIZE); |
869 |
< |
Thread t = new Thread(new Runnable() { |
870 |
< |
public void run() { |
871 |
< |
try { |
872 |
< |
q.put(new Integer(SIZE+1)); |
873 |
< |
} catch (InterruptedException ie){ |
874 |
< |
threadUnexpectedException(); |
875 |
< |
} |
876 |
< |
} |
877 |
< |
}); |
878 |
< |
try { |
879 |
< |
t.start(); |
880 |
< |
ArrayList l = new ArrayList(); |
881 |
< |
q.drainTo(l); |
1008 |
< |
assertTrue(l.size() >= SIZE); |
1009 |
< |
for (int i = 0; i < SIZE; ++i) |
1010 |
< |
assertEquals(l.get(i), new Integer(i)); |
1011 |
< |
t.join(); |
1012 |
< |
assertTrue(q.size() + l.size() >= SIZE); |
1013 |
< |
} catch (Exception e){ |
1014 |
< |
unexpectedException(); |
1015 |
< |
} |
869 |
> |
Thread t = new Thread(new CheckedRunnable() { |
870 |
> |
public void realRun() throws InterruptedException { |
871 |
> |
q.put(new Integer(SIZE+1)); |
872 |
> |
}}); |
873 |
> |
|
874 |
> |
t.start(); |
875 |
> |
ArrayList l = new ArrayList(); |
876 |
> |
q.drainTo(l); |
877 |
> |
assertTrue(l.size() >= SIZE); |
878 |
> |
for (int i = 0; i < SIZE; ++i) |
879 |
> |
assertEquals(l.get(i), new Integer(i)); |
880 |
> |
t.join(); |
881 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
882 |
|
} |
883 |
|
|
884 |
|
/** |
889 |
|
try { |
890 |
|
q.drainTo(null, 0); |
891 |
|
shouldThrow(); |
892 |
< |
} catch (NullPointerException success) { |
1027 |
< |
} |
892 |
> |
} catch (NullPointerException success) {} |
893 |
|
} |
894 |
|
|
895 |
|
/** |
900 |
|
try { |
901 |
|
q.drainTo(q, 0); |
902 |
|
shouldThrow(); |
903 |
< |
} catch (IllegalArgumentException success) { |
1039 |
< |
} |
903 |
> |
} catch (IllegalArgumentException success) {} |
904 |
|
} |
905 |
|
|
906 |
|
/** |