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