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.*; |
10 |
> |
import java.util.Arrays; |
11 |
> |
import java.util.ArrayList; |
12 |
> |
import java.util.Collection; |
13 |
> |
import java.util.Iterator; |
14 |
> |
import java.util.NoSuchElementException; |
15 |
> |
import java.util.Queue; |
16 |
> |
import java.util.concurrent.BlockingQueue; |
17 |
> |
import java.util.concurrent.CountDownLatch; |
18 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
19 |
> |
import java.util.concurrent.Executors; |
20 |
> |
import java.util.concurrent.ExecutorService; |
21 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
– |
import java.io.*; |
22 |
|
|
23 |
|
public class LinkedBlockingQueueTest extends JSR166TestCase { |
24 |
|
|
25 |
+ |
public static class Unbounded extends BlockingQueueTest { |
26 |
+ |
protected BlockingQueue emptyCollection() { |
27 |
+ |
return new LinkedBlockingQueue(); |
28 |
+ |
} |
29 |
+ |
} |
30 |
+ |
|
31 |
+ |
public static class Bounded extends BlockingQueueTest { |
32 |
+ |
protected BlockingQueue emptyCollection() { |
33 |
+ |
return new LinkedBlockingQueue(SIZE); |
34 |
+ |
} |
35 |
+ |
} |
36 |
+ |
|
37 |
|
public static void main(String[] args) { |
38 |
< |
junit.textui.TestRunner.run (suite()); |
38 |
> |
junit.textui.TestRunner.run(suite()); |
39 |
|
} |
40 |
|
|
41 |
|
public static Test suite() { |
42 |
< |
return new TestSuite(LinkedBlockingQueueTest.class); |
42 |
> |
return newTestSuite(LinkedBlockingQueueTest.class, |
43 |
> |
new Unbounded().testSuite(), |
44 |
> |
new Bounded().testSuite()); |
45 |
|
} |
46 |
|
|
25 |
– |
|
47 |
|
/** |
48 |
< |
* Create a queue of given size containing consecutive |
48 |
> |
* Returns a new queue of given size containing consecutive |
49 |
|
* Integers 0 ... n. |
50 |
|
*/ |
51 |
< |
private LinkedBlockingQueue populatedQueue(int n) { |
52 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(n); |
51 |
> |
private LinkedBlockingQueue<Integer> populatedQueue(int n) { |
52 |
> |
LinkedBlockingQueue<Integer> q = |
53 |
> |
new LinkedBlockingQueue<Integer>(n); |
54 |
|
assertTrue(q.isEmpty()); |
55 |
|
for (int i = 0; i < n; i++) |
56 |
|
assertTrue(q.offer(new Integer(i))); |
70 |
|
} |
71 |
|
|
72 |
|
/** |
73 |
< |
* Constructor throws IAE if capacity argument nonpositive |
73 |
> |
* Constructor throws IllegalArgumentException if capacity argument nonpositive |
74 |
|
*/ |
75 |
|
public void testConstructor2() { |
76 |
|
try { |
77 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(0); |
77 |
> |
new LinkedBlockingQueue(0); |
78 |
|
shouldThrow(); |
79 |
|
} catch (IllegalArgumentException success) {} |
80 |
|
} |
81 |
|
|
82 |
|
/** |
83 |
< |
* Initializing from null Collection throws NPE |
83 |
> |
* Initializing from null Collection throws NullPointerException |
84 |
|
*/ |
85 |
|
public void testConstructor3() { |
86 |
|
try { |
87 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(null); |
87 |
> |
new LinkedBlockingQueue(null); |
88 |
|
shouldThrow(); |
89 |
|
} catch (NullPointerException success) {} |
90 |
|
} |
91 |
|
|
92 |
|
/** |
93 |
< |
* Initializing from Collection of null elements throws NPE |
93 |
> |
* Initializing from Collection of null elements throws NullPointerException |
94 |
|
*/ |
95 |
|
public void testConstructor4() { |
96 |
+ |
Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
97 |
|
try { |
98 |
< |
Integer[] ints = new Integer[SIZE]; |
76 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
98 |
> |
new LinkedBlockingQueue(elements); |
99 |
|
shouldThrow(); |
100 |
|
} catch (NullPointerException success) {} |
101 |
|
} |
102 |
|
|
103 |
|
/** |
104 |
< |
* Initializing from Collection with some null elements throws NPE |
104 |
> |
* Initializing from Collection with some null elements throws |
105 |
> |
* NullPointerException |
106 |
|
*/ |
107 |
|
public void testConstructor5() { |
108 |
+ |
Integer[] ints = new Integer[SIZE]; |
109 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
110 |
+ |
ints[i] = new Integer(i); |
111 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
112 |
|
try { |
113 |
< |
Integer[] ints = new Integer[SIZE]; |
87 |
< |
for (int i = 0; i < SIZE-1; ++i) |
88 |
< |
ints[i] = new Integer(i); |
89 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
113 |
> |
new LinkedBlockingQueue(elements); |
114 |
|
shouldThrow(); |
115 |
|
} catch (NullPointerException success) {} |
116 |
|
} |
160 |
|
} |
161 |
|
|
162 |
|
/** |
139 |
– |
* offer(null) throws NPE |
140 |
– |
*/ |
141 |
– |
public void testOfferNull() { |
142 |
– |
try { |
143 |
– |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
144 |
– |
q.offer(null); |
145 |
– |
shouldThrow(); |
146 |
– |
} catch (NullPointerException success) {} |
147 |
– |
} |
148 |
– |
|
149 |
– |
/** |
150 |
– |
* add(null) throws NPE |
151 |
– |
*/ |
152 |
– |
public void testAddNull() { |
153 |
– |
try { |
154 |
– |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
155 |
– |
q.add(null); |
156 |
– |
shouldThrow(); |
157 |
– |
} catch (NullPointerException success) {} |
158 |
– |
} |
159 |
– |
|
160 |
– |
/** |
163 |
|
* Offer succeeds if not full; fails if full |
164 |
|
*/ |
165 |
|
public void testOffer() { |
169 |
|
} |
170 |
|
|
171 |
|
/** |
172 |
< |
* add succeeds if not full; throws ISE if full |
172 |
> |
* add succeeds if not full; throws IllegalStateException if full |
173 |
|
*/ |
174 |
|
public void testAdd() { |
175 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
176 |
+ |
for (int i = 0; i < SIZE; ++i) |
177 |
+ |
assertTrue(q.add(new Integer(i))); |
178 |
+ |
assertEquals(0, q.remainingCapacity()); |
179 |
|
try { |
174 |
– |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
175 |
– |
for (int i = 0; i < SIZE; ++i) { |
176 |
– |
assertTrue(q.add(new Integer(i))); |
177 |
– |
} |
178 |
– |
assertEquals(0, q.remainingCapacity()); |
180 |
|
q.add(new Integer(SIZE)); |
181 |
|
shouldThrow(); |
182 |
|
} catch (IllegalStateException success) {} |
183 |
|
} |
184 |
|
|
185 |
|
/** |
186 |
< |
* addAll(null) throws NPE |
186 |
< |
*/ |
187 |
< |
public void testAddAll1() { |
188 |
< |
try { |
189 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
190 |
< |
q.addAll(null); |
191 |
< |
shouldThrow(); |
192 |
< |
} catch (NullPointerException success) {} |
193 |
< |
} |
194 |
< |
|
195 |
< |
/** |
196 |
< |
* addAll(this) throws IAE |
186 |
> |
* addAll(this) throws IllegalArgumentException |
187 |
|
*/ |
188 |
|
public void testAddAllSelf() { |
189 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
190 |
|
try { |
200 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
191 |
|
q.addAll(q); |
192 |
|
shouldThrow(); |
193 |
|
} catch (IllegalArgumentException success) {} |
194 |
|
} |
195 |
|
|
196 |
|
/** |
207 |
– |
* addAll of a collection with null elements throws NPE |
208 |
– |
*/ |
209 |
– |
public void testAddAll2() { |
210 |
– |
try { |
211 |
– |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
212 |
– |
Integer[] ints = new Integer[SIZE]; |
213 |
– |
q.addAll(Arrays.asList(ints)); |
214 |
– |
shouldThrow(); |
215 |
– |
} catch (NullPointerException success) {} |
216 |
– |
} |
217 |
– |
/** |
197 |
|
* addAll of a collection with any null elements throws NPE after |
198 |
|
* possibly adding some elements |
199 |
|
*/ |
200 |
|
public void testAddAll3() { |
201 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
202 |
+ |
Integer[] ints = new Integer[SIZE]; |
203 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
204 |
+ |
ints[i] = new Integer(i); |
205 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
206 |
|
try { |
207 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
224 |
< |
Integer[] ints = new Integer[SIZE]; |
225 |
< |
for (int i = 0; i < SIZE-1; ++i) |
226 |
< |
ints[i] = new Integer(i); |
227 |
< |
q.addAll(Arrays.asList(ints)); |
207 |
> |
q.addAll(elements); |
208 |
|
shouldThrow(); |
209 |
|
} catch (NullPointerException success) {} |
210 |
|
} |
211 |
+ |
|
212 |
|
/** |
213 |
< |
* addAll throws ISE if not enough room |
213 |
> |
* addAll throws IllegalStateException if not enough room |
214 |
|
*/ |
215 |
|
public void testAddAll4() { |
216 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE - 1); |
217 |
+ |
Integer[] ints = new Integer[SIZE]; |
218 |
+ |
for (int i = 0; i < SIZE; ++i) |
219 |
+ |
ints[i] = new Integer(i); |
220 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
221 |
|
try { |
222 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
237 |
< |
Integer[] ints = new Integer[SIZE]; |
238 |
< |
for (int i = 0; i < SIZE; ++i) |
239 |
< |
ints[i] = new Integer(i); |
240 |
< |
q.addAll(Arrays.asList(ints)); |
222 |
> |
q.addAll(elements); |
223 |
|
shouldThrow(); |
224 |
|
} catch (IllegalStateException success) {} |
225 |
|
} |
226 |
+ |
|
227 |
|
/** |
228 |
|
* Queue contains all elements, in traversal order, of successful addAll |
229 |
|
*/ |
240 |
|
} |
241 |
|
|
242 |
|
/** |
260 |
– |
* put(null) throws NPE |
261 |
– |
*/ |
262 |
– |
public void testPutNull() throws InterruptedException { |
263 |
– |
try { |
264 |
– |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
265 |
– |
q.put(null); |
266 |
– |
shouldThrow(); |
267 |
– |
} catch (NullPointerException success) {} |
268 |
– |
} |
269 |
– |
|
270 |
– |
/** |
243 |
|
* all elements successfully put are contained |
244 |
|
*/ |
245 |
|
public void testPut() throws InterruptedException { |
256 |
|
* put blocks interruptibly if full |
257 |
|
*/ |
258 |
|
public void testBlockingPut() throws InterruptedException { |
259 |
< |
Thread t = new Thread(new CheckedRunnable() { |
260 |
< |
public void realRun() { |
261 |
< |
int added = 0; |
259 |
> |
final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
260 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
261 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
262 |
> |
public void realRun() throws InterruptedException { |
263 |
> |
for (int i = 0; i < SIZE; ++i) |
264 |
> |
q.put(i); |
265 |
> |
assertEquals(SIZE, q.size()); |
266 |
> |
assertEquals(0, q.remainingCapacity()); |
267 |
> |
|
268 |
> |
Thread.currentThread().interrupt(); |
269 |
|
try { |
270 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
271 |
< |
for (int i = 0; i < SIZE; ++i) { |
272 |
< |
q.put(new Integer(i)); |
273 |
< |
++added; |
274 |
< |
} |
275 |
< |
q.put(new Integer(SIZE)); |
276 |
< |
threadShouldThrow(); |
277 |
< |
} catch (InterruptedException success) { |
278 |
< |
threadAssertEquals(added, SIZE); |
279 |
< |
} |
270 |
> |
q.put(99); |
271 |
> |
shouldThrow(); |
272 |
> |
} catch (InterruptedException success) {} |
273 |
> |
assertFalse(Thread.interrupted()); |
274 |
> |
|
275 |
> |
pleaseInterrupt.countDown(); |
276 |
> |
try { |
277 |
> |
q.put(99); |
278 |
> |
shouldThrow(); |
279 |
> |
} catch (InterruptedException success) {} |
280 |
> |
assertFalse(Thread.interrupted()); |
281 |
|
}}); |
282 |
|
|
283 |
< |
t.start(); |
284 |
< |
Thread.sleep(SHORT_DELAY_MS); |
283 |
> |
await(pleaseInterrupt); |
284 |
> |
assertThreadStaysAlive(t); |
285 |
|
t.interrupt(); |
286 |
< |
t.join(); |
286 |
> |
awaitTermination(t); |
287 |
> |
assertEquals(SIZE, q.size()); |
288 |
> |
assertEquals(0, q.remainingCapacity()); |
289 |
|
} |
290 |
|
|
291 |
|
/** |
292 |
< |
* put blocks waiting for take when full |
292 |
> |
* put blocks interruptibly waiting for take when full |
293 |
|
*/ |
294 |
|
public void testPutWithTake() throws InterruptedException { |
295 |
+ |
final int capacity = 2; |
296 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
297 |
< |
Thread t = new Thread(new CheckedRunnable() { |
298 |
< |
public void realRun() { |
299 |
< |
int added = 0; |
297 |
> |
final CountDownLatch pleaseTake = new CountDownLatch(1); |
298 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
299 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
300 |
> |
public void realRun() throws InterruptedException { |
301 |
> |
for (int i = 0; i < capacity; i++) |
302 |
> |
q.put(i); |
303 |
> |
pleaseTake.countDown(); |
304 |
> |
q.put(86); |
305 |
> |
|
306 |
> |
pleaseInterrupt.countDown(); |
307 |
|
try { |
308 |
< |
q.put(new Object()); |
309 |
< |
++added; |
310 |
< |
q.put(new Object()); |
311 |
< |
++added; |
322 |
< |
q.put(new Object()); |
323 |
< |
++added; |
324 |
< |
q.put(new Object()); |
325 |
< |
++added; |
326 |
< |
threadShouldThrow(); |
327 |
< |
} catch (InterruptedException success) { |
328 |
< |
threadAssertTrue(added >= 2); |
329 |
< |
} |
308 |
> |
q.put(99); |
309 |
> |
shouldThrow(); |
310 |
> |
} catch (InterruptedException success) {} |
311 |
> |
assertFalse(Thread.interrupted()); |
312 |
|
}}); |
313 |
|
|
314 |
< |
t.start(); |
315 |
< |
Thread.sleep(SHORT_DELAY_MS); |
316 |
< |
q.take(); |
314 |
> |
await(pleaseTake); |
315 |
> |
assertEquals(0, q.remainingCapacity()); |
316 |
> |
assertEquals(0, q.take()); |
317 |
> |
|
318 |
> |
await(pleaseInterrupt); |
319 |
> |
assertThreadStaysAlive(t); |
320 |
|
t.interrupt(); |
321 |
< |
t.join(); |
321 |
> |
awaitTermination(t); |
322 |
> |
assertEquals(0, q.remainingCapacity()); |
323 |
|
} |
324 |
|
|
325 |
|
/** |
326 |
|
* timed offer times out if full and elements not taken |
327 |
|
*/ |
328 |
< |
public void testTimedOffer() throws InterruptedException { |
328 |
> |
public void testTimedOffer() { |
329 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
330 |
< |
Thread t = new Thread(new CheckedRunnable() { |
330 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
331 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
332 |
|
public void realRun() throws InterruptedException { |
333 |
|
q.put(new Object()); |
334 |
|
q.put(new Object()); |
335 |
< |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
335 |
> |
long startTime = System.nanoTime(); |
336 |
> |
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
337 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
338 |
> |
pleaseInterrupt.countDown(); |
339 |
|
try { |
340 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
340 |
> |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
341 |
|
shouldThrow(); |
342 |
|
} catch (InterruptedException success) {} |
343 |
|
}}); |
344 |
|
|
345 |
< |
t.start(); |
346 |
< |
Thread.sleep(SMALL_DELAY_MS); |
345 |
> |
await(pleaseInterrupt); |
346 |
> |
assertThreadStaysAlive(t); |
347 |
|
t.interrupt(); |
348 |
< |
t.join(); |
348 |
> |
awaitTermination(t); |
349 |
|
} |
350 |
|
|
351 |
|
/** |
354 |
|
public void testTake() throws InterruptedException { |
355 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
356 |
|
for (int i = 0; i < SIZE; ++i) { |
357 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
357 |
> |
assertEquals(i, q.take()); |
358 |
|
} |
359 |
|
} |
360 |
|
|
361 |
|
/** |
372 |
– |
* take blocks interruptibly when empty |
373 |
– |
*/ |
374 |
– |
public void testTakeFromEmpty() throws InterruptedException { |
375 |
– |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
376 |
– |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
377 |
– |
public void realRun() throws InterruptedException { |
378 |
– |
q.take(); |
379 |
– |
}}; |
380 |
– |
|
381 |
– |
t.start(); |
382 |
– |
Thread.sleep(SHORT_DELAY_MS); |
383 |
– |
t.interrupt(); |
384 |
– |
t.join(); |
385 |
– |
} |
386 |
– |
|
387 |
– |
/** |
362 |
|
* Take removes existing elements until empty, then blocks interruptibly |
363 |
|
*/ |
364 |
|
public void testBlockingTake() throws InterruptedException { |
365 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
365 |
> |
final BlockingQueue q = populatedQueue(SIZE); |
366 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
367 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
368 |
|
public void realRun() throws InterruptedException { |
393 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
369 |
|
for (int i = 0; i < SIZE; ++i) { |
370 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
370 |
> |
assertEquals(i, q.take()); |
371 |
|
} |
397 |
– |
q.take(); |
398 |
– |
}}; |
372 |
|
|
373 |
< |
t.start(); |
374 |
< |
Thread.sleep(SHORT_DELAY_MS); |
373 |
> |
Thread.currentThread().interrupt(); |
374 |
> |
try { |
375 |
> |
q.take(); |
376 |
> |
shouldThrow(); |
377 |
> |
} catch (InterruptedException success) {} |
378 |
> |
assertFalse(Thread.interrupted()); |
379 |
> |
|
380 |
> |
pleaseInterrupt.countDown(); |
381 |
> |
try { |
382 |
> |
q.take(); |
383 |
> |
shouldThrow(); |
384 |
> |
} catch (InterruptedException success) {} |
385 |
> |
assertFalse(Thread.interrupted()); |
386 |
> |
}}); |
387 |
> |
|
388 |
> |
await(pleaseInterrupt); |
389 |
> |
assertThreadStaysAlive(t); |
390 |
|
t.interrupt(); |
391 |
< |
t.join(); |
391 |
> |
awaitTermination(t); |
392 |
|
} |
393 |
|
|
406 |
– |
|
394 |
|
/** |
395 |
|
* poll succeeds unless empty |
396 |
|
*/ |
397 |
|
public void testPoll() { |
398 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
399 |
|
for (int i = 0; i < SIZE; ++i) { |
400 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
400 |
> |
assertEquals(i, q.poll()); |
401 |
|
} |
402 |
|
assertNull(q.poll()); |
403 |
|
} |
404 |
|
|
405 |
|
/** |
406 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
406 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
407 |
|
*/ |
408 |
|
public void testTimedPoll0() throws InterruptedException { |
409 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
410 |
|
for (int i = 0; i < SIZE; ++i) { |
411 |
< |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
411 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
412 |
|
} |
413 |
|
assertNull(q.poll(0, MILLISECONDS)); |
414 |
|
} |
415 |
|
|
416 |
|
/** |
417 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
417 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
418 |
|
*/ |
419 |
|
public void testTimedPoll() throws InterruptedException { |
420 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
420 |
> |
LinkedBlockingQueue<Integer> q = populatedQueue(SIZE); |
421 |
|
for (int i = 0; i < SIZE; ++i) { |
422 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
423 |
< |
} |
424 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
422 |
> |
long startTime = System.nanoTime(); |
423 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
424 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
425 |
> |
} |
426 |
> |
long startTime = System.nanoTime(); |
427 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
428 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
429 |
> |
checkEmpty(q); |
430 |
|
} |
431 |
|
|
432 |
|
/** |
434 |
|
* returning timeout status |
435 |
|
*/ |
436 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
437 |
< |
Thread t = new Thread(new CheckedRunnable() { |
437 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
438 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
439 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
440 |
|
public void realRun() throws InterruptedException { |
447 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
441 |
|
for (int i = 0; i < SIZE; ++i) { |
442 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
442 |
> |
long t0 = System.nanoTime(); |
443 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
444 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
445 |
|
} |
446 |
+ |
long t0 = System.nanoTime(); |
447 |
+ |
aboutToWait.countDown(); |
448 |
|
try { |
449 |
< |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
449 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
450 |
|
shouldThrow(); |
451 |
< |
} catch (InterruptedException success) {} |
452 |
< |
}}); |
453 |
< |
|
457 |
< |
t.start(); |
458 |
< |
Thread.sleep(SHORT_DELAY_MS); |
459 |
< |
t.interrupt(); |
460 |
< |
t.join(); |
461 |
< |
} |
462 |
< |
|
463 |
< |
/** |
464 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
465 |
< |
* on interruption throws |
466 |
< |
*/ |
467 |
< |
public void testTimedPollWithOffer() throws InterruptedException { |
468 |
< |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
469 |
< |
Thread t = new Thread(new CheckedRunnable() { |
470 |
< |
public void realRun() throws InterruptedException { |
471 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
472 |
< |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
473 |
< |
try { |
474 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
475 |
< |
shouldThrow(); |
476 |
< |
} catch (InterruptedException success) {} |
451 |
> |
} catch (InterruptedException success) { |
452 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
453 |
> |
} |
454 |
|
}}); |
455 |
|
|
456 |
< |
t.start(); |
457 |
< |
Thread.sleep(SMALL_DELAY_MS); |
481 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
456 |
> |
aboutToWait.await(); |
457 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
458 |
|
t.interrupt(); |
459 |
< |
t.join(); |
459 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
460 |
> |
checkEmpty(q); |
461 |
|
} |
462 |
|
|
463 |
|
/** |
466 |
|
public void testPeek() { |
467 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
468 |
|
for (int i = 0; i < SIZE; ++i) { |
469 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
470 |
< |
q.poll(); |
469 |
> |
assertEquals(i, q.peek()); |
470 |
> |
assertEquals(i, q.poll()); |
471 |
|
assertTrue(q.peek() == null || |
472 |
< |
i != ((Integer)q.peek()).intValue()); |
472 |
> |
!q.peek().equals(i)); |
473 |
|
} |
474 |
|
assertNull(q.peek()); |
475 |
|
} |
480 |
|
public void testElement() { |
481 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
482 |
|
for (int i = 0; i < SIZE; ++i) { |
483 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
484 |
< |
q.poll(); |
483 |
> |
assertEquals(i, q.element()); |
484 |
> |
assertEquals(i, q.poll()); |
485 |
|
} |
486 |
|
try { |
487 |
|
q.element(); |
495 |
|
public void testRemove() { |
496 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
497 |
|
for (int i = 0; i < SIZE; ++i) { |
498 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
498 |
> |
assertEquals(i, q.remove()); |
499 |
|
} |
500 |
|
try { |
501 |
|
q.remove(); |
504 |
|
} |
505 |
|
|
506 |
|
/** |
530 |
– |
* remove(x) removes x and returns true if present |
531 |
– |
*/ |
532 |
– |
public void testRemoveElement() { |
533 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
534 |
– |
for (int i = 1; i < SIZE; i+=2) { |
535 |
– |
assertTrue(q.remove(new Integer(i))); |
536 |
– |
} |
537 |
– |
for (int i = 0; i < SIZE; i+=2) { |
538 |
– |
assertTrue(q.remove(new Integer(i))); |
539 |
– |
assertFalse(q.remove(new Integer(i+1))); |
540 |
– |
} |
541 |
– |
assertTrue(q.isEmpty()); |
542 |
– |
} |
543 |
– |
|
544 |
– |
/** |
507 |
|
* An add following remove(x) succeeds |
508 |
|
*/ |
509 |
|
public void testRemoveElementAndAdd() throws InterruptedException { |
513 |
|
assertTrue(q.remove(new Integer(1))); |
514 |
|
assertTrue(q.remove(new Integer(2))); |
515 |
|
assertTrue(q.add(new Integer(3))); |
516 |
< |
assertTrue(q.take() != null); |
516 |
> |
assertNotNull(q.take()); |
517 |
|
} |
518 |
|
|
519 |
|
/** |
594 |
|
} |
595 |
|
|
596 |
|
/** |
597 |
< |
* toArray contains all elements |
597 |
> |
* toArray contains all elements in FIFO order |
598 |
|
*/ |
599 |
< |
public void testToArray() throws InterruptedException { |
599 |
> |
public void testToArray() { |
600 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
601 |
|
Object[] o = q.toArray(); |
602 |
|
for (int i = 0; i < o.length; i++) |
603 |
< |
assertEquals(o[i], q.take()); |
603 |
> |
assertSame(o[i], q.poll()); |
604 |
|
} |
605 |
|
|
606 |
|
/** |
607 |
< |
* toArray(a) contains all elements |
607 |
> |
* toArray(a) contains all elements in FIFO order |
608 |
|
*/ |
609 |
|
public void testToArray2() throws InterruptedException { |
610 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
610 |
> |
LinkedBlockingQueue<Integer> q = populatedQueue(SIZE); |
611 |
|
Integer[] ints = new Integer[SIZE]; |
612 |
< |
ints = (Integer[])q.toArray(ints); |
612 |
> |
Integer[] array = q.toArray(ints); |
613 |
> |
assertSame(ints, array); |
614 |
|
for (int i = 0; i < ints.length; i++) |
615 |
< |
assertEquals(ints[i], q.take()); |
653 |
< |
} |
654 |
< |
|
655 |
< |
/** |
656 |
< |
* toArray(null) throws NPE |
657 |
< |
*/ |
658 |
< |
public void testToArray_BadArg() { |
659 |
< |
try { |
660 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
661 |
< |
Object o[] = q.toArray(null); |
662 |
< |
shouldThrow(); |
663 |
< |
} catch (NullPointerException success) {} |
615 |
> |
assertSame(ints[i], q.poll()); |
616 |
|
} |
617 |
|
|
618 |
|
/** |
619 |
< |
* toArray with incompatible array type throws CCE |
619 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
620 |
|
*/ |
621 |
|
public void testToArray1_BadArg() { |
622 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
623 |
|
try { |
624 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
672 |
< |
Object o[] = q.toArray(new String[10] ); |
624 |
> |
q.toArray(new String[10]); |
625 |
|
shouldThrow(); |
626 |
|
} catch (ArrayStoreException success) {} |
627 |
|
} |
628 |
|
|
677 |
– |
|
629 |
|
/** |
630 |
|
* iterator iterates through all elements |
631 |
|
*/ |
640 |
|
/** |
641 |
|
* iterator.remove removes current element |
642 |
|
*/ |
643 |
< |
public void testIteratorRemove () { |
643 |
> |
public void testIteratorRemove() { |
644 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
645 |
|
q.add(two); |
646 |
|
q.add(one); |
651 |
|
it.remove(); |
652 |
|
|
653 |
|
it = q.iterator(); |
654 |
< |
assertEquals(it.next(), one); |
655 |
< |
assertEquals(it.next(), three); |
654 |
> |
assertSame(it.next(), one); |
655 |
> |
assertSame(it.next(), three); |
656 |
|
assertFalse(it.hasNext()); |
657 |
|
} |
658 |
|
|
708 |
– |
|
659 |
|
/** |
660 |
|
* iterator ordering is FIFO |
661 |
|
*/ |
667 |
|
assertEquals(0, q.remainingCapacity()); |
668 |
|
int k = 0; |
669 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
670 |
< |
int i = ((Integer)(it.next())).intValue(); |
721 |
< |
assertEquals(++k, i); |
670 |
> |
assertEquals(++k, it.next()); |
671 |
|
} |
672 |
|
assertEquals(3, k); |
673 |
|
} |
675 |
|
/** |
676 |
|
* Modifications do not cause iterators to fail |
677 |
|
*/ |
678 |
< |
public void testWeaklyConsistentIteration () { |
678 |
> |
public void testWeaklyConsistentIteration() { |
679 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
680 |
|
q.add(one); |
681 |
|
q.add(two); |
687 |
|
assertEquals(0, q.size()); |
688 |
|
} |
689 |
|
|
741 |
– |
|
690 |
|
/** |
691 |
|
* toString contains toStrings of elements |
692 |
|
*/ |
694 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
695 |
|
String s = q.toString(); |
696 |
|
for (int i = 0; i < SIZE; ++i) { |
697 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
697 |
> |
assertTrue(s.contains(String.valueOf(i))); |
698 |
|
} |
699 |
|
} |
700 |
|
|
753 |
– |
|
701 |
|
/** |
702 |
|
* offer transfers elements across Executor tasks |
703 |
|
*/ |
706 |
|
q.add(one); |
707 |
|
q.add(two); |
708 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
709 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
710 |
|
executor.execute(new CheckedRunnable() { |
711 |
|
public void realRun() throws InterruptedException { |
712 |
< |
threadAssertFalse(q.offer(three)); |
713 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
714 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
712 |
> |
assertFalse(q.offer(three)); |
713 |
> |
threadsStarted.await(); |
714 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
715 |
> |
assertEquals(0, q.remainingCapacity()); |
716 |
|
}}); |
717 |
|
|
718 |
|
executor.execute(new CheckedRunnable() { |
719 |
|
public void realRun() throws InterruptedException { |
720 |
< |
Thread.sleep(SMALL_DELAY_MS); |
721 |
< |
threadAssertEquals(one, q.take()); |
720 |
> |
threadsStarted.await(); |
721 |
> |
assertSame(one, q.take()); |
722 |
|
}}); |
723 |
|
|
724 |
|
joinPool(executor); |
725 |
|
} |
726 |
|
|
727 |
|
/** |
728 |
< |
* poll retrieves elements across Executor threads |
728 |
> |
* timed poll retrieves elements across Executor threads |
729 |
|
*/ |
730 |
|
public void testPollInExecutor() { |
731 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
732 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
733 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
734 |
|
executor.execute(new CheckedRunnable() { |
735 |
|
public void realRun() throws InterruptedException { |
736 |
< |
threadAssertNull(q.poll()); |
737 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
738 |
< |
threadAssertTrue(q.isEmpty()); |
736 |
> |
assertNull(q.poll()); |
737 |
> |
threadsStarted.await(); |
738 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
739 |
> |
checkEmpty(q); |
740 |
|
}}); |
741 |
|
|
742 |
|
executor.execute(new CheckedRunnable() { |
743 |
|
public void realRun() throws InterruptedException { |
744 |
< |
Thread.sleep(SMALL_DELAY_MS); |
744 |
> |
threadsStarted.await(); |
745 |
|
q.put(one); |
746 |
|
}}); |
747 |
|
|
752 |
|
* A deserialized serialized queue has same elements in same order |
753 |
|
*/ |
754 |
|
public void testSerialization() throws Exception { |
755 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
756 |
< |
|
806 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
807 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
808 |
< |
out.writeObject(q); |
809 |
< |
out.close(); |
810 |
< |
|
811 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
812 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
813 |
< |
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
814 |
< |
assertEquals(q.size(), r.size()); |
815 |
< |
while (!q.isEmpty()) |
816 |
< |
assertEquals(q.remove(), r.remove()); |
817 |
< |
} |
755 |
> |
Queue x = populatedQueue(SIZE); |
756 |
> |
Queue y = serialClone(x); |
757 |
|
|
758 |
< |
/** |
759 |
< |
* drainTo(null) throws NPE |
760 |
< |
*/ |
761 |
< |
public void testDrainToNull() { |
762 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
763 |
< |
try { |
764 |
< |
q.drainTo(null); |
765 |
< |
shouldThrow(); |
766 |
< |
} catch (NullPointerException success) {} |
828 |
< |
} |
829 |
< |
|
830 |
< |
/** |
831 |
< |
* drainTo(this) throws IAE |
832 |
< |
*/ |
833 |
< |
public void testDrainToSelf() { |
834 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
835 |
< |
try { |
836 |
< |
q.drainTo(q); |
837 |
< |
shouldThrow(); |
838 |
< |
} catch (IllegalArgumentException success) {} |
758 |
> |
assertNotSame(x, y); |
759 |
> |
assertEquals(x.size(), y.size()); |
760 |
> |
assertEquals(x.toString(), y.toString()); |
761 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
762 |
> |
while (!x.isEmpty()) { |
763 |
> |
assertFalse(y.isEmpty()); |
764 |
> |
assertEquals(x.remove(), y.remove()); |
765 |
> |
} |
766 |
> |
assertTrue(y.isEmpty()); |
767 |
|
} |
768 |
|
|
769 |
|
/** |
773 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
774 |
|
ArrayList l = new ArrayList(); |
775 |
|
q.drainTo(l); |
776 |
< |
assertEquals(q.size(), 0); |
777 |
< |
assertEquals(l.size(), SIZE); |
776 |
> |
assertEquals(0, q.size()); |
777 |
> |
assertEquals(SIZE, l.size()); |
778 |
|
for (int i = 0; i < SIZE; ++i) |
779 |
|
assertEquals(l.get(i), new Integer(i)); |
780 |
|
q.add(zero); |
784 |
|
assertTrue(q.contains(one)); |
785 |
|
l.clear(); |
786 |
|
q.drainTo(l); |
787 |
< |
assertEquals(q.size(), 0); |
788 |
< |
assertEquals(l.size(), 2); |
787 |
> |
assertEquals(0, q.size()); |
788 |
> |
assertEquals(2, l.size()); |
789 |
|
for (int i = 0; i < 2; ++i) |
790 |
|
assertEquals(l.get(i), new Integer(i)); |
791 |
|
} |
811 |
|
} |
812 |
|
|
813 |
|
/** |
814 |
< |
* drainTo(null, n) throws NPE |
887 |
< |
*/ |
888 |
< |
public void testDrainToNullN() { |
889 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
890 |
< |
try { |
891 |
< |
q.drainTo(null, 0); |
892 |
< |
shouldThrow(); |
893 |
< |
} catch (NullPointerException success) {} |
894 |
< |
} |
895 |
< |
|
896 |
< |
/** |
897 |
< |
* drainTo(this, n) throws IAE |
898 |
< |
*/ |
899 |
< |
public void testDrainToSelfN() { |
900 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
901 |
< |
try { |
902 |
< |
q.drainTo(q, 0); |
903 |
< |
shouldThrow(); |
904 |
< |
} catch (IllegalArgumentException success) {} |
905 |
< |
} |
906 |
< |
|
907 |
< |
/** |
908 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
814 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
815 |
|
*/ |
816 |
|
public void testDrainToN() { |
817 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
820 |
|
assertTrue(q.offer(new Integer(j))); |
821 |
|
ArrayList l = new ArrayList(); |
822 |
|
q.drainTo(l, i); |
823 |
< |
int k = (i < SIZE)? i : SIZE; |
824 |
< |
assertEquals(l.size(), k); |
825 |
< |
assertEquals(q.size(), SIZE-k); |
823 |
> |
int k = (i < SIZE) ? i : SIZE; |
824 |
> |
assertEquals(k, l.size()); |
825 |
> |
assertEquals(SIZE-k, q.size()); |
826 |
|
for (int j = 0; j < k; ++j) |
827 |
|
assertEquals(l.get(j), new Integer(j)); |
828 |
|
while (q.poll() != null) ; |
829 |
|
} |
830 |
|
} |
831 |
|
|
832 |
+ |
/** |
833 |
+ |
* remove(null), contains(null) always return false |
834 |
+ |
*/ |
835 |
+ |
public void testNeverContainsNull() { |
836 |
+ |
Collection<?>[] qs = { |
837 |
+ |
new LinkedBlockingQueue<Object>(), |
838 |
+ |
populatedQueue(2), |
839 |
+ |
}; |
840 |
+ |
|
841 |
+ |
for (Collection<?> q : qs) { |
842 |
+ |
assertFalse(q.contains(null)); |
843 |
+ |
assertFalse(q.remove(null)); |
844 |
+ |
} |
845 |
+ |
} |
846 |
+ |
|
847 |
|
} |