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