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