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 John Vint |
6 |
|
*/ |
7 |
|
|
40 |
|
new Generic().testSuite()); |
41 |
|
} |
42 |
|
|
43 |
– |
void checkEmpty(BlockingQueue q) { |
44 |
– |
try { |
45 |
– |
assertTrue(q.isEmpty()); |
46 |
– |
assertEquals(0, q.size()); |
47 |
– |
assertNull(q.peek()); |
48 |
– |
assertNull(q.poll()); |
49 |
– |
assertNull(q.poll(0, MILLISECONDS)); |
50 |
– |
assertEquals(q.toString(), "[]"); |
51 |
– |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
52 |
– |
assertFalse(q.iterator().hasNext()); |
53 |
– |
try { |
54 |
– |
q.element(); |
55 |
– |
shouldThrow(); |
56 |
– |
} catch (NoSuchElementException success) {} |
57 |
– |
try { |
58 |
– |
q.iterator().next(); |
59 |
– |
shouldThrow(); |
60 |
– |
} catch (NoSuchElementException success) {} |
61 |
– |
try { |
62 |
– |
q.remove(); |
63 |
– |
shouldThrow(); |
64 |
– |
} catch (NoSuchElementException success) {} |
65 |
– |
} catch (InterruptedException ie) { |
66 |
– |
threadUnexpectedException(ie); |
67 |
– |
} |
68 |
– |
} |
69 |
– |
|
43 |
|
/** |
44 |
|
* Constructor builds new queue with size being zero and empty |
45 |
|
* being true |
421 |
|
*/ |
422 |
|
public void testRemoveElement() throws InterruptedException { |
423 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
424 |
< |
for (int i = 1; i < SIZE; i += 2) { |
424 |
> |
for (int i = 1; i < SIZE; i+=2) { |
425 |
> |
assertTrue(q.contains(i)); |
426 |
|
assertTrue(q.remove(i)); |
427 |
+ |
assertFalse(q.contains(i)); |
428 |
+ |
assertTrue(q.contains(i-1)); |
429 |
|
} |
430 |
< |
for (int i = 0; i < SIZE; i += 2) { |
430 |
> |
for (int i = 0; i < SIZE; i+=2) { |
431 |
> |
assertTrue(q.contains(i)); |
432 |
|
assertTrue(q.remove(i)); |
433 |
< |
assertFalse(q.remove(i + 1)); |
433 |
> |
assertFalse(q.contains(i)); |
434 |
> |
assertFalse(q.remove(i+1)); |
435 |
> |
assertFalse(q.contains(i+1)); |
436 |
|
} |
437 |
|
checkEmpty(q); |
438 |
|
} |
1051 |
|
} |
1052 |
|
|
1053 |
|
/** |
1054 |
< |
* tryTransfer waits the amount given if interrupted, and |
1055 |
< |
* throws interrupted exception |
1054 |
> |
* tryTransfer waits the amount given, and throws |
1055 |
> |
* InterruptedException when interrupted. |
1056 |
|
*/ |
1057 |
|
public void testTryTransfer5() throws InterruptedException { |
1058 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1059 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1060 |
+ |
assertTrue(q.isEmpty()); |
1061 |
|
|
1062 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1063 |
|
public void realRun() throws InterruptedException { |
1068 |
|
shouldThrow(); |
1069 |
|
} catch (InterruptedException success) {} |
1070 |
|
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1071 |
+ |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
1072 |
|
}}); |
1073 |
|
|
1074 |
|
threadStarted.await(); |
1075 |
+ |
while (q.isEmpty()) |
1076 |
+ |
Thread.yield(); |
1077 |
|
Thread.sleep(SHORT_DELAY_MS); |
1078 |
|
t.interrupt(); |
1079 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
1123 |
|
} |
1124 |
|
|
1125 |
|
/** |
1126 |
< |
* tryTransfer attempts to enqueue into the q and fails returning |
1127 |
< |
* false not enqueueing and the successive poll is null |
1126 |
> |
* tryTransfer attempts to enqueue into the queue and fails |
1127 |
> |
* returning false not enqueueing and the successive poll is null |
1128 |
|
*/ |
1129 |
|
public void testTryTransfer8() throws InterruptedException { |
1130 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |