1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
|
import java.io.*; |
14 |
|
|
15 |
|
public class PriorityBlockingQueueTest extends JSR166TestCase { |
16 |
+ |
|
17 |
+ |
public static class Generic extends BlockingQueueTest { |
18 |
+ |
protected BlockingQueue emptyCollection() { |
19 |
+ |
return new PriorityBlockingQueue(); |
20 |
+ |
} |
21 |
+ |
} |
22 |
+ |
|
23 |
+ |
public static class InitialCapacity extends BlockingQueueTest { |
24 |
+ |
protected BlockingQueue emptyCollection() { |
25 |
+ |
return new PriorityBlockingQueue(20); |
26 |
+ |
} |
27 |
+ |
} |
28 |
+ |
|
29 |
|
public static void main(String[] args) { |
30 |
< |
junit.textui.TestRunner.run (suite()); |
30 |
> |
junit.textui.TestRunner.run(suite()); |
31 |
|
} |
32 |
|
|
33 |
|
public static Test suite() { |
34 |
< |
return new TestSuite(PriorityBlockingQueueTest.class); |
34 |
> |
return newTestSuite(PriorityBlockingQueueTest.class, |
35 |
> |
new Generic().testSuite(), |
36 |
> |
new InitialCapacity().testSuite()); |
37 |
|
} |
38 |
|
|
39 |
|
private static final int NOCAP = Integer.MAX_VALUE; |
40 |
|
|
41 |
|
/** Sample Comparator */ |
42 |
< |
static class MyReverseComparator implements Comparator { |
42 |
> |
static class MyReverseComparator implements Comparator { |
43 |
|
public int compare(Object x, Object y) { |
44 |
< |
int i = ((Integer)x).intValue(); |
28 |
< |
int j = ((Integer)y).intValue(); |
29 |
< |
if (i < j) return 1; |
30 |
< |
if (i > j) return -1; |
31 |
< |
return 0; |
44 |
> |
return ((Comparable)y).compareTo(x); |
45 |
|
} |
46 |
|
} |
47 |
|
|
35 |
– |
|
48 |
|
/** |
49 |
|
* Create a queue of given size containing consecutive |
50 |
|
* Integers 0 ... n. |
52 |
|
private PriorityBlockingQueue populatedQueue(int n) { |
53 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(n); |
54 |
|
assertTrue(q.isEmpty()); |
55 |
< |
for(int i = n-1; i >= 0; i-=2) |
56 |
< |
assertTrue(q.offer(new Integer(i))); |
57 |
< |
for(int i = (n & 1); i < n; i+=2) |
58 |
< |
assertTrue(q.offer(new Integer(i))); |
55 |
> |
for (int i = n-1; i >= 0; i-=2) |
56 |
> |
assertTrue(q.offer(new Integer(i))); |
57 |
> |
for (int i = (n & 1); i < n; i+=2) |
58 |
> |
assertTrue(q.offer(new Integer(i))); |
59 |
|
assertFalse(q.isEmpty()); |
60 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
61 |
< |
assertEquals(n, q.size()); |
61 |
> |
assertEquals(n, q.size()); |
62 |
|
return q; |
63 |
|
} |
64 |
< |
|
65 |
< |
public void testConstructor1(){ |
64 |
> |
|
65 |
> |
/** |
66 |
> |
* A new queue has unbounded capacity |
67 |
> |
*/ |
68 |
> |
public void testConstructor1() { |
69 |
|
assertEquals(NOCAP, new PriorityBlockingQueue(SIZE).remainingCapacity()); |
70 |
|
} |
71 |
|
|
72 |
< |
public void testConstructor2(){ |
72 |
> |
/** |
73 |
> |
* Constructor throws IAE if capacity argument nonpositive |
74 |
> |
*/ |
75 |
> |
public void testConstructor2() { |
76 |
|
try { |
77 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(0); |
78 |
< |
fail("Cannot make zero-sized"); |
79 |
< |
} |
62 |
< |
catch (IllegalArgumentException success) {} |
78 |
> |
shouldThrow(); |
79 |
> |
} catch (IllegalArgumentException success) {} |
80 |
|
} |
81 |
|
|
82 |
< |
public void testConstructor3(){ |
83 |
< |
|
82 |
> |
/** |
83 |
> |
* Initializing from null Collection throws NPE |
84 |
> |
*/ |
85 |
> |
public void testConstructor3() { |
86 |
|
try { |
87 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(null); |
88 |
< |
fail("Cannot make from null collection"); |
89 |
< |
} |
71 |
< |
catch (NullPointerException success) {} |
88 |
> |
shouldThrow(); |
89 |
> |
} catch (NullPointerException success) {} |
90 |
|
} |
91 |
|
|
92 |
< |
public void testConstructor4(){ |
92 |
> |
/** |
93 |
> |
* Initializing from Collection of null elements throws NPE |
94 |
> |
*/ |
95 |
> |
public void testConstructor4() { |
96 |
|
try { |
97 |
|
Integer[] ints = new Integer[SIZE]; |
98 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
99 |
< |
fail("Cannot make with null elements"); |
100 |
< |
} |
80 |
< |
catch (NullPointerException success) {} |
99 |
> |
shouldThrow(); |
100 |
> |
} catch (NullPointerException success) {} |
101 |
|
} |
102 |
|
|
103 |
< |
public void testConstructor5(){ |
103 |
> |
/** |
104 |
> |
* Initializing from Collection with some null elements throws NPE |
105 |
> |
*/ |
106 |
> |
public void testConstructor5() { |
107 |
|
try { |
108 |
|
Integer[] ints = new Integer[SIZE]; |
109 |
|
for (int i = 0; i < SIZE-1; ++i) |
110 |
|
ints[i] = new Integer(i); |
111 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
112 |
< |
fail("Cannot make with null elements"); |
113 |
< |
} |
91 |
< |
catch (NullPointerException success) {} |
112 |
> |
shouldThrow(); |
113 |
> |
} catch (NullPointerException success) {} |
114 |
|
} |
115 |
|
|
116 |
< |
public void testConstructor6(){ |
117 |
< |
try { |
118 |
< |
Integer[] ints = new Integer[SIZE]; |
119 |
< |
for (int i = 0; i < SIZE; ++i) |
120 |
< |
ints[i] = new Integer(i); |
121 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
122 |
< |
for (int i = 0; i < SIZE; ++i) |
123 |
< |
assertEquals(ints[i], q.poll()); |
124 |
< |
} |
125 |
< |
finally {} |
116 |
> |
/** |
117 |
> |
* Queue contains all elements of collection used to initialize |
118 |
> |
*/ |
119 |
> |
public void testConstructor6() { |
120 |
> |
Integer[] ints = new Integer[SIZE]; |
121 |
> |
for (int i = 0; i < SIZE; ++i) |
122 |
> |
ints[i] = new Integer(i); |
123 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
124 |
> |
for (int i = 0; i < SIZE; ++i) |
125 |
> |
assertEquals(ints[i], q.poll()); |
126 |
|
} |
127 |
|
|
128 |
< |
public void testConstructor7(){ |
129 |
< |
try { |
130 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, new MyReverseComparator()); |
131 |
< |
Integer[] ints = new Integer[SIZE]; |
132 |
< |
for (int i = 0; i < SIZE; ++i) |
133 |
< |
ints[i] = new Integer(i); |
134 |
< |
q.addAll(Arrays.asList(ints)); |
135 |
< |
for (int i = SIZE-1; i >= 0; --i) |
136 |
< |
assertEquals(ints[i], q.poll()); |
137 |
< |
} |
138 |
< |
finally {} |
128 |
> |
/** |
129 |
> |
* The comparator used in constructor is used |
130 |
> |
*/ |
131 |
> |
public void testConstructor7() { |
132 |
> |
MyReverseComparator cmp = new MyReverseComparator(); |
133 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp); |
134 |
> |
assertEquals(cmp, q.comparator()); |
135 |
> |
Integer[] ints = new Integer[SIZE]; |
136 |
> |
for (int i = 0; i < SIZE; ++i) |
137 |
> |
ints[i] = new Integer(i); |
138 |
> |
q.addAll(Arrays.asList(ints)); |
139 |
> |
for (int i = SIZE-1; i >= 0; --i) |
140 |
> |
assertEquals(ints[i], q.poll()); |
141 |
|
} |
142 |
|
|
143 |
+ |
/** |
144 |
+ |
* isEmpty is true before add, false after |
145 |
+ |
*/ |
146 |
|
public void testEmpty() { |
147 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
148 |
|
assertTrue(q.isEmpty()); |
149 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
150 |
< |
q.add(new Integer(1)); |
150 |
> |
q.add(one); |
151 |
|
assertFalse(q.isEmpty()); |
152 |
< |
q.add(new Integer(2)); |
152 |
> |
q.add(two); |
153 |
|
q.remove(); |
154 |
|
q.remove(); |
155 |
|
assertTrue(q.isEmpty()); |
156 |
|
} |
157 |
|
|
158 |
< |
public void testRemainingCapacity(){ |
158 |
> |
/** |
159 |
> |
* remainingCapacity does not change when elements added or removed, |
160 |
> |
* but size does |
161 |
> |
*/ |
162 |
> |
public void testRemainingCapacity() { |
163 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
164 |
|
for (int i = 0; i < SIZE; ++i) { |
165 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
173 |
|
} |
174 |
|
} |
175 |
|
|
176 |
< |
public void testOfferNull(){ |
177 |
< |
try { |
176 |
> |
/** |
177 |
> |
* offer(null) throws NPE |
178 |
> |
*/ |
179 |
> |
public void testOfferNull() { |
180 |
> |
try { |
181 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
182 |
|
q.offer(null); |
183 |
< |
fail("should throw NPE"); |
184 |
< |
} catch (NullPointerException success) { } |
183 |
> |
shouldThrow(); |
184 |
> |
} catch (NullPointerException success) {} |
185 |
> |
} |
186 |
> |
|
187 |
> |
/** |
188 |
> |
* add(null) throws NPE |
189 |
> |
*/ |
190 |
> |
public void testAddNull() { |
191 |
> |
try { |
192 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
193 |
> |
q.add(null); |
194 |
> |
shouldThrow(); |
195 |
> |
} catch (NullPointerException success) {} |
196 |
|
} |
197 |
|
|
198 |
+ |
/** |
199 |
+ |
* Offer of comparable element succeeds |
200 |
+ |
*/ |
201 |
|
public void testOffer() { |
202 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
203 |
< |
assertTrue(q.offer(new Integer(0))); |
204 |
< |
assertTrue(q.offer(new Integer(1))); |
203 |
> |
assertTrue(q.offer(zero)); |
204 |
> |
assertTrue(q.offer(one)); |
205 |
|
} |
206 |
|
|
207 |
+ |
/** |
208 |
+ |
* Offer of non-Comparable throws CCE |
209 |
+ |
*/ |
210 |
|
public void testOfferNonComparable() { |
211 |
|
try { |
212 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
213 |
|
q.offer(new Object()); |
214 |
|
q.offer(new Object()); |
215 |
|
q.offer(new Object()); |
216 |
< |
fail("should throw CCE"); |
217 |
< |
} |
167 |
< |
catch(ClassCastException success) {} |
216 |
> |
shouldThrow(); |
217 |
> |
} catch (ClassCastException success) {} |
218 |
|
} |
219 |
|
|
220 |
< |
public void testAdd(){ |
220 |
> |
/** |
221 |
> |
* add of comparable succeeds |
222 |
> |
*/ |
223 |
> |
public void testAdd() { |
224 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
225 |
|
for (int i = 0; i < SIZE; ++i) { |
226 |
|
assertEquals(i, q.size()); |
228 |
|
} |
229 |
|
} |
230 |
|
|
231 |
< |
public void testAddAll1(){ |
231 |
> |
/** |
232 |
> |
* addAll(null) throws NPE |
233 |
> |
*/ |
234 |
> |
public void testAddAll1() { |
235 |
|
try { |
236 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
237 |
|
q.addAll(null); |
238 |
< |
fail("Cannot add null collection"); |
239 |
< |
} |
184 |
< |
catch (NullPointerException success) {} |
238 |
> |
shouldThrow(); |
239 |
> |
} catch (NullPointerException success) {} |
240 |
|
} |
241 |
< |
public void testAddAll2(){ |
241 |
> |
|
242 |
> |
/** |
243 |
> |
* addAll(this) throws IAE |
244 |
> |
*/ |
245 |
> |
public void testAddAllSelf() { |
246 |
> |
try { |
247 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
248 |
> |
q.addAll(q); |
249 |
> |
shouldThrow(); |
250 |
> |
} catch (IllegalArgumentException success) {} |
251 |
> |
} |
252 |
> |
|
253 |
> |
/** |
254 |
> |
* addAll of a collection with null elements throws NPE |
255 |
> |
*/ |
256 |
> |
public void testAddAll2() { |
257 |
|
try { |
258 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
259 |
|
Integer[] ints = new Integer[SIZE]; |
260 |
|
q.addAll(Arrays.asList(ints)); |
261 |
< |
fail("Cannot add null elements"); |
262 |
< |
} |
193 |
< |
catch (NullPointerException success) {} |
261 |
> |
shouldThrow(); |
262 |
> |
} catch (NullPointerException success) {} |
263 |
|
} |
264 |
< |
public void testAddAll3(){ |
264 |
> |
|
265 |
> |
/** |
266 |
> |
* addAll of a collection with any null elements throws NPE after |
267 |
> |
* possibly adding some elements |
268 |
> |
*/ |
269 |
> |
public void testAddAll3() { |
270 |
|
try { |
271 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
272 |
|
Integer[] ints = new Integer[SIZE]; |
273 |
|
for (int i = 0; i < SIZE-1; ++i) |
274 |
|
ints[i] = new Integer(i); |
275 |
|
q.addAll(Arrays.asList(ints)); |
276 |
< |
fail("Cannot add null elements"); |
277 |
< |
} |
204 |
< |
catch (NullPointerException success) {} |
276 |
> |
shouldThrow(); |
277 |
> |
} catch (NullPointerException success) {} |
278 |
|
} |
279 |
|
|
280 |
< |
public void testAddAll5(){ |
281 |
< |
try { |
282 |
< |
Integer[] empty = new Integer[0]; |
283 |
< |
Integer[] ints = new Integer[SIZE]; |
284 |
< |
for (int i = SIZE-1; i >= 0; --i) |
285 |
< |
ints[i] = new Integer(i); |
286 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
287 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
288 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
289 |
< |
for (int i = 0; i < SIZE; ++i) |
290 |
< |
assertEquals(ints[i], q.poll()); |
291 |
< |
} |
292 |
< |
finally {} |
280 |
> |
/** |
281 |
> |
* Queue contains all elements of successful addAll |
282 |
> |
*/ |
283 |
> |
public void testAddAll5() { |
284 |
> |
Integer[] empty = new Integer[0]; |
285 |
> |
Integer[] ints = new Integer[SIZE]; |
286 |
> |
for (int i = SIZE-1; i >= 0; --i) |
287 |
> |
ints[i] = new Integer(i); |
288 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
289 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
290 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
291 |
> |
for (int i = 0; i < SIZE; ++i) |
292 |
> |
assertEquals(ints[i], q.poll()); |
293 |
|
} |
294 |
|
|
295 |
+ |
/** |
296 |
+ |
* put(null) throws NPE |
297 |
+ |
*/ |
298 |
|
public void testPutNull() { |
299 |
< |
try { |
299 |
> |
try { |
300 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
301 |
|
q.put(null); |
302 |
< |
fail("put should throw NPE"); |
303 |
< |
} |
228 |
< |
catch (NullPointerException success){ |
229 |
< |
} |
302 |
> |
shouldThrow(); |
303 |
> |
} catch (NullPointerException success) {} |
304 |
|
} |
305 |
|
|
306 |
+ |
/** |
307 |
+ |
* all elements successfully put are contained |
308 |
+ |
*/ |
309 |
|
public void testPut() { |
310 |
< |
try { |
311 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
312 |
< |
for (int i = 0; i < SIZE; ++i) { |
313 |
< |
Integer I = new Integer(i); |
314 |
< |
q.put(I); |
238 |
< |
assertTrue(q.contains(I)); |
239 |
< |
} |
240 |
< |
assertEquals(SIZE, q.size()); |
310 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
311 |
> |
for (int i = 0; i < SIZE; ++i) { |
312 |
> |
Integer I = new Integer(i); |
313 |
> |
q.put(I); |
314 |
> |
assertTrue(q.contains(I)); |
315 |
|
} |
316 |
< |
finally { |
243 |
< |
} |
316 |
> |
assertEquals(SIZE, q.size()); |
317 |
|
} |
318 |
|
|
319 |
< |
public void testPutWithTake() { |
319 |
> |
/** |
320 |
> |
* put doesn't block waiting for take |
321 |
> |
*/ |
322 |
> |
public void testPutWithTake() throws InterruptedException { |
323 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
324 |
< |
Thread t = new Thread(new Runnable() { |
325 |
< |
public void run(){ |
326 |
< |
int added = 0; |
327 |
< |
try { |
328 |
< |
q.put(new Integer(0)); |
329 |
< |
++added; |
330 |
< |
q.put(new Integer(0)); |
331 |
< |
++added; |
332 |
< |
q.put(new Integer(0)); |
333 |
< |
++added; |
334 |
< |
q.put(new Integer(0)); |
335 |
< |
++added; |
336 |
< |
threadAssertTrue(added == 4); |
261 |
< |
} finally { |
262 |
< |
} |
263 |
< |
} |
264 |
< |
}); |
265 |
< |
try { |
266 |
< |
t.start(); |
267 |
< |
Thread.sleep(SHORT_DELAY_MS); |
268 |
< |
q.take(); |
269 |
< |
t.interrupt(); |
270 |
< |
t.join(); |
271 |
< |
} catch (Exception e){ |
272 |
< |
fail("Unexpected exception"); |
273 |
< |
} |
324 |
> |
final int size = 4; |
325 |
> |
Thread t = new Thread(new CheckedRunnable() { |
326 |
> |
public void realRun() { |
327 |
> |
for (int i = 0; i < size; i++) |
328 |
> |
q.put(new Integer(0)); |
329 |
> |
}}); |
330 |
> |
|
331 |
> |
t.start(); |
332 |
> |
Thread.sleep(SHORT_DELAY_MS); |
333 |
> |
assertEquals(q.size(), size); |
334 |
> |
q.take(); |
335 |
> |
t.interrupt(); |
336 |
> |
t.join(); |
337 |
|
} |
338 |
|
|
339 |
< |
public void testTimedOffer() { |
339 |
> |
/** |
340 |
> |
* timed offer does not time out |
341 |
> |
*/ |
342 |
> |
public void testTimedOffer() throws InterruptedException { |
343 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
344 |
< |
Thread t = new Thread(new Runnable() { |
345 |
< |
public void run(){ |
346 |
< |
try { |
347 |
< |
q.put(new Integer(0)); |
348 |
< |
q.put(new Integer(0)); |
349 |
< |
threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
350 |
< |
threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
351 |
< |
} finally { } |
352 |
< |
} |
353 |
< |
}); |
354 |
< |
|
355 |
< |
try { |
290 |
< |
t.start(); |
291 |
< |
Thread.sleep(SMALL_DELAY_MS); |
292 |
< |
t.interrupt(); |
293 |
< |
t.join(); |
294 |
< |
} catch (Exception e){ |
295 |
< |
fail("Unexpected exception"); |
296 |
< |
} |
344 |
> |
Thread t = new Thread(new CheckedRunnable() { |
345 |
> |
public void realRun() { |
346 |
> |
q.put(new Integer(0)); |
347 |
> |
q.put(new Integer(0)); |
348 |
> |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS)); |
349 |
> |
assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS)); |
350 |
> |
}}); |
351 |
> |
|
352 |
> |
t.start(); |
353 |
> |
Thread.sleep(SMALL_DELAY_MS); |
354 |
> |
t.interrupt(); |
355 |
> |
t.join(); |
356 |
|
} |
357 |
|
|
358 |
< |
public void testTake(){ |
359 |
< |
try { |
360 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
361 |
< |
for (int i = 0; i < SIZE; ++i) { |
362 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
363 |
< |
} |
364 |
< |
} catch (InterruptedException e){ |
365 |
< |
fail("Unexpected exception"); |
307 |
< |
} |
358 |
> |
/** |
359 |
> |
* take retrieves elements in priority order |
360 |
> |
*/ |
361 |
> |
public void testTake() throws InterruptedException { |
362 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
363 |
> |
for (int i = 0; i < SIZE; ++i) { |
364 |
> |
assertEquals(i, q.take()); |
365 |
> |
} |
366 |
|
} |
367 |
|
|
368 |
< |
public void testTakeFromEmpty() { |
368 |
> |
/** |
369 |
> |
* take blocks interruptibly when empty |
370 |
> |
*/ |
371 |
> |
public void testTakeFromEmpty() throws InterruptedException { |
372 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
373 |
< |
Thread t = new Thread(new Runnable() { |
374 |
< |
public void run(){ |
375 |
< |
try { |
376 |
< |
q.take(); |
377 |
< |
threadFail("Should block"); |
378 |
< |
} catch (InterruptedException success){ } |
379 |
< |
} |
380 |
< |
}); |
381 |
< |
try { |
321 |
< |
t.start(); |
322 |
< |
Thread.sleep(SHORT_DELAY_MS); |
323 |
< |
t.interrupt(); |
324 |
< |
t.join(); |
325 |
< |
} catch (Exception e){ |
326 |
< |
fail("Unexpected exception"); |
327 |
< |
} |
373 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
374 |
> |
public void realRun() throws InterruptedException { |
375 |
> |
q.take(); |
376 |
> |
}}); |
377 |
> |
|
378 |
> |
t.start(); |
379 |
> |
Thread.sleep(SHORT_DELAY_MS); |
380 |
> |
t.interrupt(); |
381 |
> |
t.join(); |
382 |
|
} |
383 |
|
|
384 |
< |
public void testBlockingTake(){ |
385 |
< |
Thread t = new Thread(new Runnable() { |
386 |
< |
public void run() { |
387 |
< |
try { |
388 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
389 |
< |
for (int i = 0; i < SIZE; ++i) { |
390 |
< |
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
391 |
< |
} |
392 |
< |
q.take(); |
393 |
< |
threadFail("take should block"); |
394 |
< |
} catch (InterruptedException success){ |
395 |
< |
} |
396 |
< |
}}); |
384 |
> |
/** |
385 |
> |
* Take removes existing elements until empty, then blocks interruptibly |
386 |
> |
*/ |
387 |
> |
public void testBlockingTake() throws InterruptedException { |
388 |
> |
final PriorityBlockingQueue q = populatedQueue(SIZE); |
389 |
> |
Thread t = new Thread(new CheckedRunnable() { |
390 |
> |
public void realRun() throws InterruptedException { |
391 |
> |
for (int i = 0; i < SIZE; ++i) { |
392 |
> |
assertEquals(i, q.take()); |
393 |
> |
} |
394 |
> |
try { |
395 |
> |
q.take(); |
396 |
> |
shouldThrow(); |
397 |
> |
} catch (InterruptedException success) {} |
398 |
> |
}}); |
399 |
> |
|
400 |
|
t.start(); |
401 |
< |
try { |
402 |
< |
Thread.sleep(SHORT_DELAY_MS); |
403 |
< |
t.interrupt(); |
347 |
< |
t.join(); |
348 |
< |
} |
349 |
< |
catch (InterruptedException ie) { |
350 |
< |
fail("Unexpected exception"); |
351 |
< |
} |
401 |
> |
Thread.sleep(SHORT_DELAY_MS); |
402 |
> |
t.interrupt(); |
403 |
> |
t.join(); |
404 |
|
} |
405 |
|
|
406 |
|
|
407 |
< |
public void testPoll(){ |
407 |
> |
/** |
408 |
> |
* poll succeeds unless empty |
409 |
> |
*/ |
410 |
> |
public void testPoll() { |
411 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
412 |
|
for (int i = 0; i < SIZE; ++i) { |
413 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
413 |
> |
assertEquals(i, q.poll()); |
414 |
|
} |
415 |
< |
assertNull(q.poll()); |
415 |
> |
assertNull(q.poll()); |
416 |
|
} |
417 |
|
|
418 |
< |
public void testTimedPoll0() { |
419 |
< |
try { |
420 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
421 |
< |
for (int i = 0; i < SIZE; ++i) { |
422 |
< |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
423 |
< |
} |
424 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
425 |
< |
} catch (InterruptedException e){ |
426 |
< |
fail("Unexpected exception"); |
372 |
< |
} |
418 |
> |
/** |
419 |
> |
* timed pool with zero timeout succeeds when non-empty, else times out |
420 |
> |
*/ |
421 |
> |
public void testTimedPoll0() throws InterruptedException { |
422 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
423 |
> |
for (int i = 0; i < SIZE; ++i) { |
424 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
425 |
> |
} |
426 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
427 |
|
} |
428 |
|
|
429 |
< |
public void testTimedPoll() { |
430 |
< |
try { |
431 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
432 |
< |
for (int i = 0; i < SIZE; ++i) { |
433 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
434 |
< |
} |
435 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
382 |
< |
} catch (InterruptedException e){ |
383 |
< |
fail("Unexpected exception"); |
384 |
< |
} |
385 |
< |
} |
386 |
< |
|
387 |
< |
public void testInterruptedTimedPoll(){ |
388 |
< |
Thread t = new Thread(new Runnable() { |
389 |
< |
public void run() { |
390 |
< |
try { |
391 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
392 |
< |
for (int i = 0; i < SIZE; ++i) { |
393 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
394 |
< |
} |
395 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
396 |
< |
} catch (InterruptedException success){ |
397 |
< |
} |
398 |
< |
}}); |
399 |
< |
t.start(); |
400 |
< |
try { |
401 |
< |
Thread.sleep(SHORT_DELAY_MS); |
402 |
< |
t.interrupt(); |
403 |
< |
t.join(); |
404 |
< |
} |
405 |
< |
catch (InterruptedException ie) { |
406 |
< |
fail("Unexpected exception"); |
429 |
> |
/** |
430 |
> |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
431 |
> |
*/ |
432 |
> |
public void testTimedPoll() throws InterruptedException { |
433 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
434 |
> |
for (int i = 0; i < SIZE; ++i) { |
435 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
436 |
|
} |
437 |
+ |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
438 |
|
} |
439 |
|
|
440 |
< |
public void testTimedPollWithOffer(){ |
441 |
< |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
442 |
< |
Thread t = new Thread(new Runnable() { |
443 |
< |
public void run(){ |
444 |
< |
try { |
445 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
446 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
447 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
448 |
< |
threadFail("Should block"); |
449 |
< |
} catch (InterruptedException success) { } |
440 |
> |
/** |
441 |
> |
* Interrupted timed poll throws InterruptedException instead of |
442 |
> |
* returning timeout status |
443 |
> |
*/ |
444 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
445 |
> |
Thread t = new Thread(new CheckedRunnable() { |
446 |
> |
public void realRun() throws InterruptedException { |
447 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
448 |
> |
for (int i = 0; i < SIZE; ++i) { |
449 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
450 |
|
} |
451 |
< |
}); |
452 |
< |
try { |
453 |
< |
t.start(); |
454 |
< |
Thread.sleep(SMALL_DELAY_MS); |
455 |
< |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
426 |
< |
t.interrupt(); |
427 |
< |
t.join(); |
428 |
< |
} catch (Exception e){ |
429 |
< |
fail("Unexpected exception"); |
430 |
< |
} |
431 |
< |
} |
451 |
> |
try { |
452 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
453 |
> |
shouldThrow(); |
454 |
> |
} catch (InterruptedException success) {} |
455 |
> |
}}); |
456 |
|
|
457 |
+ |
t.start(); |
458 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
459 |
+ |
t.interrupt(); |
460 |
+ |
t.join(); |
461 |
+ |
} |
462 |
|
|
463 |
< |
public void testPeek(){ |
463 |
> |
/** |
464 |
> |
* peek returns next element, or null if empty |
465 |
> |
*/ |
466 |
> |
public void testPeek() { |
467 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
468 |
|
for (int i = 0; i < SIZE; ++i) { |
469 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
470 |
< |
q.poll(); |
469 |
> |
assertEquals(i, q.peek()); |
470 |
> |
assertEquals(i, q.poll()); |
471 |
|
assertTrue(q.peek() == null || |
472 |
< |
i != ((Integer)q.peek()).intValue()); |
472 |
> |
!q.peek().equals(i)); |
473 |
|
} |
474 |
< |
assertNull(q.peek()); |
474 |
> |
assertNull(q.peek()); |
475 |
|
} |
476 |
|
|
477 |
< |
public void testElement(){ |
477 |
> |
/** |
478 |
> |
* element returns next element, or throws NSEE if empty |
479 |
> |
*/ |
480 |
> |
public void testElement() { |
481 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
482 |
|
for (int i = 0; i < SIZE; ++i) { |
483 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
484 |
< |
q.poll(); |
483 |
> |
assertEquals(i, q.element()); |
484 |
> |
assertEquals(i, q.poll()); |
485 |
|
} |
486 |
|
try { |
487 |
|
q.element(); |
488 |
< |
fail("no such element"); |
489 |
< |
} |
455 |
< |
catch (NoSuchElementException success) {} |
488 |
> |
shouldThrow(); |
489 |
> |
} catch (NoSuchElementException success) {} |
490 |
|
} |
491 |
|
|
492 |
< |
public void testRemove(){ |
492 |
> |
/** |
493 |
> |
* remove removes next element, or throws NSEE if empty |
494 |
> |
*/ |
495 |
> |
public void testRemove() { |
496 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
497 |
|
for (int i = 0; i < SIZE; ++i) { |
498 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
498 |
> |
assertEquals(i, q.remove()); |
499 |
|
} |
500 |
|
try { |
501 |
|
q.remove(); |
502 |
< |
fail("remove should throw"); |
503 |
< |
} catch (NoSuchElementException success){ |
467 |
< |
} |
502 |
> |
shouldThrow(); |
503 |
> |
} catch (NoSuchElementException success) {} |
504 |
|
} |
505 |
|
|
506 |
< |
public void testRemoveElement(){ |
506 |
> |
/** |
507 |
> |
* remove(x) removes x and returns true if present |
508 |
> |
*/ |
509 |
> |
public void testRemoveElement() { |
510 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
511 |
|
for (int i = 1; i < SIZE; i+=2) { |
512 |
|
assertTrue(q.remove(new Integer(i))); |
517 |
|
} |
518 |
|
assertTrue(q.isEmpty()); |
519 |
|
} |
520 |
< |
|
521 |
< |
public void testContains(){ |
520 |
> |
|
521 |
> |
/** |
522 |
> |
* contains(x) reports true when elements added but not yet removed |
523 |
> |
*/ |
524 |
> |
public void testContains() { |
525 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
526 |
|
for (int i = 0; i < SIZE; ++i) { |
527 |
|
assertTrue(q.contains(new Integer(i))); |
530 |
|
} |
531 |
|
} |
532 |
|
|
533 |
< |
public void testClear(){ |
533 |
> |
/** |
534 |
> |
* clear removes all elements |
535 |
> |
*/ |
536 |
> |
public void testClear() { |
537 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
538 |
|
q.clear(); |
539 |
|
assertTrue(q.isEmpty()); |
540 |
|
assertEquals(0, q.size()); |
541 |
< |
assertEquals(NOCAP, q.remainingCapacity()); |
497 |
< |
q.add(new Integer(1)); |
541 |
> |
q.add(one); |
542 |
|
assertFalse(q.isEmpty()); |
543 |
+ |
assertTrue(q.contains(one)); |
544 |
|
q.clear(); |
545 |
|
assertTrue(q.isEmpty()); |
546 |
|
} |
547 |
|
|
548 |
< |
public void testContainsAll(){ |
548 |
> |
/** |
549 |
> |
* containsAll(c) is true when c contains a subset of elements |
550 |
> |
*/ |
551 |
> |
public void testContainsAll() { |
552 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
553 |
|
PriorityBlockingQueue p = new PriorityBlockingQueue(SIZE); |
554 |
|
for (int i = 0; i < SIZE; ++i) { |
559 |
|
assertTrue(p.containsAll(q)); |
560 |
|
} |
561 |
|
|
562 |
< |
public void testRetainAll(){ |
562 |
> |
/** |
563 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
564 |
> |
*/ |
565 |
> |
public void testRetainAll() { |
566 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
567 |
|
PriorityBlockingQueue p = populatedQueue(SIZE); |
568 |
|
for (int i = 0; i < SIZE; ++i) { |
578 |
|
} |
579 |
|
} |
580 |
|
|
581 |
< |
public void testRemoveAll(){ |
581 |
> |
/** |
582 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
583 |
> |
*/ |
584 |
> |
public void testRemoveAll() { |
585 |
|
for (int i = 1; i < SIZE; ++i) { |
586 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
587 |
|
PriorityBlockingQueue p = populatedQueue(i); |
594 |
|
} |
595 |
|
} |
596 |
|
|
597 |
< |
public void testToArray(){ |
597 |
> |
/** |
598 |
> |
* toArray contains all elements |
599 |
> |
*/ |
600 |
> |
public void testToArray() throws InterruptedException { |
601 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
602 |
< |
Object[] o = q.toArray(); |
602 |
> |
Object[] o = q.toArray(); |
603 |
|
Arrays.sort(o); |
604 |
< |
try { |
605 |
< |
for(int i = 0; i < o.length; i++) |
549 |
< |
assertEquals(o[i], q.take()); |
550 |
< |
} catch (InterruptedException e){ |
551 |
< |
fail("Unexpected exception"); |
552 |
< |
} |
604 |
> |
for (int i = 0; i < o.length; i++) |
605 |
> |
assertEquals(o[i], q.take()); |
606 |
|
} |
607 |
|
|
608 |
< |
public void testToArray2(){ |
608 |
> |
/** |
609 |
> |
* toArray(a) contains all elements |
610 |
> |
*/ |
611 |
> |
public void testToArray2() throws InterruptedException { |
612 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
613 |
< |
Integer[] ints = new Integer[SIZE]; |
614 |
< |
ints = (Integer[])q.toArray(ints); |
613 |
> |
Integer[] ints = new Integer[SIZE]; |
614 |
> |
ints = (Integer[])q.toArray(ints); |
615 |
|
Arrays.sort(ints); |
616 |
< |
try { |
617 |
< |
for(int i = 0; i < ints.length; i++) |
618 |
< |
assertEquals(ints[i], q.take()); |
619 |
< |
} catch (InterruptedException e){ |
620 |
< |
fail("Unexpected exception"); |
621 |
< |
} |
616 |
> |
for (int i = 0; i < ints.length; i++) |
617 |
> |
assertEquals(ints[i], q.take()); |
618 |
> |
} |
619 |
> |
|
620 |
> |
/** |
621 |
> |
* toArray(null) throws NPE |
622 |
> |
*/ |
623 |
> |
public void testToArray_BadArg() { |
624 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
625 |
> |
try { |
626 |
> |
Object o[] = q.toArray(null); |
627 |
> |
shouldThrow(); |
628 |
> |
} catch (NullPointerException success) {} |
629 |
> |
} |
630 |
> |
|
631 |
> |
/** |
632 |
> |
* toArray with incompatible array type throws CCE |
633 |
> |
*/ |
634 |
> |
public void testToArray1_BadArg() { |
635 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
636 |
> |
try { |
637 |
> |
Object o[] = q.toArray(new String[10]); |
638 |
> |
shouldThrow(); |
639 |
> |
} catch (ArrayStoreException success) {} |
640 |
|
} |
641 |
< |
|
642 |
< |
public void testIterator(){ |
641 |
> |
|
642 |
> |
/** |
643 |
> |
* iterator iterates through all elements |
644 |
> |
*/ |
645 |
> |
public void testIterator() { |
646 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
647 |
|
int i = 0; |
648 |
< |
Iterator it = q.iterator(); |
649 |
< |
while(it.hasNext()) { |
648 |
> |
Iterator it = q.iterator(); |
649 |
> |
while (it.hasNext()) { |
650 |
|
assertTrue(q.contains(it.next())); |
651 |
|
++i; |
652 |
|
} |
653 |
|
assertEquals(i, SIZE); |
654 |
|
} |
655 |
|
|
656 |
< |
public void testIteratorRemove () { |
657 |
< |
|
656 |
> |
/** |
657 |
> |
* iterator.remove removes current element |
658 |
> |
*/ |
659 |
> |
public void testIteratorRemove() { |
660 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(3); |
582 |
– |
|
661 |
|
q.add(new Integer(2)); |
662 |
|
q.add(new Integer(1)); |
663 |
|
q.add(new Integer(3)); |
673 |
|
} |
674 |
|
|
675 |
|
|
676 |
< |
public void testToString(){ |
676 |
> |
/** |
677 |
> |
* toString contains toStrings of elements |
678 |
> |
*/ |
679 |
> |
public void testToString() { |
680 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
681 |
|
String s = q.toString(); |
682 |
|
for (int i = 0; i < SIZE; ++i) { |
683 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
684 |
|
} |
685 |
< |
} |
685 |
> |
} |
686 |
|
|
687 |
+ |
/** |
688 |
+ |
* offer transfers elements across Executor tasks |
689 |
+ |
*/ |
690 |
|
public void testPollInExecutor() { |
607 |
– |
|
691 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
609 |
– |
|
692 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
693 |
+ |
executor.execute(new CheckedRunnable() { |
694 |
+ |
public void realRun() throws InterruptedException { |
695 |
+ |
assertNull(q.poll()); |
696 |
+ |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
697 |
+ |
assertTrue(q.isEmpty()); |
698 |
+ |
}}); |
699 |
+ |
|
700 |
+ |
executor.execute(new CheckedRunnable() { |
701 |
+ |
public void realRun() throws InterruptedException { |
702 |
+ |
Thread.sleep(SMALL_DELAY_MS); |
703 |
+ |
q.put(one); |
704 |
+ |
}}); |
705 |
|
|
612 |
– |
executor.execute(new Runnable() { |
613 |
– |
public void run() { |
614 |
– |
threadAssertNull(q.poll()); |
615 |
– |
try { |
616 |
– |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
617 |
– |
threadAssertTrue(q.isEmpty()); |
618 |
– |
} |
619 |
– |
catch (InterruptedException e) { |
620 |
– |
threadFail("should not be interrupted"); |
621 |
– |
} |
622 |
– |
} |
623 |
– |
}); |
624 |
– |
|
625 |
– |
executor.execute(new Runnable() { |
626 |
– |
public void run() { |
627 |
– |
try { |
628 |
– |
Thread.sleep(SMALL_DELAY_MS); |
629 |
– |
q.put(new Integer(1)); |
630 |
– |
} |
631 |
– |
catch (InterruptedException e) { |
632 |
– |
threadFail("should not be interrupted"); |
633 |
– |
} |
634 |
– |
} |
635 |
– |
}); |
636 |
– |
|
706 |
|
joinPool(executor); |
707 |
+ |
} |
708 |
+ |
|
709 |
+ |
/** |
710 |
+ |
* A deserialized serialized queue has same elements |
711 |
+ |
*/ |
712 |
+ |
public void testSerialization() throws Exception { |
713 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
714 |
+ |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
715 |
+ |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
716 |
+ |
out.writeObject(q); |
717 |
+ |
out.close(); |
718 |
+ |
|
719 |
+ |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
720 |
+ |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
721 |
+ |
PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject(); |
722 |
+ |
assertEquals(q.size(), r.size()); |
723 |
+ |
while (!q.isEmpty()) |
724 |
+ |
assertEquals(q.remove(), r.remove()); |
725 |
+ |
} |
726 |
+ |
|
727 |
+ |
/** |
728 |
+ |
* drainTo(null) throws NPE |
729 |
+ |
*/ |
730 |
+ |
public void testDrainToNull() { |
731 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
732 |
+ |
try { |
733 |
+ |
q.drainTo(null); |
734 |
+ |
shouldThrow(); |
735 |
+ |
} catch (NullPointerException success) {} |
736 |
+ |
} |
737 |
+ |
|
738 |
+ |
/** |
739 |
+ |
* drainTo(this) throws IAE |
740 |
+ |
*/ |
741 |
+ |
public void testDrainToSelf() { |
742 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
743 |
+ |
try { |
744 |
+ |
q.drainTo(q); |
745 |
+ |
shouldThrow(); |
746 |
+ |
} catch (IllegalArgumentException success) {} |
747 |
+ |
} |
748 |
+ |
|
749 |
+ |
/** |
750 |
+ |
* drainTo(c) empties queue into another collection c |
751 |
+ |
*/ |
752 |
+ |
public void testDrainTo() { |
753 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
754 |
+ |
ArrayList l = new ArrayList(); |
755 |
+ |
q.drainTo(l); |
756 |
+ |
assertEquals(q.size(), 0); |
757 |
+ |
assertEquals(l.size(), SIZE); |
758 |
+ |
for (int i = 0; i < SIZE; ++i) |
759 |
+ |
assertEquals(l.get(i), new Integer(i)); |
760 |
+ |
q.add(zero); |
761 |
+ |
q.add(one); |
762 |
+ |
assertFalse(q.isEmpty()); |
763 |
+ |
assertTrue(q.contains(zero)); |
764 |
+ |
assertTrue(q.contains(one)); |
765 |
+ |
l.clear(); |
766 |
+ |
q.drainTo(l); |
767 |
+ |
assertEquals(q.size(), 0); |
768 |
+ |
assertEquals(l.size(), 2); |
769 |
+ |
for (int i = 0; i < 2; ++i) |
770 |
+ |
assertEquals(l.get(i), new Integer(i)); |
771 |
+ |
} |
772 |
|
|
773 |
+ |
/** |
774 |
+ |
* drainTo empties queue |
775 |
+ |
*/ |
776 |
+ |
public void testDrainToWithActivePut() throws InterruptedException { |
777 |
+ |
final PriorityBlockingQueue q = populatedQueue(SIZE); |
778 |
+ |
Thread t = new Thread(new CheckedRunnable() { |
779 |
+ |
public void realRun() { |
780 |
+ |
q.put(new Integer(SIZE+1)); |
781 |
+ |
}}); |
782 |
+ |
|
783 |
+ |
t.start(); |
784 |
+ |
ArrayList l = new ArrayList(); |
785 |
+ |
q.drainTo(l); |
786 |
+ |
assertTrue(l.size() >= SIZE); |
787 |
+ |
for (int i = 0; i < SIZE; ++i) |
788 |
+ |
assertEquals(l.get(i), new Integer(i)); |
789 |
+ |
t.join(); |
790 |
+ |
assertTrue(q.size() + l.size() >= SIZE); |
791 |
+ |
} |
792 |
+ |
|
793 |
+ |
/** |
794 |
+ |
* drainTo(null, n) throws NPE |
795 |
+ |
*/ |
796 |
+ |
public void testDrainToNullN() { |
797 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
798 |
+ |
try { |
799 |
+ |
q.drainTo(null, 0); |
800 |
+ |
shouldThrow(); |
801 |
+ |
} catch (NullPointerException success) {} |
802 |
|
} |
803 |
|
|
804 |
< |
public void testSerialization() { |
804 |
> |
/** |
805 |
> |
* drainTo(this, n) throws IAE |
806 |
> |
*/ |
807 |
> |
public void testDrainToSelfN() { |
808 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
809 |
|
try { |
810 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
811 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
812 |
< |
out.writeObject(q); |
813 |
< |
out.close(); |
814 |
< |
|
815 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
816 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
817 |
< |
PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject(); |
818 |
< |
assertEquals(q.size(), r.size()); |
819 |
< |
while (!q.isEmpty()) |
820 |
< |
assertEquals(q.remove(), r.remove()); |
821 |
< |
} catch(Exception e){ |
822 |
< |
fail("unexpected exception"); |
810 |
> |
q.drainTo(q, 0); |
811 |
> |
shouldThrow(); |
812 |
> |
} catch (IllegalArgumentException success) {} |
813 |
> |
} |
814 |
> |
|
815 |
> |
/** |
816 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
817 |
> |
*/ |
818 |
> |
public void testDrainToN() { |
819 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE*2); |
820 |
> |
for (int i = 0; i < SIZE + 2; ++i) { |
821 |
> |
for (int j = 0; j < SIZE; j++) |
822 |
> |
assertTrue(q.offer(new Integer(j))); |
823 |
> |
ArrayList l = new ArrayList(); |
824 |
> |
q.drainTo(l, i); |
825 |
> |
int k = (i < SIZE) ? i : SIZE; |
826 |
> |
assertEquals(l.size(), k); |
827 |
> |
assertEquals(q.size(), SIZE-k); |
828 |
> |
for (int j = 0; j < k; ++j) |
829 |
> |
assertEquals(l.get(j), new Integer(j)); |
830 |
> |
while (q.poll() != null) ; |
831 |
|
} |
832 |
|
} |
833 |
|
|