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