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 |
– |
|
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 ArrayBlockingQueueTest extends JSR166TestCase { |
16 |
+ |
|
17 |
+ |
public static class Fair extends BlockingQueueTest { |
18 |
+ |
protected BlockingQueue emptyCollection() { |
19 |
+ |
return new ArrayBlockingQueue(20, true); |
20 |
+ |
} |
21 |
+ |
} |
22 |
+ |
|
23 |
+ |
public static class NonFair extends BlockingQueueTest { |
24 |
+ |
protected BlockingQueue emptyCollection() { |
25 |
+ |
return new ArrayBlockingQueue(20, false); |
26 |
+ |
} |
27 |
+ |
} |
28 |
+ |
|
29 |
|
public static void main(String[] args) { |
30 |
< |
junit.textui.TestRunner.run (suite()); |
30 |
> |
junit.textui.TestRunner.run(suite()); |
31 |
|
} |
32 |
+ |
|
33 |
|
public static Test suite() { |
34 |
< |
return new TestSuite(ArrayBlockingQueueTest.class); |
34 |
> |
return newTestSuite(ArrayBlockingQueueTest.class, |
35 |
> |
new Fair().testSuite(), |
36 |
> |
new NonFair().testSuite()); |
37 |
|
} |
38 |
|
|
39 |
|
/** |
40 |
|
* Create a queue of given size containing consecutive |
41 |
|
* Integers 0 ... n. |
42 |
|
*/ |
43 |
< |
private ArrayBlockingQueue populatedQueue(int n) { |
44 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(n); |
43 |
> |
private ArrayBlockingQueue<Integer> populatedQueue(int n) { |
44 |
> |
ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n); |
45 |
|
assertTrue(q.isEmpty()); |
46 |
< |
for (int i = 0; i < n; i++) |
47 |
< |
assertTrue(q.offer(new Integer(i))); |
46 |
> |
for (int i = 0; i < n; i++) |
47 |
> |
assertTrue(q.offer(new Integer(i))); |
48 |
|
assertFalse(q.isEmpty()); |
49 |
|
assertEquals(0, q.remainingCapacity()); |
50 |
< |
assertEquals(n, q.size()); |
50 |
> |
assertEquals(n, q.size()); |
51 |
|
return q; |
52 |
|
} |
53 |
|
|
59 |
|
} |
60 |
|
|
61 |
|
/** |
62 |
< |
* Constructor throws IAE if capacity argument nonpositive |
62 |
> |
* Constructor throws IAE if capacity argument nonpositive |
63 |
|
*/ |
64 |
|
public void testConstructor2() { |
65 |
|
try { |
66 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(0); |
67 |
|
shouldThrow(); |
68 |
< |
} |
53 |
< |
catch (IllegalArgumentException success) {} |
68 |
> |
} catch (IllegalArgumentException success) {} |
69 |
|
} |
70 |
|
|
71 |
|
/** |
75 |
|
try { |
76 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null); |
77 |
|
shouldThrow(); |
78 |
< |
} |
64 |
< |
catch (NullPointerException success) {} |
78 |
> |
} catch (NullPointerException success) {} |
79 |
|
} |
80 |
|
|
81 |
|
/** |
86 |
|
Integer[] ints = new Integer[SIZE]; |
87 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
88 |
|
shouldThrow(); |
89 |
< |
} |
76 |
< |
catch (NullPointerException success) {} |
89 |
> |
} catch (NullPointerException success) {} |
90 |
|
} |
91 |
|
|
92 |
|
/** |
99 |
|
ints[i] = new Integer(i); |
100 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
101 |
|
shouldThrow(); |
102 |
< |
} |
90 |
< |
catch (NullPointerException success) {} |
102 |
> |
} catch (NullPointerException success) {} |
103 |
|
} |
104 |
|
|
105 |
|
/** |
112 |
|
ints[i] = new Integer(i); |
113 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints)); |
114 |
|
shouldThrow(); |
115 |
< |
} |
104 |
< |
catch (IllegalArgumentException success) {} |
115 |
> |
} catch (IllegalArgumentException success) {} |
116 |
|
} |
117 |
|
|
118 |
|
/** |
119 |
|
* Queue contains all elements of collection used to initialize |
120 |
|
*/ |
121 |
|
public void testConstructor7() { |
122 |
< |
try { |
123 |
< |
Integer[] ints = new Integer[SIZE]; |
124 |
< |
for (int i = 0; i < SIZE; ++i) |
125 |
< |
ints[i] = new Integer(i); |
126 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints)); |
127 |
< |
for (int i = 0; i < SIZE; ++i) |
117 |
< |
assertEquals(ints[i], q.poll()); |
118 |
< |
} |
119 |
< |
finally {} |
122 |
> |
Integer[] ints = new Integer[SIZE]; |
123 |
> |
for (int i = 0; i < SIZE; ++i) |
124 |
> |
ints[i] = new Integer(i); |
125 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints)); |
126 |
> |
for (int i = 0; i < SIZE; ++i) |
127 |
> |
assertEquals(ints[i], q.poll()); |
128 |
|
} |
129 |
|
|
130 |
|
/** |
160 |
|
} |
161 |
|
|
162 |
|
/** |
163 |
< |
* offer(null) throws NPE |
163 |
> |
* offer(null) throws NPE |
164 |
|
*/ |
165 |
|
public void testOfferNull() { |
166 |
< |
try { |
166 |
> |
try { |
167 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
168 |
|
q.offer(null); |
169 |
|
shouldThrow(); |
170 |
< |
} catch (NullPointerException success) { } |
170 |
> |
} catch (NullPointerException success) {} |
171 |
|
} |
172 |
|
|
173 |
|
/** |
174 |
< |
* add(null) throws NPE |
174 |
> |
* add(null) throws NPE |
175 |
|
*/ |
176 |
|
public void testAddNull() { |
177 |
< |
try { |
177 |
> |
try { |
178 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
179 |
|
q.add(null); |
180 |
|
shouldThrow(); |
181 |
< |
} catch (NullPointerException success) { } |
181 |
> |
} catch (NullPointerException success) {} |
182 |
|
} |
183 |
|
|
184 |
|
/** |
194 |
|
* add succeeds if not full; throws ISE if full |
195 |
|
*/ |
196 |
|
public void testAdd() { |
197 |
< |
try { |
197 |
> |
try { |
198 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
199 |
|
for (int i = 0; i < SIZE; ++i) { |
200 |
|
assertTrue(q.add(new Integer(i))); |
201 |
|
} |
202 |
|
assertEquals(0, q.remainingCapacity()); |
203 |
|
q.add(new Integer(SIZE)); |
204 |
< |
} catch (IllegalStateException success){ |
205 |
< |
} |
204 |
> |
shouldThrow(); |
205 |
> |
} catch (IllegalStateException success) {} |
206 |
|
} |
207 |
|
|
208 |
|
/** |
209 |
< |
* addAll(null) throws NPE |
209 |
> |
* addAll(null) throws NPE |
210 |
|
*/ |
211 |
|
public void testAddAll1() { |
212 |
|
try { |
213 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
214 |
|
q.addAll(null); |
215 |
|
shouldThrow(); |
216 |
< |
} |
209 |
< |
catch (NullPointerException success) {} |
216 |
> |
} catch (NullPointerException success) {} |
217 |
|
} |
218 |
|
|
219 |
|
/** |
224 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
225 |
|
q.addAll(q); |
226 |
|
shouldThrow(); |
227 |
< |
} |
221 |
< |
catch (IllegalArgumentException success) {} |
227 |
> |
} catch (IllegalArgumentException success) {} |
228 |
|
} |
229 |
|
|
224 |
– |
|
230 |
|
/** |
231 |
< |
* addAll of a collection with null elements throws NPE |
231 |
> |
* addAll of a collection with null elements throws NPE |
232 |
|
*/ |
233 |
|
public void testAddAll2() { |
234 |
|
try { |
236 |
|
Integer[] ints = new Integer[SIZE]; |
237 |
|
q.addAll(Arrays.asList(ints)); |
238 |
|
shouldThrow(); |
239 |
< |
} |
235 |
< |
catch (NullPointerException success) {} |
239 |
> |
} catch (NullPointerException success) {} |
240 |
|
} |
241 |
+ |
|
242 |
|
/** |
243 |
|
* addAll of a collection with any null elements throws NPE after |
244 |
|
* possibly adding some elements |
251 |
|
ints[i] = new Integer(i); |
252 |
|
q.addAll(Arrays.asList(ints)); |
253 |
|
shouldThrow(); |
254 |
< |
} |
250 |
< |
catch (NullPointerException success) {} |
254 |
> |
} catch (NullPointerException success) {} |
255 |
|
} |
256 |
+ |
|
257 |
|
/** |
258 |
|
* addAll throws ISE if not enough room |
259 |
|
*/ |
265 |
|
ints[i] = new Integer(i); |
266 |
|
q.addAll(Arrays.asList(ints)); |
267 |
|
shouldThrow(); |
268 |
< |
} |
264 |
< |
catch (IllegalStateException success) {} |
268 |
> |
} catch (IllegalStateException success) {} |
269 |
|
} |
270 |
+ |
|
271 |
|
/** |
272 |
|
* Queue contains all elements, in traversal order, of successful addAll |
273 |
|
*/ |
274 |
|
public void testAddAll5() { |
275 |
< |
try { |
276 |
< |
Integer[] empty = new Integer[0]; |
277 |
< |
Integer[] ints = new Integer[SIZE]; |
278 |
< |
for (int i = 0; i < SIZE; ++i) |
279 |
< |
ints[i] = new Integer(i); |
280 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
281 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
282 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
283 |
< |
for (int i = 0; i < SIZE; ++i) |
279 |
< |
assertEquals(ints[i], q.poll()); |
280 |
< |
} |
281 |
< |
finally {} |
275 |
> |
Integer[] empty = new Integer[0]; |
276 |
> |
Integer[] ints = new Integer[SIZE]; |
277 |
> |
for (int i = 0; i < SIZE; ++i) |
278 |
> |
ints[i] = new Integer(i); |
279 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
280 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
281 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
282 |
> |
for (int i = 0; i < SIZE; ++i) |
283 |
> |
assertEquals(ints[i], q.poll()); |
284 |
|
} |
285 |
|
|
286 |
|
/** |
287 |
< |
* put(null) throws NPE |
287 |
> |
* put(null) throws NPE |
288 |
|
*/ |
289 |
< |
public void testPutNull() { |
290 |
< |
try { |
289 |
> |
public void testPutNull() throws InterruptedException { |
290 |
> |
try { |
291 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
292 |
|
q.put(null); |
293 |
|
shouldThrow(); |
294 |
< |
} |
295 |
< |
catch (NullPointerException success){ |
294 |
< |
} |
295 |
< |
catch (InterruptedException ie) { |
296 |
< |
unexpectedException(); |
297 |
< |
} |
298 |
< |
} |
294 |
> |
} catch (NullPointerException success) {} |
295 |
> |
} |
296 |
|
|
297 |
|
/** |
298 |
|
* all elements successfully put are contained |
299 |
|
*/ |
300 |
< |
public void testPut() { |
301 |
< |
try { |
302 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
303 |
< |
for (int i = 0; i < SIZE; ++i) { |
304 |
< |
Integer I = new Integer(i); |
305 |
< |
q.put(I); |
309 |
< |
assertTrue(q.contains(I)); |
310 |
< |
} |
311 |
< |
assertEquals(0, q.remainingCapacity()); |
312 |
< |
} |
313 |
< |
catch (InterruptedException ie) { |
314 |
< |
unexpectedException(); |
300 |
> |
public void testPut() throws InterruptedException { |
301 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
302 |
> |
for (int i = 0; i < SIZE; ++i) { |
303 |
> |
Integer I = new Integer(i); |
304 |
> |
q.put(I); |
305 |
> |
assertTrue(q.contains(I)); |
306 |
|
} |
307 |
+ |
assertEquals(0, q.remainingCapacity()); |
308 |
|
} |
309 |
|
|
310 |
|
/** |
311 |
|
* put blocks interruptibly if full |
312 |
|
*/ |
313 |
< |
public void testBlockingPut() { |
313 |
> |
public void testBlockingPut() throws InterruptedException { |
314 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
315 |
< |
Thread t = new Thread(new Runnable() { |
316 |
< |
public void run() { |
317 |
< |
int added = 0; |
318 |
< |
try { |
319 |
< |
for (int i = 0; i < SIZE; ++i) { |
320 |
< |
q.put(new Integer(i)); |
321 |
< |
++added; |
322 |
< |
} |
323 |
< |
q.put(new Integer(SIZE)); |
324 |
< |
threadShouldThrow(); |
325 |
< |
} catch (InterruptedException ie){ |
326 |
< |
threadAssertEquals(added, SIZE); |
327 |
< |
} |
328 |
< |
}}); |
329 |
< |
try { |
330 |
< |
t.start(); |
331 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
332 |
< |
t.interrupt(); |
333 |
< |
t.join(); |
334 |
< |
} |
335 |
< |
catch (InterruptedException ie) { |
336 |
< |
unexpectedException(); |
337 |
< |
} |
315 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
316 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
317 |
> |
public void realRun() throws InterruptedException { |
318 |
> |
for (int i = 0; i < SIZE; ++i) |
319 |
> |
q.put(i); |
320 |
> |
assertEquals(SIZE, q.size()); |
321 |
> |
assertEquals(0, q.remainingCapacity()); |
322 |
> |
|
323 |
> |
Thread.currentThread().interrupt(); |
324 |
> |
try { |
325 |
> |
q.put(99); |
326 |
> |
shouldThrow(); |
327 |
> |
} catch (InterruptedException success) {} |
328 |
> |
assertFalse(Thread.interrupted()); |
329 |
> |
|
330 |
> |
pleaseInterrupt.countDown(); |
331 |
> |
try { |
332 |
> |
q.put(99); |
333 |
> |
shouldThrow(); |
334 |
> |
} catch (InterruptedException success) {} |
335 |
> |
assertFalse(Thread.interrupted()); |
336 |
> |
}}); |
337 |
> |
|
338 |
> |
await(pleaseInterrupt); |
339 |
> |
assertThreadStaysAlive(t); |
340 |
> |
t.interrupt(); |
341 |
> |
awaitTermination(t); |
342 |
> |
assertEquals(SIZE, q.size()); |
343 |
> |
assertEquals(0, q.remainingCapacity()); |
344 |
|
} |
345 |
|
|
346 |
|
/** |
347 |
< |
* put blocks waiting for take when full |
347 |
> |
* put blocks interruptibly waiting for take when full |
348 |
|
*/ |
349 |
< |
public void testPutWithTake() { |
350 |
< |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
351 |
< |
Thread t = new Thread(new Runnable() { |
352 |
< |
public void run() { |
353 |
< |
int added = 0; |
354 |
< |
try { |
355 |
< |
q.put(new Object()); |
356 |
< |
++added; |
357 |
< |
q.put(new Object()); |
358 |
< |
++added; |
359 |
< |
q.put(new Object()); |
360 |
< |
++added; |
361 |
< |
q.put(new Object()); |
362 |
< |
++added; |
363 |
< |
threadShouldThrow(); |
364 |
< |
} catch (InterruptedException e){ |
365 |
< |
threadAssertTrue(added >= 2); |
366 |
< |
} |
367 |
< |
} |
368 |
< |
}); |
369 |
< |
try { |
370 |
< |
t.start(); |
371 |
< |
Thread.sleep(SHORT_DELAY_MS); |
372 |
< |
q.take(); |
373 |
< |
t.interrupt(); |
374 |
< |
t.join(); |
375 |
< |
} catch (Exception e){ |
376 |
< |
unexpectedException(); |
377 |
< |
} |
349 |
> |
public void testPutWithTake() throws InterruptedException { |
350 |
> |
final int capacity = 2; |
351 |
> |
final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity); |
352 |
> |
final CountDownLatch pleaseTake = new CountDownLatch(1); |
353 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
354 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
355 |
> |
public void realRun() throws InterruptedException { |
356 |
> |
for (int i = 0; i < capacity; i++) |
357 |
> |
q.put(i); |
358 |
> |
pleaseTake.countDown(); |
359 |
> |
q.put(86); |
360 |
> |
|
361 |
> |
pleaseInterrupt.countDown(); |
362 |
> |
try { |
363 |
> |
q.put(99); |
364 |
> |
shouldThrow(); |
365 |
> |
} catch (InterruptedException success) {} |
366 |
> |
assertFalse(Thread.interrupted()); |
367 |
> |
}}); |
368 |
> |
|
369 |
> |
await(pleaseTake); |
370 |
> |
assertEquals(q.remainingCapacity(), 0); |
371 |
> |
assertEquals(0, q.take()); |
372 |
> |
|
373 |
> |
await(pleaseInterrupt); |
374 |
> |
assertThreadStaysAlive(t); |
375 |
> |
t.interrupt(); |
376 |
> |
awaitTermination(t); |
377 |
> |
assertEquals(q.remainingCapacity(), 0); |
378 |
|
} |
379 |
|
|
380 |
|
/** |
381 |
|
* timed offer times out if full and elements not taken |
382 |
|
*/ |
383 |
< |
public void testTimedOffer() { |
383 |
> |
public void testTimedOffer() throws InterruptedException { |
384 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
385 |
< |
Thread t = new Thread(new Runnable() { |
386 |
< |
public void run() { |
387 |
< |
try { |
388 |
< |
q.put(new Object()); |
389 |
< |
q.put(new Object()); |
390 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); |
391 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
392 |
< |
threadShouldThrow(); |
393 |
< |
} catch (InterruptedException success){} |
394 |
< |
} |
395 |
< |
}); |
396 |
< |
|
397 |
< |
try { |
398 |
< |
t.start(); |
399 |
< |
Thread.sleep(SHORT_DELAY_MS); |
400 |
< |
t.interrupt(); |
401 |
< |
t.join(); |
402 |
< |
} catch (Exception e){ |
403 |
< |
unexpectedException(); |
406 |
< |
} |
385 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
386 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
387 |
> |
public void realRun() throws InterruptedException { |
388 |
> |
q.put(new Object()); |
389 |
> |
q.put(new Object()); |
390 |
> |
long startTime = System.nanoTime(); |
391 |
> |
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
392 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
393 |
> |
pleaseInterrupt.countDown(); |
394 |
> |
try { |
395 |
> |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
396 |
> |
shouldThrow(); |
397 |
> |
} catch (InterruptedException success) {} |
398 |
> |
}}); |
399 |
> |
|
400 |
> |
await(pleaseInterrupt); |
401 |
> |
assertThreadStaysAlive(t); |
402 |
> |
t.interrupt(); |
403 |
> |
awaitTermination(t); |
404 |
|
} |
405 |
|
|
406 |
|
/** |
407 |
|
* take retrieves elements in FIFO order |
408 |
|
*/ |
409 |
< |
public void testTake() { |
410 |
< |
try { |
411 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
412 |
< |
for (int i = 0; i < SIZE; ++i) { |
416 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
417 |
< |
} |
418 |
< |
} catch (InterruptedException e){ |
419 |
< |
unexpectedException(); |
420 |
< |
} |
421 |
< |
} |
422 |
< |
|
423 |
< |
/** |
424 |
< |
* take blocks interruptibly when empty |
425 |
< |
*/ |
426 |
< |
public void testTakeFromEmpty() { |
427 |
< |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
428 |
< |
Thread t = new Thread(new Runnable() { |
429 |
< |
public void run() { |
430 |
< |
try { |
431 |
< |
q.take(); |
432 |
< |
threadShouldThrow(); |
433 |
< |
} catch (InterruptedException success){ } |
434 |
< |
} |
435 |
< |
}); |
436 |
< |
try { |
437 |
< |
t.start(); |
438 |
< |
Thread.sleep(SHORT_DELAY_MS); |
439 |
< |
t.interrupt(); |
440 |
< |
t.join(); |
441 |
< |
} catch (Exception e){ |
442 |
< |
unexpectedException(); |
409 |
> |
public void testTake() throws InterruptedException { |
410 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
411 |
> |
for (int i = 0; i < SIZE; ++i) { |
412 |
> |
assertEquals(i, q.take()); |
413 |
|
} |
414 |
|
} |
415 |
|
|
416 |
|
/** |
417 |
|
* Take removes existing elements until empty, then blocks interruptibly |
418 |
|
*/ |
419 |
< |
public void testBlockingTake() { |
420 |
< |
Thread t = new Thread(new Runnable() { |
421 |
< |
public void run() { |
422 |
< |
try { |
423 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
424 |
< |
for (int i = 0; i < SIZE; ++i) { |
425 |
< |
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
426 |
< |
} |
457 |
< |
q.take(); |
458 |
< |
threadShouldThrow(); |
459 |
< |
} catch (InterruptedException success){ |
460 |
< |
} |
461 |
< |
}}); |
462 |
< |
try { |
463 |
< |
t.start(); |
464 |
< |
Thread.sleep(SHORT_DELAY_MS); |
465 |
< |
t.interrupt(); |
466 |
< |
t.join(); |
467 |
< |
} |
468 |
< |
catch (InterruptedException ie) { |
469 |
< |
unexpectedException(); |
470 |
< |
} |
471 |
< |
} |
419 |
> |
public void testBlockingTake() throws InterruptedException { |
420 |
> |
final ArrayBlockingQueue q = populatedQueue(SIZE); |
421 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
422 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
423 |
> |
public void realRun() throws InterruptedException { |
424 |
> |
for (int i = 0; i < SIZE; ++i) { |
425 |
> |
assertEquals(i, q.take()); |
426 |
> |
} |
427 |
|
|
428 |
+ |
Thread.currentThread().interrupt(); |
429 |
+ |
try { |
430 |
+ |
q.take(); |
431 |
+ |
shouldThrow(); |
432 |
+ |
} catch (InterruptedException success) {} |
433 |
+ |
assertFalse(Thread.interrupted()); |
434 |
+ |
|
435 |
+ |
pleaseInterrupt.countDown(); |
436 |
+ |
try { |
437 |
+ |
q.take(); |
438 |
+ |
shouldThrow(); |
439 |
+ |
} catch (InterruptedException success) {} |
440 |
+ |
assertFalse(Thread.interrupted()); |
441 |
+ |
}}); |
442 |
+ |
|
443 |
+ |
await(pleaseInterrupt); |
444 |
+ |
assertThreadStaysAlive(t); |
445 |
+ |
t.interrupt(); |
446 |
+ |
awaitTermination(t); |
447 |
+ |
} |
448 |
|
|
449 |
|
/** |
450 |
|
* poll succeeds unless empty |
452 |
|
public void testPoll() { |
453 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
454 |
|
for (int i = 0; i < SIZE; ++i) { |
455 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
455 |
> |
assertEquals(i, q.poll()); |
456 |
|
} |
457 |
< |
assertNull(q.poll()); |
457 |
> |
assertNull(q.poll()); |
458 |
|
} |
459 |
|
|
460 |
|
/** |
461 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
461 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
462 |
|
*/ |
463 |
< |
public void testTimedPoll0() { |
464 |
< |
try { |
465 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
466 |
< |
for (int i = 0; i < SIZE; ++i) { |
467 |
< |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
468 |
< |
} |
469 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
495 |
< |
} catch (InterruptedException e){ |
496 |
< |
unexpectedException(); |
497 |
< |
} |
463 |
> |
public void testTimedPoll0() throws InterruptedException { |
464 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
465 |
> |
for (int i = 0; i < SIZE; ++i) { |
466 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
467 |
> |
} |
468 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
469 |
> |
checkEmpty(q); |
470 |
|
} |
471 |
|
|
472 |
|
/** |
473 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
473 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
474 |
|
*/ |
475 |
< |
public void testTimedPoll() { |
476 |
< |
try { |
477 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
478 |
< |
for (int i = 0; i < SIZE; ++i) { |
479 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
480 |
< |
} |
481 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
482 |
< |
} catch (InterruptedException e){ |
483 |
< |
unexpectedException(); |
484 |
< |
} |
475 |
> |
public void testTimedPoll() throws InterruptedException { |
476 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
477 |
> |
for (int i = 0; i < SIZE; ++i) { |
478 |
> |
long startTime = System.nanoTime(); |
479 |
> |
assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
480 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
481 |
> |
} |
482 |
> |
long startTime = System.nanoTime(); |
483 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
484 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
485 |
> |
checkEmpty(q); |
486 |
|
} |
487 |
|
|
488 |
|
/** |
489 |
|
* Interrupted timed poll throws InterruptedException instead of |
490 |
|
* returning timeout status |
491 |
|
*/ |
492 |
< |
public void testInterruptedTimedPoll() { |
493 |
< |
Thread t = new Thread(new Runnable() { |
494 |
< |
public void run() { |
495 |
< |
try { |
496 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
497 |
< |
for (int i = 0; i < SIZE; ++i) { |
498 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
499 |
< |
} |
500 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
528 |
< |
} catch (InterruptedException success){ |
529 |
< |
} |
530 |
< |
}}); |
531 |
< |
try { |
532 |
< |
t.start(); |
533 |
< |
Thread.sleep(SHORT_DELAY_MS); |
534 |
< |
t.interrupt(); |
535 |
< |
t.join(); |
536 |
< |
} |
537 |
< |
catch (InterruptedException ie) { |
538 |
< |
unexpectedException(); |
539 |
< |
} |
540 |
< |
} |
541 |
< |
|
542 |
< |
/** |
543 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
544 |
< |
* on interruption throws |
545 |
< |
*/ |
546 |
< |
public void testTimedPollWithOffer() { |
547 |
< |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
548 |
< |
Thread t = new Thread(new Runnable() { |
549 |
< |
public void run() { |
550 |
< |
try { |
551 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
552 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
553 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
554 |
< |
threadShouldThrow(); |
555 |
< |
} catch (InterruptedException success) { } |
492 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
493 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
494 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
495 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
496 |
> |
public void realRun() throws InterruptedException { |
497 |
> |
for (int i = 0; i < SIZE; ++i) { |
498 |
> |
long t0 = System.nanoTime(); |
499 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
500 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
501 |
|
} |
502 |
< |
}); |
503 |
< |
try { |
504 |
< |
t.start(); |
505 |
< |
Thread.sleep(SMALL_DELAY_MS); |
506 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
507 |
< |
t.interrupt(); |
508 |
< |
t.join(); |
509 |
< |
} catch (Exception e){ |
510 |
< |
unexpectedException(); |
511 |
< |
} |
502 |
> |
long t0 = System.nanoTime(); |
503 |
> |
aboutToWait.countDown(); |
504 |
> |
try { |
505 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
506 |
> |
shouldThrow(); |
507 |
> |
} catch (InterruptedException success) { |
508 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
509 |
> |
} |
510 |
> |
}}); |
511 |
> |
|
512 |
> |
aboutToWait.await(); |
513 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
514 |
> |
t.interrupt(); |
515 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
516 |
> |
checkEmpty(q); |
517 |
|
} |
518 |
|
|
569 |
– |
|
519 |
|
/** |
520 |
|
* peek returns next element, or null if empty |
521 |
|
*/ |
522 |
|
public void testPeek() { |
523 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
524 |
|
for (int i = 0; i < SIZE; ++i) { |
525 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
526 |
< |
q.poll(); |
525 |
> |
assertEquals(i, q.peek()); |
526 |
> |
assertEquals(i, q.poll()); |
527 |
|
assertTrue(q.peek() == null || |
528 |
< |
i != ((Integer)q.peek()).intValue()); |
528 |
> |
!q.peek().equals(i)); |
529 |
|
} |
530 |
< |
assertNull(q.peek()); |
530 |
> |
assertNull(q.peek()); |
531 |
|
} |
532 |
|
|
533 |
|
/** |
536 |
|
public void testElement() { |
537 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
538 |
|
for (int i = 0; i < SIZE; ++i) { |
539 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
540 |
< |
q.poll(); |
539 |
> |
assertEquals(i, q.element()); |
540 |
> |
assertEquals(i, q.poll()); |
541 |
|
} |
542 |
|
try { |
543 |
|
q.element(); |
544 |
|
shouldThrow(); |
545 |
< |
} |
597 |
< |
catch (NoSuchElementException success) {} |
545 |
> |
} catch (NoSuchElementException success) {} |
546 |
|
} |
547 |
|
|
548 |
|
/** |
551 |
|
public void testRemove() { |
552 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
553 |
|
for (int i = 0; i < SIZE; ++i) { |
554 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
554 |
> |
assertEquals(i, q.remove()); |
555 |
|
} |
556 |
|
try { |
557 |
|
q.remove(); |
558 |
|
shouldThrow(); |
559 |
< |
} catch (NoSuchElementException success){ |
612 |
< |
} |
559 |
> |
} catch (NoSuchElementException success) {} |
560 |
|
} |
561 |
|
|
562 |
|
/** |
581 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
582 |
|
for (int i = 0; i < SIZE; ++i) { |
583 |
|
assertTrue(q.contains(new Integer(i))); |
584 |
< |
q.poll(); |
584 |
> |
assertEquals(i, q.poll()); |
585 |
|
assertFalse(q.contains(new Integer(i))); |
586 |
|
} |
587 |
|
} |
652 |
|
} |
653 |
|
|
654 |
|
/** |
655 |
< |
* toArray contains all elements |
655 |
> |
* toArray contains all elements in FIFO order |
656 |
|
*/ |
657 |
|
public void testToArray() { |
658 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
659 |
< |
Object[] o = q.toArray(); |
660 |
< |
try { |
661 |
< |
for (int i = 0; i < o.length; i++) |
715 |
< |
assertEquals(o[i], q.take()); |
716 |
< |
} catch (InterruptedException e){ |
717 |
< |
unexpectedException(); |
718 |
< |
} |
659 |
> |
Object[] o = q.toArray(); |
660 |
> |
for (int i = 0; i < o.length; i++) |
661 |
> |
assertSame(o[i], q.poll()); |
662 |
|
} |
663 |
|
|
664 |
|
/** |
665 |
< |
* toArray(a) contains all elements |
665 |
> |
* toArray(a) contains all elements in FIFO order |
666 |
|
*/ |
667 |
|
public void testToArray2() { |
668 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
669 |
< |
Integer[] ints = new Integer[SIZE]; |
670 |
< |
ints = (Integer[])q.toArray(ints); |
671 |
< |
try { |
672 |
< |
for (int i = 0; i < ints.length; i++) |
673 |
< |
assertEquals(ints[i], q.take()); |
731 |
< |
} catch (InterruptedException e){ |
732 |
< |
unexpectedException(); |
733 |
< |
} |
668 |
> |
ArrayBlockingQueue<Integer> q = populatedQueue(SIZE); |
669 |
> |
Integer[] ints = new Integer[SIZE]; |
670 |
> |
Integer[] array = q.toArray(ints); |
671 |
> |
assertSame(ints, array); |
672 |
> |
for (int i = 0; i < ints.length; i++) |
673 |
> |
assertSame(ints[i], q.poll()); |
674 |
|
} |
675 |
|
|
676 |
|
/** |
677 |
< |
* toArray(null) throws NPE |
677 |
> |
* toArray(null) throws NullPointerException |
678 |
|
*/ |
679 |
< |
public void testToArray_BadArg() { |
680 |
< |
try { |
681 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
682 |
< |
Object o[] = q.toArray(null); |
683 |
< |
shouldThrow(); |
684 |
< |
} catch (NullPointerException success){} |
679 |
> |
public void testToArray_NullArg() { |
680 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
681 |
> |
try { |
682 |
> |
q.toArray(null); |
683 |
> |
shouldThrow(); |
684 |
> |
} catch (NullPointerException success) {} |
685 |
|
} |
686 |
|
|
687 |
|
/** |
688 |
< |
* toArray with incompatible array type throws CCE |
688 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
689 |
|
*/ |
690 |
|
public void testToArray1_BadArg() { |
691 |
< |
try { |
692 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
693 |
< |
Object o[] = q.toArray(new String[10] ); |
694 |
< |
shouldThrow(); |
695 |
< |
} catch (ArrayStoreException success){} |
691 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
692 |
> |
try { |
693 |
> |
q.toArray(new String[10]); |
694 |
> |
shouldThrow(); |
695 |
> |
} catch (ArrayStoreException success) {} |
696 |
|
} |
697 |
|
|
758 |
– |
|
698 |
|
/** |
699 |
|
* iterator iterates through all elements |
700 |
|
*/ |
701 |
< |
public void testIterator() { |
701 |
> |
public void testIterator() throws InterruptedException { |
702 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
703 |
< |
Iterator it = q.iterator(); |
704 |
< |
try { |
705 |
< |
while (it.hasNext()){ |
706 |
< |
assertEquals(it.next(), q.take()); |
768 |
< |
} |
769 |
< |
} catch (InterruptedException e){ |
770 |
< |
unexpectedException(); |
771 |
< |
} |
703 |
> |
Iterator it = q.iterator(); |
704 |
> |
while (it.hasNext()) { |
705 |
> |
assertEquals(it.next(), q.take()); |
706 |
> |
} |
707 |
|
} |
708 |
|
|
709 |
|
/** |
710 |
|
* iterator.remove removes current element |
711 |
|
*/ |
712 |
< |
public void testIteratorRemove () { |
712 |
> |
public void testIteratorRemove() { |
713 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
714 |
|
q.add(two); |
715 |
|
q.add(one); |
720 |
|
it.remove(); |
721 |
|
|
722 |
|
it = q.iterator(); |
723 |
< |
assertEquals(it.next(), one); |
724 |
< |
assertEquals(it.next(), three); |
723 |
> |
assertSame(it.next(), one); |
724 |
> |
assertSame(it.next(), three); |
725 |
|
assertFalse(it.hasNext()); |
726 |
|
} |
727 |
|
|
738 |
|
|
739 |
|
int k = 0; |
740 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
741 |
< |
int i = ((Integer)(it.next())).intValue(); |
807 |
< |
assertEquals(++k, i); |
741 |
> |
assertEquals(++k, it.next()); |
742 |
|
} |
743 |
|
assertEquals(3, k); |
744 |
|
} |
746 |
|
/** |
747 |
|
* Modifications do not cause iterators to fail |
748 |
|
*/ |
749 |
< |
public void testWeaklyConsistentIteration () { |
749 |
> |
public void testWeaklyConsistentIteration() { |
750 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
751 |
|
q.add(one); |
752 |
|
q.add(two); |
753 |
|
q.add(three); |
754 |
< |
try { |
755 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
756 |
< |
q.remove(); |
823 |
< |
it.next(); |
824 |
< |
} |
825 |
< |
} |
826 |
< |
catch (ConcurrentModificationException e) { |
827 |
< |
unexpectedException(); |
754 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
755 |
> |
q.remove(); |
756 |
> |
it.next(); |
757 |
|
} |
758 |
|
assertEquals(0, q.size()); |
759 |
|
} |
760 |
|
|
832 |
– |
|
761 |
|
/** |
762 |
|
* toString contains toStrings of elements |
763 |
|
*/ |
765 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
766 |
|
String s = q.toString(); |
767 |
|
for (int i = 0; i < SIZE; ++i) { |
768 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
768 |
> |
assertTrue(s.contains(String.valueOf(i))); |
769 |
|
} |
770 |
|
} |
771 |
|
|
844 |
– |
|
772 |
|
/** |
773 |
|
* offer transfers elements across Executor tasks |
774 |
|
*/ |
777 |
|
q.add(one); |
778 |
|
q.add(two); |
779 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
780 |
< |
executor.execute(new Runnable() { |
781 |
< |
public void run() { |
782 |
< |
threadAssertFalse(q.offer(three)); |
783 |
< |
try { |
784 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
785 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
786 |
< |
} |
787 |
< |
catch (InterruptedException e) { |
788 |
< |
threadUnexpectedException(); |
789 |
< |
} |
790 |
< |
} |
791 |
< |
}); |
792 |
< |
|
793 |
< |
executor.execute(new Runnable() { |
794 |
< |
public void run() { |
868 |
< |
try { |
869 |
< |
Thread.sleep(SMALL_DELAY_MS); |
870 |
< |
threadAssertEquals(one, q.take()); |
871 |
< |
} |
872 |
< |
catch (InterruptedException e) { |
873 |
< |
threadUnexpectedException(); |
874 |
< |
} |
875 |
< |
} |
876 |
< |
}); |
780 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
781 |
> |
executor.execute(new CheckedRunnable() { |
782 |
> |
public void realRun() throws InterruptedException { |
783 |
> |
assertFalse(q.offer(three)); |
784 |
> |
threadsStarted.await(); |
785 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
786 |
> |
assertEquals(0, q.remainingCapacity()); |
787 |
> |
}}); |
788 |
> |
|
789 |
> |
executor.execute(new CheckedRunnable() { |
790 |
> |
public void realRun() throws InterruptedException { |
791 |
> |
threadsStarted.await(); |
792 |
> |
assertEquals(0, q.remainingCapacity()); |
793 |
> |
assertSame(one, q.take()); |
794 |
> |
}}); |
795 |
|
|
796 |
|
joinPool(executor); |
879 |
– |
|
797 |
|
} |
798 |
|
|
799 |
|
/** |
800 |
< |
* poll retrieves elements across Executor threads |
800 |
> |
* timed poll retrieves elements across Executor threads |
801 |
|
*/ |
802 |
|
public void testPollInExecutor() { |
803 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
804 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
805 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
806 |
< |
executor.execute(new Runnable() { |
807 |
< |
public void run() { |
808 |
< |
threadAssertNull(q.poll()); |
809 |
< |
try { |
810 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
811 |
< |
threadAssertTrue(q.isEmpty()); |
812 |
< |
} |
813 |
< |
catch (InterruptedException e) { |
814 |
< |
threadUnexpectedException(); |
815 |
< |
} |
816 |
< |
} |
817 |
< |
}); |
818 |
< |
|
901 |
< |
executor.execute(new Runnable() { |
902 |
< |
public void run() { |
903 |
< |
try { |
904 |
< |
Thread.sleep(SMALL_DELAY_MS); |
905 |
< |
q.put(one); |
906 |
< |
} |
907 |
< |
catch (InterruptedException e) { |
908 |
< |
threadUnexpectedException(); |
909 |
< |
} |
910 |
< |
} |
911 |
< |
}); |
806 |
> |
executor.execute(new CheckedRunnable() { |
807 |
> |
public void realRun() throws InterruptedException { |
808 |
> |
assertNull(q.poll()); |
809 |
> |
threadsStarted.await(); |
810 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
811 |
> |
checkEmpty(q); |
812 |
> |
}}); |
813 |
> |
|
814 |
> |
executor.execute(new CheckedRunnable() { |
815 |
> |
public void realRun() throws InterruptedException { |
816 |
> |
threadsStarted.await(); |
817 |
> |
q.put(one); |
818 |
> |
}}); |
819 |
|
|
820 |
|
joinPool(executor); |
821 |
|
} |
823 |
|
/** |
824 |
|
* A deserialized serialized queue has same elements in same order |
825 |
|
*/ |
826 |
< |
public void testSerialization() { |
826 |
> |
public void testSerialization() throws Exception { |
827 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
828 |
|
|
829 |
< |
try { |
830 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
831 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
832 |
< |
out.writeObject(q); |
833 |
< |
out.close(); |
834 |
< |
|
835 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
836 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
837 |
< |
ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject(); |
838 |
< |
assertEquals(q.size(), r.size()); |
839 |
< |
while (!q.isEmpty()) |
933 |
< |
assertEquals(q.remove(), r.remove()); |
934 |
< |
} catch (Exception e){ |
935 |
< |
unexpectedException(); |
936 |
< |
} |
829 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
830 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
831 |
> |
out.writeObject(q); |
832 |
> |
out.close(); |
833 |
> |
|
834 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
835 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
836 |
> |
ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject(); |
837 |
> |
assertEquals(q.size(), r.size()); |
838 |
> |
while (!q.isEmpty()) |
839 |
> |
assertEquals(q.remove(), r.remove()); |
840 |
|
} |
841 |
|
|
842 |
|
/** |
847 |
|
try { |
848 |
|
q.drainTo(null); |
849 |
|
shouldThrow(); |
850 |
< |
} catch (NullPointerException success) { |
948 |
< |
} |
850 |
> |
} catch (NullPointerException success) {} |
851 |
|
} |
852 |
|
|
853 |
|
/** |
858 |
|
try { |
859 |
|
q.drainTo(q); |
860 |
|
shouldThrow(); |
861 |
< |
} catch (IllegalArgumentException success) { |
960 |
< |
} |
861 |
> |
} catch (IllegalArgumentException success) {} |
862 |
|
} |
863 |
|
|
864 |
|
/** |
888 |
|
/** |
889 |
|
* drainTo empties full queue, unblocking a waiting put. |
890 |
|
*/ |
891 |
< |
public void testDrainToWithActivePut() { |
891 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
892 |
|
final ArrayBlockingQueue q = populatedQueue(SIZE); |
893 |
< |
Thread t = new Thread(new Runnable() { |
894 |
< |
public void run() { |
895 |
< |
try { |
896 |
< |
q.put(new Integer(SIZE+1)); |
897 |
< |
} catch (InterruptedException ie){ |
898 |
< |
threadUnexpectedException(); |
899 |
< |
} |
900 |
< |
} |
901 |
< |
}); |
902 |
< |
try { |
903 |
< |
t.start(); |
904 |
< |
ArrayList l = new ArrayList(); |
905 |
< |
q.drainTo(l); |
1005 |
< |
assertTrue(l.size() >= SIZE); |
1006 |
< |
for (int i = 0; i < SIZE; ++i) |
1007 |
< |
assertEquals(l.get(i), new Integer(i)); |
1008 |
< |
t.join(); |
1009 |
< |
assertTrue(q.size() + l.size() >= SIZE); |
1010 |
< |
} catch (Exception e){ |
1011 |
< |
unexpectedException(); |
1012 |
< |
} |
893 |
> |
Thread t = new Thread(new CheckedRunnable() { |
894 |
> |
public void realRun() throws InterruptedException { |
895 |
> |
q.put(new Integer(SIZE+1)); |
896 |
> |
}}); |
897 |
> |
|
898 |
> |
t.start(); |
899 |
> |
ArrayList l = new ArrayList(); |
900 |
> |
q.drainTo(l); |
901 |
> |
assertTrue(l.size() >= SIZE); |
902 |
> |
for (int i = 0; i < SIZE; ++i) |
903 |
> |
assertEquals(l.get(i), new Integer(i)); |
904 |
> |
t.join(); |
905 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
906 |
|
} |
907 |
|
|
908 |
|
/** |
913 |
|
try { |
914 |
|
q.drainTo(null, 0); |
915 |
|
shouldThrow(); |
916 |
< |
} catch (NullPointerException success) { |
1024 |
< |
} |
916 |
> |
} catch (NullPointerException success) {} |
917 |
|
} |
918 |
|
|
919 |
|
/** |
924 |
|
try { |
925 |
|
q.drainTo(q, 0); |
926 |
|
shouldThrow(); |
927 |
< |
} catch (IllegalArgumentException success) { |
1036 |
< |
} |
927 |
> |
} catch (IllegalArgumentException success) {} |
928 |
|
} |
929 |
|
|
930 |
|
/** |
931 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
931 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
932 |
|
*/ |
933 |
|
public void testDrainToN() { |
934 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2); |
937 |
|
assertTrue(q.offer(new Integer(j))); |
938 |
|
ArrayList l = new ArrayList(); |
939 |
|
q.drainTo(l, i); |
940 |
< |
int k = (i < SIZE)? i : SIZE; |
940 |
> |
int k = (i < SIZE) ? i : SIZE; |
941 |
|
assertEquals(l.size(), k); |
942 |
|
assertEquals(q.size(), SIZE-k); |
943 |
|
for (int j = 0; j < k; ++j) |
946 |
|
} |
947 |
|
} |
948 |
|
|
1058 |
– |
|
949 |
|
} |