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