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