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 |
< |
public 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 = new Thread(new CheckedRunnable() { |
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 |
< |
assertEquals(i, (int) q.take()); |
213 |
> |
assertEquals(i, q.take()); |
214 |
|
} |
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 |
|
|
231 |
< |
t.start(); |
232 |
< |
Thread.sleep(SHORT_DELAY_MS); |
231 |
> |
await(pleaseInterrupt); |
232 |
> |
assertThreadStaysAlive(t); |
233 |
|
t.interrupt(); |
234 |
< |
t.join(); |
302 |
< |
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); |
338 |
< |
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 |
|
public void realRun() throws InterruptedException { |
286 |
|
for (int i = 0; i < SIZE; ++i) { |
287 |
|
long t0 = System.nanoTime(); |
288 |
< |
threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS, |
289 |
< |
MILLISECONDS)); |
356 |
< |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
357 |
< |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
288 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
289 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
290 |
|
} |
291 |
+ |
long t0 = System.nanoTime(); |
292 |
+ |
aboutToWait.countDown(); |
293 |
|
try { |
294 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
294 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
295 |
|
shouldThrow(); |
296 |
< |
} catch (InterruptedException success) {} |
296 |
> |
} catch (InterruptedException success) { |
297 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
298 |
> |
} |
299 |
|
}}); |
300 |
|
|
301 |
< |
Thread.sleep(SMALL_DELAY_MS); |
301 |
> |
aboutToWait.await(); |
302 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
303 |
|
t.interrupt(); |
304 |
< |
t.join(); |
304 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
305 |
|
checkEmpty(q); |
306 |
|
} |
307 |
|
|
308 |
|
/** |
309 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
310 |
< |
* on interruption throws |
309 |
> |
* timed poll after thread interrupted throws InterruptedException |
310 |
> |
* instead of returning timeout status |
311 |
|
*/ |
312 |
< |
public void testTimedPollWithOffer() throws InterruptedException { |
313 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
314 |
< |
Thread t = new Thread(new CheckedRunnable() { |
312 |
> |
public void testTimedPollAfterInterrupt() throws InterruptedException { |
313 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
314 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
315 |
|
public void realRun() throws InterruptedException { |
316 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
317 |
< |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
316 |
> |
Thread.currentThread().interrupt(); |
317 |
> |
for (int i = 0; i < SIZE; ++i) { |
318 |
> |
long t0 = System.nanoTime(); |
319 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
320 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
321 |
> |
} |
322 |
|
try { |
323 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
323 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
324 |
|
shouldThrow(); |
325 |
|
} catch (InterruptedException success) {} |
326 |
|
}}); |
327 |
|
|
328 |
< |
t.start(); |
329 |
< |
Thread.sleep(SMALL_DELAY_MS); |
389 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
390 |
< |
t.interrupt(); |
391 |
< |
t.join(); |
328 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
329 |
> |
checkEmpty(q); |
330 |
|
} |
331 |
|
|
332 |
|
/** |
376 |
|
} |
377 |
|
|
378 |
|
/** |
441 |
– |
* remove(x) removes x and returns true if present |
442 |
– |
*/ |
443 |
– |
public void testRemoveElement() throws InterruptedException { |
444 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
445 |
– |
for (int i = 1; i < SIZE; i += 2) { |
446 |
– |
assertTrue(q.remove(i)); |
447 |
– |
} |
448 |
– |
for (int i = 0; i < SIZE; i += 2) { |
449 |
– |
assertTrue(q.remove(i)); |
450 |
– |
assertFalse(q.remove(i + 1)); |
451 |
– |
} |
452 |
– |
checkEmpty(q); |
453 |
– |
} |
454 |
– |
|
455 |
– |
/** |
379 |
|
* An add following remove(x) succeeds |
380 |
|
*/ |
381 |
|
public void testRemoveElementAndAdd() throws InterruptedException { |
385 |
|
assertTrue(q.remove(one)); |
386 |
|
assertTrue(q.remove(two)); |
387 |
|
assertTrue(q.add(three)); |
388 |
< |
assertTrue(q.take() == three); |
388 |
> |
assertSame(q.take(), three); |
389 |
|
} |
390 |
|
|
391 |
|
/** |
467 |
|
} |
468 |
|
|
469 |
|
/** |
470 |
< |
* toArray() contains all elements |
470 |
> |
* toArray() contains all elements in FIFO order |
471 |
|
*/ |
472 |
< |
public void testToArray() throws InterruptedException { |
472 |
> |
public void testToArray() { |
473 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
474 |
|
Object[] o = q.toArray(); |
475 |
|
for (int i = 0; i < o.length; i++) { |
476 |
< |
assertEquals(o[i], q.take()); |
476 |
> |
assertSame(o[i], q.poll()); |
477 |
|
} |
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); |
486 |
> |
Integer[] array = q.toArray(ints); |
487 |
> |
assertSame(ints, array); |
488 |
|
for (int i = 0; i < ints.length; i++) { |
489 |
< |
assertEquals(ints[i], q.take()); |
489 |
> |
assertSame(ints[i], q.poll()); |
490 |
|
} |
491 |
|
} |
492 |
|
|
493 |
|
/** |
494 |
< |
* toArray(null) throws NullPointerException |
571 |
< |
*/ |
572 |
< |
public void testToArray_BadArg() { |
573 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
574 |
< |
try { |
575 |
< |
Object o[] = q.toArray(null); |
576 |
< |
shouldThrow(); |
577 |
< |
} catch (NullPointerException success) {} |
578 |
< |
} |
579 |
< |
|
580 |
< |
/** |
581 |
< |
* 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 |
< |
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++); |
600 |
< |
} |
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 |
|
|
586 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
587 |
|
String s = q.toString(); |
588 |
|
for (int i = 0; i < SIZE; ++i) { |
589 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
589 |
> |
assertTrue(s.contains(String.valueOf(i))); |
590 |
|
} |
591 |
|
} |
592 |
|
|
595 |
|
*/ |
596 |
|
public void testOfferInExecutor() { |
597 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
598 |
< |
q.add(one); |
599 |
< |
q.add(two); |
600 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
598 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
599 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
600 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
601 |
|
|
602 |
< |
executor.execute(new CheckedRunnable() { |
603 |
< |
public void realRun() { |
604 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, |
605 |
< |
MILLISECONDS)); |
606 |
< |
}}); |
681 |
< |
|
682 |
< |
executor.execute(new CheckedRunnable() { |
683 |
< |
public void realRun() throws InterruptedException { |
684 |
< |
Thread.sleep(SMALL_DELAY_MS); |
685 |
< |
threadAssertEquals(one, q.take()); |
686 |
< |
}}); |
602 |
> |
executor.execute(new CheckedRunnable() { |
603 |
> |
public void realRun() throws InterruptedException { |
604 |
> |
threadsStarted.await(); |
605 |
> |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
606 |
> |
}}); |
607 |
|
|
608 |
< |
joinPool(executor); |
608 |
> |
executor.execute(new CheckedRunnable() { |
609 |
> |
public void realRun() throws InterruptedException { |
610 |
> |
threadsStarted.await(); |
611 |
> |
assertSame(one, q.take()); |
612 |
> |
checkEmpty(q); |
613 |
> |
}}); |
614 |
> |
} |
615 |
|
} |
616 |
|
|
617 |
|
/** |
619 |
|
*/ |
620 |
|
public void testPollInExecutor() { |
621 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
622 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
622 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
623 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
624 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
625 |
|
|
626 |
< |
executor.execute(new CheckedRunnable() { |
627 |
< |
public void realRun() throws InterruptedException { |
628 |
< |
threadAssertNull(q.poll()); |
629 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, |
630 |
< |
MILLISECONDS)); |
631 |
< |
threadAssertTrue(q.isEmpty()); |
632 |
< |
}}); |
705 |
< |
|
706 |
< |
executor.execute(new CheckedRunnable() { |
707 |
< |
public void realRun() throws InterruptedException { |
708 |
< |
Thread.sleep(SMALL_DELAY_MS); |
709 |
< |
q.put(one); |
710 |
< |
}}); |
626 |
> |
executor.execute(new CheckedRunnable() { |
627 |
> |
public void realRun() throws InterruptedException { |
628 |
> |
assertNull(q.poll()); |
629 |
> |
threadsStarted.await(); |
630 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
631 |
> |
checkEmpty(q); |
632 |
> |
}}); |
633 |
|
|
634 |
< |
joinPool(executor); |
634 |
> |
executor.execute(new CheckedRunnable() { |
635 |
> |
public void realRun() throws InterruptedException { |
636 |
> |
threadsStarted.await(); |
637 |
> |
q.put(one); |
638 |
> |
}}); |
639 |
> |
} |
640 |
|
} |
641 |
|
|
642 |
|
/** |
643 |
|
* A deserialized serialized queue has same elements in same order |
644 |
|
*/ |
645 |
|
public void testSerialization() throws Exception { |
646 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
647 |
< |
|
721 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
722 |
< |
ObjectOutputStream out |
723 |
< |
= new ObjectOutputStream(new BufferedOutputStream(bout)); |
724 |
< |
out.writeObject(q); |
725 |
< |
out.close(); |
646 |
> |
Queue x = populatedQueue(SIZE); |
647 |
> |
Queue y = serialClone(x); |
648 |
|
|
649 |
< |
ByteArrayInputStream bin |
650 |
< |
= new ByteArrayInputStream(bout.toByteArray()); |
651 |
< |
ObjectInputStream in |
652 |
< |
= new ObjectInputStream(new BufferedInputStream(bin)); |
653 |
< |
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
654 |
< |
|
655 |
< |
assertEquals(q.size(), r.size()); |
734 |
< |
while (!q.isEmpty()) { |
735 |
< |
assertEquals(q.remove(), r.remove()); |
649 |
> |
assertNotSame(y, x); |
650 |
> |
assertEquals(x.size(), y.size()); |
651 |
> |
assertEquals(x.toString(), y.toString()); |
652 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
653 |
> |
while (!x.isEmpty()) { |
654 |
> |
assertFalse(y.isEmpty()); |
655 |
> |
assertEquals(x.remove(), y.remove()); |
656 |
|
} |
657 |
< |
} |
738 |
< |
|
739 |
< |
/** |
740 |
< |
* drainTo(null) throws NullPointerException |
741 |
< |
*/ |
742 |
< |
public void testDrainToNull() { |
743 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
744 |
< |
try { |
745 |
< |
q.drainTo(null); |
746 |
< |
shouldThrow(); |
747 |
< |
} catch (NullPointerException success) {} |
748 |
< |
} |
749 |
< |
|
750 |
< |
/** |
751 |
< |
* drainTo(this) throws IllegalArgumentException |
752 |
< |
*/ |
753 |
< |
public void testDrainToSelf() { |
754 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
755 |
< |
try { |
756 |
< |
q.drainTo(q); |
757 |
< |
shouldThrow(); |
758 |
< |
} catch (IllegalArgumentException success) {} |
657 |
> |
assertTrue(y.isEmpty()); |
658 |
|
} |
659 |
|
|
660 |
|
/** |
664 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
665 |
|
ArrayList l = new ArrayList(); |
666 |
|
q.drainTo(l); |
667 |
< |
assertEquals(q.size(), 0); |
668 |
< |
assertEquals(l.size(), SIZE); |
667 |
> |
assertEquals(0, q.size()); |
668 |
> |
assertEquals(SIZE, l.size()); |
669 |
|
for (int i = 0; i < SIZE; ++i) { |
670 |
< |
assertEquals(l.get(i), i); |
670 |
> |
assertEquals(i, l.get(i)); |
671 |
|
} |
672 |
|
q.add(zero); |
673 |
|
q.add(one); |
676 |
|
assertTrue(q.contains(one)); |
677 |
|
l.clear(); |
678 |
|
q.drainTo(l); |
679 |
< |
assertEquals(q.size(), 0); |
680 |
< |
assertEquals(l.size(), 2); |
679 |
> |
assertEquals(0, q.size()); |
680 |
> |
assertEquals(2, l.size()); |
681 |
|
for (int i = 0; i < 2; ++i) { |
682 |
< |
assertEquals(l.get(i), i); |
682 |
> |
assertEquals(i, l.get(i)); |
683 |
|
} |
684 |
|
} |
685 |
|
|
695 |
|
ArrayList l = new ArrayList(); |
696 |
|
q.drainTo(l); |
697 |
|
assertTrue(l.size() >= SIZE); |
698 |
< |
for (int i = 0; i < SIZE; ++i) { |
699 |
< |
assertEquals(l.get(i), i); |
700 |
< |
} |
802 |
< |
t.join(); |
698 |
> |
for (int i = 0; i < SIZE; ++i) |
699 |
> |
assertEquals(i, l.get(i)); |
700 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
701 |
|
assertTrue(q.size() + l.size() >= SIZE); |
702 |
|
} |
703 |
|
|
704 |
|
/** |
705 |
< |
* drainTo(null, n) throws NullPointerException |
808 |
< |
*/ |
809 |
< |
public void testDrainToNullN() { |
810 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
811 |
< |
try { |
812 |
< |
q.drainTo(null, SIZE); |
813 |
< |
shouldThrow(); |
814 |
< |
} catch (NullPointerException success) {} |
815 |
< |
} |
816 |
< |
|
817 |
< |
/** |
818 |
< |
* drainTo(this, n) throws IllegalArgumentException |
819 |
< |
*/ |
820 |
< |
public void testDrainToSelfN() { |
821 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
822 |
< |
try { |
823 |
< |
q.drainTo(q, SIZE); |
824 |
< |
shouldThrow(); |
825 |
< |
} catch (IllegalArgumentException success) {} |
826 |
< |
} |
827 |
< |
|
828 |
< |
/** |
829 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
705 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
706 |
|
*/ |
707 |
|
public void testDrainToN() { |
708 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
713 |
|
ArrayList l = new ArrayList(); |
714 |
|
q.drainTo(l, i); |
715 |
|
int k = (i < SIZE) ? i : SIZE; |
716 |
< |
assertEquals(l.size(), k); |
717 |
< |
assertEquals(q.size(), SIZE - k); |
718 |
< |
for (int j = 0; j < k; ++j) { |
719 |
< |
assertEquals(l.get(j), j); |
720 |
< |
} |
845 |
< |
while (q.poll() != null) |
846 |
< |
; |
716 |
> |
assertEquals(k, l.size()); |
717 |
> |
assertEquals(SIZE - k, q.size()); |
718 |
> |
for (int j = 0; j < k; ++j) |
719 |
> |
assertEquals(j, l.get(j)); |
720 |
> |
do {} while (q.poll() != null); |
721 |
|
} |
722 |
|
} |
723 |
|
|
727 |
|
*/ |
728 |
|
public void testWaitingConsumer() throws InterruptedException { |
729 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
730 |
< |
assertEquals(q.getWaitingConsumerCount(), 0); |
730 |
> |
assertEquals(0, q.getWaitingConsumerCount()); |
731 |
|
assertFalse(q.hasWaitingConsumer()); |
732 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
733 |
|
|
734 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
735 |
|
public void realRun() throws InterruptedException { |
736 |
< |
Thread.sleep(SMALL_DELAY_MS); |
737 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
738 |
< |
threadAssertEquals(q.getWaitingConsumerCount(), 1); |
739 |
< |
threadAssertTrue(q.offer(new Object())); |
865 |
< |
threadAssertFalse(q.hasWaitingConsumer()); |
866 |
< |
threadAssertEquals(q.getWaitingConsumerCount(), 0); |
736 |
> |
threadStarted.countDown(); |
737 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
738 |
> |
assertEquals(0, q.getWaitingConsumerCount()); |
739 |
> |
assertFalse(q.hasWaitingConsumer()); |
740 |
|
}}); |
741 |
|
|
742 |
< |
assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); |
743 |
< |
assertEquals(q.getWaitingConsumerCount(), 0); |
742 |
> |
threadStarted.await(); |
743 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
744 |
> |
assertEquals(1, q.getWaitingConsumerCount()); |
745 |
> |
assertTrue(q.hasWaitingConsumer()); |
746 |
> |
|
747 |
> |
assertTrue(q.offer(one)); |
748 |
> |
assertEquals(0, q.getWaitingConsumerCount()); |
749 |
|
assertFalse(q.hasWaitingConsumer()); |
750 |
< |
t.join(); |
750 |
> |
|
751 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
752 |
|
} |
753 |
|
|
754 |
|
/** |
769 |
|
public void testTransfer2() throws InterruptedException { |
770 |
|
final LinkedTransferQueue<Integer> q |
771 |
|
= new LinkedTransferQueue<Integer>(); |
772 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
773 |
|
|
774 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
775 |
|
public void realRun() throws InterruptedException { |
776 |
< |
q.transfer(SIZE); |
777 |
< |
threadAssertTrue(q.isEmpty()); |
776 |
> |
threadStarted.countDown(); |
777 |
> |
q.transfer(five); |
778 |
> |
checkEmpty(q); |
779 |
|
}}); |
780 |
|
|
781 |
< |
Thread.sleep(SHORT_DELAY_MS); |
781 |
> |
threadStarted.await(); |
782 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
783 |
|
assertEquals(1, q.size()); |
784 |
< |
assertEquals(SIZE, (int) q.poll()); |
785 |
< |
assertTrue(q.isEmpty()); |
786 |
< |
t.join(); |
784 |
> |
assertSame(five, q.poll()); |
785 |
> |
checkEmpty(q); |
786 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
787 |
|
} |
788 |
|
|
789 |
|
/** |
795 |
|
|
796 |
|
Thread first = newStartedThread(new CheckedRunnable() { |
797 |
|
public void realRun() throws InterruptedException { |
798 |
< |
Integer i = SIZE + 1; |
799 |
< |
q.transfer(i); |
800 |
< |
threadAssertTrue(!q.contains(i)); |
919 |
< |
threadAssertEquals(1, q.size()); |
798 |
> |
q.transfer(four); |
799 |
> |
assertTrue(!q.contains(four)); |
800 |
> |
assertEquals(1, q.size()); |
801 |
|
}}); |
802 |
|
|
803 |
|
Thread interruptedThread = newStartedThread( |
804 |
|
new CheckedInterruptedRunnable() { |
805 |
|
public void realRun() throws InterruptedException { |
806 |
< |
while (q.size() == 0) |
806 |
> |
while (q.isEmpty()) |
807 |
|
Thread.yield(); |
808 |
< |
q.transfer(SIZE); |
808 |
> |
q.transfer(five); |
809 |
|
}}); |
810 |
|
|
811 |
|
while (q.size() < 2) |
812 |
|
Thread.yield(); |
813 |
|
assertEquals(2, q.size()); |
814 |
< |
assertEquals(SIZE + 1, (int) q.poll()); |
814 |
> |
assertSame(four, q.poll()); |
815 |
|
first.join(); |
816 |
|
assertEquals(1, q.size()); |
817 |
|
interruptedThread.interrupt(); |
818 |
|
interruptedThread.join(); |
819 |
< |
assertEquals(0, q.size()); |
939 |
< |
assertTrue(q.isEmpty()); |
819 |
> |
checkEmpty(q); |
820 |
|
} |
821 |
|
|
822 |
|
/** |
829 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
830 |
|
public void realRun() throws InterruptedException { |
831 |
|
q.transfer(four); |
832 |
< |
threadAssertFalse(q.contains(four)); |
833 |
< |
threadAssertEquals(three, q.poll()); |
832 |
> |
assertFalse(q.contains(four)); |
833 |
> |
assertSame(three, q.poll()); |
834 |
|
}}); |
835 |
|
|
836 |
< |
Thread.sleep(SHORT_DELAY_MS); |
836 |
> |
while (q.isEmpty()) |
837 |
> |
Thread.yield(); |
838 |
> |
assertFalse(q.isEmpty()); |
839 |
> |
assertEquals(1, q.size()); |
840 |
|
assertTrue(q.offer(three)); |
841 |
< |
assertEquals(four, q.poll()); |
842 |
< |
t.join(); |
841 |
> |
assertSame(four, q.poll()); |
842 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
843 |
|
} |
844 |
|
|
845 |
|
/** |
852 |
|
|
853 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
854 |
|
public void realRun() throws InterruptedException { |
855 |
< |
q.transfer(SIZE); |
855 |
> |
q.transfer(four); |
856 |
|
checkEmpty(q); |
857 |
|
}}); |
858 |
|
|
859 |
< |
Thread.sleep(SHORT_DELAY_MS); |
860 |
< |
assertEquals(SIZE, (int) q.take()); |
859 |
> |
while (q.isEmpty()) |
860 |
> |
Thread.yield(); |
861 |
> |
assertFalse(q.isEmpty()); |
862 |
> |
assertEquals(1, q.size()); |
863 |
> |
assertSame(four, q.take()); |
864 |
|
checkEmpty(q); |
865 |
< |
t.join(); |
865 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
866 |
|
} |
867 |
|
|
868 |
|
/** |
869 |
|
* tryTransfer(null) throws NullPointerException |
870 |
|
*/ |
871 |
|
public void testTryTransfer1() { |
872 |
+ |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
873 |
|
try { |
987 |
– |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
874 |
|
q.tryTransfer(null); |
875 |
|
shouldThrow(); |
876 |
|
} catch (NullPointerException success) {} |
899 |
|
public void realRun() { |
900 |
|
while (! q.hasWaitingConsumer()) |
901 |
|
Thread.yield(); |
902 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
903 |
< |
threadAssertTrue(q.isEmpty()); |
904 |
< |
threadAssertTrue(q.size() == 0); |
1019 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
902 |
> |
assertTrue(q.hasWaitingConsumer()); |
903 |
> |
checkEmpty(q); |
904 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
905 |
|
}}); |
906 |
|
|
907 |
< |
assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato); |
907 |
> |
assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
908 |
|
checkEmpty(q); |
909 |
< |
t.join(); |
909 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
910 |
|
} |
911 |
|
|
912 |
|
/** |
921 |
|
public void realRun() { |
922 |
|
while (! q.hasWaitingConsumer()) |
923 |
|
Thread.yield(); |
924 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
925 |
< |
threadAssertTrue(q.isEmpty()); |
926 |
< |
threadAssertTrue(q.size() == 0); |
1042 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
924 |
> |
assertTrue(q.hasWaitingConsumer()); |
925 |
> |
checkEmpty(q); |
926 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
927 |
|
}}); |
928 |
|
|
929 |
< |
assertTrue(q.take() == hotPotato); |
929 |
> |
assertSame(q.take(), hotPotato); |
930 |
|
checkEmpty(q); |
931 |
< |
t.join(); |
931 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
932 |
|
} |
933 |
|
|
934 |
|
/** |
935 |
< |
* tryTransfer waits the amount given if interrupted, and |
1052 |
< |
* throws interrupted exception |
935 |
> |
* tryTransfer blocks interruptibly if no takers |
936 |
|
*/ |
937 |
|
public void testTryTransfer5() throws InterruptedException { |
938 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
939 |
+ |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
940 |
+ |
assertTrue(q.isEmpty()); |
941 |
|
|
942 |
< |
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
942 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
943 |
|
public void realRun() throws InterruptedException { |
944 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
944 |
> |
Thread.currentThread().interrupt(); |
945 |
> |
try { |
946 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
947 |
> |
shouldThrow(); |
948 |
> |
} catch (InterruptedException success) {} |
949 |
> |
assertFalse(Thread.interrupted()); |
950 |
> |
|
951 |
> |
pleaseInterrupt.countDown(); |
952 |
> |
try { |
953 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
954 |
> |
shouldThrow(); |
955 |
> |
} catch (InterruptedException success) {} |
956 |
> |
assertFalse(Thread.interrupted()); |
957 |
|
}}); |
958 |
|
|
959 |
< |
Thread.sleep(SMALL_DELAY_MS); |
960 |
< |
toInterrupt.interrupt(); |
961 |
< |
toInterrupt.join(); |
959 |
> |
await(pleaseInterrupt); |
960 |
> |
assertThreadStaysAlive(t); |
961 |
> |
t.interrupt(); |
962 |
> |
awaitTermination(t); |
963 |
> |
checkEmpty(q); |
964 |
|
} |
965 |
|
|
966 |
|
/** |
967 |
< |
* tryTransfer gives up after the timeout and return false |
967 |
> |
* tryTransfer gives up after the timeout and returns false |
968 |
|
*/ |
969 |
|
public void testTryTransfer6() throws InterruptedException { |
970 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
971 |
|
|
972 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
973 |
|
public void realRun() throws InterruptedException { |
974 |
< |
threadAssertFalse |
975 |
< |
(q.tryTransfer(new Object(), |
976 |
< |
SHORT_DELAY_MS, MILLISECONDS)); |
974 |
> |
long t0 = System.nanoTime(); |
975 |
> |
assertFalse(q.tryTransfer(new Object(), |
976 |
> |
timeoutMillis(), MILLISECONDS)); |
977 |
> |
assertTrue(millisElapsedSince(t0) >= timeoutMillis()); |
978 |
> |
checkEmpty(q); |
979 |
|
}}); |
980 |
|
|
981 |
< |
Thread.sleep(SMALL_DELAY_MS); |
981 |
> |
awaitTermination(t); |
982 |
|
checkEmpty(q); |
1082 |
– |
t.join(); |
983 |
|
} |
984 |
|
|
985 |
|
/** |
992 |
|
|
993 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
994 |
|
public void realRun() throws InterruptedException { |
995 |
< |
threadAssertTrue(q.tryTransfer(five, |
996 |
< |
MEDIUM_DELAY_MS, MILLISECONDS)); |
1097 |
< |
threadAssertTrue(q.isEmpty()); |
995 |
> |
assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); |
996 |
> |
checkEmpty(q); |
997 |
|
}}); |
998 |
|
|
999 |
< |
Thread.sleep(SHORT_DELAY_MS); |
999 |
> |
while (q.size() != 2) |
1000 |
> |
Thread.yield(); |
1001 |
|
assertEquals(2, q.size()); |
1002 |
< |
assertEquals(four, q.poll()); |
1003 |
< |
assertEquals(five, q.poll()); |
1002 |
> |
assertSame(four, q.poll()); |
1003 |
> |
assertSame(five, q.poll()); |
1004 |
|
checkEmpty(q); |
1005 |
< |
t.join(); |
1005 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1006 |
|
} |
1007 |
|
|
1008 |
|
/** |
1009 |
< |
* tryTransfer attempts to enqueue into the q and fails returning |
1010 |
< |
* false not enqueueing and the successive poll is null |
1009 |
> |
* tryTransfer attempts to enqueue into the queue and fails |
1010 |
> |
* returning false not enqueueing and the successive poll is null |
1011 |
|
*/ |
1012 |
|
public void testTryTransfer8() throws InterruptedException { |
1013 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1014 |
|
assertTrue(q.offer(four)); |
1015 |
|
assertEquals(1, q.size()); |
1016 |
< |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1016 |
> |
long t0 = System.nanoTime(); |
1017 |
> |
assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS)); |
1018 |
> |
assertTrue(millisElapsedSince(t0) >= timeoutMillis()); |
1019 |
|
assertEquals(1, q.size()); |
1020 |
< |
assertEquals(four, q.poll()); |
1020 |
> |
assertSame(four, q.poll()); |
1021 |
|
assertNull(q.poll()); |
1022 |
|
checkEmpty(q); |
1023 |
|
} |
1024 |
|
|
1025 |
|
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1026 |
|
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
1027 |
< |
assertTrue(q.isEmpty()); |
1027 |
> |
checkEmpty(q); |
1028 |
|
for (int i = 0; i < n; i++) { |
1029 |
|
assertEquals(i, q.size()); |
1030 |
|
assertTrue(q.offer(i)); |
1033 |
|
assertFalse(q.isEmpty()); |
1034 |
|
return q; |
1035 |
|
} |
1036 |
+ |
|
1037 |
+ |
/** |
1038 |
+ |
* remove(null), contains(null) always return false |
1039 |
+ |
*/ |
1040 |
+ |
public void testNeverContainsNull() { |
1041 |
+ |
Collection<?>[] qs = { |
1042 |
+ |
new LinkedTransferQueue<Object>(), |
1043 |
+ |
populatedQueue(2), |
1044 |
+ |
}; |
1045 |
+ |
|
1046 |
+ |
for (Collection<?> q : qs) { |
1047 |
+ |
assertFalse(q.contains(null)); |
1048 |
+ |
assertFalse(q.remove(null)); |
1049 |
+ |
} |
1050 |
+ |
} |
1051 |
|
} |