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