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