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