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 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 |
< |
|
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 |
|
} |
24 |
– |
|
19 |
|
public static Test suite() { |
20 |
< |
return new TestSuite(PriorityBlockingQueueTest.class); |
20 |
> |
return new TestSuite(PriorityBlockingQueueTest.class); |
21 |
|
} |
22 |
|
|
23 |
< |
static class MyReverseComparator implements Comparator { |
23 |
> |
private static final int NOCAP = Integer.MAX_VALUE; |
24 |
> |
|
25 |
> |
/** Sample Comparator */ |
26 |
> |
static class MyReverseComparator implements Comparator { |
27 |
|
public int compare(Object x, Object y) { |
28 |
< |
int i = ((Integer)x).intValue(); |
32 |
< |
int j = ((Integer)y).intValue(); |
33 |
< |
if (i < j) return 1; |
34 |
< |
if (i > j) return -1; |
35 |
< |
return 0; |
28 |
> |
return ((Comparable)y).compareTo(x); |
29 |
|
} |
30 |
|
} |
31 |
|
|
39 |
– |
|
32 |
|
/** |
33 |
|
* Create a queue of given size containing consecutive |
34 |
|
* Integers 0 ... n. |
35 |
|
*/ |
36 |
< |
private PriorityBlockingQueue fullQueue(int n) { |
36 |
> |
private PriorityBlockingQueue populatedQueue(int n) { |
37 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(n); |
38 |
|
assertTrue(q.isEmpty()); |
39 |
< |
for(int i = n-1; i >= 0; i-=2) |
40 |
< |
assertTrue(q.offer(new Integer(i))); |
41 |
< |
for(int i = (n & 1); i < n; i+=2) |
42 |
< |
assertTrue(q.offer(new Integer(i))); |
39 |
> |
for (int i = n-1; i >= 0; i-=2) |
40 |
> |
assertTrue(q.offer(new Integer(i))); |
41 |
> |
for (int i = (n & 1); i < n; i+=2) |
42 |
> |
assertTrue(q.offer(new Integer(i))); |
43 |
|
assertFalse(q.isEmpty()); |
44 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
45 |
< |
assertEquals(n, q.size()); |
45 |
> |
assertEquals(n, q.size()); |
46 |
|
return q; |
47 |
|
} |
48 |
< |
|
49 |
< |
public void testConstructor1(){ |
50 |
< |
assertEquals(NOCAP, new PriorityBlockingQueue(N).remainingCapacity()); |
48 |
> |
|
49 |
> |
/** |
50 |
> |
* A new queue has unbounded capacity |
51 |
> |
*/ |
52 |
> |
public void testConstructor1() { |
53 |
> |
assertEquals(NOCAP, new PriorityBlockingQueue(SIZE).remainingCapacity()); |
54 |
|
} |
55 |
|
|
56 |
< |
public void testConstructor2(){ |
56 |
> |
/** |
57 |
> |
* Constructor throws IAE if capacity argument nonpositive |
58 |
> |
*/ |
59 |
> |
public void testConstructor2() { |
60 |
|
try { |
61 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(0); |
62 |
< |
fail("Cannot make zero-sized"); |
63 |
< |
} |
66 |
< |
catch (IllegalArgumentException success) {} |
62 |
> |
shouldThrow(); |
63 |
> |
} catch (IllegalArgumentException success) {} |
64 |
|
} |
65 |
|
|
66 |
< |
public void testConstructor3(){ |
67 |
< |
|
66 |
> |
/** |
67 |
> |
* Initializing from null Collection throws NPE |
68 |
> |
*/ |
69 |
> |
public void testConstructor3() { |
70 |
|
try { |
71 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(null); |
72 |
< |
fail("Cannot make from null collection"); |
73 |
< |
} |
75 |
< |
catch (NullPointerException success) {} |
72 |
> |
shouldThrow(); |
73 |
> |
} catch (NullPointerException success) {} |
74 |
|
} |
75 |
|
|
76 |
< |
public void testConstructor4(){ |
76 |
> |
/** |
77 |
> |
* Initializing from Collection of null elements throws NPE |
78 |
> |
*/ |
79 |
> |
public void testConstructor4() { |
80 |
|
try { |
81 |
< |
Integer[] ints = new Integer[N]; |
81 |
> |
Integer[] ints = new Integer[SIZE]; |
82 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
83 |
< |
fail("Cannot make with null elements"); |
84 |
< |
} |
84 |
< |
catch (NullPointerException success) {} |
83 |
> |
shouldThrow(); |
84 |
> |
} catch (NullPointerException success) {} |
85 |
|
} |
86 |
|
|
87 |
< |
public void testConstructor5(){ |
87 |
> |
/** |
88 |
> |
* Initializing from Collection with some null elements throws NPE |
89 |
> |
*/ |
90 |
> |
public void testConstructor5() { |
91 |
|
try { |
92 |
< |
Integer[] ints = new Integer[N]; |
93 |
< |
for (int i = 0; i < N-1; ++i) |
92 |
> |
Integer[] ints = new Integer[SIZE]; |
93 |
> |
for (int i = 0; i < SIZE-1; ++i) |
94 |
|
ints[i] = new Integer(i); |
95 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
96 |
< |
fail("Cannot make with null elements"); |
97 |
< |
} |
95 |
< |
catch (NullPointerException success) {} |
96 |
> |
shouldThrow(); |
97 |
> |
} catch (NullPointerException success) {} |
98 |
|
} |
99 |
|
|
100 |
< |
public void testConstructor6(){ |
101 |
< |
try { |
102 |
< |
Integer[] ints = new Integer[N]; |
103 |
< |
for (int i = 0; i < N; ++i) |
104 |
< |
ints[i] = new Integer(i); |
105 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
106 |
< |
for (int i = 0; i < N; ++i) |
107 |
< |
assertEquals(ints[i], q.poll()); |
108 |
< |
} |
109 |
< |
finally {} |
100 |
> |
/** |
101 |
> |
* Queue contains all elements of collection used to initialize |
102 |
> |
*/ |
103 |
> |
public void testConstructor6() { |
104 |
> |
Integer[] ints = new Integer[SIZE]; |
105 |
> |
for (int i = 0; i < SIZE; ++i) |
106 |
> |
ints[i] = new Integer(i); |
107 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
108 |
> |
for (int i = 0; i < SIZE; ++i) |
109 |
> |
assertEquals(ints[i], q.poll()); |
110 |
|
} |
111 |
|
|
112 |
< |
public void testConstructor7(){ |
113 |
< |
try { |
114 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N, new MyReverseComparator()); |
115 |
< |
Integer[] ints = new Integer[N]; |
116 |
< |
for (int i = 0; i < N; ++i) |
117 |
< |
ints[i] = new Integer(i); |
118 |
< |
q.addAll(Arrays.asList(ints)); |
119 |
< |
for (int i = N-1; i >= 0; --i) |
120 |
< |
assertEquals(ints[i], q.poll()); |
121 |
< |
} |
122 |
< |
finally {} |
112 |
> |
/** |
113 |
> |
* The comparator used in constructor is used |
114 |
> |
*/ |
115 |
> |
public void testConstructor7() { |
116 |
> |
MyReverseComparator cmp = new MyReverseComparator(); |
117 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp); |
118 |
> |
assertEquals(cmp, q.comparator()); |
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 |
|
|
127 |
+ |
/** |
128 |
+ |
* isEmpty is true before add, false after |
129 |
+ |
*/ |
130 |
|
public void testEmpty() { |
131 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
132 |
|
assertTrue(q.isEmpty()); |
133 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
134 |
< |
q.add(new Integer(1)); |
134 |
> |
q.add(one); |
135 |
|
assertFalse(q.isEmpty()); |
136 |
< |
q.add(new Integer(2)); |
136 |
> |
q.add(two); |
137 |
|
q.remove(); |
138 |
|
q.remove(); |
139 |
|
assertTrue(q.isEmpty()); |
140 |
|
} |
141 |
|
|
142 |
< |
public void testRemainingCapacity(){ |
143 |
< |
PriorityBlockingQueue q = fullQueue(N); |
144 |
< |
for (int i = 0; i < N; ++i) { |
142 |
> |
/** |
143 |
> |
* remainingCapacity does not change when elements added or removed, |
144 |
> |
* but size does |
145 |
> |
*/ |
146 |
> |
public void testRemainingCapacity() { |
147 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
148 |
> |
for (int i = 0; i < SIZE; ++i) { |
149 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
150 |
< |
assertEquals(N-i, q.size()); |
150 |
> |
assertEquals(SIZE-i, q.size()); |
151 |
|
q.remove(); |
152 |
|
} |
153 |
< |
for (int i = 0; i < N; ++i) { |
153 |
> |
for (int i = 0; i < SIZE; ++i) { |
154 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
155 |
|
assertEquals(i, q.size()); |
156 |
|
q.add(new Integer(i)); |
157 |
|
} |
158 |
|
} |
159 |
|
|
160 |
< |
public void testOfferNull(){ |
161 |
< |
try { |
160 |
> |
/** |
161 |
> |
* offer(null) throws NPE |
162 |
> |
*/ |
163 |
> |
public void testOfferNull() { |
164 |
> |
try { |
165 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
166 |
|
q.offer(null); |
167 |
< |
fail("should throw NPE"); |
168 |
< |
} catch (NullPointerException success) { } |
167 |
> |
shouldThrow(); |
168 |
> |
} catch (NullPointerException success) {} |
169 |
> |
} |
170 |
> |
|
171 |
> |
/** |
172 |
> |
* add(null) throws NPE |
173 |
> |
*/ |
174 |
> |
public void testAddNull() { |
175 |
> |
try { |
176 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
177 |
> |
q.add(null); |
178 |
> |
shouldThrow(); |
179 |
> |
} catch (NullPointerException success) {} |
180 |
|
} |
181 |
|
|
182 |
+ |
/** |
183 |
+ |
* Offer of comparable element succeeds |
184 |
+ |
*/ |
185 |
|
public void testOffer() { |
186 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
187 |
< |
assertTrue(q.offer(new Integer(0))); |
188 |
< |
assertTrue(q.offer(new Integer(1))); |
187 |
> |
assertTrue(q.offer(zero)); |
188 |
> |
assertTrue(q.offer(one)); |
189 |
|
} |
190 |
|
|
191 |
+ |
/** |
192 |
+ |
* Offer of non-Comparable throws CCE |
193 |
+ |
*/ |
194 |
|
public void testOfferNonComparable() { |
195 |
|
try { |
196 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
197 |
|
q.offer(new Object()); |
198 |
|
q.offer(new Object()); |
199 |
|
q.offer(new Object()); |
200 |
< |
fail("should throw CCE"); |
201 |
< |
} |
171 |
< |
catch(ClassCastException success) {} |
200 |
> |
shouldThrow(); |
201 |
> |
} catch (ClassCastException success) {} |
202 |
|
} |
203 |
|
|
204 |
< |
public void testAdd(){ |
205 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
206 |
< |
for (int i = 0; i < N; ++i) { |
204 |
> |
/** |
205 |
> |
* add of comparable succeeds |
206 |
> |
*/ |
207 |
> |
public void testAdd() { |
208 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
209 |
> |
for (int i = 0; i < SIZE; ++i) { |
210 |
|
assertEquals(i, q.size()); |
211 |
|
assertTrue(q.add(new Integer(i))); |
212 |
|
} |
213 |
|
} |
214 |
|
|
215 |
< |
public void testAddAll1(){ |
215 |
> |
/** |
216 |
> |
* addAll(null) throws NPE |
217 |
> |
*/ |
218 |
> |
public void testAddAll1() { |
219 |
|
try { |
220 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
221 |
|
q.addAll(null); |
222 |
< |
fail("Cannot add null collection"); |
223 |
< |
} |
224 |
< |
catch (NullPointerException success) {} |
222 |
> |
shouldThrow(); |
223 |
> |
} catch (NullPointerException success) {} |
224 |
> |
} |
225 |
> |
|
226 |
> |
/** |
227 |
> |
* addAll(this) throws IAE |
228 |
> |
*/ |
229 |
> |
public void testAddAllSelf() { |
230 |
> |
try { |
231 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
232 |
> |
q.addAll(q); |
233 |
> |
shouldThrow(); |
234 |
> |
} catch (IllegalArgumentException success) {} |
235 |
|
} |
236 |
< |
public void testAddAll2(){ |
236 |
> |
|
237 |
> |
/** |
238 |
> |
* addAll of a collection with null elements throws NPE |
239 |
> |
*/ |
240 |
> |
public void testAddAll2() { |
241 |
|
try { |
242 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
243 |
< |
Integer[] ints = new Integer[N]; |
242 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
243 |
> |
Integer[] ints = new Integer[SIZE]; |
244 |
|
q.addAll(Arrays.asList(ints)); |
245 |
< |
fail("Cannot add null elements"); |
246 |
< |
} |
197 |
< |
catch (NullPointerException success) {} |
245 |
> |
shouldThrow(); |
246 |
> |
} catch (NullPointerException success) {} |
247 |
|
} |
248 |
< |
public void testAddAll3(){ |
248 |
> |
|
249 |
> |
/** |
250 |
> |
* addAll of a collection with any null elements throws NPE after |
251 |
> |
* possibly adding some elements |
252 |
> |
*/ |
253 |
> |
public void testAddAll3() { |
254 |
|
try { |
255 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
256 |
< |
Integer[] ints = new Integer[N]; |
257 |
< |
for (int i = 0; i < N-1; ++i) |
255 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
256 |
> |
Integer[] ints = new Integer[SIZE]; |
257 |
> |
for (int i = 0; i < SIZE-1; ++i) |
258 |
|
ints[i] = new Integer(i); |
259 |
|
q.addAll(Arrays.asList(ints)); |
260 |
< |
fail("Cannot add null elements"); |
261 |
< |
} |
208 |
< |
catch (NullPointerException success) {} |
260 |
> |
shouldThrow(); |
261 |
> |
} catch (NullPointerException success) {} |
262 |
|
} |
263 |
|
|
264 |
< |
public void testAddAll5(){ |
265 |
< |
try { |
266 |
< |
Integer[] empty = new Integer[0]; |
267 |
< |
Integer[] ints = new Integer[N]; |
268 |
< |
for (int i = N-1; i >= 0; --i) |
269 |
< |
ints[i] = new Integer(i); |
270 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
271 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
272 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
273 |
< |
for (int i = 0; i < N; ++i) |
274 |
< |
assertEquals(ints[i], q.poll()); |
275 |
< |
} |
276 |
< |
finally {} |
264 |
> |
/** |
265 |
> |
* Queue contains all elements of successful addAll |
266 |
> |
*/ |
267 |
> |
public void testAddAll5() { |
268 |
> |
Integer[] empty = new Integer[0]; |
269 |
> |
Integer[] ints = new Integer[SIZE]; |
270 |
> |
for (int i = SIZE-1; i >= 0; --i) |
271 |
> |
ints[i] = new Integer(i); |
272 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
273 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
274 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
275 |
> |
for (int i = 0; i < SIZE; ++i) |
276 |
> |
assertEquals(ints[i], q.poll()); |
277 |
|
} |
278 |
|
|
279 |
+ |
/** |
280 |
+ |
* put(null) throws NPE |
281 |
+ |
*/ |
282 |
|
public void testPutNull() { |
283 |
< |
try { |
284 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
283 |
> |
try { |
284 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
285 |
|
q.put(null); |
286 |
< |
fail("put should throw NPE"); |
287 |
< |
} |
232 |
< |
catch (NullPointerException success){ |
233 |
< |
} |
286 |
> |
shouldThrow(); |
287 |
> |
} catch (NullPointerException success) {} |
288 |
|
} |
289 |
|
|
290 |
+ |
/** |
291 |
+ |
* all elements successfully put are contained |
292 |
+ |
*/ |
293 |
|
public void testPut() { |
294 |
< |
try { |
295 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
296 |
< |
for (int i = 0; i < N; ++i) { |
297 |
< |
Integer I = new Integer(i); |
298 |
< |
q.put(I); |
242 |
< |
assertTrue(q.contains(I)); |
243 |
< |
} |
244 |
< |
assertEquals(N, q.size()); |
294 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
295 |
> |
for (int i = 0; i < SIZE; ++i) { |
296 |
> |
Integer I = new Integer(i); |
297 |
> |
q.put(I); |
298 |
> |
assertTrue(q.contains(I)); |
299 |
|
} |
300 |
< |
finally { |
247 |
< |
} |
300 |
> |
assertEquals(SIZE, q.size()); |
301 |
|
} |
302 |
|
|
303 |
< |
public void testPutWithTake() { |
303 |
> |
/** |
304 |
> |
* put doesn't block waiting for take |
305 |
> |
*/ |
306 |
> |
public void testPutWithTake() throws InterruptedException { |
307 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
308 |
< |
Thread t = new Thread(new Runnable() { |
309 |
< |
public void run(){ |
310 |
< |
int added = 0; |
311 |
< |
try { |
312 |
< |
q.put(new Integer(0)); |
313 |
< |
++added; |
314 |
< |
q.put(new Integer(0)); |
315 |
< |
++added; |
316 |
< |
q.put(new Integer(0)); |
317 |
< |
++added; |
318 |
< |
q.put(new Integer(0)); |
319 |
< |
++added; |
320 |
< |
assertTrue(added == 4); |
265 |
< |
} finally { |
266 |
< |
} |
267 |
< |
} |
268 |
< |
}); |
269 |
< |
try { |
270 |
< |
t.start(); |
271 |
< |
Thread.sleep(SHORT_DELAY_MS); |
272 |
< |
q.take(); |
273 |
< |
t.interrupt(); |
274 |
< |
t.join(); |
275 |
< |
} catch (Exception e){ |
276 |
< |
fail("Unexpected exception"); |
277 |
< |
} |
308 |
> |
final int size = 4; |
309 |
> |
Thread t = new Thread(new CheckedRunnable() { |
310 |
> |
public void realRun() { |
311 |
> |
for (int i = 0; i < size; i++) |
312 |
> |
q.put(new Integer(0)); |
313 |
> |
}}); |
314 |
> |
|
315 |
> |
t.start(); |
316 |
> |
Thread.sleep(SHORT_DELAY_MS); |
317 |
> |
assertEquals(q.size(), size); |
318 |
> |
q.take(); |
319 |
> |
t.interrupt(); |
320 |
> |
t.join(); |
321 |
|
} |
322 |
|
|
323 |
< |
public void testTimedOffer() { |
323 |
> |
/** |
324 |
> |
* timed offer does not time out |
325 |
> |
*/ |
326 |
> |
public void testTimedOffer() throws InterruptedException { |
327 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
328 |
< |
Thread t = new Thread(new Runnable() { |
329 |
< |
public void run(){ |
330 |
< |
try { |
331 |
< |
q.put(new Integer(0)); |
332 |
< |
q.put(new Integer(0)); |
333 |
< |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); |
334 |
< |
assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
335 |
< |
} finally { } |
336 |
< |
} |
337 |
< |
}); |
338 |
< |
|
339 |
< |
try { |
294 |
< |
t.start(); |
295 |
< |
Thread.sleep(SHORT_DELAY_MS); |
296 |
< |
t.interrupt(); |
297 |
< |
t.join(); |
298 |
< |
} catch (Exception e){ |
299 |
< |
fail("Unexpected exception"); |
300 |
< |
} |
328 |
> |
Thread t = new Thread(new CheckedRunnable() { |
329 |
> |
public void realRun() { |
330 |
> |
q.put(new Integer(0)); |
331 |
> |
q.put(new Integer(0)); |
332 |
> |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS)); |
333 |
> |
assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS)); |
334 |
> |
}}); |
335 |
> |
|
336 |
> |
t.start(); |
337 |
> |
Thread.sleep(SMALL_DELAY_MS); |
338 |
> |
t.interrupt(); |
339 |
> |
t.join(); |
340 |
|
} |
341 |
|
|
342 |
< |
public void testTake(){ |
343 |
< |
try { |
344 |
< |
PriorityBlockingQueue q = fullQueue(N); |
345 |
< |
for (int i = 0; i < N; ++i) { |
346 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
347 |
< |
} |
348 |
< |
} catch (InterruptedException e){ |
349 |
< |
fail("Unexpected exception"); |
311 |
< |
} |
342 |
> |
/** |
343 |
> |
* take retrieves elements in priority order |
344 |
> |
*/ |
345 |
> |
public void testTake() throws InterruptedException { |
346 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
347 |
> |
for (int i = 0; i < SIZE; ++i) { |
348 |
> |
assertEquals(i, q.take()); |
349 |
> |
} |
350 |
|
} |
351 |
|
|
352 |
< |
public void testTakeFromEmpty() { |
352 |
> |
/** |
353 |
> |
* take blocks interruptibly when empty |
354 |
> |
*/ |
355 |
> |
public void testTakeFromEmpty() throws InterruptedException { |
356 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
357 |
< |
Thread t = new Thread(new Runnable() { |
358 |
< |
public void run(){ |
359 |
< |
try { |
360 |
< |
q.take(); |
361 |
< |
fail("Should block"); |
362 |
< |
} catch (InterruptedException success){ } |
363 |
< |
} |
364 |
< |
}); |
365 |
< |
try { |
325 |
< |
t.start(); |
326 |
< |
Thread.sleep(SHORT_DELAY_MS); |
327 |
< |
t.interrupt(); |
328 |
< |
t.join(); |
329 |
< |
} catch (Exception e){ |
330 |
< |
fail("Unexpected exception"); |
331 |
< |
} |
357 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
358 |
> |
public void realRun() throws InterruptedException { |
359 |
> |
q.take(); |
360 |
> |
}}); |
361 |
> |
|
362 |
> |
t.start(); |
363 |
> |
Thread.sleep(SHORT_DELAY_MS); |
364 |
> |
t.interrupt(); |
365 |
> |
t.join(); |
366 |
|
} |
367 |
|
|
368 |
< |
public void testBlockingTake(){ |
369 |
< |
Thread t = new Thread(new Runnable() { |
370 |
< |
public void run() { |
371 |
< |
try { |
372 |
< |
PriorityBlockingQueue q = fullQueue(N); |
373 |
< |
for (int i = 0; i < N; ++i) { |
374 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
375 |
< |
} |
376 |
< |
q.take(); |
377 |
< |
fail("take should block"); |
378 |
< |
} catch (InterruptedException success){ |
379 |
< |
} |
380 |
< |
}}); |
368 |
> |
/** |
369 |
> |
* Take removes existing elements until empty, then blocks interruptibly |
370 |
> |
*/ |
371 |
> |
public void testBlockingTake() throws InterruptedException { |
372 |
> |
final PriorityBlockingQueue q = populatedQueue(SIZE); |
373 |
> |
Thread t = new Thread(new CheckedRunnable() { |
374 |
> |
public void realRun() throws InterruptedException { |
375 |
> |
for (int i = 0; i < SIZE; ++i) { |
376 |
> |
assertEquals(i, q.take()); |
377 |
> |
} |
378 |
> |
try { |
379 |
> |
q.take(); |
380 |
> |
shouldThrow(); |
381 |
> |
} catch (InterruptedException success) {} |
382 |
> |
}}); |
383 |
> |
|
384 |
|
t.start(); |
385 |
< |
try { |
386 |
< |
Thread.sleep(SHORT_DELAY_MS); |
387 |
< |
t.interrupt(); |
351 |
< |
t.join(); |
352 |
< |
} |
353 |
< |
catch (InterruptedException ie) { |
354 |
< |
fail("Unexpected exception"); |
355 |
< |
} |
385 |
> |
Thread.sleep(SHORT_DELAY_MS); |
386 |
> |
t.interrupt(); |
387 |
> |
t.join(); |
388 |
|
} |
389 |
|
|
390 |
|
|
391 |
< |
public void testPoll(){ |
392 |
< |
PriorityBlockingQueue q = fullQueue(N); |
393 |
< |
for (int i = 0; i < N; ++i) { |
394 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
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, q.poll()); |
398 |
|
} |
399 |
< |
assertNull(q.poll()); |
399 |
> |
assertNull(q.poll()); |
400 |
|
} |
401 |
|
|
402 |
< |
public void testTimedPoll0() { |
403 |
< |
try { |
404 |
< |
PriorityBlockingQueue q = fullQueue(N); |
405 |
< |
for (int i = 0; i < N; ++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"); |
376 |
< |
} |
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, q.poll(0, MILLISECONDS)); |
409 |
> |
} |
410 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
411 |
|
} |
412 |
|
|
413 |
< |
public void testTimedPoll() { |
414 |
< |
try { |
415 |
< |
PriorityBlockingQueue q = fullQueue(N); |
416 |
< |
for (int i = 0; i < N; ++i) { |
417 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
418 |
< |
} |
419 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
386 |
< |
} catch (InterruptedException e){ |
387 |
< |
fail("Unexpected exception"); |
388 |
< |
} |
389 |
< |
} |
390 |
< |
|
391 |
< |
public void testInterruptedTimedPoll(){ |
392 |
< |
Thread t = new Thread(new Runnable() { |
393 |
< |
public void run() { |
394 |
< |
try { |
395 |
< |
PriorityBlockingQueue q = fullQueue(N); |
396 |
< |
for (int i = 0; i < N; ++i) { |
397 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
398 |
< |
} |
399 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
400 |
< |
} catch (InterruptedException success){ |
401 |
< |
} |
402 |
< |
}}); |
403 |
< |
t.start(); |
404 |
< |
try { |
405 |
< |
Thread.sleep(SHORT_DELAY_MS); |
406 |
< |
t.interrupt(); |
407 |
< |
t.join(); |
408 |
< |
} |
409 |
< |
catch (InterruptedException ie) { |
410 |
< |
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, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
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 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
430 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
431 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
432 |
< |
fail("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, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
434 |
|
} |
435 |
< |
}); |
436 |
< |
try { |
437 |
< |
t.start(); |
438 |
< |
Thread.sleep(SHORT_DELAY_MS * 2); |
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 |
< |
} |
446 |
< |
|
447 |
< |
|
448 |
< |
public void testPeek(){ |
449 |
< |
PriorityBlockingQueue q = fullQueue(N); |
450 |
< |
for (int i = 0; i < N; ++i) { |
451 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
452 |
< |
q.poll(); |
435 |
> |
try { |
436 |
> |
q.poll(SMALL_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 |
> |
try { |
456 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
457 |
> |
assertSame(zero, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
458 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
459 |
> |
shouldThrow(); |
460 |
> |
} catch (InterruptedException success) {} |
461 |
> |
}}); |
462 |
> |
|
463 |
> |
t.start(); |
464 |
> |
Thread.sleep(SMALL_DELAY_MS); |
465 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
466 |
> |
t.interrupt(); |
467 |
> |
t.join(); |
468 |
> |
} |
469 |
> |
|
470 |
> |
|
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, q.peek()); |
478 |
> |
assertEquals(i, q.poll()); |
479 |
|
assertTrue(q.peek() == null || |
480 |
< |
i != ((Integer)q.peek()).intValue()); |
480 |
> |
!q.peek().equals(i)); |
481 |
|
} |
482 |
< |
assertNull(q.peek()); |
482 |
> |
assertNull(q.peek()); |
483 |
|
} |
484 |
|
|
485 |
< |
public void testElement(){ |
486 |
< |
PriorityBlockingQueue q = fullQueue(N); |
487 |
< |
for (int i = 0; i < N; ++i) { |
488 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
489 |
< |
q.poll(); |
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, q.element()); |
492 |
> |
assertEquals(i, q.poll()); |
493 |
|
} |
494 |
|
try { |
495 |
|
q.element(); |
496 |
< |
fail("no such element"); |
497 |
< |
} |
459 |
< |
catch (NoSuchElementException success) {} |
496 |
> |
shouldThrow(); |
497 |
> |
} catch (NoSuchElementException success) {} |
498 |
|
} |
499 |
|
|
500 |
< |
public void testRemove(){ |
501 |
< |
PriorityBlockingQueue q = fullQueue(N); |
502 |
< |
for (int i = 0; i < N; ++i) { |
503 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
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, q.remove()); |
507 |
|
} |
508 |
|
try { |
509 |
|
q.remove(); |
510 |
< |
fail("remove should throw"); |
511 |
< |
} catch (NoSuchElementException success){ |
471 |
< |
} |
510 |
> |
shouldThrow(); |
511 |
> |
} catch (NoSuchElementException success) {} |
512 |
|
} |
513 |
|
|
514 |
< |
public void testRemoveElement(){ |
515 |
< |
PriorityBlockingQueue q = fullQueue(N); |
516 |
< |
for (int i = 1; i < N; i+=2) { |
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))); |
521 |
|
} |
522 |
< |
for (int i = 0; i < N; i+=2) { |
522 |
> |
for (int i = 0; i < SIZE; i+=2) { |
523 |
|
assertTrue(q.remove(new Integer(i))); |
524 |
|
assertFalse(q.remove(new Integer(i+1))); |
525 |
|
} |
526 |
|
assertTrue(q.isEmpty()); |
527 |
|
} |
528 |
< |
|
529 |
< |
public void testContains(){ |
530 |
< |
PriorityBlockingQueue q = fullQueue(N); |
531 |
< |
for (int i = 0; i < N; ++i) { |
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))); |
536 |
|
q.poll(); |
537 |
|
assertFalse(q.contains(new Integer(i))); |
538 |
|
} |
539 |
|
} |
540 |
|
|
541 |
< |
public void testClear(){ |
542 |
< |
PriorityBlockingQueue q = fullQueue(N); |
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()); |
501 |
< |
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(){ |
557 |
< |
PriorityBlockingQueue q = fullQueue(N); |
558 |
< |
PriorityBlockingQueue p = new PriorityBlockingQueue(N); |
559 |
< |
for (int i = 0; i < N; ++i) { |
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) { |
563 |
|
assertTrue(q.containsAll(p)); |
564 |
|
assertFalse(p.containsAll(q)); |
565 |
|
p.add(new Integer(i)); |
567 |
|
assertTrue(p.containsAll(q)); |
568 |
|
} |
569 |
|
|
570 |
< |
public void testRetainAll(){ |
571 |
< |
PriorityBlockingQueue q = fullQueue(N); |
572 |
< |
PriorityBlockingQueue p = fullQueue(N); |
573 |
< |
for (int i = 0; i < N; ++i) { |
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) { |
577 |
|
boolean changed = q.retainAll(p); |
578 |
|
if (i == 0) |
579 |
|
assertFalse(changed); |
581 |
|
assertTrue(changed); |
582 |
|
|
583 |
|
assertTrue(q.containsAll(p)); |
584 |
< |
assertEquals(N-i, q.size()); |
584 |
> |
assertEquals(SIZE-i, q.size()); |
585 |
|
p.remove(); |
586 |
|
} |
587 |
|
} |
588 |
|
|
589 |
< |
public void testRemoveAll(){ |
590 |
< |
for (int i = 1; i < N; ++i) { |
591 |
< |
PriorityBlockingQueue q = fullQueue(N); |
592 |
< |
PriorityBlockingQueue p = fullQueue(i); |
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); |
596 |
|
assertTrue(q.removeAll(p)); |
597 |
< |
assertEquals(N-i, q.size()); |
597 |
> |
assertEquals(SIZE-i, q.size()); |
598 |
|
for (int j = 0; j < i; ++j) { |
599 |
|
Integer I = (Integer)(p.remove()); |
600 |
|
assertFalse(q.contains(I)); |
602 |
|
} |
603 |
|
} |
604 |
|
|
605 |
< |
public void testToArray(){ |
606 |
< |
PriorityBlockingQueue q = fullQueue(N); |
607 |
< |
Object[] o = q.toArray(); |
605 |
> |
/** |
606 |
> |
* toArray contains all elements |
607 |
> |
*/ |
608 |
> |
public void testToArray() throws InterruptedException { |
609 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
610 |
> |
Object[] o = q.toArray(); |
611 |
|
Arrays.sort(o); |
612 |
< |
try { |
613 |
< |
for(int i = 0; i < o.length; i++) |
553 |
< |
assertEquals(o[i], q.take()); |
554 |
< |
} catch (InterruptedException e){ |
555 |
< |
fail("Unexpected exception"); |
556 |
< |
} |
612 |
> |
for (int i = 0; i < o.length; i++) |
613 |
> |
assertEquals(o[i], q.take()); |
614 |
|
} |
615 |
|
|
616 |
< |
public void testToArray2(){ |
617 |
< |
PriorityBlockingQueue q = fullQueue(N); |
618 |
< |
Integer[] ints = new Integer[N]; |
619 |
< |
ints = (Integer[])q.toArray(ints); |
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); |
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 |
< |
} |
630 |
< |
} |
631 |
< |
|
632 |
< |
public void testIterator(){ |
633 |
< |
PriorityBlockingQueue q = fullQueue(N); |
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 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
633 |
> |
try { |
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 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
644 |
> |
try { |
645 |
> |
Object o[] = q.toArray(new String[10]); |
646 |
> |
shouldThrow(); |
647 |
> |
} catch (ArrayStoreException success) {} |
648 |
> |
} |
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, N); |
661 |
> |
assertEquals(i, SIZE); |
662 |
|
} |
663 |
|
|
664 |
< |
public void testIteratorRemove () { |
665 |
< |
|
664 |
> |
/** |
665 |
> |
* iterator.remove removes current element |
666 |
> |
*/ |
667 |
> |
public void testIteratorRemove() { |
668 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(3); |
586 |
– |
|
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(){ |
685 |
< |
PriorityBlockingQueue q = fullQueue(N); |
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 < N; ++i) { |
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() { |
611 |
– |
|
699 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
613 |
– |
|
700 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
701 |
+ |
executor.execute(new CheckedRunnable() { |
702 |
+ |
public void realRun() throws InterruptedException { |
703 |
+ |
assertNull(q.poll()); |
704 |
+ |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
705 |
+ |
assertTrue(q.isEmpty()); |
706 |
+ |
}}); |
707 |
+ |
|
708 |
+ |
executor.execute(new CheckedRunnable() { |
709 |
+ |
public void realRun() throws InterruptedException { |
710 |
+ |
Thread.sleep(SMALL_DELAY_MS); |
711 |
+ |
q.put(one); |
712 |
+ |
}}); |
713 |
|
|
714 |
< |
executor.execute(new Runnable() { |
715 |
< |
public void run() { |
618 |
< |
assertNull("poll should fail", q.poll()); |
619 |
< |
try { |
620 |
< |
assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); |
621 |
< |
assertTrue(q.isEmpty()); |
622 |
< |
} |
623 |
< |
catch (InterruptedException e) { |
624 |
< |
fail("should not be interrupted"); |
625 |
< |
} |
626 |
< |
} |
627 |
< |
}); |
714 |
> |
joinPool(executor); |
715 |
> |
} |
716 |
|
|
717 |
< |
executor.execute(new Runnable() { |
718 |
< |
public void run() { |
719 |
< |
try { |
720 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
721 |
< |
q.put(new Integer(1)); |
722 |
< |
} |
723 |
< |
catch (InterruptedException e) { |
724 |
< |
fail("should not be interrupted"); |
725 |
< |
} |
726 |
< |
} |
727 |
< |
}); |
728 |
< |
|
729 |
< |
executor.shutdown(); |
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() { |
813 |
< |
PriorityBlockingQueue q = fullQueue(N); |
814 |
< |
try { |
815 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
816 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
817 |
< |
out.writeObject(q); |
818 |
< |
out.close(); |
819 |
< |
|
820 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
821 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
822 |
< |
PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject(); |
823 |
< |
assertEquals(q.size(), r.size()); |
824 |
< |
while (!q.isEmpty()) |
825 |
< |
assertEquals(q.remove(), r.remove()); |
826 |
< |
} catch(Exception e){ |
827 |
< |
fail("unexpected exception"); |
812 |
> |
/** |
813 |
> |
* drainTo(this, n) throws IAE |
814 |
> |
*/ |
815 |
> |
public void testDrainToSelfN() { |
816 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
817 |
> |
try { |
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 |
|
|