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