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