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