10 |
|
import java.util.concurrent.*; |
11 |
|
import java.io.*; |
12 |
|
|
13 |
< |
public class LinkedBlockingQueueTest 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 LinkedBlockingQueueTest extends JSR166TestCase { |
14 |
|
|
15 |
|
public static void main(String[] args) { |
16 |
|
junit.textui.TestRunner.run (suite()); |
20 |
|
return new TestSuite(LinkedBlockingQueueTest.class); |
21 |
|
} |
22 |
|
|
23 |
+ |
|
24 |
|
/** |
25 |
|
* Create a queue of given size containing consecutive |
26 |
|
* Integers 0 ... n. |
27 |
|
*/ |
28 |
< |
private LinkedBlockingQueue fullQueue(int n) { |
28 |
> |
private LinkedBlockingQueue populatedQueue(int n) { |
29 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(n); |
30 |
|
assertTrue(q.isEmpty()); |
31 |
|
for(int i = 0; i < n; i++) |
37 |
|
} |
38 |
|
|
39 |
|
public void testConstructor1(){ |
40 |
< |
assertEquals(N, new LinkedBlockingQueue(N).remainingCapacity()); |
40 |
> |
assertEquals(SIZE, new LinkedBlockingQueue(SIZE).remainingCapacity()); |
41 |
|
} |
42 |
|
|
43 |
|
public void testConstructor2(){ |
59 |
|
|
60 |
|
public void testConstructor4(){ |
61 |
|
try { |
62 |
< |
Integer[] ints = new Integer[N]; |
62 |
> |
Integer[] ints = new Integer[SIZE]; |
63 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
64 |
|
fail("Cannot make with null elements"); |
65 |
|
} |
68 |
|
|
69 |
|
public void testConstructor5(){ |
70 |
|
try { |
71 |
< |
Integer[] ints = new Integer[N]; |
72 |
< |
for (int i = 0; i < N-1; ++i) |
71 |
> |
Integer[] ints = new Integer[SIZE]; |
72 |
> |
for (int i = 0; i < SIZE-1; ++i) |
73 |
|
ints[i] = new Integer(i); |
74 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
75 |
|
fail("Cannot make with null elements"); |
79 |
|
|
80 |
|
public void testConstructor6(){ |
81 |
|
try { |
82 |
< |
Integer[] ints = new Integer[N]; |
83 |
< |
for (int i = 0; i < N; ++i) |
82 |
> |
Integer[] ints = new Integer[SIZE]; |
83 |
> |
for (int i = 0; i < SIZE; ++i) |
84 |
|
ints[i] = new Integer(i); |
85 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
86 |
< |
for (int i = 0; i < N; ++i) |
86 |
> |
for (int i = 0; i < SIZE; ++i) |
87 |
|
assertEquals(ints[i], q.poll()); |
88 |
|
} |
89 |
|
finally {} |
93 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
94 |
|
assertTrue(q.isEmpty()); |
95 |
|
assertEquals("should have room for 2", 2, q.remainingCapacity()); |
96 |
< |
q.add(new Integer(1)); |
96 |
> |
q.add(one); |
97 |
|
assertFalse(q.isEmpty()); |
98 |
< |
q.add(new Integer(2)); |
98 |
> |
q.add(two); |
99 |
|
assertFalse(q.isEmpty()); |
100 |
|
assertEquals("queue should be full", 0, q.remainingCapacity()); |
101 |
< |
assertFalse("offer should be rejected", q.offer(new Integer(3))); |
101 |
> |
assertFalse("offer should be rejected", q.offer(three)); |
102 |
|
} |
103 |
|
|
104 |
|
public void testRemainingCapacity(){ |
105 |
< |
LinkedBlockingQueue q = fullQueue(N); |
106 |
< |
for (int i = 0; i < N; ++i) { |
105 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
106 |
> |
for (int i = 0; i < SIZE; ++i) { |
107 |
|
assertEquals(i, q.remainingCapacity()); |
108 |
< |
assertEquals(N-i, q.size()); |
108 |
> |
assertEquals(SIZE-i, q.size()); |
109 |
|
q.remove(); |
110 |
|
} |
111 |
< |
for (int i = 0; i < N; ++i) { |
112 |
< |
assertEquals(N-i, q.remainingCapacity()); |
111 |
> |
for (int i = 0; i < SIZE; ++i) { |
112 |
> |
assertEquals(SIZE-i, q.remainingCapacity()); |
113 |
|
assertEquals(i, q.size()); |
114 |
|
q.add(new Integer(i)); |
115 |
|
} |
125 |
|
|
126 |
|
public void testOffer(){ |
127 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
128 |
< |
assertTrue(q.offer(new Integer(0))); |
129 |
< |
assertFalse(q.offer(new Integer(1))); |
128 |
> |
assertTrue(q.offer(zero)); |
129 |
> |
assertFalse(q.offer(one)); |
130 |
|
} |
131 |
|
|
132 |
|
public void testAdd(){ |
133 |
|
try { |
134 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
135 |
< |
for (int i = 0; i < N; ++i) { |
134 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
135 |
> |
for (int i = 0; i < SIZE; ++i) { |
136 |
|
assertTrue(q.add(new Integer(i))); |
137 |
|
} |
138 |
|
assertEquals(0, q.remainingCapacity()); |
139 |
< |
q.add(new Integer(N)); |
139 |
> |
q.add(new Integer(SIZE)); |
140 |
|
} catch (IllegalStateException success){ |
141 |
|
} |
142 |
|
} |
151 |
|
} |
152 |
|
public void testAddAll2(){ |
153 |
|
try { |
154 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
155 |
< |
Integer[] ints = new Integer[N]; |
154 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
155 |
> |
Integer[] ints = new Integer[SIZE]; |
156 |
|
q.addAll(Arrays.asList(ints)); |
157 |
|
fail("Cannot add null elements"); |
158 |
|
} |
160 |
|
} |
161 |
|
public void testAddAll3(){ |
162 |
|
try { |
163 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
164 |
< |
Integer[] ints = new Integer[N]; |
165 |
< |
for (int i = 0; i < N-1; ++i) |
163 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
164 |
> |
Integer[] ints = new Integer[SIZE]; |
165 |
> |
for (int i = 0; i < SIZE-1; ++i) |
166 |
|
ints[i] = new Integer(i); |
167 |
|
q.addAll(Arrays.asList(ints)); |
168 |
|
fail("Cannot add null elements"); |
172 |
|
public void testAddAll4(){ |
173 |
|
try { |
174 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
175 |
< |
Integer[] ints = new Integer[N]; |
176 |
< |
for (int i = 0; i < N; ++i) |
175 |
> |
Integer[] ints = new Integer[SIZE]; |
176 |
> |
for (int i = 0; i < SIZE; ++i) |
177 |
|
ints[i] = new Integer(i); |
178 |
|
q.addAll(Arrays.asList(ints)); |
179 |
|
fail("Cannot add with insufficient capacity"); |
183 |
|
public void testAddAll5(){ |
184 |
|
try { |
185 |
|
Integer[] empty = new Integer[0]; |
186 |
< |
Integer[] ints = new Integer[N]; |
187 |
< |
for (int i = 0; i < N; ++i) |
186 |
> |
Integer[] ints = new Integer[SIZE]; |
187 |
> |
for (int i = 0; i < SIZE; ++i) |
188 |
|
ints[i] = new Integer(i); |
189 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
189 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
190 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
191 |
|
assertTrue(q.addAll(Arrays.asList(ints))); |
192 |
< |
for (int i = 0; i < N; ++i) |
192 |
> |
for (int i = 0; i < SIZE; ++i) |
193 |
|
assertEquals(ints[i], q.poll()); |
194 |
|
} |
195 |
|
finally {} |
197 |
|
|
198 |
|
public void testPutNull() { |
199 |
|
try { |
200 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
200 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
201 |
|
q.put(null); |
202 |
|
fail("put should throw NPE"); |
203 |
|
} |
210 |
|
|
211 |
|
public void testPut() { |
212 |
|
try { |
213 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
214 |
< |
for (int i = 0; i < N; ++i) { |
213 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
214 |
> |
for (int i = 0; i < SIZE; ++i) { |
215 |
|
Integer I = new Integer(i); |
216 |
|
q.put(I); |
217 |
|
assertTrue(q.contains(I)); |
228 |
|
public void run() { |
229 |
|
int added = 0; |
230 |
|
try { |
231 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
232 |
< |
for (int i = 0; i < N; ++i) { |
231 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
232 |
> |
for (int i = 0; i < SIZE; ++i) { |
233 |
|
q.put(new Integer(i)); |
234 |
|
++added; |
235 |
|
} |
236 |
< |
q.put(new Integer(N)); |
237 |
< |
fail("put should block"); |
236 |
> |
q.put(new Integer(SIZE)); |
237 |
> |
threadFail("put should block"); |
238 |
|
} catch (InterruptedException ie){ |
239 |
< |
assertEquals(added, N); |
239 |
> |
threadAssertEquals(added, SIZE); |
240 |
|
} |
241 |
|
}}); |
242 |
|
t.start(); |
264 |
|
++added; |
265 |
|
q.put(new Object()); |
266 |
|
++added; |
267 |
< |
fail("Should block"); |
267 |
> |
threadFail("Should block"); |
268 |
|
} catch (InterruptedException e){ |
269 |
< |
assertTrue(added >= 2); |
269 |
> |
threadAssertTrue(added >= 2); |
270 |
|
} |
271 |
|
} |
272 |
|
}); |
288 |
|
try { |
289 |
|
q.put(new Object()); |
290 |
|
q.put(new Object()); |
291 |
< |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); |
291 |
> |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
292 |
|
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
293 |
< |
fail("Should block"); |
293 |
> |
threadFail("Should block"); |
294 |
|
} catch (InterruptedException success){} |
295 |
|
} |
296 |
|
}); |
297 |
|
|
298 |
|
try { |
299 |
|
t.start(); |
300 |
< |
Thread.sleep(SHORT_DELAY_MS); |
300 |
> |
Thread.sleep(SMALL_DELAY_MS); |
301 |
|
t.interrupt(); |
302 |
|
t.join(); |
303 |
|
} catch (Exception e){ |
307 |
|
|
308 |
|
public void testTake(){ |
309 |
|
try { |
310 |
< |
LinkedBlockingQueue q = fullQueue(N); |
311 |
< |
for (int i = 0; i < N; ++i) { |
310 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
311 |
> |
for (int i = 0; i < SIZE; ++i) { |
312 |
|
assertEquals(i, ((Integer)q.take()).intValue()); |
313 |
|
} |
314 |
|
} catch (InterruptedException e){ |
322 |
|
public void run(){ |
323 |
|
try { |
324 |
|
q.take(); |
325 |
< |
fail("Should block"); |
325 |
> |
threadFail("Should block"); |
326 |
|
} catch (InterruptedException success){ } |
327 |
|
} |
328 |
|
}); |
340 |
|
Thread t = new Thread(new Runnable() { |
341 |
|
public void run() { |
342 |
|
try { |
343 |
< |
LinkedBlockingQueue q = fullQueue(N); |
344 |
< |
for (int i = 0; i < N; ++i) { |
343 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
344 |
> |
for (int i = 0; i < SIZE; ++i) { |
345 |
|
assertEquals(i, ((Integer)q.take()).intValue()); |
346 |
|
} |
347 |
|
q.take(); |
348 |
< |
fail("take should block"); |
348 |
> |
threadFail("take should block"); |
349 |
|
} catch (InterruptedException success){ |
350 |
|
} |
351 |
|
}}); |
362 |
|
|
363 |
|
|
364 |
|
public void testPoll(){ |
365 |
< |
LinkedBlockingQueue q = fullQueue(N); |
366 |
< |
for (int i = 0; i < N; ++i) { |
365 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
366 |
> |
for (int i = 0; i < SIZE; ++i) { |
367 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
368 |
|
} |
369 |
|
assertNull(q.poll()); |
371 |
|
|
372 |
|
public void testTimedPoll0() { |
373 |
|
try { |
374 |
< |
LinkedBlockingQueue q = fullQueue(N); |
375 |
< |
for (int i = 0; i < N; ++i) { |
374 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
375 |
> |
for (int i = 0; i < SIZE; ++i) { |
376 |
|
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
377 |
|
} |
378 |
|
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
383 |
|
|
384 |
|
public void testTimedPoll() { |
385 |
|
try { |
386 |
< |
LinkedBlockingQueue q = fullQueue(N); |
387 |
< |
for (int i = 0; i < N; ++i) { |
386 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
387 |
> |
for (int i = 0; i < SIZE; ++i) { |
388 |
|
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
389 |
|
} |
390 |
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
397 |
|
Thread t = new Thread(new Runnable() { |
398 |
|
public void run() { |
399 |
|
try { |
400 |
< |
LinkedBlockingQueue q = fullQueue(N); |
401 |
< |
for (int i = 0; i < N; ++i) { |
402 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
400 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
401 |
> |
for (int i = 0; i < SIZE; ++i) { |
402 |
> |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
403 |
|
} |
404 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
404 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
405 |
|
} catch (InterruptedException success){ |
406 |
|
} |
407 |
|
}}); |
421 |
|
Thread t = new Thread(new Runnable() { |
422 |
|
public void run(){ |
423 |
|
try { |
424 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
424 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
425 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
426 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
427 |
< |
fail("Should block"); |
427 |
> |
threadFail("Should block"); |
428 |
|
} catch (InterruptedException success) { } |
429 |
|
} |
430 |
|
}); |
431 |
|
try { |
432 |
|
t.start(); |
433 |
< |
Thread.sleep(SHORT_DELAY_MS * 2); |
434 |
< |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
433 |
> |
Thread.sleep(SMALL_DELAY_MS); |
434 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
435 |
|
t.interrupt(); |
436 |
|
t.join(); |
437 |
|
} catch (Exception e){ |
441 |
|
|
442 |
|
|
443 |
|
public void testPeek(){ |
444 |
< |
LinkedBlockingQueue q = fullQueue(N); |
445 |
< |
for (int i = 0; i < N; ++i) { |
444 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
445 |
> |
for (int i = 0; i < SIZE; ++i) { |
446 |
|
assertEquals(i, ((Integer)q.peek()).intValue()); |
447 |
|
q.poll(); |
448 |
|
assertTrue(q.peek() == null || |
452 |
|
} |
453 |
|
|
454 |
|
public void testElement(){ |
455 |
< |
LinkedBlockingQueue q = fullQueue(N); |
456 |
< |
for (int i = 0; i < N; ++i) { |
455 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
456 |
> |
for (int i = 0; i < SIZE; ++i) { |
457 |
|
assertEquals(i, ((Integer)q.element()).intValue()); |
458 |
|
q.poll(); |
459 |
|
} |
465 |
|
} |
466 |
|
|
467 |
|
public void testRemove(){ |
468 |
< |
LinkedBlockingQueue q = fullQueue(N); |
469 |
< |
for (int i = 0; i < N; ++i) { |
468 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
469 |
> |
for (int i = 0; i < SIZE; ++i) { |
470 |
|
assertEquals(i, ((Integer)q.remove()).intValue()); |
471 |
|
} |
472 |
|
try { |
477 |
|
} |
478 |
|
|
479 |
|
public void testRemoveElement(){ |
480 |
< |
LinkedBlockingQueue q = fullQueue(N); |
481 |
< |
for (int i = 1; i < N; i+=2) { |
480 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
481 |
> |
for (int i = 1; i < SIZE; i+=2) { |
482 |
|
assertTrue(q.remove(new Integer(i))); |
483 |
|
} |
484 |
< |
for (int i = 0; i < N; i+=2) { |
484 |
> |
for (int i = 0; i < SIZE; i+=2) { |
485 |
|
assertTrue(q.remove(new Integer(i))); |
486 |
|
assertFalse(q.remove(new Integer(i+1))); |
487 |
|
} |
489 |
|
} |
490 |
|
|
491 |
|
public void testContains(){ |
492 |
< |
LinkedBlockingQueue q = fullQueue(N); |
493 |
< |
for (int i = 0; i < N; ++i) { |
492 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
493 |
> |
for (int i = 0; i < SIZE; ++i) { |
494 |
|
assertTrue(q.contains(new Integer(i))); |
495 |
|
q.poll(); |
496 |
|
assertFalse(q.contains(new Integer(i))); |
498 |
|
} |
499 |
|
|
500 |
|
public void testClear(){ |
501 |
< |
LinkedBlockingQueue q = fullQueue(N); |
501 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
502 |
|
q.clear(); |
503 |
|
assertTrue(q.isEmpty()); |
504 |
|
assertEquals(0, q.size()); |
505 |
< |
assertEquals(N, q.remainingCapacity()); |
506 |
< |
q.add(new Integer(1)); |
505 |
> |
assertEquals(SIZE, q.remainingCapacity()); |
506 |
> |
q.add(one); |
507 |
|
assertFalse(q.isEmpty()); |
508 |
|
q.clear(); |
509 |
|
assertTrue(q.isEmpty()); |
510 |
|
} |
511 |
|
|
512 |
|
public void testContainsAll(){ |
513 |
< |
LinkedBlockingQueue q = fullQueue(N); |
514 |
< |
LinkedBlockingQueue p = new LinkedBlockingQueue(N); |
515 |
< |
for (int i = 0; i < N; ++i) { |
513 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
514 |
> |
LinkedBlockingQueue p = new LinkedBlockingQueue(SIZE); |
515 |
> |
for (int i = 0; i < SIZE; ++i) { |
516 |
|
assertTrue(q.containsAll(p)); |
517 |
|
assertFalse(p.containsAll(q)); |
518 |
|
p.add(new Integer(i)); |
521 |
|
} |
522 |
|
|
523 |
|
public void testRetainAll(){ |
524 |
< |
LinkedBlockingQueue q = fullQueue(N); |
525 |
< |
LinkedBlockingQueue p = fullQueue(N); |
526 |
< |
for (int i = 0; i < N; ++i) { |
524 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
525 |
> |
LinkedBlockingQueue p = populatedQueue(SIZE); |
526 |
> |
for (int i = 0; i < SIZE; ++i) { |
527 |
|
boolean changed = q.retainAll(p); |
528 |
|
if (i == 0) |
529 |
|
assertFalse(changed); |
531 |
|
assertTrue(changed); |
532 |
|
|
533 |
|
assertTrue(q.containsAll(p)); |
534 |
< |
assertEquals(N-i, q.size()); |
534 |
> |
assertEquals(SIZE-i, q.size()); |
535 |
|
p.remove(); |
536 |
|
} |
537 |
|
} |
538 |
|
|
539 |
|
public void testRemoveAll(){ |
540 |
< |
for (int i = 1; i < N; ++i) { |
541 |
< |
LinkedBlockingQueue q = fullQueue(N); |
542 |
< |
LinkedBlockingQueue p = fullQueue(i); |
540 |
> |
for (int i = 1; i < SIZE; ++i) { |
541 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
542 |
> |
LinkedBlockingQueue p = populatedQueue(i); |
543 |
|
assertTrue(q.removeAll(p)); |
544 |
< |
assertEquals(N-i, q.size()); |
544 |
> |
assertEquals(SIZE-i, q.size()); |
545 |
|
for (int j = 0; j < i; ++j) { |
546 |
|
Integer I = (Integer)(p.remove()); |
547 |
|
assertFalse(q.contains(I)); |
551 |
|
|
552 |
|
|
553 |
|
public void testToArray(){ |
554 |
< |
LinkedBlockingQueue q = fullQueue(N); |
554 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
555 |
|
Object[] o = q.toArray(); |
556 |
|
try { |
557 |
|
for(int i = 0; i < o.length; i++) |
562 |
|
} |
563 |
|
|
564 |
|
public void testToArray2(){ |
565 |
< |
LinkedBlockingQueue q = fullQueue(N); |
566 |
< |
Integer[] ints = new Integer[N]; |
565 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
566 |
> |
Integer[] ints = new Integer[SIZE]; |
567 |
|
ints = (Integer[])q.toArray(ints); |
568 |
|
try { |
569 |
|
for(int i = 0; i < ints.length; i++) |
574 |
|
} |
575 |
|
|
576 |
|
public void testIterator(){ |
577 |
< |
LinkedBlockingQueue q = fullQueue(N); |
577 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
578 |
|
Iterator it = q.iterator(); |
579 |
|
try { |
580 |
|
while(it.hasNext()){ |
589 |
|
|
590 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
591 |
|
|
592 |
< |
q.add(new Integer(1)); |
593 |
< |
q.add(new Integer(2)); |
594 |
< |
q.add(new Integer(3)); |
592 |
> |
q.add(one); |
593 |
> |
q.add(two); |
594 |
> |
q.add(three); |
595 |
|
|
596 |
|
assertEquals("queue should be full", 0, q.remainingCapacity()); |
597 |
|
|
608 |
|
|
609 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
610 |
|
|
611 |
< |
q.add(new Integer(1)); |
612 |
< |
q.add(new Integer(2)); |
613 |
< |
q.add(new Integer(3)); |
611 |
> |
q.add(one); |
612 |
> |
q.add(two); |
613 |
> |
q.add(three); |
614 |
|
|
615 |
|
try { |
616 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
627 |
|
|
628 |
|
|
629 |
|
public void testToString(){ |
630 |
< |
LinkedBlockingQueue q = fullQueue(N); |
630 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
631 |
|
String s = q.toString(); |
632 |
< |
for (int i = 0; i < N; ++i) { |
632 |
> |
for (int i = 0; i < SIZE; ++i) { |
633 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
634 |
|
} |
635 |
|
} |
639 |
|
|
640 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
641 |
|
|
642 |
< |
q.add(new Integer(1)); |
643 |
< |
q.add(new Integer(2)); |
642 |
> |
q.add(one); |
643 |
> |
q.add(two); |
644 |
|
|
645 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
646 |
|
|
647 |
|
executor.execute(new Runnable() { |
648 |
|
public void run() { |
649 |
< |
assertFalse("offer should be rejected", q.offer(new Integer(3))); |
649 |
> |
threadAssertFalse(q.offer(three)); |
650 |
|
try { |
651 |
< |
assertTrue("offer should be accepted", q.offer(new Integer(3), MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); |
652 |
< |
assertEquals(0, q.remainingCapacity()); |
651 |
> |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
652 |
> |
threadAssertEquals(0, q.remainingCapacity()); |
653 |
|
} |
654 |
|
catch (InterruptedException e) { |
655 |
< |
fail("should not be interrupted"); |
655 |
> |
threadFail("should not be interrupted"); |
656 |
|
} |
657 |
|
} |
658 |
|
}); |
660 |
|
executor.execute(new Runnable() { |
661 |
|
public void run() { |
662 |
|
try { |
663 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
664 |
< |
assertEquals("first item in queue should be 1", new Integer(1), q.take()); |
663 |
> |
Thread.sleep(SMALL_DELAY_MS); |
664 |
> |
threadAssertEquals(one, q.take()); |
665 |
|
} |
666 |
|
catch (InterruptedException e) { |
667 |
< |
fail("should not be interrupted"); |
667 |
> |
threadFail("should not be interrupted"); |
668 |
|
} |
669 |
|
} |
670 |
|
}); |
671 |
|
|
672 |
< |
executor.shutdown(); |
672 |
> |
joinPool(executor); |
673 |
|
|
674 |
|
} |
675 |
|
|
681 |
|
|
682 |
|
executor.execute(new Runnable() { |
683 |
|
public void run() { |
684 |
< |
assertNull("poll should fail", q.poll()); |
684 |
> |
threadAssertNull(q.poll()); |
685 |
|
try { |
686 |
< |
assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); |
687 |
< |
assertTrue(q.isEmpty()); |
686 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
687 |
> |
threadAssertTrue(q.isEmpty()); |
688 |
|
} |
689 |
|
catch (InterruptedException e) { |
690 |
< |
fail("should not be interrupted"); |
690 |
> |
threadFail("should not be interrupted"); |
691 |
|
} |
692 |
|
} |
693 |
|
}); |
695 |
|
executor.execute(new Runnable() { |
696 |
|
public void run() { |
697 |
|
try { |
698 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
699 |
< |
q.put(new Integer(1)); |
698 |
> |
Thread.sleep(SMALL_DELAY_MS); |
699 |
> |
q.put(one); |
700 |
|
} |
701 |
|
catch (InterruptedException e) { |
702 |
< |
fail("should not be interrupted"); |
702 |
> |
threadFail("should not be interrupted"); |
703 |
|
} |
704 |
|
} |
705 |
|
}); |
706 |
|
|
707 |
< |
executor.shutdown(); |
712 |
< |
|
707 |
> |
joinPool(executor); |
708 |
|
} |
709 |
|
|
710 |
|
public void testSerialization() { |
711 |
< |
LinkedBlockingQueue q = fullQueue(N); |
711 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
712 |
|
|
713 |
|
try { |
714 |
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |