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.Callable; |
19 |
> |
import java.util.concurrent.CountDownLatch; |
20 |
> |
import java.util.concurrent.Executors; |
21 |
> |
import java.util.concurrent.ExecutorService; |
22 |
> |
import java.util.concurrent.LinkedTransferQueue; |
23 |
> |
|
24 |
|
import junit.framework.Test; |
22 |
– |
import junit.framework.TestSuite; |
25 |
|
|
26 |
|
@SuppressWarnings({"unchecked", "rawtypes"}) |
27 |
|
public class LinkedTransferQueueTest extends JSR166TestCase { |
28 |
+ |
public static class Generic extends BlockingQueueTest { |
29 |
+ |
protected BlockingQueue emptyCollection() { |
30 |
+ |
return new LinkedTransferQueue(); |
31 |
+ |
} |
32 |
+ |
} |
33 |
|
|
34 |
|
public static void main(String[] args) { |
35 |
< |
junit.textui.TestRunner.run(suite()); |
35 |
> |
main(suite(), args); |
36 |
|
} |
37 |
|
|
38 |
|
public static Test suite() { |
39 |
< |
return new TestSuite(LinkedTransferQueueTest.class); |
40 |
< |
} |
41 |
< |
|
42 |
< |
void checkEmpty(LinkedTransferQueue q) throws InterruptedException { |
43 |
< |
assertTrue(q.isEmpty()); |
44 |
< |
assertEquals(0, q.size()); |
45 |
< |
assertNull(q.peek()); |
46 |
< |
assertNull(q.poll()); |
47 |
< |
assertNull(q.poll(0, MILLISECONDS)); |
48 |
< |
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) {} |
39 |
> |
class Implementation implements CollectionImplementation { |
40 |
> |
public Class<?> klazz() { return LinkedTransferQueue.class; } |
41 |
> |
public Collection emptyCollection() { return new LinkedTransferQueue(); } |
42 |
> |
public Object makeElement(int i) { return i; } |
43 |
> |
public boolean isConcurrent() { return true; } |
44 |
> |
public boolean permitsNulls() { return false; } |
45 |
> |
} |
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>(); |
186 |
> |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
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 { |
211 |
< |
for (int i = 0; i < SIZE; ++i) { |
212 |
< |
threadAssertEquals(i, (int) q.take()); |
213 |
< |
} |
214 |
< |
q.take(); |
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++) assertEquals(i, q.take()); |
213 |
> |
|
214 |
> |
Thread.currentThread().interrupt(); |
215 |
> |
try { |
216 |
> |
q.take(); |
217 |
> |
shouldThrow(); |
218 |
> |
} catch (InterruptedException success) {} |
219 |
> |
assertFalse(Thread.interrupted()); |
220 |
> |
|
221 |
> |
pleaseInterrupt.countDown(); |
222 |
> |
try { |
223 |
> |
q.take(); |
224 |
> |
shouldThrow(); |
225 |
> |
} catch (InterruptedException success) {} |
226 |
> |
assertFalse(Thread.interrupted()); |
227 |
|
}}); |
228 |
< |
Thread.sleep(SMALL_DELAY_MS); |
228 |
> |
|
229 |
> |
await(pleaseInterrupt); |
230 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
231 |
|
t.interrupt(); |
232 |
< |
t.join(); |
297 |
< |
checkEmpty(q); |
232 |
> |
awaitTermination(t); |
233 |
|
} |
234 |
|
|
235 |
|
/** |
245 |
|
} |
246 |
|
|
247 |
|
/** |
248 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
248 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
249 |
|
*/ |
250 |
|
public void testTimedPoll0() throws InterruptedException { |
251 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
257 |
|
} |
258 |
|
|
259 |
|
/** |
260 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
260 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
261 |
|
*/ |
262 |
|
public void testTimedPoll() throws InterruptedException { |
263 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
264 |
< |
for (int i = 0; i < SIZE; ++i) { |
265 |
< |
long t0 = System.nanoTime(); |
264 |
> |
long startTime = System.nanoTime(); |
265 |
> |
for (int i = 0; i < SIZE; ++i) |
266 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
267 |
< |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
268 |
< |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
269 |
< |
} |
270 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
267 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
268 |
> |
|
269 |
> |
startTime = System.nanoTime(); |
270 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
271 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
272 |
|
checkEmpty(q); |
273 |
|
} |
274 |
|
|
277 |
|
* returning timeout status |
278 |
|
*/ |
279 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
280 |
< |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
280 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
281 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
282 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
283 |
< |
void realRun() throws InterruptedException { |
284 |
< |
for (int i = 0; i < SIZE; ++i) { |
285 |
< |
long t0 = System.nanoTime(); |
286 |
< |
threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS, |
287 |
< |
MILLISECONDS)); |
351 |
< |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
352 |
< |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
353 |
< |
} |
283 |
> |
public void realRun() throws InterruptedException { |
284 |
> |
for (int i = 0; i < SIZE; i++) |
285 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
286 |
> |
|
287 |
> |
Thread.currentThread().interrupt(); |
288 |
|
try { |
289 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
289 |
> |
q.poll(randomTimeout(), randomTimeUnit()); |
290 |
|
shouldThrow(); |
291 |
|
} catch (InterruptedException success) {} |
292 |
+ |
assertFalse(Thread.interrupted()); |
293 |
+ |
|
294 |
+ |
pleaseInterrupt.countDown(); |
295 |
+ |
try { |
296 |
+ |
q.poll(LONGER_DELAY_MS, MILLISECONDS); |
297 |
+ |
shouldThrow(); |
298 |
+ |
} catch (InterruptedException success) {} |
299 |
+ |
assertFalse(Thread.interrupted()); |
300 |
|
}}); |
301 |
|
|
302 |
< |
Thread.sleep(SMALL_DELAY_MS); |
302 |
> |
await(pleaseInterrupt); |
303 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
304 |
|
t.interrupt(); |
305 |
< |
t.join(); |
305 |
> |
awaitTermination(t); |
306 |
|
checkEmpty(q); |
307 |
|
} |
308 |
|
|
309 |
|
/** |
310 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
311 |
< |
* on interruption throws |
310 |
> |
* timed poll after thread interrupted throws InterruptedException |
311 |
> |
* instead of returning timeout status |
312 |
|
*/ |
313 |
< |
public void testTimedPollWithOffer() throws InterruptedException { |
314 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
315 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
316 |
< |
void realRun() throws InterruptedException { |
317 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
318 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
319 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
313 |
> |
public void testTimedPollAfterInterrupt() throws InterruptedException { |
314 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
315 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
316 |
> |
public void realRun() throws InterruptedException { |
317 |
> |
Thread.currentThread().interrupt(); |
318 |
> |
for (int i = 0; i < SIZE; ++i) |
319 |
> |
assertEquals(i, (int) q.poll(randomTimeout(), randomTimeUnit())); |
320 |
> |
try { |
321 |
> |
q.poll(randomTimeout(), randomTimeUnit()); |
322 |
> |
shouldThrow(); |
323 |
> |
} catch (InterruptedException success) {} |
324 |
> |
assertFalse(Thread.interrupted()); |
325 |
|
}}); |
326 |
< |
Thread.sleep(SMALL_DELAY_MS); |
327 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
328 |
< |
t.interrupt(); |
381 |
< |
t.join(); |
326 |
> |
|
327 |
> |
awaitTermination(t); |
328 |
> |
checkEmpty(q); |
329 |
|
} |
330 |
|
|
331 |
|
/** |
375 |
|
} |
376 |
|
|
377 |
|
/** |
431 |
– |
* remove(x) removes x and returns true if present |
432 |
– |
*/ |
433 |
– |
public void testRemoveElement() throws InterruptedException { |
434 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
435 |
– |
for (int i = 1; i < SIZE; i += 2) { |
436 |
– |
assertTrue(q.remove(i)); |
437 |
– |
} |
438 |
– |
for (int i = 0; i < SIZE; i += 2) { |
439 |
– |
assertTrue(q.remove(i)); |
440 |
– |
assertFalse(q.remove(i + 1)); |
441 |
– |
} |
442 |
– |
checkEmpty(q); |
443 |
– |
} |
444 |
– |
|
445 |
– |
/** |
378 |
|
* An add following remove(x) succeeds |
379 |
|
*/ |
380 |
|
public void testRemoveElementAndAdd() throws InterruptedException { |
384 |
|
assertTrue(q.remove(one)); |
385 |
|
assertTrue(q.remove(two)); |
386 |
|
assertTrue(q.add(three)); |
387 |
< |
assertTrue(q.take() == three); |
387 |
> |
assertSame(q.take(), three); |
388 |
|
} |
389 |
|
|
390 |
|
/** |
420 |
|
*/ |
421 |
|
public void testContainsAll() { |
422 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
423 |
< |
LinkedTransferQueue<Integer> p = new LinkedTransferQueue<Integer>(); |
423 |
> |
LinkedTransferQueue<Integer> p = new LinkedTransferQueue<>(); |
424 |
|
for (int i = 0; i < SIZE; ++i) { |
425 |
|
assertTrue(q.containsAll(p)); |
426 |
|
assertFalse(p.containsAll(q)); |
466 |
|
} |
467 |
|
|
468 |
|
/** |
469 |
< |
* toArray() contains all elements |
469 |
> |
* toArray() contains all elements in FIFO order |
470 |
|
*/ |
471 |
< |
public void testToArray() throws InterruptedException { |
471 |
> |
public void testToArray() { |
472 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
473 |
< |
Object[] o = q.toArray(); |
474 |
< |
for (int i = 0; i < o.length; i++) { |
475 |
< |
assertEquals(o[i], q.take()); |
476 |
< |
} |
473 |
> |
Object[] a = q.toArray(); |
474 |
> |
assertSame(Object[].class, a.getClass()); |
475 |
> |
for (Object o : a) |
476 |
> |
assertSame(o, q.poll()); |
477 |
> |
assertTrue(q.isEmpty()); |
478 |
|
} |
479 |
|
|
480 |
|
/** |
481 |
< |
* toArray(a) contains all elements |
481 |
> |
* toArray(a) contains all elements in FIFO order |
482 |
|
*/ |
483 |
< |
public void testToArray2() throws InterruptedException { |
483 |
> |
public void testToArray2() { |
484 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
485 |
|
Integer[] ints = new Integer[SIZE]; |
486 |
< |
ints = q.toArray(ints); |
487 |
< |
for (int i = 0; i < ints.length; i++) { |
488 |
< |
assertEquals(ints[i], q.take()); |
489 |
< |
} |
490 |
< |
} |
558 |
< |
|
559 |
< |
/** |
560 |
< |
* toArray(null) throws NullPointerException |
561 |
< |
*/ |
562 |
< |
public void testToArray_BadArg() { |
563 |
< |
try { |
564 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
565 |
< |
Object o[] = q.toArray(null); |
566 |
< |
shouldThrow(); |
567 |
< |
} catch (NullPointerException success) {} |
486 |
> |
Integer[] array = q.toArray(ints); |
487 |
> |
assertSame(ints, array); |
488 |
> |
for (Integer o : ints) |
489 |
> |
assertSame(o, q.poll()); |
490 |
> |
assertTrue(q.isEmpty()); |
491 |
|
} |
492 |
|
|
493 |
|
/** |
494 |
< |
* toArray(incompatible array type) throws CCE |
494 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
495 |
|
*/ |
496 |
|
public void testToArray1_BadArg() { |
497 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
498 |
|
try { |
499 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
576 |
< |
Object o[] = q.toArray(new String[10]); |
499 |
> |
q.toArray(new String[10]); |
500 |
|
shouldThrow(); |
501 |
|
} catch (ArrayStoreException success) {} |
502 |
|
} |
507 |
|
public void testIterator() throws InterruptedException { |
508 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
509 |
|
Iterator it = q.iterator(); |
510 |
< |
int i = 0; |
511 |
< |
while (it.hasNext()) { |
512 |
< |
assertEquals(it.next(), i++); |
513 |
< |
} |
510 |
> |
int i; |
511 |
> |
for (i = 0; it.hasNext(); i++) |
512 |
> |
assertTrue(q.contains(it.next())); |
513 |
> |
assertEquals(i, SIZE); |
514 |
> |
assertIteratorExhausted(it); |
515 |
> |
|
516 |
> |
it = q.iterator(); |
517 |
> |
for (i = 0; it.hasNext(); i++) |
518 |
> |
assertEquals(it.next(), q.take()); |
519 |
|
assertEquals(i, SIZE); |
520 |
+ |
assertIteratorExhausted(it); |
521 |
+ |
} |
522 |
+ |
|
523 |
+ |
/** |
524 |
+ |
* iterator of empty collection has no elements |
525 |
+ |
*/ |
526 |
+ |
public void testEmptyIterator() { |
527 |
+ |
assertIteratorExhausted(new LinkedTransferQueue().iterator()); |
528 |
|
} |
529 |
|
|
530 |
|
/** |
541 |
|
it.remove(); |
542 |
|
|
543 |
|
it = q.iterator(); |
544 |
< |
assertEquals(it.next(), one); |
545 |
< |
assertEquals(it.next(), three); |
544 |
> |
assertSame(it.next(), one); |
545 |
> |
assertSame(it.next(), three); |
546 |
|
assertFalse(it.hasNext()); |
547 |
|
} |
548 |
|
|
550 |
|
* iterator ordering is FIFO |
551 |
|
*/ |
552 |
|
public void testIteratorOrdering() { |
553 |
< |
final LinkedTransferQueue<Integer> q |
618 |
< |
= new LinkedTransferQueue<Integer>(); |
553 |
> |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
554 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
555 |
|
q.add(one); |
556 |
|
q.add(two); |
585 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
586 |
|
String s = q.toString(); |
587 |
|
for (int i = 0; i < SIZE; ++i) { |
588 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
588 |
> |
assertTrue(s.contains(String.valueOf(i))); |
589 |
|
} |
590 |
|
} |
591 |
|
|
594 |
|
*/ |
595 |
|
public void testOfferInExecutor() { |
596 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
597 |
< |
q.add(one); |
598 |
< |
q.add(two); |
599 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
600 |
< |
|
601 |
< |
executor.execute(new CheckedRunnable() { |
602 |
< |
void realRun() { |
603 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, |
604 |
< |
MILLISECONDS)); |
605 |
< |
}}); |
606 |
< |
|
607 |
< |
executor.execute(new CheckedRunnable() { |
673 |
< |
void realRun() throws InterruptedException { |
674 |
< |
Thread.sleep(SMALL_DELAY_MS); |
675 |
< |
threadAssertEquals(one, q.take()); |
676 |
< |
}}); |
597 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
598 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
599 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
600 |
> |
|
601 |
> |
executor.execute(new CheckedRunnable() { |
602 |
> |
public void realRun() throws InterruptedException { |
603 |
> |
threadsStarted.await(); |
604 |
> |
long startTime = System.nanoTime(); |
605 |
> |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
606 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
607 |
> |
}}); |
608 |
|
|
609 |
< |
joinPool(executor); |
609 |
> |
executor.execute(new CheckedRunnable() { |
610 |
> |
public void realRun() throws InterruptedException { |
611 |
> |
threadsStarted.await(); |
612 |
> |
assertSame(one, q.take()); |
613 |
> |
checkEmpty(q); |
614 |
> |
}}); |
615 |
> |
} |
616 |
|
} |
617 |
|
|
618 |
|
/** |
620 |
|
*/ |
621 |
|
public void testPollInExecutor() { |
622 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
623 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
624 |
< |
|
625 |
< |
executor.execute(new CheckedRunnable() { |
626 |
< |
void realRun() throws InterruptedException { |
627 |
< |
threadAssertNull(q.poll()); |
628 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, |
629 |
< |
MILLISECONDS)); |
630 |
< |
threadAssertTrue(q.isEmpty()); |
631 |
< |
}}); |
632 |
< |
|
633 |
< |
executor.execute(new CheckedRunnable() { |
634 |
< |
void realRun() throws InterruptedException { |
635 |
< |
Thread.sleep(SMALL_DELAY_MS); |
699 |
< |
q.put(one); |
700 |
< |
}}); |
623 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
624 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
625 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
626 |
> |
|
627 |
> |
executor.execute(new CheckedRunnable() { |
628 |
> |
public void realRun() throws InterruptedException { |
629 |
> |
assertNull(q.poll()); |
630 |
> |
threadsStarted.await(); |
631 |
> |
long startTime = System.nanoTime(); |
632 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
633 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
634 |
> |
checkEmpty(q); |
635 |
> |
}}); |
636 |
|
|
637 |
< |
joinPool(executor); |
637 |
> |
executor.execute(new CheckedRunnable() { |
638 |
> |
public void realRun() throws InterruptedException { |
639 |
> |
threadsStarted.await(); |
640 |
> |
q.put(one); |
641 |
> |
}}); |
642 |
> |
} |
643 |
|
} |
644 |
|
|
645 |
|
/** |
646 |
< |
* A deserialized serialized queue has same elements in same order |
646 |
> |
* A deserialized/reserialized queue has same elements in same order |
647 |
|
*/ |
648 |
|
public void testSerialization() throws Exception { |
649 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
649 |
> |
Queue x = populatedQueue(SIZE); |
650 |
> |
Queue y = serialClone(x); |
651 |
|
|
652 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
653 |
< |
ObjectOutputStream out |
654 |
< |
= new ObjectOutputStream(new BufferedOutputStream(bout)); |
655 |
< |
out.writeObject(q); |
656 |
< |
out.close(); |
657 |
< |
|
658 |
< |
ByteArrayInputStream bin |
718 |
< |
= new ByteArrayInputStream(bout.toByteArray()); |
719 |
< |
ObjectInputStream in |
720 |
< |
= new ObjectInputStream(new BufferedInputStream(bin)); |
721 |
< |
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
722 |
< |
|
723 |
< |
assertEquals(q.size(), r.size()); |
724 |
< |
while (!q.isEmpty()) { |
725 |
< |
assertEquals(q.remove(), r.remove()); |
652 |
> |
assertNotSame(y, x); |
653 |
> |
assertEquals(x.size(), y.size()); |
654 |
> |
assertEquals(x.toString(), y.toString()); |
655 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
656 |
> |
while (!x.isEmpty()) { |
657 |
> |
assertFalse(y.isEmpty()); |
658 |
> |
assertEquals(x.remove(), y.remove()); |
659 |
|
} |
660 |
< |
} |
728 |
< |
|
729 |
< |
/** |
730 |
< |
* drainTo(null) throws NullPointerException |
731 |
< |
*/ |
732 |
< |
public void testDrainToNull() { |
733 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
734 |
< |
try { |
735 |
< |
q.drainTo(null); |
736 |
< |
shouldThrow(); |
737 |
< |
} catch (NullPointerException success) {} |
738 |
< |
} |
739 |
< |
|
740 |
< |
/** |
741 |
< |
* drainTo(this) throws IllegalArgumentException |
742 |
< |
*/ |
743 |
< |
public void testDrainToSelf() { |
744 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
745 |
< |
try { |
746 |
< |
q.drainTo(q); |
747 |
< |
shouldThrow(); |
748 |
< |
} catch (IllegalArgumentException success) {} |
660 |
> |
assertTrue(y.isEmpty()); |
661 |
|
} |
662 |
|
|
663 |
|
/** |
667 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
668 |
|
ArrayList l = new ArrayList(); |
669 |
|
q.drainTo(l); |
670 |
< |
assertEquals(q.size(), 0); |
671 |
< |
assertEquals(l.size(), SIZE); |
670 |
> |
assertEquals(0, q.size()); |
671 |
> |
assertEquals(SIZE, l.size()); |
672 |
|
for (int i = 0; i < SIZE; ++i) { |
673 |
< |
assertEquals(l.get(i), i); |
673 |
> |
assertEquals(i, l.get(i)); |
674 |
|
} |
675 |
|
q.add(zero); |
676 |
|
q.add(one); |
679 |
|
assertTrue(q.contains(one)); |
680 |
|
l.clear(); |
681 |
|
q.drainTo(l); |
682 |
< |
assertEquals(q.size(), 0); |
683 |
< |
assertEquals(l.size(), 2); |
682 |
> |
assertEquals(0, q.size()); |
683 |
> |
assertEquals(2, l.size()); |
684 |
|
for (int i = 0; i < 2; ++i) { |
685 |
< |
assertEquals(l.get(i), i); |
685 |
> |
assertEquals(i, l.get(i)); |
686 |
|
} |
687 |
|
} |
688 |
|
|
692 |
|
public void testDrainToWithActivePut() throws InterruptedException { |
693 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
694 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
695 |
< |
void realRun() { |
695 |
> |
public void realRun() { |
696 |
|
q.put(SIZE + 1); |
697 |
|
}}); |
698 |
|
ArrayList l = new ArrayList(); |
699 |
|
q.drainTo(l); |
700 |
|
assertTrue(l.size() >= SIZE); |
701 |
< |
for (int i = 0; i < SIZE; ++i) { |
702 |
< |
assertEquals(l.get(i), i); |
703 |
< |
} |
792 |
< |
t.join(); |
701 |
> |
for (int i = 0; i < SIZE; ++i) |
702 |
> |
assertEquals(i, l.get(i)); |
703 |
> |
awaitTermination(t); |
704 |
|
assertTrue(q.size() + l.size() >= SIZE); |
705 |
|
} |
706 |
|
|
707 |
|
/** |
708 |
< |
* drainTo(null, n) throws NullPointerException |
798 |
< |
*/ |
799 |
< |
public void testDrainToNullN() { |
800 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
801 |
< |
try { |
802 |
< |
q.drainTo(null, SIZE); |
803 |
< |
shouldThrow(); |
804 |
< |
} catch (NullPointerException success) {} |
805 |
< |
} |
806 |
< |
|
807 |
< |
/** |
808 |
< |
* drainTo(this, n) throws IllegalArgumentException |
809 |
< |
*/ |
810 |
< |
public void testDrainToSelfN() { |
811 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
812 |
< |
try { |
813 |
< |
q.drainTo(q, SIZE); |
814 |
< |
shouldThrow(); |
815 |
< |
} catch (IllegalArgumentException success) {} |
816 |
< |
} |
817 |
< |
|
818 |
< |
/** |
819 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
708 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
709 |
|
*/ |
710 |
|
public void testDrainToN() { |
711 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
716 |
|
ArrayList l = new ArrayList(); |
717 |
|
q.drainTo(l, i); |
718 |
|
int k = (i < SIZE) ? i : SIZE; |
719 |
< |
assertEquals(l.size(), k); |
720 |
< |
assertEquals(q.size(), SIZE - k); |
721 |
< |
for (int j = 0; j < k; ++j) { |
722 |
< |
assertEquals(l.get(j), j); |
723 |
< |
} |
835 |
< |
while (q.poll() != null) |
836 |
< |
; |
719 |
> |
assertEquals(k, l.size()); |
720 |
> |
assertEquals(SIZE - k, q.size()); |
721 |
> |
for (int j = 0; j < k; ++j) |
722 |
> |
assertEquals(j, l.get(j)); |
723 |
> |
do {} while (q.poll() != null); |
724 |
|
} |
725 |
|
} |
726 |
|
|
730 |
|
*/ |
731 |
|
public void testWaitingConsumer() throws InterruptedException { |
732 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
733 |
< |
assertEquals(q.getWaitingConsumerCount(), 0); |
733 |
> |
assertEquals(0, q.getWaitingConsumerCount()); |
734 |
|
assertFalse(q.hasWaitingConsumer()); |
735 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
736 |
|
|
737 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
738 |
< |
void realRun() throws InterruptedException { |
739 |
< |
Thread.sleep(SMALL_DELAY_MS); |
740 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
741 |
< |
threadAssertEquals(q.getWaitingConsumerCount(), 1); |
742 |
< |
threadAssertTrue(q.offer(new Object())); |
743 |
< |
threadAssertFalse(q.hasWaitingConsumer()); |
744 |
< |
threadAssertEquals(q.getWaitingConsumerCount(), 0); |
738 |
> |
public void realRun() throws InterruptedException { |
739 |
> |
threadStarted.countDown(); |
740 |
> |
long startTime = System.nanoTime(); |
741 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
742 |
> |
assertEquals(0, q.getWaitingConsumerCount()); |
743 |
> |
assertFalse(q.hasWaitingConsumer()); |
744 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
745 |
|
}}); |
746 |
|
|
747 |
< |
assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); |
748 |
< |
assertEquals(q.getWaitingConsumerCount(), 0); |
747 |
> |
threadStarted.await(); |
748 |
> |
Callable<Boolean> oneConsumer |
749 |
> |
= new Callable<Boolean>() { public Boolean call() { |
750 |
> |
return q.hasWaitingConsumer() |
751 |
> |
&& q.getWaitingConsumerCount() == 1; }}; |
752 |
> |
waitForThreadToEnterWaitState(t, oneConsumer); |
753 |
> |
|
754 |
> |
assertTrue(q.offer(one)); |
755 |
> |
assertEquals(0, q.getWaitingConsumerCount()); |
756 |
|
assertFalse(q.hasWaitingConsumer()); |
757 |
< |
t.join(); |
757 |
> |
|
758 |
> |
awaitTermination(t); |
759 |
|
} |
760 |
|
|
761 |
|
/** |
770 |
|
} |
771 |
|
|
772 |
|
/** |
773 |
< |
* transfer waits until a poll occurs. The transfered element |
774 |
< |
* is returned by this associated poll. |
773 |
> |
* transfer waits until a poll occurs. The transferred element |
774 |
> |
* is returned by the associated poll. |
775 |
|
*/ |
776 |
|
public void testTransfer2() throws InterruptedException { |
777 |
< |
final LinkedTransferQueue<Integer> q |
778 |
< |
= new LinkedTransferQueue<Integer>(); |
777 |
> |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
778 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
779 |
|
|
780 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
781 |
< |
void realRun() throws InterruptedException { |
782 |
< |
q.transfer(SIZE); |
783 |
< |
threadAssertTrue(q.isEmpty()); |
781 |
> |
public void realRun() throws InterruptedException { |
782 |
> |
threadStarted.countDown(); |
783 |
> |
q.transfer(five); |
784 |
> |
checkEmpty(q); |
785 |
|
}}); |
786 |
|
|
787 |
< |
Thread.sleep(SHORT_DELAY_MS); |
788 |
< |
assertEquals(1, q.size()); |
789 |
< |
assertEquals(SIZE, (int) q.poll()); |
790 |
< |
assertTrue(q.isEmpty()); |
791 |
< |
t.join(); |
787 |
> |
threadStarted.await(); |
788 |
> |
Callable<Boolean> oneElement |
789 |
> |
= new Callable<Boolean>() { public Boolean call() { |
790 |
> |
return !q.isEmpty() && q.size() == 1; }}; |
791 |
> |
waitForThreadToEnterWaitState(t, oneElement); |
792 |
> |
|
793 |
> |
assertSame(five, q.poll()); |
794 |
> |
checkEmpty(q); |
795 |
> |
awaitTermination(t); |
796 |
|
} |
797 |
|
|
798 |
|
/** |
799 |
|
* transfer waits until a poll occurs, and then transfers in fifo order |
800 |
|
*/ |
801 |
|
public void testTransfer3() throws InterruptedException { |
802 |
< |
final LinkedTransferQueue<Integer> q |
902 |
< |
= new LinkedTransferQueue<Integer>(); |
802 |
> |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
803 |
|
|
804 |
|
Thread first = newStartedThread(new CheckedRunnable() { |
805 |
< |
void realRun() throws InterruptedException { |
806 |
< |
Integer i = SIZE + 1; |
807 |
< |
q.transfer(i); |
808 |
< |
threadAssertTrue(!q.contains(i)); |
909 |
< |
threadAssertEquals(1, q.size()); |
805 |
> |
public void realRun() throws InterruptedException { |
806 |
> |
q.transfer(four); |
807 |
> |
assertFalse(q.contains(four)); |
808 |
> |
assertEquals(1, q.size()); |
809 |
|
}}); |
810 |
|
|
811 |
|
Thread interruptedThread = newStartedThread( |
812 |
|
new CheckedInterruptedRunnable() { |
813 |
< |
void realRun() throws InterruptedException { |
814 |
< |
while (q.size() == 0) |
813 |
> |
public void realRun() throws InterruptedException { |
814 |
> |
while (q.isEmpty()) |
815 |
|
Thread.yield(); |
816 |
< |
q.transfer(SIZE); |
816 |
> |
q.transfer(five); |
817 |
|
}}); |
818 |
|
|
819 |
|
while (q.size() < 2) |
820 |
|
Thread.yield(); |
821 |
|
assertEquals(2, q.size()); |
822 |
< |
assertEquals(SIZE + 1, (int) q.poll()); |
822 |
> |
assertSame(four, q.poll()); |
823 |
|
first.join(); |
824 |
|
assertEquals(1, q.size()); |
825 |
|
interruptedThread.interrupt(); |
826 |
|
interruptedThread.join(); |
827 |
< |
assertEquals(0, q.size()); |
929 |
< |
assertTrue(q.isEmpty()); |
827 |
> |
checkEmpty(q); |
828 |
|
} |
829 |
|
|
830 |
|
/** |
835 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
836 |
|
|
837 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
838 |
< |
void realRun() throws InterruptedException { |
838 |
> |
public void realRun() throws InterruptedException { |
839 |
|
q.transfer(four); |
840 |
< |
threadAssertFalse(q.contains(four)); |
841 |
< |
threadAssertEquals(three, q.poll()); |
840 |
> |
assertFalse(q.contains(four)); |
841 |
> |
assertSame(three, q.poll()); |
842 |
|
}}); |
843 |
|
|
844 |
< |
Thread.sleep(SHORT_DELAY_MS); |
844 |
> |
while (q.isEmpty()) |
845 |
> |
Thread.yield(); |
846 |
> |
assertFalse(q.isEmpty()); |
847 |
> |
assertEquals(1, q.size()); |
848 |
|
assertTrue(q.offer(three)); |
849 |
< |
assertEquals(four, q.poll()); |
850 |
< |
t.join(); |
849 |
> |
assertSame(four, q.poll()); |
850 |
> |
awaitTermination(t); |
851 |
|
} |
852 |
|
|
853 |
|
/** |
854 |
< |
* transfer waits until a take occurs. The transfered element |
855 |
< |
* is returned by this associated take. |
854 |
> |
* transfer waits until a take occurs. The transferred element |
855 |
> |
* is returned by the associated take. |
856 |
|
*/ |
857 |
|
public void testTransfer5() throws InterruptedException { |
858 |
< |
final LinkedTransferQueue<Integer> q |
958 |
< |
= new LinkedTransferQueue<Integer>(); |
858 |
> |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
859 |
|
|
860 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
861 |
< |
void realRun() throws InterruptedException { |
862 |
< |
q.transfer(SIZE); |
861 |
> |
public void realRun() throws InterruptedException { |
862 |
> |
q.transfer(four); |
863 |
|
checkEmpty(q); |
864 |
|
}}); |
865 |
|
|
866 |
< |
Thread.sleep(SHORT_DELAY_MS); |
867 |
< |
assertEquals(SIZE, (int) q.take()); |
866 |
> |
while (q.isEmpty()) |
867 |
> |
Thread.yield(); |
868 |
> |
assertFalse(q.isEmpty()); |
869 |
> |
assertEquals(1, q.size()); |
870 |
> |
assertSame(four, q.take()); |
871 |
|
checkEmpty(q); |
872 |
< |
t.join(); |
872 |
> |
awaitTermination(t); |
873 |
|
} |
874 |
|
|
875 |
|
/** |
876 |
|
* tryTransfer(null) throws NullPointerException |
877 |
|
*/ |
878 |
|
public void testTryTransfer1() { |
879 |
+ |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
880 |
|
try { |
977 |
– |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
881 |
|
q.tryTransfer(null); |
882 |
|
shouldThrow(); |
883 |
|
} catch (NullPointerException success) {} |
903 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
904 |
|
|
905 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
906 |
< |
void realRun() { |
906 |
> |
public void realRun() { |
907 |
|
while (! q.hasWaitingConsumer()) |
908 |
|
Thread.yield(); |
909 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
910 |
< |
threadAssertTrue(q.isEmpty()); |
911 |
< |
threadAssertTrue(q.size() == 0); |
1009 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
909 |
> |
assertTrue(q.hasWaitingConsumer()); |
910 |
> |
checkEmpty(q); |
911 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
912 |
|
}}); |
913 |
|
|
914 |
< |
assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato); |
914 |
> |
long startTime = System.nanoTime(); |
915 |
> |
assertSame(hotPotato, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
916 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
917 |
|
checkEmpty(q); |
918 |
< |
t.join(); |
918 |
> |
awaitTermination(t); |
919 |
|
} |
920 |
|
|
921 |
|
/** |
927 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
928 |
|
|
929 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
930 |
< |
void realRun() { |
930 |
> |
public void realRun() { |
931 |
|
while (! q.hasWaitingConsumer()) |
932 |
|
Thread.yield(); |
933 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
934 |
< |
threadAssertTrue(q.isEmpty()); |
935 |
< |
threadAssertTrue(q.size() == 0); |
1032 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
933 |
> |
assertTrue(q.hasWaitingConsumer()); |
934 |
> |
checkEmpty(q); |
935 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
936 |
|
}}); |
937 |
|
|
938 |
< |
assertTrue(q.take() == hotPotato); |
938 |
> |
assertSame(q.take(), hotPotato); |
939 |
|
checkEmpty(q); |
940 |
< |
t.join(); |
940 |
> |
awaitTermination(t); |
941 |
|
} |
942 |
|
|
943 |
|
/** |
944 |
< |
* tryTransfer waits the amount given if interrupted, and |
1042 |
< |
* throws interrupted exception |
944 |
> |
* tryTransfer blocks interruptibly if no takers |
945 |
|
*/ |
946 |
|
public void testTryTransfer5() throws InterruptedException { |
947 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
948 |
+ |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
949 |
+ |
assertTrue(q.isEmpty()); |
950 |
+ |
|
951 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
952 |
+ |
public void realRun() throws InterruptedException { |
953 |
+ |
long startTime = System.nanoTime(); |
954 |
+ |
Thread.currentThread().interrupt(); |
955 |
+ |
try { |
956 |
+ |
q.tryTransfer(new Object(), randomTimeout(), randomTimeUnit()); |
957 |
+ |
shouldThrow(); |
958 |
+ |
} catch (InterruptedException success) {} |
959 |
+ |
assertFalse(Thread.interrupted()); |
960 |
+ |
|
961 |
+ |
pleaseInterrupt.countDown(); |
962 |
+ |
try { |
963 |
+ |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
964 |
+ |
shouldThrow(); |
965 |
+ |
} catch (InterruptedException success) {} |
966 |
+ |
assertFalse(Thread.interrupted()); |
967 |
|
|
968 |
< |
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
1048 |
< |
void realRun() throws InterruptedException { |
1049 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
968 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
969 |
|
}}); |
970 |
|
|
971 |
< |
Thread.sleep(SMALL_DELAY_MS); |
972 |
< |
toInterrupt.interrupt(); |
973 |
< |
toInterrupt.join(); |
971 |
> |
await(pleaseInterrupt); |
972 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
973 |
> |
t.interrupt(); |
974 |
> |
awaitTermination(t); |
975 |
> |
checkEmpty(q); |
976 |
|
} |
977 |
|
|
978 |
|
/** |
979 |
< |
* tryTransfer gives up after the timeout and return false |
979 |
> |
* tryTransfer gives up after the timeout and returns false |
980 |
|
*/ |
981 |
|
public void testTryTransfer6() throws InterruptedException { |
982 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
983 |
|
|
984 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
985 |
< |
void realRun() throws InterruptedException { |
986 |
< |
threadAssertFalse |
987 |
< |
(q.tryTransfer(new Object(), |
988 |
< |
SHORT_DELAY_MS, MILLISECONDS)); |
985 |
> |
public void realRun() throws InterruptedException { |
986 |
> |
long startTime = System.nanoTime(); |
987 |
> |
assertFalse(q.tryTransfer(new Object(), |
988 |
> |
timeoutMillis(), MILLISECONDS)); |
989 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
990 |
> |
checkEmpty(q); |
991 |
|
}}); |
992 |
|
|
993 |
< |
Thread.sleep(SMALL_DELAY_MS); |
993 |
> |
awaitTermination(t); |
994 |
|
checkEmpty(q); |
1072 |
– |
t.join(); |
995 |
|
} |
996 |
|
|
997 |
|
/** |
1003 |
|
assertTrue(q.offer(four)); |
1004 |
|
|
1005 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1006 |
< |
void realRun() throws InterruptedException { |
1007 |
< |
threadAssertTrue(q.tryTransfer(five, |
1008 |
< |
MEDIUM_DELAY_MS, MILLISECONDS)); |
1009 |
< |
threadAssertTrue(q.isEmpty()); |
1006 |
> |
public void realRun() throws InterruptedException { |
1007 |
> |
long startTime = System.nanoTime(); |
1008 |
> |
assertTrue(q.tryTransfer(five, LONG_DELAY_MS, MILLISECONDS)); |
1009 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1010 |
> |
checkEmpty(q); |
1011 |
|
}}); |
1012 |
|
|
1013 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1013 |
> |
while (q.size() != 2) |
1014 |
> |
Thread.yield(); |
1015 |
|
assertEquals(2, q.size()); |
1016 |
< |
assertEquals(four, q.poll()); |
1017 |
< |
assertEquals(five, q.poll()); |
1016 |
> |
assertSame(four, q.poll()); |
1017 |
> |
assertSame(five, q.poll()); |
1018 |
|
checkEmpty(q); |
1019 |
< |
t.join(); |
1019 |
> |
awaitTermination(t); |
1020 |
|
} |
1021 |
|
|
1022 |
|
/** |
1023 |
< |
* tryTransfer attempts to enqueue into the q and fails returning |
1024 |
< |
* false not enqueueing and the successive poll is null |
1023 |
> |
* tryTransfer attempts to enqueue into the queue and fails |
1024 |
> |
* returning false not enqueueing and the successive poll is null |
1025 |
|
*/ |
1026 |
|
public void testTryTransfer8() throws InterruptedException { |
1027 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1028 |
|
assertTrue(q.offer(four)); |
1029 |
|
assertEquals(1, q.size()); |
1030 |
< |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1030 |
> |
long startTime = System.nanoTime(); |
1031 |
> |
assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS)); |
1032 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1033 |
|
assertEquals(1, q.size()); |
1034 |
< |
assertEquals(four, q.poll()); |
1034 |
> |
assertSame(four, q.poll()); |
1035 |
|
assertNull(q.poll()); |
1036 |
|
checkEmpty(q); |
1037 |
|
} |
1038 |
|
|
1039 |
|
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1040 |
< |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
1041 |
< |
assertTrue(q.isEmpty()); |
1040 |
> |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
1041 |
> |
checkEmpty(q); |
1042 |
|
for (int i = 0; i < n; i++) { |
1043 |
|
assertEquals(i, q.size()); |
1044 |
|
assertTrue(q.offer(i)); |
1047 |
|
assertFalse(q.isEmpty()); |
1048 |
|
return q; |
1049 |
|
} |
1050 |
+ |
|
1051 |
+ |
/** |
1052 |
+ |
* remove(null), contains(null) always return false |
1053 |
+ |
*/ |
1054 |
+ |
public void testNeverContainsNull() { |
1055 |
+ |
Collection<?>[] qs = { |
1056 |
+ |
new LinkedTransferQueue<Object>(), |
1057 |
+ |
populatedQueue(2), |
1058 |
+ |
}; |
1059 |
+ |
|
1060 |
+ |
for (Collection<?> q : qs) { |
1061 |
+ |
assertFalse(q.contains(null)); |
1062 |
+ |
assertFalse(q.remove(null)); |
1063 |
+ |
} |
1064 |
+ |
} |
1065 |
|
} |