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