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