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