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