1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
|
|
* public domain. Use, modify, and redistribute this code in any way |
4 |
|
|
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
|
|
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
import junit.framework.*; |
9 |
|
|
import java.util.*; |
10 |
|
|
import java.util.concurrent.*; |
11 |
|
|
|
12 |
|
|
public class LinkedBlockingQueueTest extends TestCase { |
13 |
|
|
|
14 |
|
|
private static int N = 10; |
15 |
|
|
private static long SHORT_DELAY_MS = 100; |
16 |
|
|
private static long MEDIUM_DELAY_MS = 1000; |
17 |
|
|
private static long LONG_DELAY_MS = 10000; |
18 |
|
|
|
19 |
|
|
public static void main(String[] args) { |
20 |
|
|
junit.textui.TestRunner.run (suite()); |
21 |
|
|
} |
22 |
|
|
|
23 |
|
|
public static Test suite() { |
24 |
|
|
return new TestSuite(LinkedBlockingQueueTest.class); |
25 |
|
|
} |
26 |
|
|
|
27 |
|
|
/** |
28 |
|
|
* Create a queue of given size containing consecutive |
29 |
|
|
* Integers 0 ... n. |
30 |
|
|
*/ |
31 |
|
|
private LinkedBlockingQueue fullQueue(int n) { |
32 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(n); |
33 |
|
|
assertTrue(q.isEmpty()); |
34 |
|
|
for(int i = 0; i < n; i++) |
35 |
|
|
assertTrue(q.offer(new Integer(i))); |
36 |
|
|
assertFalse(q.isEmpty()); |
37 |
|
|
assertEquals(0, q.remainingCapacity()); |
38 |
|
|
assertEquals(n, q.size()); |
39 |
|
|
return q; |
40 |
|
|
} |
41 |
|
|
|
42 |
|
|
public void testConstructor1(){ |
43 |
|
|
assertEquals(N, new LinkedBlockingQueue(N).remainingCapacity()); |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
public void testConstructor2(){ |
47 |
|
|
try { |
48 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(0); |
49 |
|
|
fail("Cannot make zero-sized"); |
50 |
|
|
} |
51 |
|
|
catch (IllegalArgumentException success) {} |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
public void testConstructor3(){ |
55 |
|
|
|
56 |
|
|
try { |
57 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(null); |
58 |
|
|
fail("Cannot make from null collection"); |
59 |
|
|
} |
60 |
|
|
catch (NullPointerException success) {} |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
public void testConstructor4(){ |
64 |
|
|
try { |
65 |
|
|
Integer[] ints = new Integer[N]; |
66 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
67 |
|
|
fail("Cannot make with null elements"); |
68 |
|
|
} |
69 |
|
|
catch (NullPointerException success) {} |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
public void testConstructor5(){ |
73 |
|
|
try { |
74 |
|
|
Integer[] ints = new Integer[N]; |
75 |
|
|
for (int i = 0; i < N-1; ++i) |
76 |
|
|
ints[i] = new Integer(i); |
77 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
78 |
|
|
fail("Cannot make with null elements"); |
79 |
|
|
} |
80 |
|
|
catch (NullPointerException success) {} |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
public void testConstructor6(){ |
84 |
|
|
try { |
85 |
|
|
Integer[] ints = new Integer[N]; |
86 |
|
|
for (int i = 0; i < N; ++i) |
87 |
|
|
ints[i] = new Integer(i); |
88 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
89 |
|
|
for (int i = 0; i < N; ++i) |
90 |
|
|
assertEquals(ints[i], q.poll()); |
91 |
|
|
} |
92 |
|
|
finally {} |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
public void testEmptyFull() { |
96 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
97 |
|
|
assertTrue(q.isEmpty()); |
98 |
|
|
assertEquals("should have room for 2", 2, q.remainingCapacity()); |
99 |
|
|
q.add(new Integer(1)); |
100 |
|
|
assertFalse(q.isEmpty()); |
101 |
|
|
q.add(new Integer(2)); |
102 |
|
|
assertFalse(q.isEmpty()); |
103 |
|
|
assertEquals("queue should be full", 0, q.remainingCapacity()); |
104 |
|
|
assertFalse("offer should be rejected", q.offer(new Integer(3))); |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
public void testRemainingCapacity(){ |
108 |
|
|
LinkedBlockingQueue q = fullQueue(N); |
109 |
|
|
for (int i = 0; i < N; ++i) { |
110 |
|
|
assertEquals(i, q.remainingCapacity()); |
111 |
|
|
assertEquals(N-i, q.size()); |
112 |
|
|
q.remove(); |
113 |
|
|
} |
114 |
|
|
for (int i = 0; i < N; ++i) { |
115 |
|
|
assertEquals(N-i, q.remainingCapacity()); |
116 |
|
|
assertEquals(i, q.size()); |
117 |
|
|
q.add(new Integer(i)); |
118 |
|
|
} |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
public void testOfferNull(){ |
122 |
|
|
try { |
123 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
124 |
|
|
q.offer(null); |
125 |
|
|
fail("should throw NPE"); |
126 |
|
|
} catch (NullPointerException success) { } |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
public void testOffer(){ |
130 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
131 |
|
|
assertTrue(q.offer(new Integer(0))); |
132 |
|
|
assertFalse(q.offer(new Integer(1))); |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
public void testAdd(){ |
136 |
|
|
try { |
137 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
138 |
|
|
for (int i = 0; i < N; ++i) { |
139 |
|
|
assertTrue(q.add(new Integer(i))); |
140 |
|
|
} |
141 |
|
|
assertEquals(0, q.remainingCapacity()); |
142 |
|
|
q.add(new Integer(N)); |
143 |
|
|
} catch (IllegalStateException success){ |
144 |
|
|
} |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
public void testAddAll1(){ |
148 |
|
|
try { |
149 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
150 |
|
|
q.addAll(null); |
151 |
|
|
fail("Cannot add null collection"); |
152 |
|
|
} |
153 |
|
|
catch (NullPointerException success) {} |
154 |
|
|
} |
155 |
|
|
public void testAddAll2(){ |
156 |
|
|
try { |
157 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
158 |
|
|
Integer[] ints = new Integer[N]; |
159 |
|
|
q.addAll(Arrays.asList(ints)); |
160 |
|
|
fail("Cannot add null elements"); |
161 |
|
|
} |
162 |
|
|
catch (NullPointerException success) {} |
163 |
|
|
} |
164 |
|
|
public void testAddAll3(){ |
165 |
|
|
try { |
166 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
167 |
|
|
Integer[] ints = new Integer[N]; |
168 |
|
|
for (int i = 0; i < N-1; ++i) |
169 |
|
|
ints[i] = new Integer(i); |
170 |
|
|
q.addAll(Arrays.asList(ints)); |
171 |
|
|
fail("Cannot add null elements"); |
172 |
|
|
} |
173 |
|
|
catch (NullPointerException success) {} |
174 |
|
|
} |
175 |
|
|
public void testAddAll4(){ |
176 |
|
|
try { |
177 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
178 |
|
|
Integer[] ints = new Integer[N]; |
179 |
|
|
for (int i = 0; i < N; ++i) |
180 |
|
|
ints[i] = new Integer(i); |
181 |
|
|
q.addAll(Arrays.asList(ints)); |
182 |
|
|
fail("Cannot add with insufficient capacity"); |
183 |
|
|
} |
184 |
|
|
catch (IllegalStateException success) {} |
185 |
|
|
} |
186 |
|
|
public void testAddAll5(){ |
187 |
|
|
try { |
188 |
|
|
Integer[] empty = new Integer[0]; |
189 |
|
|
Integer[] ints = new Integer[N]; |
190 |
|
|
for (int i = 0; i < N; ++i) |
191 |
|
|
ints[i] = new Integer(i); |
192 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
193 |
|
|
assertFalse(q.addAll(Arrays.asList(empty))); |
194 |
|
|
assertTrue(q.addAll(Arrays.asList(ints))); |
195 |
|
|
for (int i = 0; i < N; ++i) |
196 |
|
|
assertEquals(ints[i], q.poll()); |
197 |
|
|
} |
198 |
|
|
finally {} |
199 |
|
|
} |
200 |
|
|
|
201 |
|
|
public void testPutNull() { |
202 |
|
|
try { |
203 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
204 |
|
|
q.put(null); |
205 |
|
|
fail("put should throw NPE"); |
206 |
|
|
} |
207 |
|
|
catch (NullPointerException success){ |
208 |
|
|
} |
209 |
|
|
catch (InterruptedException ie) { |
210 |
|
|
fail("Unexpected exception"); |
211 |
|
|
} |
212 |
|
|
} |
213 |
|
|
|
214 |
|
|
public void testPut() { |
215 |
|
|
try { |
216 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
217 |
|
|
for (int i = 0; i < N; ++i) { |
218 |
|
|
Integer I = new Integer(i); |
219 |
|
|
q.put(I); |
220 |
|
|
assertTrue(q.contains(I)); |
221 |
|
|
} |
222 |
|
|
assertEquals(0, q.remainingCapacity()); |
223 |
|
|
} |
224 |
|
|
catch (InterruptedException ie) { |
225 |
|
|
fail("Unexpected exception"); |
226 |
|
|
} |
227 |
|
|
} |
228 |
|
|
|
229 |
|
|
public void testBlockingPut(){ |
230 |
|
|
Thread t = new Thread(new Runnable() { |
231 |
|
|
public void run() { |
232 |
|
|
int added = 0; |
233 |
|
|
try { |
234 |
|
|
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
235 |
|
|
for (int i = 0; i < N; ++i) { |
236 |
|
|
q.put(new Integer(i)); |
237 |
|
|
++added; |
238 |
|
|
} |
239 |
|
|
q.put(new Integer(N)); |
240 |
|
|
fail("put should block"); |
241 |
|
|
} catch (InterruptedException ie){ |
242 |
|
|
assertEquals(added, N); |
243 |
|
|
} |
244 |
|
|
}}); |
245 |
|
|
t.start(); |
246 |
|
|
try { |
247 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
248 |
|
|
t.interrupt(); |
249 |
|
|
t.join(); |
250 |
|
|
} |
251 |
|
|
catch (InterruptedException ie) { |
252 |
|
|
fail("Unexpected exception"); |
253 |
|
|
} |
254 |
|
|
} |
255 |
|
|
|
256 |
|
|
public void testPutWithTake() { |
257 |
|
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
258 |
|
|
Thread t = new Thread(new Runnable() { |
259 |
|
|
public void run(){ |
260 |
|
|
int added = 0; |
261 |
|
|
try { |
262 |
|
|
q.put(new Object()); |
263 |
|
|
++added; |
264 |
|
|
q.put(new Object()); |
265 |
|
|
++added; |
266 |
|
|
q.put(new Object()); |
267 |
|
|
++added; |
268 |
|
|
q.put(new Object()); |
269 |
|
|
++added; |
270 |
|
|
fail("Should block"); |
271 |
|
|
} catch (InterruptedException e){ |
272 |
|
|
assertTrue(added >= 2); |
273 |
|
|
} |
274 |
|
|
} |
275 |
|
|
}); |
276 |
|
|
try { |
277 |
|
|
t.start(); |
278 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
279 |
|
|
q.take(); |
280 |
|
|
t.interrupt(); |
281 |
|
|
t.join(); |
282 |
|
|
} catch (Exception e){ |
283 |
|
|
fail("Unexpected exception"); |
284 |
|
|
} |
285 |
|
|
} |
286 |
|
|
|
287 |
|
|
public void testTimedOffer() { |
288 |
|
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
289 |
|
|
Thread t = new Thread(new Runnable() { |
290 |
|
|
public void run(){ |
291 |
|
|
try { |
292 |
|
|
q.put(new Object()); |
293 |
|
|
q.put(new Object()); |
294 |
|
|
assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); |
295 |
|
|
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
296 |
|
|
fail("Should block"); |
297 |
|
|
} catch (InterruptedException success){} |
298 |
|
|
} |
299 |
|
|
}); |
300 |
|
|
|
301 |
|
|
try { |
302 |
|
|
t.start(); |
303 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
304 |
|
|
t.interrupt(); |
305 |
|
|
t.join(); |
306 |
|
|
} catch (Exception e){ |
307 |
|
|
fail("Unexpected exception"); |
308 |
|
|
} |
309 |
|
|
} |
310 |
|
|
|
311 |
|
|
public void testTake(){ |
312 |
|
|
try { |
313 |
|
|
LinkedBlockingQueue q = fullQueue(N); |
314 |
|
|
for (int i = 0; i < N; ++i) { |
315 |
|
|
assertEquals(i, ((Integer)q.take()).intValue()); |
316 |
|
|
} |
317 |
|
|
} catch (InterruptedException e){ |
318 |
|
|
fail("Unexpected exception"); |
319 |
|
|
} |
320 |
|
|
} |
321 |
|
|
|
322 |
|
|
public void testTakeFromEmpty() { |
323 |
|
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
324 |
|
|
Thread t = new Thread(new Runnable() { |
325 |
|
|
public void run(){ |
326 |
|
|
try { |
327 |
|
|
q.take(); |
328 |
|
|
fail("Should block"); |
329 |
|
|
} catch (InterruptedException success){ } |
330 |
|
|
} |
331 |
|
|
}); |
332 |
|
|
try { |
333 |
|
|
t.start(); |
334 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
335 |
|
|
t.interrupt(); |
336 |
|
|
t.join(); |
337 |
|
|
} catch (Exception e){ |
338 |
|
|
fail("Unexpected exception"); |
339 |
|
|
} |
340 |
|
|
} |
341 |
|
|
|
342 |
|
|
public void testBlockingTake(){ |
343 |
|
|
Thread t = new Thread(new Runnable() { |
344 |
|
|
public void run() { |
345 |
|
|
try { |
346 |
|
|
LinkedBlockingQueue q = fullQueue(N); |
347 |
|
|
for (int i = 0; i < N; ++i) { |
348 |
|
|
assertEquals(i, ((Integer)q.take()).intValue()); |
349 |
|
|
} |
350 |
|
|
q.take(); |
351 |
|
|
fail("take should block"); |
352 |
|
|
} catch (InterruptedException success){ |
353 |
|
|
} |
354 |
|
|
}}); |
355 |
|
|
t.start(); |
356 |
|
|
try { |
357 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
358 |
|
|
t.interrupt(); |
359 |
|
|
t.join(); |
360 |
|
|
} |
361 |
|
|
catch (InterruptedException ie) { |
362 |
|
|
fail("Unexpected exception"); |
363 |
|
|
} |
364 |
|
|
} |
365 |
|
|
|
366 |
|
|
|
367 |
|
|
public void testPoll(){ |
368 |
|
|
LinkedBlockingQueue q = fullQueue(N); |
369 |
|
|
for (int i = 0; i < N; ++i) { |
370 |
|
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
371 |
|
|
} |
372 |
|
|
assertNull(q.poll()); |
373 |
|
|
} |
374 |
|
|
|
375 |
|
|
public void testTimedPoll0() { |
376 |
|
|
try { |
377 |
|
|
LinkedBlockingQueue q = fullQueue(N); |
378 |
|
|
for (int i = 0; i < N; ++i) { |
379 |
|
|
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
380 |
|
|
} |
381 |
|
|
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
382 |
|
|
} catch (InterruptedException e){ |
383 |
|
|
fail("Unexpected exception"); |
384 |
|
|
} |
385 |
|
|
} |
386 |
|
|
|
387 |
|
|
public void testTimedPoll() { |
388 |
|
|
try { |
389 |
|
|
LinkedBlockingQueue q = fullQueue(N); |
390 |
|
|
for (int i = 0; i < N; ++i) { |
391 |
|
|
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
392 |
|
|
} |
393 |
|
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
394 |
|
|
} catch (InterruptedException e){ |
395 |
|
|
fail("Unexpected exception"); |
396 |
|
|
} |
397 |
|
|
} |
398 |
|
|
|
399 |
|
|
public void testInterruptedTimedPoll(){ |
400 |
|
|
Thread t = new Thread(new Runnable() { |
401 |
|
|
public void run() { |
402 |
|
|
try { |
403 |
|
|
LinkedBlockingQueue q = fullQueue(N); |
404 |
|
|
for (int i = 0; i < N; ++i) { |
405 |
|
|
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
406 |
|
|
} |
407 |
|
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
408 |
|
|
} catch (InterruptedException success){ |
409 |
|
|
} |
410 |
|
|
}}); |
411 |
|
|
t.start(); |
412 |
|
|
try { |
413 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
414 |
|
|
t.interrupt(); |
415 |
|
|
t.join(); |
416 |
|
|
} |
417 |
|
|
catch (InterruptedException ie) { |
418 |
|
|
fail("Unexpected exception"); |
419 |
|
|
} |
420 |
|
|
} |
421 |
|
|
|
422 |
|
|
public void testTimedPollWithOffer(){ |
423 |
|
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
424 |
|
|
Thread t = new Thread(new Runnable() { |
425 |
|
|
public void run(){ |
426 |
|
|
try { |
427 |
|
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
428 |
|
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
429 |
|
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
430 |
|
|
fail("Should block"); |
431 |
|
|
} catch (InterruptedException success) { } |
432 |
|
|
} |
433 |
|
|
}); |
434 |
|
|
try { |
435 |
|
|
t.start(); |
436 |
|
|
Thread.sleep(SHORT_DELAY_MS * 2); |
437 |
|
|
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
438 |
|
|
t.interrupt(); |
439 |
|
|
t.join(); |
440 |
|
|
} catch (Exception e){ |
441 |
|
|
fail("Unexpected exception"); |
442 |
|
|
} |
443 |
|
|
} |
444 |
|
|
|
445 |
|
|
|
446 |
|
|
public void testPeek(){ |
447 |
|
|
LinkedBlockingQueue q = fullQueue(N); |
448 |
|
|
for (int i = 0; i < N; ++i) { |
449 |
|
|
assertEquals(i, ((Integer)q.peek()).intValue()); |
450 |
|
|
q.poll(); |
451 |
|
|
assertTrue(q.peek() == null || |
452 |
|
|
i != ((Integer)q.peek()).intValue()); |
453 |
|
|
} |
454 |
|
|
assertNull(q.peek()); |
455 |
|
|
} |
456 |
|
|
|
457 |
|
|
public void testElement(){ |
458 |
|
|
LinkedBlockingQueue q = fullQueue(N); |
459 |
|
|
for (int i = 0; i < N; ++i) { |
460 |
|
|
assertEquals(i, ((Integer)q.element()).intValue()); |
461 |
|
|
q.poll(); |
462 |
|
|
} |
463 |
|
|
try { |
464 |
|
|
q.element(); |
465 |
|
|
fail("no such element"); |
466 |
|
|
} |
467 |
|
|
catch (NoSuchElementException success) {} |
468 |
|
|
} |
469 |
|
|
|
470 |
|
|
public void testRemove(){ |
471 |
|
|
LinkedBlockingQueue q = fullQueue(N); |
472 |
|
|
for (int i = 0; i < N; ++i) { |
473 |
|
|
assertEquals(i, ((Integer)q.remove()).intValue()); |
474 |
|
|
} |
475 |
|
|
try { |
476 |
|
|
q.remove(); |
477 |
|
|
fail("remove should throw"); |
478 |
|
|
} catch (NoSuchElementException success){ |
479 |
|
|
} |
480 |
|
|
} |
481 |
|
|
|
482 |
|
|
public void testRemoveElement(){ |
483 |
|
|
LinkedBlockingQueue q = fullQueue(N); |
484 |
|
|
for (int i = 1; i < N; i+=2) { |
485 |
|
|
assertTrue(q.remove(new Integer(i))); |
486 |
|
|
} |
487 |
|
|
for (int i = 0; i < N; i+=2) { |
488 |
|
|
assertTrue(q.remove(new Integer(i))); |
489 |
|
|
assertFalse(q.remove(new Integer(i+1))); |
490 |
|
|
} |
491 |
|
|
assert(q.isEmpty()); |
492 |
|
|
} |
493 |
|
|
|
494 |
|
|
public void testContains(){ |
495 |
|
|
LinkedBlockingQueue q = fullQueue(N); |
496 |
|
|
for (int i = 0; i < N; ++i) { |
497 |
|
|
assertTrue(q.contains(new Integer(i))); |
498 |
|
|
q.poll(); |
499 |
|
|
assertFalse(q.contains(new Integer(i))); |
500 |
|
|
} |
501 |
|
|
} |
502 |
|
|
|
503 |
|
|
public void testClear(){ |
504 |
|
|
LinkedBlockingQueue q = fullQueue(N); |
505 |
|
|
q.clear(); |
506 |
|
|
assertTrue(q.isEmpty()); |
507 |
|
|
assertEquals(0, q.size()); |
508 |
|
|
assertEquals(N, q.remainingCapacity()); |
509 |
|
|
q.add(new Integer(1)); |
510 |
|
|
assertFalse(q.isEmpty()); |
511 |
|
|
q.clear(); |
512 |
|
|
assertTrue(q.isEmpty()); |
513 |
|
|
} |
514 |
|
|
|
515 |
|
|
public void testContainsAll(){ |
516 |
|
|
LinkedBlockingQueue q = fullQueue(N); |
517 |
|
|
LinkedBlockingQueue p = new LinkedBlockingQueue(N); |
518 |
|
|
for (int i = 0; i < N; ++i) { |
519 |
|
|
assertTrue(q.containsAll(p)); |
520 |
|
|
assertFalse(p.containsAll(q)); |
521 |
|
|
p.add(new Integer(i)); |
522 |
|
|
} |
523 |
|
|
assertTrue(p.containsAll(q)); |
524 |
|
|
} |
525 |
|
|
|
526 |
|
|
public void testRetainAll(){ |
527 |
|
|
LinkedBlockingQueue q = fullQueue(N); |
528 |
|
|
LinkedBlockingQueue p = fullQueue(N); |
529 |
|
|
for (int i = 0; i < N; ++i) { |
530 |
|
|
boolean changed = q.retainAll(p); |
531 |
|
|
if (i == 0) |
532 |
|
|
assertFalse(changed); |
533 |
|
|
else |
534 |
|
|
assertTrue(changed); |
535 |
|
|
|
536 |
|
|
assertTrue(q.containsAll(p)); |
537 |
|
|
assertEquals(N-i, q.size()); |
538 |
|
|
p.remove(); |
539 |
|
|
} |
540 |
|
|
} |
541 |
|
|
|
542 |
|
|
public void testRemoveAll(){ |
543 |
|
|
for (int i = 1; i < N; ++i) { |
544 |
|
|
LinkedBlockingQueue q = fullQueue(N); |
545 |
|
|
LinkedBlockingQueue p = fullQueue(i); |
546 |
|
|
assertTrue(q.removeAll(p)); |
547 |
|
|
assertEquals(N-i, q.size()); |
548 |
|
|
for (int j = 0; j < i; ++j) { |
549 |
|
|
Integer I = (Integer)(p.remove()); |
550 |
|
|
assertFalse(q.contains(I)); |
551 |
|
|
} |
552 |
|
|
} |
553 |
|
|
} |
554 |
|
|
|
555 |
|
|
|
556 |
|
|
public void testToArray(){ |
557 |
|
|
LinkedBlockingQueue q = fullQueue(N); |
558 |
|
|
Object[] o = q.toArray(); |
559 |
|
|
try { |
560 |
|
|
for(int i = 0; i < o.length; i++) |
561 |
|
|
assertEquals(o[i], q.take()); |
562 |
|
|
} catch (InterruptedException e){ |
563 |
|
|
fail("Unexpected exception"); |
564 |
|
|
} |
565 |
|
|
} |
566 |
|
|
|
567 |
|
|
public void testToArray2(){ |
568 |
|
|
LinkedBlockingQueue q = fullQueue(N); |
569 |
|
|
Integer[] ints = new Integer[N]; |
570 |
|
|
ints = (Integer[])q.toArray(ints); |
571 |
|
|
try { |
572 |
|
|
for(int i = 0; i < ints.length; i++) |
573 |
|
|
assertEquals(ints[i], q.take()); |
574 |
|
|
} catch (InterruptedException e){ |
575 |
|
|
fail("Unexpected exception"); |
576 |
|
|
} |
577 |
|
|
} |
578 |
|
|
|
579 |
|
|
public void testIterator(){ |
580 |
|
|
LinkedBlockingQueue q = fullQueue(N); |
581 |
|
|
Iterator it = q.iterator(); |
582 |
|
|
try { |
583 |
|
|
while(it.hasNext()){ |
584 |
|
|
assertEquals(it.next(), q.take()); |
585 |
|
|
} |
586 |
|
|
} catch (InterruptedException e){ |
587 |
|
|
fail("Unexpected exception"); |
588 |
|
|
} |
589 |
|
|
} |
590 |
|
|
|
591 |
|
|
public void testIteratorOrdering() { |
592 |
|
|
|
593 |
|
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
594 |
|
|
|
595 |
|
|
q.add(new Integer(1)); |
596 |
|
|
q.add(new Integer(2)); |
597 |
|
|
q.add(new Integer(3)); |
598 |
|
|
|
599 |
|
|
assertEquals("queue should be full", 0, q.remainingCapacity()); |
600 |
|
|
|
601 |
|
|
int k = 0; |
602 |
|
|
for (Iterator it = q.iterator(); it.hasNext();) { |
603 |
|
|
int i = ((Integer)(it.next())).intValue(); |
604 |
|
|
assertEquals("items should come out in order", ++k, i); |
605 |
|
|
} |
606 |
|
|
|
607 |
|
|
assertEquals("should go through 3 elements", 3, k); |
608 |
|
|
} |
609 |
|
|
|
610 |
|
|
public void testWeaklyConsistentIteration () { |
611 |
|
|
|
612 |
|
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
613 |
|
|
|
614 |
|
|
q.add(new Integer(1)); |
615 |
|
|
q.add(new Integer(2)); |
616 |
|
|
q.add(new Integer(3)); |
617 |
|
|
|
618 |
|
|
try { |
619 |
|
|
for (Iterator it = q.iterator(); it.hasNext();) { |
620 |
|
|
q.remove(); |
621 |
|
|
it.next(); |
622 |
|
|
} |
623 |
|
|
} |
624 |
|
|
catch (ConcurrentModificationException e) { |
625 |
|
|
fail("weakly consistent iterator; should not get CME"); |
626 |
|
|
} |
627 |
|
|
|
628 |
|
|
assertEquals("queue should be empty again", 0, q.size()); |
629 |
|
|
} |
630 |
|
|
|
631 |
|
|
|
632 |
|
|
public void testToString(){ |
633 |
|
|
LinkedBlockingQueue q = fullQueue(N); |
634 |
|
|
String s = q.toString(); |
635 |
|
|
for (int i = 0; i < N; ++i) { |
636 |
|
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
637 |
|
|
} |
638 |
|
|
} |
639 |
|
|
|
640 |
|
|
|
641 |
|
|
public void testOfferInExecutor() { |
642 |
|
|
|
643 |
|
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
644 |
|
|
|
645 |
|
|
q.add(new Integer(1)); |
646 |
|
|
q.add(new Integer(2)); |
647 |
|
|
|
648 |
|
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
649 |
|
|
|
650 |
|
|
executor.execute(new Runnable() { |
651 |
|
|
public void run() { |
652 |
|
|
assertFalse("offer should be rejected", q.offer(new Integer(3))); |
653 |
|
|
try { |
654 |
|
|
assertTrue("offer should be accepted", q.offer(new Integer(3), MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); |
655 |
|
|
assertEquals(0, q.remainingCapacity()); |
656 |
|
|
} |
657 |
|
|
catch (InterruptedException e) { |
658 |
|
|
fail("should not be interrupted"); |
659 |
|
|
} |
660 |
|
|
} |
661 |
|
|
}); |
662 |
|
|
|
663 |
|
|
executor.execute(new Runnable() { |
664 |
|
|
public void run() { |
665 |
|
|
try { |
666 |
|
|
Thread.sleep(MEDIUM_DELAY_MS); |
667 |
|
|
assertEquals("first item in queue should be 1", new Integer(1), q.take()); |
668 |
|
|
} |
669 |
|
|
catch (InterruptedException e) { |
670 |
|
|
fail("should not be interrupted"); |
671 |
|
|
} |
672 |
|
|
} |
673 |
|
|
}); |
674 |
|
|
|
675 |
|
|
executor.shutdown(); |
676 |
|
|
|
677 |
|
|
} |
678 |
|
|
|
679 |
|
|
public void testPollInExecutor() { |
680 |
|
|
|
681 |
|
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
682 |
|
|
|
683 |
|
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
684 |
|
|
|
685 |
|
|
executor.execute(new Runnable() { |
686 |
|
|
public void run() { |
687 |
|
|
assertNull("poll should fail", q.poll()); |
688 |
|
|
try { |
689 |
|
|
assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); |
690 |
|
|
assertTrue(q.isEmpty()); |
691 |
|
|
} |
692 |
|
|
catch (InterruptedException e) { |
693 |
|
|
fail("should not be interrupted"); |
694 |
|
|
} |
695 |
|
|
} |
696 |
|
|
}); |
697 |
|
|
|
698 |
|
|
executor.execute(new Runnable() { |
699 |
|
|
public void run() { |
700 |
|
|
try { |
701 |
|
|
Thread.sleep(MEDIUM_DELAY_MS); |
702 |
|
|
q.put(new Integer(1)); |
703 |
|
|
} |
704 |
|
|
catch (InterruptedException e) { |
705 |
|
|
fail("should not be interrupted"); |
706 |
|
|
} |
707 |
|
|
} |
708 |
|
|
}); |
709 |
|
|
|
710 |
|
|
executor.shutdown(); |
711 |
|
|
|
712 |
|
|
} |
713 |
|
|
|
714 |
|
|
} |