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