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.73 |
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.64 |
long startTime = System.nanoTime(); |
285 |
jsr166 |
1.78 |
for (int i = 0; i < SIZE; i++) |
286 |
jsr166 |
1.23 |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
287 |
jsr166 |
1.76 |
|
288 |
jsr166 |
1.78 |
Thread.currentThread().interrupt(); |
289 |
|
|
try { |
290 |
|
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
291 |
|
|
shouldThrow(); |
292 |
|
|
} catch (InterruptedException success) {} |
293 |
|
|
assertFalse(Thread.interrupted()); |
294 |
|
|
|
295 |
jsr166 |
1.76 |
pleaseInterrupt.countDown(); |
296 |
jsr166 |
1.16 |
try { |
297 |
jsr166 |
1.64 |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
298 |
jsr166 |
1.17 |
shouldThrow(); |
299 |
jsr166 |
1.66 |
} catch (InterruptedException success) {} |
300 |
jsr166 |
1.76 |
assertFalse(Thread.interrupted()); |
301 |
|
|
|
302 |
jsr166 |
1.66 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
303 |
jsr166 |
1.5 |
}}); |
304 |
jsr166 |
1.17 |
|
305 |
jsr166 |
1.76 |
await(pleaseInterrupt); |
306 |
jsr166 |
1.74 |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
307 |
jsr166 |
1.5 |
t.interrupt(); |
308 |
jsr166 |
1.64 |
awaitTermination(t); |
309 |
jsr166 |
1.12 |
checkEmpty(q); |
310 |
dl |
1.1 |
} |
311 |
|
|
|
312 |
|
|
/** |
313 |
jsr166 |
1.31 |
* timed poll after thread interrupted throws InterruptedException |
314 |
|
|
* instead of returning timeout status |
315 |
|
|
*/ |
316 |
|
|
public void testTimedPollAfterInterrupt() throws InterruptedException { |
317 |
|
|
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
318 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
319 |
|
|
public void realRun() throws InterruptedException { |
320 |
jsr166 |
1.66 |
long startTime = System.nanoTime(); |
321 |
jsr166 |
1.31 |
Thread.currentThread().interrupt(); |
322 |
jsr166 |
1.66 |
for (int i = 0; i < SIZE; ++i) |
323 |
jsr166 |
1.31 |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
324 |
|
|
try { |
325 |
jsr166 |
1.66 |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
326 |
jsr166 |
1.31 |
shouldThrow(); |
327 |
|
|
} catch (InterruptedException success) {} |
328 |
jsr166 |
1.77 |
assertFalse(Thread.interrupted()); |
329 |
jsr166 |
1.66 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
330 |
jsr166 |
1.31 |
}}); |
331 |
|
|
|
332 |
jsr166 |
1.66 |
awaitTermination(t); |
333 |
jsr166 |
1.31 |
checkEmpty(q); |
334 |
|
|
} |
335 |
|
|
|
336 |
|
|
/** |
337 |
dl |
1.1 |
* peek returns next element, or null if empty |
338 |
|
|
*/ |
339 |
jsr166 |
1.12 |
public void testPeek() throws InterruptedException { |
340 |
jsr166 |
1.5 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
341 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
342 |
jsr166 |
1.5 |
assertEquals(i, (int) q.peek()); |
343 |
|
|
assertEquals(i, (int) q.poll()); |
344 |
dl |
1.1 |
assertTrue(q.peek() == null || |
345 |
jsr166 |
1.5 |
i != (int) q.peek()); |
346 |
dl |
1.1 |
} |
347 |
|
|
assertNull(q.peek()); |
348 |
jsr166 |
1.12 |
checkEmpty(q); |
349 |
dl |
1.1 |
} |
350 |
|
|
|
351 |
|
|
/** |
352 |
jsr166 |
1.5 |
* element returns next element, or throws NoSuchElementException if empty |
353 |
dl |
1.1 |
*/ |
354 |
jsr166 |
1.12 |
public void testElement() throws InterruptedException { |
355 |
jsr166 |
1.5 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
356 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
357 |
jsr166 |
1.5 |
assertEquals(i, (int) q.element()); |
358 |
|
|
assertEquals(i, (int) q.poll()); |
359 |
dl |
1.1 |
} |
360 |
|
|
try { |
361 |
|
|
q.element(); |
362 |
|
|
shouldThrow(); |
363 |
jsr166 |
1.17 |
} catch (NoSuchElementException success) {} |
364 |
jsr166 |
1.12 |
checkEmpty(q); |
365 |
dl |
1.1 |
} |
366 |
|
|
|
367 |
|
|
/** |
368 |
jsr166 |
1.5 |
* remove removes next element, or throws NoSuchElementException if empty |
369 |
dl |
1.1 |
*/ |
370 |
jsr166 |
1.12 |
public void testRemove() throws InterruptedException { |
371 |
jsr166 |
1.5 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
372 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
373 |
jsr166 |
1.5 |
assertEquals(i, (int) q.remove()); |
374 |
dl |
1.1 |
} |
375 |
|
|
try { |
376 |
|
|
q.remove(); |
377 |
|
|
shouldThrow(); |
378 |
jsr166 |
1.17 |
} catch (NoSuchElementException success) {} |
379 |
jsr166 |
1.12 |
checkEmpty(q); |
380 |
dl |
1.1 |
} |
381 |
|
|
|
382 |
|
|
/** |
383 |
|
|
* An add following remove(x) succeeds |
384 |
|
|
*/ |
385 |
jsr166 |
1.5 |
public void testRemoveElementAndAdd() throws InterruptedException { |
386 |
|
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
387 |
|
|
assertTrue(q.add(one)); |
388 |
|
|
assertTrue(q.add(two)); |
389 |
|
|
assertTrue(q.remove(one)); |
390 |
|
|
assertTrue(q.remove(two)); |
391 |
|
|
assertTrue(q.add(three)); |
392 |
jsr166 |
1.23 |
assertSame(q.take(), three); |
393 |
dl |
1.1 |
} |
394 |
|
|
|
395 |
|
|
/** |
396 |
|
|
* contains(x) reports true when elements added but not yet removed |
397 |
|
|
*/ |
398 |
|
|
public void testContains() { |
399 |
jsr166 |
1.5 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
400 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
401 |
jsr166 |
1.5 |
assertTrue(q.contains(i)); |
402 |
|
|
assertEquals(i, (int) q.poll()); |
403 |
|
|
assertFalse(q.contains(i)); |
404 |
dl |
1.1 |
} |
405 |
|
|
} |
406 |
|
|
|
407 |
|
|
/** |
408 |
|
|
* clear removes all elements |
409 |
|
|
*/ |
410 |
jsr166 |
1.12 |
public void testClear() throws InterruptedException { |
411 |
dl |
1.1 |
LinkedTransferQueue q = populatedQueue(SIZE); |
412 |
|
|
q.clear(); |
413 |
jsr166 |
1.12 |
checkEmpty(q); |
414 |
jsr166 |
1.5 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
415 |
dl |
1.1 |
q.add(one); |
416 |
|
|
assertFalse(q.isEmpty()); |
417 |
jsr166 |
1.12 |
assertEquals(1, q.size()); |
418 |
dl |
1.1 |
assertTrue(q.contains(one)); |
419 |
|
|
q.clear(); |
420 |
jsr166 |
1.12 |
checkEmpty(q); |
421 |
dl |
1.1 |
} |
422 |
|
|
|
423 |
|
|
/** |
424 |
|
|
* containsAll(c) is true when c contains a subset of elements |
425 |
|
|
*/ |
426 |
|
|
public void testContainsAll() { |
427 |
jsr166 |
1.5 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
428 |
jsr166 |
1.68 |
LinkedTransferQueue<Integer> p = new LinkedTransferQueue<>(); |
429 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
430 |
|
|
assertTrue(q.containsAll(p)); |
431 |
|
|
assertFalse(p.containsAll(q)); |
432 |
jsr166 |
1.5 |
p.add(i); |
433 |
dl |
1.1 |
} |
434 |
|
|
assertTrue(p.containsAll(q)); |
435 |
|
|
} |
436 |
|
|
|
437 |
|
|
/** |
438 |
jsr166 |
1.7 |
* retainAll(c) retains only those elements of c and reports true |
439 |
|
|
* if changed |
440 |
dl |
1.1 |
*/ |
441 |
|
|
public void testRetainAll() { |
442 |
|
|
LinkedTransferQueue q = populatedQueue(SIZE); |
443 |
|
|
LinkedTransferQueue p = populatedQueue(SIZE); |
444 |
|
|
for (int i = 0; i < SIZE; ++i) { |
445 |
|
|
boolean changed = q.retainAll(p); |
446 |
|
|
if (i == 0) { |
447 |
|
|
assertFalse(changed); |
448 |
|
|
} else { |
449 |
|
|
assertTrue(changed); |
450 |
|
|
} |
451 |
|
|
assertTrue(q.containsAll(p)); |
452 |
|
|
assertEquals(SIZE - i, q.size()); |
453 |
|
|
p.remove(); |
454 |
|
|
} |
455 |
|
|
} |
456 |
|
|
|
457 |
|
|
/** |
458 |
jsr166 |
1.7 |
* removeAll(c) removes only those elements of c and reports true |
459 |
|
|
* if changed |
460 |
dl |
1.1 |
*/ |
461 |
|
|
public void testRemoveAll() { |
462 |
|
|
for (int i = 1; i < SIZE; ++i) { |
463 |
|
|
LinkedTransferQueue q = populatedQueue(SIZE); |
464 |
|
|
LinkedTransferQueue p = populatedQueue(i); |
465 |
|
|
assertTrue(q.removeAll(p)); |
466 |
|
|
assertEquals(SIZE - i, q.size()); |
467 |
|
|
for (int j = 0; j < i; ++j) { |
468 |
jsr166 |
1.5 |
assertFalse(q.contains(p.remove())); |
469 |
dl |
1.1 |
} |
470 |
|
|
} |
471 |
|
|
} |
472 |
|
|
|
473 |
|
|
/** |
474 |
jsr166 |
1.38 |
* toArray() contains all elements in FIFO order |
475 |
dl |
1.1 |
*/ |
476 |
jsr166 |
1.38 |
public void testToArray() { |
477 |
dl |
1.1 |
LinkedTransferQueue q = populatedQueue(SIZE); |
478 |
jsr166 |
1.81 |
Object[] a = q.toArray(); |
479 |
|
|
assertSame(Object[].class, a.getClass()); |
480 |
|
|
for (Object o : a) |
481 |
|
|
assertSame(o, q.poll()); |
482 |
|
|
assertTrue(q.isEmpty()); |
483 |
dl |
1.1 |
} |
484 |
|
|
|
485 |
|
|
/** |
486 |
jsr166 |
1.38 |
* toArray(a) contains all elements in FIFO order |
487 |
dl |
1.1 |
*/ |
488 |
jsr166 |
1.38 |
public void testToArray2() { |
489 |
jsr166 |
1.5 |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
490 |
dl |
1.1 |
Integer[] ints = new Integer[SIZE]; |
491 |
jsr166 |
1.39 |
Integer[] array = q.toArray(ints); |
492 |
|
|
assertSame(ints, array); |
493 |
jsr166 |
1.81 |
for (Integer o : ints) |
494 |
|
|
assertSame(o, q.poll()); |
495 |
|
|
assertTrue(q.isEmpty()); |
496 |
dl |
1.1 |
} |
497 |
|
|
|
498 |
|
|
/** |
499 |
jsr166 |
1.36 |
* toArray(incompatible array type) throws ArrayStoreException |
500 |
dl |
1.1 |
*/ |
501 |
|
|
public void testToArray1_BadArg() { |
502 |
jsr166 |
1.20 |
LinkedTransferQueue q = populatedQueue(SIZE); |
503 |
dl |
1.1 |
try { |
504 |
jsr166 |
1.36 |
q.toArray(new String[10]); |
505 |
dl |
1.1 |
shouldThrow(); |
506 |
jsr166 |
1.17 |
} catch (ArrayStoreException success) {} |
507 |
dl |
1.1 |
} |
508 |
|
|
|
509 |
|
|
/** |
510 |
|
|
* iterator iterates through all elements |
511 |
|
|
*/ |
512 |
jsr166 |
1.5 |
public void testIterator() throws InterruptedException { |
513 |
dl |
1.1 |
LinkedTransferQueue q = populatedQueue(SIZE); |
514 |
|
|
Iterator it = q.iterator(); |
515 |
jsr166 |
1.57 |
int i; |
516 |
|
|
for (i = 0; it.hasNext(); i++) |
517 |
|
|
assertTrue(q.contains(it.next())); |
518 |
|
|
assertEquals(i, SIZE); |
519 |
|
|
assertIteratorExhausted(it); |
520 |
|
|
|
521 |
|
|
it = q.iterator(); |
522 |
|
|
for (i = 0; it.hasNext(); i++) |
523 |
|
|
assertEquals(it.next(), q.take()); |
524 |
jsr166 |
1.10 |
assertEquals(i, SIZE); |
525 |
jsr166 |
1.57 |
assertIteratorExhausted(it); |
526 |
|
|
} |
527 |
|
|
|
528 |
|
|
/** |
529 |
|
|
* iterator of empty collection has no elements |
530 |
|
|
*/ |
531 |
|
|
public void testEmptyIterator() { |
532 |
|
|
assertIteratorExhausted(new LinkedTransferQueue().iterator()); |
533 |
dl |
1.1 |
} |
534 |
|
|
|
535 |
|
|
/** |
536 |
jsr166 |
1.12 |
* iterator.remove() removes current element |
537 |
dl |
1.1 |
*/ |
538 |
|
|
public void testIteratorRemove() { |
539 |
|
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
540 |
|
|
q.add(two); |
541 |
|
|
q.add(one); |
542 |
|
|
q.add(three); |
543 |
|
|
|
544 |
|
|
Iterator it = q.iterator(); |
545 |
|
|
it.next(); |
546 |
|
|
it.remove(); |
547 |
|
|
|
548 |
|
|
it = q.iterator(); |
549 |
jsr166 |
1.24 |
assertSame(it.next(), one); |
550 |
|
|
assertSame(it.next(), three); |
551 |
dl |
1.1 |
assertFalse(it.hasNext()); |
552 |
|
|
} |
553 |
|
|
|
554 |
|
|
/** |
555 |
|
|
* iterator ordering is FIFO |
556 |
|
|
*/ |
557 |
|
|
public void testIteratorOrdering() { |
558 |
jsr166 |
1.68 |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
559 |
jsr166 |
1.5 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
560 |
dl |
1.1 |
q.add(one); |
561 |
|
|
q.add(two); |
562 |
|
|
q.add(three); |
563 |
jsr166 |
1.5 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
564 |
dl |
1.1 |
int k = 0; |
565 |
jsr166 |
1.5 |
for (Integer n : q) { |
566 |
|
|
assertEquals(++k, (int) n); |
567 |
dl |
1.1 |
} |
568 |
|
|
assertEquals(3, k); |
569 |
|
|
} |
570 |
|
|
|
571 |
|
|
/** |
572 |
|
|
* Modifications do not cause iterators to fail |
573 |
|
|
*/ |
574 |
|
|
public void testWeaklyConsistentIteration() { |
575 |
|
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
576 |
|
|
q.add(one); |
577 |
|
|
q.add(two); |
578 |
|
|
q.add(three); |
579 |
jsr166 |
1.5 |
for (Iterator it = q.iterator(); it.hasNext();) { |
580 |
|
|
q.remove(); |
581 |
|
|
it.next(); |
582 |
dl |
1.1 |
} |
583 |
|
|
assertEquals(0, q.size()); |
584 |
|
|
} |
585 |
|
|
|
586 |
|
|
/** |
587 |
|
|
* toString contains toStrings of elements |
588 |
|
|
*/ |
589 |
|
|
public void testToString() { |
590 |
|
|
LinkedTransferQueue q = populatedQueue(SIZE); |
591 |
|
|
String s = q.toString(); |
592 |
|
|
for (int i = 0; i < SIZE; ++i) { |
593 |
jsr166 |
1.46 |
assertTrue(s.contains(String.valueOf(i))); |
594 |
dl |
1.1 |
} |
595 |
|
|
} |
596 |
|
|
|
597 |
|
|
/** |
598 |
|
|
* offer transfers elements across Executor tasks |
599 |
|
|
*/ |
600 |
|
|
public void testOfferInExecutor() { |
601 |
|
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
602 |
jsr166 |
1.46 |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
603 |
jsr166 |
1.63 |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
604 |
|
|
try (PoolCleaner cleaner = cleaner(executor)) { |
605 |
jsr166 |
1.7 |
|
606 |
jsr166 |
1.63 |
executor.execute(new CheckedRunnable() { |
607 |
|
|
public void realRun() throws InterruptedException { |
608 |
|
|
threadsStarted.await(); |
609 |
jsr166 |
1.66 |
long startTime = System.nanoTime(); |
610 |
jsr166 |
1.63 |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
611 |
jsr166 |
1.66 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
612 |
jsr166 |
1.63 |
}}); |
613 |
dl |
1.1 |
|
614 |
jsr166 |
1.63 |
executor.execute(new CheckedRunnable() { |
615 |
|
|
public void realRun() throws InterruptedException { |
616 |
|
|
threadsStarted.await(); |
617 |
|
|
assertSame(one, q.take()); |
618 |
|
|
checkEmpty(q); |
619 |
|
|
}}); |
620 |
|
|
} |
621 |
dl |
1.1 |
} |
622 |
|
|
|
623 |
|
|
/** |
624 |
jsr166 |
1.13 |
* timed poll retrieves elements across Executor threads |
625 |
dl |
1.1 |
*/ |
626 |
|
|
public void testPollInExecutor() { |
627 |
|
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
628 |
jsr166 |
1.46 |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
629 |
jsr166 |
1.63 |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
630 |
|
|
try (PoolCleaner cleaner = cleaner(executor)) { |
631 |
jsr166 |
1.7 |
|
632 |
jsr166 |
1.63 |
executor.execute(new CheckedRunnable() { |
633 |
|
|
public void realRun() throws InterruptedException { |
634 |
|
|
assertNull(q.poll()); |
635 |
|
|
threadsStarted.await(); |
636 |
jsr166 |
1.66 |
long startTime = System.nanoTime(); |
637 |
jsr166 |
1.63 |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
638 |
jsr166 |
1.66 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
639 |
jsr166 |
1.63 |
checkEmpty(q); |
640 |
|
|
}}); |
641 |
dl |
1.1 |
|
642 |
jsr166 |
1.63 |
executor.execute(new CheckedRunnable() { |
643 |
|
|
public void realRun() throws InterruptedException { |
644 |
|
|
threadsStarted.await(); |
645 |
|
|
q.put(one); |
646 |
|
|
}}); |
647 |
|
|
} |
648 |
dl |
1.1 |
} |
649 |
|
|
|
650 |
|
|
/** |
651 |
jsr166 |
1.79 |
* A deserialized/reserialized queue has same elements in same order |
652 |
dl |
1.1 |
*/ |
653 |
jsr166 |
1.5 |
public void testSerialization() throws Exception { |
654 |
jsr166 |
1.48 |
Queue x = populatedQueue(SIZE); |
655 |
|
|
Queue y = serialClone(x); |
656 |
dl |
1.1 |
|
657 |
jsr166 |
1.52 |
assertNotSame(y, x); |
658 |
jsr166 |
1.48 |
assertEquals(x.size(), y.size()); |
659 |
|
|
assertEquals(x.toString(), y.toString()); |
660 |
|
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
661 |
|
|
while (!x.isEmpty()) { |
662 |
|
|
assertFalse(y.isEmpty()); |
663 |
|
|
assertEquals(x.remove(), y.remove()); |
664 |
dl |
1.1 |
} |
665 |
jsr166 |
1.48 |
assertTrue(y.isEmpty()); |
666 |
dl |
1.1 |
} |
667 |
|
|
|
668 |
|
|
/** |
669 |
|
|
* drainTo(c) empties queue into another collection c |
670 |
|
|
*/ |
671 |
|
|
public void testDrainTo() { |
672 |
|
|
LinkedTransferQueue q = populatedQueue(SIZE); |
673 |
|
|
ArrayList l = new ArrayList(); |
674 |
|
|
q.drainTo(l); |
675 |
jsr166 |
1.50 |
assertEquals(0, q.size()); |
676 |
|
|
assertEquals(SIZE, l.size()); |
677 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
678 |
jsr166 |
1.51 |
assertEquals(i, l.get(i)); |
679 |
dl |
1.1 |
} |
680 |
|
|
q.add(zero); |
681 |
|
|
q.add(one); |
682 |
|
|
assertFalse(q.isEmpty()); |
683 |
|
|
assertTrue(q.contains(zero)); |
684 |
|
|
assertTrue(q.contains(one)); |
685 |
|
|
l.clear(); |
686 |
|
|
q.drainTo(l); |
687 |
jsr166 |
1.50 |
assertEquals(0, q.size()); |
688 |
|
|
assertEquals(2, l.size()); |
689 |
dl |
1.1 |
for (int i = 0; i < 2; ++i) { |
690 |
jsr166 |
1.51 |
assertEquals(i, l.get(i)); |
691 |
dl |
1.1 |
} |
692 |
|
|
} |
693 |
|
|
|
694 |
|
|
/** |
695 |
jsr166 |
1.13 |
* drainTo(c) empties full queue, unblocking a waiting put. |
696 |
dl |
1.1 |
*/ |
697 |
jsr166 |
1.5 |
public void testDrainToWithActivePut() throws InterruptedException { |
698 |
dl |
1.1 |
final LinkedTransferQueue q = populatedQueue(SIZE); |
699 |
jsr166 |
1.9 |
Thread t = newStartedThread(new CheckedRunnable() { |
700 |
jsr166 |
1.21 |
public void realRun() { |
701 |
jsr166 |
1.7 |
q.put(SIZE + 1); |
702 |
jsr166 |
1.5 |
}}); |
703 |
|
|
ArrayList l = new ArrayList(); |
704 |
|
|
q.drainTo(l); |
705 |
|
|
assertTrue(l.size() >= SIZE); |
706 |
jsr166 |
1.51 |
for (int i = 0; i < SIZE; ++i) |
707 |
|
|
assertEquals(i, l.get(i)); |
708 |
jsr166 |
1.66 |
awaitTermination(t); |
709 |
jsr166 |
1.5 |
assertTrue(q.size() + l.size() >= SIZE); |
710 |
dl |
1.1 |
} |
711 |
|
|
|
712 |
|
|
/** |
713 |
jsr166 |
1.26 |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
714 |
dl |
1.1 |
*/ |
715 |
|
|
public void testDrainToN() { |
716 |
|
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
717 |
|
|
for (int i = 0; i < SIZE + 2; ++i) { |
718 |
|
|
for (int j = 0; j < SIZE; j++) { |
719 |
jsr166 |
1.5 |
assertTrue(q.offer(j)); |
720 |
dl |
1.1 |
} |
721 |
|
|
ArrayList l = new ArrayList(); |
722 |
|
|
q.drainTo(l, i); |
723 |
|
|
int k = (i < SIZE) ? i : SIZE; |
724 |
jsr166 |
1.51 |
assertEquals(k, l.size()); |
725 |
|
|
assertEquals(SIZE - k, q.size()); |
726 |
|
|
for (int j = 0; j < k; ++j) |
727 |
|
|
assertEquals(j, l.get(j)); |
728 |
jsr166 |
1.56 |
do {} while (q.poll() != null); |
729 |
dl |
1.1 |
} |
730 |
|
|
} |
731 |
|
|
|
732 |
jsr166 |
1.3 |
/** |
733 |
jsr166 |
1.13 |
* timed poll() or take() increments the waiting consumer count; |
734 |
|
|
* offer(e) decrements the waiting consumer count |
735 |
dl |
1.1 |
*/ |
736 |
jsr166 |
1.5 |
public void testWaitingConsumer() throws InterruptedException { |
737 |
|
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
738 |
jsr166 |
1.50 |
assertEquals(0, q.getWaitingConsumerCount()); |
739 |
jsr166 |
1.13 |
assertFalse(q.hasWaitingConsumer()); |
740 |
jsr166 |
1.31 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
741 |
jsr166 |
1.9 |
|
742 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
743 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
744 |
jsr166 |
1.31 |
threadStarted.countDown(); |
745 |
jsr166 |
1.66 |
long startTime = System.nanoTime(); |
746 |
jsr166 |
1.31 |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
747 |
jsr166 |
1.50 |
assertEquals(0, q.getWaitingConsumerCount()); |
748 |
jsr166 |
1.22 |
assertFalse(q.hasWaitingConsumer()); |
749 |
jsr166 |
1.66 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
750 |
jsr166 |
1.9 |
}}); |
751 |
|
|
|
752 |
jsr166 |
1.31 |
threadStarted.await(); |
753 |
jsr166 |
1.70 |
Callable<Boolean> oneConsumer |
754 |
|
|
= new Callable<Boolean>() { public Boolean call() { |
755 |
|
|
return q.hasWaitingConsumer() |
756 |
|
|
&& q.getWaitingConsumerCount() == 1; }}; |
757 |
|
|
waitForThreadToEnterWaitState(t, oneConsumer); |
758 |
jsr166 |
1.31 |
|
759 |
|
|
assertTrue(q.offer(one)); |
760 |
jsr166 |
1.50 |
assertEquals(0, q.getWaitingConsumerCount()); |
761 |
jsr166 |
1.13 |
assertFalse(q.hasWaitingConsumer()); |
762 |
jsr166 |
1.31 |
|
763 |
jsr166 |
1.66 |
awaitTermination(t); |
764 |
dl |
1.1 |
} |
765 |
jsr166 |
1.3 |
|
766 |
|
|
/** |
767 |
jsr166 |
1.5 |
* transfer(null) throws NullPointerException |
768 |
dl |
1.1 |
*/ |
769 |
jsr166 |
1.5 |
public void testTransfer1() throws InterruptedException { |
770 |
dl |
1.1 |
try { |
771 |
|
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
772 |
|
|
q.transfer(null); |
773 |
|
|
shouldThrow(); |
774 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
775 |
dl |
1.1 |
} |
776 |
|
|
|
777 |
jsr166 |
1.3 |
/** |
778 |
jsr166 |
1.80 |
* transfer waits until a poll occurs. The transferred element |
779 |
jsr166 |
1.69 |
* is returned by the associated poll. |
780 |
dl |
1.1 |
*/ |
781 |
jsr166 |
1.5 |
public void testTransfer2() throws InterruptedException { |
782 |
jsr166 |
1.68 |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
783 |
jsr166 |
1.31 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
784 |
jsr166 |
1.5 |
|
785 |
jsr166 |
1.9 |
Thread t = newStartedThread(new CheckedRunnable() { |
786 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
787 |
jsr166 |
1.31 |
threadStarted.countDown(); |
788 |
jsr166 |
1.34 |
q.transfer(five); |
789 |
jsr166 |
1.31 |
checkEmpty(q); |
790 |
jsr166 |
1.9 |
}}); |
791 |
dl |
1.1 |
|
792 |
jsr166 |
1.31 |
threadStarted.await(); |
793 |
jsr166 |
1.70 |
Callable<Boolean> oneElement |
794 |
|
|
= new Callable<Boolean>() { public Boolean call() { |
795 |
|
|
return !q.isEmpty() && q.size() == 1; }}; |
796 |
|
|
waitForThreadToEnterWaitState(t, oneElement); |
797 |
|
|
|
798 |
jsr166 |
1.34 |
assertSame(five, q.poll()); |
799 |
jsr166 |
1.31 |
checkEmpty(q); |
800 |
jsr166 |
1.66 |
awaitTermination(t); |
801 |
dl |
1.1 |
} |
802 |
jsr166 |
1.3 |
|
803 |
|
|
/** |
804 |
dl |
1.6 |
* transfer waits until a poll occurs, and then transfers in fifo order |
805 |
dl |
1.1 |
*/ |
806 |
jsr166 |
1.5 |
public void testTransfer3() throws InterruptedException { |
807 |
jsr166 |
1.68 |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
808 |
jsr166 |
1.5 |
|
809 |
jsr166 |
1.9 |
Thread first = newStartedThread(new CheckedRunnable() { |
810 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
811 |
jsr166 |
1.32 |
q.transfer(four); |
812 |
jsr166 |
1.71 |
assertFalse(q.contains(four)); |
813 |
jsr166 |
1.24 |
assertEquals(1, q.size()); |
814 |
jsr166 |
1.7 |
}}); |
815 |
jsr166 |
1.5 |
|
816 |
jsr166 |
1.9 |
Thread interruptedThread = newStartedThread( |
817 |
|
|
new CheckedInterruptedRunnable() { |
818 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
819 |
jsr166 |
1.32 |
while (q.isEmpty()) |
820 |
jsr166 |
1.9 |
Thread.yield(); |
821 |
jsr166 |
1.32 |
q.transfer(five); |
822 |
jsr166 |
1.9 |
}}); |
823 |
jsr166 |
1.5 |
|
824 |
|
|
while (q.size() < 2) |
825 |
|
|
Thread.yield(); |
826 |
|
|
assertEquals(2, q.size()); |
827 |
jsr166 |
1.32 |
assertSame(four, q.poll()); |
828 |
jsr166 |
1.5 |
first.join(); |
829 |
|
|
assertEquals(1, q.size()); |
830 |
|
|
interruptedThread.interrupt(); |
831 |
|
|
interruptedThread.join(); |
832 |
jsr166 |
1.31 |
checkEmpty(q); |
833 |
dl |
1.1 |
} |
834 |
|
|
|
835 |
|
|
/** |
836 |
dl |
1.6 |
* transfer waits until a poll occurs, at which point the polling |
837 |
|
|
* thread returns the element |
838 |
dl |
1.1 |
*/ |
839 |
jsr166 |
1.5 |
public void testTransfer4() throws InterruptedException { |
840 |
dl |
1.1 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
841 |
jsr166 |
1.9 |
|
842 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
843 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
844 |
jsr166 |
1.7 |
q.transfer(four); |
845 |
jsr166 |
1.24 |
assertFalse(q.contains(four)); |
846 |
|
|
assertSame(three, q.poll()); |
847 |
jsr166 |
1.9 |
}}); |
848 |
|
|
|
849 |
jsr166 |
1.31 |
while (q.isEmpty()) |
850 |
|
|
Thread.yield(); |
851 |
|
|
assertFalse(q.isEmpty()); |
852 |
|
|
assertEquals(1, q.size()); |
853 |
jsr166 |
1.5 |
assertTrue(q.offer(three)); |
854 |
jsr166 |
1.24 |
assertSame(four, q.poll()); |
855 |
jsr166 |
1.66 |
awaitTermination(t); |
856 |
dl |
1.1 |
} |
857 |
jsr166 |
1.3 |
|
858 |
|
|
/** |
859 |
jsr166 |
1.80 |
* transfer waits until a take occurs. The transferred element |
860 |
jsr166 |
1.69 |
* is returned by the associated take. |
861 |
dl |
1.6 |
*/ |
862 |
|
|
public void testTransfer5() throws InterruptedException { |
863 |
jsr166 |
1.68 |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
864 |
dl |
1.6 |
|
865 |
jsr166 |
1.9 |
Thread t = newStartedThread(new CheckedRunnable() { |
866 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
867 |
jsr166 |
1.31 |
q.transfer(four); |
868 |
jsr166 |
1.12 |
checkEmpty(q); |
869 |
jsr166 |
1.9 |
}}); |
870 |
dl |
1.6 |
|
871 |
jsr166 |
1.31 |
while (q.isEmpty()) |
872 |
|
|
Thread.yield(); |
873 |
|
|
assertFalse(q.isEmpty()); |
874 |
|
|
assertEquals(1, q.size()); |
875 |
|
|
assertSame(four, q.take()); |
876 |
jsr166 |
1.12 |
checkEmpty(q); |
877 |
jsr166 |
1.66 |
awaitTermination(t); |
878 |
dl |
1.6 |
} |
879 |
|
|
|
880 |
|
|
/** |
881 |
jsr166 |
1.5 |
* tryTransfer(null) throws NullPointerException |
882 |
dl |
1.1 |
*/ |
883 |
|
|
public void testTryTransfer1() { |
884 |
jsr166 |
1.60 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
885 |
dl |
1.1 |
try { |
886 |
|
|
q.tryTransfer(null); |
887 |
jsr166 |
1.8 |
shouldThrow(); |
888 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
889 |
dl |
1.1 |
} |
890 |
jsr166 |
1.3 |
|
891 |
|
|
/** |
892 |
jsr166 |
1.5 |
* tryTransfer returns false and does not enqueue if there are no |
893 |
|
|
* consumers waiting to poll or take. |
894 |
dl |
1.1 |
*/ |
895 |
jsr166 |
1.12 |
public void testTryTransfer2() throws InterruptedException { |
896 |
jsr166 |
1.5 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
897 |
|
|
assertFalse(q.tryTransfer(new Object())); |
898 |
|
|
assertFalse(q.hasWaitingConsumer()); |
899 |
jsr166 |
1.12 |
checkEmpty(q); |
900 |
dl |
1.1 |
} |
901 |
jsr166 |
1.3 |
|
902 |
|
|
/** |
903 |
jsr166 |
1.5 |
* If there is a consumer waiting in timed poll, tryTransfer |
904 |
|
|
* returns true while successfully transfering object. |
905 |
dl |
1.1 |
*/ |
906 |
jsr166 |
1.5 |
public void testTryTransfer3() throws InterruptedException { |
907 |
|
|
final Object hotPotato = new Object(); |
908 |
|
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
909 |
jsr166 |
1.9 |
|
910 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
911 |
jsr166 |
1.21 |
public void realRun() { |
912 |
jsr166 |
1.7 |
while (! q.hasWaitingConsumer()) |
913 |
|
|
Thread.yield(); |
914 |
jsr166 |
1.24 |
assertTrue(q.hasWaitingConsumer()); |
915 |
jsr166 |
1.31 |
checkEmpty(q); |
916 |
jsr166 |
1.24 |
assertTrue(q.tryTransfer(hotPotato)); |
917 |
jsr166 |
1.9 |
}}); |
918 |
|
|
|
919 |
jsr166 |
1.66 |
long startTime = System.nanoTime(); |
920 |
|
|
assertSame(hotPotato, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
921 |
|
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
922 |
jsr166 |
1.12 |
checkEmpty(q); |
923 |
jsr166 |
1.66 |
awaitTermination(t); |
924 |
jsr166 |
1.5 |
} |
925 |
dl |
1.1 |
|
926 |
jsr166 |
1.5 |
/** |
927 |
|
|
* If there is a consumer waiting in take, tryTransfer returns |
928 |
|
|
* true while successfully transfering object. |
929 |
|
|
*/ |
930 |
|
|
public void testTryTransfer4() throws InterruptedException { |
931 |
|
|
final Object hotPotato = new Object(); |
932 |
|
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
933 |
jsr166 |
1.9 |
|
934 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
935 |
jsr166 |
1.21 |
public void realRun() { |
936 |
jsr166 |
1.7 |
while (! q.hasWaitingConsumer()) |
937 |
|
|
Thread.yield(); |
938 |
jsr166 |
1.24 |
assertTrue(q.hasWaitingConsumer()); |
939 |
jsr166 |
1.31 |
checkEmpty(q); |
940 |
jsr166 |
1.24 |
assertTrue(q.tryTransfer(hotPotato)); |
941 |
jsr166 |
1.9 |
}}); |
942 |
|
|
|
943 |
jsr166 |
1.23 |
assertSame(q.take(), hotPotato); |
944 |
jsr166 |
1.12 |
checkEmpty(q); |
945 |
jsr166 |
1.66 |
awaitTermination(t); |
946 |
dl |
1.1 |
} |
947 |
|
|
|
948 |
jsr166 |
1.3 |
/** |
949 |
jsr166 |
1.46 |
* tryTransfer blocks interruptibly if no takers |
950 |
dl |
1.1 |
*/ |
951 |
jsr166 |
1.5 |
public void testTryTransfer5() throws InterruptedException { |
952 |
dl |
1.1 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
953 |
jsr166 |
1.46 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
954 |
jsr166 |
1.40 |
assertTrue(q.isEmpty()); |
955 |
jsr166 |
1.9 |
|
956 |
jsr166 |
1.31 |
Thread t = newStartedThread(new CheckedRunnable() { |
957 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
958 |
jsr166 |
1.66 |
long startTime = System.nanoTime(); |
959 |
jsr166 |
1.46 |
Thread.currentThread().interrupt(); |
960 |
|
|
try { |
961 |
|
|
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
962 |
|
|
shouldThrow(); |
963 |
|
|
} catch (InterruptedException success) {} |
964 |
|
|
assertFalse(Thread.interrupted()); |
965 |
|
|
|
966 |
|
|
pleaseInterrupt.countDown(); |
967 |
jsr166 |
1.31 |
try { |
968 |
|
|
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
969 |
|
|
shouldThrow(); |
970 |
|
|
} catch (InterruptedException success) {} |
971 |
jsr166 |
1.46 |
assertFalse(Thread.interrupted()); |
972 |
jsr166 |
1.66 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
973 |
jsr166 |
1.5 |
}}); |
974 |
jsr166 |
1.9 |
|
975 |
jsr166 |
1.46 |
await(pleaseInterrupt); |
976 |
jsr166 |
1.75 |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
977 |
jsr166 |
1.31 |
t.interrupt(); |
978 |
jsr166 |
1.46 |
awaitTermination(t); |
979 |
jsr166 |
1.31 |
checkEmpty(q); |
980 |
dl |
1.1 |
} |
981 |
|
|
|
982 |
jsr166 |
1.3 |
/** |
983 |
jsr166 |
1.33 |
* tryTransfer gives up after the timeout and returns false |
984 |
dl |
1.1 |
*/ |
985 |
jsr166 |
1.5 |
public void testTryTransfer6() throws InterruptedException { |
986 |
dl |
1.1 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
987 |
jsr166 |
1.9 |
|
988 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
989 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
990 |
jsr166 |
1.65 |
long startTime = System.nanoTime(); |
991 |
jsr166 |
1.23 |
assertFalse(q.tryTransfer(new Object(), |
992 |
jsr166 |
1.46 |
timeoutMillis(), MILLISECONDS)); |
993 |
jsr166 |
1.65 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
994 |
jsr166 |
1.32 |
checkEmpty(q); |
995 |
jsr166 |
1.9 |
}}); |
996 |
|
|
|
997 |
jsr166 |
1.46 |
awaitTermination(t); |
998 |
jsr166 |
1.27 |
checkEmpty(q); |
999 |
dl |
1.1 |
} |
1000 |
|
|
|
1001 |
jsr166 |
1.3 |
/** |
1002 |
dl |
1.1 |
* tryTransfer waits for any elements previously in to be removed |
1003 |
|
|
* before transfering to a poll or take |
1004 |
|
|
*/ |
1005 |
jsr166 |
1.5 |
public void testTryTransfer7() throws InterruptedException { |
1006 |
dl |
1.1 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1007 |
jsr166 |
1.5 |
assertTrue(q.offer(four)); |
1008 |
jsr166 |
1.9 |
|
1009 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
1010 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
1011 |
jsr166 |
1.66 |
long startTime = System.nanoTime(); |
1012 |
|
|
assertTrue(q.tryTransfer(five, LONG_DELAY_MS, MILLISECONDS)); |
1013 |
|
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1014 |
jsr166 |
1.31 |
checkEmpty(q); |
1015 |
jsr166 |
1.9 |
}}); |
1016 |
|
|
|
1017 |
jsr166 |
1.32 |
while (q.size() != 2) |
1018 |
|
|
Thread.yield(); |
1019 |
jsr166 |
1.5 |
assertEquals(2, q.size()); |
1020 |
jsr166 |
1.24 |
assertSame(four, q.poll()); |
1021 |
|
|
assertSame(five, q.poll()); |
1022 |
jsr166 |
1.12 |
checkEmpty(q); |
1023 |
jsr166 |
1.66 |
awaitTermination(t); |
1024 |
dl |
1.1 |
} |
1025 |
|
|
|
1026 |
jsr166 |
1.3 |
/** |
1027 |
jsr166 |
1.41 |
* tryTransfer attempts to enqueue into the queue and fails |
1028 |
|
|
* returning false not enqueueing and the successive poll is null |
1029 |
dl |
1.1 |
*/ |
1030 |
jsr166 |
1.5 |
public void testTryTransfer8() throws InterruptedException { |
1031 |
dl |
1.1 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1032 |
jsr166 |
1.5 |
assertTrue(q.offer(four)); |
1033 |
|
|
assertEquals(1, q.size()); |
1034 |
jsr166 |
1.65 |
long startTime = System.nanoTime(); |
1035 |
jsr166 |
1.46 |
assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS)); |
1036 |
jsr166 |
1.65 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1037 |
jsr166 |
1.5 |
assertEquals(1, q.size()); |
1038 |
jsr166 |
1.24 |
assertSame(four, q.poll()); |
1039 |
jsr166 |
1.13 |
assertNull(q.poll()); |
1040 |
jsr166 |
1.12 |
checkEmpty(q); |
1041 |
dl |
1.1 |
} |
1042 |
|
|
|
1043 |
jsr166 |
1.5 |
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1044 |
jsr166 |
1.68 |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
1045 |
jsr166 |
1.31 |
checkEmpty(q); |
1046 |
jsr166 |
1.5 |
for (int i = 0; i < n; i++) { |
1047 |
|
|
assertEquals(i, q.size()); |
1048 |
dl |
1.1 |
assertTrue(q.offer(i)); |
1049 |
jsr166 |
1.5 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
1050 |
dl |
1.1 |
} |
1051 |
|
|
assertFalse(q.isEmpty()); |
1052 |
|
|
return q; |
1053 |
|
|
} |
1054 |
jsr166 |
1.53 |
|
1055 |
|
|
/** |
1056 |
|
|
* remove(null), contains(null) always return false |
1057 |
|
|
*/ |
1058 |
|
|
public void testNeverContainsNull() { |
1059 |
|
|
Collection<?>[] qs = { |
1060 |
|
|
new LinkedTransferQueue<Object>(), |
1061 |
|
|
populatedQueue(2), |
1062 |
|
|
}; |
1063 |
|
|
|
1064 |
|
|
for (Collection<?> q : qs) { |
1065 |
|
|
assertFalse(q.contains(null)); |
1066 |
|
|
assertFalse(q.remove(null)); |
1067 |
|
|
} |
1068 |
|
|
} |
1069 |
dl |
1.1 |
} |