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