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