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