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 |
|
|
8 |
< |
import java.io.BufferedInputStream; |
9 |
< |
import java.io.BufferedOutputStream; |
10 |
< |
import java.io.ByteArrayInputStream; |
11 |
< |
import java.io.ByteArrayOutputStream; |
12 |
< |
import java.io.ObjectInputStream; |
13 |
< |
import java.io.ObjectOutputStream; |
8 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
9 |
> |
|
10 |
|
import java.util.ArrayList; |
11 |
|
import java.util.Arrays; |
12 |
+ |
import java.util.Collection; |
13 |
|
import java.util.Iterator; |
14 |
|
import java.util.List; |
15 |
|
import java.util.NoSuchElementException; |
16 |
< |
import java.util.concurrent.*; |
17 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
16 |
> |
import java.util.Queue; |
17 |
> |
import java.util.concurrent.BlockingQueue; |
18 |
> |
import java.util.concurrent.CountDownLatch; |
19 |
> |
import java.util.concurrent.Executors; |
20 |
> |
import java.util.concurrent.ExecutorService; |
21 |
> |
import java.util.concurrent.LinkedTransferQueue; |
22 |
> |
|
23 |
|
import junit.framework.Test; |
22 |
– |
import junit.framework.TestSuite; |
24 |
|
|
25 |
|
@SuppressWarnings({"unchecked", "rawtypes"}) |
26 |
|
public class LinkedTransferQueueTest extends JSR166TestCase { |
27 |
+ |
static class Implementation implements CollectionImplementation { |
28 |
+ |
public Class<?> klazz() { return LinkedTransferQueue.class; } |
29 |
+ |
public Collection emptyCollection() { return new LinkedTransferQueue(); } |
30 |
+ |
public Object makeElement(int i) { return i; } |
31 |
+ |
public boolean isConcurrent() { return true; } |
32 |
+ |
public boolean permitsNulls() { return false; } |
33 |
+ |
} |
34 |
|
|
35 |
< |
public static void main(String[] args) { |
36 |
< |
junit.textui.TestRunner.run(suite()); |
35 |
> |
public static class Generic extends BlockingQueueTest { |
36 |
> |
protected BlockingQueue emptyCollection() { |
37 |
> |
return new LinkedTransferQueue(); |
38 |
> |
} |
39 |
|
} |
40 |
|
|
41 |
< |
public static Test suite() { |
42 |
< |
return new TestSuite(LinkedTransferQueueTest.class); |
41 |
> |
public static void main(String[] args) { |
42 |
> |
main(suite(), args); |
43 |
|
} |
44 |
|
|
45 |
< |
void checkEmpty(LinkedTransferQueue q) throws InterruptedException { |
46 |
< |
assertTrue(q.isEmpty()); |
47 |
< |
assertEquals(0, q.size()); |
48 |
< |
assertNull(q.peek()); |
39 |
< |
assertNull(q.poll()); |
40 |
< |
assertNull(q.poll(0, MILLISECONDS)); |
41 |
< |
assertEquals(q.toString(), "[]"); |
42 |
< |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
43 |
< |
assertFalse(q.iterator().hasNext()); |
44 |
< |
try { |
45 |
< |
q.element(); |
46 |
< |
shouldThrow(); |
47 |
< |
} catch (NoSuchElementException success) {} |
48 |
< |
try { |
49 |
< |
q.iterator().next(); |
50 |
< |
shouldThrow(); |
51 |
< |
} catch (NoSuchElementException success) {} |
52 |
< |
try { |
53 |
< |
q.remove(); |
54 |
< |
shouldThrow(); |
55 |
< |
} catch (NoSuchElementException success) {} |
45 |
> |
public static Test suite() { |
46 |
> |
return newTestSuite(LinkedTransferQueueTest.class, |
47 |
> |
new Generic().testSuite(), |
48 |
> |
CollectionTest.testSuite(new Implementation())); |
49 |
|
} |
50 |
|
|
51 |
|
/** |
73 |
|
* NullPointerException |
74 |
|
*/ |
75 |
|
public void testConstructor3() { |
76 |
+ |
Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
77 |
|
try { |
78 |
< |
Integer[] ints = new Integer[SIZE]; |
85 |
< |
new LinkedTransferQueue(Arrays.asList(ints)); |
78 |
> |
new LinkedTransferQueue(elements); |
79 |
|
shouldThrow(); |
80 |
|
} catch (NullPointerException success) {} |
81 |
|
} |
85 |
|
* throws NullPointerException |
86 |
|
*/ |
87 |
|
public void testConstructor4() { |
88 |
+ |
Integer[] ints = new Integer[SIZE]; |
89 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
90 |
+ |
ints[i] = i; |
91 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
92 |
|
try { |
93 |
< |
Integer[] ints = new Integer[SIZE]; |
97 |
< |
for (int i = 0; i < SIZE - 1; ++i) { |
98 |
< |
ints[i] = i; |
99 |
< |
} |
100 |
< |
new LinkedTransferQueue(Arrays.asList(ints)); |
93 |
> |
new LinkedTransferQueue(elements); |
94 |
|
shouldThrow(); |
95 |
|
} catch (NullPointerException success) {} |
96 |
|
} |
123 |
|
* remainingCapacity() always returns Integer.MAX_VALUE |
124 |
|
*/ |
125 |
|
public void testRemainingCapacity() { |
126 |
< |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
126 |
> |
BlockingQueue q = populatedQueue(SIZE); |
127 |
|
for (int i = 0; i < SIZE; ++i) { |
128 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
129 |
|
assertEquals(SIZE - i, q.size()); |
130 |
< |
q.remove(); |
130 |
> |
assertEquals(i, q.remove()); |
131 |
|
} |
132 |
|
for (int i = 0; i < SIZE; ++i) { |
133 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
134 |
|
assertEquals(i, q.size()); |
135 |
< |
q.add(i); |
135 |
> |
assertTrue(q.add(i)); |
136 |
|
} |
137 |
|
} |
138 |
|
|
139 |
|
/** |
147 |
– |
* offer(null) throws NullPointerException |
148 |
– |
*/ |
149 |
– |
public void testOfferNull() { |
150 |
– |
try { |
151 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
152 |
– |
q.offer(null); |
153 |
– |
shouldThrow(); |
154 |
– |
} catch (NullPointerException success) {} |
155 |
– |
} |
156 |
– |
|
157 |
– |
/** |
158 |
– |
* add(null) throws NullPointerException |
159 |
– |
*/ |
160 |
– |
public void testAddNull() { |
161 |
– |
try { |
162 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
163 |
– |
q.add(null); |
164 |
– |
shouldThrow(); |
165 |
– |
} catch (NullPointerException success) {} |
166 |
– |
} |
167 |
– |
|
168 |
– |
/** |
169 |
– |
* addAll(null) throws NullPointerException |
170 |
– |
*/ |
171 |
– |
public void testAddAll1() { |
172 |
– |
try { |
173 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
174 |
– |
q.addAll(null); |
175 |
– |
shouldThrow(); |
176 |
– |
} catch (NullPointerException success) {} |
177 |
– |
} |
178 |
– |
|
179 |
– |
/** |
140 |
|
* addAll(this) throws IllegalArgumentException |
141 |
|
*/ |
142 |
|
public void testAddAllSelf() { |
143 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
144 |
|
try { |
184 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
145 |
|
q.addAll(q); |
146 |
|
shouldThrow(); |
147 |
|
} catch (IllegalArgumentException success) {} |
148 |
|
} |
149 |
|
|
150 |
|
/** |
191 |
– |
* addAll of a collection with null elements throws NullPointerException |
192 |
– |
*/ |
193 |
– |
public void testAddAll2() { |
194 |
– |
try { |
195 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
196 |
– |
Integer[] ints = new Integer[SIZE]; |
197 |
– |
q.addAll(Arrays.asList(ints)); |
198 |
– |
shouldThrow(); |
199 |
– |
} catch (NullPointerException success) {} |
200 |
– |
} |
201 |
– |
|
202 |
– |
/** |
151 |
|
* addAll of a collection with any null elements throws |
152 |
|
* NullPointerException after possibly adding some elements |
153 |
|
*/ |
154 |
|
public void testAddAll3() { |
155 |
+ |
LinkedTransferQueue q = new LinkedTransferQueue(); |
156 |
+ |
Integer[] ints = new Integer[SIZE]; |
157 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
158 |
+ |
ints[i] = i; |
159 |
|
try { |
208 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
209 |
– |
Integer[] ints = new Integer[SIZE]; |
210 |
– |
for (int i = 0; i < SIZE - 1; ++i) { |
211 |
– |
ints[i] = i; |
212 |
– |
} |
160 |
|
q.addAll(Arrays.asList(ints)); |
161 |
|
shouldThrow(); |
162 |
|
} catch (NullPointerException success) {} |
180 |
|
} |
181 |
|
|
182 |
|
/** |
236 |
– |
* put(null) throws NullPointerException |
237 |
– |
*/ |
238 |
– |
public void testPutNull() throws InterruptedException { |
239 |
– |
try { |
240 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
241 |
– |
q.put(null); |
242 |
– |
shouldThrow(); |
243 |
– |
} catch (NullPointerException success) {} |
244 |
– |
} |
245 |
– |
|
246 |
– |
/** |
183 |
|
* all elements successfully put are contained |
184 |
|
*/ |
185 |
|
public void testPut() { |
186 |
|
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
187 |
|
for (int i = 0; i < SIZE; ++i) { |
188 |
< |
assertEquals(q.size(), i); |
188 |
> |
assertEquals(i, q.size()); |
189 |
|
q.put(i); |
190 |
|
assertTrue(q.contains(i)); |
191 |
|
} |
202 |
|
} |
203 |
|
|
204 |
|
/** |
205 |
< |
* take blocks interruptibly when empty |
270 |
< |
*/ |
271 |
< |
public void testTakeFromEmpty() throws InterruptedException { |
272 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
273 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
274 |
< |
void realRun() throws InterruptedException { |
275 |
< |
q.take(); |
276 |
< |
}}); |
277 |
< |
Thread.sleep(SHORT_DELAY_MS); |
278 |
< |
t.interrupt(); |
279 |
< |
t.join(); |
280 |
< |
} |
281 |
< |
|
282 |
< |
/** |
283 |
< |
* Take removes existing elements until empty, then blocks interruptibly |
205 |
> |
* take removes existing elements until empty, then blocks interruptibly |
206 |
|
*/ |
207 |
|
public void testBlockingTake() throws InterruptedException { |
208 |
< |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
209 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
210 |
< |
void realRun() throws InterruptedException { |
208 |
> |
final BlockingQueue q = populatedQueue(SIZE); |
209 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
210 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
211 |
> |
public void realRun() throws InterruptedException { |
212 |
|
for (int i = 0; i < SIZE; ++i) { |
213 |
< |
threadAssertEquals(i, (int) q.take()); |
213 |
> |
assertEquals(i, q.take()); |
214 |
|
} |
215 |
< |
q.take(); |
215 |
> |
|
216 |
> |
Thread.currentThread().interrupt(); |
217 |
> |
try { |
218 |
> |
q.take(); |
219 |
> |
shouldThrow(); |
220 |
> |
} catch (InterruptedException success) {} |
221 |
> |
assertFalse(Thread.interrupted()); |
222 |
> |
|
223 |
> |
pleaseInterrupt.countDown(); |
224 |
> |
try { |
225 |
> |
q.take(); |
226 |
> |
shouldThrow(); |
227 |
> |
} catch (InterruptedException success) {} |
228 |
> |
assertFalse(Thread.interrupted()); |
229 |
|
}}); |
230 |
< |
Thread.sleep(SMALL_DELAY_MS); |
230 |
> |
|
231 |
> |
await(pleaseInterrupt); |
232 |
> |
assertThreadStaysAlive(t); |
233 |
|
t.interrupt(); |
234 |
< |
t.join(); |
297 |
< |
checkEmpty(q); |
234 |
> |
awaitTermination(t); |
235 |
|
} |
236 |
|
|
237 |
|
/** |
247 |
|
} |
248 |
|
|
249 |
|
/** |
250 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
250 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
251 |
|
*/ |
252 |
|
public void testTimedPoll0() throws InterruptedException { |
253 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
259 |
|
} |
260 |
|
|
261 |
|
/** |
262 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
262 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
263 |
|
*/ |
264 |
|
public void testTimedPoll() throws InterruptedException { |
265 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
266 |
|
for (int i = 0; i < SIZE; ++i) { |
267 |
< |
long t0 = System.nanoTime(); |
267 |
> |
long startTime = System.nanoTime(); |
268 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
269 |
< |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
333 |
< |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
269 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
270 |
|
} |
271 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
271 |
> |
long startTime = System.nanoTime(); |
272 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
273 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
274 |
|
checkEmpty(q); |
275 |
|
} |
276 |
|
|
279 |
|
* returning timeout status |
280 |
|
*/ |
281 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
282 |
< |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
282 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
283 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
284 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
285 |
< |
void realRun() throws InterruptedException { |
285 |
> |
public void realRun() throws InterruptedException { |
286 |
> |
long startTime = System.nanoTime(); |
287 |
|
for (int i = 0; i < SIZE; ++i) { |
288 |
< |
long t0 = System.nanoTime(); |
349 |
< |
threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS, |
350 |
< |
MILLISECONDS)); |
351 |
< |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
352 |
< |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
288 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
289 |
|
} |
290 |
+ |
aboutToWait.countDown(); |
291 |
|
try { |
292 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
293 |
|
shouldThrow(); |
294 |
< |
} catch (InterruptedException success) {} |
294 |
> |
} catch (InterruptedException success) { |
295 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
296 |
> |
} |
297 |
|
}}); |
298 |
|
|
299 |
< |
Thread.sleep(SMALL_DELAY_MS); |
299 |
> |
aboutToWait.await(); |
300 |
> |
waitForThreadToEnterWaitState(t, LONG_DELAY_MS); |
301 |
|
t.interrupt(); |
302 |
< |
t.join(); |
302 |
> |
awaitTermination(t); |
303 |
|
checkEmpty(q); |
304 |
|
} |
305 |
|
|
306 |
|
/** |
307 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
308 |
< |
* on interruption throws |
307 |
> |
* timed poll after thread interrupted throws InterruptedException |
308 |
> |
* instead of returning timeout status |
309 |
|
*/ |
310 |
< |
public void testTimedPollWithOffer() throws InterruptedException { |
311 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
312 |
< |
Thread t = new Thread(new CheckedRunnable() { |
313 |
< |
void realRun() throws InterruptedException { |
314 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
315 |
< |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
310 |
> |
public void testTimedPollAfterInterrupt() throws InterruptedException { |
311 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
312 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
313 |
> |
public void realRun() throws InterruptedException { |
314 |
> |
Thread.currentThread().interrupt(); |
315 |
> |
for (int i = 0; i < SIZE; ++i) { |
316 |
> |
long t0 = System.nanoTime(); |
317 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
318 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
319 |
> |
} |
320 |
|
try { |
321 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
321 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
322 |
|
shouldThrow(); |
323 |
|
} catch (InterruptedException success) {} |
324 |
|
}}); |
325 |
|
|
326 |
< |
Thread.sleep(SMALL_DELAY_MS); |
327 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
384 |
< |
t.interrupt(); |
385 |
< |
t.join(); |
326 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
327 |
> |
checkEmpty(q); |
328 |
|
} |
329 |
|
|
330 |
|
/** |
374 |
|
} |
375 |
|
|
376 |
|
/** |
435 |
– |
* remove(x) removes x and returns true if present |
436 |
– |
*/ |
437 |
– |
public void testRemoveElement() throws InterruptedException { |
438 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
439 |
– |
for (int i = 1; i < SIZE; i += 2) { |
440 |
– |
assertTrue(q.remove(i)); |
441 |
– |
} |
442 |
– |
for (int i = 0; i < SIZE; i += 2) { |
443 |
– |
assertTrue(q.remove(i)); |
444 |
– |
assertFalse(q.remove(i + 1)); |
445 |
– |
} |
446 |
– |
checkEmpty(q); |
447 |
– |
} |
448 |
– |
|
449 |
– |
/** |
377 |
|
* An add following remove(x) succeeds |
378 |
|
*/ |
379 |
|
public void testRemoveElementAndAdd() throws InterruptedException { |
383 |
|
assertTrue(q.remove(one)); |
384 |
|
assertTrue(q.remove(two)); |
385 |
|
assertTrue(q.add(three)); |
386 |
< |
assertTrue(q.take() == three); |
386 |
> |
assertSame(q.take(), three); |
387 |
|
} |
388 |
|
|
389 |
|
/** |
465 |
|
} |
466 |
|
|
467 |
|
/** |
468 |
< |
* toArray() contains all elements |
468 |
> |
* toArray() contains all elements in FIFO order |
469 |
|
*/ |
470 |
< |
public void testToArray() throws InterruptedException { |
470 |
> |
public void testToArray() { |
471 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
472 |
|
Object[] o = q.toArray(); |
473 |
|
for (int i = 0; i < o.length; i++) { |
474 |
< |
assertEquals(o[i], q.take()); |
474 |
> |
assertSame(o[i], q.poll()); |
475 |
|
} |
476 |
|
} |
477 |
|
|
478 |
|
/** |
479 |
< |
* toArray(a) contains all elements |
479 |
> |
* toArray(a) contains all elements in FIFO order |
480 |
|
*/ |
481 |
< |
public void testToArray2() throws InterruptedException { |
481 |
> |
public void testToArray2() { |
482 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
483 |
|
Integer[] ints = new Integer[SIZE]; |
484 |
< |
ints = q.toArray(ints); |
484 |
> |
Integer[] array = q.toArray(ints); |
485 |
> |
assertSame(ints, array); |
486 |
|
for (int i = 0; i < ints.length; i++) { |
487 |
< |
assertEquals(ints[i], q.take()); |
487 |
> |
assertSame(ints[i], q.poll()); |
488 |
|
} |
489 |
|
} |
490 |
|
|
491 |
|
/** |
492 |
< |
* toArray(null) throws NullPointerException |
565 |
< |
*/ |
566 |
< |
public void testToArray_BadArg() { |
567 |
< |
try { |
568 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
569 |
< |
Object o[] = q.toArray(null); |
570 |
< |
shouldThrow(); |
571 |
< |
} catch (NullPointerException success) {} |
572 |
< |
} |
573 |
< |
|
574 |
< |
/** |
575 |
< |
* toArray(incompatible array type) throws CCE |
492 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
493 |
|
*/ |
494 |
|
public void testToArray1_BadArg() { |
495 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
496 |
|
try { |
497 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
580 |
< |
Object o[] = q.toArray(new String[10]); |
497 |
> |
q.toArray(new String[10]); |
498 |
|
shouldThrow(); |
499 |
|
} catch (ArrayStoreException success) {} |
500 |
|
} |
505 |
|
public void testIterator() throws InterruptedException { |
506 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
507 |
|
Iterator it = q.iterator(); |
508 |
< |
int i = 0; |
509 |
< |
while (it.hasNext()) { |
510 |
< |
assertEquals(it.next(), i++); |
511 |
< |
} |
508 |
> |
int i; |
509 |
> |
for (i = 0; it.hasNext(); i++) |
510 |
> |
assertTrue(q.contains(it.next())); |
511 |
> |
assertEquals(i, SIZE); |
512 |
> |
assertIteratorExhausted(it); |
513 |
> |
|
514 |
> |
it = q.iterator(); |
515 |
> |
for (i = 0; it.hasNext(); i++) |
516 |
> |
assertEquals(it.next(), q.take()); |
517 |
|
assertEquals(i, SIZE); |
518 |
+ |
assertIteratorExhausted(it); |
519 |
+ |
} |
520 |
+ |
|
521 |
+ |
/** |
522 |
+ |
* iterator of empty collection has no elements |
523 |
+ |
*/ |
524 |
+ |
public void testEmptyIterator() { |
525 |
+ |
assertIteratorExhausted(new LinkedTransferQueue().iterator()); |
526 |
|
} |
527 |
|
|
528 |
|
/** |
539 |
|
it.remove(); |
540 |
|
|
541 |
|
it = q.iterator(); |
542 |
< |
assertEquals(it.next(), one); |
543 |
< |
assertEquals(it.next(), three); |
542 |
> |
assertSame(it.next(), one); |
543 |
> |
assertSame(it.next(), three); |
544 |
|
assertFalse(it.hasNext()); |
545 |
|
} |
546 |
|
|
584 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
585 |
|
String s = q.toString(); |
586 |
|
for (int i = 0; i < SIZE; ++i) { |
587 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
587 |
> |
assertTrue(s.contains(String.valueOf(i))); |
588 |
|
} |
589 |
|
} |
590 |
|
|
593 |
|
*/ |
594 |
|
public void testOfferInExecutor() { |
595 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
596 |
< |
q.add(one); |
597 |
< |
q.add(two); |
598 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
599 |
< |
|
600 |
< |
executor.execute(new CheckedRunnable() { |
601 |
< |
void realRun() { |
602 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, |
603 |
< |
MILLISECONDS)); |
604 |
< |
}}); |
675 |
< |
|
676 |
< |
executor.execute(new CheckedRunnable() { |
677 |
< |
void realRun() throws InterruptedException { |
678 |
< |
Thread.sleep(SMALL_DELAY_MS); |
679 |
< |
threadAssertEquals(one, q.take()); |
680 |
< |
}}); |
596 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
597 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
598 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
599 |
> |
|
600 |
> |
executor.execute(new CheckedRunnable() { |
601 |
> |
public void realRun() throws InterruptedException { |
602 |
> |
threadsStarted.await(); |
603 |
> |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
604 |
> |
}}); |
605 |
|
|
606 |
< |
joinPool(executor); |
606 |
> |
executor.execute(new CheckedRunnable() { |
607 |
> |
public void realRun() throws InterruptedException { |
608 |
> |
threadsStarted.await(); |
609 |
> |
assertSame(one, q.take()); |
610 |
> |
checkEmpty(q); |
611 |
> |
}}); |
612 |
> |
} |
613 |
|
} |
614 |
|
|
615 |
|
/** |
617 |
|
*/ |
618 |
|
public void testPollInExecutor() { |
619 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
620 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
621 |
< |
|
622 |
< |
executor.execute(new CheckedRunnable() { |
623 |
< |
void realRun() throws InterruptedException { |
624 |
< |
threadAssertNull(q.poll()); |
625 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, |
626 |
< |
MILLISECONDS)); |
627 |
< |
threadAssertTrue(q.isEmpty()); |
628 |
< |
}}); |
629 |
< |
|
630 |
< |
executor.execute(new CheckedRunnable() { |
701 |
< |
void realRun() throws InterruptedException { |
702 |
< |
Thread.sleep(SMALL_DELAY_MS); |
703 |
< |
q.put(one); |
704 |
< |
}}); |
620 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
621 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
622 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
623 |
> |
|
624 |
> |
executor.execute(new CheckedRunnable() { |
625 |
> |
public void realRun() throws InterruptedException { |
626 |
> |
assertNull(q.poll()); |
627 |
> |
threadsStarted.await(); |
628 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
629 |
> |
checkEmpty(q); |
630 |
> |
}}); |
631 |
|
|
632 |
< |
joinPool(executor); |
632 |
> |
executor.execute(new CheckedRunnable() { |
633 |
> |
public void realRun() throws InterruptedException { |
634 |
> |
threadsStarted.await(); |
635 |
> |
q.put(one); |
636 |
> |
}}); |
637 |
> |
} |
638 |
|
} |
639 |
|
|
640 |
|
/** |
641 |
|
* A deserialized serialized queue has same elements in same order |
642 |
|
*/ |
643 |
|
public void testSerialization() throws Exception { |
644 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
645 |
< |
|
715 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
716 |
< |
ObjectOutputStream out |
717 |
< |
= new ObjectOutputStream(new BufferedOutputStream(bout)); |
718 |
< |
out.writeObject(q); |
719 |
< |
out.close(); |
644 |
> |
Queue x = populatedQueue(SIZE); |
645 |
> |
Queue y = serialClone(x); |
646 |
|
|
647 |
< |
ByteArrayInputStream bin |
648 |
< |
= new ByteArrayInputStream(bout.toByteArray()); |
649 |
< |
ObjectInputStream in |
650 |
< |
= new ObjectInputStream(new BufferedInputStream(bin)); |
651 |
< |
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
652 |
< |
|
653 |
< |
assertEquals(q.size(), r.size()); |
728 |
< |
while (!q.isEmpty()) { |
729 |
< |
assertEquals(q.remove(), r.remove()); |
647 |
> |
assertNotSame(y, x); |
648 |
> |
assertEquals(x.size(), y.size()); |
649 |
> |
assertEquals(x.toString(), y.toString()); |
650 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
651 |
> |
while (!x.isEmpty()) { |
652 |
> |
assertFalse(y.isEmpty()); |
653 |
> |
assertEquals(x.remove(), y.remove()); |
654 |
|
} |
655 |
< |
} |
732 |
< |
|
733 |
< |
/** |
734 |
< |
* drainTo(null) throws NullPointerException |
735 |
< |
*/ |
736 |
< |
public void testDrainToNull() { |
737 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
738 |
< |
try { |
739 |
< |
q.drainTo(null); |
740 |
< |
shouldThrow(); |
741 |
< |
} catch (NullPointerException success) {} |
742 |
< |
} |
743 |
< |
|
744 |
< |
/** |
745 |
< |
* drainTo(this) throws IllegalArgumentException |
746 |
< |
*/ |
747 |
< |
public void testDrainToSelf() { |
748 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
749 |
< |
try { |
750 |
< |
q.drainTo(q); |
751 |
< |
shouldThrow(); |
752 |
< |
} catch (IllegalArgumentException success) {} |
655 |
> |
assertTrue(y.isEmpty()); |
656 |
|
} |
657 |
|
|
658 |
|
/** |
662 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
663 |
|
ArrayList l = new ArrayList(); |
664 |
|
q.drainTo(l); |
665 |
< |
assertEquals(q.size(), 0); |
666 |
< |
assertEquals(l.size(), SIZE); |
665 |
> |
assertEquals(0, q.size()); |
666 |
> |
assertEquals(SIZE, l.size()); |
667 |
|
for (int i = 0; i < SIZE; ++i) { |
668 |
< |
assertEquals(l.get(i), i); |
668 |
> |
assertEquals(i, l.get(i)); |
669 |
|
} |
670 |
|
q.add(zero); |
671 |
|
q.add(one); |
674 |
|
assertTrue(q.contains(one)); |
675 |
|
l.clear(); |
676 |
|
q.drainTo(l); |
677 |
< |
assertEquals(q.size(), 0); |
678 |
< |
assertEquals(l.size(), 2); |
677 |
> |
assertEquals(0, q.size()); |
678 |
> |
assertEquals(2, l.size()); |
679 |
|
for (int i = 0; i < 2; ++i) { |
680 |
< |
assertEquals(l.get(i), i); |
680 |
> |
assertEquals(i, l.get(i)); |
681 |
|
} |
682 |
|
} |
683 |
|
|
687 |
|
public void testDrainToWithActivePut() throws InterruptedException { |
688 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
689 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
690 |
< |
void realRun() { |
690 |
> |
public void realRun() { |
691 |
|
q.put(SIZE + 1); |
692 |
|
}}); |
693 |
|
ArrayList l = new ArrayList(); |
694 |
|
q.drainTo(l); |
695 |
|
assertTrue(l.size() >= SIZE); |
696 |
< |
for (int i = 0; i < SIZE; ++i) { |
697 |
< |
assertEquals(l.get(i), i); |
698 |
< |
} |
796 |
< |
t.join(); |
696 |
> |
for (int i = 0; i < SIZE; ++i) |
697 |
> |
assertEquals(i, l.get(i)); |
698 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
699 |
|
assertTrue(q.size() + l.size() >= SIZE); |
700 |
|
} |
701 |
|
|
702 |
|
/** |
703 |
< |
* drainTo(null, n) throws NullPointerException |
802 |
< |
*/ |
803 |
< |
public void testDrainToNullN() { |
804 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
805 |
< |
try { |
806 |
< |
q.drainTo(null, SIZE); |
807 |
< |
shouldThrow(); |
808 |
< |
} catch (NullPointerException success) {} |
809 |
< |
} |
810 |
< |
|
811 |
< |
/** |
812 |
< |
* drainTo(this, n) throws IllegalArgumentException |
813 |
< |
*/ |
814 |
< |
public void testDrainToSelfN() { |
815 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
816 |
< |
try { |
817 |
< |
q.drainTo(q, SIZE); |
818 |
< |
shouldThrow(); |
819 |
< |
} catch (IllegalArgumentException success) {} |
820 |
< |
} |
821 |
< |
|
822 |
< |
/** |
823 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
703 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
704 |
|
*/ |
705 |
|
public void testDrainToN() { |
706 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
711 |
|
ArrayList l = new ArrayList(); |
712 |
|
q.drainTo(l, i); |
713 |
|
int k = (i < SIZE) ? i : SIZE; |
714 |
< |
assertEquals(l.size(), k); |
715 |
< |
assertEquals(q.size(), SIZE - k); |
716 |
< |
for (int j = 0; j < k; ++j) { |
717 |
< |
assertEquals(l.get(j), j); |
718 |
< |
} |
839 |
< |
while (q.poll() != null) |
840 |
< |
; |
714 |
> |
assertEquals(k, l.size()); |
715 |
> |
assertEquals(SIZE - k, q.size()); |
716 |
> |
for (int j = 0; j < k; ++j) |
717 |
> |
assertEquals(j, l.get(j)); |
718 |
> |
do {} while (q.poll() != null); |
719 |
|
} |
720 |
|
} |
721 |
|
|
725 |
|
*/ |
726 |
|
public void testWaitingConsumer() throws InterruptedException { |
727 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
728 |
< |
assertEquals(q.getWaitingConsumerCount(), 0); |
728 |
> |
assertEquals(0, q.getWaitingConsumerCount()); |
729 |
|
assertFalse(q.hasWaitingConsumer()); |
730 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
731 |
|
|
732 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
733 |
< |
void realRun() throws InterruptedException { |
734 |
< |
Thread.sleep(SMALL_DELAY_MS); |
735 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
736 |
< |
threadAssertEquals(q.getWaitingConsumerCount(), 1); |
737 |
< |
threadAssertTrue(q.offer(new Object())); |
859 |
< |
threadAssertFalse(q.hasWaitingConsumer()); |
860 |
< |
threadAssertEquals(q.getWaitingConsumerCount(), 0); |
733 |
> |
public void realRun() throws InterruptedException { |
734 |
> |
threadStarted.countDown(); |
735 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
736 |
> |
assertEquals(0, q.getWaitingConsumerCount()); |
737 |
> |
assertFalse(q.hasWaitingConsumer()); |
738 |
|
}}); |
739 |
|
|
740 |
< |
assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); |
741 |
< |
assertEquals(q.getWaitingConsumerCount(), 0); |
740 |
> |
threadStarted.await(); |
741 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
742 |
> |
assertEquals(1, q.getWaitingConsumerCount()); |
743 |
> |
assertTrue(q.hasWaitingConsumer()); |
744 |
> |
|
745 |
> |
assertTrue(q.offer(one)); |
746 |
> |
assertEquals(0, q.getWaitingConsumerCount()); |
747 |
|
assertFalse(q.hasWaitingConsumer()); |
748 |
< |
t.join(); |
748 |
> |
|
749 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
750 |
|
} |
751 |
|
|
752 |
|
/** |
767 |
|
public void testTransfer2() throws InterruptedException { |
768 |
|
final LinkedTransferQueue<Integer> q |
769 |
|
= new LinkedTransferQueue<Integer>(); |
770 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
771 |
|
|
772 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
773 |
< |
void realRun() throws InterruptedException { |
774 |
< |
q.transfer(SIZE); |
775 |
< |
threadAssertTrue(q.isEmpty()); |
773 |
> |
public void realRun() throws InterruptedException { |
774 |
> |
threadStarted.countDown(); |
775 |
> |
q.transfer(five); |
776 |
> |
checkEmpty(q); |
777 |
|
}}); |
778 |
|
|
779 |
< |
Thread.sleep(SHORT_DELAY_MS); |
779 |
> |
threadStarted.await(); |
780 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
781 |
|
assertEquals(1, q.size()); |
782 |
< |
assertEquals(SIZE, (int) q.poll()); |
783 |
< |
assertTrue(q.isEmpty()); |
784 |
< |
t.join(); |
782 |
> |
assertSame(five, q.poll()); |
783 |
> |
checkEmpty(q); |
784 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
785 |
|
} |
786 |
|
|
787 |
|
/** |
792 |
|
= new LinkedTransferQueue<Integer>(); |
793 |
|
|
794 |
|
Thread first = newStartedThread(new CheckedRunnable() { |
795 |
< |
void realRun() throws InterruptedException { |
796 |
< |
Integer i = SIZE + 1; |
797 |
< |
q.transfer(i); |
798 |
< |
threadAssertTrue(!q.contains(i)); |
913 |
< |
threadAssertEquals(1, q.size()); |
795 |
> |
public void realRun() throws InterruptedException { |
796 |
> |
q.transfer(four); |
797 |
> |
assertTrue(!q.contains(four)); |
798 |
> |
assertEquals(1, q.size()); |
799 |
|
}}); |
800 |
|
|
801 |
|
Thread interruptedThread = newStartedThread( |
802 |
|
new CheckedInterruptedRunnable() { |
803 |
< |
void realRun() throws InterruptedException { |
804 |
< |
while (q.size() == 0) |
803 |
> |
public void realRun() throws InterruptedException { |
804 |
> |
while (q.isEmpty()) |
805 |
|
Thread.yield(); |
806 |
< |
q.transfer(SIZE); |
806 |
> |
q.transfer(five); |
807 |
|
}}); |
808 |
|
|
809 |
|
while (q.size() < 2) |
810 |
|
Thread.yield(); |
811 |
|
assertEquals(2, q.size()); |
812 |
< |
assertEquals(SIZE + 1, (int) q.poll()); |
812 |
> |
assertSame(four, q.poll()); |
813 |
|
first.join(); |
814 |
|
assertEquals(1, q.size()); |
815 |
|
interruptedThread.interrupt(); |
816 |
|
interruptedThread.join(); |
817 |
< |
assertEquals(0, q.size()); |
933 |
< |
assertTrue(q.isEmpty()); |
817 |
> |
checkEmpty(q); |
818 |
|
} |
819 |
|
|
820 |
|
/** |
825 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
826 |
|
|
827 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
828 |
< |
void realRun() throws InterruptedException { |
828 |
> |
public void realRun() throws InterruptedException { |
829 |
|
q.transfer(four); |
830 |
< |
threadAssertFalse(q.contains(four)); |
831 |
< |
threadAssertEquals(three, q.poll()); |
830 |
> |
assertFalse(q.contains(four)); |
831 |
> |
assertSame(three, q.poll()); |
832 |
|
}}); |
833 |
|
|
834 |
< |
Thread.sleep(SHORT_DELAY_MS); |
834 |
> |
while (q.isEmpty()) |
835 |
> |
Thread.yield(); |
836 |
> |
assertFalse(q.isEmpty()); |
837 |
> |
assertEquals(1, q.size()); |
838 |
|
assertTrue(q.offer(three)); |
839 |
< |
assertEquals(four, q.poll()); |
840 |
< |
t.join(); |
839 |
> |
assertSame(four, q.poll()); |
840 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
841 |
|
} |
842 |
|
|
843 |
|
/** |
849 |
|
= new LinkedTransferQueue<Integer>(); |
850 |
|
|
851 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
852 |
< |
void realRun() throws InterruptedException { |
853 |
< |
q.transfer(SIZE); |
852 |
> |
public void realRun() throws InterruptedException { |
853 |
> |
q.transfer(four); |
854 |
|
checkEmpty(q); |
855 |
|
}}); |
856 |
|
|
857 |
< |
Thread.sleep(SHORT_DELAY_MS); |
858 |
< |
assertEquals(SIZE, (int) q.take()); |
857 |
> |
while (q.isEmpty()) |
858 |
> |
Thread.yield(); |
859 |
> |
assertFalse(q.isEmpty()); |
860 |
> |
assertEquals(1, q.size()); |
861 |
> |
assertSame(four, q.take()); |
862 |
|
checkEmpty(q); |
863 |
< |
t.join(); |
863 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
864 |
|
} |
865 |
|
|
866 |
|
/** |
867 |
|
* tryTransfer(null) throws NullPointerException |
868 |
|
*/ |
869 |
|
public void testTryTransfer1() { |
870 |
+ |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
871 |
|
try { |
981 |
– |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
872 |
|
q.tryTransfer(null); |
873 |
|
shouldThrow(); |
874 |
|
} catch (NullPointerException success) {} |
894 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
895 |
|
|
896 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
897 |
< |
void realRun() { |
897 |
> |
public void realRun() { |
898 |
|
while (! q.hasWaitingConsumer()) |
899 |
|
Thread.yield(); |
900 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
901 |
< |
threadAssertTrue(q.isEmpty()); |
902 |
< |
threadAssertTrue(q.size() == 0); |
1013 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
900 |
> |
assertTrue(q.hasWaitingConsumer()); |
901 |
> |
checkEmpty(q); |
902 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
903 |
|
}}); |
904 |
|
|
905 |
< |
assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato); |
905 |
> |
assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
906 |
|
checkEmpty(q); |
907 |
< |
t.join(); |
907 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
908 |
|
} |
909 |
|
|
910 |
|
/** |
916 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
917 |
|
|
918 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
919 |
< |
void realRun() { |
919 |
> |
public void realRun() { |
920 |
|
while (! q.hasWaitingConsumer()) |
921 |
|
Thread.yield(); |
922 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
923 |
< |
threadAssertTrue(q.isEmpty()); |
924 |
< |
threadAssertTrue(q.size() == 0); |
1036 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
922 |
> |
assertTrue(q.hasWaitingConsumer()); |
923 |
> |
checkEmpty(q); |
924 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
925 |
|
}}); |
926 |
|
|
927 |
< |
assertTrue(q.take() == hotPotato); |
927 |
> |
assertSame(q.take(), hotPotato); |
928 |
|
checkEmpty(q); |
929 |
< |
t.join(); |
929 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
930 |
|
} |
931 |
|
|
932 |
|
/** |
933 |
< |
* tryTransfer waits the amount given if interrupted, and |
1046 |
< |
* throws interrupted exception |
933 |
> |
* tryTransfer blocks interruptibly if no takers |
934 |
|
*/ |
935 |
|
public void testTryTransfer5() throws InterruptedException { |
936 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
937 |
+ |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
938 |
+ |
assertTrue(q.isEmpty()); |
939 |
|
|
940 |
< |
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
941 |
< |
void realRun() throws InterruptedException { |
942 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
940 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
941 |
> |
public void realRun() throws InterruptedException { |
942 |
> |
Thread.currentThread().interrupt(); |
943 |
> |
try { |
944 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
945 |
> |
shouldThrow(); |
946 |
> |
} catch (InterruptedException success) {} |
947 |
> |
assertFalse(Thread.interrupted()); |
948 |
> |
|
949 |
> |
pleaseInterrupt.countDown(); |
950 |
> |
try { |
951 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
952 |
> |
shouldThrow(); |
953 |
> |
} catch (InterruptedException success) {} |
954 |
> |
assertFalse(Thread.interrupted()); |
955 |
|
}}); |
956 |
|
|
957 |
< |
Thread.sleep(SMALL_DELAY_MS); |
958 |
< |
toInterrupt.interrupt(); |
959 |
< |
toInterrupt.join(); |
957 |
> |
await(pleaseInterrupt); |
958 |
> |
assertThreadStaysAlive(t); |
959 |
> |
t.interrupt(); |
960 |
> |
awaitTermination(t); |
961 |
> |
checkEmpty(q); |
962 |
|
} |
963 |
|
|
964 |
|
/** |
965 |
< |
* tryTransfer gives up after the timeout and return false |
965 |
> |
* tryTransfer gives up after the timeout and returns false |
966 |
|
*/ |
967 |
|
public void testTryTransfer6() throws InterruptedException { |
968 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
969 |
|
|
970 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
971 |
< |
void realRun() throws InterruptedException { |
972 |
< |
threadAssertFalse |
973 |
< |
(q.tryTransfer(new Object(), |
974 |
< |
SHORT_DELAY_MS, MILLISECONDS)); |
971 |
> |
public void realRun() throws InterruptedException { |
972 |
> |
long t0 = System.nanoTime(); |
973 |
> |
assertFalse(q.tryTransfer(new Object(), |
974 |
> |
timeoutMillis(), MILLISECONDS)); |
975 |
> |
assertTrue(millisElapsedSince(t0) >= timeoutMillis()); |
976 |
> |
checkEmpty(q); |
977 |
|
}}); |
978 |
|
|
979 |
< |
Thread.sleep(SMALL_DELAY_MS); |
979 |
> |
awaitTermination(t); |
980 |
|
checkEmpty(q); |
1076 |
– |
t.join(); |
981 |
|
} |
982 |
|
|
983 |
|
/** |
989 |
|
assertTrue(q.offer(four)); |
990 |
|
|
991 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
992 |
< |
void realRun() throws InterruptedException { |
993 |
< |
threadAssertTrue(q.tryTransfer(five, |
994 |
< |
MEDIUM_DELAY_MS, MILLISECONDS)); |
1091 |
< |
threadAssertTrue(q.isEmpty()); |
992 |
> |
public void realRun() throws InterruptedException { |
993 |
> |
assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); |
994 |
> |
checkEmpty(q); |
995 |
|
}}); |
996 |
|
|
997 |
< |
Thread.sleep(SHORT_DELAY_MS); |
997 |
> |
while (q.size() != 2) |
998 |
> |
Thread.yield(); |
999 |
|
assertEquals(2, q.size()); |
1000 |
< |
assertEquals(four, q.poll()); |
1001 |
< |
assertEquals(five, q.poll()); |
1000 |
> |
assertSame(four, q.poll()); |
1001 |
> |
assertSame(five, q.poll()); |
1002 |
|
checkEmpty(q); |
1003 |
< |
t.join(); |
1003 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1004 |
|
} |
1005 |
|
|
1006 |
|
/** |
1007 |
< |
* tryTransfer attempts to enqueue into the q and fails returning |
1008 |
< |
* false not enqueueing and the successive poll is null |
1007 |
> |
* tryTransfer attempts to enqueue into the queue and fails |
1008 |
> |
* returning false not enqueueing and the successive poll is null |
1009 |
|
*/ |
1010 |
|
public void testTryTransfer8() throws InterruptedException { |
1011 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1012 |
|
assertTrue(q.offer(four)); |
1013 |
|
assertEquals(1, q.size()); |
1014 |
< |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1014 |
> |
long t0 = System.nanoTime(); |
1015 |
> |
assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS)); |
1016 |
> |
assertTrue(millisElapsedSince(t0) >= timeoutMillis()); |
1017 |
|
assertEquals(1, q.size()); |
1018 |
< |
assertEquals(four, q.poll()); |
1018 |
> |
assertSame(four, q.poll()); |
1019 |
|
assertNull(q.poll()); |
1020 |
|
checkEmpty(q); |
1021 |
|
} |
1022 |
|
|
1023 |
|
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1024 |
|
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
1025 |
< |
assertTrue(q.isEmpty()); |
1025 |
> |
checkEmpty(q); |
1026 |
|
for (int i = 0; i < n; i++) { |
1027 |
|
assertEquals(i, q.size()); |
1028 |
|
assertTrue(q.offer(i)); |
1031 |
|
assertFalse(q.isEmpty()); |
1032 |
|
return q; |
1033 |
|
} |
1034 |
+ |
|
1035 |
+ |
/** |
1036 |
+ |
* remove(null), contains(null) always return false |
1037 |
+ |
*/ |
1038 |
+ |
public void testNeverContainsNull() { |
1039 |
+ |
Collection<?>[] qs = { |
1040 |
+ |
new LinkedTransferQueue<Object>(), |
1041 |
+ |
populatedQueue(2), |
1042 |
+ |
}; |
1043 |
+ |
|
1044 |
+ |
for (Collection<?> q : qs) { |
1045 |
+ |
assertFalse(q.contains(null)); |
1046 |
+ |
assertFalse(q.remove(null)); |
1047 |
+ |
} |
1048 |
+ |
} |
1049 |
|
} |