10 |
|
import java.util.concurrent.*; |
11 |
|
import java.io.*; |
12 |
|
|
13 |
< |
public class PriorityBlockingQueueTest extends TestCase { |
14 |
< |
|
15 |
< |
private static final int N = 10; |
16 |
< |
private static final long SHORT_DELAY_MS = 100; |
17 |
< |
private static final long MEDIUM_DELAY_MS = 1000; |
18 |
< |
private static final long LONG_DELAY_MS = 10000; |
19 |
< |
private static final int NOCAP = Integer.MAX_VALUE; |
20 |
< |
|
13 |
> |
public class PriorityBlockingQueueTest extends JSR166TestCase { |
14 |
|
public static void main(String[] args) { |
15 |
|
junit.textui.TestRunner.run (suite()); |
16 |
|
} |
19 |
|
return new TestSuite(PriorityBlockingQueueTest.class); |
20 |
|
} |
21 |
|
|
22 |
+ |
private static final int NOCAP = Integer.MAX_VALUE; |
23 |
+ |
|
24 |
+ |
/** Sample Comparator */ |
25 |
|
static class MyReverseComparator implements Comparator { |
26 |
|
public int compare(Object x, Object y) { |
27 |
|
int i = ((Integer)x).intValue(); |
37 |
|
* Create a queue of given size containing consecutive |
38 |
|
* Integers 0 ... n. |
39 |
|
*/ |
40 |
< |
private PriorityBlockingQueue fullQueue(int n) { |
40 |
> |
private PriorityBlockingQueue populatedQueue(int n) { |
41 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(n); |
42 |
|
assertTrue(q.isEmpty()); |
43 |
|
for(int i = n-1; i >= 0; i-=2) |
50 |
|
return q; |
51 |
|
} |
52 |
|
|
53 |
< |
public void testConstructor1(){ |
54 |
< |
assertEquals(NOCAP, new PriorityBlockingQueue(N).remainingCapacity()); |
53 |
> |
/** |
54 |
> |
* |
55 |
> |
*/ |
56 |
> |
public void testConstructor1() { |
57 |
> |
assertEquals(NOCAP, new PriorityBlockingQueue(SIZE).remainingCapacity()); |
58 |
|
} |
59 |
|
|
60 |
< |
public void testConstructor2(){ |
60 |
> |
/** |
61 |
> |
* |
62 |
> |
*/ |
63 |
> |
public void testConstructor2() { |
64 |
|
try { |
65 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(0); |
66 |
< |
fail("Cannot make zero-sized"); |
66 |
> |
shouldThrow(); |
67 |
|
} |
68 |
|
catch (IllegalArgumentException success) {} |
69 |
|
} |
70 |
|
|
71 |
< |
public void testConstructor3(){ |
71 |
> |
/** |
72 |
> |
* |
73 |
> |
*/ |
74 |
> |
public void testConstructor3() { |
75 |
|
|
76 |
|
try { |
77 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(null); |
78 |
< |
fail("Cannot make from null collection"); |
78 |
> |
shouldThrow(); |
79 |
|
} |
80 |
|
catch (NullPointerException success) {} |
81 |
|
} |
82 |
|
|
83 |
< |
public void testConstructor4(){ |
83 |
> |
/** |
84 |
> |
* |
85 |
> |
*/ |
86 |
> |
public void testConstructor4() { |
87 |
|
try { |
88 |
< |
Integer[] ints = new Integer[N]; |
88 |
> |
Integer[] ints = new Integer[SIZE]; |
89 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
90 |
< |
fail("Cannot make with null elements"); |
90 |
> |
shouldThrow(); |
91 |
|
} |
92 |
|
catch (NullPointerException success) {} |
93 |
|
} |
94 |
|
|
95 |
< |
public void testConstructor5(){ |
95 |
> |
/** |
96 |
> |
* |
97 |
> |
*/ |
98 |
> |
public void testConstructor5() { |
99 |
|
try { |
100 |
< |
Integer[] ints = new Integer[N]; |
101 |
< |
for (int i = 0; i < N-1; ++i) |
100 |
> |
Integer[] ints = new Integer[SIZE]; |
101 |
> |
for (int i = 0; i < SIZE-1; ++i) |
102 |
|
ints[i] = new Integer(i); |
103 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
104 |
< |
fail("Cannot make with null elements"); |
104 |
> |
shouldThrow(); |
105 |
|
} |
106 |
|
catch (NullPointerException success) {} |
107 |
|
} |
108 |
|
|
109 |
< |
public void testConstructor6(){ |
109 |
> |
/** |
110 |
> |
* |
111 |
> |
*/ |
112 |
> |
public void testConstructor6() { |
113 |
|
try { |
114 |
< |
Integer[] ints = new Integer[N]; |
115 |
< |
for (int i = 0; i < N; ++i) |
114 |
> |
Integer[] ints = new Integer[SIZE]; |
115 |
> |
for (int i = 0; i < SIZE; ++i) |
116 |
|
ints[i] = new Integer(i); |
117 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
118 |
< |
for (int i = 0; i < N; ++i) |
118 |
> |
for (int i = 0; i < SIZE; ++i) |
119 |
|
assertEquals(ints[i], q.poll()); |
120 |
|
} |
121 |
|
finally {} |
122 |
|
} |
123 |
|
|
124 |
< |
public void testConstructor7(){ |
124 |
> |
/** |
125 |
> |
* |
126 |
> |
*/ |
127 |
> |
public void testConstructor7() { |
128 |
|
try { |
129 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N, new MyReverseComparator()); |
130 |
< |
Integer[] ints = new Integer[N]; |
131 |
< |
for (int i = 0; i < N; ++i) |
129 |
> |
MyReverseComparator cmp = new MyReverseComparator(); |
130 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp); |
131 |
> |
assertEquals(cmp, q.comparator()); |
132 |
> |
Integer[] ints = new Integer[SIZE]; |
133 |
> |
for (int i = 0; i < SIZE; ++i) |
134 |
|
ints[i] = new Integer(i); |
135 |
|
q.addAll(Arrays.asList(ints)); |
136 |
< |
for (int i = N-1; i >= 0; --i) |
136 |
> |
for (int i = SIZE-1; i >= 0; --i) |
137 |
|
assertEquals(ints[i], q.poll()); |
138 |
|
} |
139 |
|
finally {} |
140 |
|
} |
141 |
|
|
142 |
+ |
/** |
143 |
+ |
* |
144 |
+ |
*/ |
145 |
|
public void testEmpty() { |
146 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
147 |
|
assertTrue(q.isEmpty()); |
154 |
|
assertTrue(q.isEmpty()); |
155 |
|
} |
156 |
|
|
157 |
< |
public void testRemainingCapacity(){ |
158 |
< |
PriorityBlockingQueue q = fullQueue(N); |
159 |
< |
for (int i = 0; i < N; ++i) { |
157 |
> |
/** |
158 |
> |
* |
159 |
> |
*/ |
160 |
> |
public void testRemainingCapacity() { |
161 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
162 |
> |
for (int i = 0; i < SIZE; ++i) { |
163 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
164 |
< |
assertEquals(N-i, q.size()); |
164 |
> |
assertEquals(SIZE-i, q.size()); |
165 |
|
q.remove(); |
166 |
|
} |
167 |
< |
for (int i = 0; i < N; ++i) { |
167 |
> |
for (int i = 0; i < SIZE; ++i) { |
168 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
169 |
|
assertEquals(i, q.size()); |
170 |
|
q.add(new Integer(i)); |
171 |
|
} |
172 |
|
} |
173 |
|
|
174 |
< |
public void testOfferNull(){ |
174 |
> |
/** |
175 |
> |
* |
176 |
> |
*/ |
177 |
> |
public void testOfferNull() { |
178 |
|
try { |
179 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
180 |
|
q.offer(null); |
181 |
< |
fail("should throw NPE"); |
181 |
> |
shouldThrow(); |
182 |
|
} catch (NullPointerException success) { } |
183 |
|
} |
184 |
|
|
185 |
+ |
/** |
186 |
+ |
* |
187 |
+ |
*/ |
188 |
|
public void testOffer() { |
189 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
190 |
|
assertTrue(q.offer(new Integer(0))); |
191 |
|
assertTrue(q.offer(new Integer(1))); |
192 |
|
} |
193 |
|
|
194 |
+ |
/** |
195 |
+ |
* |
196 |
+ |
*/ |
197 |
|
public void testOfferNonComparable() { |
198 |
|
try { |
199 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
200 |
|
q.offer(new Object()); |
201 |
|
q.offer(new Object()); |
202 |
|
q.offer(new Object()); |
203 |
< |
fail("should throw CCE"); |
203 |
> |
shouldThrow(); |
204 |
|
} |
205 |
|
catch(ClassCastException success) {} |
206 |
|
} |
207 |
|
|
208 |
< |
public void testAdd(){ |
209 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
210 |
< |
for (int i = 0; i < N; ++i) { |
208 |
> |
/** |
209 |
> |
* |
210 |
> |
*/ |
211 |
> |
public void testAdd() { |
212 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
213 |
> |
for (int i = 0; i < SIZE; ++i) { |
214 |
|
assertEquals(i, q.size()); |
215 |
|
assertTrue(q.add(new Integer(i))); |
216 |
|
} |
217 |
|
} |
218 |
|
|
219 |
< |
public void testAddAll1(){ |
219 |
> |
/** |
220 |
> |
* |
221 |
> |
*/ |
222 |
> |
public void testAddAll1() { |
223 |
|
try { |
224 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
225 |
|
q.addAll(null); |
226 |
< |
fail("Cannot add null collection"); |
226 |
> |
shouldThrow(); |
227 |
|
} |
228 |
|
catch (NullPointerException success) {} |
229 |
|
} |
230 |
< |
public void testAddAll2(){ |
230 |
> |
/** |
231 |
> |
* |
232 |
> |
*/ |
233 |
> |
public void testAddAll2() { |
234 |
|
try { |
235 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
236 |
< |
Integer[] ints = new Integer[N]; |
235 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
236 |
> |
Integer[] ints = new Integer[SIZE]; |
237 |
|
q.addAll(Arrays.asList(ints)); |
238 |
< |
fail("Cannot add null elements"); |
238 |
> |
shouldThrow(); |
239 |
|
} |
240 |
|
catch (NullPointerException success) {} |
241 |
|
} |
242 |
< |
public void testAddAll3(){ |
242 |
> |
/** |
243 |
> |
* |
244 |
> |
*/ |
245 |
> |
public void testAddAll3() { |
246 |
|
try { |
247 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
248 |
< |
Integer[] ints = new Integer[N]; |
249 |
< |
for (int i = 0; i < N-1; ++i) |
247 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
248 |
> |
Integer[] ints = new Integer[SIZE]; |
249 |
> |
for (int i = 0; i < SIZE-1; ++i) |
250 |
|
ints[i] = new Integer(i); |
251 |
|
q.addAll(Arrays.asList(ints)); |
252 |
< |
fail("Cannot add null elements"); |
252 |
> |
shouldThrow(); |
253 |
|
} |
254 |
|
catch (NullPointerException success) {} |
255 |
|
} |
256 |
|
|
257 |
< |
public void testAddAll5(){ |
257 |
> |
/** |
258 |
> |
* |
259 |
> |
*/ |
260 |
> |
public void testAddAll5() { |
261 |
|
try { |
262 |
|
Integer[] empty = new Integer[0]; |
263 |
< |
Integer[] ints = new Integer[N]; |
264 |
< |
for (int i = N-1; i >= 0; --i) |
263 |
> |
Integer[] ints = new Integer[SIZE]; |
264 |
> |
for (int i = SIZE-1; i >= 0; --i) |
265 |
|
ints[i] = new Integer(i); |
266 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
266 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
267 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
268 |
|
assertTrue(q.addAll(Arrays.asList(ints))); |
269 |
< |
for (int i = 0; i < N; ++i) |
269 |
> |
for (int i = 0; i < SIZE; ++i) |
270 |
|
assertEquals(ints[i], q.poll()); |
271 |
|
} |
272 |
|
finally {} |
273 |
|
} |
274 |
|
|
275 |
+ |
/** |
276 |
+ |
* |
277 |
+ |
*/ |
278 |
|
public void testPutNull() { |
279 |
|
try { |
280 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
280 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
281 |
|
q.put(null); |
282 |
< |
fail("put should throw NPE"); |
282 |
> |
shouldThrow(); |
283 |
|
} |
284 |
|
catch (NullPointerException success){ |
285 |
|
} |
286 |
|
} |
287 |
|
|
288 |
+ |
/** |
289 |
+ |
* |
290 |
+ |
*/ |
291 |
|
public void testPut() { |
292 |
|
try { |
293 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
294 |
< |
for (int i = 0; i < N; ++i) { |
293 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
294 |
> |
for (int i = 0; i < SIZE; ++i) { |
295 |
|
Integer I = new Integer(i); |
296 |
|
q.put(I); |
297 |
|
assertTrue(q.contains(I)); |
298 |
|
} |
299 |
< |
assertEquals(N, q.size()); |
299 |
> |
assertEquals(SIZE, q.size()); |
300 |
|
} |
301 |
|
finally { |
302 |
|
} |
303 |
|
} |
304 |
|
|
305 |
+ |
/** |
306 |
+ |
* |
307 |
+ |
*/ |
308 |
|
public void testPutWithTake() { |
309 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
310 |
|
Thread t = new Thread(new Runnable() { |
311 |
< |
public void run(){ |
311 |
> |
public void run() { |
312 |
|
int added = 0; |
313 |
|
try { |
314 |
|
q.put(new Integer(0)); |
319 |
|
++added; |
320 |
|
q.put(new Integer(0)); |
321 |
|
++added; |
322 |
< |
assertTrue(added == 4); |
322 |
> |
threadAssertTrue(added == 4); |
323 |
|
} finally { |
324 |
|
} |
325 |
|
} |
331 |
|
t.interrupt(); |
332 |
|
t.join(); |
333 |
|
} catch (Exception e){ |
334 |
< |
fail("Unexpected exception"); |
334 |
> |
unexpectedException(); |
335 |
|
} |
336 |
|
} |
337 |
|
|
338 |
+ |
/** |
339 |
+ |
* |
340 |
+ |
*/ |
341 |
|
public void testTimedOffer() { |
342 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
343 |
|
Thread t = new Thread(new Runnable() { |
344 |
< |
public void run(){ |
344 |
> |
public void run() { |
345 |
|
try { |
346 |
|
q.put(new Integer(0)); |
347 |
|
q.put(new Integer(0)); |
348 |
< |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); |
349 |
< |
assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
348 |
> |
threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
349 |
> |
threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
350 |
|
} finally { } |
351 |
|
} |
352 |
|
}); |
353 |
|
|
354 |
|
try { |
355 |
|
t.start(); |
356 |
< |
Thread.sleep(SHORT_DELAY_MS); |
356 |
> |
Thread.sleep(SMALL_DELAY_MS); |
357 |
|
t.interrupt(); |
358 |
|
t.join(); |
359 |
|
} catch (Exception e){ |
360 |
< |
fail("Unexpected exception"); |
360 |
> |
unexpectedException(); |
361 |
|
} |
362 |
|
} |
363 |
|
|
364 |
< |
public void testTake(){ |
364 |
> |
/** |
365 |
> |
* |
366 |
> |
*/ |
367 |
> |
public void testTake() { |
368 |
|
try { |
369 |
< |
PriorityBlockingQueue q = fullQueue(N); |
370 |
< |
for (int i = 0; i < N; ++i) { |
369 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
370 |
> |
for (int i = 0; i < SIZE; ++i) { |
371 |
|
assertEquals(i, ((Integer)q.take()).intValue()); |
372 |
|
} |
373 |
|
} catch (InterruptedException e){ |
374 |
< |
fail("Unexpected exception"); |
374 |
> |
unexpectedException(); |
375 |
|
} |
376 |
|
} |
377 |
|
|
378 |
+ |
/** |
379 |
+ |
* |
380 |
+ |
*/ |
381 |
|
public void testTakeFromEmpty() { |
382 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
383 |
|
Thread t = new Thread(new Runnable() { |
384 |
< |
public void run(){ |
384 |
> |
public void run() { |
385 |
|
try { |
386 |
|
q.take(); |
387 |
< |
fail("Should block"); |
387 |
> |
threadShouldThrow(); |
388 |
|
} catch (InterruptedException success){ } |
389 |
|
} |
390 |
|
}); |
394 |
|
t.interrupt(); |
395 |
|
t.join(); |
396 |
|
} catch (Exception e){ |
397 |
< |
fail("Unexpected exception"); |
397 |
> |
unexpectedException(); |
398 |
|
} |
399 |
|
} |
400 |
|
|
401 |
< |
public void testBlockingTake(){ |
401 |
> |
/** |
402 |
> |
* |
403 |
> |
*/ |
404 |
> |
public void testBlockingTake() { |
405 |
|
Thread t = new Thread(new Runnable() { |
406 |
|
public void run() { |
407 |
|
try { |
408 |
< |
PriorityBlockingQueue q = fullQueue(N); |
409 |
< |
for (int i = 0; i < N; ++i) { |
410 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
408 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
409 |
> |
for (int i = 0; i < SIZE; ++i) { |
410 |
> |
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
411 |
|
} |
412 |
|
q.take(); |
413 |
< |
fail("take should block"); |
413 |
> |
threadShouldThrow(); |
414 |
|
} catch (InterruptedException success){ |
415 |
|
} |
416 |
|
}}); |
421 |
|
t.join(); |
422 |
|
} |
423 |
|
catch (InterruptedException ie) { |
424 |
< |
fail("Unexpected exception"); |
424 |
> |
unexpectedException(); |
425 |
|
} |
426 |
|
} |
427 |
|
|
428 |
|
|
429 |
< |
public void testPoll(){ |
430 |
< |
PriorityBlockingQueue q = fullQueue(N); |
431 |
< |
for (int i = 0; i < N; ++i) { |
429 |
> |
/** |
430 |
> |
* |
431 |
> |
*/ |
432 |
> |
public void testPoll() { |
433 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
434 |
> |
for (int i = 0; i < SIZE; ++i) { |
435 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
436 |
|
} |
437 |
|
assertNull(q.poll()); |
438 |
|
} |
439 |
|
|
440 |
+ |
/** |
441 |
+ |
* |
442 |
+ |
*/ |
443 |
|
public void testTimedPoll0() { |
444 |
|
try { |
445 |
< |
PriorityBlockingQueue q = fullQueue(N); |
446 |
< |
for (int i = 0; i < N; ++i) { |
445 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
446 |
> |
for (int i = 0; i < SIZE; ++i) { |
447 |
|
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
448 |
|
} |
449 |
|
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
450 |
|
} catch (InterruptedException e){ |
451 |
< |
fail("Unexpected exception"); |
451 |
> |
unexpectedException(); |
452 |
|
} |
453 |
|
} |
454 |
|
|
455 |
+ |
/** |
456 |
+ |
* |
457 |
+ |
*/ |
458 |
|
public void testTimedPoll() { |
459 |
|
try { |
460 |
< |
PriorityBlockingQueue q = fullQueue(N); |
461 |
< |
for (int i = 0; i < N; ++i) { |
460 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
461 |
> |
for (int i = 0; i < SIZE; ++i) { |
462 |
|
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
463 |
|
} |
464 |
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
465 |
|
} catch (InterruptedException e){ |
466 |
< |
fail("Unexpected exception"); |
466 |
> |
unexpectedException(); |
467 |
|
} |
468 |
|
} |
469 |
|
|
470 |
< |
public void testInterruptedTimedPoll(){ |
470 |
> |
/** |
471 |
> |
* |
472 |
> |
*/ |
473 |
> |
public void testInterruptedTimedPoll() { |
474 |
|
Thread t = new Thread(new Runnable() { |
475 |
|
public void run() { |
476 |
|
try { |
477 |
< |
PriorityBlockingQueue q = fullQueue(N); |
478 |
< |
for (int i = 0; i < N; ++i) { |
479 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
477 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
478 |
> |
for (int i = 0; i < SIZE; ++i) { |
479 |
> |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
480 |
|
} |
481 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
481 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
482 |
|
} catch (InterruptedException success){ |
483 |
|
} |
484 |
|
}}); |
489 |
|
t.join(); |
490 |
|
} |
491 |
|
catch (InterruptedException ie) { |
492 |
< |
fail("Unexpected exception"); |
492 |
> |
unexpectedException(); |
493 |
|
} |
494 |
|
} |
495 |
|
|
496 |
< |
public void testTimedPollWithOffer(){ |
496 |
> |
/** |
497 |
> |
* |
498 |
> |
*/ |
499 |
> |
public void testTimedPollWithOffer() { |
500 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
501 |
|
Thread t = new Thread(new Runnable() { |
502 |
< |
public void run(){ |
502 |
> |
public void run() { |
503 |
|
try { |
504 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
504 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
505 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
506 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
507 |
< |
fail("Should block"); |
507 |
> |
threadShouldThrow(); |
508 |
|
} catch (InterruptedException success) { } |
509 |
|
} |
510 |
|
}); |
511 |
|
try { |
512 |
|
t.start(); |
513 |
< |
Thread.sleep(SHORT_DELAY_MS * 2); |
513 |
> |
Thread.sleep(SMALL_DELAY_MS); |
514 |
|
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
515 |
|
t.interrupt(); |
516 |
|
t.join(); |
517 |
|
} catch (Exception e){ |
518 |
< |
fail("Unexpected exception"); |
518 |
> |
unexpectedException(); |
519 |
|
} |
520 |
|
} |
521 |
|
|
522 |
|
|
523 |
< |
public void testPeek(){ |
524 |
< |
PriorityBlockingQueue q = fullQueue(N); |
525 |
< |
for (int i = 0; i < N; ++i) { |
523 |
> |
/** |
524 |
> |
* |
525 |
> |
*/ |
526 |
> |
public void testPeek() { |
527 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
528 |
> |
for (int i = 0; i < SIZE; ++i) { |
529 |
|
assertEquals(i, ((Integer)q.peek()).intValue()); |
530 |
|
q.poll(); |
531 |
|
assertTrue(q.peek() == null || |
534 |
|
assertNull(q.peek()); |
535 |
|
} |
536 |
|
|
537 |
< |
public void testElement(){ |
538 |
< |
PriorityBlockingQueue q = fullQueue(N); |
539 |
< |
for (int i = 0; i < N; ++i) { |
537 |
> |
/** |
538 |
> |
* |
539 |
> |
*/ |
540 |
> |
public void testElement() { |
541 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
542 |
> |
for (int i = 0; i < SIZE; ++i) { |
543 |
|
assertEquals(i, ((Integer)q.element()).intValue()); |
544 |
|
q.poll(); |
545 |
|
} |
546 |
|
try { |
547 |
|
q.element(); |
548 |
< |
fail("no such element"); |
548 |
> |
shouldThrow(); |
549 |
|
} |
550 |
|
catch (NoSuchElementException success) {} |
551 |
|
} |
552 |
|
|
553 |
< |
public void testRemove(){ |
554 |
< |
PriorityBlockingQueue q = fullQueue(N); |
555 |
< |
for (int i = 0; i < N; ++i) { |
553 |
> |
/** |
554 |
> |
* |
555 |
> |
*/ |
556 |
> |
public void testRemove() { |
557 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
558 |
> |
for (int i = 0; i < SIZE; ++i) { |
559 |
|
assertEquals(i, ((Integer)q.remove()).intValue()); |
560 |
|
} |
561 |
|
try { |
562 |
|
q.remove(); |
563 |
< |
fail("remove should throw"); |
563 |
> |
shouldThrow(); |
564 |
|
} catch (NoSuchElementException success){ |
565 |
|
} |
566 |
|
} |
567 |
|
|
568 |
< |
public void testRemoveElement(){ |
569 |
< |
PriorityBlockingQueue q = fullQueue(N); |
570 |
< |
for (int i = 1; i < N; i+=2) { |
568 |
> |
/** |
569 |
> |
* |
570 |
> |
*/ |
571 |
> |
public void testRemoveElement() { |
572 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
573 |
> |
for (int i = 1; i < SIZE; i+=2) { |
574 |
|
assertTrue(q.remove(new Integer(i))); |
575 |
|
} |
576 |
< |
for (int i = 0; i < N; i+=2) { |
576 |
> |
for (int i = 0; i < SIZE; i+=2) { |
577 |
|
assertTrue(q.remove(new Integer(i))); |
578 |
|
assertFalse(q.remove(new Integer(i+1))); |
579 |
|
} |
580 |
|
assertTrue(q.isEmpty()); |
581 |
|
} |
582 |
|
|
583 |
< |
public void testContains(){ |
584 |
< |
PriorityBlockingQueue q = fullQueue(N); |
585 |
< |
for (int i = 0; i < N; ++i) { |
583 |
> |
/** |
584 |
> |
* |
585 |
> |
*/ |
586 |
> |
public void testContains() { |
587 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
588 |
> |
for (int i = 0; i < SIZE; ++i) { |
589 |
|
assertTrue(q.contains(new Integer(i))); |
590 |
|
q.poll(); |
591 |
|
assertFalse(q.contains(new Integer(i))); |
592 |
|
} |
593 |
|
} |
594 |
|
|
595 |
< |
public void testClear(){ |
596 |
< |
PriorityBlockingQueue q = fullQueue(N); |
595 |
> |
/** |
596 |
> |
* |
597 |
> |
*/ |
598 |
> |
public void testClear() { |
599 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
600 |
|
q.clear(); |
601 |
|
assertTrue(q.isEmpty()); |
602 |
|
assertEquals(0, q.size()); |
607 |
|
assertTrue(q.isEmpty()); |
608 |
|
} |
609 |
|
|
610 |
< |
public void testContainsAll(){ |
611 |
< |
PriorityBlockingQueue q = fullQueue(N); |
612 |
< |
PriorityBlockingQueue p = new PriorityBlockingQueue(N); |
613 |
< |
for (int i = 0; i < N; ++i) { |
610 |
> |
/** |
611 |
> |
* |
612 |
> |
*/ |
613 |
> |
public void testContainsAll() { |
614 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
615 |
> |
PriorityBlockingQueue p = new PriorityBlockingQueue(SIZE); |
616 |
> |
for (int i = 0; i < SIZE; ++i) { |
617 |
|
assertTrue(q.containsAll(p)); |
618 |
|
assertFalse(p.containsAll(q)); |
619 |
|
p.add(new Integer(i)); |
621 |
|
assertTrue(p.containsAll(q)); |
622 |
|
} |
623 |
|
|
624 |
< |
public void testRetainAll(){ |
625 |
< |
PriorityBlockingQueue q = fullQueue(N); |
626 |
< |
PriorityBlockingQueue p = fullQueue(N); |
627 |
< |
for (int i = 0; i < N; ++i) { |
624 |
> |
/** |
625 |
> |
* |
626 |
> |
*/ |
627 |
> |
public void testRetainAll() { |
628 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
629 |
> |
PriorityBlockingQueue p = populatedQueue(SIZE); |
630 |
> |
for (int i = 0; i < SIZE; ++i) { |
631 |
|
boolean changed = q.retainAll(p); |
632 |
|
if (i == 0) |
633 |
|
assertFalse(changed); |
635 |
|
assertTrue(changed); |
636 |
|
|
637 |
|
assertTrue(q.containsAll(p)); |
638 |
< |
assertEquals(N-i, q.size()); |
638 |
> |
assertEquals(SIZE-i, q.size()); |
639 |
|
p.remove(); |
640 |
|
} |
641 |
|
} |
642 |
|
|
643 |
< |
public void testRemoveAll(){ |
644 |
< |
for (int i = 1; i < N; ++i) { |
645 |
< |
PriorityBlockingQueue q = fullQueue(N); |
646 |
< |
PriorityBlockingQueue p = fullQueue(i); |
643 |
> |
/** |
644 |
> |
* |
645 |
> |
*/ |
646 |
> |
public void testRemoveAll() { |
647 |
> |
for (int i = 1; i < SIZE; ++i) { |
648 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
649 |
> |
PriorityBlockingQueue p = populatedQueue(i); |
650 |
|
assertTrue(q.removeAll(p)); |
651 |
< |
assertEquals(N-i, q.size()); |
651 |
> |
assertEquals(SIZE-i, q.size()); |
652 |
|
for (int j = 0; j < i; ++j) { |
653 |
|
Integer I = (Integer)(p.remove()); |
654 |
|
assertFalse(q.contains(I)); |
656 |
|
} |
657 |
|
} |
658 |
|
|
659 |
< |
public void testToArray(){ |
660 |
< |
PriorityBlockingQueue q = fullQueue(N); |
659 |
> |
/** |
660 |
> |
* |
661 |
> |
*/ |
662 |
> |
public void testToArray() { |
663 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
664 |
|
Object[] o = q.toArray(); |
665 |
|
Arrays.sort(o); |
666 |
|
try { |
667 |
|
for(int i = 0; i < o.length; i++) |
668 |
|
assertEquals(o[i], q.take()); |
669 |
|
} catch (InterruptedException e){ |
670 |
< |
fail("Unexpected exception"); |
670 |
> |
unexpectedException(); |
671 |
|
} |
672 |
|
} |
673 |
|
|
674 |
< |
public void testToArray2(){ |
675 |
< |
PriorityBlockingQueue q = fullQueue(N); |
676 |
< |
Integer[] ints = new Integer[N]; |
674 |
> |
/** |
675 |
> |
* |
676 |
> |
*/ |
677 |
> |
public void testToArray2() { |
678 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
679 |
> |
Integer[] ints = new Integer[SIZE]; |
680 |
|
ints = (Integer[])q.toArray(ints); |
681 |
|
Arrays.sort(ints); |
682 |
|
try { |
683 |
|
for(int i = 0; i < ints.length; i++) |
684 |
|
assertEquals(ints[i], q.take()); |
685 |
|
} catch (InterruptedException e){ |
686 |
< |
fail("Unexpected exception"); |
686 |
> |
unexpectedException(); |
687 |
|
} |
688 |
|
} |
689 |
|
|
690 |
< |
public void testIterator(){ |
691 |
< |
PriorityBlockingQueue q = fullQueue(N); |
690 |
> |
/** |
691 |
> |
* |
692 |
> |
*/ |
693 |
> |
public void testIterator() { |
694 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
695 |
|
int i = 0; |
696 |
|
Iterator it = q.iterator(); |
697 |
|
while(it.hasNext()) { |
698 |
|
assertTrue(q.contains(it.next())); |
699 |
|
++i; |
700 |
|
} |
701 |
< |
assertEquals(i, N); |
701 |
> |
assertEquals(i, SIZE); |
702 |
|
} |
703 |
|
|
704 |
+ |
/** |
705 |
+ |
* |
706 |
+ |
*/ |
707 |
|
public void testIteratorRemove () { |
584 |
– |
|
708 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(3); |
586 |
– |
|
709 |
|
q.add(new Integer(2)); |
710 |
|
q.add(new Integer(1)); |
711 |
|
q.add(new Integer(3)); |
721 |
|
} |
722 |
|
|
723 |
|
|
724 |
< |
public void testToString(){ |
725 |
< |
PriorityBlockingQueue q = fullQueue(N); |
724 |
> |
/** |
725 |
> |
* |
726 |
> |
*/ |
727 |
> |
public void testToString() { |
728 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
729 |
|
String s = q.toString(); |
730 |
< |
for (int i = 0; i < N; ++i) { |
730 |
> |
for (int i = 0; i < SIZE; ++i) { |
731 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
732 |
|
} |
733 |
|
} |
734 |
|
|
735 |
+ |
/** |
736 |
+ |
* |
737 |
+ |
*/ |
738 |
|
public void testPollInExecutor() { |
739 |
|
|
740 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
743 |
|
|
744 |
|
executor.execute(new Runnable() { |
745 |
|
public void run() { |
746 |
< |
assertNull("poll should fail", q.poll()); |
746 |
> |
threadAssertNull(q.poll()); |
747 |
|
try { |
748 |
< |
assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); |
749 |
< |
assertTrue(q.isEmpty()); |
748 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
749 |
> |
threadAssertTrue(q.isEmpty()); |
750 |
|
} |
751 |
|
catch (InterruptedException e) { |
752 |
< |
fail("should not be interrupted"); |
752 |
> |
threadUnexpectedException(); |
753 |
|
} |
754 |
|
} |
755 |
|
}); |
757 |
|
executor.execute(new Runnable() { |
758 |
|
public void run() { |
759 |
|
try { |
760 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
760 |
> |
Thread.sleep(SMALL_DELAY_MS); |
761 |
|
q.put(new Integer(1)); |
762 |
|
} |
763 |
|
catch (InterruptedException e) { |
764 |
< |
fail("should not be interrupted"); |
764 |
> |
threadUnexpectedException(); |
765 |
|
} |
766 |
|
} |
767 |
|
}); |
768 |
|
|
769 |
< |
executor.shutdown(); |
769 |
> |
joinPool(executor); |
770 |
|
|
771 |
|
} |
772 |
|
|
773 |
+ |
/** |
774 |
+ |
* |
775 |
+ |
*/ |
776 |
|
public void testSerialization() { |
777 |
< |
PriorityBlockingQueue q = fullQueue(N); |
777 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
778 |
|
try { |
779 |
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
780 |
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
788 |
|
while (!q.isEmpty()) |
789 |
|
assertEquals(q.remove(), r.remove()); |
790 |
|
} catch(Exception e){ |
791 |
< |
fail("unexpected exception"); |
791 |
> |
unexpectedException(); |
792 |
|
} |
793 |
|
} |
794 |
|
|