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