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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
|
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 |
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 |
|
|
67 |
|
} |
68 |
|
|
69 |
|
/** |
70 |
< |
* Constructor throws IAE if capacity argument nonpositive |
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 |
< |
} |
53 |
< |
catch (IllegalArgumentException success) {} |
76 |
> |
} catch (IllegalArgumentException success) {} |
77 |
|
} |
78 |
|
|
79 |
|
/** |
81 |
|
*/ |
82 |
|
public void testConstructor3() { |
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 |
|
* 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 |
|
* 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 |
|
* 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 |
|
* 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 |
|
/** |
171 |
|
} |
172 |
|
|
173 |
|
/** |
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 |
– |
/** |
174 |
|
* Offer succeeds if not full; fails if full |
175 |
|
*/ |
176 |
|
public void testOffer() { |
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)); |
196 |
– |
} catch (IllegalStateException success) { |
197 |
– |
} |
198 |
– |
} |
199 |
– |
|
200 |
– |
/** |
201 |
– |
* addAll(null) throws NPE |
202 |
– |
*/ |
203 |
– |
public void testAddAll1() { |
204 |
– |
try { |
205 |
– |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
206 |
– |
q.addAll(null); |
193 |
|
shouldThrow(); |
194 |
< |
} |
209 |
< |
catch (NullPointerException success) {} |
194 |
> |
} catch (IllegalStateException success) {} |
195 |
|
} |
196 |
|
|
197 |
|
/** |
202 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
203 |
|
q.addAll(q); |
204 |
|
shouldThrow(); |
205 |
< |
} |
221 |
< |
catch (IllegalArgumentException success) {} |
205 |
> |
} catch (IllegalArgumentException success) {} |
206 |
|
} |
207 |
|
|
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 |
– |
} |
208 |
|
/** |
209 |
|
* addAll of a collection with any null elements throws NPE after |
210 |
|
* possibly adding some elements |
217 |
|
ints[i] = new Integer(i); |
218 |
|
q.addAll(Arrays.asList(ints)); |
219 |
|
shouldThrow(); |
220 |
< |
} |
250 |
< |
catch (NullPointerException success) {} |
220 |
> |
} catch (NullPointerException success) {} |
221 |
|
} |
222 |
+ |
|
223 |
|
/** |
224 |
|
* addAll throws ISE if not enough room |
225 |
|
*/ |
231 |
|
ints[i] = new Integer(i); |
232 |
|
q.addAll(Arrays.asList(ints)); |
233 |
|
shouldThrow(); |
234 |
< |
} |
264 |
< |
catch (IllegalStateException success) {} |
234 |
> |
} catch (IllegalStateException success) {} |
235 |
|
} |
236 |
+ |
|
237 |
|
/** |
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) |
279 |
< |
assertEquals(ints[i], q.poll()); |
280 |
< |
} |
281 |
< |
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 |
|
/** |
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 |
– |
/** |
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); |
309 |
< |
assertTrue(q.contains(I)); |
310 |
< |
} |
311 |
< |
assertEquals(0, q.remainingCapacity()); |
312 |
< |
} |
313 |
< |
catch (InterruptedException ie) { |
314 |
< |
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 |
|
* put blocks interruptibly if full |
267 |
|
*/ |
268 |
< |
public void testBlockingPut() { |
268 |
> |
public void testBlockingPut() throws InterruptedException { |
269 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
270 |
< |
Thread t = new Thread(new Runnable() { |
271 |
< |
public void run() { |
272 |
< |
int added = 0; |
273 |
< |
try { |
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(MEDIUM_DELAY_MS); |
287 |
< |
t.interrupt(); |
288 |
< |
t.join(); |
289 |
< |
} |
290 |
< |
catch (InterruptedException ie) { |
291 |
< |
unexpectedException(); |
292 |
< |
} |
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 |
< |
* put blocks waiting for take when full |
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(q.remainingCapacity(), 0); |
326 |
> |
assertEquals(0, q.take()); |
327 |
> |
|
328 |
> |
await(pleaseInterrupt); |
329 |
> |
assertThreadStaysAlive(t); |
330 |
> |
t.interrupt(); |
331 |
> |
awaitTermination(t); |
332 |
> |
assertEquals(q.remainingCapacity(), 0); |
333 |
|
} |
334 |
|
|
335 |
|
/** |
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(); |
354 |
< |
Thread.sleep(SHORT_DELAY_MS); |
355 |
< |
t.interrupt(); |
356 |
< |
t.join(); |
357 |
< |
} catch (Exception e) { |
358 |
< |
unexpectedException(); |
406 |
< |
} |
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 |
> |
await(pleaseInterrupt); |
356 |
> |
assertThreadStaysAlive(t); |
357 |
> |
t.interrupt(); |
358 |
> |
awaitTermination(t); |
359 |
|
} |
360 |
|
|
361 |
|
/** |
362 |
|
* take retrieves elements in FIFO order |
363 |
|
*/ |
364 |
< |
public void testTake() { |
365 |
< |
try { |
366 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
367 |
< |
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(); |
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 |
|
* 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 |
< |
} |
382 |
< |
q.take(); |
383 |
< |
threadShouldThrow(); |
384 |
< |
} catch (InterruptedException success) { |
385 |
< |
} |
386 |
< |
}}); |
387 |
< |
try { |
388 |
< |
t.start(); |
464 |
< |
Thread.sleep(SHORT_DELAY_MS); |
465 |
< |
t.interrupt(); |
466 |
< |
t.join(); |
467 |
< |
} |
468 |
< |
catch (InterruptedException ie) { |
469 |
< |
unexpectedException(); |
470 |
< |
} |
471 |
< |
} |
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 |
|
* poll succeeds unless empty |
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 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
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)); |
495 |
< |
} catch (InterruptedException e) { |
496 |
< |
unexpectedException(); |
497 |
< |
} |
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 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
428 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
429 |
|
*/ |
430 |
< |
public void testTimedPoll() { |
431 |
< |
try { |
432 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
433 |
< |
for (int i = 0; i < SIZE; ++i) { |
434 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
435 |
< |
} |
436 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
437 |
< |
} catch (InterruptedException e) { |
438 |
< |
unexpectedException(); |
439 |
< |
} |
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 |
|
* Interrupted timed poll throws InterruptedException instead of |
445 |
|
* returning timeout status |
446 |
|
*/ |
447 |
< |
public void testInterruptedTimedPoll() { |
448 |
< |
Thread t = new Thread(new Runnable() { |
449 |
< |
public void run() { |
450 |
< |
try { |
451 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
452 |
< |
for (int i = 0; i < SIZE; ++i) { |
453 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
454 |
< |
} |
455 |
< |
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) { } |
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(); |
466 |
< |
} |
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 |
|
|
569 |
– |
|
474 |
|
/** |
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 |
|
/** |
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 |
< |
} |
597 |
< |
catch (NoSuchElementException success) {} |
500 |
> |
} catch (NoSuchElementException success) {} |
501 |
|
} |
502 |
|
|
503 |
|
/** |
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) { |
612 |
< |
} |
613 |
< |
} |
614 |
< |
|
615 |
< |
/** |
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()); |
514 |
> |
} catch (NoSuchElementException success) {} |
515 |
|
} |
516 |
|
|
517 |
|
/** |
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 |
|
} |
592 |
|
} |
593 |
|
|
594 |
|
/** |
595 |
< |
* toArray contains all elements |
595 |
> |
* toArray contains all elements in FIFO order |
596 |
|
*/ |
597 |
|
public void testToArray() { |
598 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
599 |
< |
Object[] o = q.toArray(); |
600 |
< |
try { |
601 |
< |
for (int i = 0; i < o.length; i++) |
715 |
< |
assertEquals(o[i], q.take()); |
716 |
< |
} catch (InterruptedException e) { |
717 |
< |
unexpectedException(); |
718 |
< |
} |
599 |
> |
Object[] o = q.toArray(); |
600 |
> |
for (int i = 0; i < o.length; i++) |
601 |
> |
assertSame(o[i], q.poll()); |
602 |
|
} |
603 |
|
|
604 |
|
/** |
605 |
< |
* toArray(a) contains all elements |
605 |
> |
* toArray(a) contains all elements in FIFO order |
606 |
|
*/ |
607 |
|
public void testToArray2() { |
608 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
609 |
< |
Integer[] ints = new Integer[SIZE]; |
610 |
< |
ints = (Integer[])q.toArray(ints); |
611 |
< |
try { |
612 |
< |
for (int i = 0; i < ints.length; i++) |
613 |
< |
assertEquals(ints[i], q.take()); |
731 |
< |
} catch (InterruptedException e) { |
732 |
< |
unexpectedException(); |
733 |
< |
} |
608 |
> |
ArrayBlockingQueue<Integer> q = populatedQueue(SIZE); |
609 |
> |
Integer[] ints = new Integer[SIZE]; |
610 |
> |
Integer[] array = q.toArray(ints); |
611 |
> |
assertSame(ints, array); |
612 |
> |
for (int i = 0; i < ints.length; i++) |
613 |
> |
assertSame(ints[i], q.poll()); |
614 |
|
} |
615 |
|
|
616 |
|
/** |
617 |
< |
* toArray(null) throws NPE |
738 |
< |
*/ |
739 |
< |
public void testToArray_BadArg() { |
740 |
< |
try { |
741 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
742 |
< |
Object o[] = q.toArray(null); |
743 |
< |
shouldThrow(); |
744 |
< |
} catch (NullPointerException success) {} |
745 |
< |
} |
746 |
< |
|
747 |
< |
/** |
748 |
< |
* toArray with incompatible array type throws CCE |
617 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
618 |
|
*/ |
619 |
|
public void testToArray1_BadArg() { |
620 |
< |
try { |
621 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
622 |
< |
Object o[] = q.toArray(new String[10] ); |
623 |
< |
shouldThrow(); |
624 |
< |
} catch (ArrayStoreException success) {} |
620 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
621 |
> |
try { |
622 |
> |
q.toArray(new String[10]); |
623 |
> |
shouldThrow(); |
624 |
> |
} catch (ArrayStoreException success) {} |
625 |
|
} |
626 |
|
|
758 |
– |
|
627 |
|
/** |
628 |
|
* iterator iterates through all elements |
629 |
|
*/ |
630 |
< |
public void testIterator() { |
630 |
> |
public void testIterator() throws InterruptedException { |
631 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
632 |
< |
Iterator it = q.iterator(); |
633 |
< |
try { |
634 |
< |
while (it.hasNext()) { |
635 |
< |
assertEquals(it.next(), q.take()); |
768 |
< |
} |
769 |
< |
} catch (InterruptedException e) { |
770 |
< |
unexpectedException(); |
771 |
< |
} |
632 |
> |
Iterator it = q.iterator(); |
633 |
> |
while (it.hasNext()) { |
634 |
> |
assertEquals(it.next(), q.take()); |
635 |
> |
} |
636 |
|
} |
637 |
|
|
638 |
|
/** |
639 |
|
* iterator.remove removes current element |
640 |
|
*/ |
641 |
< |
public void testIteratorRemove () { |
641 |
> |
public void testIteratorRemove() { |
642 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
643 |
|
q.add(two); |
644 |
|
q.add(one); |
649 |
|
it.remove(); |
650 |
|
|
651 |
|
it = q.iterator(); |
652 |
< |
assertEquals(it.next(), one); |
653 |
< |
assertEquals(it.next(), three); |
652 |
> |
assertSame(it.next(), one); |
653 |
> |
assertSame(it.next(), three); |
654 |
|
assertFalse(it.hasNext()); |
655 |
|
} |
656 |
|
|
667 |
|
|
668 |
|
int k = 0; |
669 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
670 |
< |
int i = ((Integer)(it.next())).intValue(); |
807 |
< |
assertEquals(++k, i); |
670 |
> |
assertEquals(++k, it.next()); |
671 |
|
} |
672 |
|
assertEquals(3, k); |
673 |
|
} |
675 |
|
/** |
676 |
|
* Modifications do not cause iterators to fail |
677 |
|
*/ |
678 |
< |
public void testWeaklyConsistentIteration () { |
678 |
> |
public void testWeaklyConsistentIteration() { |
679 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
680 |
|
q.add(one); |
681 |
|
q.add(two); |
682 |
|
q.add(three); |
683 |
< |
try { |
684 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
685 |
< |
q.remove(); |
823 |
< |
it.next(); |
824 |
< |
} |
825 |
< |
} |
826 |
< |
catch (ConcurrentModificationException e) { |
827 |
< |
unexpectedException(); |
683 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
684 |
> |
q.remove(); |
685 |
> |
it.next(); |
686 |
|
} |
687 |
|
assertEquals(0, q.size()); |
688 |
|
} |
689 |
|
|
832 |
– |
|
690 |
|
/** |
691 |
|
* toString contains toStrings of elements |
692 |
|
*/ |
694 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
695 |
|
String s = q.toString(); |
696 |
|
for (int i = 0; i < SIZE; ++i) { |
697 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
697 |
> |
assertTrue(s.contains(String.valueOf(i))); |
698 |
|
} |
699 |
|
} |
700 |
|
|
844 |
– |
|
701 |
|
/** |
702 |
|
* offer transfers elements across Executor tasks |
703 |
|
*/ |
706 |
|
q.add(one); |
707 |
|
q.add(two); |
708 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
709 |
< |
executor.execute(new Runnable() { |
710 |
< |
public void run() { |
711 |
< |
threadAssertFalse(q.offer(three)); |
712 |
< |
try { |
713 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
714 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
715 |
< |
} |
716 |
< |
catch (InterruptedException e) { |
717 |
< |
threadUnexpectedException(); |
718 |
< |
} |
719 |
< |
} |
720 |
< |
}); |
721 |
< |
|
722 |
< |
executor.execute(new Runnable() { |
723 |
< |
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 |
< |
}); |
709 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
710 |
> |
executor.execute(new CheckedRunnable() { |
711 |
> |
public void realRun() throws InterruptedException { |
712 |
> |
assertFalse(q.offer(three)); |
713 |
> |
threadsStarted.await(); |
714 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
715 |
> |
assertEquals(0, q.remainingCapacity()); |
716 |
> |
}}); |
717 |
> |
|
718 |
> |
executor.execute(new CheckedRunnable() { |
719 |
> |
public void realRun() throws InterruptedException { |
720 |
> |
threadsStarted.await(); |
721 |
> |
assertEquals(0, q.remainingCapacity()); |
722 |
> |
assertSame(one, q.take()); |
723 |
> |
}}); |
724 |
|
|
725 |
|
joinPool(executor); |
879 |
– |
|
726 |
|
} |
727 |
|
|
728 |
|
/** |
729 |
< |
* poll retrieves elements across Executor threads |
729 |
> |
* timed poll retrieves elements across Executor threads |
730 |
|
*/ |
731 |
|
public void testPollInExecutor() { |
732 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
733 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
734 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
735 |
< |
executor.execute(new Runnable() { |
736 |
< |
public void run() { |
737 |
< |
threadAssertNull(q.poll()); |
738 |
< |
try { |
739 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
740 |
< |
threadAssertTrue(q.isEmpty()); |
741 |
< |
} |
742 |
< |
catch (InterruptedException e) { |
743 |
< |
threadUnexpectedException(); |
744 |
< |
} |
745 |
< |
} |
746 |
< |
}); |
747 |
< |
|
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 |
< |
}); |
735 |
> |
executor.execute(new CheckedRunnable() { |
736 |
> |
public void realRun() throws InterruptedException { |
737 |
> |
assertNull(q.poll()); |
738 |
> |
threadsStarted.await(); |
739 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
740 |
> |
checkEmpty(q); |
741 |
> |
}}); |
742 |
> |
|
743 |
> |
executor.execute(new CheckedRunnable() { |
744 |
> |
public void realRun() throws InterruptedException { |
745 |
> |
threadsStarted.await(); |
746 |
> |
q.put(one); |
747 |
> |
}}); |
748 |
|
|
749 |
|
joinPool(executor); |
750 |
|
} |
752 |
|
/** |
753 |
|
* A deserialized serialized queue has same elements in same order |
754 |
|
*/ |
755 |
< |
public void testSerialization() { |
756 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
757 |
< |
|
758 |
< |
try { |
759 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
760 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
761 |
< |
out.writeObject(q); |
762 |
< |
out.close(); |
763 |
< |
|
764 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
765 |
< |
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) { |
755 |
> |
public void testSerialization() throws Exception { |
756 |
> |
Queue x = populatedQueue(SIZE); |
757 |
> |
Queue y = serialClone(x); |
758 |
> |
|
759 |
> |
assertTrue(x != y); |
760 |
> |
assertEquals(x.size(), y.size()); |
761 |
> |
assertEquals(x.toString(), y.toString()); |
762 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
763 |
> |
while (!x.isEmpty()) { |
764 |
> |
assertFalse(y.isEmpty()); |
765 |
> |
assertEquals(x.remove(), y.remove()); |
766 |
|
} |
767 |
+ |
assertTrue(y.isEmpty()); |
768 |
|
} |
769 |
|
|
770 |
|
/** |
794 |
|
/** |
795 |
|
* drainTo empties full queue, unblocking a waiting put. |
796 |
|
*/ |
797 |
< |
public void testDrainToWithActivePut() { |
797 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
798 |
|
final ArrayBlockingQueue q = populatedQueue(SIZE); |
799 |
< |
Thread t = new Thread(new Runnable() { |
800 |
< |
public void run() { |
801 |
< |
try { |
802 |
< |
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 |
< |
} |
1014 |
< |
|
1015 |
< |
/** |
1016 |
< |
* drainTo(null, n) throws NPE |
1017 |
< |
*/ |
1018 |
< |
public void testDrainToNullN() { |
1019 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
1020 |
< |
try { |
1021 |
< |
q.drainTo(null, 0); |
1022 |
< |
shouldThrow(); |
1023 |
< |
} catch (NullPointerException success) { |
1024 |
< |
} |
1025 |
< |
} |
799 |
> |
Thread t = new Thread(new CheckedRunnable() { |
800 |
> |
public void realRun() throws InterruptedException { |
801 |
> |
q.put(new Integer(SIZE+1)); |
802 |
> |
}}); |
803 |
|
|
804 |
< |
/** |
805 |
< |
* drainTo(this, n) throws IAE |
806 |
< |
*/ |
807 |
< |
public void testDrainToSelfN() { |
808 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
809 |
< |
try { |
810 |
< |
q.drainTo(q, 0); |
811 |
< |
shouldThrow(); |
1035 |
< |
} catch (IllegalArgumentException success) { |
1036 |
< |
} |
804 |
> |
t.start(); |
805 |
> |
ArrayList l = new ArrayList(); |
806 |
> |
q.drainTo(l); |
807 |
> |
assertTrue(l.size() >= SIZE); |
808 |
> |
for (int i = 0; i < SIZE; ++i) |
809 |
> |
assertEquals(l.get(i), new Integer(i)); |
810 |
> |
t.join(); |
811 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
812 |
|
} |
813 |
|
|
814 |
|
/** |
815 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
815 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
816 |
|
*/ |
817 |
|
public void testDrainToN() { |
818 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2); |
821 |
|
assertTrue(q.offer(new Integer(j))); |
822 |
|
ArrayList l = new ArrayList(); |
823 |
|
q.drainTo(l, i); |
824 |
< |
int k = (i < SIZE)? i : SIZE; |
824 |
> |
int k = (i < SIZE) ? i : SIZE; |
825 |
|
assertEquals(l.size(), k); |
826 |
|
assertEquals(q.size(), SIZE-k); |
827 |
|
for (int j = 0; j < k; ++j) |
830 |
|
} |
831 |
|
} |
832 |
|
|
1058 |
– |
|
833 |
|
} |