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