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