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 |
jsr166 |
1.27 |
|
218 |
dl |
1.4 |
/** |
219 |
dl |
1.5 |
* addAll of a collection with any null elements throws NPE after |
220 |
|
|
* possibly adding some elements |
221 |
dl |
1.4 |
*/ |
222 |
|
|
public void testAddAll3() { |
223 |
dl |
1.1 |
try { |
224 |
dl |
1.3 |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
225 |
|
|
Integer[] ints = new Integer[SIZE]; |
226 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
227 |
dl |
1.1 |
ints[i] = new Integer(i); |
228 |
|
|
q.addAll(Arrays.asList(ints)); |
229 |
dl |
1.4 |
shouldThrow(); |
230 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
231 |
dl |
1.1 |
} |
232 |
jsr166 |
1.27 |
|
233 |
dl |
1.4 |
/** |
234 |
dl |
1.5 |
* addAll throws ISE if not enough room |
235 |
dl |
1.4 |
*/ |
236 |
|
|
public void testAddAll4() { |
237 |
dl |
1.1 |
try { |
238 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
239 |
dl |
1.3 |
Integer[] ints = new Integer[SIZE]; |
240 |
|
|
for (int i = 0; i < SIZE; ++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 (IllegalStateException success) {} |
245 |
dl |
1.1 |
} |
246 |
jsr166 |
1.27 |
|
247 |
dl |
1.4 |
/** |
248 |
dl |
1.5 |
* Queue contains all elements, in traversal order, of successful addAll |
249 |
dl |
1.4 |
*/ |
250 |
|
|
public void testAddAll5() { |
251 |
jsr166 |
1.17 |
Integer[] empty = new Integer[0]; |
252 |
|
|
Integer[] ints = new Integer[SIZE]; |
253 |
|
|
for (int i = 0; i < SIZE; ++i) |
254 |
|
|
ints[i] = new Integer(i); |
255 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
256 |
|
|
assertFalse(q.addAll(Arrays.asList(empty))); |
257 |
|
|
assertTrue(q.addAll(Arrays.asList(ints))); |
258 |
|
|
for (int i = 0; i < SIZE; ++i) |
259 |
|
|
assertEquals(ints[i], q.poll()); |
260 |
dl |
1.1 |
} |
261 |
|
|
|
262 |
dl |
1.4 |
/** |
263 |
dl |
1.5 |
* put(null) throws NPE |
264 |
dl |
1.4 |
*/ |
265 |
jsr166 |
1.17 |
public void testPutNull() throws InterruptedException { |
266 |
jsr166 |
1.15 |
try { |
267 |
dl |
1.3 |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
268 |
dl |
1.1 |
q.put(null); |
269 |
dl |
1.4 |
shouldThrow(); |
270 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
271 |
dl |
1.1 |
} |
272 |
|
|
|
273 |
dl |
1.4 |
/** |
274 |
dl |
1.5 |
* all elements successfully put are contained |
275 |
dl |
1.4 |
*/ |
276 |
jsr166 |
1.17 |
public void testPut() throws InterruptedException { |
277 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
278 |
|
|
for (int i = 0; i < SIZE; ++i) { |
279 |
|
|
Integer I = new Integer(i); |
280 |
|
|
q.put(I); |
281 |
|
|
assertTrue(q.contains(I)); |
282 |
dl |
1.1 |
} |
283 |
jsr166 |
1.17 |
assertEquals(0, q.remainingCapacity()); |
284 |
dl |
1.1 |
} |
285 |
|
|
|
286 |
dl |
1.4 |
/** |
287 |
dl |
1.5 |
* put blocks interruptibly if full |
288 |
dl |
1.4 |
*/ |
289 |
jsr166 |
1.17 |
public void testBlockingPut() throws InterruptedException { |
290 |
jsr166 |
1.22 |
final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
291 |
jsr166 |
1.17 |
Thread t = new Thread(new CheckedRunnable() { |
292 |
jsr166 |
1.22 |
public void realRun() throws InterruptedException { |
293 |
|
|
for (int i = 0; i < SIZE; ++i) |
294 |
|
|
q.put(i); |
295 |
|
|
assertEquals(SIZE, q.size()); |
296 |
|
|
assertEquals(0, q.remainingCapacity()); |
297 |
jsr166 |
1.17 |
try { |
298 |
jsr166 |
1.22 |
q.put(99); |
299 |
|
|
shouldThrow(); |
300 |
|
|
} catch (InterruptedException success) {} |
301 |
jsr166 |
1.17 |
}}); |
302 |
|
|
|
303 |
dl |
1.1 |
t.start(); |
304 |
jsr166 |
1.17 |
Thread.sleep(SHORT_DELAY_MS); |
305 |
|
|
t.interrupt(); |
306 |
|
|
t.join(); |
307 |
jsr166 |
1.22 |
assertEquals(SIZE, q.size()); |
308 |
|
|
assertEquals(0, q.remainingCapacity()); |
309 |
dl |
1.1 |
} |
310 |
|
|
|
311 |
dl |
1.4 |
/** |
312 |
dl |
1.5 |
* put blocks waiting for take when full |
313 |
dl |
1.4 |
*/ |
314 |
jsr166 |
1.17 |
public void testPutWithTake() throws InterruptedException { |
315 |
jsr166 |
1.22 |
final int capacity = 2; |
316 |
dl |
1.1 |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
317 |
jsr166 |
1.17 |
Thread t = new Thread(new CheckedRunnable() { |
318 |
jsr166 |
1.22 |
public void realRun() throws InterruptedException { |
319 |
|
|
for (int i = 0; i < capacity + 1; i++) |
320 |
|
|
q.put(i); |
321 |
jsr166 |
1.17 |
try { |
322 |
jsr166 |
1.22 |
q.put(99); |
323 |
|
|
shouldThrow(); |
324 |
|
|
} catch (InterruptedException success) {} |
325 |
jsr166 |
1.17 |
}}); |
326 |
|
|
|
327 |
|
|
t.start(); |
328 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
329 |
jsr166 |
1.22 |
assertEquals(q.remainingCapacity(), 0); |
330 |
|
|
assertEquals(0, q.take()); |
331 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
332 |
jsr166 |
1.17 |
t.interrupt(); |
333 |
|
|
t.join(); |
334 |
jsr166 |
1.22 |
assertEquals(q.remainingCapacity(), 0); |
335 |
dl |
1.1 |
} |
336 |
|
|
|
337 |
dl |
1.4 |
/** |
338 |
dl |
1.5 |
* timed offer times out if full and elements not taken |
339 |
dl |
1.4 |
*/ |
340 |
jsr166 |
1.17 |
public void testTimedOffer() throws InterruptedException { |
341 |
dl |
1.1 |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
342 |
jsr166 |
1.21 |
Thread t = new Thread(new CheckedRunnable() { |
343 |
jsr166 |
1.17 |
public void realRun() throws InterruptedException { |
344 |
|
|
q.put(new Object()); |
345 |
|
|
q.put(new Object()); |
346 |
jsr166 |
1.21 |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
347 |
|
|
try { |
348 |
|
|
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
349 |
|
|
shouldThrow(); |
350 |
|
|
} catch (InterruptedException success) {} |
351 |
|
|
}}); |
352 |
jsr166 |
1.12 |
|
353 |
jsr166 |
1.17 |
t.start(); |
354 |
|
|
Thread.sleep(SMALL_DELAY_MS); |
355 |
|
|
t.interrupt(); |
356 |
|
|
t.join(); |
357 |
dl |
1.1 |
} |
358 |
|
|
|
359 |
dl |
1.4 |
/** |
360 |
dl |
1.5 |
* take retrieves elements in FIFO order |
361 |
dl |
1.4 |
*/ |
362 |
jsr166 |
1.17 |
public void testTake() throws InterruptedException { |
363 |
|
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
364 |
|
|
for (int i = 0; i < SIZE; ++i) { |
365 |
jsr166 |
1.23 |
assertEquals(i, q.take()); |
366 |
jsr166 |
1.15 |
} |
367 |
dl |
1.1 |
} |
368 |
|
|
|
369 |
dl |
1.4 |
/** |
370 |
dl |
1.5 |
* take blocks interruptibly when empty |
371 |
dl |
1.4 |
*/ |
372 |
jsr166 |
1.17 |
public void testTakeFromEmpty() throws InterruptedException { |
373 |
dl |
1.1 |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
374 |
jsr166 |
1.17 |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
375 |
|
|
public void realRun() throws InterruptedException { |
376 |
|
|
q.take(); |
377 |
|
|
}}; |
378 |
|
|
|
379 |
|
|
t.start(); |
380 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
381 |
|
|
t.interrupt(); |
382 |
|
|
t.join(); |
383 |
dl |
1.1 |
} |
384 |
|
|
|
385 |
dl |
1.4 |
/** |
386 |
dl |
1.5 |
* Take removes existing elements until empty, then blocks interruptibly |
387 |
dl |
1.4 |
*/ |
388 |
jsr166 |
1.17 |
public void testBlockingTake() throws InterruptedException { |
389 |
jsr166 |
1.22 |
final LinkedBlockingQueue q = populatedQueue(SIZE); |
390 |
|
|
Thread t = new Thread(new CheckedRunnable() { |
391 |
jsr166 |
1.17 |
public void realRun() throws InterruptedException { |
392 |
|
|
for (int i = 0; i < SIZE; ++i) { |
393 |
jsr166 |
1.23 |
assertEquals(i, q.take()); |
394 |
jsr166 |
1.17 |
} |
395 |
jsr166 |
1.22 |
try { |
396 |
|
|
q.take(); |
397 |
|
|
shouldThrow(); |
398 |
|
|
} catch (InterruptedException success) {} |
399 |
|
|
}}); |
400 |
jsr166 |
1.17 |
|
401 |
dl |
1.1 |
t.start(); |
402 |
jsr166 |
1.17 |
Thread.sleep(SHORT_DELAY_MS); |
403 |
|
|
t.interrupt(); |
404 |
|
|
t.join(); |
405 |
dl |
1.1 |
} |
406 |
|
|
|
407 |
dl |
1.4 |
/** |
408 |
dl |
1.5 |
* poll succeeds unless empty |
409 |
dl |
1.4 |
*/ |
410 |
|
|
public void testPoll() { |
411 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
412 |
|
|
for (int i = 0; i < SIZE; ++i) { |
413 |
jsr166 |
1.23 |
assertEquals(i, q.poll()); |
414 |
dl |
1.1 |
} |
415 |
jsr166 |
1.15 |
assertNull(q.poll()); |
416 |
dl |
1.1 |
} |
417 |
|
|
|
418 |
dl |
1.4 |
/** |
419 |
dl |
1.5 |
* timed pool with zero timeout succeeds when non-empty, else times out |
420 |
dl |
1.4 |
*/ |
421 |
jsr166 |
1.17 |
public void testTimedPoll0() throws InterruptedException { |
422 |
|
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
423 |
|
|
for (int i = 0; i < SIZE; ++i) { |
424 |
jsr166 |
1.23 |
assertEquals(i, q.poll(0, MILLISECONDS)); |
425 |
jsr166 |
1.15 |
} |
426 |
jsr166 |
1.17 |
assertNull(q.poll(0, MILLISECONDS)); |
427 |
dl |
1.1 |
} |
428 |
|
|
|
429 |
dl |
1.4 |
/** |
430 |
dl |
1.5 |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
431 |
dl |
1.4 |
*/ |
432 |
jsr166 |
1.17 |
public void testTimedPoll() throws InterruptedException { |
433 |
|
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
434 |
|
|
for (int i = 0; i < SIZE; ++i) { |
435 |
jsr166 |
1.23 |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
436 |
jsr166 |
1.15 |
} |
437 |
jsr166 |
1.17 |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
438 |
dl |
1.1 |
} |
439 |
|
|
|
440 |
dl |
1.4 |
/** |
441 |
dl |
1.5 |
* Interrupted timed poll throws InterruptedException instead of |
442 |
|
|
* returning timeout status |
443 |
dl |
1.4 |
*/ |
444 |
jsr166 |
1.17 |
public void testInterruptedTimedPoll() throws InterruptedException { |
445 |
jsr166 |
1.19 |
Thread t = new Thread(new CheckedRunnable() { |
446 |
jsr166 |
1.17 |
public void realRun() throws InterruptedException { |
447 |
|
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
448 |
|
|
for (int i = 0; i < SIZE; ++i) { |
449 |
jsr166 |
1.23 |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
450 |
jsr166 |
1.17 |
} |
451 |
jsr166 |
1.19 |
try { |
452 |
|
|
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
453 |
|
|
shouldThrow(); |
454 |
|
|
} catch (InterruptedException success) {} |
455 |
|
|
}}); |
456 |
jsr166 |
1.17 |
|
457 |
dl |
1.1 |
t.start(); |
458 |
jsr166 |
1.17 |
Thread.sleep(SHORT_DELAY_MS); |
459 |
|
|
t.interrupt(); |
460 |
|
|
t.join(); |
461 |
dl |
1.1 |
} |
462 |
|
|
|
463 |
dl |
1.4 |
/** |
464 |
dl |
1.5 |
* timed poll before a delayed offer fails; after offer succeeds; |
465 |
|
|
* on interruption throws |
466 |
dl |
1.4 |
*/ |
467 |
jsr166 |
1.17 |
public void testTimedPollWithOffer() throws InterruptedException { |
468 |
dl |
1.1 |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
469 |
jsr166 |
1.20 |
Thread t = new Thread(new CheckedRunnable() { |
470 |
jsr166 |
1.17 |
public void realRun() throws InterruptedException { |
471 |
jsr166 |
1.20 |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
472 |
|
|
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
473 |
|
|
try { |
474 |
|
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
475 |
|
|
shouldThrow(); |
476 |
|
|
} catch (InterruptedException success) {} |
477 |
|
|
}}); |
478 |
jsr166 |
1.17 |
|
479 |
|
|
t.start(); |
480 |
|
|
Thread.sleep(SMALL_DELAY_MS); |
481 |
|
|
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
482 |
|
|
t.interrupt(); |
483 |
|
|
t.join(); |
484 |
jsr166 |
1.12 |
} |
485 |
dl |
1.1 |
|
486 |
dl |
1.4 |
/** |
487 |
dl |
1.5 |
* peek returns next element, or null if empty |
488 |
dl |
1.4 |
*/ |
489 |
|
|
public void testPeek() { |
490 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
491 |
|
|
for (int i = 0; i < SIZE; ++i) { |
492 |
jsr166 |
1.23 |
assertEquals(i, q.peek()); |
493 |
|
|
assertEquals(i, q.poll()); |
494 |
dl |
1.1 |
assertTrue(q.peek() == null || |
495 |
jsr166 |
1.23 |
!q.peek().equals(i)); |
496 |
dl |
1.1 |
} |
497 |
jsr166 |
1.15 |
assertNull(q.peek()); |
498 |
dl |
1.1 |
} |
499 |
|
|
|
500 |
dl |
1.4 |
/** |
501 |
dl |
1.5 |
* element returns next element, or throws NSEE if empty |
502 |
dl |
1.4 |
*/ |
503 |
|
|
public void testElement() { |
504 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
505 |
|
|
for (int i = 0; i < SIZE; ++i) { |
506 |
jsr166 |
1.23 |
assertEquals(i, q.element()); |
507 |
|
|
assertEquals(i, q.poll()); |
508 |
dl |
1.1 |
} |
509 |
|
|
try { |
510 |
|
|
q.element(); |
511 |
dl |
1.4 |
shouldThrow(); |
512 |
jsr166 |
1.17 |
} catch (NoSuchElementException success) {} |
513 |
dl |
1.1 |
} |
514 |
|
|
|
515 |
dl |
1.4 |
/** |
516 |
dl |
1.5 |
* remove removes next element, or throws NSEE if empty |
517 |
dl |
1.4 |
*/ |
518 |
|
|
public void testRemove() { |
519 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
520 |
|
|
for (int i = 0; i < SIZE; ++i) { |
521 |
jsr166 |
1.23 |
assertEquals(i, q.remove()); |
522 |
dl |
1.1 |
} |
523 |
|
|
try { |
524 |
|
|
q.remove(); |
525 |
dl |
1.4 |
shouldThrow(); |
526 |
jsr166 |
1.17 |
} catch (NoSuchElementException success) {} |
527 |
dl |
1.1 |
} |
528 |
|
|
|
529 |
dl |
1.4 |
/** |
530 |
dl |
1.5 |
* remove(x) removes x and returns true if present |
531 |
dl |
1.4 |
*/ |
532 |
|
|
public void testRemoveElement() { |
533 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
534 |
|
|
for (int i = 1; i < SIZE; i+=2) { |
535 |
dl |
1.1 |
assertTrue(q.remove(new Integer(i))); |
536 |
|
|
} |
537 |
dl |
1.3 |
for (int i = 0; i < SIZE; i+=2) { |
538 |
dl |
1.1 |
assertTrue(q.remove(new Integer(i))); |
539 |
|
|
assertFalse(q.remove(new Integer(i+1))); |
540 |
|
|
} |
541 |
dl |
1.2 |
assertTrue(q.isEmpty()); |
542 |
dl |
1.1 |
} |
543 |
dl |
1.11 |
|
544 |
|
|
/** |
545 |
|
|
* An add following remove(x) succeeds |
546 |
|
|
*/ |
547 |
jsr166 |
1.17 |
public void testRemoveElementAndAdd() throws InterruptedException { |
548 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
549 |
|
|
assertTrue(q.add(new Integer(1))); |
550 |
|
|
assertTrue(q.add(new Integer(2))); |
551 |
|
|
assertTrue(q.remove(new Integer(1))); |
552 |
|
|
assertTrue(q.remove(new Integer(2))); |
553 |
|
|
assertTrue(q.add(new Integer(3))); |
554 |
|
|
assertTrue(q.take() != null); |
555 |
dl |
1.11 |
} |
556 |
jsr166 |
1.12 |
|
557 |
dl |
1.4 |
/** |
558 |
dl |
1.5 |
* contains(x) reports true when elements added but not yet removed |
559 |
dl |
1.4 |
*/ |
560 |
|
|
public void testContains() { |
561 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
562 |
|
|
for (int i = 0; i < SIZE; ++i) { |
563 |
dl |
1.1 |
assertTrue(q.contains(new Integer(i))); |
564 |
|
|
q.poll(); |
565 |
|
|
assertFalse(q.contains(new Integer(i))); |
566 |
|
|
} |
567 |
|
|
} |
568 |
|
|
|
569 |
dl |
1.4 |
/** |
570 |
dl |
1.5 |
* clear removes all elements |
571 |
dl |
1.4 |
*/ |
572 |
|
|
public void testClear() { |
573 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
574 |
dl |
1.1 |
q.clear(); |
575 |
|
|
assertTrue(q.isEmpty()); |
576 |
|
|
assertEquals(0, q.size()); |
577 |
dl |
1.3 |
assertEquals(SIZE, q.remainingCapacity()); |
578 |
|
|
q.add(one); |
579 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
580 |
dl |
1.10 |
assertTrue(q.contains(one)); |
581 |
dl |
1.1 |
q.clear(); |
582 |
|
|
assertTrue(q.isEmpty()); |
583 |
|
|
} |
584 |
|
|
|
585 |
dl |
1.4 |
/** |
586 |
dl |
1.5 |
* containsAll(c) is true when c contains a subset of elements |
587 |
dl |
1.4 |
*/ |
588 |
|
|
public void testContainsAll() { |
589 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
590 |
|
|
LinkedBlockingQueue p = new LinkedBlockingQueue(SIZE); |
591 |
|
|
for (int i = 0; i < SIZE; ++i) { |
592 |
dl |
1.1 |
assertTrue(q.containsAll(p)); |
593 |
|
|
assertFalse(p.containsAll(q)); |
594 |
|
|
p.add(new Integer(i)); |
595 |
|
|
} |
596 |
|
|
assertTrue(p.containsAll(q)); |
597 |
|
|
} |
598 |
|
|
|
599 |
dl |
1.4 |
/** |
600 |
dl |
1.5 |
* retainAll(c) retains only those elements of c and reports true if changed |
601 |
dl |
1.4 |
*/ |
602 |
|
|
public void testRetainAll() { |
603 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
604 |
|
|
LinkedBlockingQueue p = populatedQueue(SIZE); |
605 |
|
|
for (int i = 0; i < SIZE; ++i) { |
606 |
dl |
1.1 |
boolean changed = q.retainAll(p); |
607 |
|
|
if (i == 0) |
608 |
|
|
assertFalse(changed); |
609 |
|
|
else |
610 |
|
|
assertTrue(changed); |
611 |
|
|
|
612 |
|
|
assertTrue(q.containsAll(p)); |
613 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
614 |
dl |
1.1 |
p.remove(); |
615 |
|
|
} |
616 |
|
|
} |
617 |
|
|
|
618 |
dl |
1.4 |
/** |
619 |
dl |
1.5 |
* removeAll(c) removes only those elements of c and reports true if changed |
620 |
dl |
1.4 |
*/ |
621 |
|
|
public void testRemoveAll() { |
622 |
dl |
1.3 |
for (int i = 1; i < SIZE; ++i) { |
623 |
|
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
624 |
|
|
LinkedBlockingQueue p = populatedQueue(i); |
625 |
dl |
1.1 |
assertTrue(q.removeAll(p)); |
626 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
627 |
dl |
1.1 |
for (int j = 0; j < i; ++j) { |
628 |
|
|
Integer I = (Integer)(p.remove()); |
629 |
|
|
assertFalse(q.contains(I)); |
630 |
|
|
} |
631 |
|
|
} |
632 |
|
|
} |
633 |
|
|
|
634 |
dl |
1.4 |
/** |
635 |
dl |
1.5 |
* toArray contains all elements |
636 |
dl |
1.4 |
*/ |
637 |
jsr166 |
1.17 |
public void testToArray() throws InterruptedException { |
638 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
639 |
jsr166 |
1.15 |
Object[] o = q.toArray(); |
640 |
|
|
for (int i = 0; i < o.length; i++) |
641 |
|
|
assertEquals(o[i], q.take()); |
642 |
dl |
1.1 |
} |
643 |
|
|
|
644 |
dl |
1.4 |
/** |
645 |
dl |
1.5 |
* toArray(a) contains all elements |
646 |
dl |
1.4 |
*/ |
647 |
jsr166 |
1.17 |
public void testToArray2() throws InterruptedException { |
648 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
649 |
jsr166 |
1.15 |
Integer[] ints = new Integer[SIZE]; |
650 |
|
|
ints = (Integer[])q.toArray(ints); |
651 |
jsr166 |
1.17 |
for (int i = 0; i < ints.length; i++) |
652 |
|
|
assertEquals(ints[i], q.take()); |
653 |
dl |
1.1 |
} |
654 |
dl |
1.6 |
|
655 |
|
|
/** |
656 |
|
|
* toArray(null) throws NPE |
657 |
|
|
*/ |
658 |
|
|
public void testToArray_BadArg() { |
659 |
jsr166 |
1.23 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
660 |
jsr166 |
1.15 |
try { |
661 |
|
|
Object o[] = q.toArray(null); |
662 |
|
|
shouldThrow(); |
663 |
|
|
} catch (NullPointerException success) {} |
664 |
dl |
1.6 |
} |
665 |
|
|
|
666 |
|
|
/** |
667 |
dl |
1.8 |
* toArray with incompatible array type throws CCE |
668 |
dl |
1.6 |
*/ |
669 |
|
|
public void testToArray1_BadArg() { |
670 |
jsr166 |
1.23 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
671 |
jsr166 |
1.15 |
try { |
672 |
jsr166 |
1.23 |
Object o[] = q.toArray(new String[10]); |
673 |
jsr166 |
1.15 |
shouldThrow(); |
674 |
jsr166 |
1.17 |
} catch (ArrayStoreException success) {} |
675 |
dl |
1.6 |
} |
676 |
|
|
|
677 |
jsr166 |
1.12 |
|
678 |
dl |
1.4 |
/** |
679 |
dl |
1.5 |
* iterator iterates through all elements |
680 |
dl |
1.4 |
*/ |
681 |
jsr166 |
1.17 |
public void testIterator() throws InterruptedException { |
682 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
683 |
jsr166 |
1.15 |
Iterator it = q.iterator(); |
684 |
jsr166 |
1.17 |
while (it.hasNext()) { |
685 |
|
|
assertEquals(it.next(), q.take()); |
686 |
jsr166 |
1.15 |
} |
687 |
dl |
1.1 |
} |
688 |
|
|
|
689 |
dl |
1.4 |
/** |
690 |
dl |
1.5 |
* iterator.remove removes current element |
691 |
|
|
*/ |
692 |
jsr166 |
1.26 |
public void testIteratorRemove() { |
693 |
dl |
1.5 |
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
694 |
|
|
q.add(two); |
695 |
|
|
q.add(one); |
696 |
|
|
q.add(three); |
697 |
|
|
|
698 |
|
|
Iterator it = q.iterator(); |
699 |
|
|
it.next(); |
700 |
|
|
it.remove(); |
701 |
jsr166 |
1.12 |
|
702 |
dl |
1.5 |
it = q.iterator(); |
703 |
jsr166 |
1.25 |
assertSame(it.next(), one); |
704 |
|
|
assertSame(it.next(), three); |
705 |
dl |
1.5 |
assertFalse(it.hasNext()); |
706 |
|
|
} |
707 |
|
|
|
708 |
|
|
|
709 |
|
|
/** |
710 |
|
|
* iterator ordering is FIFO |
711 |
dl |
1.4 |
*/ |
712 |
dl |
1.1 |
public void testIteratorOrdering() { |
713 |
|
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
714 |
dl |
1.3 |
q.add(one); |
715 |
|
|
q.add(two); |
716 |
|
|
q.add(three); |
717 |
dl |
1.4 |
assertEquals(0, q.remainingCapacity()); |
718 |
dl |
1.1 |
int k = 0; |
719 |
|
|
for (Iterator it = q.iterator(); it.hasNext();) { |
720 |
jsr166 |
1.23 |
assertEquals(++k, it.next()); |
721 |
dl |
1.1 |
} |
722 |
dl |
1.4 |
assertEquals(3, k); |
723 |
dl |
1.1 |
} |
724 |
|
|
|
725 |
dl |
1.4 |
/** |
726 |
dl |
1.5 |
* Modifications do not cause iterators to fail |
727 |
dl |
1.4 |
*/ |
728 |
jsr166 |
1.26 |
public void testWeaklyConsistentIteration() { |
729 |
dl |
1.1 |
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
730 |
dl |
1.3 |
q.add(one); |
731 |
|
|
q.add(two); |
732 |
|
|
q.add(three); |
733 |
jsr166 |
1.17 |
for (Iterator it = q.iterator(); it.hasNext();) { |
734 |
|
|
q.remove(); |
735 |
|
|
it.next(); |
736 |
dl |
1.1 |
} |
737 |
dl |
1.4 |
assertEquals(0, q.size()); |
738 |
dl |
1.1 |
} |
739 |
|
|
|
740 |
|
|
|
741 |
dl |
1.4 |
/** |
742 |
dl |
1.5 |
* toString contains toStrings of elements |
743 |
dl |
1.4 |
*/ |
744 |
|
|
public void testToString() { |
745 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
746 |
dl |
1.1 |
String s = q.toString(); |
747 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
748 |
dl |
1.1 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
749 |
|
|
} |
750 |
jsr166 |
1.12 |
} |
751 |
dl |
1.1 |
|
752 |
|
|
|
753 |
dl |
1.4 |
/** |
754 |
dl |
1.5 |
* offer transfers elements across Executor tasks |
755 |
dl |
1.4 |
*/ |
756 |
dl |
1.1 |
public void testOfferInExecutor() { |
757 |
|
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
758 |
dl |
1.3 |
q.add(one); |
759 |
|
|
q.add(two); |
760 |
dl |
1.1 |
ExecutorService executor = Executors.newFixedThreadPool(2); |
761 |
jsr166 |
1.17 |
executor.execute(new CheckedRunnable() { |
762 |
|
|
public void realRun() throws InterruptedException { |
763 |
jsr166 |
1.24 |
assertFalse(q.offer(three)); |
764 |
|
|
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
765 |
|
|
assertEquals(0, q.remainingCapacity()); |
766 |
jsr166 |
1.17 |
}}); |
767 |
|
|
|
768 |
|
|
executor.execute(new CheckedRunnable() { |
769 |
|
|
public void realRun() throws InterruptedException { |
770 |
|
|
Thread.sleep(SMALL_DELAY_MS); |
771 |
jsr166 |
1.24 |
assertSame(one, q.take()); |
772 |
jsr166 |
1.17 |
}}); |
773 |
jsr166 |
1.12 |
|
774 |
dl |
1.3 |
joinPool(executor); |
775 |
dl |
1.1 |
} |
776 |
|
|
|
777 |
dl |
1.4 |
/** |
778 |
dl |
1.5 |
* poll retrieves elements across Executor threads |
779 |
dl |
1.4 |
*/ |
780 |
dl |
1.1 |
public void testPollInExecutor() { |
781 |
|
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
782 |
|
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
783 |
jsr166 |
1.17 |
executor.execute(new CheckedRunnable() { |
784 |
|
|
public void realRun() throws InterruptedException { |
785 |
jsr166 |
1.24 |
assertNull(q.poll()); |
786 |
|
|
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
787 |
|
|
assertTrue(q.isEmpty()); |
788 |
jsr166 |
1.17 |
}}); |
789 |
|
|
|
790 |
|
|
executor.execute(new CheckedRunnable() { |
791 |
|
|
public void realRun() throws InterruptedException { |
792 |
|
|
Thread.sleep(SMALL_DELAY_MS); |
793 |
|
|
q.put(one); |
794 |
|
|
}}); |
795 |
jsr166 |
1.12 |
|
796 |
dl |
1.3 |
joinPool(executor); |
797 |
dl |
1.2 |
} |
798 |
|
|
|
799 |
dl |
1.4 |
/** |
800 |
dl |
1.5 |
* A deserialized serialized queue has same elements in same order |
801 |
dl |
1.4 |
*/ |
802 |
jsr166 |
1.17 |
public void testSerialization() throws Exception { |
803 |
dl |
1.3 |
LinkedBlockingQueue q = populatedQueue(SIZE); |
804 |
dl |
1.2 |
|
805 |
jsr166 |
1.17 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
806 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
807 |
|
|
out.writeObject(q); |
808 |
|
|
out.close(); |
809 |
|
|
|
810 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
811 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
812 |
|
|
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
813 |
|
|
assertEquals(q.size(), r.size()); |
814 |
|
|
while (!q.isEmpty()) |
815 |
|
|
assertEquals(q.remove(), r.remove()); |
816 |
dl |
1.6 |
} |
817 |
|
|
|
818 |
|
|
/** |
819 |
|
|
* drainTo(null) throws NPE |
820 |
jsr166 |
1.12 |
*/ |
821 |
dl |
1.6 |
public void testDrainToNull() { |
822 |
|
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
823 |
|
|
try { |
824 |
|
|
q.drainTo(null); |
825 |
|
|
shouldThrow(); |
826 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
827 |
dl |
1.6 |
} |
828 |
|
|
|
829 |
|
|
/** |
830 |
|
|
* drainTo(this) throws IAE |
831 |
jsr166 |
1.12 |
*/ |
832 |
dl |
1.6 |
public void testDrainToSelf() { |
833 |
|
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
834 |
|
|
try { |
835 |
|
|
q.drainTo(q); |
836 |
|
|
shouldThrow(); |
837 |
jsr166 |
1.17 |
} catch (IllegalArgumentException success) {} |
838 |
dl |
1.6 |
} |
839 |
|
|
|
840 |
|
|
/** |
841 |
|
|
* drainTo(c) empties queue into another collection c |
842 |
jsr166 |
1.12 |
*/ |
843 |
dl |
1.6 |
public void testDrainTo() { |
844 |
|
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
845 |
|
|
ArrayList l = new ArrayList(); |
846 |
|
|
q.drainTo(l); |
847 |
|
|
assertEquals(q.size(), 0); |
848 |
|
|
assertEquals(l.size(), SIZE); |
849 |
jsr166 |
1.12 |
for (int i = 0; i < SIZE; ++i) |
850 |
dl |
1.6 |
assertEquals(l.get(i), new Integer(i)); |
851 |
dl |
1.10 |
q.add(zero); |
852 |
|
|
q.add(one); |
853 |
|
|
assertFalse(q.isEmpty()); |
854 |
|
|
assertTrue(q.contains(zero)); |
855 |
|
|
assertTrue(q.contains(one)); |
856 |
|
|
l.clear(); |
857 |
|
|
q.drainTo(l); |
858 |
|
|
assertEquals(q.size(), 0); |
859 |
|
|
assertEquals(l.size(), 2); |
860 |
jsr166 |
1.12 |
for (int i = 0; i < 2; ++i) |
861 |
dl |
1.10 |
assertEquals(l.get(i), new Integer(i)); |
862 |
dl |
1.6 |
} |
863 |
|
|
|
864 |
|
|
/** |
865 |
|
|
* drainTo empties full queue, unblocking a waiting put. |
866 |
jsr166 |
1.12 |
*/ |
867 |
jsr166 |
1.17 |
public void testDrainToWithActivePut() throws InterruptedException { |
868 |
dl |
1.6 |
final LinkedBlockingQueue q = populatedQueue(SIZE); |
869 |
jsr166 |
1.17 |
Thread t = new Thread(new CheckedRunnable() { |
870 |
|
|
public void realRun() throws InterruptedException { |
871 |
|
|
q.put(new Integer(SIZE+1)); |
872 |
|
|
}}); |
873 |
|
|
|
874 |
|
|
t.start(); |
875 |
|
|
ArrayList l = new ArrayList(); |
876 |
|
|
q.drainTo(l); |
877 |
|
|
assertTrue(l.size() >= SIZE); |
878 |
|
|
for (int i = 0; i < SIZE; ++i) |
879 |
|
|
assertEquals(l.get(i), new Integer(i)); |
880 |
|
|
t.join(); |
881 |
|
|
assertTrue(q.size() + l.size() >= SIZE); |
882 |
dl |
1.6 |
} |
883 |
|
|
|
884 |
|
|
/** |
885 |
|
|
* drainTo(null, n) throws NPE |
886 |
jsr166 |
1.12 |
*/ |
887 |
dl |
1.6 |
public void testDrainToNullN() { |
888 |
|
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
889 |
|
|
try { |
890 |
|
|
q.drainTo(null, 0); |
891 |
|
|
shouldThrow(); |
892 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
893 |
dl |
1.6 |
} |
894 |
|
|
|
895 |
|
|
/** |
896 |
|
|
* drainTo(this, n) throws IAE |
897 |
jsr166 |
1.12 |
*/ |
898 |
dl |
1.6 |
public void testDrainToSelfN() { |
899 |
|
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
900 |
|
|
try { |
901 |
|
|
q.drainTo(q, 0); |
902 |
|
|
shouldThrow(); |
903 |
jsr166 |
1.17 |
} catch (IllegalArgumentException success) {} |
904 |
dl |
1.6 |
} |
905 |
|
|
|
906 |
|
|
/** |
907 |
|
|
* drainTo(c, n) empties first max {n, size} elements of queue into c |
908 |
jsr166 |
1.12 |
*/ |
909 |
dl |
1.6 |
public void testDrainToN() { |
910 |
dl |
1.10 |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
911 |
dl |
1.6 |
for (int i = 0; i < SIZE + 2; ++i) { |
912 |
jsr166 |
1.13 |
for (int j = 0; j < SIZE; j++) |
913 |
dl |
1.10 |
assertTrue(q.offer(new Integer(j))); |
914 |
dl |
1.6 |
ArrayList l = new ArrayList(); |
915 |
|
|
q.drainTo(l, i); |
916 |
|
|
int k = (i < SIZE)? i : SIZE; |
917 |
dl |
1.10 |
assertEquals(l.size(), k); |
918 |
dl |
1.6 |
assertEquals(q.size(), SIZE-k); |
919 |
jsr166 |
1.12 |
for (int j = 0; j < k; ++j) |
920 |
dl |
1.6 |
assertEquals(l.get(j), new Integer(j)); |
921 |
dl |
1.10 |
while (q.poll() != null) ; |
922 |
dl |
1.2 |
} |
923 |
dl |
1.1 |
} |
924 |
|
|
|
925 |
|
|
} |