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