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.Iterator; |
15 |
> |
import java.util.NoSuchElementException; |
16 |
> |
import java.util.Queue; |
17 |
> |
import java.util.concurrent.BlockingQueue; |
18 |
> |
import java.util.concurrent.CountDownLatch; |
19 |
> |
import java.util.concurrent.Executors; |
20 |
> |
import java.util.concurrent.ExecutorService; |
21 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
22 |
> |
|
23 |
> |
import junit.framework.Test; |
24 |
|
|
25 |
|
public class LinkedBlockingQueueTest extends JSR166TestCase { |
26 |
|
|
27 |
+ |
public static class Unbounded extends BlockingQueueTest { |
28 |
+ |
protected BlockingQueue emptyCollection() { |
29 |
+ |
return new LinkedBlockingQueue(); |
30 |
+ |
} |
31 |
+ |
} |
32 |
+ |
|
33 |
+ |
public static class Bounded extends BlockingQueueTest { |
34 |
+ |
protected BlockingQueue emptyCollection() { |
35 |
+ |
return new LinkedBlockingQueue(SIZE); |
36 |
+ |
} |
37 |
+ |
} |
38 |
+ |
|
39 |
|
public static void main(String[] args) { |
40 |
< |
junit.textui.TestRunner.run (suite()); |
40 |
> |
main(suite(), args); |
41 |
|
} |
42 |
|
|
43 |
|
public static Test suite() { |
44 |
< |
return new TestSuite(LinkedBlockingQueueTest.class); |
44 |
> |
return newTestSuite(LinkedBlockingQueueTest.class, |
45 |
> |
new Unbounded().testSuite(), |
46 |
> |
new Bounded().testSuite()); |
47 |
|
} |
48 |
|
|
25 |
– |
|
49 |
|
/** |
50 |
< |
* Create a queue of given size containing consecutive |
50 |
> |
* Returns a new queue of given size containing consecutive |
51 |
|
* Integers 0 ... n. |
52 |
|
*/ |
53 |
< |
private LinkedBlockingQueue populatedQueue(int n) { |
54 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(n); |
53 |
> |
private LinkedBlockingQueue<Integer> populatedQueue(int n) { |
54 |
> |
LinkedBlockingQueue<Integer> q = |
55 |
> |
new LinkedBlockingQueue<Integer>(n); |
56 |
|
assertTrue(q.isEmpty()); |
57 |
|
for (int i = 0; i < n; i++) |
58 |
|
assertTrue(q.offer(new Integer(i))); |
72 |
|
} |
73 |
|
|
74 |
|
/** |
75 |
< |
* Constructor throws IAE if capacity argument nonpositive |
75 |
> |
* Constructor throws IllegalArgumentException if capacity argument nonpositive |
76 |
|
*/ |
77 |
|
public void testConstructor2() { |
78 |
|
try { |
79 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(0); |
79 |
> |
new LinkedBlockingQueue(0); |
80 |
|
shouldThrow(); |
81 |
|
} catch (IllegalArgumentException success) {} |
82 |
|
} |
83 |
|
|
84 |
|
/** |
85 |
< |
* Initializing from null Collection throws NPE |
85 |
> |
* Initializing from null Collection throws NullPointerException |
86 |
|
*/ |
87 |
|
public void testConstructor3() { |
88 |
|
try { |
89 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(null); |
89 |
> |
new LinkedBlockingQueue(null); |
90 |
|
shouldThrow(); |
91 |
|
} catch (NullPointerException success) {} |
92 |
|
} |
93 |
|
|
94 |
|
/** |
95 |
< |
* Initializing from Collection of null elements throws NPE |
95 |
> |
* Initializing from Collection of null elements throws NullPointerException |
96 |
|
*/ |
97 |
|
public void testConstructor4() { |
98 |
+ |
Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
99 |
|
try { |
100 |
< |
Integer[] ints = new Integer[SIZE]; |
76 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
100 |
> |
new LinkedBlockingQueue(elements); |
101 |
|
shouldThrow(); |
102 |
|
} catch (NullPointerException success) {} |
103 |
|
} |
104 |
|
|
105 |
|
/** |
106 |
< |
* Initializing from Collection with some null elements throws NPE |
106 |
> |
* Initializing from Collection with some null elements throws |
107 |
> |
* NullPointerException |
108 |
|
*/ |
109 |
|
public void testConstructor5() { |
110 |
+ |
Integer[] ints = new Integer[SIZE]; |
111 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
112 |
+ |
ints[i] = new Integer(i); |
113 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
114 |
|
try { |
115 |
< |
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)); |
115 |
> |
new LinkedBlockingQueue(elements); |
116 |
|
shouldThrow(); |
117 |
|
} catch (NullPointerException success) {} |
118 |
|
} |
148 |
|
* remainingCapacity decreases on add, increases on remove |
149 |
|
*/ |
150 |
|
public void testRemainingCapacity() { |
151 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
151 |
> |
BlockingQueue q = populatedQueue(SIZE); |
152 |
|
for (int i = 0; i < SIZE; ++i) { |
153 |
|
assertEquals(i, q.remainingCapacity()); |
154 |
< |
assertEquals(SIZE-i, q.size()); |
155 |
< |
q.remove(); |
154 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
155 |
> |
assertEquals(i, q.remove()); |
156 |
|
} |
157 |
|
for (int i = 0; i < SIZE; ++i) { |
158 |
< |
assertEquals(SIZE-i, q.remainingCapacity()); |
159 |
< |
assertEquals(i, q.size()); |
160 |
< |
q.add(new Integer(i)); |
158 |
> |
assertEquals(SIZE - i, q.remainingCapacity()); |
159 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
160 |
> |
assertTrue(q.add(i)); |
161 |
|
} |
162 |
|
} |
163 |
|
|
164 |
|
/** |
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 |
– |
/** |
165 |
|
* Offer succeeds if not full; fails if full |
166 |
|
*/ |
167 |
|
public void testOffer() { |
171 |
|
} |
172 |
|
|
173 |
|
/** |
174 |
< |
* add succeeds if not full; throws ISE if full |
174 |
> |
* add succeeds if not full; throws IllegalStateException if full |
175 |
|
*/ |
176 |
|
public void testAdd() { |
177 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
178 |
+ |
for (int i = 0; i < SIZE; ++i) |
179 |
+ |
assertTrue(q.add(new Integer(i))); |
180 |
+ |
assertEquals(0, q.remainingCapacity()); |
181 |
|
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()); |
182 |
|
q.add(new Integer(SIZE)); |
183 |
|
shouldThrow(); |
184 |
|
} catch (IllegalStateException success) {} |
185 |
|
} |
186 |
|
|
187 |
|
/** |
188 |
< |
* 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 |
188 |
> |
* addAll(this) throws IllegalArgumentException |
189 |
|
*/ |
190 |
|
public void testAddAllSelf() { |
191 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
192 |
|
try { |
200 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
193 |
|
q.addAll(q); |
194 |
|
shouldThrow(); |
195 |
|
} catch (IllegalArgumentException success) {} |
196 |
|
} |
197 |
|
|
198 |
|
/** |
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 |
– |
/** |
199 |
|
* addAll of a collection with any null elements throws NPE after |
200 |
|
* possibly adding some elements |
201 |
|
*/ |
202 |
|
public void testAddAll3() { |
203 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
204 |
+ |
Integer[] ints = new Integer[SIZE]; |
205 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
206 |
+ |
ints[i] = new Integer(i); |
207 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
208 |
|
try { |
209 |
< |
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)); |
209 |
> |
q.addAll(elements); |
210 |
|
shouldThrow(); |
211 |
|
} catch (NullPointerException success) {} |
212 |
|
} |
213 |
+ |
|
214 |
|
/** |
215 |
< |
* addAll throws ISE if not enough room |
215 |
> |
* addAll throws IllegalStateException if not enough room |
216 |
|
*/ |
217 |
|
public void testAddAll4() { |
218 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE - 1); |
219 |
+ |
Integer[] ints = new Integer[SIZE]; |
220 |
+ |
for (int i = 0; i < SIZE; ++i) |
221 |
+ |
ints[i] = new Integer(i); |
222 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
223 |
|
try { |
224 |
< |
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)); |
224 |
> |
q.addAll(elements); |
225 |
|
shouldThrow(); |
226 |
|
} catch (IllegalStateException success) {} |
227 |
|
} |
228 |
+ |
|
229 |
|
/** |
230 |
|
* Queue contains all elements, in traversal order, of successful addAll |
231 |
|
*/ |
242 |
|
} |
243 |
|
|
244 |
|
/** |
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 |
– |
/** |
245 |
|
* all elements successfully put are contained |
246 |
|
*/ |
247 |
|
public void testPut() throws InterruptedException { |
248 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
249 |
|
for (int i = 0; i < SIZE; ++i) { |
250 |
< |
Integer I = new Integer(i); |
251 |
< |
q.put(I); |
252 |
< |
assertTrue(q.contains(I)); |
250 |
> |
Integer x = new Integer(i); |
251 |
> |
q.put(x); |
252 |
> |
assertTrue(q.contains(x)); |
253 |
|
} |
254 |
|
assertEquals(0, q.remainingCapacity()); |
255 |
|
} |
259 |
|
*/ |
260 |
|
public void testBlockingPut() throws InterruptedException { |
261 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
262 |
< |
Thread t = new Thread(new CheckedRunnable() { |
262 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
263 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
264 |
|
public void realRun() throws InterruptedException { |
265 |
|
for (int i = 0; i < SIZE; ++i) |
266 |
|
q.put(i); |
267 |
|
assertEquals(SIZE, q.size()); |
268 |
|
assertEquals(0, q.remainingCapacity()); |
269 |
+ |
|
270 |
+ |
Thread.currentThread().interrupt(); |
271 |
+ |
try { |
272 |
+ |
q.put(99); |
273 |
+ |
shouldThrow(); |
274 |
+ |
} catch (InterruptedException success) {} |
275 |
+ |
assertFalse(Thread.interrupted()); |
276 |
+ |
|
277 |
+ |
pleaseInterrupt.countDown(); |
278 |
|
try { |
279 |
|
q.put(99); |
280 |
|
shouldThrow(); |
281 |
|
} catch (InterruptedException success) {} |
282 |
+ |
assertFalse(Thread.interrupted()); |
283 |
|
}}); |
284 |
|
|
285 |
< |
t.start(); |
286 |
< |
Thread.sleep(SHORT_DELAY_MS); |
285 |
> |
await(pleaseInterrupt); |
286 |
> |
assertThreadStaysAlive(t); |
287 |
|
t.interrupt(); |
288 |
< |
t.join(); |
288 |
> |
awaitTermination(t); |
289 |
|
assertEquals(SIZE, q.size()); |
290 |
|
assertEquals(0, q.remainingCapacity()); |
291 |
|
} |
292 |
|
|
293 |
|
/** |
294 |
< |
* put blocks waiting for take when full |
294 |
> |
* put blocks interruptibly waiting for take when full |
295 |
|
*/ |
296 |
|
public void testPutWithTake() throws InterruptedException { |
297 |
|
final int capacity = 2; |
298 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
299 |
< |
Thread t = new Thread(new CheckedRunnable() { |
299 |
> |
final CountDownLatch pleaseTake = new CountDownLatch(1); |
300 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
301 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
302 |
|
public void realRun() throws InterruptedException { |
303 |
< |
for (int i = 0; i < capacity + 1; i++) |
303 |
> |
for (int i = 0; i < capacity; i++) |
304 |
|
q.put(i); |
305 |
+ |
pleaseTake.countDown(); |
306 |
+ |
q.put(86); |
307 |
+ |
|
308 |
+ |
pleaseInterrupt.countDown(); |
309 |
|
try { |
310 |
|
q.put(99); |
311 |
|
shouldThrow(); |
312 |
|
} catch (InterruptedException success) {} |
313 |
+ |
assertFalse(Thread.interrupted()); |
314 |
|
}}); |
315 |
|
|
316 |
< |
t.start(); |
317 |
< |
Thread.sleep(SHORT_DELAY_MS); |
326 |
< |
assertEquals(q.remainingCapacity(), 0); |
316 |
> |
await(pleaseTake); |
317 |
> |
assertEquals(0, q.remainingCapacity()); |
318 |
|
assertEquals(0, q.take()); |
319 |
< |
Thread.sleep(SHORT_DELAY_MS); |
319 |
> |
|
320 |
> |
await(pleaseInterrupt); |
321 |
> |
assertThreadStaysAlive(t); |
322 |
|
t.interrupt(); |
323 |
< |
t.join(); |
324 |
< |
assertEquals(q.remainingCapacity(), 0); |
323 |
> |
awaitTermination(t); |
324 |
> |
assertEquals(0, q.remainingCapacity()); |
325 |
|
} |
326 |
|
|
327 |
|
/** |
328 |
|
* timed offer times out if full and elements not taken |
329 |
|
*/ |
330 |
< |
public void testTimedOffer() throws InterruptedException { |
330 |
> |
public void testTimedOffer() { |
331 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
332 |
< |
Thread t = new Thread(new CheckedRunnable() { |
332 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
333 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
334 |
|
public void realRun() throws InterruptedException { |
335 |
|
q.put(new Object()); |
336 |
|
q.put(new Object()); |
337 |
< |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
337 |
> |
long startTime = System.nanoTime(); |
338 |
> |
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
339 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
340 |
> |
pleaseInterrupt.countDown(); |
341 |
|
try { |
342 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
342 |
> |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
343 |
|
shouldThrow(); |
344 |
|
} catch (InterruptedException success) {} |
345 |
|
}}); |
346 |
|
|
347 |
< |
t.start(); |
348 |
< |
Thread.sleep(SMALL_DELAY_MS); |
347 |
> |
await(pleaseInterrupt); |
348 |
> |
assertThreadStaysAlive(t); |
349 |
|
t.interrupt(); |
350 |
< |
t.join(); |
350 |
> |
awaitTermination(t); |
351 |
|
} |
352 |
|
|
353 |
|
/** |
361 |
|
} |
362 |
|
|
363 |
|
/** |
367 |
– |
* take blocks interruptibly when empty |
368 |
– |
*/ |
369 |
– |
public void testTakeFromEmpty() throws InterruptedException { |
370 |
– |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
371 |
– |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
372 |
– |
public void realRun() throws InterruptedException { |
373 |
– |
q.take(); |
374 |
– |
}}; |
375 |
– |
|
376 |
– |
t.start(); |
377 |
– |
Thread.sleep(SHORT_DELAY_MS); |
378 |
– |
t.interrupt(); |
379 |
– |
t.join(); |
380 |
– |
} |
381 |
– |
|
382 |
– |
/** |
364 |
|
* Take removes existing elements until empty, then blocks interruptibly |
365 |
|
*/ |
366 |
|
public void testBlockingTake() throws InterruptedException { |
367 |
< |
final LinkedBlockingQueue q = populatedQueue(SIZE); |
368 |
< |
Thread t = new Thread(new CheckedRunnable() { |
367 |
> |
final BlockingQueue q = populatedQueue(SIZE); |
368 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
369 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
370 |
|
public void realRun() throws InterruptedException { |
371 |
|
for (int i = 0; i < SIZE; ++i) { |
372 |
|
assertEquals(i, q.take()); |
373 |
|
} |
374 |
+ |
|
375 |
+ |
Thread.currentThread().interrupt(); |
376 |
+ |
try { |
377 |
+ |
q.take(); |
378 |
+ |
shouldThrow(); |
379 |
+ |
} catch (InterruptedException success) {} |
380 |
+ |
assertFalse(Thread.interrupted()); |
381 |
+ |
|
382 |
+ |
pleaseInterrupt.countDown(); |
383 |
|
try { |
384 |
|
q.take(); |
385 |
|
shouldThrow(); |
386 |
|
} catch (InterruptedException success) {} |
387 |
+ |
assertFalse(Thread.interrupted()); |
388 |
|
}}); |
389 |
|
|
390 |
< |
t.start(); |
391 |
< |
Thread.sleep(SHORT_DELAY_MS); |
390 |
> |
await(pleaseInterrupt); |
391 |
> |
assertThreadStaysAlive(t); |
392 |
|
t.interrupt(); |
393 |
< |
t.join(); |
393 |
> |
awaitTermination(t); |
394 |
|
} |
395 |
|
|
396 |
|
/** |
405 |
|
} |
406 |
|
|
407 |
|
/** |
408 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
408 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
409 |
|
*/ |
410 |
|
public void testTimedPoll0() throws InterruptedException { |
411 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
416 |
|
} |
417 |
|
|
418 |
|
/** |
419 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
419 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
420 |
|
*/ |
421 |
|
public void testTimedPoll() throws InterruptedException { |
422 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
422 |
> |
LinkedBlockingQueue<Integer> q = populatedQueue(SIZE); |
423 |
|
for (int i = 0; i < SIZE; ++i) { |
424 |
< |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
425 |
< |
} |
426 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
424 |
> |
long startTime = System.nanoTime(); |
425 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
426 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
427 |
> |
} |
428 |
> |
long startTime = System.nanoTime(); |
429 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
430 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
431 |
> |
checkEmpty(q); |
432 |
|
} |
433 |
|
|
434 |
|
/** |
436 |
|
* returning timeout status |
437 |
|
*/ |
438 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
439 |
< |
Thread t = new Thread(new CheckedRunnable() { |
439 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
440 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
441 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
442 |
|
public void realRun() throws InterruptedException { |
443 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
443 |
> |
long startTime = System.nanoTime(); |
444 |
|
for (int i = 0; i < SIZE; ++i) { |
445 |
< |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
445 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
446 |
|
} |
447 |
< |
try { |
449 |
< |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
450 |
< |
shouldThrow(); |
451 |
< |
} catch (InterruptedException success) {} |
452 |
< |
}}); |
453 |
< |
|
454 |
< |
t.start(); |
455 |
< |
Thread.sleep(SHORT_DELAY_MS); |
456 |
< |
t.interrupt(); |
457 |
< |
t.join(); |
458 |
< |
} |
459 |
< |
|
460 |
< |
/** |
461 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
462 |
< |
* on interruption throws |
463 |
< |
*/ |
464 |
< |
public void testTimedPollWithOffer() throws InterruptedException { |
465 |
< |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
466 |
< |
Thread t = new Thread(new CheckedRunnable() { |
467 |
< |
public void realRun() throws InterruptedException { |
468 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
469 |
< |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
447 |
> |
aboutToWait.countDown(); |
448 |
|
try { |
449 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
450 |
|
shouldThrow(); |
451 |
< |
} catch (InterruptedException success) {} |
451 |
> |
} catch (InterruptedException success) { |
452 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
453 |
> |
} |
454 |
|
}}); |
455 |
|
|
456 |
< |
t.start(); |
457 |
< |
Thread.sleep(SMALL_DELAY_MS); |
478 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
456 |
> |
await(aboutToWait); |
457 |
> |
waitForThreadToEnterWaitState(t, LONG_DELAY_MS); |
458 |
|
t.interrupt(); |
459 |
< |
t.join(); |
459 |
> |
awaitTermination(t); |
460 |
> |
checkEmpty(q); |
461 |
|
} |
462 |
|
|
463 |
|
/** |
504 |
|
} |
505 |
|
|
506 |
|
/** |
527 |
– |
* remove(x) removes x and returns true if present |
528 |
– |
*/ |
529 |
– |
public void testRemoveElement() { |
530 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
531 |
– |
for (int i = 1; i < SIZE; i+=2) { |
532 |
– |
assertTrue(q.remove(new Integer(i))); |
533 |
– |
} |
534 |
– |
for (int i = 0; i < SIZE; i+=2) { |
535 |
– |
assertTrue(q.remove(new Integer(i))); |
536 |
– |
assertFalse(q.remove(new Integer(i+1))); |
537 |
– |
} |
538 |
– |
assertTrue(q.isEmpty()); |
539 |
– |
} |
540 |
– |
|
541 |
– |
/** |
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 |
|
/** |
572 |
|
assertTrue(changed); |
573 |
|
|
574 |
|
assertTrue(q.containsAll(p)); |
575 |
< |
assertEquals(SIZE-i, q.size()); |
575 |
> |
assertEquals(SIZE - i, q.size()); |
576 |
|
p.remove(); |
577 |
|
} |
578 |
|
} |
585 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
586 |
|
LinkedBlockingQueue p = populatedQueue(i); |
587 |
|
assertTrue(q.removeAll(p)); |
588 |
< |
assertEquals(SIZE-i, q.size()); |
588 |
> |
assertEquals(SIZE - i, q.size()); |
589 |
|
for (int j = 0; j < i; ++j) { |
590 |
< |
Integer I = (Integer)(p.remove()); |
591 |
< |
assertFalse(q.contains(I)); |
590 |
> |
Integer x = (Integer)(p.remove()); |
591 |
> |
assertFalse(q.contains(x)); |
592 |
|
} |
593 |
|
} |
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()); |
615 |
> |
assertSame(ints[i], q.poll()); |
616 |
|
} |
617 |
|
|
618 |
|
/** |
619 |
< |
* toArray(null) throws NPE |
654 |
< |
*/ |
655 |
< |
public void testToArray_BadArg() { |
656 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
657 |
< |
try { |
658 |
< |
Object o[] = q.toArray(null); |
659 |
< |
shouldThrow(); |
660 |
< |
} catch (NullPointerException success) {} |
661 |
< |
} |
662 |
< |
|
663 |
< |
/** |
664 |
< |
* 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 |
< |
Object o[] = q.toArray(new String[10]); |
624 |
> |
q.toArray(new String[10]); |
625 |
|
shouldThrow(); |
626 |
|
} catch (ArrayStoreException success) {} |
627 |
|
} |
628 |
|
|
674 |
– |
|
629 |
|
/** |
630 |
|
* iterator iterates through all elements |
631 |
|
*/ |
632 |
|
public void testIterator() throws InterruptedException { |
633 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
634 |
|
Iterator it = q.iterator(); |
635 |
< |
while (it.hasNext()) { |
635 |
> |
int i; |
636 |
> |
for (i = 0; it.hasNext(); i++) |
637 |
> |
assertTrue(q.contains(it.next())); |
638 |
> |
assertEquals(i, SIZE); |
639 |
> |
assertIteratorExhausted(it); |
640 |
> |
|
641 |
> |
it = q.iterator(); |
642 |
> |
for (i = 0; it.hasNext(); i++) |
643 |
|
assertEquals(it.next(), q.take()); |
644 |
< |
} |
644 |
> |
assertEquals(i, SIZE); |
645 |
> |
assertIteratorExhausted(it); |
646 |
> |
} |
647 |
> |
|
648 |
> |
/** |
649 |
> |
* iterator of empty collection has no elements |
650 |
> |
*/ |
651 |
> |
public void testEmptyIterator() { |
652 |
> |
assertIteratorExhausted(new LinkedBlockingQueue().iterator()); |
653 |
|
} |
654 |
|
|
655 |
|
/** |
656 |
|
* iterator.remove removes current element |
657 |
|
*/ |
658 |
< |
public void testIteratorRemove () { |
658 |
> |
public void testIteratorRemove() { |
659 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
660 |
|
q.add(two); |
661 |
|
q.add(one); |
671 |
|
assertFalse(it.hasNext()); |
672 |
|
} |
673 |
|
|
705 |
– |
|
674 |
|
/** |
675 |
|
* iterator ordering is FIFO |
676 |
|
*/ |
690 |
|
/** |
691 |
|
* Modifications do not cause iterators to fail |
692 |
|
*/ |
693 |
< |
public void testWeaklyConsistentIteration () { |
693 |
> |
public void testWeaklyConsistentIteration() { |
694 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
695 |
|
q.add(one); |
696 |
|
q.add(two); |
702 |
|
assertEquals(0, q.size()); |
703 |
|
} |
704 |
|
|
737 |
– |
|
705 |
|
/** |
706 |
|
* toString contains toStrings of elements |
707 |
|
*/ |
709 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
710 |
|
String s = q.toString(); |
711 |
|
for (int i = 0; i < SIZE; ++i) { |
712 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
712 |
> |
assertTrue(s.contains(String.valueOf(i))); |
713 |
|
} |
714 |
|
} |
715 |
|
|
749 |
– |
|
716 |
|
/** |
717 |
|
* offer transfers elements across Executor tasks |
718 |
|
*/ |
720 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
721 |
|
q.add(one); |
722 |
|
q.add(two); |
723 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
724 |
< |
executor.execute(new CheckedRunnable() { |
725 |
< |
public void realRun() throws InterruptedException { |
726 |
< |
assertFalse(q.offer(three)); |
727 |
< |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
728 |
< |
assertEquals(0, q.remainingCapacity()); |
729 |
< |
}}); |
730 |
< |
|
731 |
< |
executor.execute(new CheckedRunnable() { |
732 |
< |
public void realRun() throws InterruptedException { |
733 |
< |
Thread.sleep(SMALL_DELAY_MS); |
734 |
< |
assertSame(one, q.take()); |
735 |
< |
}}); |
736 |
< |
|
737 |
< |
joinPool(executor); |
723 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
724 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
725 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
726 |
> |
executor.execute(new CheckedRunnable() { |
727 |
> |
public void realRun() throws InterruptedException { |
728 |
> |
assertFalse(q.offer(three)); |
729 |
> |
threadsStarted.await(); |
730 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
731 |
> |
assertEquals(0, q.remainingCapacity()); |
732 |
> |
}}); |
733 |
> |
|
734 |
> |
executor.execute(new CheckedRunnable() { |
735 |
> |
public void realRun() throws InterruptedException { |
736 |
> |
threadsStarted.await(); |
737 |
> |
assertSame(one, q.take()); |
738 |
> |
}}); |
739 |
> |
} |
740 |
|
} |
741 |
|
|
742 |
|
/** |
743 |
< |
* poll retrieves elements across Executor threads |
743 |
> |
* timed poll retrieves elements across Executor threads |
744 |
|
*/ |
745 |
|
public void testPollInExecutor() { |
746 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
747 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
748 |
< |
executor.execute(new CheckedRunnable() { |
749 |
< |
public void realRun() throws InterruptedException { |
750 |
< |
assertNull(q.poll()); |
751 |
< |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
752 |
< |
assertTrue(q.isEmpty()); |
753 |
< |
}}); |
754 |
< |
|
755 |
< |
executor.execute(new CheckedRunnable() { |
756 |
< |
public void realRun() throws InterruptedException { |
757 |
< |
Thread.sleep(SMALL_DELAY_MS); |
758 |
< |
q.put(one); |
759 |
< |
}}); |
760 |
< |
|
761 |
< |
joinPool(executor); |
747 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
748 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
749 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
750 |
> |
executor.execute(new CheckedRunnable() { |
751 |
> |
public void realRun() throws InterruptedException { |
752 |
> |
assertNull(q.poll()); |
753 |
> |
threadsStarted.await(); |
754 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
755 |
> |
checkEmpty(q); |
756 |
> |
}}); |
757 |
> |
|
758 |
> |
executor.execute(new CheckedRunnable() { |
759 |
> |
public void realRun() throws InterruptedException { |
760 |
> |
threadsStarted.await(); |
761 |
> |
q.put(one); |
762 |
> |
}}); |
763 |
> |
} |
764 |
|
} |
765 |
|
|
766 |
|
/** |
767 |
|
* A deserialized serialized queue has same elements in same order |
768 |
|
*/ |
769 |
|
public void testSerialization() throws Exception { |
770 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
771 |
< |
|
802 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
803 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
804 |
< |
out.writeObject(q); |
805 |
< |
out.close(); |
806 |
< |
|
807 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
808 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
809 |
< |
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
810 |
< |
assertEquals(q.size(), r.size()); |
811 |
< |
while (!q.isEmpty()) |
812 |
< |
assertEquals(q.remove(), r.remove()); |
813 |
< |
} |
770 |
> |
Queue x = populatedQueue(SIZE); |
771 |
> |
Queue y = serialClone(x); |
772 |
|
|
773 |
< |
/** |
774 |
< |
* drainTo(null) throws NPE |
775 |
< |
*/ |
776 |
< |
public void testDrainToNull() { |
777 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
778 |
< |
try { |
779 |
< |
q.drainTo(null); |
780 |
< |
shouldThrow(); |
781 |
< |
} catch (NullPointerException success) {} |
824 |
< |
} |
825 |
< |
|
826 |
< |
/** |
827 |
< |
* drainTo(this) throws IAE |
828 |
< |
*/ |
829 |
< |
public void testDrainToSelf() { |
830 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
831 |
< |
try { |
832 |
< |
q.drainTo(q); |
833 |
< |
shouldThrow(); |
834 |
< |
} catch (IllegalArgumentException success) {} |
773 |
> |
assertNotSame(x, y); |
774 |
> |
assertEquals(x.size(), y.size()); |
775 |
> |
assertEquals(x.toString(), y.toString()); |
776 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
777 |
> |
while (!x.isEmpty()) { |
778 |
> |
assertFalse(y.isEmpty()); |
779 |
> |
assertEquals(x.remove(), y.remove()); |
780 |
> |
} |
781 |
> |
assertTrue(y.isEmpty()); |
782 |
|
} |
783 |
|
|
784 |
|
/** |
788 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
789 |
|
ArrayList l = new ArrayList(); |
790 |
|
q.drainTo(l); |
791 |
< |
assertEquals(q.size(), 0); |
792 |
< |
assertEquals(l.size(), SIZE); |
791 |
> |
assertEquals(0, q.size()); |
792 |
> |
assertEquals(SIZE, l.size()); |
793 |
|
for (int i = 0; i < SIZE; ++i) |
794 |
|
assertEquals(l.get(i), new Integer(i)); |
795 |
|
q.add(zero); |
799 |
|
assertTrue(q.contains(one)); |
800 |
|
l.clear(); |
801 |
|
q.drainTo(l); |
802 |
< |
assertEquals(q.size(), 0); |
803 |
< |
assertEquals(l.size(), 2); |
802 |
> |
assertEquals(0, q.size()); |
803 |
> |
assertEquals(2, l.size()); |
804 |
|
for (int i = 0; i < 2; ++i) |
805 |
|
assertEquals(l.get(i), new Integer(i)); |
806 |
|
} |
812 |
|
final LinkedBlockingQueue q = populatedQueue(SIZE); |
813 |
|
Thread t = new Thread(new CheckedRunnable() { |
814 |
|
public void realRun() throws InterruptedException { |
815 |
< |
q.put(new Integer(SIZE+1)); |
815 |
> |
q.put(new Integer(SIZE + 1)); |
816 |
|
}}); |
817 |
|
|
818 |
|
t.start(); |
826 |
|
} |
827 |
|
|
828 |
|
/** |
829 |
< |
* drainTo(null, n) throws NPE |
883 |
< |
*/ |
884 |
< |
public void testDrainToNullN() { |
885 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
886 |
< |
try { |
887 |
< |
q.drainTo(null, 0); |
888 |
< |
shouldThrow(); |
889 |
< |
} catch (NullPointerException success) {} |
890 |
< |
} |
891 |
< |
|
892 |
< |
/** |
893 |
< |
* drainTo(this, n) throws IAE |
894 |
< |
*/ |
895 |
< |
public void testDrainToSelfN() { |
896 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
897 |
< |
try { |
898 |
< |
q.drainTo(q, 0); |
899 |
< |
shouldThrow(); |
900 |
< |
} catch (IllegalArgumentException success) {} |
901 |
< |
} |
902 |
< |
|
903 |
< |
/** |
904 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
829 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
830 |
|
*/ |
831 |
|
public void testDrainToN() { |
832 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
835 |
|
assertTrue(q.offer(new Integer(j))); |
836 |
|
ArrayList l = new ArrayList(); |
837 |
|
q.drainTo(l, i); |
838 |
< |
int k = (i < SIZE)? i : SIZE; |
839 |
< |
assertEquals(l.size(), k); |
840 |
< |
assertEquals(q.size(), SIZE-k); |
838 |
> |
int k = (i < SIZE) ? i : SIZE; |
839 |
> |
assertEquals(k, l.size()); |
840 |
> |
assertEquals(SIZE - k, q.size()); |
841 |
|
for (int j = 0; j < k; ++j) |
842 |
|
assertEquals(l.get(j), new Integer(j)); |
843 |
< |
while (q.poll() != null) ; |
843 |
> |
do {} while (q.poll() != null); |
844 |
> |
} |
845 |
> |
} |
846 |
> |
|
847 |
> |
/** |
848 |
> |
* remove(null), contains(null) always return false |
849 |
> |
*/ |
850 |
> |
public void testNeverContainsNull() { |
851 |
> |
Collection<?>[] qs = { |
852 |
> |
new LinkedBlockingQueue<Object>(), |
853 |
> |
populatedQueue(2), |
854 |
> |
}; |
855 |
> |
|
856 |
> |
for (Collection<?> q : qs) { |
857 |
> |
assertFalse(q.contains(null)); |
858 |
> |
assertFalse(q.remove(null)); |
859 |
|
} |
860 |
|
} |
861 |
|
|