1 |
dl |
1.1 |
/* |
2 |
dl |
1.7 |
* 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 |
jsr166 |
1.40 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.12 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
import junit.framework.*; |
10 |
|
|
import java.util.*; |
11 |
|
|
import java.util.concurrent.*; |
12 |
jsr166 |
1.16 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
dl |
1.2 |
import java.io.*; |
14 |
dl |
1.1 |
|
15 |
dl |
1.3 |
public class LinkedBlockingQueueTest extends JSR166TestCase { |
16 |
dl |
1.1 |
|
17 |
jsr166 |
1.29 |
public static class Unbounded extends BlockingQueueTest { |
18 |
|
|
protected BlockingQueue emptyCollection() { |
19 |
|
|
return new LinkedBlockingQueue(); |
20 |
|
|
} |
21 |
|
|
} |
22 |
|
|
|
23 |
|
|
public static class Bounded extends BlockingQueueTest { |
24 |
|
|
protected BlockingQueue emptyCollection() { |
25 |
|
|
return new LinkedBlockingQueue(20); |
26 |
|
|
} |
27 |
|
|
} |
28 |
|
|
|
29 |
dl |
1.1 |
public static void main(String[] args) { |
30 |
jsr166 |
1.26 |
junit.textui.TestRunner.run(suite()); |
31 |
dl |
1.1 |
} |
32 |
|
|
|
33 |
|
|
public static Test suite() { |
34 |
jsr166 |
1.29 |
return newTestSuite(LinkedBlockingQueueTest.class, |
35 |
|
|
new Unbounded().testSuite(), |
36 |
|
|
new Bounded().testSuite()); |
37 |
dl |
1.1 |
} |
38 |
|
|
|
39 |
dl |
1.3 |
|
40 |
dl |
1.1 |
/** |
41 |
|
|
* Create a queue of given size containing consecutive |
42 |
|
|
* Integers 0 ... n. |
43 |
|
|
*/ |
44 |
jsr166 |
1.37 |
private LinkedBlockingQueue<Integer> populatedQueue(int n) { |
45 |
|
|
LinkedBlockingQueue<Integer> q = |
46 |
|
|
new LinkedBlockingQueue<Integer>(n); |
47 |
dl |
1.1 |
assertTrue(q.isEmpty()); |
48 |
jsr166 |
1.15 |
for (int i = 0; i < n; i++) |
49 |
|
|
assertTrue(q.offer(new Integer(i))); |
50 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
51 |
|
|
assertEquals(0, q.remainingCapacity()); |
52 |
jsr166 |
1.15 |
assertEquals(n, q.size()); |
53 |
dl |
1.1 |
return q; |
54 |
|
|
} |
55 |
jsr166 |
1.12 |
|
56 |
dl |
1.4 |
/** |
57 |
dl |
1.5 |
* A new queue has the indicated capacity, or Integer.MAX_VALUE if |
58 |
|
|
* none given |
59 |
dl |
1.4 |
*/ |
60 |
|
|
public void testConstructor1() { |
61 |
dl |
1.3 |
assertEquals(SIZE, new LinkedBlockingQueue(SIZE).remainingCapacity()); |
62 |
dl |
1.5 |
assertEquals(Integer.MAX_VALUE, new LinkedBlockingQueue().remainingCapacity()); |
63 |
dl |
1.1 |
} |
64 |
|
|
|
65 |
dl |
1.4 |
/** |
66 |
jsr166 |
1.18 |
* Constructor throws IAE if capacity argument nonpositive |
67 |
dl |
1.4 |
*/ |
68 |
|
|
public void testConstructor2() { |
69 |
dl |
1.1 |
try { |
70 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(0); |
71 |
dl |
1.4 |
shouldThrow(); |
72 |
jsr166 |
1.17 |
} catch (IllegalArgumentException success) {} |
73 |
dl |
1.1 |
} |
74 |
|
|
|
75 |
dl |
1.4 |
/** |
76 |
dl |
1.5 |
* Initializing from null Collection throws NPE |
77 |
dl |
1.4 |
*/ |
78 |
|
|
public void testConstructor3() { |
79 |
dl |
1.1 |
try { |
80 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(null); |
81 |
dl |
1.4 |
shouldThrow(); |
82 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
83 |
dl |
1.1 |
} |
84 |
|
|
|
85 |
dl |
1.4 |
/** |
86 |
dl |
1.5 |
* Initializing from Collection of null elements throws NPE |
87 |
dl |
1.4 |
*/ |
88 |
|
|
public void testConstructor4() { |
89 |
dl |
1.1 |
try { |
90 |
dl |
1.3 |
Integer[] ints = new Integer[SIZE]; |
91 |
dl |
1.1 |
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
92 |
dl |
1.4 |
shouldThrow(); |
93 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
94 |
dl |
1.1 |
} |
95 |
|
|
|
96 |
dl |
1.4 |
/** |
97 |
dl |
1.5 |
* Initializing from Collection with some null elements throws NPE |
98 |
dl |
1.4 |
*/ |
99 |
|
|
public void testConstructor5() { |
100 |
dl |
1.1 |
try { |
101 |
dl |
1.3 |
Integer[] ints = new Integer[SIZE]; |
102 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
103 |
dl |
1.1 |
ints[i] = new Integer(i); |
104 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
105 |
dl |
1.4 |
shouldThrow(); |
106 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
107 |
dl |
1.1 |
} |
108 |
|
|
|
109 |
dl |
1.4 |
/** |
110 |
dl |
1.5 |
* Queue contains all elements of collection used to initialize |
111 |
dl |
1.4 |
*/ |
112 |
|
|
public void testConstructor6() { |
113 |
jsr166 |
1.17 |
Integer[] ints = new Integer[SIZE]; |
114 |
|
|
for (int i = 0; i < SIZE; ++i) |
115 |
|
|
ints[i] = new Integer(i); |
116 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
117 |
|
|
for (int i = 0; i < SIZE; ++i) |
118 |
|
|
assertEquals(ints[i], q.poll()); |
119 |
dl |
1.1 |
} |
120 |
|
|
|
121 |
dl |
1.4 |
/** |
122 |
dl |
1.5 |
* Queue transitions from empty to full when elements added |
123 |
dl |
1.4 |
*/ |
124 |
dl |
1.1 |
public void testEmptyFull() { |
125 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
126 |
|
|
assertTrue(q.isEmpty()); |
127 |
|
|
assertEquals("should have room for 2", 2, q.remainingCapacity()); |
128 |
dl |
1.3 |
q.add(one); |
129 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
130 |
dl |
1.3 |
q.add(two); |
131 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
132 |
dl |
1.4 |
assertEquals(0, q.remainingCapacity()); |
133 |
|
|
assertFalse(q.offer(three)); |
134 |
dl |
1.1 |
} |
135 |
|
|
|
136 |
dl |
1.4 |
/** |
137 |
dl |
1.5 |
* remainingCapacity decreases on add, increases on remove |
138 |
dl |
1.4 |
*/ |
139 |
|
|
public void testRemainingCapacity() { |
140 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
141 |
|
|
for (int i = 0; i < SIZE; ++i) { |
142 |
dl |
1.1 |
assertEquals(i, q.remainingCapacity()); |
143 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
144 |
dl |
1.1 |
q.remove(); |
145 |
|
|
} |
146 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
147 |
|
|
assertEquals(SIZE-i, q.remainingCapacity()); |
148 |
dl |
1.1 |
assertEquals(i, q.size()); |
149 |
|
|
q.add(new Integer(i)); |
150 |
|
|
} |
151 |
|
|
} |
152 |
|
|
|
153 |
dl |
1.4 |
/** |
154 |
dl |
1.5 |
* offer(null) throws NPE |
155 |
dl |
1.4 |
*/ |
156 |
|
|
public void testOfferNull() { |
157 |
jsr166 |
1.15 |
try { |
158 |
dl |
1.1 |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
159 |
|
|
q.offer(null); |
160 |
dl |
1.4 |
shouldThrow(); |
161 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
162 |
dl |
1.1 |
} |
163 |
|
|
|
164 |
dl |
1.4 |
/** |
165 |
dl |
1.6 |
* add(null) throws NPE |
166 |
|
|
*/ |
167 |
|
|
public void testAddNull() { |
168 |
jsr166 |
1.15 |
try { |
169 |
dl |
1.6 |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
170 |
|
|
q.add(null); |
171 |
|
|
shouldThrow(); |
172 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
173 |
dl |
1.6 |
} |
174 |
|
|
|
175 |
|
|
/** |
176 |
dl |
1.5 |
* Offer succeeds if not full; fails if full |
177 |
dl |
1.4 |
*/ |
178 |
|
|
public void testOffer() { |
179 |
dl |
1.1 |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
180 |
dl |
1.3 |
assertTrue(q.offer(zero)); |
181 |
|
|
assertFalse(q.offer(one)); |
182 |
dl |
1.1 |
} |
183 |
|
|
|
184 |
dl |
1.4 |
/** |
185 |
dl |
1.5 |
* add succeeds if not full; throws ISE if full |
186 |
dl |
1.4 |
*/ |
187 |
|
|
public void testAdd() { |
188 |
jsr166 |
1.15 |
try { |
189 |
dl |
1.3 |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
190 |
|
|
for (int i = 0; i < SIZE; ++i) { |
191 |
dl |
1.1 |
assertTrue(q.add(new Integer(i))); |
192 |
|
|
} |
193 |
|
|
assertEquals(0, q.remainingCapacity()); |
194 |
dl |
1.3 |
q.add(new Integer(SIZE)); |
195 |
jsr166 |
1.17 |
shouldThrow(); |
196 |
|
|
} catch (IllegalStateException success) {} |
197 |
dl |
1.1 |
} |
198 |
|
|
|
199 |
dl |
1.4 |
/** |
200 |
dl |
1.5 |
* addAll(null) throws NPE |
201 |
dl |
1.4 |
*/ |
202 |
|
|
public void testAddAll1() { |
203 |
dl |
1.1 |
try { |
204 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
205 |
|
|
q.addAll(null); |
206 |
dl |
1.4 |
shouldThrow(); |
207 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
208 |
dl |
1.1 |
} |
209 |
dl |
1.6 |
|
210 |
|
|
/** |
211 |
|
|
* addAll(this) throws IAE |
212 |
|
|
*/ |
213 |
|
|
public void testAddAllSelf() { |
214 |
|
|
try { |
215 |
|
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
216 |
|
|
q.addAll(q); |
217 |
|
|
shouldThrow(); |
218 |
jsr166 |
1.17 |
} catch (IllegalArgumentException success) {} |
219 |
dl |
1.6 |
} |
220 |
|
|
|
221 |
dl |
1.4 |
/** |
222 |
dl |
1.5 |
* addAll of a collection with null elements throws NPE |
223 |
dl |
1.4 |
*/ |
224 |
|
|
public void testAddAll2() { |
225 |
dl |
1.1 |
try { |
226 |
dl |
1.3 |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
227 |
|
|
Integer[] ints = new Integer[SIZE]; |
228 |
dl |
1.1 |
q.addAll(Arrays.asList(ints)); |
229 |
dl |
1.4 |
shouldThrow(); |
230 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
231 |
dl |
1.1 |
} |
232 |
jsr166 |
1.27 |
|
233 |
dl |
1.4 |
/** |
234 |
dl |
1.5 |
* addAll of a collection with any null elements throws NPE after |
235 |
|
|
* possibly adding some elements |
236 |
dl |
1.4 |
*/ |
237 |
|
|
public void testAddAll3() { |
238 |
dl |
1.1 |
try { |
239 |
dl |
1.3 |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
240 |
|
|
Integer[] ints = new Integer[SIZE]; |
241 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
242 |
dl |
1.1 |
ints[i] = new Integer(i); |
243 |
|
|
q.addAll(Arrays.asList(ints)); |
244 |
dl |
1.4 |
shouldThrow(); |
245 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
246 |
dl |
1.1 |
} |
247 |
jsr166 |
1.27 |
|
248 |
dl |
1.4 |
/** |
249 |
dl |
1.5 |
* addAll throws ISE if not enough room |
250 |
dl |
1.4 |
*/ |
251 |
|
|
public void testAddAll4() { |
252 |
dl |
1.1 |
try { |
253 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
254 |
dl |
1.3 |
Integer[] ints = new Integer[SIZE]; |
255 |
|
|
for (int i = 0; i < SIZE; ++i) |
256 |
dl |
1.1 |
ints[i] = new Integer(i); |
257 |
|
|
q.addAll(Arrays.asList(ints)); |
258 |
dl |
1.4 |
shouldThrow(); |
259 |
jsr166 |
1.17 |
} catch (IllegalStateException success) {} |
260 |
dl |
1.1 |
} |
261 |
jsr166 |
1.27 |
|
262 |
dl |
1.4 |
/** |
263 |
dl |
1.5 |
* Queue contains all elements, in traversal order, of successful addAll |
264 |
dl |
1.4 |
*/ |
265 |
|
|
public void testAddAll5() { |
266 |
jsr166 |
1.17 |
Integer[] empty = new Integer[0]; |
267 |
|
|
Integer[] ints = new Integer[SIZE]; |
268 |
|
|
for (int i = 0; i < SIZE; ++i) |
269 |
|
|
ints[i] = new Integer(i); |
270 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
271 |
|
|
assertFalse(q.addAll(Arrays.asList(empty))); |
272 |
|
|
assertTrue(q.addAll(Arrays.asList(ints))); |
273 |
|
|
for (int i = 0; i < SIZE; ++i) |
274 |
|
|
assertEquals(ints[i], q.poll()); |
275 |
dl |
1.1 |
} |
276 |
|
|
|
277 |
dl |
1.4 |
/** |
278 |
dl |
1.5 |
* put(null) throws NPE |
279 |
dl |
1.4 |
*/ |
280 |
jsr166 |
1.41 |
public void testPutNull() throws InterruptedException { |
281 |
jsr166 |
1.15 |
try { |
282 |
dl |
1.3 |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
283 |
dl |
1.1 |
q.put(null); |
284 |
dl |
1.4 |
shouldThrow(); |
285 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
286 |
jsr166 |
1.41 |
} |
287 |
dl |
1.1 |
|
288 |
dl |
1.4 |
/** |
289 |
dl |
1.5 |
* all elements successfully put are contained |
290 |
dl |
1.4 |
*/ |
291 |
jsr166 |
1.17 |
public void testPut() throws InterruptedException { |
292 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
293 |
|
|
for (int i = 0; i < SIZE; ++i) { |
294 |
|
|
Integer I = new Integer(i); |
295 |
|
|
q.put(I); |
296 |
|
|
assertTrue(q.contains(I)); |
297 |
dl |
1.1 |
} |
298 |
jsr166 |
1.17 |
assertEquals(0, q.remainingCapacity()); |
299 |
dl |
1.1 |
} |
300 |
|
|
|
301 |
dl |
1.4 |
/** |
302 |
dl |
1.5 |
* put blocks interruptibly if full |
303 |
dl |
1.4 |
*/ |
304 |
jsr166 |
1.17 |
public void testBlockingPut() throws InterruptedException { |
305 |
jsr166 |
1.22 |
final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
306 |
jsr166 |
1.17 |
Thread t = new Thread(new CheckedRunnable() { |
307 |
jsr166 |
1.22 |
public void realRun() throws InterruptedException { |
308 |
|
|
for (int i = 0; i < SIZE; ++i) |
309 |
|
|
q.put(i); |
310 |
|
|
assertEquals(SIZE, q.size()); |
311 |
|
|
assertEquals(0, q.remainingCapacity()); |
312 |
jsr166 |
1.17 |
try { |
313 |
jsr166 |
1.22 |
q.put(99); |
314 |
|
|
shouldThrow(); |
315 |
|
|
} catch (InterruptedException success) {} |
316 |
jsr166 |
1.17 |
}}); |
317 |
|
|
|
318 |
dl |
1.1 |
t.start(); |
319 |
dl |
1.42 |
delay(SHORT_DELAY_MS); |
320 |
jsr166 |
1.17 |
t.interrupt(); |
321 |
|
|
t.join(); |
322 |
jsr166 |
1.22 |
assertEquals(SIZE, q.size()); |
323 |
|
|
assertEquals(0, q.remainingCapacity()); |
324 |
dl |
1.1 |
} |
325 |
|
|
|
326 |
dl |
1.4 |
/** |
327 |
dl |
1.5 |
* put blocks waiting for take when full |
328 |
dl |
1.4 |
*/ |
329 |
jsr166 |
1.17 |
public void testPutWithTake() throws InterruptedException { |
330 |
jsr166 |
1.22 |
final int capacity = 2; |
331 |
dl |
1.1 |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
332 |
jsr166 |
1.17 |
Thread t = new Thread(new CheckedRunnable() { |
333 |
jsr166 |
1.22 |
public void realRun() throws InterruptedException { |
334 |
|
|
for (int i = 0; i < capacity + 1; i++) |
335 |
|
|
q.put(i); |
336 |
jsr166 |
1.17 |
try { |
337 |
jsr166 |
1.22 |
q.put(99); |
338 |
|
|
shouldThrow(); |
339 |
|
|
} catch (InterruptedException success) {} |
340 |
jsr166 |
1.17 |
}}); |
341 |
|
|
|
342 |
|
|
t.start(); |
343 |
dl |
1.42 |
delay(SHORT_DELAY_MS); |
344 |
jsr166 |
1.22 |
assertEquals(q.remainingCapacity(), 0); |
345 |
|
|
assertEquals(0, q.take()); |
346 |
dl |
1.42 |
delay(SHORT_DELAY_MS); |
347 |
jsr166 |
1.17 |
t.interrupt(); |
348 |
|
|
t.join(); |
349 |
jsr166 |
1.22 |
assertEquals(q.remainingCapacity(), 0); |
350 |
dl |
1.1 |
} |
351 |
|
|
|
352 |
dl |
1.4 |
/** |
353 |
dl |
1.5 |
* timed offer times out if full and elements not taken |
354 |
dl |
1.4 |
*/ |
355 |
jsr166 |
1.43 |
public void testTimedOffer() { |
356 |
dl |
1.1 |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
357 |
jsr166 |
1.43 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
358 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
359 |
jsr166 |
1.17 |
public void realRun() throws InterruptedException { |
360 |
|
|
q.put(new Object()); |
361 |
|
|
q.put(new Object()); |
362 |
jsr166 |
1.43 |
long startTime = System.nanoTime(); |
363 |
|
|
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
364 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
365 |
|
|
pleaseInterrupt.countDown(); |
366 |
jsr166 |
1.21 |
try { |
367 |
jsr166 |
1.43 |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
368 |
jsr166 |
1.21 |
shouldThrow(); |
369 |
|
|
} catch (InterruptedException success) {} |
370 |
|
|
}}); |
371 |
jsr166 |
1.12 |
|
372 |
jsr166 |
1.43 |
await(pleaseInterrupt); |
373 |
jsr166 |
1.17 |
t.interrupt(); |
374 |
jsr166 |
1.43 |
awaitTermination(t); |
375 |
dl |
1.1 |
} |
376 |
|
|
|
377 |
dl |
1.4 |
/** |
378 |
dl |
1.5 |
* take retrieves elements in FIFO order |
379 |
dl |
1.4 |
*/ |
380 |
jsr166 |
1.17 |
public void testTake() throws InterruptedException { |
381 |
|
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
382 |
|
|
for (int i = 0; i < SIZE; ++i) { |
383 |
jsr166 |
1.23 |
assertEquals(i, q.take()); |
384 |
jsr166 |
1.15 |
} |
385 |
dl |
1.1 |
} |
386 |
|
|
|
387 |
dl |
1.4 |
/** |
388 |
dl |
1.5 |
* Take removes existing elements until empty, then blocks interruptibly |
389 |
dl |
1.4 |
*/ |
390 |
jsr166 |
1.17 |
public void testBlockingTake() throws InterruptedException { |
391 |
jsr166 |
1.22 |
final LinkedBlockingQueue q = populatedQueue(SIZE); |
392 |
|
|
Thread t = new Thread(new CheckedRunnable() { |
393 |
jsr166 |
1.17 |
public void realRun() throws InterruptedException { |
394 |
|
|
for (int i = 0; i < SIZE; ++i) { |
395 |
jsr166 |
1.23 |
assertEquals(i, q.take()); |
396 |
jsr166 |
1.17 |
} |
397 |
jsr166 |
1.22 |
try { |
398 |
|
|
q.take(); |
399 |
|
|
shouldThrow(); |
400 |
|
|
} catch (InterruptedException success) {} |
401 |
|
|
}}); |
402 |
jsr166 |
1.17 |
|
403 |
dl |
1.1 |
t.start(); |
404 |
dl |
1.42 |
delay(SHORT_DELAY_MS); |
405 |
jsr166 |
1.17 |
t.interrupt(); |
406 |
|
|
t.join(); |
407 |
dl |
1.1 |
} |
408 |
|
|
|
409 |
dl |
1.4 |
/** |
410 |
dl |
1.5 |
* poll succeeds unless empty |
411 |
dl |
1.4 |
*/ |
412 |
|
|
public void testPoll() { |
413 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
414 |
|
|
for (int i = 0; i < SIZE; ++i) { |
415 |
jsr166 |
1.23 |
assertEquals(i, q.poll()); |
416 |
dl |
1.1 |
} |
417 |
jsr166 |
1.15 |
assertNull(q.poll()); |
418 |
dl |
1.1 |
} |
419 |
|
|
|
420 |
dl |
1.4 |
/** |
421 |
jsr166 |
1.33 |
* timed poll with zero timeout succeeds when non-empty, else times out |
422 |
dl |
1.4 |
*/ |
423 |
jsr166 |
1.17 |
public void testTimedPoll0() throws InterruptedException { |
424 |
|
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
425 |
|
|
for (int i = 0; i < SIZE; ++i) { |
426 |
jsr166 |
1.23 |
assertEquals(i, q.poll(0, MILLISECONDS)); |
427 |
jsr166 |
1.15 |
} |
428 |
jsr166 |
1.17 |
assertNull(q.poll(0, MILLISECONDS)); |
429 |
dl |
1.1 |
} |
430 |
|
|
|
431 |
dl |
1.4 |
/** |
432 |
jsr166 |
1.33 |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
433 |
dl |
1.4 |
*/ |
434 |
jsr166 |
1.17 |
public void testTimedPoll() throws InterruptedException { |
435 |
|
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
436 |
|
|
for (int i = 0; i < SIZE; ++i) { |
437 |
jsr166 |
1.23 |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
438 |
jsr166 |
1.15 |
} |
439 |
jsr166 |
1.17 |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
440 |
dl |
1.1 |
} |
441 |
|
|
|
442 |
dl |
1.4 |
/** |
443 |
dl |
1.5 |
* Interrupted timed poll throws InterruptedException instead of |
444 |
|
|
* returning timeout status |
445 |
dl |
1.4 |
*/ |
446 |
jsr166 |
1.17 |
public void testInterruptedTimedPoll() throws InterruptedException { |
447 |
jsr166 |
1.39 |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
448 |
|
|
final CountDownLatch aboutToWait = new CountDownLatch(1); |
449 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
450 |
jsr166 |
1.17 |
public void realRun() throws InterruptedException { |
451 |
|
|
for (int i = 0; i < SIZE; ++i) { |
452 |
jsr166 |
1.39 |
long t0 = System.nanoTime(); |
453 |
|
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
454 |
|
|
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
455 |
jsr166 |
1.17 |
} |
456 |
jsr166 |
1.39 |
long t0 = System.nanoTime(); |
457 |
|
|
aboutToWait.countDown(); |
458 |
jsr166 |
1.19 |
try { |
459 |
jsr166 |
1.39 |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
460 |
jsr166 |
1.19 |
shouldThrow(); |
461 |
jsr166 |
1.39 |
} catch (InterruptedException success) { |
462 |
|
|
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
463 |
|
|
} |
464 |
jsr166 |
1.19 |
}}); |
465 |
jsr166 |
1.17 |
|
466 |
jsr166 |
1.39 |
aboutToWait.await(); |
467 |
|
|
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
468 |
jsr166 |
1.17 |
t.interrupt(); |
469 |
jsr166 |
1.39 |
awaitTermination(t, MEDIUM_DELAY_MS); |
470 |
|
|
checkEmpty(q); |
471 |
dl |
1.1 |
} |
472 |
|
|
|
473 |
dl |
1.4 |
/** |
474 |
dl |
1.5 |
* peek returns next element, or null if empty |
475 |
dl |
1.4 |
*/ |
476 |
|
|
public void testPeek() { |
477 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
478 |
|
|
for (int i = 0; i < SIZE; ++i) { |
479 |
jsr166 |
1.23 |
assertEquals(i, q.peek()); |
480 |
|
|
assertEquals(i, q.poll()); |
481 |
dl |
1.1 |
assertTrue(q.peek() == null || |
482 |
jsr166 |
1.23 |
!q.peek().equals(i)); |
483 |
dl |
1.1 |
} |
484 |
jsr166 |
1.15 |
assertNull(q.peek()); |
485 |
dl |
1.1 |
} |
486 |
|
|
|
487 |
dl |
1.4 |
/** |
488 |
dl |
1.5 |
* element returns next element, or throws NSEE if empty |
489 |
dl |
1.4 |
*/ |
490 |
|
|
public void testElement() { |
491 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
492 |
|
|
for (int i = 0; i < SIZE; ++i) { |
493 |
jsr166 |
1.23 |
assertEquals(i, q.element()); |
494 |
|
|
assertEquals(i, q.poll()); |
495 |
dl |
1.1 |
} |
496 |
|
|
try { |
497 |
|
|
q.element(); |
498 |
dl |
1.4 |
shouldThrow(); |
499 |
jsr166 |
1.17 |
} catch (NoSuchElementException success) {} |
500 |
dl |
1.1 |
} |
501 |
|
|
|
502 |
dl |
1.4 |
/** |
503 |
dl |
1.5 |
* remove removes next element, or throws NSEE if empty |
504 |
dl |
1.4 |
*/ |
505 |
|
|
public void testRemove() { |
506 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
507 |
|
|
for (int i = 0; i < SIZE; ++i) { |
508 |
jsr166 |
1.23 |
assertEquals(i, q.remove()); |
509 |
dl |
1.1 |
} |
510 |
|
|
try { |
511 |
|
|
q.remove(); |
512 |
dl |
1.4 |
shouldThrow(); |
513 |
jsr166 |
1.17 |
} catch (NoSuchElementException success) {} |
514 |
dl |
1.1 |
} |
515 |
|
|
|
516 |
dl |
1.4 |
/** |
517 |
dl |
1.5 |
* remove(x) removes x and returns true if present |
518 |
dl |
1.4 |
*/ |
519 |
|
|
public void testRemoveElement() { |
520 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
521 |
|
|
for (int i = 1; i < SIZE; i+=2) { |
522 |
jsr166 |
1.38 |
assertTrue(q.contains(i)); |
523 |
|
|
assertTrue(q.remove(i)); |
524 |
|
|
assertFalse(q.contains(i)); |
525 |
|
|
assertTrue(q.contains(i-1)); |
526 |
dl |
1.1 |
} |
527 |
dl |
1.3 |
for (int i = 0; i < SIZE; i+=2) { |
528 |
jsr166 |
1.38 |
assertTrue(q.contains(i)); |
529 |
|
|
assertTrue(q.remove(i)); |
530 |
|
|
assertFalse(q.contains(i)); |
531 |
|
|
assertFalse(q.remove(i+1)); |
532 |
|
|
assertFalse(q.contains(i+1)); |
533 |
dl |
1.1 |
} |
534 |
dl |
1.2 |
assertTrue(q.isEmpty()); |
535 |
dl |
1.1 |
} |
536 |
dl |
1.11 |
|
537 |
|
|
/** |
538 |
|
|
* An add following remove(x) succeeds |
539 |
|
|
*/ |
540 |
jsr166 |
1.17 |
public void testRemoveElementAndAdd() throws InterruptedException { |
541 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
542 |
|
|
assertTrue(q.add(new Integer(1))); |
543 |
|
|
assertTrue(q.add(new Integer(2))); |
544 |
|
|
assertTrue(q.remove(new Integer(1))); |
545 |
|
|
assertTrue(q.remove(new Integer(2))); |
546 |
|
|
assertTrue(q.add(new Integer(3))); |
547 |
|
|
assertTrue(q.take() != null); |
548 |
dl |
1.11 |
} |
549 |
jsr166 |
1.12 |
|
550 |
dl |
1.4 |
/** |
551 |
dl |
1.5 |
* contains(x) reports true when elements added but not yet removed |
552 |
dl |
1.4 |
*/ |
553 |
|
|
public void testContains() { |
554 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
555 |
|
|
for (int i = 0; i < SIZE; ++i) { |
556 |
dl |
1.1 |
assertTrue(q.contains(new Integer(i))); |
557 |
|
|
q.poll(); |
558 |
|
|
assertFalse(q.contains(new Integer(i))); |
559 |
|
|
} |
560 |
|
|
} |
561 |
|
|
|
562 |
dl |
1.4 |
/** |
563 |
dl |
1.5 |
* clear removes all elements |
564 |
dl |
1.4 |
*/ |
565 |
|
|
public void testClear() { |
566 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
567 |
dl |
1.1 |
q.clear(); |
568 |
|
|
assertTrue(q.isEmpty()); |
569 |
|
|
assertEquals(0, q.size()); |
570 |
dl |
1.3 |
assertEquals(SIZE, q.remainingCapacity()); |
571 |
|
|
q.add(one); |
572 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
573 |
dl |
1.10 |
assertTrue(q.contains(one)); |
574 |
dl |
1.1 |
q.clear(); |
575 |
|
|
assertTrue(q.isEmpty()); |
576 |
|
|
} |
577 |
|
|
|
578 |
dl |
1.4 |
/** |
579 |
dl |
1.5 |
* containsAll(c) is true when c contains a subset of elements |
580 |
dl |
1.4 |
*/ |
581 |
|
|
public void testContainsAll() { |
582 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
583 |
|
|
LinkedBlockingQueue p = new LinkedBlockingQueue(SIZE); |
584 |
|
|
for (int i = 0; i < SIZE; ++i) { |
585 |
dl |
1.1 |
assertTrue(q.containsAll(p)); |
586 |
|
|
assertFalse(p.containsAll(q)); |
587 |
|
|
p.add(new Integer(i)); |
588 |
|
|
} |
589 |
|
|
assertTrue(p.containsAll(q)); |
590 |
|
|
} |
591 |
|
|
|
592 |
dl |
1.4 |
/** |
593 |
dl |
1.5 |
* retainAll(c) retains only those elements of c and reports true if changed |
594 |
dl |
1.4 |
*/ |
595 |
|
|
public void testRetainAll() { |
596 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
597 |
|
|
LinkedBlockingQueue p = populatedQueue(SIZE); |
598 |
|
|
for (int i = 0; i < SIZE; ++i) { |
599 |
dl |
1.1 |
boolean changed = q.retainAll(p); |
600 |
|
|
if (i == 0) |
601 |
|
|
assertFalse(changed); |
602 |
|
|
else |
603 |
|
|
assertTrue(changed); |
604 |
|
|
|
605 |
|
|
assertTrue(q.containsAll(p)); |
606 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
607 |
dl |
1.1 |
p.remove(); |
608 |
|
|
} |
609 |
|
|
} |
610 |
|
|
|
611 |
dl |
1.4 |
/** |
612 |
dl |
1.5 |
* removeAll(c) removes only those elements of c and reports true if changed |
613 |
dl |
1.4 |
*/ |
614 |
|
|
public void testRemoveAll() { |
615 |
dl |
1.3 |
for (int i = 1; i < SIZE; ++i) { |
616 |
|
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
617 |
|
|
LinkedBlockingQueue p = populatedQueue(i); |
618 |
dl |
1.1 |
assertTrue(q.removeAll(p)); |
619 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
620 |
dl |
1.1 |
for (int j = 0; j < i; ++j) { |
621 |
|
|
Integer I = (Integer)(p.remove()); |
622 |
|
|
assertFalse(q.contains(I)); |
623 |
|
|
} |
624 |
|
|
} |
625 |
|
|
} |
626 |
|
|
|
627 |
dl |
1.4 |
/** |
628 |
jsr166 |
1.36 |
* toArray contains all elements in FIFO order |
629 |
dl |
1.4 |
*/ |
630 |
jsr166 |
1.36 |
public void testToArray() { |
631 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
632 |
jsr166 |
1.15 |
Object[] o = q.toArray(); |
633 |
|
|
for (int i = 0; i < o.length; i++) |
634 |
jsr166 |
1.36 |
assertSame(o[i], q.poll()); |
635 |
dl |
1.1 |
} |
636 |
|
|
|
637 |
dl |
1.4 |
/** |
638 |
jsr166 |
1.36 |
* toArray(a) contains all elements in FIFO order |
639 |
dl |
1.4 |
*/ |
640 |
jsr166 |
1.17 |
public void testToArray2() throws InterruptedException { |
641 |
jsr166 |
1.37 |
LinkedBlockingQueue<Integer> q = populatedQueue(SIZE); |
642 |
jsr166 |
1.15 |
Integer[] ints = new Integer[SIZE]; |
643 |
jsr166 |
1.37 |
Integer[] array = q.toArray(ints); |
644 |
|
|
assertSame(ints, array); |
645 |
jsr166 |
1.17 |
for (int i = 0; i < ints.length; i++) |
646 |
jsr166 |
1.36 |
assertSame(ints[i], q.poll()); |
647 |
dl |
1.1 |
} |
648 |
dl |
1.6 |
|
649 |
|
|
/** |
650 |
jsr166 |
1.35 |
* toArray(null) throws NullPointerException |
651 |
dl |
1.6 |
*/ |
652 |
jsr166 |
1.35 |
public void testToArray_NullArg() { |
653 |
jsr166 |
1.23 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
654 |
jsr166 |
1.15 |
try { |
655 |
jsr166 |
1.35 |
q.toArray(null); |
656 |
jsr166 |
1.15 |
shouldThrow(); |
657 |
|
|
} catch (NullPointerException success) {} |
658 |
dl |
1.6 |
} |
659 |
|
|
|
660 |
|
|
/** |
661 |
jsr166 |
1.34 |
* toArray(incompatible array type) throws ArrayStoreException |
662 |
dl |
1.6 |
*/ |
663 |
|
|
public void testToArray1_BadArg() { |
664 |
jsr166 |
1.23 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
665 |
jsr166 |
1.15 |
try { |
666 |
jsr166 |
1.34 |
q.toArray(new String[10]); |
667 |
jsr166 |
1.15 |
shouldThrow(); |
668 |
jsr166 |
1.17 |
} catch (ArrayStoreException success) {} |
669 |
dl |
1.6 |
} |
670 |
|
|
|
671 |
jsr166 |
1.12 |
|
672 |
dl |
1.4 |
/** |
673 |
dl |
1.5 |
* iterator iterates through all elements |
674 |
dl |
1.4 |
*/ |
675 |
jsr166 |
1.17 |
public void testIterator() throws InterruptedException { |
676 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
677 |
jsr166 |
1.15 |
Iterator it = q.iterator(); |
678 |
jsr166 |
1.17 |
while (it.hasNext()) { |
679 |
|
|
assertEquals(it.next(), q.take()); |
680 |
jsr166 |
1.15 |
} |
681 |
dl |
1.1 |
} |
682 |
|
|
|
683 |
dl |
1.4 |
/** |
684 |
dl |
1.5 |
* iterator.remove removes current element |
685 |
|
|
*/ |
686 |
jsr166 |
1.26 |
public void testIteratorRemove() { |
687 |
dl |
1.5 |
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
688 |
|
|
q.add(two); |
689 |
|
|
q.add(one); |
690 |
|
|
q.add(three); |
691 |
|
|
|
692 |
|
|
Iterator it = q.iterator(); |
693 |
|
|
it.next(); |
694 |
|
|
it.remove(); |
695 |
jsr166 |
1.12 |
|
696 |
dl |
1.5 |
it = q.iterator(); |
697 |
jsr166 |
1.25 |
assertSame(it.next(), one); |
698 |
|
|
assertSame(it.next(), three); |
699 |
dl |
1.5 |
assertFalse(it.hasNext()); |
700 |
|
|
} |
701 |
|
|
|
702 |
|
|
|
703 |
|
|
/** |
704 |
|
|
* iterator ordering is FIFO |
705 |
dl |
1.4 |
*/ |
706 |
dl |
1.1 |
public void testIteratorOrdering() { |
707 |
|
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
708 |
dl |
1.3 |
q.add(one); |
709 |
|
|
q.add(two); |
710 |
|
|
q.add(three); |
711 |
dl |
1.4 |
assertEquals(0, q.remainingCapacity()); |
712 |
dl |
1.1 |
int k = 0; |
713 |
|
|
for (Iterator it = q.iterator(); it.hasNext();) { |
714 |
jsr166 |
1.23 |
assertEquals(++k, it.next()); |
715 |
dl |
1.1 |
} |
716 |
dl |
1.4 |
assertEquals(3, k); |
717 |
dl |
1.1 |
} |
718 |
|
|
|
719 |
dl |
1.4 |
/** |
720 |
dl |
1.5 |
* Modifications do not cause iterators to fail |
721 |
dl |
1.4 |
*/ |
722 |
jsr166 |
1.26 |
public void testWeaklyConsistentIteration() { |
723 |
dl |
1.1 |
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
724 |
dl |
1.3 |
q.add(one); |
725 |
|
|
q.add(two); |
726 |
|
|
q.add(three); |
727 |
jsr166 |
1.17 |
for (Iterator it = q.iterator(); it.hasNext();) { |
728 |
|
|
q.remove(); |
729 |
|
|
it.next(); |
730 |
dl |
1.1 |
} |
731 |
dl |
1.4 |
assertEquals(0, q.size()); |
732 |
dl |
1.1 |
} |
733 |
|
|
|
734 |
|
|
|
735 |
dl |
1.4 |
/** |
736 |
dl |
1.5 |
* toString contains toStrings of elements |
737 |
dl |
1.4 |
*/ |
738 |
|
|
public void testToString() { |
739 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
740 |
dl |
1.1 |
String s = q.toString(); |
741 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
742 |
dl |
1.1 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
743 |
|
|
} |
744 |
jsr166 |
1.12 |
} |
745 |
dl |
1.1 |
|
746 |
|
|
|
747 |
dl |
1.4 |
/** |
748 |
dl |
1.5 |
* offer transfers elements across Executor tasks |
749 |
dl |
1.4 |
*/ |
750 |
dl |
1.1 |
public void testOfferInExecutor() { |
751 |
|
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
752 |
dl |
1.3 |
q.add(one); |
753 |
|
|
q.add(two); |
754 |
dl |
1.1 |
ExecutorService executor = Executors.newFixedThreadPool(2); |
755 |
jsr166 |
1.17 |
executor.execute(new CheckedRunnable() { |
756 |
|
|
public void realRun() throws InterruptedException { |
757 |
jsr166 |
1.24 |
assertFalse(q.offer(three)); |
758 |
|
|
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
759 |
|
|
assertEquals(0, q.remainingCapacity()); |
760 |
jsr166 |
1.17 |
}}); |
761 |
|
|
|
762 |
|
|
executor.execute(new CheckedRunnable() { |
763 |
|
|
public void realRun() throws InterruptedException { |
764 |
dl |
1.42 |
delay(SMALL_DELAY_MS); |
765 |
jsr166 |
1.24 |
assertSame(one, q.take()); |
766 |
jsr166 |
1.17 |
}}); |
767 |
jsr166 |
1.12 |
|
768 |
dl |
1.3 |
joinPool(executor); |
769 |
dl |
1.1 |
} |
770 |
|
|
|
771 |
dl |
1.4 |
/** |
772 |
dl |
1.5 |
* poll retrieves elements across Executor threads |
773 |
dl |
1.4 |
*/ |
774 |
dl |
1.1 |
public void testPollInExecutor() { |
775 |
|
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
776 |
|
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
777 |
jsr166 |
1.17 |
executor.execute(new CheckedRunnable() { |
778 |
|
|
public void realRun() throws InterruptedException { |
779 |
jsr166 |
1.24 |
assertNull(q.poll()); |
780 |
|
|
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
781 |
|
|
assertTrue(q.isEmpty()); |
782 |
jsr166 |
1.17 |
}}); |
783 |
|
|
|
784 |
|
|
executor.execute(new CheckedRunnable() { |
785 |
|
|
public void realRun() throws InterruptedException { |
786 |
dl |
1.42 |
delay(SMALL_DELAY_MS); |
787 |
jsr166 |
1.17 |
q.put(one); |
788 |
|
|
}}); |
789 |
jsr166 |
1.12 |
|
790 |
dl |
1.3 |
joinPool(executor); |
791 |
dl |
1.2 |
} |
792 |
|
|
|
793 |
dl |
1.4 |
/** |
794 |
dl |
1.5 |
* A deserialized serialized queue has same elements in same order |
795 |
dl |
1.4 |
*/ |
796 |
jsr166 |
1.17 |
public void testSerialization() throws Exception { |
797 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
798 |
dl |
1.2 |
|
799 |
jsr166 |
1.17 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
800 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
801 |
|
|
out.writeObject(q); |
802 |
|
|
out.close(); |
803 |
|
|
|
804 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
805 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
806 |
|
|
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
807 |
|
|
assertEquals(q.size(), r.size()); |
808 |
|
|
while (!q.isEmpty()) |
809 |
|
|
assertEquals(q.remove(), r.remove()); |
810 |
dl |
1.6 |
} |
811 |
|
|
|
812 |
|
|
/** |
813 |
|
|
* drainTo(null) throws NPE |
814 |
jsr166 |
1.12 |
*/ |
815 |
dl |
1.6 |
public void testDrainToNull() { |
816 |
|
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
817 |
|
|
try { |
818 |
|
|
q.drainTo(null); |
819 |
|
|
shouldThrow(); |
820 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
821 |
dl |
1.6 |
} |
822 |
|
|
|
823 |
|
|
/** |
824 |
|
|
* drainTo(this) throws IAE |
825 |
jsr166 |
1.12 |
*/ |
826 |
dl |
1.6 |
public void testDrainToSelf() { |
827 |
|
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
828 |
|
|
try { |
829 |
|
|
q.drainTo(q); |
830 |
|
|
shouldThrow(); |
831 |
jsr166 |
1.17 |
} catch (IllegalArgumentException success) {} |
832 |
dl |
1.6 |
} |
833 |
|
|
|
834 |
|
|
/** |
835 |
|
|
* drainTo(c) empties queue into another collection c |
836 |
jsr166 |
1.12 |
*/ |
837 |
dl |
1.6 |
public void testDrainTo() { |
838 |
|
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
839 |
|
|
ArrayList l = new ArrayList(); |
840 |
|
|
q.drainTo(l); |
841 |
|
|
assertEquals(q.size(), 0); |
842 |
|
|
assertEquals(l.size(), SIZE); |
843 |
jsr166 |
1.12 |
for (int i = 0; i < SIZE; ++i) |
844 |
dl |
1.6 |
assertEquals(l.get(i), new Integer(i)); |
845 |
dl |
1.10 |
q.add(zero); |
846 |
|
|
q.add(one); |
847 |
|
|
assertFalse(q.isEmpty()); |
848 |
|
|
assertTrue(q.contains(zero)); |
849 |
|
|
assertTrue(q.contains(one)); |
850 |
|
|
l.clear(); |
851 |
|
|
q.drainTo(l); |
852 |
|
|
assertEquals(q.size(), 0); |
853 |
|
|
assertEquals(l.size(), 2); |
854 |
jsr166 |
1.12 |
for (int i = 0; i < 2; ++i) |
855 |
dl |
1.10 |
assertEquals(l.get(i), new Integer(i)); |
856 |
dl |
1.6 |
} |
857 |
|
|
|
858 |
|
|
/** |
859 |
|
|
* drainTo empties full queue, unblocking a waiting put. |
860 |
jsr166 |
1.12 |
*/ |
861 |
jsr166 |
1.17 |
public void testDrainToWithActivePut() throws InterruptedException { |
862 |
dl |
1.6 |
final LinkedBlockingQueue q = populatedQueue(SIZE); |
863 |
jsr166 |
1.17 |
Thread t = new Thread(new CheckedRunnable() { |
864 |
|
|
public void realRun() throws InterruptedException { |
865 |
|
|
q.put(new Integer(SIZE+1)); |
866 |
|
|
}}); |
867 |
|
|
|
868 |
|
|
t.start(); |
869 |
|
|
ArrayList l = new ArrayList(); |
870 |
|
|
q.drainTo(l); |
871 |
|
|
assertTrue(l.size() >= SIZE); |
872 |
|
|
for (int i = 0; i < SIZE; ++i) |
873 |
|
|
assertEquals(l.get(i), new Integer(i)); |
874 |
|
|
t.join(); |
875 |
|
|
assertTrue(q.size() + l.size() >= SIZE); |
876 |
dl |
1.6 |
} |
877 |
|
|
|
878 |
|
|
/** |
879 |
|
|
* drainTo(null, n) throws NPE |
880 |
jsr166 |
1.12 |
*/ |
881 |
dl |
1.6 |
public void testDrainToNullN() { |
882 |
|
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
883 |
|
|
try { |
884 |
|
|
q.drainTo(null, 0); |
885 |
|
|
shouldThrow(); |
886 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
887 |
dl |
1.6 |
} |
888 |
|
|
|
889 |
|
|
/** |
890 |
|
|
* drainTo(this, n) throws IAE |
891 |
jsr166 |
1.12 |
*/ |
892 |
dl |
1.6 |
public void testDrainToSelfN() { |
893 |
|
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
894 |
|
|
try { |
895 |
|
|
q.drainTo(q, 0); |
896 |
|
|
shouldThrow(); |
897 |
jsr166 |
1.17 |
} catch (IllegalArgumentException success) {} |
898 |
dl |
1.6 |
} |
899 |
|
|
|
900 |
|
|
/** |
901 |
jsr166 |
1.30 |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
902 |
jsr166 |
1.12 |
*/ |
903 |
dl |
1.6 |
public void testDrainToN() { |
904 |
dl |
1.10 |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
905 |
dl |
1.6 |
for (int i = 0; i < SIZE + 2; ++i) { |
906 |
jsr166 |
1.13 |
for (int j = 0; j < SIZE; j++) |
907 |
dl |
1.10 |
assertTrue(q.offer(new Integer(j))); |
908 |
dl |
1.6 |
ArrayList l = new ArrayList(); |
909 |
|
|
q.drainTo(l, i); |
910 |
jsr166 |
1.31 |
int k = (i < SIZE) ? i : SIZE; |
911 |
dl |
1.10 |
assertEquals(l.size(), k); |
912 |
dl |
1.6 |
assertEquals(q.size(), SIZE-k); |
913 |
jsr166 |
1.12 |
for (int j = 0; j < k; ++j) |
914 |
dl |
1.6 |
assertEquals(l.get(j), new Integer(j)); |
915 |
dl |
1.10 |
while (q.poll() != null) ; |
916 |
dl |
1.2 |
} |
917 |
dl |
1.1 |
} |
918 |
|
|
|
919 |
|
|
} |