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