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 |
> |
* |
39 |
> |
*/ |
40 |
> |
public void testConstructor1() { |
41 |
> |
assertEquals(SIZE, new ArrayBlockingQueue(SIZE).remainingCapacity()); |
42 |
|
} |
43 |
|
|
44 |
< |
public void testConstructor2(){ |
44 |
> |
/** |
45 |
> |
* |
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(){ |
55 |
> |
/** |
56 |
> |
* |
57 |
> |
*/ |
58 |
> |
public void testConstructor3() { |
59 |
|
|
60 |
|
try { |
61 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null); |
62 |
< |
fail("Cannot make from null collection"); |
62 |
> |
shouldThrow(); |
63 |
|
} |
64 |
|
catch (NullPointerException success) {} |
65 |
|
} |
66 |
|
|
67 |
< |
public void testConstructor4(){ |
67 |
> |
/** |
68 |
> |
* |
69 |
> |
*/ |
70 |
> |
public void testConstructor4() { |
71 |
|
try { |
72 |
< |
Integer[] ints = new Integer[N]; |
73 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(N, false, Arrays.asList(ints)); |
74 |
< |
fail("Cannot make with null elements"); |
72 |
> |
Integer[] ints = new Integer[SIZE]; |
73 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
74 |
> |
shouldThrow(); |
75 |
|
} |
76 |
|
catch (NullPointerException success) {} |
77 |
|
} |
78 |
|
|
79 |
< |
public void testConstructor5(){ |
79 |
> |
/** |
80 |
> |
* |
81 |
> |
*/ |
82 |
> |
public void testConstructor5() { |
83 |
|
try { |
84 |
< |
Integer[] ints = new Integer[N]; |
85 |
< |
for (int i = 0; i < N-1; ++i) |
84 |
> |
Integer[] ints = new Integer[SIZE]; |
85 |
> |
for (int i = 0; i < SIZE-1; ++i) |
86 |
|
ints[i] = new Integer(i); |
87 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(N, false, Arrays.asList(ints)); |
88 |
< |
fail("Cannot make with null elements"); |
87 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
88 |
> |
shouldThrow(); |
89 |
|
} |
90 |
|
catch (NullPointerException success) {} |
91 |
|
} |
92 |
|
|
93 |
< |
public void testConstructor6(){ |
93 |
> |
/** |
94 |
> |
* |
95 |
> |
*/ |
96 |
> |
public void testConstructor6() { |
97 |
|
try { |
98 |
< |
Integer[] ints = new Integer[N]; |
99 |
< |
for (int i = 0; i < N; ++i) |
98 |
> |
Integer[] ints = new Integer[SIZE]; |
99 |
> |
for (int i = 0; i < SIZE; ++i) |
100 |
|
ints[i] = new Integer(i); |
101 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints)); |
102 |
< |
fail("Cannot make with insufficient capacity"); |
102 |
> |
shouldThrow(); |
103 |
|
} |
104 |
|
catch (IllegalArgumentException success) {} |
105 |
|
} |
106 |
|
|
107 |
< |
public void testConstructor7(){ |
107 |
> |
/** |
108 |
> |
* |
109 |
> |
*/ |
110 |
> |
public void testConstructor7() { |
111 |
|
try { |
112 |
< |
Integer[] ints = new Integer[N]; |
113 |
< |
for (int i = 0; i < N; ++i) |
112 |
> |
Integer[] ints = new Integer[SIZE]; |
113 |
> |
for (int i = 0; i < SIZE; ++i) |
114 |
|
ints[i] = new Integer(i); |
115 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(N, true, Arrays.asList(ints)); |
116 |
< |
for (int i = 0; i < N; ++i) |
115 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints)); |
116 |
> |
for (int i = 0; i < SIZE; ++i) |
117 |
|
assertEquals(ints[i], q.poll()); |
118 |
|
} |
119 |
|
finally {} |
120 |
|
} |
121 |
|
|
122 |
+ |
/** |
123 |
+ |
* |
124 |
+ |
*/ |
125 |
|
public void testEmptyFull() { |
126 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
127 |
|
assertTrue(q.isEmpty()); |
128 |
< |
assertEquals("should have room for 2", 2, q.remainingCapacity()); |
129 |
< |
q.add(new Integer(1)); |
128 |
> |
assertEquals(2, q.remainingCapacity()); |
129 |
> |
q.add(one); |
130 |
|
assertFalse(q.isEmpty()); |
131 |
< |
q.add(new Integer(2)); |
131 |
> |
q.add(two); |
132 |
|
assertFalse(q.isEmpty()); |
133 |
< |
assertEquals("queue should be full", 0, q.remainingCapacity()); |
134 |
< |
assertFalse("offer should be rejected", q.offer(new Integer(3))); |
133 |
> |
assertEquals(0, q.remainingCapacity()); |
134 |
> |
assertFalse(q.offer(three)); |
135 |
|
} |
136 |
|
|
137 |
< |
public void testRemainingCapacity(){ |
138 |
< |
ArrayBlockingQueue q = fullQueue(N); |
139 |
< |
for (int i = 0; i < N; ++i) { |
137 |
> |
/** |
138 |
> |
* |
139 |
> |
*/ |
140 |
> |
public void testRemainingCapacity() { |
141 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
142 |
> |
for (int i = 0; i < SIZE; ++i) { |
143 |
|
assertEquals(i, q.remainingCapacity()); |
144 |
< |
assertEquals(N-i, q.size()); |
144 |
> |
assertEquals(SIZE-i, q.size()); |
145 |
|
q.remove(); |
146 |
|
} |
147 |
< |
for (int i = 0; i < N; ++i) { |
148 |
< |
assertEquals(N-i, q.remainingCapacity()); |
147 |
> |
for (int i = 0; i < SIZE; ++i) { |
148 |
> |
assertEquals(SIZE-i, q.remainingCapacity()); |
149 |
|
assertEquals(i, q.size()); |
150 |
|
q.add(new Integer(i)); |
151 |
|
} |
152 |
|
} |
153 |
|
|
154 |
< |
public void testOfferNull(){ |
154 |
> |
/** |
155 |
> |
* |
156 |
> |
*/ |
157 |
> |
public void testOfferNull() { |
158 |
|
try { |
159 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
160 |
|
q.offer(null); |
161 |
< |
fail("should throw NPE"); |
161 |
> |
shouldThrow(); |
162 |
|
} catch (NullPointerException success) { } |
163 |
|
} |
164 |
|
|
165 |
< |
public void testOffer(){ |
165 |
> |
/** |
166 |
> |
* |
167 |
> |
*/ |
168 |
> |
public void testOffer() { |
169 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
170 |
< |
assertTrue(q.offer(new Integer(0))); |
171 |
< |
assertFalse(q.offer(new Integer(1))); |
170 |
> |
assertTrue(q.offer(zero)); |
171 |
> |
assertFalse(q.offer(one)); |
172 |
|
} |
173 |
|
|
174 |
< |
public void testAdd(){ |
174 |
> |
/** |
175 |
> |
* |
176 |
> |
*/ |
177 |
> |
public void testAdd() { |
178 |
|
try { |
179 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(N); |
180 |
< |
for (int i = 0; i < N; ++i) { |
179 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
180 |
> |
for (int i = 0; i < SIZE; ++i) { |
181 |
|
assertTrue(q.add(new Integer(i))); |
182 |
|
} |
183 |
|
assertEquals(0, q.remainingCapacity()); |
184 |
< |
q.add(new Integer(N)); |
184 |
> |
q.add(new Integer(SIZE)); |
185 |
|
} catch (IllegalStateException success){ |
186 |
|
} |
187 |
|
} |
188 |
|
|
189 |
< |
public void testAddAll1(){ |
189 |
> |
/** |
190 |
> |
* |
191 |
> |
*/ |
192 |
> |
public void testAddAll1() { |
193 |
|
try { |
194 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
195 |
|
q.addAll(null); |
196 |
< |
fail("Cannot add null collection"); |
196 |
> |
shouldThrow(); |
197 |
|
} |
198 |
|
catch (NullPointerException success) {} |
199 |
|
} |
200 |
< |
public void testAddAll2(){ |
200 |
> |
/** |
201 |
> |
* |
202 |
> |
*/ |
203 |
> |
public void testAddAll2() { |
204 |
|
try { |
205 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(N); |
206 |
< |
Integer[] ints = new Integer[N]; |
205 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
206 |
> |
Integer[] ints = new Integer[SIZE]; |
207 |
|
q.addAll(Arrays.asList(ints)); |
208 |
< |
fail("Cannot add null elements"); |
208 |
> |
shouldThrow(); |
209 |
|
} |
210 |
|
catch (NullPointerException success) {} |
211 |
|
} |
212 |
< |
public void testAddAll3(){ |
212 |
> |
/** |
213 |
> |
* |
214 |
> |
*/ |
215 |
> |
public void testAddAll3() { |
216 |
|
try { |
217 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(N); |
218 |
< |
Integer[] ints = new Integer[N]; |
219 |
< |
for (int i = 0; i < N-1; ++i) |
217 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
218 |
> |
Integer[] ints = new Integer[SIZE]; |
219 |
> |
for (int i = 0; i < SIZE-1; ++i) |
220 |
|
ints[i] = new Integer(i); |
221 |
|
q.addAll(Arrays.asList(ints)); |
222 |
< |
fail("Cannot add null elements"); |
222 |
> |
shouldThrow(); |
223 |
|
} |
224 |
|
catch (NullPointerException success) {} |
225 |
|
} |
226 |
< |
public void testAddAll4(){ |
226 |
> |
/** |
227 |
> |
* |
228 |
> |
*/ |
229 |
> |
public void testAddAll4() { |
230 |
|
try { |
231 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
232 |
< |
Integer[] ints = new Integer[N]; |
233 |
< |
for (int i = 0; i < N; ++i) |
232 |
> |
Integer[] ints = new Integer[SIZE]; |
233 |
> |
for (int i = 0; i < SIZE; ++i) |
234 |
|
ints[i] = new Integer(i); |
235 |
|
q.addAll(Arrays.asList(ints)); |
236 |
< |
fail("Cannot add with insufficient capacity"); |
236 |
> |
shouldThrow(); |
237 |
|
} |
238 |
|
catch (IllegalStateException success) {} |
239 |
|
} |
240 |
< |
public void testAddAll5(){ |
240 |
> |
/** |
241 |
> |
* |
242 |
> |
*/ |
243 |
> |
public void testAddAll5() { |
244 |
|
try { |
245 |
|
Integer[] empty = new Integer[0]; |
246 |
< |
Integer[] ints = new Integer[N]; |
247 |
< |
for (int i = 0; i < N; ++i) |
246 |
> |
Integer[] ints = new Integer[SIZE]; |
247 |
> |
for (int i = 0; i < SIZE; ++i) |
248 |
|
ints[i] = new Integer(i); |
249 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(N); |
249 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
250 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
251 |
|
assertTrue(q.addAll(Arrays.asList(ints))); |
252 |
< |
for (int i = 0; i < N; ++i) |
252 |
> |
for (int i = 0; i < SIZE; ++i) |
253 |
|
assertEquals(ints[i], q.poll()); |
254 |
|
} |
255 |
|
finally {} |
256 |
|
} |
257 |
|
|
258 |
+ |
/** |
259 |
+ |
* |
260 |
+ |
*/ |
261 |
|
public void testPutNull() { |
262 |
|
try { |
263 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(N); |
263 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
264 |
|
q.put(null); |
265 |
< |
fail("put should throw NPE"); |
265 |
> |
shouldThrow(); |
266 |
|
} |
267 |
|
catch (NullPointerException success){ |
268 |
|
} |
269 |
|
catch (InterruptedException ie) { |
270 |
< |
fail("Unexpected exception"); |
270 |
> |
unexpectedException(); |
271 |
|
} |
272 |
|
} |
273 |
|
|
274 |
+ |
/** |
275 |
+ |
* |
276 |
+ |
*/ |
277 |
|
public void testPut() { |
278 |
|
try { |
279 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(N); |
280 |
< |
for (int i = 0; i < N; ++i) { |
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)); |
285 |
|
assertEquals(0, q.remainingCapacity()); |
286 |
|
} |
287 |
|
catch (InterruptedException ie) { |
288 |
< |
fail("Unexpected exception"); |
288 |
> |
unexpectedException(); |
289 |
|
} |
290 |
|
} |
291 |
|
|
292 |
< |
public void testBlockingPut(){ |
292 |
> |
/** |
293 |
> |
* |
294 |
> |
*/ |
295 |
> |
public void testBlockingPut() { |
296 |
|
Thread t = new Thread(new Runnable() { |
297 |
|
public void run() { |
298 |
|
int added = 0; |
299 |
|
try { |
300 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(N); |
301 |
< |
for (int i = 0; i < N; ++i) { |
300 |
> |
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(N)); |
306 |
< |
fail("put should block"); |
305 |
> |
q.put(new Integer(SIZE)); |
306 |
> |
threadShouldThrow(); |
307 |
|
} catch (InterruptedException ie){ |
308 |
< |
assertEquals(added, N); |
308 |
> |
threadAssertEquals(added, SIZE); |
309 |
|
} |
310 |
|
}}); |
256 |
– |
t.start(); |
311 |
|
try { |
312 |
+ |
t.start(); |
313 |
|
Thread.sleep(SHORT_DELAY_MS); |
314 |
|
t.interrupt(); |
315 |
|
t.join(); |
316 |
|
} |
317 |
|
catch (InterruptedException ie) { |
318 |
< |
fail("Unexpected exception"); |
318 |
> |
unexpectedException(); |
319 |
|
} |
320 |
|
} |
321 |
|
|
322 |
+ |
/** |
323 |
+ |
* |
324 |
+ |
*/ |
325 |
|
public void testPutWithTake() { |
326 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
327 |
|
Thread t = new Thread(new Runnable() { |
328 |
< |
public void run(){ |
328 |
> |
public void run() { |
329 |
|
int added = 0; |
330 |
|
try { |
331 |
|
q.put(new Object()); |
336 |
|
++added; |
337 |
|
q.put(new Object()); |
338 |
|
++added; |
339 |
< |
fail("Should block"); |
339 |
> |
threadShouldThrow(); |
340 |
|
} catch (InterruptedException e){ |
341 |
< |
assertTrue(added >= 2); |
341 |
> |
threadAssertTrue(added >= 2); |
342 |
|
} |
343 |
|
} |
344 |
|
}); |
349 |
|
t.interrupt(); |
350 |
|
t.join(); |
351 |
|
} catch (Exception e){ |
352 |
< |
fail("Unexpected exception"); |
352 |
> |
unexpectedException(); |
353 |
|
} |
354 |
|
} |
355 |
|
|
356 |
+ |
/** |
357 |
+ |
* |
358 |
+ |
*/ |
359 |
|
public void testTimedOffer() { |
360 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
361 |
|
Thread t = new Thread(new Runnable() { |
362 |
< |
public void run(){ |
362 |
> |
public void run() { |
363 |
|
try { |
364 |
|
q.put(new Object()); |
365 |
|
q.put(new Object()); |
366 |
< |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); |
366 |
> |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); |
367 |
|
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
368 |
< |
fail("Should block"); |
368 |
> |
threadShouldThrow(); |
369 |
|
} catch (InterruptedException success){} |
370 |
|
} |
371 |
|
}); |
376 |
|
t.interrupt(); |
377 |
|
t.join(); |
378 |
|
} catch (Exception e){ |
379 |
< |
fail("Unexpected exception"); |
379 |
> |
unexpectedException(); |
380 |
|
} |
381 |
|
} |
382 |
|
|
383 |
< |
public void testTake(){ |
383 |
> |
/** |
384 |
> |
* |
385 |
> |
*/ |
386 |
> |
public void testTake() { |
387 |
|
try { |
388 |
< |
ArrayBlockingQueue q = fullQueue(N); |
389 |
< |
for (int i = 0; i < N; ++i) { |
388 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
389 |
> |
for (int i = 0; i < SIZE; ++i) { |
390 |
|
assertEquals(i, ((Integer)q.take()).intValue()); |
391 |
|
} |
392 |
|
} catch (InterruptedException e){ |
393 |
< |
fail("Unexpected exception"); |
393 |
> |
unexpectedException(); |
394 |
|
} |
395 |
|
} |
396 |
|
|
397 |
+ |
/** |
398 |
+ |
* |
399 |
+ |
*/ |
400 |
|
public void testTakeFromEmpty() { |
401 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
402 |
|
Thread t = new Thread(new Runnable() { |
403 |
< |
public void run(){ |
403 |
> |
public void run() { |
404 |
|
try { |
405 |
|
q.take(); |
406 |
< |
fail("Should block"); |
406 |
> |
threadShouldThrow(); |
407 |
|
} catch (InterruptedException success){ } |
408 |
|
} |
409 |
|
}); |
413 |
|
t.interrupt(); |
414 |
|
t.join(); |
415 |
|
} catch (Exception e){ |
416 |
< |
fail("Unexpected exception"); |
416 |
> |
unexpectedException(); |
417 |
|
} |
418 |
|
} |
419 |
|
|
420 |
< |
public void testBlockingTake(){ |
420 |
> |
/** |
421 |
> |
* |
422 |
> |
*/ |
423 |
> |
public void testBlockingTake() { |
424 |
|
Thread t = new Thread(new Runnable() { |
425 |
|
public void run() { |
426 |
|
try { |
427 |
< |
ArrayBlockingQueue q = fullQueue(N); |
428 |
< |
for (int i = 0; i < N; ++i) { |
429 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
427 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
428 |
> |
for (int i = 0; i < SIZE; ++i) { |
429 |
> |
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
430 |
|
} |
431 |
|
q.take(); |
432 |
< |
fail("take should block"); |
432 |
> |
threadShouldThrow(); |
433 |
|
} catch (InterruptedException success){ |
434 |
|
} |
435 |
|
}}); |
366 |
– |
t.start(); |
436 |
|
try { |
437 |
< |
Thread.sleep(SHORT_DELAY_MS); |
438 |
< |
t.interrupt(); |
439 |
< |
t.join(); |
437 |
> |
t.start(); |
438 |
> |
Thread.sleep(SHORT_DELAY_MS); |
439 |
> |
t.interrupt(); |
440 |
> |
t.join(); |
441 |
|
} |
442 |
|
catch (InterruptedException ie) { |
443 |
< |
fail("Unexpected exception"); |
443 |
> |
unexpectedException(); |
444 |
|
} |
445 |
|
} |
446 |
|
|
447 |
|
|
448 |
< |
public void testPoll(){ |
449 |
< |
ArrayBlockingQueue q = fullQueue(N); |
450 |
< |
for (int i = 0; i < N; ++i) { |
448 |
> |
/** |
449 |
> |
* |
450 |
> |
*/ |
451 |
> |
public void testPoll() { |
452 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
453 |
> |
for (int i = 0; i < SIZE; ++i) { |
454 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
455 |
|
} |
456 |
|
assertNull(q.poll()); |
457 |
|
} |
458 |
|
|
459 |
+ |
/** |
460 |
+ |
* |
461 |
+ |
*/ |
462 |
|
public void testTimedPoll0() { |
463 |
|
try { |
464 |
< |
ArrayBlockingQueue q = fullQueue(N); |
465 |
< |
for (int i = 0; i < N; ++i) { |
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 |
< |
fail("Unexpected exception"); |
470 |
> |
unexpectedException(); |
471 |
|
} |
472 |
|
} |
473 |
|
|
474 |
+ |
/** |
475 |
+ |
* |
476 |
+ |
*/ |
477 |
|
public void testTimedPoll() { |
478 |
|
try { |
479 |
< |
ArrayBlockingQueue q = fullQueue(N); |
480 |
< |
for (int i = 0; i < N; ++i) { |
479 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
480 |
> |
for (int i = 0; i < SIZE; ++i) { |
481 |
|
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 |
< |
fail("Unexpected exception"); |
485 |
> |
unexpectedException(); |
486 |
|
} |
487 |
|
} |
488 |
|
|
489 |
< |
public void testInterruptedTimedPoll(){ |
489 |
> |
/** |
490 |
> |
* |
491 |
> |
*/ |
492 |
> |
public void testInterruptedTimedPoll() { |
493 |
|
Thread t = new Thread(new Runnable() { |
494 |
|
public void run() { |
495 |
|
try { |
496 |
< |
ArrayBlockingQueue q = fullQueue(N); |
497 |
< |
for (int i = 0; i < N; ++i) { |
498 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
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 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
500 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
501 |
|
} catch (InterruptedException success){ |
502 |
|
} |
503 |
|
}}); |
422 |
– |
t.start(); |
504 |
|
try { |
505 |
< |
Thread.sleep(SHORT_DELAY_MS); |
506 |
< |
t.interrupt(); |
507 |
< |
t.join(); |
505 |
> |
t.start(); |
506 |
> |
Thread.sleep(SHORT_DELAY_MS); |
507 |
> |
t.interrupt(); |
508 |
> |
t.join(); |
509 |
|
} |
510 |
|
catch (InterruptedException ie) { |
511 |
< |
fail("Unexpected exception"); |
511 |
> |
unexpectedException(); |
512 |
|
} |
513 |
|
} |
514 |
|
|
515 |
< |
public void testTimedPollWithOffer(){ |
515 |
> |
/** |
516 |
> |
* |
517 |
> |
*/ |
518 |
> |
public void testTimedPollWithOffer() { |
519 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
520 |
|
Thread t = new Thread(new Runnable() { |
521 |
< |
public void run(){ |
521 |
> |
public void run() { |
522 |
|
try { |
523 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
523 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
524 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
525 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
526 |
< |
fail("Should block"); |
526 |
> |
threadShouldThrow(); |
527 |
|
} catch (InterruptedException success) { } |
528 |
|
} |
529 |
|
}); |
530 |
|
try { |
531 |
|
t.start(); |
532 |
< |
Thread.sleep(SHORT_DELAY_MS * 2); |
533 |
< |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
532 |
> |
Thread.sleep(SMALL_DELAY_MS); |
533 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
534 |
|
t.interrupt(); |
535 |
|
t.join(); |
536 |
|
} catch (Exception e){ |
537 |
< |
fail("Unexpected exception"); |
537 |
> |
unexpectedException(); |
538 |
|
} |
539 |
|
} |
540 |
|
|
541 |
|
|
542 |
< |
public void testPeek(){ |
543 |
< |
ArrayBlockingQueue q = fullQueue(N); |
544 |
< |
for (int i = 0; i < N; ++i) { |
542 |
> |
/** |
543 |
> |
* |
544 |
> |
*/ |
545 |
> |
public void testPeek() { |
546 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
547 |
> |
for (int i = 0; i < SIZE; ++i) { |
548 |
|
assertEquals(i, ((Integer)q.peek()).intValue()); |
549 |
|
q.poll(); |
550 |
|
assertTrue(q.peek() == null || |
553 |
|
assertNull(q.peek()); |
554 |
|
} |
555 |
|
|
556 |
< |
public void testElement(){ |
557 |
< |
ArrayBlockingQueue q = fullQueue(N); |
558 |
< |
for (int i = 0; i < N; ++i) { |
556 |
> |
/** |
557 |
> |
* |
558 |
> |
*/ |
559 |
> |
public void testElement() { |
560 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
561 |
> |
for (int i = 0; i < SIZE; ++i) { |
562 |
|
assertEquals(i, ((Integer)q.element()).intValue()); |
563 |
|
q.poll(); |
564 |
|
} |
565 |
|
try { |
566 |
|
q.element(); |
567 |
< |
fail("no such element"); |
567 |
> |
shouldThrow(); |
568 |
|
} |
569 |
|
catch (NoSuchElementException success) {} |
570 |
|
} |
571 |
|
|
572 |
< |
public void testRemove(){ |
573 |
< |
ArrayBlockingQueue q = fullQueue(N); |
574 |
< |
for (int i = 0; i < N; ++i) { |
572 |
> |
/** |
573 |
> |
* |
574 |
> |
*/ |
575 |
> |
public void testRemove() { |
576 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
577 |
> |
for (int i = 0; i < SIZE; ++i) { |
578 |
|
assertEquals(i, ((Integer)q.remove()).intValue()); |
579 |
|
} |
580 |
|
try { |
581 |
|
q.remove(); |
582 |
< |
fail("remove should throw"); |
582 |
> |
shouldThrow(); |
583 |
|
} catch (NoSuchElementException success){ |
584 |
|
} |
585 |
|
} |
586 |
|
|
587 |
< |
public void testRemoveElement(){ |
588 |
< |
ArrayBlockingQueue q = fullQueue(N); |
589 |
< |
for (int i = 1; i < N; i+=2) { |
587 |
> |
/** |
588 |
> |
* |
589 |
> |
*/ |
590 |
> |
public void testRemoveElement() { |
591 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
592 |
> |
for (int i = 1; i < SIZE; i+=2) { |
593 |
|
assertTrue(q.remove(new Integer(i))); |
594 |
|
} |
595 |
< |
for (int i = 0; i < N; i+=2) { |
595 |
> |
for (int i = 0; i < SIZE; i+=2) { |
596 |
|
assertTrue(q.remove(new Integer(i))); |
597 |
|
assertFalse(q.remove(new Integer(i+1))); |
598 |
|
} |
599 |
< |
assert(q.isEmpty()); |
599 |
> |
assertTrue(q.isEmpty()); |
600 |
|
} |
601 |
|
|
602 |
< |
public void testContains(){ |
603 |
< |
ArrayBlockingQueue q = fullQueue(N); |
604 |
< |
for (int i = 0; i < N; ++i) { |
602 |
> |
/** |
603 |
> |
* |
604 |
> |
*/ |
605 |
> |
public void testContains() { |
606 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
607 |
> |
for (int i = 0; i < SIZE; ++i) { |
608 |
|
assertTrue(q.contains(new Integer(i))); |
609 |
|
q.poll(); |
610 |
|
assertFalse(q.contains(new Integer(i))); |
611 |
|
} |
612 |
|
} |
613 |
|
|
614 |
< |
public void testClear(){ |
615 |
< |
ArrayBlockingQueue q = fullQueue(N); |
614 |
> |
/** |
615 |
> |
* |
616 |
> |
*/ |
617 |
> |
public void testClear() { |
618 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
619 |
|
q.clear(); |
620 |
|
assertTrue(q.isEmpty()); |
621 |
|
assertEquals(0, q.size()); |
622 |
< |
assertEquals(N, q.remainingCapacity()); |
623 |
< |
q.add(new Integer(1)); |
622 |
> |
assertEquals(SIZE, q.remainingCapacity()); |
623 |
> |
q.add(one); |
624 |
|
assertFalse(q.isEmpty()); |
625 |
|
q.clear(); |
626 |
|
assertTrue(q.isEmpty()); |
627 |
|
} |
628 |
|
|
629 |
< |
public void testContainsAll(){ |
630 |
< |
ArrayBlockingQueue q = fullQueue(N); |
631 |
< |
ArrayBlockingQueue p = new ArrayBlockingQueue(N); |
632 |
< |
for (int i = 0; i < N; ++i) { |
629 |
> |
/** |
630 |
> |
* |
631 |
> |
*/ |
632 |
> |
public void testContainsAll() { |
633 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
634 |
> |
ArrayBlockingQueue p = new ArrayBlockingQueue(SIZE); |
635 |
> |
for (int i = 0; i < SIZE; ++i) { |
636 |
|
assertTrue(q.containsAll(p)); |
637 |
|
assertFalse(p.containsAll(q)); |
638 |
|
p.add(new Integer(i)); |
640 |
|
assertTrue(p.containsAll(q)); |
641 |
|
} |
642 |
|
|
643 |
< |
public void testRetainAll(){ |
644 |
< |
ArrayBlockingQueue q = fullQueue(N); |
645 |
< |
ArrayBlockingQueue p = fullQueue(N); |
646 |
< |
for (int i = 0; i < N; ++i) { |
643 |
> |
/** |
644 |
> |
* |
645 |
> |
*/ |
646 |
> |
public void testRetainAll() { |
647 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
648 |
> |
ArrayBlockingQueue p = populatedQueue(SIZE); |
649 |
> |
for (int i = 0; i < SIZE; ++i) { |
650 |
|
boolean changed = q.retainAll(p); |
651 |
|
if (i == 0) |
652 |
|
assertFalse(changed); |
654 |
|
assertTrue(changed); |
655 |
|
|
656 |
|
assertTrue(q.containsAll(p)); |
657 |
< |
assertEquals(N-i, q.size()); |
657 |
> |
assertEquals(SIZE-i, q.size()); |
658 |
|
p.remove(); |
659 |
|
} |
660 |
|
} |
661 |
|
|
662 |
< |
public void testRemoveAll(){ |
663 |
< |
for (int i = 1; i < N; ++i) { |
664 |
< |
ArrayBlockingQueue q = fullQueue(N); |
665 |
< |
ArrayBlockingQueue p = fullQueue(i); |
662 |
> |
/** |
663 |
> |
* |
664 |
> |
*/ |
665 |
> |
public void testRemoveAll() { |
666 |
> |
for (int i = 1; i < SIZE; ++i) { |
667 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
668 |
> |
ArrayBlockingQueue p = populatedQueue(i); |
669 |
|
assertTrue(q.removeAll(p)); |
670 |
< |
assertEquals(N-i, q.size()); |
670 |
> |
assertEquals(SIZE-i, q.size()); |
671 |
|
for (int j = 0; j < i; ++j) { |
672 |
|
Integer I = (Integer)(p.remove()); |
673 |
|
assertFalse(q.contains(I)); |
676 |
|
} |
677 |
|
|
678 |
|
|
679 |
< |
public void testToArray(){ |
680 |
< |
ArrayBlockingQueue q = fullQueue(N); |
679 |
> |
/** |
680 |
> |
* |
681 |
> |
*/ |
682 |
> |
public void testToArray() { |
683 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
684 |
|
Object[] o = q.toArray(); |
685 |
|
try { |
686 |
|
for(int i = 0; i < o.length; i++) |
687 |
|
assertEquals(o[i], q.take()); |
688 |
|
} catch (InterruptedException e){ |
689 |
< |
fail("Unexpected exception"); |
689 |
> |
unexpectedException(); |
690 |
|
} |
691 |
|
} |
692 |
|
|
693 |
< |
public void testToArray2(){ |
694 |
< |
ArrayBlockingQueue q = fullQueue(N); |
695 |
< |
Integer[] ints = new Integer[N]; |
693 |
> |
/** |
694 |
> |
* |
695 |
> |
*/ |
696 |
> |
public void testToArray2() { |
697 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
698 |
> |
Integer[] ints = new Integer[SIZE]; |
699 |
|
ints = (Integer[])q.toArray(ints); |
700 |
|
try { |
701 |
|
for(int i = 0; i < ints.length; i++) |
702 |
|
assertEquals(ints[i], q.take()); |
703 |
|
} catch (InterruptedException e){ |
704 |
< |
fail("Unexpected exception"); |
704 |
> |
unexpectedException(); |
705 |
|
} |
706 |
|
} |
707 |
|
|
708 |
< |
public void testIterator(){ |
709 |
< |
ArrayBlockingQueue q = fullQueue(N); |
708 |
> |
/** |
709 |
> |
* |
710 |
> |
*/ |
711 |
> |
public void testIterator() { |
712 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
713 |
|
Iterator it = q.iterator(); |
714 |
|
try { |
715 |
|
while(it.hasNext()){ |
716 |
|
assertEquals(it.next(), q.take()); |
717 |
|
} |
718 |
|
} catch (InterruptedException e){ |
719 |
< |
fail("Unexpected exception"); |
719 |
> |
unexpectedException(); |
720 |
|
} |
721 |
|
} |
722 |
|
|
723 |
+ |
/** |
724 |
+ |
* |
725 |
+ |
*/ |
726 |
|
public void testIteratorOrdering() { |
603 |
– |
|
727 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
728 |
< |
|
729 |
< |
q.add(new Integer(1)); |
730 |
< |
q.add(new Integer(2)); |
608 |
< |
q.add(new Integer(3)); |
728 |
> |
q.add(one); |
729 |
> |
q.add(two); |
730 |
> |
q.add(three); |
731 |
|
|
732 |
|
assertEquals("queue should be full", 0, q.remainingCapacity()); |
733 |
|
|
734 |
|
int k = 0; |
735 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
736 |
|
int i = ((Integer)(it.next())).intValue(); |
737 |
< |
assertEquals("items should come out in order", ++k, i); |
737 |
> |
assertEquals(++k, i); |
738 |
|
} |
739 |
< |
|
618 |
< |
assertEquals("should go through 3 elements", 3, k); |
739 |
> |
assertEquals(3, k); |
740 |
|
} |
741 |
|
|
742 |
+ |
/** |
743 |
+ |
* |
744 |
+ |
*/ |
745 |
|
public void testWeaklyConsistentIteration () { |
622 |
– |
|
746 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
747 |
< |
|
748 |
< |
q.add(new Integer(1)); |
749 |
< |
q.add(new Integer(2)); |
627 |
< |
q.add(new Integer(3)); |
628 |
< |
|
747 |
> |
q.add(one); |
748 |
> |
q.add(two); |
749 |
> |
q.add(three); |
750 |
|
try { |
751 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
752 |
|
q.remove(); |
754 |
|
} |
755 |
|
} |
756 |
|
catch (ConcurrentModificationException e) { |
757 |
< |
fail("weakly consistent iterator; should not get CME"); |
757 |
> |
unexpectedException(); |
758 |
|
} |
759 |
|
|
760 |
< |
assertEquals("queue should be empty again", 0, q.size()); |
760 |
> |
assertEquals(0, q.size()); |
761 |
|
} |
762 |
|
|
763 |
|
|
764 |
< |
public void testToString(){ |
765 |
< |
ArrayBlockingQueue q = fullQueue(N); |
764 |
> |
/** |
765 |
> |
* |
766 |
> |
*/ |
767 |
> |
public void testToString() { |
768 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
769 |
|
String s = q.toString(); |
770 |
< |
for (int i = 0; i < N; ++i) { |
770 |
> |
for (int i = 0; i < SIZE; ++i) { |
771 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
772 |
|
} |
773 |
|
} |
774 |
|
|
775 |
|
|
776 |
+ |
/** |
777 |
+ |
* |
778 |
+ |
*/ |
779 |
|
public void testOfferInExecutor() { |
780 |
|
|
781 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
782 |
|
|
783 |
< |
q.add(new Integer(1)); |
784 |
< |
q.add(new Integer(2)); |
783 |
> |
q.add(one); |
784 |
> |
q.add(two); |
785 |
|
|
786 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
787 |
|
|
788 |
|
executor.execute(new Runnable() { |
789 |
|
public void run() { |
790 |
< |
assertFalse("offer should be rejected", q.offer(new Integer(3))); |
790 |
> |
threadAssertFalse(q.offer(three)); |
791 |
|
try { |
792 |
< |
assertTrue("offer should be accepted", q.offer(new Integer(3), MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); |
793 |
< |
assertEquals(0, q.remainingCapacity()); |
792 |
> |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
793 |
> |
threadAssertEquals(0, q.remainingCapacity()); |
794 |
|
} |
795 |
|
catch (InterruptedException e) { |
796 |
< |
fail("should not be interrupted"); |
796 |
> |
threadUnexpectedException(); |
797 |
|
} |
798 |
|
} |
799 |
|
}); |
801 |
|
executor.execute(new Runnable() { |
802 |
|
public void run() { |
803 |
|
try { |
804 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
805 |
< |
assertEquals("first item in queue should be 1", new Integer(1), q.take()); |
804 |
> |
Thread.sleep(SMALL_DELAY_MS); |
805 |
> |
threadAssertEquals(one, q.take()); |
806 |
|
} |
807 |
|
catch (InterruptedException e) { |
808 |
< |
fail("should not be interrupted"); |
808 |
> |
threadUnexpectedException(); |
809 |
|
} |
810 |
|
} |
811 |
|
}); |
812 |
|
|
813 |
< |
executor.shutdown(); |
813 |
> |
joinPool(executor); |
814 |
|
|
815 |
|
} |
816 |
|
|
817 |
+ |
/** |
818 |
+ |
* |
819 |
+ |
*/ |
820 |
|
public void testPollInExecutor() { |
821 |
|
|
822 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
825 |
|
|
826 |
|
executor.execute(new Runnable() { |
827 |
|
public void run() { |
828 |
< |
assertNull("poll should fail", q.poll()); |
828 |
> |
threadAssertNull(q.poll()); |
829 |
|
try { |
830 |
< |
assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); |
831 |
< |
assertTrue(q.isEmpty()); |
830 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
831 |
> |
threadAssertTrue(q.isEmpty()); |
832 |
|
} |
833 |
|
catch (InterruptedException e) { |
834 |
< |
fail("should not be interrupted"); |
834 |
> |
threadUnexpectedException(); |
835 |
|
} |
836 |
|
} |
837 |
|
}); |
839 |
|
executor.execute(new Runnable() { |
840 |
|
public void run() { |
841 |
|
try { |
842 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
843 |
< |
q.put(new Integer(1)); |
842 |
> |
Thread.sleep(SMALL_DELAY_MS); |
843 |
> |
q.put(one); |
844 |
|
} |
845 |
|
catch (InterruptedException e) { |
846 |
< |
fail("should not be interrupted"); |
846 |
> |
threadUnexpectedException(); |
847 |
|
} |
848 |
|
} |
849 |
|
}); |
850 |
|
|
851 |
< |
executor.shutdown(); |
851 |
> |
joinPool(executor); |
852 |
|
|
853 |
|
} |
854 |
|
|
855 |
+ |
/** |
856 |
+ |
* |
857 |
+ |
*/ |
858 |
+ |
public void testSerialization() { |
859 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
860 |
+ |
|
861 |
+ |
try { |
862 |
+ |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
863 |
+ |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
864 |
+ |
out.writeObject(q); |
865 |
+ |
out.close(); |
866 |
+ |
|
867 |
+ |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
868 |
+ |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
869 |
+ |
ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject(); |
870 |
+ |
assertEquals(q.size(), r.size()); |
871 |
+ |
while (!q.isEmpty()) |
872 |
+ |
assertEquals(q.remove(), r.remove()); |
873 |
+ |
} catch(Exception e){ |
874 |
+ |
unexpectedException(); |
875 |
+ |
} |
876 |
+ |
} |
877 |
+ |
|
878 |
+ |
|
879 |
|
} |