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