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