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