author | dl |
Wed, 09 Aug 2017 17:40:38 -0700 | |
changeset 46146 | b3e220a04d3f |
parent 45937 | 646816090183 |
permissions | -rw-r--r-- |
35394 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
6 |
* published by the Free Software Foundation. |
|
7 |
* |
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
12 |
* accompanied this code). |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License version |
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 |
* |
|
18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
19 |
* or visit www.oracle.com if you need additional information or have any |
|
20 |
* questions. |
|
21 |
*/ |
|
22 |
||
23 |
/* |
|
24 |
* This file is available under and governed by the GNU General Public |
|
25 |
* License version 2 only, as published by the Free Software Foundation. |
|
26 |
* However, the following notice accompanied the original version of this |
|
27 |
* file: |
|
28 |
* |
|
29 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
30 |
* Expert Group and released to the public domain, as explained at |
|
31 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
|
32 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
|
33 |
* Pat Fisher, Mike Judd. |
|
34 |
*/ |
|
35 |
||
36 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
|
37 |
||
38 |
import java.util.ArrayList; |
|
39 |
import java.util.Arrays; |
|
40 |
import java.util.Collection; |
|
41 |
import java.util.Comparator; |
|
42 |
import java.util.Iterator; |
|
43 |
import java.util.NoSuchElementException; |
|
44 |
import java.util.Queue; |
|
45 |
import java.util.concurrent.BlockingQueue; |
|
46 |
import java.util.concurrent.CountDownLatch; |
|
47 |
import java.util.concurrent.Executors; |
|
48 |
import java.util.concurrent.ExecutorService; |
|
49 |
import java.util.concurrent.PriorityBlockingQueue; |
|
50 |
||
51 |
import junit.framework.Test; |
|
52 |
||
53 |
public class PriorityBlockingQueueTest extends JSR166TestCase { |
|
54 |
||
55 |
public static class Generic extends BlockingQueueTest { |
|
56 |
protected BlockingQueue emptyCollection() { |
|
57 |
return new PriorityBlockingQueue(); |
|
58 |
} |
|
59 |
} |
|
60 |
||
61 |
public static class InitialCapacity extends BlockingQueueTest { |
|
62 |
protected BlockingQueue emptyCollection() { |
|
63 |
return new PriorityBlockingQueue(SIZE); |
|
64 |
} |
|
65 |
} |
|
66 |
||
67 |
public static void main(String[] args) { |
|
68 |
main(suite(), args); |
|
69 |
} |
|
70 |
||
71 |
public static Test suite() { |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
40280
diff
changeset
|
72 |
class Implementation implements CollectionImplementation { |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
40280
diff
changeset
|
73 |
public Class<?> klazz() { return PriorityBlockingQueue.class; } |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
40280
diff
changeset
|
74 |
public Collection emptyCollection() { return new PriorityBlockingQueue(); } |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
40280
diff
changeset
|
75 |
public Object makeElement(int i) { return i; } |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
40280
diff
changeset
|
76 |
public boolean isConcurrent() { return true; } |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
40280
diff
changeset
|
77 |
public boolean permitsNulls() { return false; } |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
40280
diff
changeset
|
78 |
} |
35394 | 79 |
return newTestSuite(PriorityBlockingQueueTest.class, |
80 |
new Generic().testSuite(), |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
40280
diff
changeset
|
81 |
new InitialCapacity().testSuite(), |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
40280
diff
changeset
|
82 |
CollectionTest.testSuite(new Implementation())); |
35394 | 83 |
} |
84 |
||
85 |
/** Sample Comparator */ |
|
86 |
static class MyReverseComparator implements Comparator { |
|
87 |
public int compare(Object x, Object y) { |
|
88 |
return ((Comparable)y).compareTo(x); |
|
89 |
} |
|
90 |
} |
|
91 |
||
92 |
/** |
|
93 |
* Returns a new queue of given size containing consecutive |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
40280
diff
changeset
|
94 |
* Integers 0 ... n - 1. |
35394 | 95 |
*/ |
44589
64d9270bd24c
8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents:
42322
diff
changeset
|
96 |
private static PriorityBlockingQueue<Integer> populatedQueue(int n) { |
35394 | 97 |
PriorityBlockingQueue<Integer> q = |
98 |
new PriorityBlockingQueue<Integer>(n); |
|
99 |
assertTrue(q.isEmpty()); |
|
100 |
for (int i = n - 1; i >= 0; i -= 2) |
|
101 |
assertTrue(q.offer(new Integer(i))); |
|
102 |
for (int i = (n & 1); i < n; i += 2) |
|
103 |
assertTrue(q.offer(new Integer(i))); |
|
104 |
assertFalse(q.isEmpty()); |
|
105 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
|
106 |
assertEquals(n, q.size()); |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
40280
diff
changeset
|
107 |
assertEquals((Integer) 0, q.peek()); |
35394 | 108 |
return q; |
109 |
} |
|
110 |
||
111 |
/** |
|
112 |
* A new queue has unbounded capacity |
|
113 |
*/ |
|
114 |
public void testConstructor1() { |
|
115 |
assertEquals(Integer.MAX_VALUE, |
|
116 |
new PriorityBlockingQueue(SIZE).remainingCapacity()); |
|
117 |
} |
|
118 |
||
119 |
/** |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
120 |
* Constructor throws IllegalArgumentException if capacity argument nonpositive |
35394 | 121 |
*/ |
122 |
public void testConstructor2() { |
|
123 |
try { |
|
124 |
new PriorityBlockingQueue(0); |
|
125 |
shouldThrow(); |
|
126 |
} catch (IllegalArgumentException success) {} |
|
127 |
} |
|
128 |
||
129 |
/** |
|
130 |
* Initializing from null Collection throws NPE |
|
131 |
*/ |
|
132 |
public void testConstructor3() { |
|
133 |
try { |
|
134 |
new PriorityBlockingQueue(null); |
|
135 |
shouldThrow(); |
|
136 |
} catch (NullPointerException success) {} |
|
137 |
} |
|
138 |
||
139 |
/** |
|
140 |
* Initializing from Collection of null elements throws NPE |
|
141 |
*/ |
|
142 |
public void testConstructor4() { |
|
143 |
Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
|
144 |
try { |
|
145 |
new PriorityBlockingQueue(elements); |
|
146 |
shouldThrow(); |
|
147 |
} catch (NullPointerException success) {} |
|
148 |
} |
|
149 |
||
150 |
/** |
|
151 |
* Initializing from Collection with some null elements throws NPE |
|
152 |
*/ |
|
153 |
public void testConstructor5() { |
|
154 |
Integer[] ints = new Integer[SIZE]; |
|
155 |
for (int i = 0; i < SIZE - 1; ++i) |
|
156 |
ints[i] = i; |
|
157 |
Collection<Integer> elements = Arrays.asList(ints); |
|
158 |
try { |
|
159 |
new PriorityBlockingQueue(elements); |
|
160 |
shouldThrow(); |
|
161 |
} catch (NullPointerException success) {} |
|
162 |
} |
|
163 |
||
164 |
/** |
|
165 |
* Queue contains all elements of collection used to initialize |
|
166 |
*/ |
|
167 |
public void testConstructor6() { |
|
168 |
Integer[] ints = new Integer[SIZE]; |
|
169 |
for (int i = 0; i < SIZE; ++i) |
|
170 |
ints[i] = i; |
|
171 |
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
|
172 |
for (int i = 0; i < SIZE; ++i) |
|
173 |
assertEquals(ints[i], q.poll()); |
|
174 |
} |
|
175 |
||
176 |
/** |
|
177 |
* The comparator used in constructor is used |
|
178 |
*/ |
|
179 |
public void testConstructor7() { |
|
180 |
MyReverseComparator cmp = new MyReverseComparator(); |
|
181 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp); |
|
182 |
assertEquals(cmp, q.comparator()); |
|
183 |
Integer[] ints = new Integer[SIZE]; |
|
184 |
for (int i = 0; i < SIZE; ++i) |
|
185 |
ints[i] = new Integer(i); |
|
186 |
q.addAll(Arrays.asList(ints)); |
|
187 |
for (int i = SIZE - 1; i >= 0; --i) |
|
188 |
assertEquals(ints[i], q.poll()); |
|
189 |
} |
|
190 |
||
191 |
/** |
|
192 |
* isEmpty is true before add, false after |
|
193 |
*/ |
|
194 |
public void testEmpty() { |
|
195 |
PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
|
196 |
assertTrue(q.isEmpty()); |
|
197 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
|
198 |
q.add(one); |
|
199 |
assertFalse(q.isEmpty()); |
|
200 |
q.add(two); |
|
201 |
q.remove(); |
|
202 |
q.remove(); |
|
203 |
assertTrue(q.isEmpty()); |
|
204 |
} |
|
205 |
||
206 |
/** |
|
207 |
* remainingCapacity() always returns Integer.MAX_VALUE |
|
208 |
*/ |
|
209 |
public void testRemainingCapacity() { |
|
210 |
BlockingQueue q = populatedQueue(SIZE); |
|
211 |
for (int i = 0; i < SIZE; ++i) { |
|
212 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
|
213 |
assertEquals(SIZE - i, q.size()); |
|
214 |
assertEquals(i, q.remove()); |
|
215 |
} |
|
216 |
for (int i = 0; i < SIZE; ++i) { |
|
217 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
|
218 |
assertEquals(i, q.size()); |
|
219 |
assertTrue(q.add(i)); |
|
220 |
} |
|
221 |
} |
|
222 |
||
223 |
/** |
|
224 |
* Offer of comparable element succeeds |
|
225 |
*/ |
|
226 |
public void testOffer() { |
|
227 |
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
|
228 |
assertTrue(q.offer(zero)); |
|
229 |
assertTrue(q.offer(one)); |
|
230 |
} |
|
231 |
||
232 |
/** |
|
233 |
* Offer of non-Comparable throws CCE |
|
234 |
*/ |
|
235 |
public void testOfferNonComparable() { |
|
236 |
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
|
237 |
try { |
|
238 |
q.offer(new Object()); |
|
239 |
shouldThrow(); |
|
39039
9dea20ff2dee
8066070: PriorityQueue corrupted when adding non-Comparable
dl
parents:
35394
diff
changeset
|
240 |
} catch (ClassCastException success) { |
9dea20ff2dee
8066070: PriorityQueue corrupted when adding non-Comparable
dl
parents:
35394
diff
changeset
|
241 |
assertTrue(q.isEmpty()); |
9dea20ff2dee
8066070: PriorityQueue corrupted when adding non-Comparable
dl
parents:
35394
diff
changeset
|
242 |
assertEquals(0, q.size()); |
9dea20ff2dee
8066070: PriorityQueue corrupted when adding non-Comparable
dl
parents:
35394
diff
changeset
|
243 |
assertNull(q.poll()); |
9dea20ff2dee
8066070: PriorityQueue corrupted when adding non-Comparable
dl
parents:
35394
diff
changeset
|
244 |
} |
35394 | 245 |
} |
246 |
||
247 |
/** |
|
248 |
* add of comparable succeeds |
|
249 |
*/ |
|
250 |
public void testAdd() { |
|
251 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
|
252 |
for (int i = 0; i < SIZE; ++i) { |
|
253 |
assertEquals(i, q.size()); |
|
254 |
assertTrue(q.add(new Integer(i))); |
|
255 |
} |
|
256 |
} |
|
257 |
||
258 |
/** |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
259 |
* addAll(this) throws IllegalArgumentException |
35394 | 260 |
*/ |
261 |
public void testAddAllSelf() { |
|
262 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
263 |
try { |
|
264 |
q.addAll(q); |
|
265 |
shouldThrow(); |
|
266 |
} catch (IllegalArgumentException success) {} |
|
267 |
} |
|
268 |
||
269 |
/** |
|
270 |
* addAll of a collection with any null elements throws NPE after |
|
271 |
* possibly adding some elements |
|
272 |
*/ |
|
273 |
public void testAddAll3() { |
|
274 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
|
275 |
Integer[] ints = new Integer[SIZE]; |
|
276 |
for (int i = 0; i < SIZE - 1; ++i) |
|
277 |
ints[i] = new Integer(i); |
|
278 |
try { |
|
279 |
q.addAll(Arrays.asList(ints)); |
|
280 |
shouldThrow(); |
|
281 |
} catch (NullPointerException success) {} |
|
282 |
} |
|
283 |
||
284 |
/** |
|
285 |
* Queue contains all elements of successful addAll |
|
286 |
*/ |
|
287 |
public void testAddAll5() { |
|
288 |
Integer[] empty = new Integer[0]; |
|
289 |
Integer[] ints = new Integer[SIZE]; |
|
290 |
for (int i = SIZE - 1; i >= 0; --i) |
|
291 |
ints[i] = new Integer(i); |
|
292 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
|
293 |
assertFalse(q.addAll(Arrays.asList(empty))); |
|
294 |
assertTrue(q.addAll(Arrays.asList(ints))); |
|
295 |
for (int i = 0; i < SIZE; ++i) |
|
296 |
assertEquals(ints[i], q.poll()); |
|
297 |
} |
|
298 |
||
299 |
/** |
|
300 |
* all elements successfully put are contained |
|
301 |
*/ |
|
302 |
public void testPut() { |
|
303 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
|
304 |
for (int i = 0; i < SIZE; ++i) { |
|
305 |
Integer x = new Integer(i); |
|
306 |
q.put(x); |
|
307 |
assertTrue(q.contains(x)); |
|
308 |
} |
|
309 |
assertEquals(SIZE, q.size()); |
|
310 |
} |
|
311 |
||
312 |
/** |
|
313 |
* put doesn't block waiting for take |
|
314 |
*/ |
|
315 |
public void testPutWithTake() throws InterruptedException { |
|
316 |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
|
317 |
final int size = 4; |
|
318 |
Thread t = newStartedThread(new CheckedRunnable() { |
|
319 |
public void realRun() { |
|
320 |
for (int i = 0; i < size; i++) |
|
321 |
q.put(new Integer(0)); |
|
322 |
}}); |
|
323 |
||
324 |
awaitTermination(t); |
|
325 |
assertEquals(size, q.size()); |
|
326 |
q.take(); |
|
327 |
} |
|
328 |
||
329 |
/** |
|
330 |
* timed offer does not time out |
|
331 |
*/ |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
332 |
public void testTimedOffer() { |
35394 | 333 |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
334 |
Thread t = newStartedThread(new CheckedRunnable() { |
|
335 |
public void realRun() { |
|
336 |
q.put(new Integer(0)); |
|
337 |
q.put(new Integer(0)); |
|
338 |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS)); |
|
339 |
assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS)); |
|
340 |
}}); |
|
341 |
||
342 |
awaitTermination(t); |
|
343 |
} |
|
344 |
||
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, q.take()); |
|
352 |
} |
|
353 |
} |
|
354 |
||
355 |
/** |
|
356 |
* Take removes existing elements until empty, then blocks interruptibly |
|
357 |
*/ |
|
358 |
public void testBlockingTake() throws InterruptedException { |
|
359 |
final PriorityBlockingQueue q = populatedQueue(SIZE); |
|
360 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
|
361 |
Thread t = newStartedThread(new CheckedRunnable() { |
|
362 |
public void realRun() throws InterruptedException { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
363 |
for (int i = 0; i < SIZE; i++) assertEquals(i, q.take()); |
35394 | 364 |
|
365 |
Thread.currentThread().interrupt(); |
|
366 |
try { |
|
367 |
q.take(); |
|
368 |
shouldThrow(); |
|
369 |
} catch (InterruptedException success) {} |
|
370 |
assertFalse(Thread.interrupted()); |
|
371 |
||
372 |
pleaseInterrupt.countDown(); |
|
373 |
try { |
|
374 |
q.take(); |
|
375 |
shouldThrow(); |
|
376 |
} catch (InterruptedException success) {} |
|
377 |
assertFalse(Thread.interrupted()); |
|
378 |
}}); |
|
379 |
||
380 |
await(pleaseInterrupt); |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
381 |
assertThreadBlocks(t, Thread.State.WAITING); |
35394 | 382 |
t.interrupt(); |
383 |
awaitTermination(t); |
|
384 |
} |
|
385 |
||
386 |
/** |
|
387 |
* poll succeeds unless empty |
|
388 |
*/ |
|
389 |
public void testPoll() { |
|
390 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
391 |
for (int i = 0; i < SIZE; ++i) { |
|
392 |
assertEquals(i, q.poll()); |
|
393 |
} |
|
394 |
assertNull(q.poll()); |
|
395 |
} |
|
396 |
||
397 |
/** |
|
398 |
* timed poll with zero timeout succeeds when non-empty, else times out |
|
399 |
*/ |
|
400 |
public void testTimedPoll0() throws InterruptedException { |
|
401 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
402 |
for (int i = 0; i < SIZE; ++i) { |
|
403 |
assertEquals(i, q.poll(0, MILLISECONDS)); |
|
404 |
} |
|
405 |
assertNull(q.poll(0, MILLISECONDS)); |
|
406 |
} |
|
407 |
||
408 |
/** |
|
409 |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
|
410 |
*/ |
|
411 |
public void testTimedPoll() throws InterruptedException { |
|
412 |
PriorityBlockingQueue<Integer> q = populatedQueue(SIZE); |
|
413 |
for (int i = 0; i < SIZE; ++i) { |
|
414 |
long startTime = System.nanoTime(); |
|
415 |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
|
416 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
|
417 |
} |
|
418 |
long startTime = System.nanoTime(); |
|
419 |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
|
420 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
|
421 |
checkEmpty(q); |
|
422 |
} |
|
423 |
||
424 |
/** |
|
425 |
* Interrupted timed poll throws InterruptedException instead of |
|
426 |
* returning timeout status |
|
427 |
*/ |
|
428 |
public void testInterruptedTimedPoll() throws InterruptedException { |
|
429 |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
430 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
35394 | 431 |
Thread t = newStartedThread(new CheckedRunnable() { |
432 |
public void realRun() throws InterruptedException { |
|
433 |
long startTime = System.nanoTime(); |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
434 |
for (int i = 0; i < SIZE; i++) |
35394 | 435 |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
436 |
|
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
437 |
Thread.currentThread().interrupt(); |
35394 | 438 |
try { |
439 |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
|
440 |
shouldThrow(); |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
441 |
} catch (InterruptedException success) {} |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
442 |
assertFalse(Thread.interrupted()); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
443 |
|
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
444 |
pleaseInterrupt.countDown(); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
445 |
try { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
446 |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
447 |
shouldThrow(); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
448 |
} catch (InterruptedException success) {} |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
449 |
assertFalse(Thread.interrupted()); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
450 |
|
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
451 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
35394 | 452 |
}}); |
453 |
||
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
454 |
await(pleaseInterrupt); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
455 |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
35394 | 456 |
t.interrupt(); |
457 |
awaitTermination(t); |
|
458 |
} |
|
459 |
||
460 |
/** |
|
461 |
* peek returns next element, or null if empty |
|
462 |
*/ |
|
463 |
public void testPeek() { |
|
464 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
465 |
for (int i = 0; i < SIZE; ++i) { |
|
466 |
assertEquals(i, q.peek()); |
|
467 |
assertEquals(i, q.poll()); |
|
468 |
assertTrue(q.peek() == null || |
|
469 |
!q.peek().equals(i)); |
|
470 |
} |
|
471 |
assertNull(q.peek()); |
|
472 |
} |
|
473 |
||
474 |
/** |
|
475 |
* element returns next element, or throws NSEE if empty |
|
476 |
*/ |
|
477 |
public void testElement() { |
|
478 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
479 |
for (int i = 0; i < SIZE; ++i) { |
|
480 |
assertEquals(i, q.element()); |
|
481 |
assertEquals(i, q.poll()); |
|
482 |
} |
|
483 |
try { |
|
484 |
q.element(); |
|
485 |
shouldThrow(); |
|
486 |
} catch (NoSuchElementException success) {} |
|
487 |
} |
|
488 |
||
489 |
/** |
|
490 |
* remove removes next element, or throws NSEE if empty |
|
491 |
*/ |
|
492 |
public void testRemove() { |
|
493 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
494 |
for (int i = 0; i < SIZE; ++i) { |
|
495 |
assertEquals(i, q.remove()); |
|
496 |
} |
|
497 |
try { |
|
498 |
q.remove(); |
|
499 |
shouldThrow(); |
|
500 |
} catch (NoSuchElementException success) {} |
|
501 |
} |
|
502 |
||
503 |
/** |
|
504 |
* contains(x) reports true when elements added but not yet removed |
|
505 |
*/ |
|
506 |
public void testContains() { |
|
507 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
508 |
for (int i = 0; i < SIZE; ++i) { |
|
509 |
assertTrue(q.contains(new Integer(i))); |
|
510 |
q.poll(); |
|
511 |
assertFalse(q.contains(new Integer(i))); |
|
512 |
} |
|
513 |
} |
|
514 |
||
515 |
/** |
|
516 |
* clear removes all elements |
|
517 |
*/ |
|
518 |
public void testClear() { |
|
519 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
520 |
q.clear(); |
|
521 |
assertTrue(q.isEmpty()); |
|
522 |
assertEquals(0, q.size()); |
|
523 |
q.add(one); |
|
524 |
assertFalse(q.isEmpty()); |
|
525 |
assertTrue(q.contains(one)); |
|
526 |
q.clear(); |
|
527 |
assertTrue(q.isEmpty()); |
|
528 |
} |
|
529 |
||
530 |
/** |
|
531 |
* containsAll(c) is true when c contains a subset of elements |
|
532 |
*/ |
|
533 |
public void testContainsAll() { |
|
534 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
535 |
PriorityBlockingQueue p = new PriorityBlockingQueue(SIZE); |
|
536 |
for (int i = 0; i < SIZE; ++i) { |
|
537 |
assertTrue(q.containsAll(p)); |
|
538 |
assertFalse(p.containsAll(q)); |
|
539 |
p.add(new Integer(i)); |
|
540 |
} |
|
541 |
assertTrue(p.containsAll(q)); |
|
542 |
} |
|
543 |
||
544 |
/** |
|
545 |
* retainAll(c) retains only those elements of c and reports true if changed |
|
546 |
*/ |
|
547 |
public void testRetainAll() { |
|
548 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
549 |
PriorityBlockingQueue p = populatedQueue(SIZE); |
|
550 |
for (int i = 0; i < SIZE; ++i) { |
|
551 |
boolean changed = q.retainAll(p); |
|
552 |
if (i == 0) |
|
553 |
assertFalse(changed); |
|
554 |
else |
|
555 |
assertTrue(changed); |
|
556 |
||
557 |
assertTrue(q.containsAll(p)); |
|
558 |
assertEquals(SIZE - i, q.size()); |
|
559 |
p.remove(); |
|
560 |
} |
|
561 |
} |
|
562 |
||
563 |
/** |
|
564 |
* removeAll(c) removes only those elements of c and reports true if changed |
|
565 |
*/ |
|
566 |
public void testRemoveAll() { |
|
567 |
for (int i = 1; i < SIZE; ++i) { |
|
568 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
569 |
PriorityBlockingQueue p = populatedQueue(i); |
|
570 |
assertTrue(q.removeAll(p)); |
|
571 |
assertEquals(SIZE - i, q.size()); |
|
572 |
for (int j = 0; j < i; ++j) { |
|
573 |
Integer x = (Integer)(p.remove()); |
|
574 |
assertFalse(q.contains(x)); |
|
575 |
} |
|
576 |
} |
|
577 |
} |
|
578 |
||
579 |
/** |
|
580 |
* toArray contains all elements |
|
581 |
*/ |
|
582 |
public void testToArray() throws InterruptedException { |
|
583 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
584 |
Object[] o = q.toArray(); |
|
585 |
Arrays.sort(o); |
|
586 |
for (int i = 0; i < o.length; i++) |
|
587 |
assertSame(o[i], q.take()); |
|
588 |
} |
|
589 |
||
590 |
/** |
|
591 |
* toArray(a) contains all elements |
|
592 |
*/ |
|
593 |
public void testToArray2() throws InterruptedException { |
|
594 |
PriorityBlockingQueue<Integer> q = populatedQueue(SIZE); |
|
595 |
Integer[] ints = new Integer[SIZE]; |
|
596 |
Integer[] array = q.toArray(ints); |
|
597 |
assertSame(ints, array); |
|
598 |
Arrays.sort(ints); |
|
599 |
for (int i = 0; i < ints.length; i++) |
|
600 |
assertSame(ints[i], q.take()); |
|
601 |
} |
|
602 |
||
603 |
/** |
|
604 |
* toArray(incompatible array type) throws ArrayStoreException |
|
605 |
*/ |
|
606 |
public void testToArray1_BadArg() { |
|
607 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
608 |
try { |
|
609 |
q.toArray(new String[10]); |
|
610 |
shouldThrow(); |
|
611 |
} catch (ArrayStoreException success) {} |
|
612 |
} |
|
613 |
||
614 |
/** |
|
615 |
* iterator iterates through all elements |
|
616 |
*/ |
|
617 |
public void testIterator() { |
|
618 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
619 |
Iterator it = q.iterator(); |
|
620 |
int i; |
|
621 |
for (i = 0; it.hasNext(); i++) |
|
622 |
assertTrue(q.contains(it.next())); |
|
623 |
assertEquals(i, SIZE); |
|
624 |
assertIteratorExhausted(it); |
|
625 |
} |
|
626 |
||
627 |
/** |
|
628 |
* iterator of empty collection has no elements |
|
629 |
*/ |
|
630 |
public void testEmptyIterator() { |
|
631 |
assertIteratorExhausted(new PriorityBlockingQueue().iterator()); |
|
632 |
} |
|
633 |
||
634 |
/** |
|
635 |
* iterator.remove removes current element |
|
636 |
*/ |
|
637 |
public void testIteratorRemove() { |
|
638 |
final PriorityBlockingQueue q = new PriorityBlockingQueue(3); |
|
639 |
q.add(new Integer(2)); |
|
640 |
q.add(new Integer(1)); |
|
641 |
q.add(new Integer(3)); |
|
642 |
||
643 |
Iterator it = q.iterator(); |
|
644 |
it.next(); |
|
645 |
it.remove(); |
|
646 |
||
647 |
it = q.iterator(); |
|
648 |
assertEquals(it.next(), new Integer(2)); |
|
649 |
assertEquals(it.next(), new Integer(3)); |
|
650 |
assertFalse(it.hasNext()); |
|
651 |
} |
|
652 |
||
653 |
/** |
|
654 |
* toString contains toStrings of elements |
|
655 |
*/ |
|
656 |
public void testToString() { |
|
657 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
658 |
String s = q.toString(); |
|
659 |
for (int i = 0; i < SIZE; ++i) { |
|
660 |
assertTrue(s.contains(String.valueOf(i))); |
|
661 |
} |
|
662 |
} |
|
663 |
||
664 |
/** |
|
665 |
* timed poll transfers elements across Executor tasks |
|
666 |
*/ |
|
667 |
public void testPollInExecutor() { |
|
668 |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
|
669 |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
|
670 |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
|
671 |
try (PoolCleaner cleaner = cleaner(executor)) { |
|
672 |
executor.execute(new CheckedRunnable() { |
|
673 |
public void realRun() throws InterruptedException { |
|
674 |
assertNull(q.poll()); |
|
675 |
threadsStarted.await(); |
|
676 |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
|
677 |
checkEmpty(q); |
|
678 |
}}); |
|
679 |
||
680 |
executor.execute(new CheckedRunnable() { |
|
681 |
public void realRun() throws InterruptedException { |
|
682 |
threadsStarted.await(); |
|
683 |
q.put(one); |
|
684 |
}}); |
|
685 |
} |
|
686 |
} |
|
687 |
||
688 |
/** |
|
46146
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
45937
diff
changeset
|
689 |
* A deserialized/reserialized queue has same elements |
35394 | 690 |
*/ |
691 |
public void testSerialization() throws Exception { |
|
692 |
Queue x = populatedQueue(SIZE); |
|
693 |
Queue y = serialClone(x); |
|
694 |
||
695 |
assertNotSame(x, y); |
|
696 |
assertEquals(x.size(), y.size()); |
|
697 |
while (!x.isEmpty()) { |
|
698 |
assertFalse(y.isEmpty()); |
|
699 |
assertEquals(x.remove(), y.remove()); |
|
700 |
} |
|
701 |
assertTrue(y.isEmpty()); |
|
702 |
} |
|
703 |
||
704 |
/** |
|
705 |
* drainTo(c) empties queue into another collection c |
|
706 |
*/ |
|
707 |
public void testDrainTo() { |
|
708 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
709 |
ArrayList l = new ArrayList(); |
|
710 |
q.drainTo(l); |
|
711 |
assertEquals(0, q.size()); |
|
712 |
assertEquals(SIZE, l.size()); |
|
713 |
for (int i = 0; i < SIZE; ++i) |
|
714 |
assertEquals(l.get(i), new Integer(i)); |
|
715 |
q.add(zero); |
|
716 |
q.add(one); |
|
717 |
assertFalse(q.isEmpty()); |
|
718 |
assertTrue(q.contains(zero)); |
|
719 |
assertTrue(q.contains(one)); |
|
720 |
l.clear(); |
|
721 |
q.drainTo(l); |
|
722 |
assertEquals(0, q.size()); |
|
723 |
assertEquals(2, l.size()); |
|
724 |
for (int i = 0; i < 2; ++i) |
|
725 |
assertEquals(l.get(i), new Integer(i)); |
|
726 |
} |
|
727 |
||
728 |
/** |
|
729 |
* drainTo empties queue |
|
730 |
*/ |
|
731 |
public void testDrainToWithActivePut() throws InterruptedException { |
|
732 |
final PriorityBlockingQueue q = populatedQueue(SIZE); |
|
733 |
Thread t = new Thread(new CheckedRunnable() { |
|
734 |
public void realRun() { |
|
735 |
q.put(new Integer(SIZE + 1)); |
|
736 |
}}); |
|
737 |
||
738 |
t.start(); |
|
739 |
ArrayList l = new ArrayList(); |
|
740 |
q.drainTo(l); |
|
741 |
assertTrue(l.size() >= SIZE); |
|
742 |
for (int i = 0; i < SIZE; ++i) |
|
743 |
assertEquals(l.get(i), new Integer(i)); |
|
744 |
t.join(); |
|
745 |
assertTrue(q.size() + l.size() >= SIZE); |
|
746 |
} |
|
747 |
||
748 |
/** |
|
749 |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
|
750 |
*/ |
|
751 |
public void testDrainToN() { |
|
752 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE * 2); |
|
753 |
for (int i = 0; i < SIZE + 2; ++i) { |
|
754 |
for (int j = 0; j < SIZE; j++) |
|
755 |
assertTrue(q.offer(new Integer(j))); |
|
756 |
ArrayList l = new ArrayList(); |
|
757 |
q.drainTo(l, i); |
|
758 |
int k = (i < SIZE) ? i : SIZE; |
|
759 |
assertEquals(k, l.size()); |
|
760 |
assertEquals(SIZE - k, q.size()); |
|
761 |
for (int j = 0; j < k; ++j) |
|
762 |
assertEquals(l.get(j), new Integer(j)); |
|
763 |
do {} while (q.poll() != null); |
|
764 |
} |
|
765 |
} |
|
766 |
||
767 |
/** |
|
768 |
* remove(null), contains(null) always return false |
|
769 |
*/ |
|
770 |
public void testNeverContainsNull() { |
|
771 |
Collection<?>[] qs = { |
|
772 |
new PriorityBlockingQueue<Object>(), |
|
773 |
populatedQueue(2), |
|
774 |
}; |
|
775 |
||
776 |
for (Collection<?> q : qs) { |
|
777 |
assertFalse(q.contains(null)); |
|
778 |
assertFalse(q.remove(null)); |
|
779 |
} |
|
780 |
} |
|
781 |
||
782 |
} |