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