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