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 { |
15 |
> |
public class PriorityBlockingQueueTest extends JSR166TestCase { |
16 |
|
|
17 |
< |
private static final int N = 10; |
18 |
< |
private static final long SHORT_DELAY_MS = 100; |
19 |
< |
private static final long MEDIUM_DELAY_MS = 1000; |
20 |
< |
private static final long LONG_DELAY_MS = 10000; |
21 |
< |
private static final int NOCAP = Integer.MAX_VALUE; |
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 |
< |
static class MyReverseComparator implements Comparator { |
39 |
> |
private static final int NOCAP = Integer.MAX_VALUE; |
40 |
> |
|
41 |
> |
/** Sample Comparator */ |
42 |
> |
static class MyReverseComparator implements Comparator { |
43 |
|
public int compare(Object x, Object y) { |
44 |
< |
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; |
44 |
> |
return ((Comparable)y).compareTo(x); |
45 |
|
} |
46 |
|
} |
47 |
|
|
39 |
– |
|
48 |
|
/** |
49 |
|
* Create a queue of given size containing consecutive |
50 |
|
* Integers 0 ... n. |
51 |
|
*/ |
52 |
< |
private PriorityBlockingQueue fullQueue(int 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(){ |
66 |
< |
assertEquals(NOCAP, new PriorityBlockingQueue(N).remainingCapacity()); |
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 |
< |
} |
66 |
< |
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 |
< |
} |
75 |
< |
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[N]; |
97 |
> |
Integer[] ints = new Integer[SIZE]; |
98 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
99 |
< |
fail("Cannot make with null elements"); |
100 |
< |
} |
84 |
< |
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[N]; |
109 |
< |
for (int i = 0; i < N-1; ++i) |
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 |
< |
} |
95 |
< |
catch (NullPointerException success) {} |
112 |
> |
shouldThrow(); |
113 |
> |
} catch (NullPointerException success) {} |
114 |
|
} |
115 |
|
|
116 |
< |
public void testConstructor6(){ |
117 |
< |
try { |
118 |
< |
Integer[] ints = new Integer[N]; |
119 |
< |
for (int i = 0; i < N; ++i) |
120 |
< |
ints[i] = new Integer(i); |
121 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
122 |
< |
for (int i = 0; i < N; ++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(N, new MyReverseComparator()); |
131 |
< |
Integer[] ints = new Integer[N]; |
132 |
< |
for (int i = 0; i < N; ++i) |
133 |
< |
ints[i] = new Integer(i); |
134 |
< |
q.addAll(Arrays.asList(ints)); |
135 |
< |
for (int i = N-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(){ |
159 |
< |
PriorityBlockingQueue q = fullQueue(N); |
160 |
< |
for (int i = 0; i < N; ++i) { |
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()); |
166 |
< |
assertEquals(N-i, q.size()); |
166 |
> |
assertEquals(SIZE-i, q.size()); |
167 |
|
q.remove(); |
168 |
|
} |
169 |
< |
for (int i = 0; i < N; ++i) { |
169 |
> |
for (int i = 0; i < SIZE; ++i) { |
170 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
171 |
|
assertEquals(i, q.size()); |
172 |
|
q.add(new Integer(i)); |
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 |
< |
} |
171 |
< |
catch(ClassCastException success) {} |
216 |
> |
shouldThrow(); |
217 |
> |
} catch (ClassCastException success) {} |
218 |
|
} |
219 |
|
|
220 |
< |
public void testAdd(){ |
221 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
222 |
< |
for (int i = 0; i < N; ++i) { |
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()); |
227 |
|
assertTrue(q.add(new Integer(i))); |
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 |
< |
} |
188 |
< |
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 = new PriorityBlockingQueue(N); |
248 |
< |
Integer[] ints = new Integer[N]; |
249 |
< |
q.addAll(Arrays.asList(ints)); |
250 |
< |
fail("Cannot add null elements"); |
196 |
< |
} |
197 |
< |
catch (NullPointerException success) {} |
247 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
248 |
> |
q.addAll(q); |
249 |
> |
shouldThrow(); |
250 |
> |
} catch (IllegalArgumentException success) {} |
251 |
|
} |
252 |
< |
public void testAddAll3(){ |
252 |
> |
|
253 |
> |
/** |
254 |
> |
* addAll of a collection with null elements throws NPE |
255 |
> |
*/ |
256 |
> |
public void testAddAll2() { |
257 |
|
try { |
258 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
259 |
< |
Integer[] ints = new Integer[N]; |
203 |
< |
for (int i = 0; i < N-1; ++i) |
204 |
< |
ints[i] = new Integer(i); |
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 |
< |
} |
208 |
< |
catch (NullPointerException success) {} |
261 |
> |
shouldThrow(); |
262 |
> |
} catch (NullPointerException success) {} |
263 |
|
} |
264 |
|
|
265 |
< |
public void testAddAll5(){ |
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 |
< |
Integer[] empty = new Integer[0]; |
272 |
< |
Integer[] ints = new Integer[N]; |
273 |
< |
for (int i = N-1; i >= 0; --i) |
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 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
276 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
277 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
278 |
< |
for (int i = 0; i < N; ++i) |
279 |
< |
assertEquals(ints[i], q.poll()); |
280 |
< |
} |
281 |
< |
finally {} |
275 |
> |
q.addAll(Arrays.asList(ints)); |
276 |
> |
shouldThrow(); |
277 |
> |
} catch (NullPointerException success) {} |
278 |
> |
} |
279 |
> |
|
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 { |
300 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
299 |
> |
try { |
300 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
301 |
|
q.put(null); |
302 |
< |
fail("put should throw NPE"); |
303 |
< |
} |
232 |
< |
catch (NullPointerException success){ |
233 |
< |
} |
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(N); |
312 |
< |
for (int i = 0; i < N; ++i) { |
313 |
< |
Integer I = new Integer(i); |
314 |
< |
q.put(I); |
242 |
< |
assertTrue(q.contains(I)); |
243 |
< |
} |
244 |
< |
assertEquals(N, 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 { |
247 |
< |
} |
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 |
< |
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 |
< |
} |
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 |
< |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); |
350 |
< |
assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
289 |
< |
} finally { } |
290 |
< |
} |
291 |
< |
}); |
292 |
< |
|
293 |
< |
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 |
< |
} |
301 |
< |
} |
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 |
< |
public void testTake(){ |
353 |
< |
try { |
354 |
< |
PriorityBlockingQueue q = fullQueue(N); |
355 |
< |
for (int i = 0; i < N; ++i) { |
307 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
308 |
< |
} |
309 |
< |
} catch (InterruptedException e){ |
310 |
< |
fail("Unexpected exception"); |
311 |
< |
} |
352 |
> |
t.start(); |
353 |
> |
Thread.sleep(SMALL_DELAY_MS); |
354 |
> |
t.interrupt(); |
355 |
> |
t.join(); |
356 |
|
} |
357 |
|
|
358 |
< |
public void testTakeFromEmpty() { |
359 |
< |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
360 |
< |
Thread t = new Thread(new Runnable() { |
361 |
< |
public void run(){ |
362 |
< |
try { |
363 |
< |
q.take(); |
364 |
< |
fail("Should block"); |
321 |
< |
} catch (InterruptedException success){ } |
322 |
< |
} |
323 |
< |
}); |
324 |
< |
try { |
325 |
< |
t.start(); |
326 |
< |
Thread.sleep(SHORT_DELAY_MS); |
327 |
< |
t.interrupt(); |
328 |
< |
t.join(); |
329 |
< |
} catch (Exception e){ |
330 |
< |
fail("Unexpected exception"); |
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 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 |
> |
* peek returns next element, or null if empty |
449 |
> |
*/ |
450 |
> |
public void testPeek() { |
451 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
452 |
> |
for (int i = 0; i < SIZE; ++i) { |
453 |
> |
assertEquals(i, q.peek()); |
454 |
> |
assertEquals(i, q.poll()); |
455 |
|
assertTrue(q.peek() == null || |
456 |
< |
i != ((Integer)q.peek()).intValue()); |
456 |
> |
!q.peek().equals(i)); |
457 |
|
} |
458 |
< |
assertNull(q.peek()); |
458 |
> |
assertNull(q.peek()); |
459 |
|
} |
460 |
|
|
461 |
< |
public void testElement(){ |
462 |
< |
PriorityBlockingQueue q = fullQueue(N); |
463 |
< |
for (int i = 0; i < N; ++i) { |
464 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
465 |
< |
q.poll(); |
461 |
> |
/** |
462 |
> |
* element returns next element, or throws NSEE if empty |
463 |
> |
*/ |
464 |
> |
public void testElement() { |
465 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
466 |
> |
for (int i = 0; i < SIZE; ++i) { |
467 |
> |
assertEquals(i, q.element()); |
468 |
> |
assertEquals(i, q.poll()); |
469 |
|
} |
470 |
|
try { |
471 |
|
q.element(); |
472 |
< |
fail("no such element"); |
473 |
< |
} |
459 |
< |
catch (NoSuchElementException success) {} |
472 |
> |
shouldThrow(); |
473 |
> |
} catch (NoSuchElementException success) {} |
474 |
|
} |
475 |
|
|
476 |
< |
public void testRemove(){ |
477 |
< |
PriorityBlockingQueue q = fullQueue(N); |
478 |
< |
for (int i = 0; i < N; ++i) { |
479 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
476 |
> |
/** |
477 |
> |
* remove removes next element, or throws NSEE if empty |
478 |
> |
*/ |
479 |
> |
public void testRemove() { |
480 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
481 |
> |
for (int i = 0; i < SIZE; ++i) { |
482 |
> |
assertEquals(i, q.remove()); |
483 |
|
} |
484 |
|
try { |
485 |
|
q.remove(); |
486 |
< |
fail("remove should throw"); |
487 |
< |
} catch (NoSuchElementException success){ |
471 |
< |
} |
486 |
> |
shouldThrow(); |
487 |
> |
} catch (NoSuchElementException success) {} |
488 |
|
} |
489 |
|
|
490 |
< |
public void testRemoveElement(){ |
491 |
< |
PriorityBlockingQueue q = fullQueue(N); |
492 |
< |
for (int i = 1; i < N; i+=2) { |
490 |
> |
/** |
491 |
> |
* remove(x) removes x and returns true if present |
492 |
> |
*/ |
493 |
> |
public void testRemoveElement() { |
494 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
495 |
> |
for (int i = 1; i < SIZE; i+=2) { |
496 |
|
assertTrue(q.remove(new Integer(i))); |
497 |
|
} |
498 |
< |
for (int i = 0; i < N; i+=2) { |
498 |
> |
for (int i = 0; i < SIZE; i+=2) { |
499 |
|
assertTrue(q.remove(new Integer(i))); |
500 |
|
assertFalse(q.remove(new Integer(i+1))); |
501 |
|
} |
502 |
|
assertTrue(q.isEmpty()); |
503 |
|
} |
504 |
< |
|
505 |
< |
public void testContains(){ |
506 |
< |
PriorityBlockingQueue q = fullQueue(N); |
507 |
< |
for (int i = 0; i < N; ++i) { |
504 |
> |
|
505 |
> |
/** |
506 |
> |
* contains(x) reports true when elements added but not yet removed |
507 |
> |
*/ |
508 |
> |
public void testContains() { |
509 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
510 |
> |
for (int i = 0; i < SIZE; ++i) { |
511 |
|
assertTrue(q.contains(new Integer(i))); |
512 |
|
q.poll(); |
513 |
|
assertFalse(q.contains(new Integer(i))); |
514 |
|
} |
515 |
|
} |
516 |
|
|
517 |
< |
public void testClear(){ |
518 |
< |
PriorityBlockingQueue q = fullQueue(N); |
517 |
> |
/** |
518 |
> |
* clear removes all elements |
519 |
> |
*/ |
520 |
> |
public void testClear() { |
521 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
522 |
|
q.clear(); |
523 |
|
assertTrue(q.isEmpty()); |
524 |
|
assertEquals(0, q.size()); |
525 |
< |
assertEquals(NOCAP, q.remainingCapacity()); |
501 |
< |
q.add(new Integer(1)); |
525 |
> |
q.add(one); |
526 |
|
assertFalse(q.isEmpty()); |
527 |
+ |
assertTrue(q.contains(one)); |
528 |
|
q.clear(); |
529 |
|
assertTrue(q.isEmpty()); |
530 |
|
} |
531 |
|
|
532 |
< |
public void testContainsAll(){ |
533 |
< |
PriorityBlockingQueue q = fullQueue(N); |
534 |
< |
PriorityBlockingQueue p = new PriorityBlockingQueue(N); |
535 |
< |
for (int i = 0; i < N; ++i) { |
532 |
> |
/** |
533 |
> |
* containsAll(c) is true when c contains a subset of elements |
534 |
> |
*/ |
535 |
> |
public void testContainsAll() { |
536 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
537 |
> |
PriorityBlockingQueue p = new PriorityBlockingQueue(SIZE); |
538 |
> |
for (int i = 0; i < SIZE; ++i) { |
539 |
|
assertTrue(q.containsAll(p)); |
540 |
|
assertFalse(p.containsAll(q)); |
541 |
|
p.add(new Integer(i)); |
543 |
|
assertTrue(p.containsAll(q)); |
544 |
|
} |
545 |
|
|
546 |
< |
public void testRetainAll(){ |
547 |
< |
PriorityBlockingQueue q = fullQueue(N); |
548 |
< |
PriorityBlockingQueue p = fullQueue(N); |
549 |
< |
for (int i = 0; i < N; ++i) { |
546 |
> |
/** |
547 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
548 |
> |
*/ |
549 |
> |
public void testRetainAll() { |
550 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
551 |
> |
PriorityBlockingQueue p = populatedQueue(SIZE); |
552 |
> |
for (int i = 0; i < SIZE; ++i) { |
553 |
|
boolean changed = q.retainAll(p); |
554 |
|
if (i == 0) |
555 |
|
assertFalse(changed); |
557 |
|
assertTrue(changed); |
558 |
|
|
559 |
|
assertTrue(q.containsAll(p)); |
560 |
< |
assertEquals(N-i, q.size()); |
560 |
> |
assertEquals(SIZE-i, q.size()); |
561 |
|
p.remove(); |
562 |
|
} |
563 |
|
} |
564 |
|
|
565 |
< |
public void testRemoveAll(){ |
566 |
< |
for (int i = 1; i < N; ++i) { |
567 |
< |
PriorityBlockingQueue q = fullQueue(N); |
568 |
< |
PriorityBlockingQueue p = fullQueue(i); |
565 |
> |
/** |
566 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
567 |
> |
*/ |
568 |
> |
public void testRemoveAll() { |
569 |
> |
for (int i = 1; i < SIZE; ++i) { |
570 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
571 |
> |
PriorityBlockingQueue p = populatedQueue(i); |
572 |
|
assertTrue(q.removeAll(p)); |
573 |
< |
assertEquals(N-i, q.size()); |
573 |
> |
assertEquals(SIZE-i, q.size()); |
574 |
|
for (int j = 0; j < i; ++j) { |
575 |
|
Integer I = (Integer)(p.remove()); |
576 |
|
assertFalse(q.contains(I)); |
578 |
|
} |
579 |
|
} |
580 |
|
|
581 |
< |
public void testToArray(){ |
582 |
< |
PriorityBlockingQueue q = fullQueue(N); |
583 |
< |
Object[] o = q.toArray(); |
581 |
> |
/** |
582 |
> |
* toArray contains all elements |
583 |
> |
*/ |
584 |
> |
public void testToArray() throws InterruptedException { |
585 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
586 |
> |
Object[] o = q.toArray(); |
587 |
|
Arrays.sort(o); |
588 |
< |
try { |
589 |
< |
for(int i = 0; i < o.length; i++) |
553 |
< |
assertEquals(o[i], q.take()); |
554 |
< |
} catch (InterruptedException e){ |
555 |
< |
fail("Unexpected exception"); |
556 |
< |
} |
588 |
> |
for (int i = 0; i < o.length; i++) |
589 |
> |
assertEquals(o[i], q.take()); |
590 |
|
} |
591 |
|
|
592 |
< |
public void testToArray2(){ |
593 |
< |
PriorityBlockingQueue q = fullQueue(N); |
594 |
< |
Integer[] ints = new Integer[N]; |
595 |
< |
ints = (Integer[])q.toArray(ints); |
592 |
> |
/** |
593 |
> |
* toArray(a) contains all elements |
594 |
> |
*/ |
595 |
> |
public void testToArray2() throws InterruptedException { |
596 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
597 |
> |
Integer[] ints = new Integer[SIZE]; |
598 |
> |
ints = (Integer[])q.toArray(ints); |
599 |
|
Arrays.sort(ints); |
600 |
< |
try { |
601 |
< |
for(int i = 0; i < ints.length; i++) |
602 |
< |
assertEquals(ints[i], q.take()); |
603 |
< |
} catch (InterruptedException e){ |
604 |
< |
fail("Unexpected exception"); |
605 |
< |
} |
606 |
< |
} |
607 |
< |
|
608 |
< |
public void testIterator(){ |
609 |
< |
PriorityBlockingQueue q = fullQueue(N); |
600 |
> |
for (int i = 0; i < ints.length; i++) |
601 |
> |
assertEquals(ints[i], q.take()); |
602 |
> |
} |
603 |
> |
|
604 |
> |
/** |
605 |
> |
* toArray(null) throws NPE |
606 |
> |
*/ |
607 |
> |
public void testToArray_BadArg() { |
608 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
609 |
> |
try { |
610 |
> |
Object o[] = q.toArray(null); |
611 |
> |
shouldThrow(); |
612 |
> |
} catch (NullPointerException success) {} |
613 |
> |
} |
614 |
> |
|
615 |
> |
/** |
616 |
> |
* toArray with incompatible array type throws CCE |
617 |
> |
*/ |
618 |
> |
public void testToArray1_BadArg() { |
619 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
620 |
> |
try { |
621 |
> |
Object o[] = q.toArray(new String[10]); |
622 |
> |
shouldThrow(); |
623 |
> |
} catch (ArrayStoreException success) {} |
624 |
> |
} |
625 |
> |
|
626 |
> |
/** |
627 |
> |
* iterator iterates through all elements |
628 |
> |
*/ |
629 |
> |
public void testIterator() { |
630 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
631 |
|
int i = 0; |
632 |
< |
Iterator it = q.iterator(); |
633 |
< |
while(it.hasNext()) { |
632 |
> |
Iterator it = q.iterator(); |
633 |
> |
while (it.hasNext()) { |
634 |
|
assertTrue(q.contains(it.next())); |
635 |
|
++i; |
636 |
|
} |
637 |
< |
assertEquals(i, N); |
637 |
> |
assertEquals(i, SIZE); |
638 |
|
} |
639 |
|
|
640 |
< |
public void testIteratorRemove () { |
641 |
< |
|
640 |
> |
/** |
641 |
> |
* iterator.remove removes current element |
642 |
> |
*/ |
643 |
> |
public void testIteratorRemove() { |
644 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(3); |
586 |
– |
|
645 |
|
q.add(new Integer(2)); |
646 |
|
q.add(new Integer(1)); |
647 |
|
q.add(new Integer(3)); |
657 |
|
} |
658 |
|
|
659 |
|
|
660 |
< |
public void testToString(){ |
661 |
< |
PriorityBlockingQueue q = fullQueue(N); |
660 |
> |
/** |
661 |
> |
* toString contains toStrings of elements |
662 |
> |
*/ |
663 |
> |
public void testToString() { |
664 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
665 |
|
String s = q.toString(); |
666 |
< |
for (int i = 0; i < N; ++i) { |
666 |
> |
for (int i = 0; i < SIZE; ++i) { |
667 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
668 |
|
} |
669 |
< |
} |
669 |
> |
} |
670 |
|
|
671 |
+ |
/** |
672 |
+ |
* offer transfers elements across Executor tasks |
673 |
+ |
*/ |
674 |
|
public void testPollInExecutor() { |
611 |
– |
|
675 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
613 |
– |
|
676 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
677 |
+ |
executor.execute(new CheckedRunnable() { |
678 |
+ |
public void realRun() throws InterruptedException { |
679 |
+ |
assertNull(q.poll()); |
680 |
+ |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
681 |
+ |
assertTrue(q.isEmpty()); |
682 |
+ |
}}); |
683 |
+ |
|
684 |
+ |
executor.execute(new CheckedRunnable() { |
685 |
+ |
public void realRun() throws InterruptedException { |
686 |
+ |
Thread.sleep(SMALL_DELAY_MS); |
687 |
+ |
q.put(one); |
688 |
+ |
}}); |
689 |
|
|
690 |
< |
executor.execute(new Runnable() { |
691 |
< |
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 |
< |
}); |
690 |
> |
joinPool(executor); |
691 |
> |
} |
692 |
|
|
693 |
< |
executor.execute(new Runnable() { |
694 |
< |
public void run() { |
695 |
< |
try { |
696 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
697 |
< |
q.put(new Integer(1)); |
698 |
< |
} |
699 |
< |
catch (InterruptedException e) { |
700 |
< |
fail("should not be interrupted"); |
701 |
< |
} |
702 |
< |
} |
703 |
< |
}); |
704 |
< |
|
705 |
< |
executor.shutdown(); |
693 |
> |
/** |
694 |
> |
* A deserialized serialized queue has same elements |
695 |
> |
*/ |
696 |
> |
public void testSerialization() throws Exception { |
697 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
698 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
699 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
700 |
> |
out.writeObject(q); |
701 |
> |
out.close(); |
702 |
> |
|
703 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
704 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
705 |
> |
PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject(); |
706 |
> |
assertEquals(q.size(), r.size()); |
707 |
> |
while (!q.isEmpty()) |
708 |
> |
assertEquals(q.remove(), r.remove()); |
709 |
> |
} |
710 |
> |
|
711 |
> |
/** |
712 |
> |
* drainTo(null) throws NPE |
713 |
> |
*/ |
714 |
> |
public void testDrainToNull() { |
715 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
716 |
> |
try { |
717 |
> |
q.drainTo(null); |
718 |
> |
shouldThrow(); |
719 |
> |
} catch (NullPointerException success) {} |
720 |
> |
} |
721 |
|
|
722 |
+ |
/** |
723 |
+ |
* drainTo(this) throws IAE |
724 |
+ |
*/ |
725 |
+ |
public void testDrainToSelf() { |
726 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
727 |
+ |
try { |
728 |
+ |
q.drainTo(q); |
729 |
+ |
shouldThrow(); |
730 |
+ |
} catch (IllegalArgumentException success) {} |
731 |
|
} |
732 |
|
|
733 |
< |
public void testSerialization() { |
734 |
< |
PriorityBlockingQueue q = fullQueue(N); |
735 |
< |
try { |
736 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
737 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
738 |
< |
out.writeObject(q); |
739 |
< |
out.close(); |
740 |
< |
|
741 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
742 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
743 |
< |
PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject(); |
744 |
< |
assertEquals(q.size(), r.size()); |
745 |
< |
while (!q.isEmpty()) |
746 |
< |
assertEquals(q.remove(), r.remove()); |
747 |
< |
} catch(Exception e){ |
748 |
< |
fail("unexpected exception"); |
733 |
> |
/** |
734 |
> |
* drainTo(c) empties queue into another collection c |
735 |
> |
*/ |
736 |
> |
public void testDrainTo() { |
737 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
738 |
> |
ArrayList l = new ArrayList(); |
739 |
> |
q.drainTo(l); |
740 |
> |
assertEquals(q.size(), 0); |
741 |
> |
assertEquals(l.size(), SIZE); |
742 |
> |
for (int i = 0; i < SIZE; ++i) |
743 |
> |
assertEquals(l.get(i), new Integer(i)); |
744 |
> |
q.add(zero); |
745 |
> |
q.add(one); |
746 |
> |
assertFalse(q.isEmpty()); |
747 |
> |
assertTrue(q.contains(zero)); |
748 |
> |
assertTrue(q.contains(one)); |
749 |
> |
l.clear(); |
750 |
> |
q.drainTo(l); |
751 |
> |
assertEquals(q.size(), 0); |
752 |
> |
assertEquals(l.size(), 2); |
753 |
> |
for (int i = 0; i < 2; ++i) |
754 |
> |
assertEquals(l.get(i), new Integer(i)); |
755 |
> |
} |
756 |
> |
|
757 |
> |
/** |
758 |
> |
* drainTo empties queue |
759 |
> |
*/ |
760 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
761 |
> |
final PriorityBlockingQueue q = populatedQueue(SIZE); |
762 |
> |
Thread t = new Thread(new CheckedRunnable() { |
763 |
> |
public void realRun() { |
764 |
> |
q.put(new Integer(SIZE+1)); |
765 |
> |
}}); |
766 |
> |
|
767 |
> |
t.start(); |
768 |
> |
ArrayList l = new ArrayList(); |
769 |
> |
q.drainTo(l); |
770 |
> |
assertTrue(l.size() >= SIZE); |
771 |
> |
for (int i = 0; i < SIZE; ++i) |
772 |
> |
assertEquals(l.get(i), new Integer(i)); |
773 |
> |
t.join(); |
774 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
775 |
> |
} |
776 |
> |
|
777 |
> |
/** |
778 |
> |
* drainTo(null, n) throws NPE |
779 |
> |
*/ |
780 |
> |
public void testDrainToNullN() { |
781 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
782 |
> |
try { |
783 |
> |
q.drainTo(null, 0); |
784 |
> |
shouldThrow(); |
785 |
> |
} catch (NullPointerException success) {} |
786 |
> |
} |
787 |
> |
|
788 |
> |
/** |
789 |
> |
* drainTo(this, n) throws IAE |
790 |
> |
*/ |
791 |
> |
public void testDrainToSelfN() { |
792 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
793 |
> |
try { |
794 |
> |
q.drainTo(q, 0); |
795 |
> |
shouldThrow(); |
796 |
> |
} catch (IllegalArgumentException success) {} |
797 |
> |
} |
798 |
> |
|
799 |
> |
/** |
800 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
801 |
> |
*/ |
802 |
> |
public void testDrainToN() { |
803 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE*2); |
804 |
> |
for (int i = 0; i < SIZE + 2; ++i) { |
805 |
> |
for (int j = 0; j < SIZE; j++) |
806 |
> |
assertTrue(q.offer(new Integer(j))); |
807 |
> |
ArrayList l = new ArrayList(); |
808 |
> |
q.drainTo(l, i); |
809 |
> |
int k = (i < SIZE) ? i : SIZE; |
810 |
> |
assertEquals(l.size(), k); |
811 |
> |
assertEquals(q.size(), SIZE-k); |
812 |
> |
for (int j = 0; j < k; ++j) |
813 |
> |
assertEquals(l.get(j), new Integer(j)); |
814 |
> |
while (q.poll() != null) ; |
815 |
|
} |
816 |
|
} |
817 |
|
|