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.58 |
BlockingQueue 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 |
jsr166 |
1.58 |
assertEquals(i, q.remove()); |
123 |
dl |
1.1 |
} |
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.58 |
assertTrue(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.57 |
int i; |
504 |
|
|
for (i = 0; it.hasNext(); i++) |
505 |
|
|
assertTrue(q.contains(it.next())); |
506 |
|
|
assertEquals(i, SIZE); |
507 |
|
|
assertIteratorExhausted(it); |
508 |
|
|
|
509 |
|
|
it = q.iterator(); |
510 |
|
|
for (i = 0; it.hasNext(); i++) |
511 |
|
|
assertEquals(it.next(), q.take()); |
512 |
jsr166 |
1.10 |
assertEquals(i, SIZE); |
513 |
jsr166 |
1.57 |
assertIteratorExhausted(it); |
514 |
|
|
} |
515 |
|
|
|
516 |
|
|
/** |
517 |
|
|
* iterator of empty collection has no elements |
518 |
|
|
*/ |
519 |
|
|
public void testEmptyIterator() { |
520 |
|
|
assertIteratorExhausted(new LinkedTransferQueue().iterator()); |
521 |
dl |
1.1 |
} |
522 |
|
|
|
523 |
|
|
/** |
524 |
jsr166 |
1.12 |
* iterator.remove() removes current element |
525 |
dl |
1.1 |
*/ |
526 |
|
|
public void testIteratorRemove() { |
527 |
|
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
528 |
|
|
q.add(two); |
529 |
|
|
q.add(one); |
530 |
|
|
q.add(three); |
531 |
|
|
|
532 |
|
|
Iterator it = q.iterator(); |
533 |
|
|
it.next(); |
534 |
|
|
it.remove(); |
535 |
|
|
|
536 |
|
|
it = q.iterator(); |
537 |
jsr166 |
1.24 |
assertSame(it.next(), one); |
538 |
|
|
assertSame(it.next(), three); |
539 |
dl |
1.1 |
assertFalse(it.hasNext()); |
540 |
|
|
} |
541 |
|
|
|
542 |
|
|
/** |
543 |
|
|
* iterator ordering is FIFO |
544 |
|
|
*/ |
545 |
|
|
public void testIteratorOrdering() { |
546 |
jsr166 |
1.7 |
final LinkedTransferQueue<Integer> q |
547 |
|
|
= new LinkedTransferQueue<Integer>(); |
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 |
dl |
1.1 |
ExecutorService executor = Executors.newFixedThreadPool(2); |
593 |
jsr166 |
1.7 |
|
594 |
|
|
executor.execute(new CheckedRunnable() { |
595 |
jsr166 |
1.31 |
public void realRun() throws InterruptedException { |
596 |
|
|
threadsStarted.await(); |
597 |
jsr166 |
1.46 |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
598 |
jsr166 |
1.5 |
}}); |
599 |
dl |
1.1 |
|
600 |
jsr166 |
1.7 |
executor.execute(new CheckedRunnable() { |
601 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
602 |
jsr166 |
1.31 |
threadsStarted.await(); |
603 |
jsr166 |
1.24 |
assertSame(one, q.take()); |
604 |
jsr166 |
1.31 |
checkEmpty(q); |
605 |
jsr166 |
1.5 |
}}); |
606 |
dl |
1.1 |
|
607 |
|
|
joinPool(executor); |
608 |
|
|
} |
609 |
|
|
|
610 |
|
|
/** |
611 |
jsr166 |
1.13 |
* timed poll retrieves elements across Executor threads |
612 |
dl |
1.1 |
*/ |
613 |
|
|
public void testPollInExecutor() { |
614 |
|
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
615 |
jsr166 |
1.46 |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
616 |
dl |
1.1 |
ExecutorService executor = Executors.newFixedThreadPool(2); |
617 |
jsr166 |
1.7 |
|
618 |
|
|
executor.execute(new CheckedRunnable() { |
619 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
620 |
jsr166 |
1.22 |
assertNull(q.poll()); |
621 |
jsr166 |
1.31 |
threadsStarted.await(); |
622 |
jsr166 |
1.46 |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
623 |
jsr166 |
1.31 |
checkEmpty(q); |
624 |
jsr166 |
1.5 |
}}); |
625 |
dl |
1.1 |
|
626 |
jsr166 |
1.7 |
executor.execute(new CheckedRunnable() { |
627 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
628 |
jsr166 |
1.31 |
threadsStarted.await(); |
629 |
jsr166 |
1.7 |
q.put(one); |
630 |
jsr166 |
1.5 |
}}); |
631 |
dl |
1.1 |
|
632 |
|
|
joinPool(executor); |
633 |
|
|
} |
634 |
|
|
|
635 |
|
|
/** |
636 |
|
|
* A deserialized serialized queue has same elements in same order |
637 |
|
|
*/ |
638 |
jsr166 |
1.5 |
public void testSerialization() throws Exception { |
639 |
jsr166 |
1.48 |
Queue x = populatedQueue(SIZE); |
640 |
|
|
Queue y = serialClone(x); |
641 |
dl |
1.1 |
|
642 |
jsr166 |
1.52 |
assertNotSame(y, x); |
643 |
jsr166 |
1.48 |
assertEquals(x.size(), y.size()); |
644 |
|
|
assertEquals(x.toString(), y.toString()); |
645 |
|
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
646 |
|
|
while (!x.isEmpty()) { |
647 |
|
|
assertFalse(y.isEmpty()); |
648 |
|
|
assertEquals(x.remove(), y.remove()); |
649 |
dl |
1.1 |
} |
650 |
jsr166 |
1.48 |
assertTrue(y.isEmpty()); |
651 |
dl |
1.1 |
} |
652 |
|
|
|
653 |
|
|
/** |
654 |
|
|
* drainTo(c) empties queue into another collection c |
655 |
|
|
*/ |
656 |
|
|
public void testDrainTo() { |
657 |
|
|
LinkedTransferQueue q = populatedQueue(SIZE); |
658 |
|
|
ArrayList l = new ArrayList(); |
659 |
|
|
q.drainTo(l); |
660 |
jsr166 |
1.50 |
assertEquals(0, q.size()); |
661 |
|
|
assertEquals(SIZE, l.size()); |
662 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
663 |
jsr166 |
1.51 |
assertEquals(i, l.get(i)); |
664 |
dl |
1.1 |
} |
665 |
|
|
q.add(zero); |
666 |
|
|
q.add(one); |
667 |
|
|
assertFalse(q.isEmpty()); |
668 |
|
|
assertTrue(q.contains(zero)); |
669 |
|
|
assertTrue(q.contains(one)); |
670 |
|
|
l.clear(); |
671 |
|
|
q.drainTo(l); |
672 |
jsr166 |
1.50 |
assertEquals(0, q.size()); |
673 |
|
|
assertEquals(2, l.size()); |
674 |
dl |
1.1 |
for (int i = 0; i < 2; ++i) { |
675 |
jsr166 |
1.51 |
assertEquals(i, l.get(i)); |
676 |
dl |
1.1 |
} |
677 |
|
|
} |
678 |
|
|
|
679 |
|
|
/** |
680 |
jsr166 |
1.13 |
* drainTo(c) empties full queue, unblocking a waiting put. |
681 |
dl |
1.1 |
*/ |
682 |
jsr166 |
1.5 |
public void testDrainToWithActivePut() throws InterruptedException { |
683 |
dl |
1.1 |
final LinkedTransferQueue q = populatedQueue(SIZE); |
684 |
jsr166 |
1.9 |
Thread t = newStartedThread(new CheckedRunnable() { |
685 |
jsr166 |
1.21 |
public void realRun() { |
686 |
jsr166 |
1.7 |
q.put(SIZE + 1); |
687 |
jsr166 |
1.5 |
}}); |
688 |
|
|
ArrayList l = new ArrayList(); |
689 |
|
|
q.drainTo(l); |
690 |
|
|
assertTrue(l.size() >= SIZE); |
691 |
jsr166 |
1.51 |
for (int i = 0; i < SIZE; ++i) |
692 |
|
|
assertEquals(i, l.get(i)); |
693 |
jsr166 |
1.29 |
awaitTermination(t, MEDIUM_DELAY_MS); |
694 |
jsr166 |
1.5 |
assertTrue(q.size() + l.size() >= SIZE); |
695 |
dl |
1.1 |
} |
696 |
|
|
|
697 |
|
|
/** |
698 |
jsr166 |
1.26 |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
699 |
dl |
1.1 |
*/ |
700 |
|
|
public void testDrainToN() { |
701 |
|
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
702 |
|
|
for (int i = 0; i < SIZE + 2; ++i) { |
703 |
|
|
for (int j = 0; j < SIZE; j++) { |
704 |
jsr166 |
1.5 |
assertTrue(q.offer(j)); |
705 |
dl |
1.1 |
} |
706 |
|
|
ArrayList l = new ArrayList(); |
707 |
|
|
q.drainTo(l, i); |
708 |
|
|
int k = (i < SIZE) ? i : SIZE; |
709 |
jsr166 |
1.51 |
assertEquals(k, l.size()); |
710 |
|
|
assertEquals(SIZE - k, q.size()); |
711 |
|
|
for (int j = 0; j < k; ++j) |
712 |
|
|
assertEquals(j, l.get(j)); |
713 |
jsr166 |
1.56 |
do {} while (q.poll() != null); |
714 |
dl |
1.1 |
} |
715 |
|
|
} |
716 |
|
|
|
717 |
jsr166 |
1.3 |
/** |
718 |
jsr166 |
1.13 |
* timed poll() or take() increments the waiting consumer count; |
719 |
|
|
* offer(e) decrements the waiting consumer count |
720 |
dl |
1.1 |
*/ |
721 |
jsr166 |
1.5 |
public void testWaitingConsumer() throws InterruptedException { |
722 |
|
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
723 |
jsr166 |
1.50 |
assertEquals(0, q.getWaitingConsumerCount()); |
724 |
jsr166 |
1.13 |
assertFalse(q.hasWaitingConsumer()); |
725 |
jsr166 |
1.31 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
726 |
jsr166 |
1.9 |
|
727 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
728 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
729 |
jsr166 |
1.31 |
threadStarted.countDown(); |
730 |
|
|
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
731 |
jsr166 |
1.50 |
assertEquals(0, q.getWaitingConsumerCount()); |
732 |
jsr166 |
1.22 |
assertFalse(q.hasWaitingConsumer()); |
733 |
jsr166 |
1.9 |
}}); |
734 |
|
|
|
735 |
jsr166 |
1.31 |
threadStarted.await(); |
736 |
|
|
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
737 |
jsr166 |
1.50 |
assertEquals(1, q.getWaitingConsumerCount()); |
738 |
jsr166 |
1.31 |
assertTrue(q.hasWaitingConsumer()); |
739 |
|
|
|
740 |
|
|
assertTrue(q.offer(one)); |
741 |
jsr166 |
1.50 |
assertEquals(0, q.getWaitingConsumerCount()); |
742 |
jsr166 |
1.13 |
assertFalse(q.hasWaitingConsumer()); |
743 |
jsr166 |
1.31 |
|
744 |
jsr166 |
1.29 |
awaitTermination(t, MEDIUM_DELAY_MS); |
745 |
dl |
1.1 |
} |
746 |
jsr166 |
1.3 |
|
747 |
|
|
/** |
748 |
jsr166 |
1.5 |
* transfer(null) throws NullPointerException |
749 |
dl |
1.1 |
*/ |
750 |
jsr166 |
1.5 |
public void testTransfer1() throws InterruptedException { |
751 |
dl |
1.1 |
try { |
752 |
|
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
753 |
|
|
q.transfer(null); |
754 |
|
|
shouldThrow(); |
755 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
756 |
dl |
1.1 |
} |
757 |
|
|
|
758 |
jsr166 |
1.3 |
/** |
759 |
dl |
1.6 |
* transfer waits until a poll occurs. The transfered element |
760 |
|
|
* is returned by this associated poll. |
761 |
dl |
1.1 |
*/ |
762 |
jsr166 |
1.5 |
public void testTransfer2() throws InterruptedException { |
763 |
jsr166 |
1.7 |
final LinkedTransferQueue<Integer> q |
764 |
|
|
= new LinkedTransferQueue<Integer>(); |
765 |
jsr166 |
1.31 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
766 |
jsr166 |
1.5 |
|
767 |
jsr166 |
1.9 |
Thread t = newStartedThread(new CheckedRunnable() { |
768 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
769 |
jsr166 |
1.31 |
threadStarted.countDown(); |
770 |
jsr166 |
1.34 |
q.transfer(five); |
771 |
jsr166 |
1.31 |
checkEmpty(q); |
772 |
jsr166 |
1.9 |
}}); |
773 |
dl |
1.1 |
|
774 |
jsr166 |
1.31 |
threadStarted.await(); |
775 |
|
|
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
776 |
jsr166 |
1.5 |
assertEquals(1, q.size()); |
777 |
jsr166 |
1.34 |
assertSame(five, q.poll()); |
778 |
jsr166 |
1.31 |
checkEmpty(q); |
779 |
jsr166 |
1.29 |
awaitTermination(t, MEDIUM_DELAY_MS); |
780 |
dl |
1.1 |
} |
781 |
jsr166 |
1.3 |
|
782 |
|
|
/** |
783 |
dl |
1.6 |
* transfer waits until a poll occurs, and then transfers in fifo order |
784 |
dl |
1.1 |
*/ |
785 |
jsr166 |
1.5 |
public void testTransfer3() throws InterruptedException { |
786 |
jsr166 |
1.7 |
final LinkedTransferQueue<Integer> q |
787 |
|
|
= new LinkedTransferQueue<Integer>(); |
788 |
jsr166 |
1.5 |
|
789 |
jsr166 |
1.9 |
Thread first = newStartedThread(new CheckedRunnable() { |
790 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
791 |
jsr166 |
1.32 |
q.transfer(four); |
792 |
|
|
assertTrue(!q.contains(four)); |
793 |
jsr166 |
1.24 |
assertEquals(1, q.size()); |
794 |
jsr166 |
1.7 |
}}); |
795 |
jsr166 |
1.5 |
|
796 |
jsr166 |
1.9 |
Thread interruptedThread = newStartedThread( |
797 |
|
|
new CheckedInterruptedRunnable() { |
798 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
799 |
jsr166 |
1.32 |
while (q.isEmpty()) |
800 |
jsr166 |
1.9 |
Thread.yield(); |
801 |
jsr166 |
1.32 |
q.transfer(five); |
802 |
jsr166 |
1.9 |
}}); |
803 |
jsr166 |
1.5 |
|
804 |
|
|
while (q.size() < 2) |
805 |
|
|
Thread.yield(); |
806 |
|
|
assertEquals(2, q.size()); |
807 |
jsr166 |
1.32 |
assertSame(four, q.poll()); |
808 |
jsr166 |
1.5 |
first.join(); |
809 |
|
|
assertEquals(1, q.size()); |
810 |
|
|
interruptedThread.interrupt(); |
811 |
|
|
interruptedThread.join(); |
812 |
jsr166 |
1.31 |
checkEmpty(q); |
813 |
dl |
1.1 |
} |
814 |
|
|
|
815 |
|
|
/** |
816 |
dl |
1.6 |
* transfer waits until a poll occurs, at which point the polling |
817 |
|
|
* thread returns the element |
818 |
dl |
1.1 |
*/ |
819 |
jsr166 |
1.5 |
public void testTransfer4() throws InterruptedException { |
820 |
dl |
1.1 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
821 |
jsr166 |
1.9 |
|
822 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
823 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
824 |
jsr166 |
1.7 |
q.transfer(four); |
825 |
jsr166 |
1.24 |
assertFalse(q.contains(four)); |
826 |
|
|
assertSame(three, q.poll()); |
827 |
jsr166 |
1.9 |
}}); |
828 |
|
|
|
829 |
jsr166 |
1.31 |
while (q.isEmpty()) |
830 |
|
|
Thread.yield(); |
831 |
|
|
assertFalse(q.isEmpty()); |
832 |
|
|
assertEquals(1, q.size()); |
833 |
jsr166 |
1.5 |
assertTrue(q.offer(three)); |
834 |
jsr166 |
1.24 |
assertSame(four, q.poll()); |
835 |
jsr166 |
1.29 |
awaitTermination(t, MEDIUM_DELAY_MS); |
836 |
dl |
1.1 |
} |
837 |
jsr166 |
1.3 |
|
838 |
|
|
/** |
839 |
dl |
1.6 |
* transfer waits until a take occurs. The transfered element |
840 |
|
|
* is returned by this associated take. |
841 |
|
|
*/ |
842 |
|
|
public void testTransfer5() throws InterruptedException { |
843 |
jsr166 |
1.7 |
final LinkedTransferQueue<Integer> q |
844 |
|
|
= new LinkedTransferQueue<Integer>(); |
845 |
dl |
1.6 |
|
846 |
jsr166 |
1.9 |
Thread t = newStartedThread(new CheckedRunnable() { |
847 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
848 |
jsr166 |
1.31 |
q.transfer(four); |
849 |
jsr166 |
1.12 |
checkEmpty(q); |
850 |
jsr166 |
1.9 |
}}); |
851 |
dl |
1.6 |
|
852 |
jsr166 |
1.31 |
while (q.isEmpty()) |
853 |
|
|
Thread.yield(); |
854 |
|
|
assertFalse(q.isEmpty()); |
855 |
|
|
assertEquals(1, q.size()); |
856 |
|
|
assertSame(four, q.take()); |
857 |
jsr166 |
1.12 |
checkEmpty(q); |
858 |
jsr166 |
1.29 |
awaitTermination(t, MEDIUM_DELAY_MS); |
859 |
dl |
1.6 |
} |
860 |
|
|
|
861 |
|
|
/** |
862 |
jsr166 |
1.5 |
* tryTransfer(null) throws NullPointerException |
863 |
dl |
1.1 |
*/ |
864 |
|
|
public void testTryTransfer1() { |
865 |
|
|
try { |
866 |
|
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
867 |
|
|
q.tryTransfer(null); |
868 |
jsr166 |
1.8 |
shouldThrow(); |
869 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
870 |
dl |
1.1 |
} |
871 |
jsr166 |
1.3 |
|
872 |
|
|
/** |
873 |
jsr166 |
1.5 |
* tryTransfer returns false and does not enqueue if there are no |
874 |
|
|
* consumers waiting to poll or take. |
875 |
dl |
1.1 |
*/ |
876 |
jsr166 |
1.12 |
public void testTryTransfer2() throws InterruptedException { |
877 |
jsr166 |
1.5 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
878 |
|
|
assertFalse(q.tryTransfer(new Object())); |
879 |
|
|
assertFalse(q.hasWaitingConsumer()); |
880 |
jsr166 |
1.12 |
checkEmpty(q); |
881 |
dl |
1.1 |
} |
882 |
jsr166 |
1.3 |
|
883 |
|
|
/** |
884 |
jsr166 |
1.5 |
* If there is a consumer waiting in timed poll, tryTransfer |
885 |
|
|
* returns true while successfully transfering object. |
886 |
dl |
1.1 |
*/ |
887 |
jsr166 |
1.5 |
public void testTryTransfer3() throws InterruptedException { |
888 |
|
|
final Object hotPotato = new Object(); |
889 |
|
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
890 |
jsr166 |
1.9 |
|
891 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
892 |
jsr166 |
1.21 |
public void realRun() { |
893 |
jsr166 |
1.7 |
while (! q.hasWaitingConsumer()) |
894 |
|
|
Thread.yield(); |
895 |
jsr166 |
1.24 |
assertTrue(q.hasWaitingConsumer()); |
896 |
jsr166 |
1.31 |
checkEmpty(q); |
897 |
jsr166 |
1.24 |
assertTrue(q.tryTransfer(hotPotato)); |
898 |
jsr166 |
1.9 |
}}); |
899 |
|
|
|
900 |
jsr166 |
1.23 |
assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
901 |
jsr166 |
1.12 |
checkEmpty(q); |
902 |
jsr166 |
1.29 |
awaitTermination(t, MEDIUM_DELAY_MS); |
903 |
jsr166 |
1.5 |
} |
904 |
dl |
1.1 |
|
905 |
jsr166 |
1.5 |
/** |
906 |
|
|
* If there is a consumer waiting in take, tryTransfer returns |
907 |
|
|
* true while successfully transfering object. |
908 |
|
|
*/ |
909 |
|
|
public void testTryTransfer4() throws InterruptedException { |
910 |
|
|
final Object hotPotato = new Object(); |
911 |
|
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
912 |
jsr166 |
1.9 |
|
913 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
914 |
jsr166 |
1.21 |
public void realRun() { |
915 |
jsr166 |
1.7 |
while (! q.hasWaitingConsumer()) |
916 |
|
|
Thread.yield(); |
917 |
jsr166 |
1.24 |
assertTrue(q.hasWaitingConsumer()); |
918 |
jsr166 |
1.31 |
checkEmpty(q); |
919 |
jsr166 |
1.24 |
assertTrue(q.tryTransfer(hotPotato)); |
920 |
jsr166 |
1.9 |
}}); |
921 |
|
|
|
922 |
jsr166 |
1.23 |
assertSame(q.take(), hotPotato); |
923 |
jsr166 |
1.12 |
checkEmpty(q); |
924 |
jsr166 |
1.29 |
awaitTermination(t, MEDIUM_DELAY_MS); |
925 |
dl |
1.1 |
} |
926 |
|
|
|
927 |
jsr166 |
1.3 |
/** |
928 |
jsr166 |
1.46 |
* tryTransfer blocks interruptibly if no takers |
929 |
dl |
1.1 |
*/ |
930 |
jsr166 |
1.5 |
public void testTryTransfer5() throws InterruptedException { |
931 |
dl |
1.1 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
932 |
jsr166 |
1.46 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
933 |
jsr166 |
1.40 |
assertTrue(q.isEmpty()); |
934 |
jsr166 |
1.9 |
|
935 |
jsr166 |
1.31 |
Thread t = newStartedThread(new CheckedRunnable() { |
936 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
937 |
jsr166 |
1.46 |
Thread.currentThread().interrupt(); |
938 |
|
|
try { |
939 |
|
|
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
940 |
|
|
shouldThrow(); |
941 |
|
|
} catch (InterruptedException success) {} |
942 |
|
|
assertFalse(Thread.interrupted()); |
943 |
|
|
|
944 |
|
|
pleaseInterrupt.countDown(); |
945 |
jsr166 |
1.31 |
try { |
946 |
|
|
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
947 |
|
|
shouldThrow(); |
948 |
|
|
} catch (InterruptedException success) {} |
949 |
jsr166 |
1.46 |
assertFalse(Thread.interrupted()); |
950 |
jsr166 |
1.5 |
}}); |
951 |
jsr166 |
1.9 |
|
952 |
jsr166 |
1.46 |
await(pleaseInterrupt); |
953 |
|
|
assertThreadStaysAlive(t); |
954 |
jsr166 |
1.31 |
t.interrupt(); |
955 |
jsr166 |
1.46 |
awaitTermination(t); |
956 |
jsr166 |
1.31 |
checkEmpty(q); |
957 |
dl |
1.1 |
} |
958 |
|
|
|
959 |
jsr166 |
1.3 |
/** |
960 |
jsr166 |
1.33 |
* tryTransfer gives up after the timeout and returns false |
961 |
dl |
1.1 |
*/ |
962 |
jsr166 |
1.5 |
public void testTryTransfer6() throws InterruptedException { |
963 |
dl |
1.1 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
964 |
jsr166 |
1.9 |
|
965 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
966 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
967 |
jsr166 |
1.27 |
long t0 = System.nanoTime(); |
968 |
jsr166 |
1.23 |
assertFalse(q.tryTransfer(new Object(), |
969 |
jsr166 |
1.46 |
timeoutMillis(), MILLISECONDS)); |
970 |
|
|
assertTrue(millisElapsedSince(t0) >= timeoutMillis()); |
971 |
jsr166 |
1.32 |
checkEmpty(q); |
972 |
jsr166 |
1.9 |
}}); |
973 |
|
|
|
974 |
jsr166 |
1.46 |
awaitTermination(t); |
975 |
jsr166 |
1.27 |
checkEmpty(q); |
976 |
dl |
1.1 |
} |
977 |
|
|
|
978 |
jsr166 |
1.3 |
/** |
979 |
dl |
1.1 |
* tryTransfer waits for any elements previously in to be removed |
980 |
|
|
* before transfering to a poll or take |
981 |
|
|
*/ |
982 |
jsr166 |
1.5 |
public void testTryTransfer7() throws InterruptedException { |
983 |
dl |
1.1 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
984 |
jsr166 |
1.5 |
assertTrue(q.offer(four)); |
985 |
jsr166 |
1.9 |
|
986 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
987 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
988 |
jsr166 |
1.23 |
assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); |
989 |
jsr166 |
1.31 |
checkEmpty(q); |
990 |
jsr166 |
1.9 |
}}); |
991 |
|
|
|
992 |
jsr166 |
1.32 |
while (q.size() != 2) |
993 |
|
|
Thread.yield(); |
994 |
jsr166 |
1.5 |
assertEquals(2, q.size()); |
995 |
jsr166 |
1.24 |
assertSame(four, q.poll()); |
996 |
|
|
assertSame(five, q.poll()); |
997 |
jsr166 |
1.12 |
checkEmpty(q); |
998 |
jsr166 |
1.29 |
awaitTermination(t, MEDIUM_DELAY_MS); |
999 |
dl |
1.1 |
} |
1000 |
|
|
|
1001 |
jsr166 |
1.3 |
/** |
1002 |
jsr166 |
1.41 |
* tryTransfer attempts to enqueue into the queue and fails |
1003 |
|
|
* returning false not enqueueing and the successive poll is null |
1004 |
dl |
1.1 |
*/ |
1005 |
jsr166 |
1.5 |
public void testTryTransfer8() throws InterruptedException { |
1006 |
dl |
1.1 |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1007 |
jsr166 |
1.5 |
assertTrue(q.offer(four)); |
1008 |
|
|
assertEquals(1, q.size()); |
1009 |
jsr166 |
1.32 |
long t0 = System.nanoTime(); |
1010 |
jsr166 |
1.46 |
assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS)); |
1011 |
|
|
assertTrue(millisElapsedSince(t0) >= timeoutMillis()); |
1012 |
jsr166 |
1.5 |
assertEquals(1, q.size()); |
1013 |
jsr166 |
1.24 |
assertSame(four, q.poll()); |
1014 |
jsr166 |
1.13 |
assertNull(q.poll()); |
1015 |
jsr166 |
1.12 |
checkEmpty(q); |
1016 |
dl |
1.1 |
} |
1017 |
|
|
|
1018 |
jsr166 |
1.5 |
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1019 |
|
|
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
1020 |
jsr166 |
1.31 |
checkEmpty(q); |
1021 |
jsr166 |
1.5 |
for (int i = 0; i < n; i++) { |
1022 |
|
|
assertEquals(i, q.size()); |
1023 |
dl |
1.1 |
assertTrue(q.offer(i)); |
1024 |
jsr166 |
1.5 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
1025 |
dl |
1.1 |
} |
1026 |
|
|
assertFalse(q.isEmpty()); |
1027 |
|
|
return q; |
1028 |
|
|
} |
1029 |
jsr166 |
1.53 |
|
1030 |
|
|
/** |
1031 |
|
|
* remove(null), contains(null) always return false |
1032 |
|
|
*/ |
1033 |
|
|
public void testNeverContainsNull() { |
1034 |
|
|
Collection<?>[] qs = { |
1035 |
|
|
new LinkedTransferQueue<Object>(), |
1036 |
|
|
populatedQueue(2), |
1037 |
|
|
}; |
1038 |
|
|
|
1039 |
|
|
for (Collection<?> q : qs) { |
1040 |
|
|
assertFalse(q.contains(null)); |
1041 |
|
|
assertFalse(q.remove(null)); |
1042 |
|
|
} |
1043 |
|
|
} |
1044 |
dl |
1.1 |
} |