1 |
dl |
1.1 |
/* |
2 |
dl |
1.7 |
* 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 |
jsr166 |
1.40 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.12 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
jsr166 |
1.54 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
|
|
11 |
|
|
import java.util.ArrayList; |
12 |
jsr166 |
1.45 |
import java.util.Arrays; |
13 |
|
|
import java.util.Collection; |
14 |
|
|
import java.util.Iterator; |
15 |
|
|
import java.util.NoSuchElementException; |
16 |
jsr166 |
1.46 |
import java.util.Queue; |
17 |
jsr166 |
1.45 |
import java.util.concurrent.BlockingQueue; |
18 |
|
|
import java.util.concurrent.CountDownLatch; |
19 |
|
|
import java.util.concurrent.Executors; |
20 |
|
|
import java.util.concurrent.ExecutorService; |
21 |
jsr166 |
1.54 |
import java.util.concurrent.LinkedBlockingQueue; |
22 |
|
|
|
23 |
|
|
import junit.framework.Test; |
24 |
dl |
1.1 |
|
25 |
dl |
1.3 |
public class LinkedBlockingQueueTest extends JSR166TestCase { |
26 |
dl |
1.1 |
|
27 |
jsr166 |
1.29 |
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 |
jsr166 |
1.47 |
return new LinkedBlockingQueue(SIZE); |
36 |
jsr166 |
1.29 |
} |
37 |
|
|
} |
38 |
|
|
|
39 |
dl |
1.1 |
public static void main(String[] args) { |
40 |
jsr166 |
1.59 |
main(suite(), args); |
41 |
dl |
1.1 |
} |
42 |
|
|
|
43 |
|
|
public static Test suite() { |
44 |
jsr166 |
1.65 |
class Implementation implements CollectionImplementation { |
45 |
|
|
public Class<?> klazz() { return LinkedBlockingQueue.class; } |
46 |
|
|
public Collection emptyCollection() { return new LinkedBlockingQueue(); } |
47 |
dl |
1.82 |
public Object makeElement(int i) { return JSR166TestCase.itemFor(i); } |
48 |
jsr166 |
1.65 |
public boolean isConcurrent() { return true; } |
49 |
|
|
public boolean permitsNulls() { return false; } |
50 |
|
|
} |
51 |
jsr166 |
1.29 |
return newTestSuite(LinkedBlockingQueueTest.class, |
52 |
|
|
new Unbounded().testSuite(), |
53 |
jsr166 |
1.65 |
new Bounded().testSuite(), |
54 |
|
|
CollectionTest.testSuite(new Implementation())); |
55 |
dl |
1.1 |
} |
56 |
|
|
|
57 |
|
|
/** |
58 |
jsr166 |
1.51 |
* Returns a new queue of given size containing consecutive |
59 |
dl |
1.82 |
* Items 0 ... n - 1. |
60 |
dl |
1.1 |
*/ |
61 |
dl |
1.82 |
private static LinkedBlockingQueue<Item> populatedQueue(int n) { |
62 |
|
|
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<>(n); |
63 |
dl |
1.1 |
assertTrue(q.isEmpty()); |
64 |
jsr166 |
1.15 |
for (int i = 0; i < n; i++) |
65 |
dl |
1.82 |
mustOffer(q, i); |
66 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
67 |
dl |
1.82 |
mustEqual(0, q.remainingCapacity()); |
68 |
|
|
mustEqual(n, q.size()); |
69 |
|
|
mustEqual(0, q.peek()); |
70 |
dl |
1.1 |
return q; |
71 |
|
|
} |
72 |
jsr166 |
1.12 |
|
73 |
dl |
1.4 |
/** |
74 |
dl |
1.5 |
* A new queue has the indicated capacity, or Integer.MAX_VALUE if |
75 |
|
|
* none given |
76 |
dl |
1.4 |
*/ |
77 |
|
|
public void testConstructor1() { |
78 |
dl |
1.82 |
mustEqual(SIZE, new LinkedBlockingQueue<Item>(SIZE).remainingCapacity()); |
79 |
|
|
mustEqual(Integer.MAX_VALUE, new LinkedBlockingQueue<Item>().remainingCapacity()); |
80 |
dl |
1.1 |
} |
81 |
|
|
|
82 |
dl |
1.4 |
/** |
83 |
jsr166 |
1.45 |
* Constructor throws IllegalArgumentException if capacity argument nonpositive |
84 |
dl |
1.4 |
*/ |
85 |
|
|
public void testConstructor2() { |
86 |
dl |
1.1 |
try { |
87 |
dl |
1.82 |
new LinkedBlockingQueue<Item>(0); |
88 |
dl |
1.4 |
shouldThrow(); |
89 |
jsr166 |
1.17 |
} catch (IllegalArgumentException success) {} |
90 |
dl |
1.1 |
} |
91 |
|
|
|
92 |
dl |
1.4 |
/** |
93 |
jsr166 |
1.45 |
* Initializing from null Collection throws NullPointerException |
94 |
dl |
1.4 |
*/ |
95 |
|
|
public void testConstructor3() { |
96 |
dl |
1.1 |
try { |
97 |
dl |
1.82 |
new LinkedBlockingQueue<Item>(null); |
98 |
dl |
1.4 |
shouldThrow(); |
99 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
100 |
dl |
1.1 |
} |
101 |
|
|
|
102 |
dl |
1.4 |
/** |
103 |
jsr166 |
1.45 |
* Initializing from Collection of null elements throws NullPointerException |
104 |
dl |
1.4 |
*/ |
105 |
|
|
public void testConstructor4() { |
106 |
dl |
1.82 |
Collection<Item> elements = Arrays.asList(new Item[SIZE]); |
107 |
dl |
1.1 |
try { |
108 |
dl |
1.82 |
new LinkedBlockingQueue<Item>(elements); |
109 |
dl |
1.4 |
shouldThrow(); |
110 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
111 |
dl |
1.1 |
} |
112 |
|
|
|
113 |
dl |
1.4 |
/** |
114 |
jsr166 |
1.45 |
* Initializing from Collection with some null elements throws |
115 |
|
|
* NullPointerException |
116 |
dl |
1.4 |
*/ |
117 |
|
|
public void testConstructor5() { |
118 |
dl |
1.82 |
Item[] items = new Item[2]; |
119 |
|
|
items[0] = zero; |
120 |
|
|
Collection<Item> elements = Arrays.asList(items); |
121 |
dl |
1.1 |
try { |
122 |
dl |
1.82 |
new LinkedBlockingQueue<Item>(elements); |
123 |
dl |
1.4 |
shouldThrow(); |
124 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
125 |
dl |
1.1 |
} |
126 |
|
|
|
127 |
dl |
1.4 |
/** |
128 |
dl |
1.5 |
* Queue contains all elements of collection used to initialize |
129 |
dl |
1.4 |
*/ |
130 |
|
|
public void testConstructor6() { |
131 |
dl |
1.82 |
Item[] items = defaultItems; |
132 |
|
|
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(Arrays.asList(items)); |
133 |
jsr166 |
1.17 |
for (int i = 0; i < SIZE; ++i) |
134 |
dl |
1.82 |
mustEqual(items[i], q.poll()); |
135 |
dl |
1.1 |
} |
136 |
|
|
|
137 |
dl |
1.4 |
/** |
138 |
dl |
1.5 |
* Queue transitions from empty to full when elements added |
139 |
dl |
1.4 |
*/ |
140 |
dl |
1.1 |
public void testEmptyFull() { |
141 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(2); |
142 |
dl |
1.1 |
assertTrue(q.isEmpty()); |
143 |
dl |
1.82 |
mustEqual(2, q.remainingCapacity()); |
144 |
dl |
1.3 |
q.add(one); |
145 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
146 |
dl |
1.3 |
q.add(two); |
147 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
148 |
dl |
1.82 |
mustEqual(0, q.remainingCapacity()); |
149 |
dl |
1.4 |
assertFalse(q.offer(three)); |
150 |
dl |
1.1 |
} |
151 |
|
|
|
152 |
dl |
1.4 |
/** |
153 |
dl |
1.5 |
* remainingCapacity decreases on add, increases on remove |
154 |
dl |
1.4 |
*/ |
155 |
|
|
public void testRemainingCapacity() { |
156 |
dl |
1.82 |
BlockingQueue<Item> q = populatedQueue(SIZE); |
157 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
158 |
dl |
1.82 |
mustEqual(i, q.remainingCapacity()); |
159 |
|
|
mustEqual(SIZE, q.size() + q.remainingCapacity()); |
160 |
|
|
mustEqual(i, q.remove()); |
161 |
dl |
1.1 |
} |
162 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
163 |
dl |
1.82 |
mustEqual(SIZE - i, q.remainingCapacity()); |
164 |
|
|
mustEqual(SIZE, q.size() + q.remainingCapacity()); |
165 |
|
|
mustAdd(q, i); |
166 |
dl |
1.1 |
} |
167 |
|
|
} |
168 |
|
|
|
169 |
dl |
1.4 |
/** |
170 |
dl |
1.5 |
* Offer succeeds if not full; fails if full |
171 |
dl |
1.4 |
*/ |
172 |
|
|
public void testOffer() { |
173 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(1); |
174 |
dl |
1.3 |
assertTrue(q.offer(zero)); |
175 |
|
|
assertFalse(q.offer(one)); |
176 |
dl |
1.1 |
} |
177 |
|
|
|
178 |
dl |
1.4 |
/** |
179 |
jsr166 |
1.45 |
* add succeeds if not full; throws IllegalStateException if full |
180 |
dl |
1.4 |
*/ |
181 |
|
|
public void testAdd() { |
182 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(SIZE); |
183 |
jsr166 |
1.45 |
for (int i = 0; i < SIZE; ++i) |
184 |
dl |
1.82 |
mustAdd(q, i); |
185 |
|
|
mustEqual(0, q.remainingCapacity()); |
186 |
jsr166 |
1.15 |
try { |
187 |
dl |
1.82 |
q.add(new Item(SIZE)); |
188 |
jsr166 |
1.17 |
shouldThrow(); |
189 |
|
|
} catch (IllegalStateException success) {} |
190 |
dl |
1.1 |
} |
191 |
|
|
|
192 |
dl |
1.4 |
/** |
193 |
jsr166 |
1.45 |
* addAll(this) throws IllegalArgumentException |
194 |
dl |
1.6 |
*/ |
195 |
|
|
public void testAddAllSelf() { |
196 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = populatedQueue(SIZE); |
197 |
dl |
1.6 |
try { |
198 |
|
|
q.addAll(q); |
199 |
|
|
shouldThrow(); |
200 |
jsr166 |
1.17 |
} catch (IllegalArgumentException success) {} |
201 |
dl |
1.6 |
} |
202 |
|
|
|
203 |
dl |
1.4 |
/** |
204 |
dl |
1.5 |
* addAll of a collection with any null elements throws NPE after |
205 |
|
|
* possibly adding some elements |
206 |
dl |
1.4 |
*/ |
207 |
|
|
public void testAddAll3() { |
208 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(SIZE); |
209 |
|
|
Item[] items = new Item[2]; items[0] = zero; |
210 |
|
|
Collection<Item> elements = Arrays.asList(items); |
211 |
dl |
1.1 |
try { |
212 |
jsr166 |
1.45 |
q.addAll(elements); |
213 |
dl |
1.4 |
shouldThrow(); |
214 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
215 |
dl |
1.1 |
} |
216 |
jsr166 |
1.27 |
|
217 |
dl |
1.4 |
/** |
218 |
jsr166 |
1.45 |
* addAll throws IllegalStateException if not enough room |
219 |
dl |
1.4 |
*/ |
220 |
|
|
public void testAddAll4() { |
221 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(SIZE - 1); |
222 |
|
|
Item[] items = defaultItems; |
223 |
|
|
Collection<Item> elements = Arrays.asList(items); |
224 |
dl |
1.1 |
try { |
225 |
jsr166 |
1.45 |
q.addAll(elements); |
226 |
dl |
1.4 |
shouldThrow(); |
227 |
jsr166 |
1.17 |
} catch (IllegalStateException success) {} |
228 |
dl |
1.1 |
} |
229 |
jsr166 |
1.27 |
|
230 |
dl |
1.4 |
/** |
231 |
dl |
1.5 |
* Queue contains all elements, in traversal order, of successful addAll |
232 |
dl |
1.4 |
*/ |
233 |
|
|
public void testAddAll5() { |
234 |
dl |
1.82 |
Item[] empty = new Item[0]; |
235 |
|
|
Item[] items = defaultItems; |
236 |
|
|
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(SIZE); |
237 |
jsr166 |
1.17 |
assertFalse(q.addAll(Arrays.asList(empty))); |
238 |
dl |
1.82 |
assertTrue(q.addAll(Arrays.asList(items))); |
239 |
jsr166 |
1.17 |
for (int i = 0; i < SIZE; ++i) |
240 |
dl |
1.82 |
mustEqual(items[i], q.poll()); |
241 |
dl |
1.1 |
} |
242 |
|
|
|
243 |
dl |
1.4 |
/** |
244 |
dl |
1.5 |
* all elements successfully put are contained |
245 |
dl |
1.4 |
*/ |
246 |
jsr166 |
1.17 |
public void testPut() throws InterruptedException { |
247 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(SIZE); |
248 |
jsr166 |
1.17 |
for (int i = 0; i < SIZE; ++i) { |
249 |
dl |
1.82 |
Item x = itemFor(i); |
250 |
jsr166 |
1.56 |
q.put(x); |
251 |
dl |
1.82 |
mustContain(q, x); |
252 |
dl |
1.1 |
} |
253 |
dl |
1.82 |
mustEqual(0, q.remainingCapacity()); |
254 |
dl |
1.1 |
} |
255 |
|
|
|
256 |
dl |
1.4 |
/** |
257 |
dl |
1.5 |
* put blocks interruptibly if full |
258 |
dl |
1.4 |
*/ |
259 |
jsr166 |
1.17 |
public void testBlockingPut() throws InterruptedException { |
260 |
dl |
1.82 |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(SIZE); |
261 |
jsr166 |
1.44 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
262 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
263 |
jsr166 |
1.22 |
public void realRun() throws InterruptedException { |
264 |
|
|
for (int i = 0; i < SIZE; ++i) |
265 |
dl |
1.82 |
q.put(itemFor(i)); |
266 |
|
|
mustEqual(SIZE, q.size()); |
267 |
|
|
mustEqual(0, q.remainingCapacity()); |
268 |
jsr166 |
1.44 |
|
269 |
|
|
Thread.currentThread().interrupt(); |
270 |
|
|
try { |
271 |
dl |
1.82 |
q.put(ninetynine); |
272 |
jsr166 |
1.44 |
shouldThrow(); |
273 |
|
|
} catch (InterruptedException success) {} |
274 |
|
|
assertFalse(Thread.interrupted()); |
275 |
|
|
|
276 |
|
|
pleaseInterrupt.countDown(); |
277 |
jsr166 |
1.17 |
try { |
278 |
dl |
1.82 |
q.put(ninetynine); |
279 |
jsr166 |
1.22 |
shouldThrow(); |
280 |
|
|
} catch (InterruptedException success) {} |
281 |
jsr166 |
1.44 |
assertFalse(Thread.interrupted()); |
282 |
jsr166 |
1.17 |
}}); |
283 |
|
|
|
284 |
jsr166 |
1.44 |
await(pleaseInterrupt); |
285 |
jsr166 |
1.79 |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
286 |
jsr166 |
1.17 |
t.interrupt(); |
287 |
jsr166 |
1.44 |
awaitTermination(t); |
288 |
dl |
1.82 |
mustEqual(SIZE, q.size()); |
289 |
|
|
mustEqual(0, q.remainingCapacity()); |
290 |
dl |
1.1 |
} |
291 |
|
|
|
292 |
dl |
1.4 |
/** |
293 |
jsr166 |
1.44 |
* put blocks interruptibly waiting for take when full |
294 |
dl |
1.4 |
*/ |
295 |
jsr166 |
1.17 |
public void testPutWithTake() throws InterruptedException { |
296 |
jsr166 |
1.22 |
final int capacity = 2; |
297 |
dl |
1.82 |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(2); |
298 |
jsr166 |
1.44 |
final CountDownLatch pleaseTake = new CountDownLatch(1); |
299 |
|
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
300 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
301 |
jsr166 |
1.22 |
public void realRun() throws InterruptedException { |
302 |
jsr166 |
1.44 |
for (int i = 0; i < capacity; i++) |
303 |
dl |
1.82 |
q.put(itemFor(i)); |
304 |
jsr166 |
1.44 |
pleaseTake.countDown(); |
305 |
dl |
1.82 |
q.put(eightysix); |
306 |
jsr166 |
1.44 |
|
307 |
jsr166 |
1.74 |
Thread.currentThread().interrupt(); |
308 |
|
|
try { |
309 |
dl |
1.82 |
q.put(ninetynine); |
310 |
jsr166 |
1.74 |
shouldThrow(); |
311 |
|
|
} catch (InterruptedException success) {} |
312 |
|
|
assertFalse(Thread.interrupted()); |
313 |
|
|
|
314 |
jsr166 |
1.44 |
pleaseInterrupt.countDown(); |
315 |
jsr166 |
1.17 |
try { |
316 |
dl |
1.82 |
q.put(ninetynine); |
317 |
jsr166 |
1.22 |
shouldThrow(); |
318 |
|
|
} catch (InterruptedException success) {} |
319 |
jsr166 |
1.44 |
assertFalse(Thread.interrupted()); |
320 |
jsr166 |
1.17 |
}}); |
321 |
|
|
|
322 |
jsr166 |
1.44 |
await(pleaseTake); |
323 |
dl |
1.82 |
mustEqual(0, q.remainingCapacity()); |
324 |
|
|
mustEqual(0, q.take()); |
325 |
jsr166 |
1.44 |
|
326 |
|
|
await(pleaseInterrupt); |
327 |
jsr166 |
1.79 |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
328 |
jsr166 |
1.17 |
t.interrupt(); |
329 |
jsr166 |
1.44 |
awaitTermination(t); |
330 |
dl |
1.82 |
mustEqual(0, q.remainingCapacity()); |
331 |
dl |
1.1 |
} |
332 |
|
|
|
333 |
dl |
1.4 |
/** |
334 |
dl |
1.5 |
* timed offer times out if full and elements not taken |
335 |
dl |
1.4 |
*/ |
336 |
jsr166 |
1.43 |
public void testTimedOffer() { |
337 |
dl |
1.82 |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(2); |
338 |
jsr166 |
1.43 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
339 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
340 |
jsr166 |
1.17 |
public void realRun() throws InterruptedException { |
341 |
dl |
1.82 |
q.put(zero); |
342 |
|
|
q.put(one); |
343 |
jsr166 |
1.79 |
long startTime = System.nanoTime(); |
344 |
jsr166 |
1.75 |
|
345 |
dl |
1.82 |
assertFalse(q.offer(two, timeoutMillis(), MILLISECONDS)); |
346 |
jsr166 |
1.43 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
347 |
jsr166 |
1.75 |
|
348 |
|
|
Thread.currentThread().interrupt(); |
349 |
|
|
try { |
350 |
dl |
1.82 |
q.offer(three, randomTimeout(), randomTimeUnit()); |
351 |
jsr166 |
1.75 |
shouldThrow(); |
352 |
|
|
} catch (InterruptedException success) {} |
353 |
|
|
assertFalse(Thread.interrupted()); |
354 |
|
|
|
355 |
jsr166 |
1.43 |
pleaseInterrupt.countDown(); |
356 |
jsr166 |
1.21 |
try { |
357 |
dl |
1.82 |
q.offer(two, LONGER_DELAY_MS, MILLISECONDS); |
358 |
jsr166 |
1.21 |
shouldThrow(); |
359 |
|
|
} catch (InterruptedException success) {} |
360 |
jsr166 |
1.72 |
assertFalse(Thread.interrupted()); |
361 |
jsr166 |
1.21 |
}}); |
362 |
jsr166 |
1.12 |
|
363 |
jsr166 |
1.43 |
await(pleaseInterrupt); |
364 |
jsr166 |
1.79 |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
365 |
jsr166 |
1.17 |
t.interrupt(); |
366 |
jsr166 |
1.43 |
awaitTermination(t); |
367 |
dl |
1.1 |
} |
368 |
|
|
|
369 |
dl |
1.4 |
/** |
370 |
dl |
1.5 |
* take retrieves elements in FIFO order |
371 |
dl |
1.4 |
*/ |
372 |
jsr166 |
1.17 |
public void testTake() throws InterruptedException { |
373 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = populatedQueue(SIZE); |
374 |
jsr166 |
1.17 |
for (int i = 0; i < SIZE; ++i) { |
375 |
dl |
1.82 |
mustEqual(i, q.take()); |
376 |
jsr166 |
1.15 |
} |
377 |
dl |
1.1 |
} |
378 |
|
|
|
379 |
dl |
1.4 |
/** |
380 |
dl |
1.5 |
* Take removes existing elements until empty, then blocks interruptibly |
381 |
dl |
1.4 |
*/ |
382 |
jsr166 |
1.17 |
public void testBlockingTake() throws InterruptedException { |
383 |
dl |
1.82 |
final BlockingQueue<Item> q = populatedQueue(SIZE); |
384 |
jsr166 |
1.44 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
385 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
386 |
jsr166 |
1.17 |
public void realRun() throws InterruptedException { |
387 |
dl |
1.82 |
for (int i = 0; i < SIZE; i++) mustEqual(i, q.take()); |
388 |
jsr166 |
1.44 |
|
389 |
|
|
Thread.currentThread().interrupt(); |
390 |
|
|
try { |
391 |
|
|
q.take(); |
392 |
|
|
shouldThrow(); |
393 |
|
|
} catch (InterruptedException success) {} |
394 |
|
|
assertFalse(Thread.interrupted()); |
395 |
|
|
|
396 |
|
|
pleaseInterrupt.countDown(); |
397 |
jsr166 |
1.22 |
try { |
398 |
|
|
q.take(); |
399 |
|
|
shouldThrow(); |
400 |
|
|
} catch (InterruptedException success) {} |
401 |
jsr166 |
1.44 |
assertFalse(Thread.interrupted()); |
402 |
jsr166 |
1.22 |
}}); |
403 |
jsr166 |
1.17 |
|
404 |
jsr166 |
1.44 |
await(pleaseInterrupt); |
405 |
jsr166 |
1.79 |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
406 |
jsr166 |
1.17 |
t.interrupt(); |
407 |
jsr166 |
1.44 |
awaitTermination(t); |
408 |
dl |
1.1 |
} |
409 |
|
|
|
410 |
dl |
1.4 |
/** |
411 |
dl |
1.5 |
* poll succeeds unless empty |
412 |
dl |
1.4 |
*/ |
413 |
|
|
public void testPoll() { |
414 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = populatedQueue(SIZE); |
415 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
416 |
dl |
1.82 |
mustEqual(i, q.poll()); |
417 |
dl |
1.1 |
} |
418 |
jsr166 |
1.15 |
assertNull(q.poll()); |
419 |
dl |
1.1 |
} |
420 |
|
|
|
421 |
dl |
1.4 |
/** |
422 |
jsr166 |
1.33 |
* timed poll with zero timeout succeeds when non-empty, else times out |
423 |
dl |
1.4 |
*/ |
424 |
jsr166 |
1.17 |
public void testTimedPoll0() throws InterruptedException { |
425 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = populatedQueue(SIZE); |
426 |
jsr166 |
1.17 |
for (int i = 0; i < SIZE; ++i) { |
427 |
dl |
1.82 |
mustEqual(i, q.poll(0, MILLISECONDS)); |
428 |
jsr166 |
1.15 |
} |
429 |
jsr166 |
1.17 |
assertNull(q.poll(0, MILLISECONDS)); |
430 |
dl |
1.1 |
} |
431 |
|
|
|
432 |
dl |
1.4 |
/** |
433 |
jsr166 |
1.33 |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
434 |
dl |
1.4 |
*/ |
435 |
jsr166 |
1.17 |
public void testTimedPoll() throws InterruptedException { |
436 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = populatedQueue(SIZE); |
437 |
jsr166 |
1.17 |
for (int i = 0; i < SIZE; ++i) { |
438 |
jsr166 |
1.44 |
long startTime = System.nanoTime(); |
439 |
dl |
1.82 |
mustEqual(i, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
440 |
jsr166 |
1.44 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
441 |
|
|
} |
442 |
|
|
long startTime = System.nanoTime(); |
443 |
|
|
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
444 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
445 |
|
|
checkEmpty(q); |
446 |
dl |
1.1 |
} |
447 |
|
|
|
448 |
dl |
1.4 |
/** |
449 |
dl |
1.5 |
* Interrupted timed poll throws InterruptedException instead of |
450 |
|
|
* returning timeout status |
451 |
dl |
1.4 |
*/ |
452 |
jsr166 |
1.17 |
public void testInterruptedTimedPoll() throws InterruptedException { |
453 |
dl |
1.82 |
final BlockingQueue<Item> q = populatedQueue(SIZE); |
454 |
jsr166 |
1.71 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
455 |
jsr166 |
1.39 |
Thread t = newStartedThread(new CheckedRunnable() { |
456 |
jsr166 |
1.17 |
public void realRun() throws InterruptedException { |
457 |
jsr166 |
1.73 |
for (int i = 0; i < SIZE; i++) |
458 |
dl |
1.82 |
mustEqual(i, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
459 |
jsr166 |
1.73 |
|
460 |
|
|
Thread.currentThread().interrupt(); |
461 |
|
|
try { |
462 |
jsr166 |
1.79 |
q.poll(randomTimeout(), randomTimeUnit()); |
463 |
jsr166 |
1.73 |
shouldThrow(); |
464 |
|
|
} catch (InterruptedException success) {} |
465 |
|
|
assertFalse(Thread.interrupted()); |
466 |
jsr166 |
1.71 |
|
467 |
|
|
pleaseInterrupt.countDown(); |
468 |
jsr166 |
1.19 |
try { |
469 |
jsr166 |
1.81 |
q.poll(LONGER_DELAY_MS, MILLISECONDS); |
470 |
jsr166 |
1.19 |
shouldThrow(); |
471 |
jsr166 |
1.71 |
} catch (InterruptedException success) {} |
472 |
|
|
assertFalse(Thread.interrupted()); |
473 |
jsr166 |
1.19 |
}}); |
474 |
jsr166 |
1.17 |
|
475 |
jsr166 |
1.71 |
await(pleaseInterrupt); |
476 |
jsr166 |
1.79 |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
477 |
jsr166 |
1.17 |
t.interrupt(); |
478 |
jsr166 |
1.62 |
awaitTermination(t); |
479 |
jsr166 |
1.39 |
checkEmpty(q); |
480 |
dl |
1.1 |
} |
481 |
|
|
|
482 |
dl |
1.4 |
/** |
483 |
dl |
1.5 |
* peek returns next element, or null if empty |
484 |
dl |
1.4 |
*/ |
485 |
|
|
public void testPeek() { |
486 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = populatedQueue(SIZE); |
487 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
488 |
dl |
1.82 |
mustEqual(i, q.peek()); |
489 |
|
|
mustEqual(i, q.poll()); |
490 |
dl |
1.1 |
assertTrue(q.peek() == null || |
491 |
jsr166 |
1.23 |
!q.peek().equals(i)); |
492 |
dl |
1.1 |
} |
493 |
jsr166 |
1.15 |
assertNull(q.peek()); |
494 |
dl |
1.1 |
} |
495 |
|
|
|
496 |
dl |
1.4 |
/** |
497 |
dl |
1.5 |
* element returns next element, or throws NSEE if empty |
498 |
dl |
1.4 |
*/ |
499 |
|
|
public void testElement() { |
500 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = populatedQueue(SIZE); |
501 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
502 |
dl |
1.82 |
mustEqual(i, q.element()); |
503 |
|
|
mustEqual(i, q.poll()); |
504 |
dl |
1.1 |
} |
505 |
|
|
try { |
506 |
|
|
q.element(); |
507 |
dl |
1.4 |
shouldThrow(); |
508 |
jsr166 |
1.17 |
} catch (NoSuchElementException success) {} |
509 |
dl |
1.1 |
} |
510 |
|
|
|
511 |
dl |
1.4 |
/** |
512 |
dl |
1.5 |
* remove removes next element, or throws NSEE if empty |
513 |
dl |
1.4 |
*/ |
514 |
|
|
public void testRemove() { |
515 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = populatedQueue(SIZE); |
516 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
517 |
dl |
1.82 |
mustEqual(i, q.remove()); |
518 |
dl |
1.1 |
} |
519 |
|
|
try { |
520 |
|
|
q.remove(); |
521 |
dl |
1.4 |
shouldThrow(); |
522 |
jsr166 |
1.17 |
} catch (NoSuchElementException success) {} |
523 |
dl |
1.1 |
} |
524 |
|
|
|
525 |
dl |
1.4 |
/** |
526 |
dl |
1.11 |
* An add following remove(x) succeeds |
527 |
|
|
*/ |
528 |
jsr166 |
1.17 |
public void testRemoveElementAndAdd() throws InterruptedException { |
529 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(); |
530 |
|
|
assertTrue(q.add(one)); |
531 |
|
|
assertTrue(q.add(two)); |
532 |
|
|
mustRemove(q, one); |
533 |
|
|
mustRemove(q, two); |
534 |
|
|
mustAdd(q, three); |
535 |
jsr166 |
1.52 |
assertNotNull(q.take()); |
536 |
dl |
1.11 |
} |
537 |
jsr166 |
1.12 |
|
538 |
dl |
1.4 |
/** |
539 |
dl |
1.5 |
* contains(x) reports true when elements added but not yet removed |
540 |
dl |
1.4 |
*/ |
541 |
|
|
public void testContains() { |
542 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = populatedQueue(SIZE); |
543 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
544 |
dl |
1.82 |
mustContain(q, i); |
545 |
dl |
1.1 |
q.poll(); |
546 |
dl |
1.82 |
mustNotContain(q, i); |
547 |
dl |
1.1 |
} |
548 |
|
|
} |
549 |
|
|
|
550 |
dl |
1.4 |
/** |
551 |
dl |
1.5 |
* clear removes all elements |
552 |
dl |
1.4 |
*/ |
553 |
|
|
public void testClear() { |
554 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = populatedQueue(SIZE); |
555 |
dl |
1.1 |
q.clear(); |
556 |
|
|
assertTrue(q.isEmpty()); |
557 |
dl |
1.82 |
mustEqual(0, q.size()); |
558 |
|
|
mustEqual(SIZE, q.remainingCapacity()); |
559 |
dl |
1.3 |
q.add(one); |
560 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
561 |
dl |
1.10 |
assertTrue(q.contains(one)); |
562 |
dl |
1.1 |
q.clear(); |
563 |
|
|
assertTrue(q.isEmpty()); |
564 |
|
|
} |
565 |
|
|
|
566 |
dl |
1.4 |
/** |
567 |
dl |
1.5 |
* containsAll(c) is true when c contains a subset of elements |
568 |
dl |
1.4 |
*/ |
569 |
|
|
public void testContainsAll() { |
570 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = populatedQueue(SIZE); |
571 |
|
|
LinkedBlockingQueue<Item> p = new LinkedBlockingQueue<Item>(SIZE); |
572 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
573 |
dl |
1.1 |
assertTrue(q.containsAll(p)); |
574 |
|
|
assertFalse(p.containsAll(q)); |
575 |
dl |
1.82 |
mustAdd(p, i); |
576 |
dl |
1.1 |
} |
577 |
|
|
assertTrue(p.containsAll(q)); |
578 |
|
|
} |
579 |
|
|
|
580 |
dl |
1.4 |
/** |
581 |
dl |
1.5 |
* retainAll(c) retains only those elements of c and reports true if changed |
582 |
dl |
1.4 |
*/ |
583 |
|
|
public void testRetainAll() { |
584 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = populatedQueue(SIZE); |
585 |
|
|
LinkedBlockingQueue<Item> p = populatedQueue(SIZE); |
586 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
587 |
dl |
1.1 |
boolean changed = q.retainAll(p); |
588 |
|
|
if (i == 0) |
589 |
|
|
assertFalse(changed); |
590 |
|
|
else |
591 |
|
|
assertTrue(changed); |
592 |
|
|
|
593 |
|
|
assertTrue(q.containsAll(p)); |
594 |
dl |
1.82 |
mustEqual(SIZE - i, q.size()); |
595 |
dl |
1.1 |
p.remove(); |
596 |
|
|
} |
597 |
|
|
} |
598 |
|
|
|
599 |
dl |
1.4 |
/** |
600 |
dl |
1.5 |
* removeAll(c) removes only those elements of c and reports true if changed |
601 |
dl |
1.4 |
*/ |
602 |
|
|
public void testRemoveAll() { |
603 |
dl |
1.3 |
for (int i = 1; i < SIZE; ++i) { |
604 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = populatedQueue(SIZE); |
605 |
|
|
LinkedBlockingQueue<Item> p = populatedQueue(i); |
606 |
dl |
1.1 |
assertTrue(q.removeAll(p)); |
607 |
dl |
1.82 |
mustEqual(SIZE - i, q.size()); |
608 |
dl |
1.1 |
for (int j = 0; j < i; ++j) { |
609 |
dl |
1.82 |
mustNotContain(q, p.remove()); |
610 |
dl |
1.1 |
} |
611 |
|
|
} |
612 |
|
|
} |
613 |
|
|
|
614 |
dl |
1.4 |
/** |
615 |
jsr166 |
1.36 |
* toArray contains all elements in FIFO order |
616 |
dl |
1.4 |
*/ |
617 |
jsr166 |
1.36 |
public void testToArray() { |
618 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = populatedQueue(SIZE); |
619 |
jsr166 |
1.78 |
Object[] a = q.toArray(); |
620 |
|
|
assertSame(Object[].class, a.getClass()); |
621 |
|
|
for (Object o : a) |
622 |
|
|
assertSame(o, q.poll()); |
623 |
|
|
assertTrue(q.isEmpty()); |
624 |
dl |
1.1 |
} |
625 |
|
|
|
626 |
dl |
1.4 |
/** |
627 |
jsr166 |
1.36 |
* toArray(a) contains all elements in FIFO order |
628 |
dl |
1.4 |
*/ |
629 |
jsr166 |
1.17 |
public void testToArray2() throws InterruptedException { |
630 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = populatedQueue(SIZE); |
631 |
|
|
Item[] items = new Item[SIZE]; |
632 |
|
|
Item[] array = q.toArray(items); |
633 |
|
|
assertSame(items, array); |
634 |
|
|
for (Item o : items) |
635 |
jsr166 |
1.78 |
assertSame(o, q.poll()); |
636 |
|
|
assertTrue(q.isEmpty()); |
637 |
dl |
1.1 |
} |
638 |
dl |
1.6 |
|
639 |
|
|
/** |
640 |
jsr166 |
1.34 |
* toArray(incompatible array type) throws ArrayStoreException |
641 |
dl |
1.6 |
*/ |
642 |
|
|
public void testToArray1_BadArg() { |
643 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = populatedQueue(SIZE); |
644 |
jsr166 |
1.15 |
try { |
645 |
jsr166 |
1.34 |
q.toArray(new String[10]); |
646 |
jsr166 |
1.15 |
shouldThrow(); |
647 |
jsr166 |
1.17 |
} catch (ArrayStoreException success) {} |
648 |
dl |
1.6 |
} |
649 |
|
|
|
650 |
dl |
1.4 |
/** |
651 |
dl |
1.5 |
* iterator iterates through all elements |
652 |
dl |
1.4 |
*/ |
653 |
jsr166 |
1.17 |
public void testIterator() throws InterruptedException { |
654 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = populatedQueue(SIZE); |
655 |
|
|
Iterator<? extends Item> it = q.iterator(); |
656 |
jsr166 |
1.57 |
int i; |
657 |
|
|
for (i = 0; it.hasNext(); i++) |
658 |
dl |
1.82 |
mustContain(q, it.next()); |
659 |
|
|
mustEqual(i, SIZE); |
660 |
jsr166 |
1.57 |
assertIteratorExhausted(it); |
661 |
|
|
|
662 |
|
|
it = q.iterator(); |
663 |
|
|
for (i = 0; it.hasNext(); i++) |
664 |
dl |
1.82 |
mustEqual(it.next(), q.take()); |
665 |
|
|
mustEqual(i, SIZE); |
666 |
jsr166 |
1.57 |
assertIteratorExhausted(it); |
667 |
|
|
} |
668 |
|
|
|
669 |
|
|
/** |
670 |
|
|
* iterator of empty collection has no elements |
671 |
|
|
*/ |
672 |
|
|
public void testEmptyIterator() { |
673 |
dl |
1.82 |
assertIteratorExhausted(new LinkedBlockingQueue<Item>().iterator()); |
674 |
dl |
1.1 |
} |
675 |
|
|
|
676 |
dl |
1.4 |
/** |
677 |
dl |
1.5 |
* iterator.remove removes current element |
678 |
|
|
*/ |
679 |
jsr166 |
1.26 |
public void testIteratorRemove() { |
680 |
dl |
1.82 |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(3); |
681 |
dl |
1.5 |
q.add(two); |
682 |
|
|
q.add(one); |
683 |
|
|
q.add(three); |
684 |
|
|
|
685 |
dl |
1.82 |
Iterator<? extends Item> it = q.iterator(); |
686 |
dl |
1.5 |
it.next(); |
687 |
|
|
it.remove(); |
688 |
jsr166 |
1.12 |
|
689 |
dl |
1.5 |
it = q.iterator(); |
690 |
jsr166 |
1.25 |
assertSame(it.next(), one); |
691 |
|
|
assertSame(it.next(), three); |
692 |
dl |
1.5 |
assertFalse(it.hasNext()); |
693 |
|
|
} |
694 |
|
|
|
695 |
|
|
/** |
696 |
|
|
* iterator ordering is FIFO |
697 |
dl |
1.4 |
*/ |
698 |
dl |
1.1 |
public void testIteratorOrdering() { |
699 |
dl |
1.82 |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(3); |
700 |
dl |
1.3 |
q.add(one); |
701 |
|
|
q.add(two); |
702 |
|
|
q.add(three); |
703 |
dl |
1.82 |
mustEqual(0, q.remainingCapacity()); |
704 |
dl |
1.1 |
int k = 0; |
705 |
dl |
1.82 |
for (Iterator<? extends Item> it = q.iterator(); it.hasNext();) { |
706 |
|
|
mustEqual(++k, it.next()); |
707 |
dl |
1.1 |
} |
708 |
dl |
1.82 |
mustEqual(3, k); |
709 |
dl |
1.1 |
} |
710 |
|
|
|
711 |
dl |
1.4 |
/** |
712 |
dl |
1.5 |
* Modifications do not cause iterators to fail |
713 |
dl |
1.4 |
*/ |
714 |
jsr166 |
1.26 |
public void testWeaklyConsistentIteration() { |
715 |
dl |
1.82 |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(3); |
716 |
dl |
1.3 |
q.add(one); |
717 |
|
|
q.add(two); |
718 |
|
|
q.add(three); |
719 |
dl |
1.82 |
for (Iterator<? extends Item> it = q.iterator(); it.hasNext();) { |
720 |
jsr166 |
1.17 |
q.remove(); |
721 |
|
|
it.next(); |
722 |
dl |
1.1 |
} |
723 |
dl |
1.82 |
mustEqual(0, q.size()); |
724 |
dl |
1.1 |
} |
725 |
|
|
|
726 |
dl |
1.4 |
/** |
727 |
dl |
1.5 |
* toString contains toStrings of elements |
728 |
dl |
1.4 |
*/ |
729 |
|
|
public void testToString() { |
730 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = populatedQueue(SIZE); |
731 |
dl |
1.1 |
String s = q.toString(); |
732 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
733 |
jsr166 |
1.44 |
assertTrue(s.contains(String.valueOf(i))); |
734 |
dl |
1.1 |
} |
735 |
jsr166 |
1.12 |
} |
736 |
dl |
1.1 |
|
737 |
dl |
1.4 |
/** |
738 |
dl |
1.5 |
* offer transfers elements across Executor tasks |
739 |
dl |
1.4 |
*/ |
740 |
dl |
1.1 |
public void testOfferInExecutor() { |
741 |
dl |
1.82 |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(2); |
742 |
dl |
1.3 |
q.add(one); |
743 |
|
|
q.add(two); |
744 |
jsr166 |
1.44 |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
745 |
jsr166 |
1.61 |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
746 |
|
|
try (PoolCleaner cleaner = cleaner(executor)) { |
747 |
|
|
executor.execute(new CheckedRunnable() { |
748 |
|
|
public void realRun() throws InterruptedException { |
749 |
|
|
assertFalse(q.offer(three)); |
750 |
|
|
threadsStarted.await(); |
751 |
|
|
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
752 |
dl |
1.82 |
mustEqual(0, q.remainingCapacity()); |
753 |
jsr166 |
1.61 |
}}); |
754 |
|
|
|
755 |
|
|
executor.execute(new CheckedRunnable() { |
756 |
|
|
public void realRun() throws InterruptedException { |
757 |
|
|
threadsStarted.await(); |
758 |
|
|
assertSame(one, q.take()); |
759 |
|
|
}}); |
760 |
|
|
} |
761 |
dl |
1.1 |
} |
762 |
|
|
|
763 |
dl |
1.4 |
/** |
764 |
jsr166 |
1.44 |
* timed poll retrieves elements across Executor threads |
765 |
dl |
1.4 |
*/ |
766 |
dl |
1.1 |
public void testPollInExecutor() { |
767 |
dl |
1.82 |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(2); |
768 |
jsr166 |
1.44 |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
769 |
jsr166 |
1.61 |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
770 |
|
|
try (PoolCleaner cleaner = cleaner(executor)) { |
771 |
|
|
executor.execute(new CheckedRunnable() { |
772 |
|
|
public void realRun() throws InterruptedException { |
773 |
|
|
assertNull(q.poll()); |
774 |
|
|
threadsStarted.await(); |
775 |
|
|
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
776 |
|
|
checkEmpty(q); |
777 |
|
|
}}); |
778 |
|
|
|
779 |
|
|
executor.execute(new CheckedRunnable() { |
780 |
|
|
public void realRun() throws InterruptedException { |
781 |
|
|
threadsStarted.await(); |
782 |
|
|
q.put(one); |
783 |
|
|
}}); |
784 |
|
|
} |
785 |
dl |
1.2 |
} |
786 |
|
|
|
787 |
dl |
1.4 |
/** |
788 |
jsr166 |
1.76 |
* A deserialized/reserialized queue has same elements in same order |
789 |
dl |
1.4 |
*/ |
790 |
jsr166 |
1.17 |
public void testSerialization() throws Exception { |
791 |
dl |
1.82 |
Queue<Item> x = populatedQueue(SIZE); |
792 |
|
|
Queue<Item> y = serialClone(x); |
793 |
dl |
1.2 |
|
794 |
jsr166 |
1.52 |
assertNotSame(x, y); |
795 |
dl |
1.82 |
mustEqual(x.size(), y.size()); |
796 |
|
|
mustEqual(x.toString(), y.toString()); |
797 |
jsr166 |
1.46 |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
798 |
|
|
while (!x.isEmpty()) { |
799 |
|
|
assertFalse(y.isEmpty()); |
800 |
dl |
1.82 |
mustEqual(x.remove(), y.remove()); |
801 |
jsr166 |
1.46 |
} |
802 |
|
|
assertTrue(y.isEmpty()); |
803 |
dl |
1.6 |
} |
804 |
|
|
|
805 |
|
|
/** |
806 |
|
|
* drainTo(c) empties queue into another collection c |
807 |
jsr166 |
1.12 |
*/ |
808 |
dl |
1.6 |
public void testDrainTo() { |
809 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = populatedQueue(SIZE); |
810 |
|
|
ArrayList<Item> l = new ArrayList<Item>(); |
811 |
dl |
1.6 |
q.drainTo(l); |
812 |
dl |
1.82 |
mustEqual(0, q.size()); |
813 |
|
|
mustEqual(SIZE, l.size()); |
814 |
jsr166 |
1.12 |
for (int i = 0; i < SIZE; ++i) |
815 |
dl |
1.82 |
mustEqual(l.get(i), i); |
816 |
dl |
1.10 |
q.add(zero); |
817 |
|
|
q.add(one); |
818 |
|
|
assertFalse(q.isEmpty()); |
819 |
dl |
1.82 |
mustContain(q, zero); |
820 |
|
|
mustContain(q, one); |
821 |
dl |
1.10 |
l.clear(); |
822 |
|
|
q.drainTo(l); |
823 |
dl |
1.82 |
mustEqual(0, q.size()); |
824 |
|
|
mustEqual(2, l.size()); |
825 |
jsr166 |
1.12 |
for (int i = 0; i < 2; ++i) |
826 |
dl |
1.82 |
mustEqual(l.get(i), i); |
827 |
dl |
1.6 |
} |
828 |
|
|
|
829 |
|
|
/** |
830 |
|
|
* drainTo empties full queue, unblocking a waiting put. |
831 |
jsr166 |
1.12 |
*/ |
832 |
jsr166 |
1.17 |
public void testDrainToWithActivePut() throws InterruptedException { |
833 |
dl |
1.82 |
final LinkedBlockingQueue<Item> q = populatedQueue(SIZE); |
834 |
jsr166 |
1.17 |
Thread t = new Thread(new CheckedRunnable() { |
835 |
|
|
public void realRun() throws InterruptedException { |
836 |
dl |
1.82 |
q.put(new Item(SIZE + 1)); |
837 |
jsr166 |
1.17 |
}}); |
838 |
|
|
|
839 |
|
|
t.start(); |
840 |
dl |
1.82 |
ArrayList<Item> l = new ArrayList<Item>(); |
841 |
jsr166 |
1.17 |
q.drainTo(l); |
842 |
|
|
assertTrue(l.size() >= SIZE); |
843 |
|
|
for (int i = 0; i < SIZE; ++i) |
844 |
dl |
1.82 |
mustEqual(l.get(i), i); |
845 |
jsr166 |
1.17 |
t.join(); |
846 |
|
|
assertTrue(q.size() + l.size() >= SIZE); |
847 |
dl |
1.6 |
} |
848 |
|
|
|
849 |
|
|
/** |
850 |
jsr166 |
1.30 |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
851 |
jsr166 |
1.12 |
*/ |
852 |
dl |
1.6 |
public void testDrainToN() { |
853 |
dl |
1.82 |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(); |
854 |
dl |
1.6 |
for (int i = 0; i < SIZE + 2; ++i) { |
855 |
jsr166 |
1.13 |
for (int j = 0; j < SIZE; j++) |
856 |
dl |
1.82 |
mustOffer(q, j); |
857 |
|
|
ArrayList<Item> l = new ArrayList<Item>(); |
858 |
dl |
1.6 |
q.drainTo(l, i); |
859 |
jsr166 |
1.31 |
int k = (i < SIZE) ? i : SIZE; |
860 |
dl |
1.82 |
mustEqual(k, l.size()); |
861 |
|
|
mustEqual(SIZE - k, q.size()); |
862 |
jsr166 |
1.12 |
for (int j = 0; j < k; ++j) |
863 |
dl |
1.82 |
mustEqual(l.get(j), j); |
864 |
jsr166 |
1.55 |
do {} while (q.poll() != null); |
865 |
dl |
1.2 |
} |
866 |
dl |
1.1 |
} |
867 |
|
|
|
868 |
jsr166 |
1.53 |
/** |
869 |
|
|
* remove(null), contains(null) always return false |
870 |
|
|
*/ |
871 |
|
|
public void testNeverContainsNull() { |
872 |
|
|
Collection<?>[] qs = { |
873 |
|
|
new LinkedBlockingQueue<Object>(), |
874 |
|
|
populatedQueue(2), |
875 |
|
|
}; |
876 |
|
|
|
877 |
|
|
for (Collection<?> q : qs) { |
878 |
|
|
assertFalse(q.contains(null)); |
879 |
|
|
assertFalse(q.remove(null)); |
880 |
|
|
} |
881 |
|
|
} |
882 |
|
|
|
883 |
dl |
1.1 |
} |