1 |
dl |
1.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 |
jsr166 |
1.44 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.1 |
* Other contributors include John Vint |
6 |
|
|
*/ |
7 |
|
|
|
8 |
jsr166 |
1.55 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
9 |
|
|
|
10 |
|
|
import java.util.ArrayList; |
11 |
jsr166 |
1.48 |
import java.util.Arrays; |
12 |
jsr166 |
1.47 |
import java.util.Collection; |
13 |
dl |
1.1 |
import java.util.Iterator; |
14 |
jsr166 |
1.12 |
import java.util.List; |
15 |
dl |
1.1 |
import java.util.NoSuchElementException; |
16 |
jsr166 |
1.48 |
import java.util.Queue; |
17 |
jsr166 |
1.47 |
import java.util.concurrent.BlockingQueue; |
18 |
jsr166 |
1.70 |
import java.util.concurrent.Callable; |
19 |
jsr166 |
1.47 |
import java.util.concurrent.CountDownLatch; |
20 |
|
|
import java.util.concurrent.Executors; |
21 |
|
|
import java.util.concurrent.ExecutorService; |
22 |
|
|
import java.util.concurrent.LinkedTransferQueue; |
23 |
jsr166 |
1.55 |
|
24 |
|
|
import junit.framework.Test; |
25 |
dl |
1.1 |
|
26 |
jsr166 |
1.10 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
27 |
dl |
1.1 |
public class LinkedTransferQueueTest extends JSR166TestCase { |
28 |
jsr166 |
1.25 |
public static class Generic extends BlockingQueueTest { |
29 |
|
|
protected BlockingQueue emptyCollection() { |
30 |
|
|
return new LinkedTransferQueue(); |
31 |
|
|
} |
32 |
|
|
} |
33 |
|
|
|
34 |
dl |
1.1 |
public static void main(String[] args) { |
35 |
jsr166 |
1.59 |
main(suite(), args); |
36 |
dl |
1.1 |
} |
37 |
|
|
|
38 |
|
|
public static Test suite() { |
39 |
jsr166 |
1.67 |
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 |
jsr166 |
1.25 |
return newTestSuite(LinkedTransferQueueTest.class, |
47 |
jsr166 |
1.62 |
new Generic().testSuite(), |
48 |
|
|
CollectionTest.testSuite(new Implementation())); |
49 |
dl |
1.1 |
} |
50 |
|
|
|
51 |
jsr166 |
1.3 |
/** |
52 |
jsr166 |
1.7 |
* Constructor builds new queue with size being zero and empty |
53 |
|
|
* being true |
54 |
dl |
1.1 |
*/ |
55 |
|
|
public void testConstructor1() { |
56 |
|
|
assertEquals(0, new LinkedTransferQueue().size()); |
57 |
|
|
assertTrue(new LinkedTransferQueue().isEmpty()); |
58 |
|
|
} |
59 |
|
|
|
60 |
jsr166 |
1.3 |
/** |
61 |
jsr166 |
1.7 |
* Initializing constructor with null collection throws |
62 |
|
|
* NullPointerException |
63 |
dl |
1.1 |
*/ |
64 |
|
|
public void testConstructor2() { |
65 |
|
|
try { |
66 |
|
|
new LinkedTransferQueue(null); |
67 |
|
|
shouldThrow(); |
68 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
69 |
dl |
1.1 |
} |
70 |
|
|
|
71 |
|
|
/** |
72 |
jsr166 |
1.7 |
* Initializing from Collection of null elements throws |
73 |
|
|
* NullPointerException |
74 |
dl |
1.1 |
*/ |
75 |
|
|
public void testConstructor3() { |
76 |
jsr166 |
1.47 |
Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
77 |
dl |
1.1 |
try { |
78 |
jsr166 |
1.47 |
new LinkedTransferQueue(elements); |
79 |
dl |
1.1 |
shouldThrow(); |
80 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
81 |
dl |
1.1 |
} |
82 |
jsr166 |
1.3 |
|
83 |
|
|
/** |
84 |
dl |
1.1 |
* Initializing constructor with a collection containing some null elements |
85 |
jsr166 |
1.5 |
* throws NullPointerException |
86 |
dl |
1.1 |
*/ |
87 |
|
|
public void testConstructor4() { |
88 |
jsr166 |
1.47 |
Integer[] ints = new Integer[SIZE]; |
89 |
jsr166 |
1.61 |
for (int i = 0; i < SIZE - 1; ++i) |
90 |
jsr166 |
1.47 |
ints[i] = i; |
91 |
|
|
Collection<Integer> elements = Arrays.asList(ints); |
92 |
dl |
1.1 |
try { |
93 |
jsr166 |
1.47 |
new LinkedTransferQueue(elements); |
94 |
dl |
1.1 |
shouldThrow(); |
95 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
96 |
dl |
1.1 |
} |
97 |
|
|
|
98 |
jsr166 |
1.3 |
/** |
99 |
dl |
1.1 |
* Queue contains all elements of the collection it is initialized by |
100 |
|
|
*/ |
101 |
|
|
public void testConstructor5() { |
102 |
jsr166 |
1.12 |
Integer[] ints = new Integer[SIZE]; |
103 |
|
|
for (int i = 0; i < SIZE; ++i) { |
104 |
|
|
ints[i] = i; |
105 |
|
|
} |
106 |
|
|
List intList = Arrays.asList(ints); |
107 |
|
|
LinkedTransferQueue q |
108 |
|
|
= new LinkedTransferQueue(intList); |
109 |
|
|
assertEquals(q.size(), intList.size()); |
110 |
|
|
assertEquals(q.toString(), intList.toString()); |
111 |
|
|
assertTrue(Arrays.equals(q.toArray(), |
112 |
|
|
intList.toArray())); |
113 |
|
|
assertTrue(Arrays.equals(q.toArray(new Object[0]), |
114 |
|
|
intList.toArray(new Object[0]))); |
115 |
|
|
assertTrue(Arrays.equals(q.toArray(new Object[SIZE]), |
116 |
|
|
intList.toArray(new Object[SIZE]))); |
117 |
|
|
for (int i = 0; i < SIZE; ++i) { |
118 |
|
|
assertEquals(ints[i], q.poll()); |
119 |
dl |
1.1 |
} |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
/** |
123 |
jsr166 |
1.12 |
* remainingCapacity() always returns Integer.MAX_VALUE |
124 |
dl |
1.1 |
*/ |
125 |
|
|
public void testRemainingCapacity() { |
126 |
jsr166 |
1.58 |
BlockingQueue q = populatedQueue(SIZE); |
127 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
128 |
jsr166 |
1.5 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
129 |
dl |
1.1 |
assertEquals(SIZE - i, q.size()); |
130 |
jsr166 |
1.58 |
assertEquals(i, q.remove()); |
131 |
dl |
1.1 |
} |
132 |
|
|
for (int i = 0; i < SIZE; ++i) { |
133 |
jsr166 |
1.5 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
134 |
dl |
1.1 |
assertEquals(i, q.size()); |
135 |
jsr166 |
1.58 |
assertTrue(q.add(i)); |
136 |
dl |
1.1 |
} |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
/** |
140 |
jsr166 |
1.5 |
* addAll(this) throws IllegalArgumentException |
141 |
dl |
1.1 |
*/ |
142 |
|
|
public void testAddAllSelf() { |
143 |
jsr166 |
1.60 |
LinkedTransferQueue q = populatedQueue(SIZE); |
144 |
dl |
1.1 |
try { |
145 |
|
|
q.addAll(q); |
146 |
|
|
shouldThrow(); |
147 |
jsr166 |
1.17 |
} catch (IllegalArgumentException success) {} |
148 |
dl |
1.1 |
} |
149 |
|
|
|
150 |
|
|
/** |
151 |
jsr166 |
1.7 |
* addAll of a collection with any null elements throws |
152 |
|
|
* NullPointerException after possibly adding some elements |
153 |
dl |
1.1 |
*/ |
154 |
|
|
public void testAddAll3() { |
155 |
jsr166 |
1.60 |
LinkedTransferQueue q = new LinkedTransferQueue(); |
156 |
|
|
Integer[] ints = new Integer[SIZE]; |
157 |
|
|
for (int i = 0; i < SIZE - 1; ++i) |
158 |
|
|
ints[i] = i; |
159 |
dl |
1.1 |
try { |
160 |
|
|
q.addAll(Arrays.asList(ints)); |
161 |
|
|
shouldThrow(); |
162 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
163 |
dl |
1.1 |
} |
164 |
|
|
|
165 |
|
|
/** |
166 |
|
|
* Queue contains all elements, in traversal order, of successful addAll |
167 |
|
|
*/ |
168 |
|
|
public void testAddAll5() { |
169 |
jsr166 |
1.5 |
Integer[] empty = new Integer[0]; |
170 |
|
|
Integer[] ints = new Integer[SIZE]; |
171 |
|
|
for (int i = 0; i < SIZE; ++i) { |
172 |
|
|
ints[i] = i; |
173 |
|
|
} |
174 |
|
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
175 |
|
|
assertFalse(q.addAll(Arrays.asList(empty))); |
176 |
|
|
assertTrue(q.addAll(Arrays.asList(ints))); |
177 |
|
|
for (int i = 0; i < SIZE; ++i) { |
178 |
|
|
assertEquals(ints[i], q.poll()); |
179 |
dl |
1.1 |
} |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
/** |
183 |
|
|
* all elements successfully put are contained |
184 |
|
|
*/ |
185 |
|
|
public void testPut() { |
186 |
jsr166 |
1.68 |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
187 |
jsr166 |
1.5 |
for (int i = 0; i < SIZE; ++i) { |
188 |
jsr166 |
1.51 |
assertEquals(i, q.size()); |
189 |
jsr166 |
1.5 |
q.put(i); |
190 |
|
|
assertTrue(q.contains(i)); |
191 |
dl |
1.1 |
} |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
/** |
195 |
|
|
* take retrieves elements in FIFO order |
196 |
|
|
*/ |
197 |
jsr166 |
1.5 |
public void testTake() throws InterruptedException { |
198 |
|
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
199 |
|
|
for (int i = 0; i < SIZE; ++i) { |
200 |
|
|
assertEquals(i, (int) q.take()); |
201 |
dl |
1.1 |
} |
202 |
|
|
} |
203 |
|
|
|
204 |
|
|
/** |
205 |
jsr166 |
1.28 |
* take removes existing elements until empty, then blocks interruptibly |
206 |
dl |
1.1 |
*/ |
207 |
jsr166 |
1.5 |
public void testBlockingTake() throws InterruptedException { |
208 |
jsr166 |
1.46 |
final BlockingQueue q = populatedQueue(SIZE); |
209 |
|
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
210 |
jsr166 |
1.31 |
Thread t = newStartedThread(new CheckedRunnable() { |
211 |
jsr166 |
1.19 |
public void realRun() throws InterruptedException { |
212 |
jsr166 |
1.73 |
for (int i = 0; i < SIZE; i++) assertEquals(i, q.take()); |
213 |
jsr166 |
1.46 |
|
214 |
|
|
Thread.currentThread().interrupt(); |
215 |
|
|
try { |
216 |
|
|
q.take(); |
217 |
|
|
shouldThrow(); |
218 |
|
|
} catch (InterruptedException success) {} |
219 |
|
|
assertFalse(Thread.interrupted()); |
220 |
|
|
|
221 |
|
|
pleaseInterrupt.countDown(); |
222 |
jsr166 |
1.19 |
try { |
223 |
|
|
q.take(); |
224 |
|
|
shouldThrow(); |
225 |
|
|
} catch (InterruptedException success) {} |
226 |
jsr166 |
1.46 |
assertFalse(Thread.interrupted()); |
227 |
jsr166 |
1.5 |
}}); |
228 |
jsr166 |
1.19 |
|
229 |
jsr166 |
1.46 |
await(pleaseInterrupt); |
230 |
jsr166 |
1.82 |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
231 |
jsr166 |
1.5 |
t.interrupt(); |
232 |
jsr166 |
1.46 |
awaitTermination(t); |
233 |
dl |
1.1 |
} |
234 |
|
|
|
235 |
|
|
/** |
236 |
|
|
* poll succeeds unless empty |
237 |
|
|
*/ |
238 |
jsr166 |
1.12 |
public void testPoll() throws InterruptedException { |
239 |
jsr166 |
1.5 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
240 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
241 |
jsr166 |
1.5 |
assertEquals(i, (int) q.poll()); |
242 |
dl |
1.1 |
} |
243 |
|
|
assertNull(q.poll()); |
244 |
jsr166 |
1.12 |
checkEmpty(q); |
245 |
dl |
1.1 |
} |
246 |
|
|
|
247 |
|
|
/** |
248 |
jsr166 |
1.30 |
* timed poll with zero timeout succeeds when non-empty, else times out |
249 |
dl |
1.1 |
*/ |
250 |
jsr166 |
1.5 |
public void testTimedPoll0() throws InterruptedException { |
251 |
|
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
252 |
|
|
for (int i = 0; i < SIZE; ++i) { |
253 |
jsr166 |
1.7 |
assertEquals(i, (int) q.poll(0, MILLISECONDS)); |
254 |
dl |
1.1 |
} |
255 |
jsr166 |
1.7 |
assertNull(q.poll(0, MILLISECONDS)); |
256 |
jsr166 |
1.12 |
checkEmpty(q); |
257 |
dl |
1.1 |
} |
258 |
|
|
|
259 |
|
|
/** |
260 |
jsr166 |
1.30 |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
261 |
dl |
1.1 |
*/ |
262 |
jsr166 |
1.5 |
public void testTimedPoll() throws InterruptedException { |
263 |
|
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
264 |
jsr166 |
1.66 |
long startTime = System.nanoTime(); |
265 |
|
|
for (int i = 0; i < SIZE; ++i) |
266 |
jsr166 |
1.46 |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
267 |
jsr166 |
1.66 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
268 |
|
|
|
269 |
|
|
startTime = System.nanoTime(); |
270 |
jsr166 |
1.46 |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
271 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
272 |
jsr166 |
1.12 |
checkEmpty(q); |
273 |
dl |
1.1 |
} |
274 |
|
|
|
275 |
|
|
/** |
276 |
|
|
* Interrupted timed poll throws InterruptedException instead of |
277 |
|
|
* returning timeout status |
278 |
|
|
*/ |
279 |
jsr166 |
1.5 |
public void testInterruptedTimedPoll() throws InterruptedException { |
280 |
jsr166 |
1.31 |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
281 |
jsr166 |
1.76 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
282 |
jsr166 |
1.16 |
Thread t = newStartedThread(new CheckedRunnable() { |
283 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
284 |
jsr166 |
1.78 |
for (int i = 0; i < SIZE; i++) |
285 |
jsr166 |
1.23 |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
286 |
jsr166 |
1.76 |
|
287 |
jsr166 |
1.78 |
Thread.currentThread().interrupt(); |
288 |
|
|
try { |
289 |
jsr166 |
1.82 |
q.poll(randomTimeout(), randomTimeUnit()); |
290 |
jsr166 |
1.78 |
shouldThrow(); |
291 |
|
|
} catch (InterruptedException success) {} |
292 |
|
|
assertFalse(Thread.interrupted()); |
293 |
|
|
|
294 |
jsr166 |
1.76 |
pleaseInterrupt.countDown(); |
295 |
jsr166 |
1.16 |
try { |
296 |
jsr166 |
1.83 |
q.poll(LONGER_DELAY_MS, MILLISECONDS); |
297 |
jsr166 |
1.17 |
shouldThrow(); |
298 |
jsr166 |
1.66 |
} catch (InterruptedException success) {} |
299 |
jsr166 |
1.76 |
assertFalse(Thread.interrupted()); |
300 |
jsr166 |
1.5 |
}}); |
301 |
jsr166 |
1.17 |
|
302 |
jsr166 |
1.76 |
await(pleaseInterrupt); |
303 |
jsr166 |
1.82 |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
304 |
jsr166 |
1.5 |
t.interrupt(); |
305 |
jsr166 |
1.64 |
awaitTermination(t); |
306 |
jsr166 |
1.12 |
checkEmpty(q); |
307 |
dl |
1.1 |
} |
308 |
|
|
|
309 |
|
|
/** |
310 |
jsr166 |
1.31 |
* timed poll after thread interrupted throws InterruptedException |
311 |
|
|
* instead of returning timeout status |
312 |
|
|
*/ |
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 |
jsr166 |
1.66 |
for (int i = 0; i < SIZE; ++i) |
319 |
jsr166 |
1.82 |
assertEquals(i, (int) q.poll(randomTimeout(), randomTimeUnit())); |
320 |
jsr166 |
1.31 |
try { |
321 |
jsr166 |
1.82 |
q.poll(randomTimeout(), randomTimeUnit()); |
322 |
jsr166 |
1.31 |
shouldThrow(); |
323 |
|
|
} catch (InterruptedException success) {} |
324 |
jsr166 |
1.77 |
assertFalse(Thread.interrupted()); |
325 |
jsr166 |
1.31 |
}}); |
326 |
|
|
|
327 |
jsr166 |
1.66 |
awaitTermination(t); |
328 |
jsr166 |
1.31 |
checkEmpty(q); |
329 |
|
|
} |
330 |
|
|
|
331 |
|
|
/** |
332 |
dl |
1.1 |
* peek returns next element, or null if empty |
333 |
|
|
*/ |
334 |
jsr166 |
1.12 |
public void testPeek() throws InterruptedException { |
335 |
jsr166 |
1.5 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
336 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
337 |
jsr166 |
1.5 |
assertEquals(i, (int) q.peek()); |
338 |
|
|
assertEquals(i, (int) q.poll()); |
339 |
dl |
1.1 |
assertTrue(q.peek() == null || |
340 |
jsr166 |
1.5 |
i != (int) q.peek()); |
341 |
dl |
1.1 |
} |
342 |
|
|
assertNull(q.peek()); |
343 |
jsr166 |
1.12 |
checkEmpty(q); |
344 |
dl |
1.1 |
} |
345 |
|
|
|
346 |
|
|
/** |
347 |
jsr166 |
1.5 |
* element returns next element, or throws NoSuchElementException if empty |
348 |
dl |
1.1 |
*/ |
349 |
jsr166 |
1.12 |
public void testElement() throws InterruptedException { |
350 |
jsr166 |
1.5 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
351 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
352 |
jsr166 |
1.5 |
assertEquals(i, (int) q.element()); |
353 |
|
|
assertEquals(i, (int) q.poll()); |
354 |
dl |
1.1 |
} |
355 |
|
|
try { |
356 |
|
|
q.element(); |
357 |
|
|
shouldThrow(); |
358 |
jsr166 |
1.17 |
} catch (NoSuchElementException success) {} |
359 |
jsr166 |
1.12 |
checkEmpty(q); |
360 |
dl |
1.1 |
} |
361 |
|
|
|
362 |
|
|
/** |
363 |
jsr166 |
1.5 |
* remove removes next element, or throws NoSuchElementException if empty |
364 |
dl |
1.1 |
*/ |
365 |
jsr166 |
1.12 |
public void testRemove() throws InterruptedException { |
366 |
jsr166 |
1.5 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
367 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
368 |
jsr166 |
1.5 |
assertEquals(i, (int) q.remove()); |
369 |
dl |
1.1 |
} |
370 |
|
|
try { |
371 |
|
|
q.remove(); |
372 |
|
|
shouldThrow(); |
373 |
jsr166 |
1.17 |
} catch (NoSuchElementException success) {} |
374 |
jsr166 |
1.12 |
checkEmpty(q); |
375 |
dl |
1.1 |
} |
376 |
|
|
|
377 |
|
|
/** |
378 |
|
|
* An add following remove(x) succeeds |
379 |
|
|
*/ |
380 |
jsr166 |
1.5 |
public void testRemoveElementAndAdd() throws InterruptedException { |
381 |
|
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
382 |
|
|
assertTrue(q.add(one)); |
383 |
|
|
assertTrue(q.add(two)); |
384 |
|
|
assertTrue(q.remove(one)); |
385 |
|
|
assertTrue(q.remove(two)); |
386 |
|
|
assertTrue(q.add(three)); |
387 |
jsr166 |
1.23 |
assertSame(q.take(), three); |
388 |
dl |
1.1 |
} |
389 |
|
|
|
390 |
|
|
/** |
391 |
|
|
* contains(x) reports true when elements added but not yet removed |
392 |
|
|
*/ |
393 |
|
|
public void testContains() { |
394 |
jsr166 |
1.5 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
395 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
396 |
jsr166 |
1.5 |
assertTrue(q.contains(i)); |
397 |
|
|
assertEquals(i, (int) q.poll()); |
398 |
|
|
assertFalse(q.contains(i)); |
399 |
dl |
1.1 |
} |
400 |
|
|
} |
401 |
|
|
|
402 |
|
|
/** |
403 |
|
|
* clear removes all elements |
404 |
|
|
*/ |
405 |
jsr166 |
1.12 |
public void testClear() throws InterruptedException { |
406 |
dl |
1.1 |
LinkedTransferQueue q = populatedQueue(SIZE); |
407 |
|
|
q.clear(); |
408 |
jsr166 |
1.12 |
checkEmpty(q); |
409 |
jsr166 |
1.5 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
410 |
dl |
1.1 |
q.add(one); |
411 |
|
|
assertFalse(q.isEmpty()); |
412 |
jsr166 |
1.12 |
assertEquals(1, q.size()); |
413 |
dl |
1.1 |
assertTrue(q.contains(one)); |
414 |
|
|
q.clear(); |
415 |
jsr166 |
1.12 |
checkEmpty(q); |
416 |
dl |
1.1 |
} |
417 |
|
|
|
418 |
|
|
/** |
419 |
|
|
* containsAll(c) is true when c contains a subset of elements |
420 |
|
|
*/ |
421 |
|
|
public void testContainsAll() { |
422 |
jsr166 |
1.5 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
423 |
jsr166 |
1.68 |
LinkedTransferQueue<Integer> p = new LinkedTransferQueue<>(); |
424 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
425 |
|
|
assertTrue(q.containsAll(p)); |
426 |
|
|
assertFalse(p.containsAll(q)); |
427 |
jsr166 |
1.5 |
p.add(i); |
428 |
dl |
1.1 |
} |
429 |
|
|
assertTrue(p.containsAll(q)); |
430 |
|
|
} |
431 |
|
|
|
432 |
|
|
/** |
433 |
jsr166 |
1.7 |
* retainAll(c) retains only those elements of c and reports true |
434 |
|
|
* if changed |
435 |
dl |
1.1 |
*/ |
436 |
|
|
public void testRetainAll() { |
437 |
|
|
LinkedTransferQueue q = populatedQueue(SIZE); |
438 |
|
|
LinkedTransferQueue p = populatedQueue(SIZE); |
439 |
|
|
for (int i = 0; i < SIZE; ++i) { |
440 |
|
|
boolean changed = q.retainAll(p); |
441 |
|
|
if (i == 0) { |
442 |
|
|
assertFalse(changed); |
443 |
|
|
} else { |
444 |
|
|
assertTrue(changed); |
445 |
|
|
} |
446 |
|
|
assertTrue(q.containsAll(p)); |
447 |
|
|
assertEquals(SIZE - i, q.size()); |
448 |
|
|
p.remove(); |
449 |
|
|
} |
450 |
|
|
} |
451 |
|
|
|
452 |
|
|
/** |
453 |
jsr166 |
1.7 |
* removeAll(c) removes only those elements of c and reports true |
454 |
|
|
* if changed |
455 |
dl |
1.1 |
*/ |
456 |
|
|
public void testRemoveAll() { |
457 |
|
|
for (int i = 1; i < SIZE; ++i) { |
458 |
|
|
LinkedTransferQueue q = populatedQueue(SIZE); |
459 |
|
|
LinkedTransferQueue p = populatedQueue(i); |
460 |
|
|
assertTrue(q.removeAll(p)); |
461 |
|
|
assertEquals(SIZE - i, q.size()); |
462 |
|
|
for (int j = 0; j < i; ++j) { |
463 |
jsr166 |
1.5 |
assertFalse(q.contains(p.remove())); |
464 |
dl |
1.1 |
} |
465 |
|
|
} |
466 |
|
|
} |
467 |
|
|
|
468 |
|
|
/** |
469 |
jsr166 |
1.38 |
* toArray() contains all elements in FIFO order |
470 |
dl |
1.1 |
*/ |
471 |
jsr166 |
1.38 |
public void testToArray() { |
472 |
dl |
1.1 |
LinkedTransferQueue q = populatedQueue(SIZE); |
473 |
jsr166 |
1.81 |
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 |
dl |
1.1 |
} |
479 |
|
|
|
480 |
|
|
/** |
481 |
jsr166 |
1.38 |
* toArray(a) contains all elements in FIFO order |
482 |
dl |
1.1 |
*/ |
483 |
jsr166 |
1.38 |
public void testToArray2() { |
484 |
jsr166 |
1.5 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
485 |
dl |
1.1 |
Integer[] ints = new Integer[SIZE]; |
486 |
jsr166 |
1.39 |
Integer[] array = q.toArray(ints); |
487 |
|
|
assertSame(ints, array); |
488 |
jsr166 |
1.81 |
for (Integer o : ints) |
489 |
|
|
assertSame(o, q.poll()); |
490 |
|
|
assertTrue(q.isEmpty()); |
491 |
dl |
1.1 |
} |
492 |
|
|
|
493 |
|
|
/** |
494 |
jsr166 |
1.36 |
* toArray(incompatible array type) throws ArrayStoreException |
495 |
dl |
1.1 |
*/ |
496 |
|
|
public void testToArray1_BadArg() { |
497 |
jsr166 |
1.20 |
LinkedTransferQueue q = populatedQueue(SIZE); |
498 |
dl |
1.1 |
try { |
499 |
jsr166 |
1.36 |
q.toArray(new String[10]); |
500 |
dl |
1.1 |
shouldThrow(); |
501 |
jsr166 |
1.17 |
} catch (ArrayStoreException success) {} |
502 |
dl |
1.1 |
} |
503 |
|
|
|
504 |
|
|
/** |
505 |
|
|
* iterator iterates through all elements |
506 |
|
|
*/ |
507 |
jsr166 |
1.5 |
public void testIterator() throws InterruptedException { |
508 |
dl |
1.1 |
LinkedTransferQueue q = populatedQueue(SIZE); |
509 |
|
|
Iterator it = q.iterator(); |
510 |
jsr166 |
1.57 |
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 |
jsr166 |
1.10 |
assertEquals(i, SIZE); |
520 |
jsr166 |
1.57 |
assertIteratorExhausted(it); |
521 |
|
|
} |
522 |
|
|
|
523 |
|
|
/** |
524 |
|
|
* iterator of empty collection has no elements |
525 |
|
|
*/ |
526 |
|
|
public void testEmptyIterator() { |
527 |
|
|
assertIteratorExhausted(new LinkedTransferQueue().iterator()); |
528 |
dl |
1.1 |
} |
529 |
|
|
|
530 |
|
|
/** |
531 |
jsr166 |
1.12 |
* iterator.remove() removes current element |
532 |
dl |
1.1 |
*/ |
533 |
|
|
public void testIteratorRemove() { |
534 |
|
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
535 |
|
|
q.add(two); |
536 |
|
|
q.add(one); |
537 |
|
|
q.add(three); |
538 |
|
|
|
539 |
|
|
Iterator it = q.iterator(); |
540 |
|
|
it.next(); |
541 |
|
|
it.remove(); |
542 |
|
|
|
543 |
|
|
it = q.iterator(); |
544 |
jsr166 |
1.24 |
assertSame(it.next(), one); |
545 |
|
|
assertSame(it.next(), three); |
546 |
dl |
1.1 |
assertFalse(it.hasNext()); |
547 |
|
|
} |
548 |
|
|
|
549 |
|
|
/** |
550 |
|
|
* iterator ordering is FIFO |
551 |
|
|
*/ |
552 |
|
|
public void testIteratorOrdering() { |
553 |
jsr166 |
1.68 |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
554 |
jsr166 |
1.5 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
555 |
dl |
1.1 |
q.add(one); |
556 |
|
|
q.add(two); |
557 |
|
|
q.add(three); |
558 |
jsr166 |
1.5 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
559 |
dl |
1.1 |
int k = 0; |
560 |
jsr166 |
1.5 |
for (Integer n : q) { |
561 |
|
|
assertEquals(++k, (int) n); |
562 |
dl |
1.1 |
} |
563 |
|
|
assertEquals(3, k); |
564 |
|
|
} |
565 |
|
|
|
566 |
|
|
/** |
567 |
|
|
* Modifications do not cause iterators to fail |
568 |
|
|
*/ |
569 |
|
|
public void testWeaklyConsistentIteration() { |
570 |
|
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
571 |
|
|
q.add(one); |
572 |
|
|
q.add(two); |
573 |
|
|
q.add(three); |
574 |
jsr166 |
1.5 |
for (Iterator it = q.iterator(); it.hasNext();) { |
575 |
|
|
q.remove(); |
576 |
|
|
it.next(); |
577 |
dl |
1.1 |
} |
578 |
|
|
assertEquals(0, q.size()); |
579 |
|
|
} |
580 |
|
|
|
581 |
|
|
/** |
582 |
|
|
* toString contains toStrings of elements |
583 |
|
|
*/ |
584 |
|
|
public void testToString() { |
585 |
|
|
LinkedTransferQueue q = populatedQueue(SIZE); |
586 |
|
|
String s = q.toString(); |
587 |
|
|
for (int i = 0; i < SIZE; ++i) { |
588 |
jsr166 |
1.46 |
assertTrue(s.contains(String.valueOf(i))); |
589 |
dl |
1.1 |
} |
590 |
|
|
} |
591 |
|
|
|
592 |
|
|
/** |
593 |
|
|
* offer transfers elements across Executor tasks |
594 |
|
|
*/ |
595 |
|
|
public void testOfferInExecutor() { |
596 |
|
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
597 |
jsr166 |
1.46 |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
598 |
jsr166 |
1.63 |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
599 |
|
|
try (PoolCleaner cleaner = cleaner(executor)) { |
600 |
jsr166 |
1.7 |
|
601 |
jsr166 |
1.63 |
executor.execute(new CheckedRunnable() { |
602 |
|
|
public void realRun() throws InterruptedException { |
603 |
|
|
threadsStarted.await(); |
604 |
jsr166 |
1.66 |
long startTime = System.nanoTime(); |
605 |
jsr166 |
1.63 |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
606 |
jsr166 |
1.66 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
607 |
jsr166 |
1.63 |
}}); |
608 |
dl |
1.1 |
|
609 |
jsr166 |
1.63 |
executor.execute(new CheckedRunnable() { |
610 |
|
|
public void realRun() throws InterruptedException { |
611 |
|
|
threadsStarted.await(); |
612 |
|
|
assertSame(one, q.take()); |
613 |
|
|
checkEmpty(q); |
614 |
|
|
}}); |
615 |
|
|
} |
616 |
dl |
1.1 |
} |
617 |
|
|
|
618 |
|
|
/** |
619 |
jsr166 |
1.13 |
* timed poll retrieves elements across Executor threads |
620 |
dl |
1.1 |
*/ |
621 |
|
|
public void testPollInExecutor() { |
622 |
|
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
623 |
jsr166 |
1.46 |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
624 |
jsr166 |
1.63 |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
625 |
|
|
try (PoolCleaner cleaner = cleaner(executor)) { |
626 |
jsr166 |
1.7 |
|
627 |
jsr166 |
1.63 |
executor.execute(new CheckedRunnable() { |
628 |
|
|
public void realRun() throws InterruptedException { |
629 |
|
|
assertNull(q.poll()); |
630 |
|
|
threadsStarted.await(); |
631 |
jsr166 |
1.66 |
long startTime = System.nanoTime(); |
632 |
jsr166 |
1.63 |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
633 |
jsr166 |
1.66 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
634 |
jsr166 |
1.63 |
checkEmpty(q); |
635 |
|
|
}}); |
636 |
dl |
1.1 |
|
637 |
jsr166 |
1.63 |
executor.execute(new CheckedRunnable() { |
638 |
|
|
public void realRun() throws InterruptedException { |
639 |
|
|
threadsStarted.await(); |
640 |
|
|
q.put(one); |
641 |
|
|
}}); |
642 |
|
|
} |
643 |
dl |
1.1 |
} |
644 |
|
|
|
645 |
|
|
/** |
646 |
jsr166 |
1.79 |
* A deserialized/reserialized queue has same elements in same order |
647 |
dl |
1.1 |
*/ |
648 |
jsr166 |
1.5 |
public void testSerialization() throws Exception { |
649 |
jsr166 |
1.48 |
Queue x = populatedQueue(SIZE); |
650 |
|
|
Queue y = serialClone(x); |
651 |
dl |
1.1 |
|
652 |
jsr166 |
1.52 |
assertNotSame(y, x); |
653 |
jsr166 |
1.48 |
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 |
dl |
1.1 |
} |
660 |
jsr166 |
1.48 |
assertTrue(y.isEmpty()); |
661 |
dl |
1.1 |
} |
662 |
|
|
|
663 |
|
|
/** |
664 |
|
|
* drainTo(c) empties queue into another collection c |
665 |
|
|
*/ |
666 |
|
|
public void testDrainTo() { |
667 |
|
|
LinkedTransferQueue q = populatedQueue(SIZE); |
668 |
|
|
ArrayList l = new ArrayList(); |
669 |
|
|
q.drainTo(l); |
670 |
jsr166 |
1.50 |
assertEquals(0, q.size()); |
671 |
|
|
assertEquals(SIZE, l.size()); |
672 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
673 |
jsr166 |
1.51 |
assertEquals(i, l.get(i)); |
674 |
dl |
1.1 |
} |
675 |
|
|
q.add(zero); |
676 |
|
|
q.add(one); |
677 |
|
|
assertFalse(q.isEmpty()); |
678 |
|
|
assertTrue(q.contains(zero)); |
679 |
|
|
assertTrue(q.contains(one)); |
680 |
|
|
l.clear(); |
681 |
|
|
q.drainTo(l); |
682 |
jsr166 |
1.50 |
assertEquals(0, q.size()); |
683 |
|
|
assertEquals(2, l.size()); |
684 |
dl |
1.1 |
for (int i = 0; i < 2; ++i) { |
685 |
jsr166 |
1.51 |
assertEquals(i, l.get(i)); |
686 |
dl |
1.1 |
} |
687 |
|
|
} |
688 |
|
|
|
689 |
|
|
/** |
690 |
jsr166 |
1.13 |
* drainTo(c) empties full queue, unblocking a waiting put. |
691 |
dl |
1.1 |
*/ |
692 |
jsr166 |
1.5 |
public void testDrainToWithActivePut() throws InterruptedException { |
693 |
dl |
1.1 |
final LinkedTransferQueue q = populatedQueue(SIZE); |
694 |
jsr166 |
1.9 |
Thread t = newStartedThread(new CheckedRunnable() { |
695 |
jsr166 |
1.21 |
public void realRun() { |
696 |
jsr166 |
1.7 |
q.put(SIZE + 1); |
697 |
jsr166 |
1.5 |
}}); |
698 |
|
|
ArrayList l = new ArrayList(); |
699 |
|
|
q.drainTo(l); |
700 |
|
|
assertTrue(l.size() >= SIZE); |
701 |
jsr166 |
1.51 |
for (int i = 0; i < SIZE; ++i) |
702 |
|
|
assertEquals(i, l.get(i)); |
703 |
jsr166 |
1.66 |
awaitTermination(t); |
704 |
jsr166 |
1.5 |
assertTrue(q.size() + l.size() >= SIZE); |
705 |
dl |
1.1 |
} |
706 |
|
|
|
707 |
|
|
/** |
708 |
jsr166 |
1.26 |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
709 |
dl |
1.1 |
*/ |
710 |
|
|
public void testDrainToN() { |
711 |
|
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
712 |
|
|
for (int i = 0; i < SIZE + 2; ++i) { |
713 |
|
|
for (int j = 0; j < SIZE; j++) { |
714 |
jsr166 |
1.5 |
assertTrue(q.offer(j)); |
715 |
dl |
1.1 |
} |
716 |
|
|
ArrayList l = new ArrayList(); |
717 |
|
|
q.drainTo(l, i); |
718 |
|
|
int k = (i < SIZE) ? i : SIZE; |
719 |
jsr166 |
1.51 |
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 |
jsr166 |
1.56 |
do {} while (q.poll() != null); |
724 |
dl |
1.1 |
} |
725 |
|
|
} |
726 |
|
|
|
727 |
jsr166 |
1.3 |
/** |
728 |
jsr166 |
1.13 |
* timed poll() or take() increments the waiting consumer count; |
729 |
|
|
* offer(e) decrements the waiting consumer count |
730 |
dl |
1.1 |
*/ |
731 |
jsr166 |
1.5 |
public void testWaitingConsumer() throws InterruptedException { |
732 |
|
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
733 |
jsr166 |
1.50 |
assertEquals(0, q.getWaitingConsumerCount()); |
734 |
jsr166 |
1.13 |
assertFalse(q.hasWaitingConsumer()); |
735 |
jsr166 |
1.31 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
736 |
jsr166 |
1.9 |
|
737 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
738 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
739 |
jsr166 |
1.31 |
threadStarted.countDown(); |
740 |
jsr166 |
1.66 |
long startTime = System.nanoTime(); |
741 |
jsr166 |
1.31 |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
742 |
jsr166 |
1.50 |
assertEquals(0, q.getWaitingConsumerCount()); |
743 |
jsr166 |
1.22 |
assertFalse(q.hasWaitingConsumer()); |
744 |
jsr166 |
1.66 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
745 |
jsr166 |
1.9 |
}}); |
746 |
|
|
|
747 |
jsr166 |
1.31 |
threadStarted.await(); |
748 |
jsr166 |
1.70 |
Callable<Boolean> oneConsumer |
749 |
|
|
= new Callable<Boolean>() { public Boolean call() { |
750 |
|
|
return q.hasWaitingConsumer() |
751 |
|
|
&& q.getWaitingConsumerCount() == 1; }}; |
752 |
|
|
waitForThreadToEnterWaitState(t, oneConsumer); |
753 |
jsr166 |
1.31 |
|
754 |
|
|
assertTrue(q.offer(one)); |
755 |
jsr166 |
1.50 |
assertEquals(0, q.getWaitingConsumerCount()); |
756 |
jsr166 |
1.13 |
assertFalse(q.hasWaitingConsumer()); |
757 |
jsr166 |
1.31 |
|
758 |
jsr166 |
1.66 |
awaitTermination(t); |
759 |
dl |
1.1 |
} |
760 |
jsr166 |
1.3 |
|
761 |
|
|
/** |
762 |
jsr166 |
1.5 |
* transfer(null) throws NullPointerException |
763 |
dl |
1.1 |
*/ |
764 |
jsr166 |
1.5 |
public void testTransfer1() throws InterruptedException { |
765 |
dl |
1.1 |
try { |
766 |
|
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
767 |
|
|
q.transfer(null); |
768 |
|
|
shouldThrow(); |
769 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
770 |
dl |
1.1 |
} |
771 |
|
|
|
772 |
jsr166 |
1.3 |
/** |
773 |
jsr166 |
1.80 |
* transfer waits until a poll occurs. The transferred element |
774 |
jsr166 |
1.69 |
* is returned by the associated poll. |
775 |
dl |
1.1 |
*/ |
776 |
jsr166 |
1.5 |
public void testTransfer2() throws InterruptedException { |
777 |
jsr166 |
1.68 |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
778 |
jsr166 |
1.31 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
779 |
jsr166 |
1.5 |
|
780 |
jsr166 |
1.9 |
Thread t = newStartedThread(new CheckedRunnable() { |
781 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
782 |
jsr166 |
1.31 |
threadStarted.countDown(); |
783 |
jsr166 |
1.34 |
q.transfer(five); |
784 |
jsr166 |
1.31 |
checkEmpty(q); |
785 |
jsr166 |
1.9 |
}}); |
786 |
dl |
1.1 |
|
787 |
jsr166 |
1.31 |
threadStarted.await(); |
788 |
jsr166 |
1.70 |
Callable<Boolean> oneElement |
789 |
|
|
= new Callable<Boolean>() { public Boolean call() { |
790 |
|
|
return !q.isEmpty() && q.size() == 1; }}; |
791 |
|
|
waitForThreadToEnterWaitState(t, oneElement); |
792 |
|
|
|
793 |
jsr166 |
1.34 |
assertSame(five, q.poll()); |
794 |
jsr166 |
1.31 |
checkEmpty(q); |
795 |
jsr166 |
1.66 |
awaitTermination(t); |
796 |
dl |
1.1 |
} |
797 |
jsr166 |
1.3 |
|
798 |
|
|
/** |
799 |
dl |
1.6 |
* transfer waits until a poll occurs, and then transfers in fifo order |
800 |
dl |
1.1 |
*/ |
801 |
jsr166 |
1.5 |
public void testTransfer3() throws InterruptedException { |
802 |
jsr166 |
1.68 |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
803 |
jsr166 |
1.5 |
|
804 |
jsr166 |
1.9 |
Thread first = newStartedThread(new CheckedRunnable() { |
805 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
806 |
jsr166 |
1.32 |
q.transfer(four); |
807 |
jsr166 |
1.71 |
assertFalse(q.contains(four)); |
808 |
jsr166 |
1.24 |
assertEquals(1, q.size()); |
809 |
jsr166 |
1.7 |
}}); |
810 |
jsr166 |
1.5 |
|
811 |
jsr166 |
1.9 |
Thread interruptedThread = newStartedThread( |
812 |
|
|
new CheckedInterruptedRunnable() { |
813 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
814 |
jsr166 |
1.32 |
while (q.isEmpty()) |
815 |
jsr166 |
1.9 |
Thread.yield(); |
816 |
jsr166 |
1.32 |
q.transfer(five); |
817 |
jsr166 |
1.9 |
}}); |
818 |
jsr166 |
1.5 |
|
819 |
|
|
while (q.size() < 2) |
820 |
|
|
Thread.yield(); |
821 |
|
|
assertEquals(2, q.size()); |
822 |
jsr166 |
1.32 |
assertSame(four, q.poll()); |
823 |
jsr166 |
1.5 |
first.join(); |
824 |
|
|
assertEquals(1, q.size()); |
825 |
|
|
interruptedThread.interrupt(); |
826 |
|
|
interruptedThread.join(); |
827 |
jsr166 |
1.31 |
checkEmpty(q); |
828 |
dl |
1.1 |
} |
829 |
|
|
|
830 |
|
|
/** |
831 |
dl |
1.6 |
* transfer waits until a poll occurs, at which point the polling |
832 |
|
|
* thread returns the element |
833 |
dl |
1.1 |
*/ |
834 |
jsr166 |
1.5 |
public void testTransfer4() throws InterruptedException { |
835 |
dl |
1.1 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
836 |
jsr166 |
1.9 |
|
837 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
838 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
839 |
jsr166 |
1.7 |
q.transfer(four); |
840 |
jsr166 |
1.24 |
assertFalse(q.contains(four)); |
841 |
|
|
assertSame(three, q.poll()); |
842 |
jsr166 |
1.9 |
}}); |
843 |
|
|
|
844 |
jsr166 |
1.31 |
while (q.isEmpty()) |
845 |
|
|
Thread.yield(); |
846 |
|
|
assertFalse(q.isEmpty()); |
847 |
|
|
assertEquals(1, q.size()); |
848 |
jsr166 |
1.5 |
assertTrue(q.offer(three)); |
849 |
jsr166 |
1.24 |
assertSame(four, q.poll()); |
850 |
jsr166 |
1.66 |
awaitTermination(t); |
851 |
dl |
1.1 |
} |
852 |
jsr166 |
1.3 |
|
853 |
|
|
/** |
854 |
jsr166 |
1.80 |
* transfer waits until a take occurs. The transferred element |
855 |
jsr166 |
1.69 |
* is returned by the associated take. |
856 |
dl |
1.6 |
*/ |
857 |
|
|
public void testTransfer5() throws InterruptedException { |
858 |
jsr166 |
1.68 |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
859 |
dl |
1.6 |
|
860 |
jsr166 |
1.9 |
Thread t = newStartedThread(new CheckedRunnable() { |
861 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
862 |
jsr166 |
1.31 |
q.transfer(four); |
863 |
jsr166 |
1.12 |
checkEmpty(q); |
864 |
jsr166 |
1.9 |
}}); |
865 |
dl |
1.6 |
|
866 |
jsr166 |
1.31 |
while (q.isEmpty()) |
867 |
|
|
Thread.yield(); |
868 |
|
|
assertFalse(q.isEmpty()); |
869 |
|
|
assertEquals(1, q.size()); |
870 |
|
|
assertSame(four, q.take()); |
871 |
jsr166 |
1.12 |
checkEmpty(q); |
872 |
jsr166 |
1.66 |
awaitTermination(t); |
873 |
dl |
1.6 |
} |
874 |
|
|
|
875 |
|
|
/** |
876 |
jsr166 |
1.5 |
* tryTransfer(null) throws NullPointerException |
877 |
dl |
1.1 |
*/ |
878 |
|
|
public void testTryTransfer1() { |
879 |
jsr166 |
1.60 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
880 |
dl |
1.1 |
try { |
881 |
|
|
q.tryTransfer(null); |
882 |
jsr166 |
1.8 |
shouldThrow(); |
883 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
884 |
dl |
1.1 |
} |
885 |
jsr166 |
1.3 |
|
886 |
|
|
/** |
887 |
jsr166 |
1.5 |
* tryTransfer returns false and does not enqueue if there are no |
888 |
|
|
* consumers waiting to poll or take. |
889 |
dl |
1.1 |
*/ |
890 |
jsr166 |
1.12 |
public void testTryTransfer2() throws InterruptedException { |
891 |
jsr166 |
1.5 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
892 |
|
|
assertFalse(q.tryTransfer(new Object())); |
893 |
|
|
assertFalse(q.hasWaitingConsumer()); |
894 |
jsr166 |
1.12 |
checkEmpty(q); |
895 |
dl |
1.1 |
} |
896 |
jsr166 |
1.3 |
|
897 |
|
|
/** |
898 |
jsr166 |
1.5 |
* If there is a consumer waiting in timed poll, tryTransfer |
899 |
|
|
* returns true while successfully transfering object. |
900 |
dl |
1.1 |
*/ |
901 |
jsr166 |
1.5 |
public void testTryTransfer3() throws InterruptedException { |
902 |
|
|
final Object hotPotato = new Object(); |
903 |
|
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
904 |
jsr166 |
1.9 |
|
905 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
906 |
jsr166 |
1.21 |
public void realRun() { |
907 |
jsr166 |
1.7 |
while (! q.hasWaitingConsumer()) |
908 |
|
|
Thread.yield(); |
909 |
jsr166 |
1.24 |
assertTrue(q.hasWaitingConsumer()); |
910 |
jsr166 |
1.31 |
checkEmpty(q); |
911 |
jsr166 |
1.24 |
assertTrue(q.tryTransfer(hotPotato)); |
912 |
jsr166 |
1.9 |
}}); |
913 |
|
|
|
914 |
jsr166 |
1.66 |
long startTime = System.nanoTime(); |
915 |
|
|
assertSame(hotPotato, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
916 |
|
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
917 |
jsr166 |
1.12 |
checkEmpty(q); |
918 |
jsr166 |
1.66 |
awaitTermination(t); |
919 |
jsr166 |
1.5 |
} |
920 |
dl |
1.1 |
|
921 |
jsr166 |
1.5 |
/** |
922 |
|
|
* If there is a consumer waiting in take, tryTransfer returns |
923 |
|
|
* true while successfully transfering object. |
924 |
|
|
*/ |
925 |
|
|
public void testTryTransfer4() throws InterruptedException { |
926 |
|
|
final Object hotPotato = new Object(); |
927 |
|
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
928 |
jsr166 |
1.9 |
|
929 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
930 |
jsr166 |
1.21 |
public void realRun() { |
931 |
jsr166 |
1.7 |
while (! q.hasWaitingConsumer()) |
932 |
|
|
Thread.yield(); |
933 |
jsr166 |
1.24 |
assertTrue(q.hasWaitingConsumer()); |
934 |
jsr166 |
1.31 |
checkEmpty(q); |
935 |
jsr166 |
1.24 |
assertTrue(q.tryTransfer(hotPotato)); |
936 |
jsr166 |
1.9 |
}}); |
937 |
|
|
|
938 |
jsr166 |
1.23 |
assertSame(q.take(), hotPotato); |
939 |
jsr166 |
1.12 |
checkEmpty(q); |
940 |
jsr166 |
1.66 |
awaitTermination(t); |
941 |
dl |
1.1 |
} |
942 |
|
|
|
943 |
jsr166 |
1.3 |
/** |
944 |
jsr166 |
1.46 |
* tryTransfer blocks interruptibly if no takers |
945 |
dl |
1.1 |
*/ |
946 |
jsr166 |
1.5 |
public void testTryTransfer5() throws InterruptedException { |
947 |
dl |
1.1 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
948 |
jsr166 |
1.46 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
949 |
jsr166 |
1.40 |
assertTrue(q.isEmpty()); |
950 |
jsr166 |
1.9 |
|
951 |
jsr166 |
1.31 |
Thread t = newStartedThread(new CheckedRunnable() { |
952 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
953 |
jsr166 |
1.46 |
Thread.currentThread().interrupt(); |
954 |
|
|
try { |
955 |
jsr166 |
1.82 |
q.tryTransfer(new Object(), randomTimeout(), randomTimeUnit()); |
956 |
jsr166 |
1.46 |
shouldThrow(); |
957 |
|
|
} catch (InterruptedException success) {} |
958 |
|
|
assertFalse(Thread.interrupted()); |
959 |
|
|
|
960 |
|
|
pleaseInterrupt.countDown(); |
961 |
jsr166 |
1.31 |
try { |
962 |
jsr166 |
1.85 |
q.tryTransfer(new Object(), LONGER_DELAY_MS, MILLISECONDS); |
963 |
jsr166 |
1.31 |
shouldThrow(); |
964 |
|
|
} catch (InterruptedException success) {} |
965 |
jsr166 |
1.46 |
assertFalse(Thread.interrupted()); |
966 |
jsr166 |
1.5 |
}}); |
967 |
jsr166 |
1.9 |
|
968 |
jsr166 |
1.46 |
await(pleaseInterrupt); |
969 |
jsr166 |
1.82 |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
970 |
jsr166 |
1.31 |
t.interrupt(); |
971 |
jsr166 |
1.46 |
awaitTermination(t); |
972 |
jsr166 |
1.31 |
checkEmpty(q); |
973 |
dl |
1.1 |
} |
974 |
|
|
|
975 |
jsr166 |
1.3 |
/** |
976 |
jsr166 |
1.33 |
* tryTransfer gives up after the timeout and returns false |
977 |
dl |
1.1 |
*/ |
978 |
jsr166 |
1.5 |
public void testTryTransfer6() throws InterruptedException { |
979 |
dl |
1.1 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
980 |
jsr166 |
1.9 |
|
981 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
982 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
983 |
jsr166 |
1.65 |
long startTime = System.nanoTime(); |
984 |
jsr166 |
1.23 |
assertFalse(q.tryTransfer(new Object(), |
985 |
jsr166 |
1.46 |
timeoutMillis(), MILLISECONDS)); |
986 |
jsr166 |
1.65 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
987 |
jsr166 |
1.32 |
checkEmpty(q); |
988 |
jsr166 |
1.9 |
}}); |
989 |
|
|
|
990 |
jsr166 |
1.46 |
awaitTermination(t); |
991 |
jsr166 |
1.27 |
checkEmpty(q); |
992 |
dl |
1.1 |
} |
993 |
|
|
|
994 |
jsr166 |
1.3 |
/** |
995 |
dl |
1.1 |
* tryTransfer waits for any elements previously in to be removed |
996 |
|
|
* before transfering to a poll or take |
997 |
|
|
*/ |
998 |
jsr166 |
1.5 |
public void testTryTransfer7() throws InterruptedException { |
999 |
dl |
1.1 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1000 |
jsr166 |
1.5 |
assertTrue(q.offer(four)); |
1001 |
jsr166 |
1.9 |
|
1002 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
1003 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
1004 |
jsr166 |
1.66 |
long startTime = System.nanoTime(); |
1005 |
|
|
assertTrue(q.tryTransfer(five, LONG_DELAY_MS, MILLISECONDS)); |
1006 |
|
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1007 |
jsr166 |
1.31 |
checkEmpty(q); |
1008 |
jsr166 |
1.9 |
}}); |
1009 |
|
|
|
1010 |
jsr166 |
1.32 |
while (q.size() != 2) |
1011 |
|
|
Thread.yield(); |
1012 |
jsr166 |
1.5 |
assertEquals(2, q.size()); |
1013 |
jsr166 |
1.24 |
assertSame(four, q.poll()); |
1014 |
|
|
assertSame(five, q.poll()); |
1015 |
jsr166 |
1.12 |
checkEmpty(q); |
1016 |
jsr166 |
1.66 |
awaitTermination(t); |
1017 |
dl |
1.1 |
} |
1018 |
|
|
|
1019 |
jsr166 |
1.3 |
/** |
1020 |
jsr166 |
1.41 |
* tryTransfer attempts to enqueue into the queue and fails |
1021 |
|
|
* returning false not enqueueing and the successive poll is null |
1022 |
dl |
1.1 |
*/ |
1023 |
jsr166 |
1.5 |
public void testTryTransfer8() throws InterruptedException { |
1024 |
dl |
1.1 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1025 |
jsr166 |
1.5 |
assertTrue(q.offer(four)); |
1026 |
|
|
assertEquals(1, q.size()); |
1027 |
jsr166 |
1.65 |
long startTime = System.nanoTime(); |
1028 |
jsr166 |
1.46 |
assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS)); |
1029 |
jsr166 |
1.65 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1030 |
jsr166 |
1.5 |
assertEquals(1, q.size()); |
1031 |
jsr166 |
1.24 |
assertSame(four, q.poll()); |
1032 |
jsr166 |
1.13 |
assertNull(q.poll()); |
1033 |
jsr166 |
1.12 |
checkEmpty(q); |
1034 |
dl |
1.1 |
} |
1035 |
|
|
|
1036 |
jsr166 |
1.5 |
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1037 |
jsr166 |
1.68 |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
1038 |
jsr166 |
1.31 |
checkEmpty(q); |
1039 |
jsr166 |
1.5 |
for (int i = 0; i < n; i++) { |
1040 |
|
|
assertEquals(i, q.size()); |
1041 |
dl |
1.1 |
assertTrue(q.offer(i)); |
1042 |
jsr166 |
1.5 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
1043 |
dl |
1.1 |
} |
1044 |
|
|
assertFalse(q.isEmpty()); |
1045 |
|
|
return q; |
1046 |
|
|
} |
1047 |
jsr166 |
1.53 |
|
1048 |
|
|
/** |
1049 |
|
|
* remove(null), contains(null) always return false |
1050 |
|
|
*/ |
1051 |
|
|
public void testNeverContainsNull() { |
1052 |
|
|
Collection<?>[] qs = { |
1053 |
|
|
new LinkedTransferQueue<Object>(), |
1054 |
|
|
populatedQueue(2), |
1055 |
|
|
}; |
1056 |
|
|
|
1057 |
|
|
for (Collection<?> q : qs) { |
1058 |
|
|
assertFalse(q.contains(null)); |
1059 |
|
|
assertFalse(q.remove(null)); |
1060 |
|
|
} |
1061 |
|
|
} |
1062 |
dl |
1.1 |
} |