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