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