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