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