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