10 |
|
import java.util.concurrent.*; |
11 |
|
import java.io.*; |
12 |
|
|
13 |
< |
public class ArrayBlockingQueueTest extends TestCase { |
14 |
< |
|
15 |
< |
private static int N = 10; |
16 |
< |
private static long SHORT_DELAY_MS = 100; |
17 |
< |
private static long MEDIUM_DELAY_MS = 1000; |
18 |
< |
private static long LONG_DELAY_MS = 10000; |
13 |
> |
public class ArrayBlockingQueueTest extends JSR166TestCase { |
14 |
|
|
15 |
|
public static void main(String[] args) { |
16 |
|
junit.textui.TestRunner.run (suite()); |
24 |
|
* Create a queue of given size containing consecutive |
25 |
|
* Integers 0 ... n. |
26 |
|
*/ |
27 |
< |
private ArrayBlockingQueue fullQueue(int n) { |
27 |
> |
private ArrayBlockingQueue populatedQueue(int n) { |
28 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(n); |
29 |
|
assertTrue(q.isEmpty()); |
30 |
|
for(int i = 0; i < n; i++) |
36 |
|
} |
37 |
|
|
38 |
|
public void testConstructor1(){ |
39 |
< |
assertEquals(N, new ArrayBlockingQueue(N).remainingCapacity()); |
39 |
> |
assertEquals(SIZE, new ArrayBlockingQueue(SIZE).remainingCapacity()); |
40 |
|
} |
41 |
|
|
42 |
|
public void testConstructor2(){ |
58 |
|
|
59 |
|
public void testConstructor4(){ |
60 |
|
try { |
61 |
< |
Integer[] ints = new Integer[N]; |
62 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(N, false, Arrays.asList(ints)); |
61 |
> |
Integer[] ints = new Integer[SIZE]; |
62 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
63 |
|
fail("Cannot make with null elements"); |
64 |
|
} |
65 |
|
catch (NullPointerException success) {} |
67 |
|
|
68 |
|
public void testConstructor5(){ |
69 |
|
try { |
70 |
< |
Integer[] ints = new Integer[N]; |
71 |
< |
for (int i = 0; i < N-1; ++i) |
70 |
> |
Integer[] ints = new Integer[SIZE]; |
71 |
> |
for (int i = 0; i < SIZE-1; ++i) |
72 |
|
ints[i] = new Integer(i); |
73 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(N, false, Arrays.asList(ints)); |
73 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
74 |
|
fail("Cannot make with null elements"); |
75 |
|
} |
76 |
|
catch (NullPointerException success) {} |
78 |
|
|
79 |
|
public void testConstructor6(){ |
80 |
|
try { |
81 |
< |
Integer[] ints = new Integer[N]; |
82 |
< |
for (int i = 0; i < N; ++i) |
81 |
> |
Integer[] ints = new Integer[SIZE]; |
82 |
> |
for (int i = 0; i < SIZE; ++i) |
83 |
|
ints[i] = new Integer(i); |
84 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints)); |
85 |
|
fail("Cannot make with insufficient capacity"); |
89 |
|
|
90 |
|
public void testConstructor7(){ |
91 |
|
try { |
92 |
< |
Integer[] ints = new Integer[N]; |
93 |
< |
for (int i = 0; i < N; ++i) |
92 |
> |
Integer[] ints = new Integer[SIZE]; |
93 |
> |
for (int i = 0; i < SIZE; ++i) |
94 |
|
ints[i] = new Integer(i); |
95 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(N, true, Arrays.asList(ints)); |
96 |
< |
for (int i = 0; i < N; ++i) |
95 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints)); |
96 |
> |
for (int i = 0; i < SIZE; ++i) |
97 |
|
assertEquals(ints[i], q.poll()); |
98 |
|
} |
99 |
|
finally {} |
103 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
104 |
|
assertTrue(q.isEmpty()); |
105 |
|
assertEquals("should have room for 2", 2, q.remainingCapacity()); |
106 |
< |
q.add(new Integer(1)); |
106 |
> |
q.add(one); |
107 |
|
assertFalse(q.isEmpty()); |
108 |
< |
q.add(new Integer(2)); |
108 |
> |
q.add(two); |
109 |
|
assertFalse(q.isEmpty()); |
110 |
|
assertEquals("queue should be full", 0, q.remainingCapacity()); |
111 |
< |
assertFalse("offer should be rejected", q.offer(new Integer(3))); |
111 |
> |
assertFalse("offer should be rejected", q.offer(three)); |
112 |
|
} |
113 |
|
|
114 |
|
public void testRemainingCapacity(){ |
115 |
< |
ArrayBlockingQueue q = fullQueue(N); |
116 |
< |
for (int i = 0; i < N; ++i) { |
115 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
116 |
> |
for (int i = 0; i < SIZE; ++i) { |
117 |
|
assertEquals(i, q.remainingCapacity()); |
118 |
< |
assertEquals(N-i, q.size()); |
118 |
> |
assertEquals(SIZE-i, q.size()); |
119 |
|
q.remove(); |
120 |
|
} |
121 |
< |
for (int i = 0; i < N; ++i) { |
122 |
< |
assertEquals(N-i, q.remainingCapacity()); |
121 |
> |
for (int i = 0; i < SIZE; ++i) { |
122 |
> |
assertEquals(SIZE-i, q.remainingCapacity()); |
123 |
|
assertEquals(i, q.size()); |
124 |
|
q.add(new Integer(i)); |
125 |
|
} |
135 |
|
|
136 |
|
public void testOffer(){ |
137 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
138 |
< |
assertTrue(q.offer(new Integer(0))); |
139 |
< |
assertFalse(q.offer(new Integer(1))); |
138 |
> |
assertTrue(q.offer(zero)); |
139 |
> |
assertFalse(q.offer(one)); |
140 |
|
} |
141 |
|
|
142 |
|
public void testAdd(){ |
143 |
|
try { |
144 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(N); |
145 |
< |
for (int i = 0; i < N; ++i) { |
144 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
145 |
> |
for (int i = 0; i < SIZE; ++i) { |
146 |
|
assertTrue(q.add(new Integer(i))); |
147 |
|
} |
148 |
|
assertEquals(0, q.remainingCapacity()); |
149 |
< |
q.add(new Integer(N)); |
149 |
> |
q.add(new Integer(SIZE)); |
150 |
|
} catch (IllegalStateException success){ |
151 |
|
} |
152 |
|
} |
161 |
|
} |
162 |
|
public void testAddAll2(){ |
163 |
|
try { |
164 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(N); |
165 |
< |
Integer[] ints = new Integer[N]; |
164 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
165 |
> |
Integer[] ints = new Integer[SIZE]; |
166 |
|
q.addAll(Arrays.asList(ints)); |
167 |
|
fail("Cannot add null elements"); |
168 |
|
} |
170 |
|
} |
171 |
|
public void testAddAll3(){ |
172 |
|
try { |
173 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(N); |
174 |
< |
Integer[] ints = new Integer[N]; |
175 |
< |
for (int i = 0; i < N-1; ++i) |
173 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
174 |
> |
Integer[] ints = new Integer[SIZE]; |
175 |
> |
for (int i = 0; i < SIZE-1; ++i) |
176 |
|
ints[i] = new Integer(i); |
177 |
|
q.addAll(Arrays.asList(ints)); |
178 |
|
fail("Cannot add null elements"); |
182 |
|
public void testAddAll4(){ |
183 |
|
try { |
184 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
185 |
< |
Integer[] ints = new Integer[N]; |
186 |
< |
for (int i = 0; i < N; ++i) |
185 |
> |
Integer[] ints = new Integer[SIZE]; |
186 |
> |
for (int i = 0; i < SIZE; ++i) |
187 |
|
ints[i] = new Integer(i); |
188 |
|
q.addAll(Arrays.asList(ints)); |
189 |
|
fail("Cannot add with insufficient capacity"); |
193 |
|
public void testAddAll5(){ |
194 |
|
try { |
195 |
|
Integer[] empty = new Integer[0]; |
196 |
< |
Integer[] ints = new Integer[N]; |
197 |
< |
for (int i = 0; i < N; ++i) |
196 |
> |
Integer[] ints = new Integer[SIZE]; |
197 |
> |
for (int i = 0; i < SIZE; ++i) |
198 |
|
ints[i] = new Integer(i); |
199 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(N); |
199 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
200 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
201 |
|
assertTrue(q.addAll(Arrays.asList(ints))); |
202 |
< |
for (int i = 0; i < N; ++i) |
202 |
> |
for (int i = 0; i < SIZE; ++i) |
203 |
|
assertEquals(ints[i], q.poll()); |
204 |
|
} |
205 |
|
finally {} |
207 |
|
|
208 |
|
public void testPutNull() { |
209 |
|
try { |
210 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(N); |
210 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
211 |
|
q.put(null); |
212 |
|
fail("put should throw NPE"); |
213 |
|
} |
220 |
|
|
221 |
|
public void testPut() { |
222 |
|
try { |
223 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(N); |
224 |
< |
for (int i = 0; i < N; ++i) { |
223 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
224 |
> |
for (int i = 0; i < SIZE; ++i) { |
225 |
|
Integer I = new Integer(i); |
226 |
|
q.put(I); |
227 |
|
assertTrue(q.contains(I)); |
238 |
|
public void run() { |
239 |
|
int added = 0; |
240 |
|
try { |
241 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(N); |
242 |
< |
for (int i = 0; i < N; ++i) { |
241 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
242 |
> |
for (int i = 0; i < SIZE; ++i) { |
243 |
|
q.put(new Integer(i)); |
244 |
|
++added; |
245 |
|
} |
246 |
< |
q.put(new Integer(N)); |
247 |
< |
fail("put should block"); |
246 |
> |
q.put(new Integer(SIZE)); |
247 |
> |
threadFail("put should block"); |
248 |
|
} catch (InterruptedException ie){ |
249 |
< |
assertEquals(added, N); |
249 |
> |
threadAssertEquals(added, SIZE); |
250 |
|
} |
251 |
|
}}); |
257 |
– |
t.start(); |
252 |
|
try { |
253 |
+ |
t.start(); |
254 |
|
Thread.sleep(SHORT_DELAY_MS); |
255 |
|
t.interrupt(); |
256 |
|
t.join(); |
274 |
|
++added; |
275 |
|
q.put(new Object()); |
276 |
|
++added; |
277 |
< |
fail("Should block"); |
277 |
> |
threadFail("Should block"); |
278 |
|
} catch (InterruptedException e){ |
279 |
< |
assertTrue(added >= 2); |
279 |
> |
threadAssertTrue(added >= 2); |
280 |
|
} |
281 |
|
} |
282 |
|
}); |
298 |
|
try { |
299 |
|
q.put(new Object()); |
300 |
|
q.put(new Object()); |
301 |
< |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); |
301 |
> |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); |
302 |
|
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
303 |
< |
fail("Should block"); |
303 |
> |
threadFail("Should block"); |
304 |
|
} catch (InterruptedException success){} |
305 |
|
} |
306 |
|
}); |
317 |
|
|
318 |
|
public void testTake(){ |
319 |
|
try { |
320 |
< |
ArrayBlockingQueue q = fullQueue(N); |
321 |
< |
for (int i = 0; i < N; ++i) { |
320 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
321 |
> |
for (int i = 0; i < SIZE; ++i) { |
322 |
|
assertEquals(i, ((Integer)q.take()).intValue()); |
323 |
|
} |
324 |
|
} catch (InterruptedException e){ |
332 |
|
public void run(){ |
333 |
|
try { |
334 |
|
q.take(); |
335 |
< |
fail("Should block"); |
335 |
> |
threadFail("Should block"); |
336 |
|
} catch (InterruptedException success){ } |
337 |
|
} |
338 |
|
}); |
350 |
|
Thread t = new Thread(new Runnable() { |
351 |
|
public void run() { |
352 |
|
try { |
353 |
< |
ArrayBlockingQueue q = fullQueue(N); |
354 |
< |
for (int i = 0; i < N; ++i) { |
355 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
353 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
354 |
> |
for (int i = 0; i < SIZE; ++i) { |
355 |
> |
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
356 |
|
} |
357 |
|
q.take(); |
358 |
< |
fail("take should block"); |
358 |
> |
threadFail("take should block"); |
359 |
|
} catch (InterruptedException success){ |
360 |
|
} |
361 |
|
}}); |
367 |
– |
t.start(); |
362 |
|
try { |
363 |
< |
Thread.sleep(SHORT_DELAY_MS); |
364 |
< |
t.interrupt(); |
365 |
< |
t.join(); |
363 |
> |
t.start(); |
364 |
> |
Thread.sleep(SHORT_DELAY_MS); |
365 |
> |
t.interrupt(); |
366 |
> |
t.join(); |
367 |
|
} |
368 |
|
catch (InterruptedException ie) { |
369 |
|
fail("Unexpected exception"); |
372 |
|
|
373 |
|
|
374 |
|
public void testPoll(){ |
375 |
< |
ArrayBlockingQueue q = fullQueue(N); |
376 |
< |
for (int i = 0; i < N; ++i) { |
375 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
376 |
> |
for (int i = 0; i < SIZE; ++i) { |
377 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
378 |
|
} |
379 |
|
assertNull(q.poll()); |
381 |
|
|
382 |
|
public void testTimedPoll0() { |
383 |
|
try { |
384 |
< |
ArrayBlockingQueue q = fullQueue(N); |
385 |
< |
for (int i = 0; i < N; ++i) { |
384 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
385 |
> |
for (int i = 0; i < SIZE; ++i) { |
386 |
|
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
387 |
|
} |
388 |
|
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
393 |
|
|
394 |
|
public void testTimedPoll() { |
395 |
|
try { |
396 |
< |
ArrayBlockingQueue q = fullQueue(N); |
397 |
< |
for (int i = 0; i < N; ++i) { |
396 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
397 |
> |
for (int i = 0; i < SIZE; ++i) { |
398 |
|
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
399 |
|
} |
400 |
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
407 |
|
Thread t = new Thread(new Runnable() { |
408 |
|
public void run() { |
409 |
|
try { |
410 |
< |
ArrayBlockingQueue q = fullQueue(N); |
411 |
< |
for (int i = 0; i < N; ++i) { |
412 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
410 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
411 |
> |
for (int i = 0; i < SIZE; ++i) { |
412 |
> |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
413 |
|
} |
414 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
414 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
415 |
|
} catch (InterruptedException success){ |
416 |
|
} |
417 |
|
}}); |
423 |
– |
t.start(); |
418 |
|
try { |
419 |
< |
Thread.sleep(SHORT_DELAY_MS); |
420 |
< |
t.interrupt(); |
421 |
< |
t.join(); |
419 |
> |
t.start(); |
420 |
> |
Thread.sleep(SHORT_DELAY_MS); |
421 |
> |
t.interrupt(); |
422 |
> |
t.join(); |
423 |
|
} |
424 |
|
catch (InterruptedException ie) { |
425 |
|
fail("Unexpected exception"); |
431 |
|
Thread t = new Thread(new Runnable() { |
432 |
|
public void run(){ |
433 |
|
try { |
434 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
434 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
435 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
436 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
437 |
< |
fail("Should block"); |
437 |
> |
threadFail("Should block"); |
438 |
|
} catch (InterruptedException success) { } |
439 |
|
} |
440 |
|
}); |
441 |
|
try { |
442 |
|
t.start(); |
443 |
< |
Thread.sleep(SHORT_DELAY_MS * 2); |
444 |
< |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
443 |
> |
Thread.sleep(SMALL_DELAY_MS); |
444 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
445 |
|
t.interrupt(); |
446 |
|
t.join(); |
447 |
|
} catch (Exception e){ |
451 |
|
|
452 |
|
|
453 |
|
public void testPeek(){ |
454 |
< |
ArrayBlockingQueue q = fullQueue(N); |
455 |
< |
for (int i = 0; i < N; ++i) { |
454 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
455 |
> |
for (int i = 0; i < SIZE; ++i) { |
456 |
|
assertEquals(i, ((Integer)q.peek()).intValue()); |
457 |
|
q.poll(); |
458 |
|
assertTrue(q.peek() == null || |
462 |
|
} |
463 |
|
|
464 |
|
public void testElement(){ |
465 |
< |
ArrayBlockingQueue q = fullQueue(N); |
466 |
< |
for (int i = 0; i < N; ++i) { |
465 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
466 |
> |
for (int i = 0; i < SIZE; ++i) { |
467 |
|
assertEquals(i, ((Integer)q.element()).intValue()); |
468 |
|
q.poll(); |
469 |
|
} |
475 |
|
} |
476 |
|
|
477 |
|
public void testRemove(){ |
478 |
< |
ArrayBlockingQueue q = fullQueue(N); |
479 |
< |
for (int i = 0; i < N; ++i) { |
478 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
479 |
> |
for (int i = 0; i < SIZE; ++i) { |
480 |
|
assertEquals(i, ((Integer)q.remove()).intValue()); |
481 |
|
} |
482 |
|
try { |
487 |
|
} |
488 |
|
|
489 |
|
public void testRemoveElement(){ |
490 |
< |
ArrayBlockingQueue q = fullQueue(N); |
491 |
< |
for (int i = 1; i < N; i+=2) { |
490 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
491 |
> |
for (int i = 1; i < SIZE; i+=2) { |
492 |
|
assertTrue(q.remove(new Integer(i))); |
493 |
|
} |
494 |
< |
for (int i = 0; i < N; i+=2) { |
494 |
> |
for (int i = 0; i < SIZE; i+=2) { |
495 |
|
assertTrue(q.remove(new Integer(i))); |
496 |
|
assertFalse(q.remove(new Integer(i+1))); |
497 |
|
} |
499 |
|
} |
500 |
|
|
501 |
|
public void testContains(){ |
502 |
< |
ArrayBlockingQueue q = fullQueue(N); |
503 |
< |
for (int i = 0; i < N; ++i) { |
502 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
503 |
> |
for (int i = 0; i < SIZE; ++i) { |
504 |
|
assertTrue(q.contains(new Integer(i))); |
505 |
|
q.poll(); |
506 |
|
assertFalse(q.contains(new Integer(i))); |
508 |
|
} |
509 |
|
|
510 |
|
public void testClear(){ |
511 |
< |
ArrayBlockingQueue q = fullQueue(N); |
511 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
512 |
|
q.clear(); |
513 |
|
assertTrue(q.isEmpty()); |
514 |
|
assertEquals(0, q.size()); |
515 |
< |
assertEquals(N, q.remainingCapacity()); |
516 |
< |
q.add(new Integer(1)); |
515 |
> |
assertEquals(SIZE, q.remainingCapacity()); |
516 |
> |
q.add(one); |
517 |
|
assertFalse(q.isEmpty()); |
518 |
|
q.clear(); |
519 |
|
assertTrue(q.isEmpty()); |
520 |
|
} |
521 |
|
|
522 |
|
public void testContainsAll(){ |
523 |
< |
ArrayBlockingQueue q = fullQueue(N); |
524 |
< |
ArrayBlockingQueue p = new ArrayBlockingQueue(N); |
525 |
< |
for (int i = 0; i < N; ++i) { |
523 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
524 |
> |
ArrayBlockingQueue p = new ArrayBlockingQueue(SIZE); |
525 |
> |
for (int i = 0; i < SIZE; ++i) { |
526 |
|
assertTrue(q.containsAll(p)); |
527 |
|
assertFalse(p.containsAll(q)); |
528 |
|
p.add(new Integer(i)); |
531 |
|
} |
532 |
|
|
533 |
|
public void testRetainAll(){ |
534 |
< |
ArrayBlockingQueue q = fullQueue(N); |
535 |
< |
ArrayBlockingQueue p = fullQueue(N); |
536 |
< |
for (int i = 0; i < N; ++i) { |
534 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
535 |
> |
ArrayBlockingQueue p = populatedQueue(SIZE); |
536 |
> |
for (int i = 0; i < SIZE; ++i) { |
537 |
|
boolean changed = q.retainAll(p); |
538 |
|
if (i == 0) |
539 |
|
assertFalse(changed); |
541 |
|
assertTrue(changed); |
542 |
|
|
543 |
|
assertTrue(q.containsAll(p)); |
544 |
< |
assertEquals(N-i, q.size()); |
544 |
> |
assertEquals(SIZE-i, q.size()); |
545 |
|
p.remove(); |
546 |
|
} |
547 |
|
} |
548 |
|
|
549 |
|
public void testRemoveAll(){ |
550 |
< |
for (int i = 1; i < N; ++i) { |
551 |
< |
ArrayBlockingQueue q = fullQueue(N); |
552 |
< |
ArrayBlockingQueue p = fullQueue(i); |
550 |
> |
for (int i = 1; i < SIZE; ++i) { |
551 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
552 |
> |
ArrayBlockingQueue p = populatedQueue(i); |
553 |
|
assertTrue(q.removeAll(p)); |
554 |
< |
assertEquals(N-i, q.size()); |
554 |
> |
assertEquals(SIZE-i, q.size()); |
555 |
|
for (int j = 0; j < i; ++j) { |
556 |
|
Integer I = (Integer)(p.remove()); |
557 |
|
assertFalse(q.contains(I)); |
561 |
|
|
562 |
|
|
563 |
|
public void testToArray(){ |
564 |
< |
ArrayBlockingQueue q = fullQueue(N); |
564 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
565 |
|
Object[] o = q.toArray(); |
566 |
|
try { |
567 |
|
for(int i = 0; i < o.length; i++) |
572 |
|
} |
573 |
|
|
574 |
|
public void testToArray2(){ |
575 |
< |
ArrayBlockingQueue q = fullQueue(N); |
576 |
< |
Integer[] ints = new Integer[N]; |
575 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
576 |
> |
Integer[] ints = new Integer[SIZE]; |
577 |
|
ints = (Integer[])q.toArray(ints); |
578 |
|
try { |
579 |
|
for(int i = 0; i < ints.length; i++) |
584 |
|
} |
585 |
|
|
586 |
|
public void testIterator(){ |
587 |
< |
ArrayBlockingQueue q = fullQueue(N); |
587 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
588 |
|
Iterator it = q.iterator(); |
589 |
|
try { |
590 |
|
while(it.hasNext()){ |
599 |
|
|
600 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
601 |
|
|
602 |
< |
q.add(new Integer(1)); |
603 |
< |
q.add(new Integer(2)); |
604 |
< |
q.add(new Integer(3)); |
602 |
> |
q.add(one); |
603 |
> |
q.add(two); |
604 |
> |
q.add(three); |
605 |
|
|
606 |
|
assertEquals("queue should be full", 0, q.remainingCapacity()); |
607 |
|
|
618 |
|
|
619 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
620 |
|
|
621 |
< |
q.add(new Integer(1)); |
622 |
< |
q.add(new Integer(2)); |
623 |
< |
q.add(new Integer(3)); |
621 |
> |
q.add(one); |
622 |
> |
q.add(two); |
623 |
> |
q.add(three); |
624 |
|
|
625 |
|
try { |
626 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
637 |
|
|
638 |
|
|
639 |
|
public void testToString(){ |
640 |
< |
ArrayBlockingQueue q = fullQueue(N); |
640 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
641 |
|
String s = q.toString(); |
642 |
< |
for (int i = 0; i < N; ++i) { |
642 |
> |
for (int i = 0; i < SIZE; ++i) { |
643 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
644 |
|
} |
645 |
|
} |
649 |
|
|
650 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
651 |
|
|
652 |
< |
q.add(new Integer(1)); |
653 |
< |
q.add(new Integer(2)); |
652 |
> |
q.add(one); |
653 |
> |
q.add(two); |
654 |
|
|
655 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
656 |
|
|
657 |
|
executor.execute(new Runnable() { |
658 |
|
public void run() { |
659 |
< |
assertFalse("offer should be rejected", q.offer(new Integer(3))); |
659 |
> |
threadAssertFalse(q.offer(three)); |
660 |
|
try { |
661 |
< |
assertTrue("offer should be accepted", q.offer(new Integer(3), MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); |
662 |
< |
assertEquals(0, q.remainingCapacity()); |
661 |
> |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
662 |
> |
threadAssertEquals(0, q.remainingCapacity()); |
663 |
|
} |
664 |
|
catch (InterruptedException e) { |
665 |
< |
fail("should not be interrupted"); |
665 |
> |
threadFail("should not be interrupted"); |
666 |
|
} |
667 |
|
} |
668 |
|
}); |
670 |
|
executor.execute(new Runnable() { |
671 |
|
public void run() { |
672 |
|
try { |
673 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
674 |
< |
assertEquals("first item in queue should be 1", new Integer(1), q.take()); |
673 |
> |
Thread.sleep(SMALL_DELAY_MS); |
674 |
> |
threadAssertEquals(one, q.take()); |
675 |
|
} |
676 |
|
catch (InterruptedException e) { |
677 |
< |
fail("should not be interrupted"); |
677 |
> |
threadFail("should not be interrupted"); |
678 |
|
} |
679 |
|
} |
680 |
|
}); |
681 |
|
|
682 |
< |
executor.shutdown(); |
682 |
> |
joinPool(executor); |
683 |
|
|
684 |
|
} |
685 |
|
|
691 |
|
|
692 |
|
executor.execute(new Runnable() { |
693 |
|
public void run() { |
694 |
< |
assertNull("poll should fail", q.poll()); |
694 |
> |
threadAssertNull(q.poll()); |
695 |
|
try { |
696 |
< |
assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); |
697 |
< |
assertTrue(q.isEmpty()); |
696 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
697 |
> |
threadAssertTrue(q.isEmpty()); |
698 |
|
} |
699 |
|
catch (InterruptedException e) { |
700 |
< |
fail("should not be interrupted"); |
700 |
> |
threadFail("should not be interrupted"); |
701 |
|
} |
702 |
|
} |
703 |
|
}); |
705 |
|
executor.execute(new Runnable() { |
706 |
|
public void run() { |
707 |
|
try { |
708 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
709 |
< |
q.put(new Integer(1)); |
708 |
> |
Thread.sleep(SMALL_DELAY_MS); |
709 |
> |
q.put(one); |
710 |
|
} |
711 |
|
catch (InterruptedException e) { |
712 |
< |
fail("should not be interrupted"); |
712 |
> |
threadFail("should not be interrupted"); |
713 |
|
} |
714 |
|
} |
715 |
|
}); |
716 |
|
|
717 |
< |
executor.shutdown(); |
717 |
> |
joinPool(executor); |
718 |
|
|
719 |
|
} |
720 |
|
|
721 |
|
public void testSerialization() { |
722 |
< |
ArrayBlockingQueue q = fullQueue(N); |
722 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
723 |
|
|
724 |
|
try { |
725 |
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |