1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import junit.framework.*; |
8 |
< |
import java.util.*; |
9 |
< |
import java.util.concurrent.*; |
10 |
< |
import java.io.*; |
8 |
> |
import java.util.Arrays; |
9 |
> |
import java.util.ArrayList; |
10 |
> |
import java.util.Collection; |
11 |
> |
import java.util.Iterator; |
12 |
> |
import java.util.NoSuchElementException; |
13 |
> |
import java.util.Queue; |
14 |
> |
import java.util.concurrent.BlockingDeque; |
15 |
> |
import java.util.concurrent.BlockingQueue; |
16 |
> |
import java.util.concurrent.CountDownLatch; |
17 |
> |
import java.util.concurrent.Executors; |
18 |
> |
import java.util.concurrent.ExecutorService; |
19 |
> |
import java.util.concurrent.LinkedBlockingDeque; |
20 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
21 |
|
|
22 |
|
public class LinkedBlockingDequeTest extends JSR166TestCase { |
23 |
+ |
|
24 |
+ |
public static class Unbounded extends BlockingQueueTest { |
25 |
+ |
protected BlockingQueue emptyCollection() { |
26 |
+ |
return new LinkedBlockingDeque(); |
27 |
+ |
} |
28 |
+ |
} |
29 |
+ |
|
30 |
+ |
public static class Bounded extends BlockingQueueTest { |
31 |
+ |
protected BlockingQueue emptyCollection() { |
32 |
+ |
return new LinkedBlockingDeque(SIZE); |
33 |
+ |
} |
34 |
+ |
} |
35 |
+ |
|
36 |
|
public static void main(String[] args) { |
37 |
< |
junit.textui.TestRunner.run (suite()); |
37 |
> |
junit.textui.TestRunner.run(suite()); |
38 |
|
} |
39 |
|
|
40 |
|
public static Test suite() { |
41 |
< |
return new TestSuite(LinkedBlockingDequeTest.class); |
41 |
> |
return newTestSuite(LinkedBlockingDequeTest.class, |
42 |
> |
new Unbounded().testSuite(), |
43 |
> |
new Bounded().testSuite()); |
44 |
|
} |
45 |
|
|
46 |
|
/** |
47 |
< |
* Create a deque of given size containing consecutive |
47 |
> |
* Returns a new deque of given size containing consecutive |
48 |
|
* Integers 0 ... n. |
49 |
|
*/ |
50 |
< |
private LinkedBlockingDeque populatedDeque(int n) { |
51 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(n); |
50 |
> |
private LinkedBlockingDeque<Integer> populatedDeque(int n) { |
51 |
> |
LinkedBlockingDeque<Integer> q = |
52 |
> |
new LinkedBlockingDeque<Integer>(n); |
53 |
|
assertTrue(q.isEmpty()); |
54 |
< |
for(int i = 0; i < n; i++) |
55 |
< |
assertTrue(q.offer(new Integer(i))); |
54 |
> |
for (int i = 0; i < n; i++) |
55 |
> |
assertTrue(q.offer(new Integer(i))); |
56 |
|
assertFalse(q.isEmpty()); |
57 |
|
assertEquals(0, q.remainingCapacity()); |
58 |
< |
assertEquals(n, q.size()); |
58 |
> |
assertEquals(n, q.size()); |
59 |
|
return q; |
60 |
|
} |
61 |
|
|
89 |
|
} |
90 |
|
|
91 |
|
/** |
92 |
< |
* offer(null) throws NPE |
92 |
> |
* offerFirst(null) throws NullPointerException |
93 |
|
*/ |
94 |
|
public void testOfferFirstNull() { |
95 |
< |
try { |
96 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
95 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
96 |
> |
try { |
97 |
|
q.offerFirst(null); |
98 |
|
shouldThrow(); |
99 |
< |
} catch (NullPointerException success) { |
100 |
< |
} |
99 |
> |
} catch (NullPointerException success) {} |
100 |
> |
} |
101 |
> |
|
102 |
> |
/** |
103 |
> |
* offerLast(null) throws NullPointerException |
104 |
> |
*/ |
105 |
> |
public void testOfferLastNull() { |
106 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
107 |
> |
try { |
108 |
> |
q.offerLast(null); |
109 |
> |
shouldThrow(); |
110 |
> |
} catch (NullPointerException success) {} |
111 |
|
} |
112 |
|
|
113 |
|
/** |
114 |
< |
* OfferFirst succeeds |
114 |
> |
* OfferFirst succeeds |
115 |
|
*/ |
116 |
|
public void testOfferFirst() { |
117 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
120 |
|
} |
121 |
|
|
122 |
|
/** |
123 |
< |
* OfferLast succeeds |
123 |
> |
* OfferLast succeeds |
124 |
|
*/ |
125 |
|
public void testOfferLast() { |
126 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
129 |
|
} |
130 |
|
|
131 |
|
/** |
132 |
< |
* pollFirst succeeds unless empty |
132 |
> |
* pollFirst succeeds unless empty |
133 |
|
*/ |
134 |
|
public void testPollFirst() { |
135 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
136 |
|
for (int i = 0; i < SIZE; ++i) { |
137 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
137 |
> |
assertEquals(i, q.pollFirst()); |
138 |
|
} |
139 |
< |
assertNull(q.pollFirst()); |
139 |
> |
assertNull(q.pollFirst()); |
140 |
|
} |
141 |
|
|
142 |
|
/** |
143 |
< |
* pollLast succeeds unless empty |
143 |
> |
* pollLast succeeds unless empty |
144 |
|
*/ |
145 |
|
public void testPollLast() { |
146 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
147 |
|
for (int i = SIZE-1; i >= 0; --i) { |
148 |
< |
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
148 |
> |
assertEquals(i, q.pollLast()); |
149 |
|
} |
150 |
< |
assertNull(q.pollLast()); |
150 |
> |
assertNull(q.pollLast()); |
151 |
|
} |
152 |
|
|
153 |
|
/** |
154 |
< |
* peekFirst returns next element, or null if empty |
154 |
> |
* peekFirst returns next element, or null if empty |
155 |
|
*/ |
156 |
|
public void testPeekFirst() { |
157 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
158 |
|
for (int i = 0; i < SIZE; ++i) { |
159 |
< |
assertEquals(i, ((Integer)q.peekFirst()).intValue()); |
160 |
< |
q.pollFirst(); |
159 |
> |
assertEquals(i, q.peekFirst()); |
160 |
> |
assertEquals(i, q.pollFirst()); |
161 |
|
assertTrue(q.peekFirst() == null || |
162 |
< |
i != ((Integer)q.peekFirst()).intValue()); |
162 |
> |
!q.peekFirst().equals(i)); |
163 |
|
} |
164 |
< |
assertNull(q.peekFirst()); |
164 |
> |
assertNull(q.peekFirst()); |
165 |
|
} |
166 |
|
|
167 |
|
/** |
168 |
< |
* peek returns next element, or null if empty |
168 |
> |
* peek returns next element, or null if empty |
169 |
|
*/ |
170 |
|
public void testPeek() { |
171 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
172 |
|
for (int i = 0; i < SIZE; ++i) { |
173 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
174 |
< |
q.pollFirst(); |
173 |
> |
assertEquals(i, q.peek()); |
174 |
> |
assertEquals(i, q.pollFirst()); |
175 |
|
assertTrue(q.peek() == null || |
176 |
< |
i != ((Integer)q.peek()).intValue()); |
176 |
> |
!q.peek().equals(i)); |
177 |
|
} |
178 |
< |
assertNull(q.peek()); |
178 |
> |
assertNull(q.peek()); |
179 |
|
} |
180 |
|
|
181 |
|
/** |
182 |
< |
* peekLast returns next element, or null if empty |
182 |
> |
* peekLast returns next element, or null if empty |
183 |
|
*/ |
184 |
|
public void testPeekLast() { |
185 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
186 |
|
for (int i = SIZE-1; i >= 0; --i) { |
187 |
< |
assertEquals(i, ((Integer)q.peekLast()).intValue()); |
188 |
< |
q.pollLast(); |
187 |
> |
assertEquals(i, q.peekLast()); |
188 |
> |
assertEquals(i, q.pollLast()); |
189 |
|
assertTrue(q.peekLast() == null || |
190 |
< |
i != ((Integer)q.peekLast()).intValue()); |
190 |
> |
!q.peekLast().equals(i)); |
191 |
|
} |
192 |
< |
assertNull(q.peekLast()); |
192 |
> |
assertNull(q.peekLast()); |
193 |
|
} |
194 |
|
|
195 |
|
/** |
196 |
< |
* getFirst returns next getFirst, or throws NSEE if empty |
196 |
> |
* getFirst() returns first element, or throws NSEE if empty |
197 |
|
*/ |
198 |
|
public void testFirstElement() { |
199 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
200 |
|
for (int i = 0; i < SIZE; ++i) { |
201 |
< |
assertEquals(i, ((Integer)q.getFirst()).intValue()); |
202 |
< |
q.pollFirst(); |
201 |
> |
assertEquals(i, q.getFirst()); |
202 |
> |
assertEquals(i, q.pollFirst()); |
203 |
|
} |
204 |
|
try { |
205 |
|
q.getFirst(); |
206 |
|
shouldThrow(); |
207 |
< |
} |
208 |
< |
catch (NoSuchElementException success) {} |
207 |
> |
} catch (NoSuchElementException success) {} |
208 |
> |
assertNull(q.peekFirst()); |
209 |
|
} |
210 |
|
|
211 |
|
/** |
212 |
< |
* getLast returns next element, or throws NSEE if empty |
212 |
> |
* getLast() returns last element, or throws NSEE if empty |
213 |
|
*/ |
214 |
|
public void testLastElement() { |
215 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
216 |
|
for (int i = SIZE-1; i >= 0; --i) { |
217 |
< |
assertEquals(i, ((Integer)q.getLast()).intValue()); |
218 |
< |
q.pollLast(); |
217 |
> |
assertEquals(i, q.getLast()); |
218 |
> |
assertEquals(i, q.pollLast()); |
219 |
|
} |
220 |
|
try { |
221 |
|
q.getLast(); |
222 |
|
shouldThrow(); |
223 |
< |
} |
224 |
< |
catch (NoSuchElementException success) {} |
189 |
< |
assertNull(q.peekLast()); |
223 |
> |
} catch (NoSuchElementException success) {} |
224 |
> |
assertNull(q.peekLast()); |
225 |
|
} |
226 |
|
|
227 |
|
/** |
228 |
< |
* removeFirst removes next element, or throws NSEE if empty |
228 |
> |
* removeFirst() removes first element, or throws NSEE if empty |
229 |
|
*/ |
230 |
|
public void testRemoveFirst() { |
231 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
232 |
|
for (int i = 0; i < SIZE; ++i) { |
233 |
< |
assertEquals(i, ((Integer)q.removeFirst()).intValue()); |
233 |
> |
assertEquals(i, q.removeFirst()); |
234 |
|
} |
235 |
|
try { |
236 |
|
q.removeFirst(); |
237 |
|
shouldThrow(); |
238 |
< |
} catch (NoSuchElementException success){ |
239 |
< |
} |
238 |
> |
} catch (NoSuchElementException success) {} |
239 |
> |
assertNull(q.peekFirst()); |
240 |
> |
} |
241 |
> |
|
242 |
> |
/** |
243 |
> |
* removeLast() removes last element, or throws NSEE if empty |
244 |
> |
*/ |
245 |
> |
public void testRemoveLast() { |
246 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
247 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
248 |
> |
assertEquals(i, q.removeLast()); |
249 |
> |
} |
250 |
> |
try { |
251 |
> |
q.removeLast(); |
252 |
> |
shouldThrow(); |
253 |
> |
} catch (NoSuchElementException success) {} |
254 |
> |
assertNull(q.peekLast()); |
255 |
|
} |
256 |
|
|
257 |
|
/** |
258 |
< |
* remove removes next element, or throws NSEE if empty |
258 |
> |
* remove removes next element, or throws NSEE if empty |
259 |
|
*/ |
260 |
|
public void testRemove() { |
261 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
262 |
|
for (int i = 0; i < SIZE; ++i) { |
263 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
263 |
> |
assertEquals(i, q.remove()); |
264 |
|
} |
265 |
|
try { |
266 |
|
q.remove(); |
267 |
|
shouldThrow(); |
268 |
< |
} catch (NoSuchElementException success){ |
219 |
< |
} |
268 |
> |
} catch (NoSuchElementException success) {} |
269 |
|
} |
270 |
|
|
271 |
|
/** |
304 |
|
public void testAddFirst() { |
305 |
|
LinkedBlockingDeque q = populatedDeque(3); |
306 |
|
q.pollLast(); |
307 |
< |
q.addFirst(four); |
308 |
< |
assertEquals(four,q.peekFirst()); |
309 |
< |
} |
307 |
> |
q.addFirst(four); |
308 |
> |
assertSame(four, q.peekFirst()); |
309 |
> |
} |
310 |
|
|
311 |
|
/** |
312 |
|
* peekLast returns element inserted with addLast |
314 |
|
public void testAddLast() { |
315 |
|
LinkedBlockingDeque q = populatedDeque(3); |
316 |
|
q.pollLast(); |
317 |
< |
q.addLast(four); |
318 |
< |
assertEquals(four,q.peekLast()); |
319 |
< |
} |
271 |
< |
|
317 |
> |
q.addLast(four); |
318 |
> |
assertSame(four, q.peekLast()); |
319 |
> |
} |
320 |
|
|
321 |
|
/** |
322 |
|
* A new deque has the indicated capacity, or Integer.MAX_VALUE if |
328 |
|
} |
329 |
|
|
330 |
|
/** |
331 |
< |
* Constructor throws IAE if capacity argument nonpositive |
331 |
> |
* Constructor throws IllegalArgumentException if capacity argument nonpositive |
332 |
|
*/ |
333 |
|
public void testConstructor2() { |
334 |
|
try { |
335 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(0); |
335 |
> |
new LinkedBlockingDeque(0); |
336 |
|
shouldThrow(); |
337 |
< |
} |
290 |
< |
catch (IllegalArgumentException success) {} |
337 |
> |
} catch (IllegalArgumentException success) {} |
338 |
|
} |
339 |
|
|
340 |
|
/** |
341 |
< |
* Initializing from null Collection throws NPE |
341 |
> |
* Initializing from null Collection throws NullPointerException |
342 |
|
*/ |
343 |
|
public void testConstructor3() { |
344 |
|
try { |
345 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(null); |
345 |
> |
new LinkedBlockingDeque(null); |
346 |
|
shouldThrow(); |
347 |
< |
} |
301 |
< |
catch (NullPointerException success) {} |
347 |
> |
} catch (NullPointerException success) {} |
348 |
|
} |
349 |
|
|
350 |
|
/** |
351 |
< |
* Initializing from Collection of null elements throws NPE |
351 |
> |
* Initializing from Collection of null elements throws NullPointerException |
352 |
|
*/ |
353 |
|
public void testConstructor4() { |
354 |
+ |
Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
355 |
|
try { |
356 |
< |
Integer[] ints = new Integer[SIZE]; |
310 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
356 |
> |
new LinkedBlockingDeque(elements); |
357 |
|
shouldThrow(); |
358 |
< |
} |
313 |
< |
catch (NullPointerException success) {} |
358 |
> |
} catch (NullPointerException success) {} |
359 |
|
} |
360 |
|
|
361 |
|
/** |
362 |
< |
* Initializing from Collection with some null elements throws NPE |
362 |
> |
* Initializing from Collection with some null elements throws |
363 |
> |
* NullPointerException |
364 |
|
*/ |
365 |
|
public void testConstructor5() { |
366 |
+ |
Integer[] ints = new Integer[SIZE]; |
367 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
368 |
+ |
ints[i] = i; |
369 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
370 |
|
try { |
371 |
< |
Integer[] ints = new Integer[SIZE]; |
322 |
< |
for (int i = 0; i < SIZE-1; ++i) |
323 |
< |
ints[i] = new Integer(i); |
324 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
371 |
> |
new LinkedBlockingDeque(elements); |
372 |
|
shouldThrow(); |
373 |
< |
} |
327 |
< |
catch (NullPointerException success) {} |
373 |
> |
} catch (NullPointerException success) {} |
374 |
|
} |
375 |
|
|
376 |
|
/** |
377 |
|
* Deque contains all elements of collection used to initialize |
378 |
|
*/ |
379 |
|
public void testConstructor6() { |
380 |
< |
try { |
381 |
< |
Integer[] ints = new Integer[SIZE]; |
382 |
< |
for (int i = 0; i < SIZE; ++i) |
383 |
< |
ints[i] = new Integer(i); |
384 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
385 |
< |
for (int i = 0; i < SIZE; ++i) |
340 |
< |
assertEquals(ints[i], q.poll()); |
341 |
< |
} |
342 |
< |
finally {} |
380 |
> |
Integer[] ints = new Integer[SIZE]; |
381 |
> |
for (int i = 0; i < SIZE; ++i) |
382 |
> |
ints[i] = i; |
383 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
384 |
> |
for (int i = 0; i < SIZE; ++i) |
385 |
> |
assertEquals(ints[i], q.poll()); |
386 |
|
} |
387 |
|
|
388 |
|
/** |
418 |
|
} |
419 |
|
|
420 |
|
/** |
378 |
– |
* offer(null) throws NPE |
379 |
– |
*/ |
380 |
– |
public void testOfferNull() { |
381 |
– |
try { |
382 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
383 |
– |
q.offer(null); |
384 |
– |
shouldThrow(); |
385 |
– |
} catch (NullPointerException success) { } |
386 |
– |
} |
387 |
– |
|
388 |
– |
/** |
389 |
– |
* add(null) throws NPE |
390 |
– |
*/ |
391 |
– |
public void testAddNull() { |
392 |
– |
try { |
393 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
394 |
– |
q.add(null); |
395 |
– |
shouldThrow(); |
396 |
– |
} catch (NullPointerException success) { } |
397 |
– |
} |
398 |
– |
|
399 |
– |
/** |
421 |
|
* push(null) throws NPE |
422 |
|
*/ |
423 |
|
public void testPushNull() { |
424 |
< |
try { |
424 |
> |
try { |
425 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
426 |
|
q.push(null); |
427 |
|
shouldThrow(); |
428 |
< |
} catch (NullPointerException success) { } |
428 |
> |
} catch (NullPointerException success) {} |
429 |
|
} |
430 |
|
|
431 |
|
/** |
432 |
|
* push succeeds if not full; throws ISE if full |
433 |
|
*/ |
434 |
|
public void testPush() { |
435 |
< |
try { |
435 |
> |
try { |
436 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
437 |
|
for (int i = 0; i < SIZE; ++i) { |
438 |
|
Integer I = new Integer(i); |
441 |
|
} |
442 |
|
assertEquals(0, q.remainingCapacity()); |
443 |
|
q.push(new Integer(SIZE)); |
444 |
< |
} catch (IllegalStateException success){ |
445 |
< |
} |
444 |
> |
shouldThrow(); |
445 |
> |
} catch (IllegalStateException success) {} |
446 |
|
} |
447 |
|
|
448 |
|
/** |
451 |
|
public void testPushWithPeek() { |
452 |
|
LinkedBlockingDeque q = populatedDeque(3); |
453 |
|
q.pollLast(); |
454 |
< |
q.push(four); |
455 |
< |
assertEquals(four,q.peekFirst()); |
456 |
< |
} |
436 |
< |
|
454 |
> |
q.push(four); |
455 |
> |
assertSame(four, q.peekFirst()); |
456 |
> |
} |
457 |
|
|
458 |
|
/** |
459 |
< |
* pop removes next element, or throws NSEE if empty |
459 |
> |
* pop removes next element, or throws NSEE if empty |
460 |
|
*/ |
461 |
|
public void testPop() { |
462 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
463 |
|
for (int i = 0; i < SIZE; ++i) { |
464 |
< |
assertEquals(i, ((Integer)q.pop()).intValue()); |
464 |
> |
assertEquals(i, q.pop()); |
465 |
|
} |
466 |
|
try { |
467 |
|
q.pop(); |
468 |
|
shouldThrow(); |
469 |
< |
} catch (NoSuchElementException success){ |
450 |
< |
} |
469 |
> |
} catch (NoSuchElementException success) {} |
470 |
|
} |
471 |
|
|
453 |
– |
|
472 |
|
/** |
473 |
|
* Offer succeeds if not full; fails if full |
474 |
|
*/ |
482 |
|
* add succeeds if not full; throws ISE if full |
483 |
|
*/ |
484 |
|
public void testAdd() { |
485 |
< |
try { |
486 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
487 |
< |
for (int i = 0; i < SIZE; ++i) { |
488 |
< |
assertTrue(q.add(new Integer(i))); |
471 |
< |
} |
472 |
< |
assertEquals(0, q.remainingCapacity()); |
473 |
< |
q.add(new Integer(SIZE)); |
474 |
< |
} catch (IllegalStateException success){ |
475 |
< |
} |
476 |
< |
} |
477 |
< |
|
478 |
< |
/** |
479 |
< |
* addAll(null) throws NPE |
480 |
< |
*/ |
481 |
< |
public void testAddAll1() { |
485 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
486 |
> |
for (int i = 0; i < SIZE; ++i) |
487 |
> |
assertTrue(q.add(new Integer(i))); |
488 |
> |
assertEquals(0, q.remainingCapacity()); |
489 |
|
try { |
490 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
484 |
< |
q.addAll(null); |
490 |
> |
q.add(new Integer(SIZE)); |
491 |
|
shouldThrow(); |
492 |
< |
} |
487 |
< |
catch (NullPointerException success) {} |
492 |
> |
} catch (IllegalStateException success) {} |
493 |
|
} |
494 |
|
|
495 |
|
/** |
496 |
|
* addAll(this) throws IAE |
497 |
|
*/ |
498 |
|
public void testAddAllSelf() { |
499 |
+ |
LinkedBlockingDeque q = populatedDeque(SIZE); |
500 |
|
try { |
495 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
501 |
|
q.addAll(q); |
502 |
|
shouldThrow(); |
503 |
< |
} |
499 |
< |
catch (IllegalArgumentException success) {} |
503 |
> |
} catch (IllegalArgumentException success) {} |
504 |
|
} |
505 |
|
|
506 |
|
/** |
503 |
– |
* addAll of a collection with null elements throws NPE |
504 |
– |
*/ |
505 |
– |
public void testAddAll2() { |
506 |
– |
try { |
507 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
508 |
– |
Integer[] ints = new Integer[SIZE]; |
509 |
– |
q.addAll(Arrays.asList(ints)); |
510 |
– |
shouldThrow(); |
511 |
– |
} |
512 |
– |
catch (NullPointerException success) {} |
513 |
– |
} |
514 |
– |
/** |
507 |
|
* addAll of a collection with any null elements throws NPE after |
508 |
|
* possibly adding some elements |
509 |
|
*/ |
510 |
|
public void testAddAll3() { |
511 |
+ |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
512 |
+ |
Integer[] ints = new Integer[SIZE]; |
513 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
514 |
+ |
ints[i] = new Integer(i); |
515 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
516 |
|
try { |
517 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
521 |
< |
Integer[] ints = new Integer[SIZE]; |
522 |
< |
for (int i = 0; i < SIZE-1; ++i) |
523 |
< |
ints[i] = new Integer(i); |
524 |
< |
q.addAll(Arrays.asList(ints)); |
517 |
> |
q.addAll(elements); |
518 |
|
shouldThrow(); |
519 |
< |
} |
527 |
< |
catch (NullPointerException success) {} |
519 |
> |
} catch (NullPointerException success) {} |
520 |
|
} |
521 |
+ |
|
522 |
|
/** |
523 |
< |
* addAll throws ISE if not enough room |
523 |
> |
* addAll throws IllegalStateException if not enough room |
524 |
|
*/ |
525 |
|
public void testAddAll4() { |
526 |
+ |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE - 1); |
527 |
+ |
Integer[] ints = new Integer[SIZE]; |
528 |
+ |
for (int i = 0; i < SIZE; ++i) |
529 |
+ |
ints[i] = new Integer(i); |
530 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
531 |
|
try { |
532 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
535 |
< |
Integer[] ints = new Integer[SIZE]; |
536 |
< |
for (int i = 0; i < SIZE; ++i) |
537 |
< |
ints[i] = new Integer(i); |
538 |
< |
q.addAll(Arrays.asList(ints)); |
532 |
> |
q.addAll(elements); |
533 |
|
shouldThrow(); |
534 |
< |
} |
541 |
< |
catch (IllegalStateException success) {} |
534 |
> |
} catch (IllegalStateException success) {} |
535 |
|
} |
536 |
+ |
|
537 |
|
/** |
538 |
|
* Deque contains all elements, in traversal order, of successful addAll |
539 |
|
*/ |
540 |
|
public void testAddAll5() { |
541 |
< |
try { |
542 |
< |
Integer[] empty = new Integer[0]; |
543 |
< |
Integer[] ints = new Integer[SIZE]; |
544 |
< |
for (int i = 0; i < SIZE; ++i) |
545 |
< |
ints[i] = new Integer(i); |
546 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
547 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
548 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
549 |
< |
for (int i = 0; i < SIZE; ++i) |
556 |
< |
assertEquals(ints[i], q.poll()); |
557 |
< |
} |
558 |
< |
finally {} |
541 |
> |
Integer[] empty = new Integer[0]; |
542 |
> |
Integer[] ints = new Integer[SIZE]; |
543 |
> |
for (int i = 0; i < SIZE; ++i) |
544 |
> |
ints[i] = new Integer(i); |
545 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
546 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
547 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
548 |
> |
for (int i = 0; i < SIZE; ++i) |
549 |
> |
assertEquals(ints[i], q.poll()); |
550 |
|
} |
551 |
|
|
561 |
– |
|
562 |
– |
/** |
563 |
– |
* put(null) throws NPE |
564 |
– |
*/ |
565 |
– |
public void testPutNull() { |
566 |
– |
try { |
567 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
568 |
– |
q.put(null); |
569 |
– |
shouldThrow(); |
570 |
– |
} |
571 |
– |
catch (NullPointerException success){ |
572 |
– |
} |
573 |
– |
catch (InterruptedException ie) { |
574 |
– |
unexpectedException(); |
575 |
– |
} |
576 |
– |
} |
577 |
– |
|
552 |
|
/** |
553 |
|
* all elements successfully put are contained |
554 |
|
*/ |
555 |
< |
public void testPut() { |
556 |
< |
try { |
557 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
558 |
< |
for (int i = 0; i < SIZE; ++i) { |
559 |
< |
Integer I = new Integer(i); |
560 |
< |
q.put(I); |
587 |
< |
assertTrue(q.contains(I)); |
588 |
< |
} |
589 |
< |
assertEquals(0, q.remainingCapacity()); |
590 |
< |
} |
591 |
< |
catch (InterruptedException ie) { |
592 |
< |
unexpectedException(); |
555 |
> |
public void testPut() throws InterruptedException { |
556 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
557 |
> |
for (int i = 0; i < SIZE; ++i) { |
558 |
> |
Integer I = new Integer(i); |
559 |
> |
q.put(I); |
560 |
> |
assertTrue(q.contains(I)); |
561 |
|
} |
562 |
+ |
assertEquals(0, q.remainingCapacity()); |
563 |
|
} |
564 |
|
|
565 |
|
/** |
566 |
|
* put blocks interruptibly if full |
567 |
|
*/ |
568 |
< |
public void testBlockingPut() { |
569 |
< |
Thread t = new Thread(new Runnable() { |
570 |
< |
public void run() { |
571 |
< |
int added = 0; |
572 |
< |
try { |
573 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
574 |
< |
for (int i = 0; i < SIZE; ++i) { |
575 |
< |
q.put(new Integer(i)); |
576 |
< |
++added; |
577 |
< |
} |
578 |
< |
q.put(new Integer(SIZE)); |
579 |
< |
threadShouldThrow(); |
580 |
< |
} catch (InterruptedException ie){ |
581 |
< |
threadAssertEquals(added, SIZE); |
582 |
< |
} |
583 |
< |
}}); |
584 |
< |
t.start(); |
585 |
< |
try { |
586 |
< |
Thread.sleep(SHORT_DELAY_MS); |
587 |
< |
t.interrupt(); |
588 |
< |
t.join(); |
589 |
< |
} |
590 |
< |
catch (InterruptedException ie) { |
591 |
< |
unexpectedException(); |
592 |
< |
} |
568 |
> |
public void testBlockingPut() throws InterruptedException { |
569 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
570 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
571 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
572 |
> |
public void realRun() throws InterruptedException { |
573 |
> |
for (int i = 0; i < SIZE; ++i) |
574 |
> |
q.put(i); |
575 |
> |
assertEquals(SIZE, q.size()); |
576 |
> |
assertEquals(0, q.remainingCapacity()); |
577 |
> |
|
578 |
> |
Thread.currentThread().interrupt(); |
579 |
> |
try { |
580 |
> |
q.put(99); |
581 |
> |
shouldThrow(); |
582 |
> |
} catch (InterruptedException success) {} |
583 |
> |
assertFalse(Thread.interrupted()); |
584 |
> |
|
585 |
> |
pleaseInterrupt.countDown(); |
586 |
> |
try { |
587 |
> |
q.put(99); |
588 |
> |
shouldThrow(); |
589 |
> |
} catch (InterruptedException success) {} |
590 |
> |
assertFalse(Thread.interrupted()); |
591 |
> |
}}); |
592 |
> |
|
593 |
> |
await(pleaseInterrupt); |
594 |
> |
assertThreadStaysAlive(t); |
595 |
> |
t.interrupt(); |
596 |
> |
awaitTermination(t); |
597 |
> |
assertEquals(SIZE, q.size()); |
598 |
> |
assertEquals(0, q.remainingCapacity()); |
599 |
|
} |
600 |
|
|
601 |
|
/** |
602 |
< |
* put blocks waiting for take when full |
602 |
> |
* put blocks interruptibly waiting for take when full |
603 |
|
*/ |
604 |
< |
public void testPutWithTake() { |
605 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
606 |
< |
Thread t = new Thread(new Runnable() { |
607 |
< |
public void run() { |
608 |
< |
int added = 0; |
609 |
< |
try { |
610 |
< |
q.put(new Object()); |
611 |
< |
++added; |
612 |
< |
q.put(new Object()); |
613 |
< |
++added; |
614 |
< |
q.put(new Object()); |
615 |
< |
++added; |
616 |
< |
q.put(new Object()); |
617 |
< |
++added; |
618 |
< |
threadShouldThrow(); |
619 |
< |
} catch (InterruptedException e){ |
620 |
< |
threadAssertTrue(added >= 2); |
621 |
< |
} |
622 |
< |
} |
623 |
< |
}); |
624 |
< |
try { |
625 |
< |
t.start(); |
626 |
< |
Thread.sleep(SHORT_DELAY_MS); |
627 |
< |
q.take(); |
628 |
< |
t.interrupt(); |
629 |
< |
t.join(); |
630 |
< |
} catch (Exception e){ |
631 |
< |
unexpectedException(); |
632 |
< |
} |
604 |
> |
public void testPutWithTake() throws InterruptedException { |
605 |
> |
final int capacity = 2; |
606 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
607 |
> |
final CountDownLatch pleaseTake = new CountDownLatch(1); |
608 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
609 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
610 |
> |
public void realRun() throws InterruptedException { |
611 |
> |
for (int i = 0; i < capacity; i++) |
612 |
> |
q.put(i); |
613 |
> |
pleaseTake.countDown(); |
614 |
> |
q.put(86); |
615 |
> |
|
616 |
> |
pleaseInterrupt.countDown(); |
617 |
> |
try { |
618 |
> |
q.put(99); |
619 |
> |
shouldThrow(); |
620 |
> |
} catch (InterruptedException success) {} |
621 |
> |
assertFalse(Thread.interrupted()); |
622 |
> |
}}); |
623 |
> |
|
624 |
> |
await(pleaseTake); |
625 |
> |
assertEquals(0, q.remainingCapacity()); |
626 |
> |
assertEquals(0, q.take()); |
627 |
> |
|
628 |
> |
await(pleaseInterrupt); |
629 |
> |
assertThreadStaysAlive(t); |
630 |
> |
t.interrupt(); |
631 |
> |
awaitTermination(t); |
632 |
> |
assertEquals(0, q.remainingCapacity()); |
633 |
|
} |
634 |
|
|
635 |
|
/** |
636 |
|
* timed offer times out if full and elements not taken |
637 |
|
*/ |
638 |
< |
public void testTimedOffer() { |
638 |
> |
public void testTimedOffer() throws InterruptedException { |
639 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
640 |
< |
Thread t = new Thread(new Runnable() { |
641 |
< |
public void run() { |
642 |
< |
try { |
643 |
< |
q.put(new Object()); |
644 |
< |
q.put(new Object()); |
645 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
646 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
647 |
< |
threadShouldThrow(); |
648 |
< |
} catch (InterruptedException success){} |
649 |
< |
} |
650 |
< |
}); |
651 |
< |
|
652 |
< |
try { |
653 |
< |
t.start(); |
654 |
< |
Thread.sleep(SMALL_DELAY_MS); |
655 |
< |
t.interrupt(); |
656 |
< |
t.join(); |
657 |
< |
} catch (Exception e){ |
658 |
< |
unexpectedException(); |
684 |
< |
} |
640 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
641 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
642 |
> |
public void realRun() throws InterruptedException { |
643 |
> |
q.put(new Object()); |
644 |
> |
q.put(new Object()); |
645 |
> |
long startTime = System.nanoTime(); |
646 |
> |
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
647 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
648 |
> |
pleaseInterrupt.countDown(); |
649 |
> |
try { |
650 |
> |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
651 |
> |
shouldThrow(); |
652 |
> |
} catch (InterruptedException success) {} |
653 |
> |
}}); |
654 |
> |
|
655 |
> |
await(pleaseInterrupt); |
656 |
> |
assertThreadStaysAlive(t); |
657 |
> |
t.interrupt(); |
658 |
> |
awaitTermination(t); |
659 |
|
} |
660 |
|
|
661 |
|
/** |
662 |
|
* take retrieves elements in FIFO order |
663 |
|
*/ |
664 |
< |
public void testTake() { |
665 |
< |
try { |
666 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
667 |
< |
for (int i = 0; i < SIZE; ++i) { |
668 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
695 |
< |
} |
696 |
< |
} catch (InterruptedException e){ |
697 |
< |
unexpectedException(); |
698 |
< |
} |
664 |
> |
public void testTake() throws InterruptedException { |
665 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
666 |
> |
for (int i = 0; i < SIZE; ++i) { |
667 |
> |
assertEquals(i, q.take()); |
668 |
> |
} |
669 |
|
} |
670 |
|
|
671 |
|
/** |
672 |
< |
* take blocks interruptibly when empty |
672 |
> |
* take removes existing elements until empty, then blocks interruptibly |
673 |
|
*/ |
674 |
< |
public void testTakeFromEmpty() { |
675 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
676 |
< |
Thread t = new Thread(new Runnable() { |
677 |
< |
public void run() { |
678 |
< |
try { |
679 |
< |
q.take(); |
680 |
< |
threadShouldThrow(); |
711 |
< |
} catch (InterruptedException success){ } |
674 |
> |
public void testBlockingTake() throws InterruptedException { |
675 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
676 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
677 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
678 |
> |
public void realRun() throws InterruptedException { |
679 |
> |
for (int i = 0; i < SIZE; ++i) { |
680 |
> |
assertEquals(i, q.take()); |
681 |
|
} |
713 |
– |
}); |
714 |
– |
try { |
715 |
– |
t.start(); |
716 |
– |
Thread.sleep(SHORT_DELAY_MS); |
717 |
– |
t.interrupt(); |
718 |
– |
t.join(); |
719 |
– |
} catch (Exception e){ |
720 |
– |
unexpectedException(); |
721 |
– |
} |
722 |
– |
} |
682 |
|
|
683 |
< |
/** |
684 |
< |
* Take removes existing elements until empty, then blocks interruptibly |
685 |
< |
*/ |
686 |
< |
public void testBlockingTake() { |
687 |
< |
Thread t = new Thread(new Runnable() { |
688 |
< |
public void run() { |
730 |
< |
try { |
731 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
732 |
< |
for (int i = 0; i < SIZE; ++i) { |
733 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
734 |
< |
} |
735 |
< |
q.take(); |
736 |
< |
threadShouldThrow(); |
737 |
< |
} catch (InterruptedException success){ |
738 |
< |
} |
739 |
< |
}}); |
740 |
< |
t.start(); |
741 |
< |
try { |
742 |
< |
Thread.sleep(SHORT_DELAY_MS); |
743 |
< |
t.interrupt(); |
744 |
< |
t.join(); |
745 |
< |
} |
746 |
< |
catch (InterruptedException ie) { |
747 |
< |
unexpectedException(); |
748 |
< |
} |
749 |
< |
} |
683 |
> |
Thread.currentThread().interrupt(); |
684 |
> |
try { |
685 |
> |
q.take(); |
686 |
> |
shouldThrow(); |
687 |
> |
} catch (InterruptedException success) {} |
688 |
> |
assertFalse(Thread.interrupted()); |
689 |
|
|
690 |
+ |
pleaseInterrupt.countDown(); |
691 |
+ |
try { |
692 |
+ |
q.take(); |
693 |
+ |
shouldThrow(); |
694 |
+ |
} catch (InterruptedException success) {} |
695 |
+ |
assertFalse(Thread.interrupted()); |
696 |
+ |
}}); |
697 |
+ |
|
698 |
+ |
await(pleaseInterrupt); |
699 |
+ |
assertThreadStaysAlive(t); |
700 |
+ |
t.interrupt(); |
701 |
+ |
awaitTermination(t); |
702 |
+ |
} |
703 |
|
|
704 |
|
/** |
705 |
|
* poll succeeds unless empty |
707 |
|
public void testPoll() { |
708 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
709 |
|
for (int i = 0; i < SIZE; ++i) { |
710 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
710 |
> |
assertEquals(i, q.poll()); |
711 |
|
} |
712 |
< |
assertNull(q.poll()); |
712 |
> |
assertNull(q.poll()); |
713 |
|
} |
714 |
|
|
715 |
|
/** |
716 |
|
* timed poll with zero timeout succeeds when non-empty, else times out |
717 |
|
*/ |
718 |
< |
public void testTimedPoll0() { |
719 |
< |
try { |
720 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
721 |
< |
for (int i = 0; i < SIZE; ++i) { |
722 |
< |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
723 |
< |
} |
772 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
773 |
< |
} catch (InterruptedException e){ |
774 |
< |
unexpectedException(); |
775 |
< |
} |
718 |
> |
public void testTimedPoll0() throws InterruptedException { |
719 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
720 |
> |
for (int i = 0; i < SIZE; ++i) { |
721 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
722 |
> |
} |
723 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
724 |
|
} |
725 |
|
|
726 |
|
/** |
727 |
|
* timed poll with nonzero timeout succeeds when non-empty, else times out |
728 |
|
*/ |
729 |
< |
public void testTimedPoll() { |
730 |
< |
try { |
731 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
732 |
< |
for (int i = 0; i < SIZE; ++i) { |
733 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
734 |
< |
} |
735 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
736 |
< |
} catch (InterruptedException e){ |
737 |
< |
unexpectedException(); |
738 |
< |
} |
729 |
> |
public void testTimedPoll() throws InterruptedException { |
730 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
731 |
> |
for (int i = 0; i < SIZE; ++i) { |
732 |
> |
long startTime = System.nanoTime(); |
733 |
> |
assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
734 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
735 |
> |
} |
736 |
> |
long startTime = System.nanoTime(); |
737 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
738 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
739 |
> |
checkEmpty(q); |
740 |
|
} |
741 |
|
|
742 |
|
/** |
743 |
|
* Interrupted timed poll throws InterruptedException instead of |
744 |
|
* returning timeout status |
745 |
|
*/ |
746 |
< |
public void testInterruptedTimedPoll() { |
747 |
< |
Thread t = new Thread(new Runnable() { |
748 |
< |
public void run() { |
749 |
< |
try { |
750 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
751 |
< |
for (int i = 0; i < SIZE; ++i) { |
752 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
753 |
< |
} |
754 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
806 |
< |
} catch (InterruptedException success){ |
807 |
< |
} |
808 |
< |
}}); |
809 |
< |
t.start(); |
810 |
< |
try { |
811 |
< |
Thread.sleep(SHORT_DELAY_MS); |
812 |
< |
t.interrupt(); |
813 |
< |
t.join(); |
814 |
< |
} |
815 |
< |
catch (InterruptedException ie) { |
816 |
< |
unexpectedException(); |
817 |
< |
} |
818 |
< |
} |
819 |
< |
|
820 |
< |
/** |
821 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
822 |
< |
* on interruption throws |
823 |
< |
*/ |
824 |
< |
public void testTimedPollWithOffer() { |
825 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
826 |
< |
Thread t = new Thread(new Runnable() { |
827 |
< |
public void run() { |
828 |
< |
try { |
829 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
830 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
831 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
832 |
< |
threadShouldThrow(); |
833 |
< |
} catch (InterruptedException success) { } |
746 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
747 |
> |
final BlockingQueue<Integer> q = populatedDeque(SIZE); |
748 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
749 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
750 |
> |
public void realRun() throws InterruptedException { |
751 |
> |
for (int i = 0; i < SIZE; ++i) { |
752 |
> |
long t0 = System.nanoTime(); |
753 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
754 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
755 |
|
} |
756 |
< |
}); |
757 |
< |
try { |
758 |
< |
t.start(); |
759 |
< |
Thread.sleep(SMALL_DELAY_MS); |
760 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
761 |
< |
t.interrupt(); |
762 |
< |
t.join(); |
763 |
< |
} catch (Exception e){ |
764 |
< |
unexpectedException(); |
765 |
< |
} |
766 |
< |
} |
767 |
< |
|
756 |
> |
long t0 = System.nanoTime(); |
757 |
> |
aboutToWait.countDown(); |
758 |
> |
try { |
759 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
760 |
> |
shouldThrow(); |
761 |
> |
} catch (InterruptedException success) { |
762 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
763 |
> |
} |
764 |
> |
}}); |
765 |
> |
|
766 |
> |
aboutToWait.await(); |
767 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
768 |
> |
t.interrupt(); |
769 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
770 |
> |
checkEmpty(q); |
771 |
> |
} |
772 |
|
|
773 |
|
/** |
774 |
|
* putFirst(null) throws NPE |
775 |
|
*/ |
776 |
< |
public void testPutFirstNull() { |
777 |
< |
try { |
778 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
776 |
> |
public void testPutFirstNull() throws InterruptedException { |
777 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
778 |
> |
try { |
779 |
|
q.putFirst(null); |
780 |
|
shouldThrow(); |
781 |
< |
} |
782 |
< |
catch (NullPointerException success){ |
858 |
< |
} |
859 |
< |
catch (InterruptedException ie) { |
860 |
< |
unexpectedException(); |
861 |
< |
} |
862 |
< |
} |
781 |
> |
} catch (NullPointerException success) {} |
782 |
> |
} |
783 |
|
|
784 |
|
/** |
785 |
|
* all elements successfully putFirst are contained |
786 |
|
*/ |
787 |
< |
public void testPutFirst() { |
788 |
< |
try { |
789 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
790 |
< |
for (int i = 0; i < SIZE; ++i) { |
791 |
< |
Integer I = new Integer(i); |
792 |
< |
q.putFirst(I); |
873 |
< |
assertTrue(q.contains(I)); |
874 |
< |
} |
875 |
< |
assertEquals(0, q.remainingCapacity()); |
876 |
< |
} |
877 |
< |
catch (InterruptedException ie) { |
878 |
< |
unexpectedException(); |
787 |
> |
public void testPutFirst() throws InterruptedException { |
788 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
789 |
> |
for (int i = 0; i < SIZE; ++i) { |
790 |
> |
Integer I = new Integer(i); |
791 |
> |
q.putFirst(I); |
792 |
> |
assertTrue(q.contains(I)); |
793 |
|
} |
794 |
+ |
assertEquals(0, q.remainingCapacity()); |
795 |
|
} |
796 |
|
|
797 |
|
/** |
798 |
|
* putFirst blocks interruptibly if full |
799 |
|
*/ |
800 |
< |
public void testBlockingPutFirst() { |
801 |
< |
Thread t = new Thread(new Runnable() { |
802 |
< |
public void run() { |
803 |
< |
int added = 0; |
804 |
< |
try { |
805 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
806 |
< |
for (int i = 0; i < SIZE; ++i) { |
807 |
< |
q.putFirst(new Integer(i)); |
808 |
< |
++added; |
809 |
< |
} |
810 |
< |
q.putFirst(new Integer(SIZE)); |
811 |
< |
threadShouldThrow(); |
812 |
< |
} catch (InterruptedException ie){ |
813 |
< |
threadAssertEquals(added, SIZE); |
814 |
< |
} |
815 |
< |
}}); |
816 |
< |
t.start(); |
817 |
< |
try { |
818 |
< |
Thread.sleep(SHORT_DELAY_MS); |
819 |
< |
t.interrupt(); |
820 |
< |
t.join(); |
821 |
< |
} |
822 |
< |
catch (InterruptedException ie) { |
823 |
< |
unexpectedException(); |
824 |
< |
} |
800 |
> |
public void testBlockingPutFirst() throws InterruptedException { |
801 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
802 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
803 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
804 |
> |
public void realRun() throws InterruptedException { |
805 |
> |
for (int i = 0; i < SIZE; ++i) |
806 |
> |
q.putFirst(i); |
807 |
> |
assertEquals(SIZE, q.size()); |
808 |
> |
assertEquals(0, q.remainingCapacity()); |
809 |
> |
|
810 |
> |
Thread.currentThread().interrupt(); |
811 |
> |
try { |
812 |
> |
q.putFirst(99); |
813 |
> |
shouldThrow(); |
814 |
> |
} catch (InterruptedException success) {} |
815 |
> |
assertFalse(Thread.interrupted()); |
816 |
> |
|
817 |
> |
pleaseInterrupt.countDown(); |
818 |
> |
try { |
819 |
> |
q.putFirst(99); |
820 |
> |
shouldThrow(); |
821 |
> |
} catch (InterruptedException success) {} |
822 |
> |
assertFalse(Thread.interrupted()); |
823 |
> |
}}); |
824 |
> |
|
825 |
> |
await(pleaseInterrupt); |
826 |
> |
assertThreadStaysAlive(t); |
827 |
> |
t.interrupt(); |
828 |
> |
awaitTermination(t); |
829 |
> |
assertEquals(SIZE, q.size()); |
830 |
> |
assertEquals(0, q.remainingCapacity()); |
831 |
|
} |
832 |
|
|
833 |
|
/** |
834 |
< |
* putFirst blocks waiting for take when full |
834 |
> |
* putFirst blocks interruptibly waiting for take when full |
835 |
|
*/ |
836 |
< |
public void testPutFirstWithTake() { |
837 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
838 |
< |
Thread t = new Thread(new Runnable() { |
839 |
< |
public void run() { |
840 |
< |
int added = 0; |
841 |
< |
try { |
842 |
< |
q.putFirst(new Object()); |
843 |
< |
++added; |
844 |
< |
q.putFirst(new Object()); |
845 |
< |
++added; |
846 |
< |
q.putFirst(new Object()); |
847 |
< |
++added; |
848 |
< |
q.putFirst(new Object()); |
849 |
< |
++added; |
850 |
< |
threadShouldThrow(); |
851 |
< |
} catch (InterruptedException e){ |
852 |
< |
threadAssertTrue(added >= 2); |
853 |
< |
} |
854 |
< |
} |
855 |
< |
}); |
856 |
< |
try { |
857 |
< |
t.start(); |
858 |
< |
Thread.sleep(SHORT_DELAY_MS); |
859 |
< |
q.take(); |
860 |
< |
t.interrupt(); |
861 |
< |
t.join(); |
862 |
< |
} catch (Exception e){ |
863 |
< |
unexpectedException(); |
864 |
< |
} |
836 |
> |
public void testPutFirstWithTake() throws InterruptedException { |
837 |
> |
final int capacity = 2; |
838 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
839 |
> |
final CountDownLatch pleaseTake = new CountDownLatch(1); |
840 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
841 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
842 |
> |
public void realRun() throws InterruptedException { |
843 |
> |
for (int i = 0; i < capacity; i++) |
844 |
> |
q.putFirst(i); |
845 |
> |
pleaseTake.countDown(); |
846 |
> |
q.putFirst(86); |
847 |
> |
|
848 |
> |
pleaseInterrupt.countDown(); |
849 |
> |
try { |
850 |
> |
q.putFirst(99); |
851 |
> |
shouldThrow(); |
852 |
> |
} catch (InterruptedException success) {} |
853 |
> |
assertFalse(Thread.interrupted()); |
854 |
> |
}}); |
855 |
> |
|
856 |
> |
await(pleaseTake); |
857 |
> |
assertEquals(0, q.remainingCapacity()); |
858 |
> |
assertEquals(capacity - 1, q.take()); |
859 |
> |
|
860 |
> |
await(pleaseInterrupt); |
861 |
> |
assertThreadStaysAlive(t); |
862 |
> |
t.interrupt(); |
863 |
> |
awaitTermination(t); |
864 |
> |
assertEquals(0, q.remainingCapacity()); |
865 |
|
} |
866 |
|
|
867 |
|
/** |
868 |
|
* timed offerFirst times out if full and elements not taken |
869 |
|
*/ |
870 |
< |
public void testTimedOfferFirst() { |
870 |
> |
public void testTimedOfferFirst() throws InterruptedException { |
871 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
872 |
< |
Thread t = new Thread(new Runnable() { |
873 |
< |
public void run() { |
874 |
< |
try { |
875 |
< |
q.putFirst(new Object()); |
876 |
< |
q.putFirst(new Object()); |
877 |
< |
threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
878 |
< |
q.offerFirst(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
879 |
< |
threadShouldThrow(); |
880 |
< |
} catch (InterruptedException success){} |
881 |
< |
} |
882 |
< |
}); |
883 |
< |
|
884 |
< |
try { |
885 |
< |
t.start(); |
886 |
< |
Thread.sleep(SMALL_DELAY_MS); |
887 |
< |
t.interrupt(); |
888 |
< |
t.join(); |
889 |
< |
} catch (Exception e){ |
890 |
< |
unexpectedException(); |
970 |
< |
} |
872 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
873 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
874 |
> |
public void realRun() throws InterruptedException { |
875 |
> |
q.putFirst(new Object()); |
876 |
> |
q.putFirst(new Object()); |
877 |
> |
long startTime = System.nanoTime(); |
878 |
> |
assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS)); |
879 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
880 |
> |
pleaseInterrupt.countDown(); |
881 |
> |
try { |
882 |
> |
q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
883 |
> |
shouldThrow(); |
884 |
> |
} catch (InterruptedException success) {} |
885 |
> |
}}); |
886 |
> |
|
887 |
> |
await(pleaseInterrupt); |
888 |
> |
assertThreadStaysAlive(t); |
889 |
> |
t.interrupt(); |
890 |
> |
awaitTermination(t); |
891 |
|
} |
892 |
|
|
893 |
|
/** |
894 |
|
* take retrieves elements in FIFO order |
895 |
|
*/ |
896 |
< |
public void testTakeFirst() { |
897 |
< |
try { |
898 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
899 |
< |
for (int i = 0; i < SIZE; ++i) { |
900 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
981 |
< |
} |
982 |
< |
} catch (InterruptedException e){ |
983 |
< |
unexpectedException(); |
984 |
< |
} |
896 |
> |
public void testTakeFirst() throws InterruptedException { |
897 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
898 |
> |
for (int i = 0; i < SIZE; ++i) { |
899 |
> |
assertEquals(i, q.takeFirst()); |
900 |
> |
} |
901 |
|
} |
902 |
|
|
903 |
|
/** |
904 |
< |
* takeFirst blocks interruptibly when empty |
904 |
> |
* takeFirst() blocks interruptibly when empty |
905 |
|
*/ |
906 |
< |
public void testTakeFirstFromEmpty() { |
907 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
908 |
< |
Thread t = new Thread(new Runnable() { |
909 |
< |
public void run() { |
910 |
< |
try { |
911 |
< |
q.takeFirst(); |
912 |
< |
threadShouldThrow(); |
913 |
< |
} catch (InterruptedException success){ } |
914 |
< |
} |
915 |
< |
}); |
916 |
< |
try { |
917 |
< |
t.start(); |
918 |
< |
Thread.sleep(SHORT_DELAY_MS); |
919 |
< |
t.interrupt(); |
920 |
< |
t.join(); |
921 |
< |
} catch (Exception e){ |
922 |
< |
unexpectedException(); |
1007 |
< |
} |
906 |
> |
public void testTakeFirstFromEmptyBlocksInterruptibly() { |
907 |
> |
final BlockingDeque q = new LinkedBlockingDeque(); |
908 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
909 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
910 |
> |
public void realRun() { |
911 |
> |
threadStarted.countDown(); |
912 |
> |
try { |
913 |
> |
q.takeFirst(); |
914 |
> |
shouldThrow(); |
915 |
> |
} catch (InterruptedException success) {} |
916 |
> |
assertFalse(Thread.interrupted()); |
917 |
> |
}}); |
918 |
> |
|
919 |
> |
await(threadStarted); |
920 |
> |
assertThreadStaysAlive(t); |
921 |
> |
t.interrupt(); |
922 |
> |
awaitTermination(t); |
923 |
|
} |
924 |
|
|
925 |
|
/** |
926 |
< |
* TakeFirst removes existing elements until empty, then blocks interruptibly |
927 |
< |
*/ |
928 |
< |
public void testBlockingTakeFirst() { |
929 |
< |
Thread t = new Thread(new Runnable() { |
930 |
< |
public void run() { |
931 |
< |
try { |
932 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
933 |
< |
for (int i = 0; i < SIZE; ++i) { |
934 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
935 |
< |
} |
936 |
< |
q.takeFirst(); |
937 |
< |
threadShouldThrow(); |
938 |
< |
} catch (InterruptedException success){ |
939 |
< |
} |
940 |
< |
}}); |
941 |
< |
t.start(); |
942 |
< |
try { |
943 |
< |
Thread.sleep(SHORT_DELAY_MS); |
944 |
< |
t.interrupt(); |
945 |
< |
t.join(); |
946 |
< |
} |
947 |
< |
catch (InterruptedException ie) { |
948 |
< |
unexpectedException(); |
949 |
< |
} |
926 |
> |
* takeFirst() throws InterruptedException immediately if interrupted |
927 |
> |
* before waiting |
928 |
> |
*/ |
929 |
> |
public void testTakeFirstFromEmptyAfterInterrupt() { |
930 |
> |
final BlockingDeque q = new LinkedBlockingDeque(); |
931 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
932 |
> |
public void realRun() { |
933 |
> |
Thread.currentThread().interrupt(); |
934 |
> |
try { |
935 |
> |
q.takeFirst(); |
936 |
> |
shouldThrow(); |
937 |
> |
} catch (InterruptedException success) {} |
938 |
> |
assertFalse(Thread.interrupted()); |
939 |
> |
}}); |
940 |
> |
|
941 |
> |
awaitTermination(t); |
942 |
> |
} |
943 |
> |
|
944 |
> |
/** |
945 |
> |
* takeLast() blocks interruptibly when empty |
946 |
> |
*/ |
947 |
> |
public void testTakeLastFromEmptyBlocksInterruptibly() { |
948 |
> |
final BlockingDeque q = new LinkedBlockingDeque(); |
949 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
950 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
951 |
> |
public void realRun() { |
952 |
> |
threadStarted.countDown(); |
953 |
> |
try { |
954 |
> |
q.takeLast(); |
955 |
> |
shouldThrow(); |
956 |
> |
} catch (InterruptedException success) {} |
957 |
> |
assertFalse(Thread.interrupted()); |
958 |
> |
}}); |
959 |
> |
|
960 |
> |
await(threadStarted); |
961 |
> |
assertThreadStaysAlive(t); |
962 |
> |
t.interrupt(); |
963 |
> |
awaitTermination(t); |
964 |
|
} |
965 |
|
|
966 |
+ |
/** |
967 |
+ |
* takeLast() throws InterruptedException immediately if interrupted |
968 |
+ |
* before waiting |
969 |
+ |
*/ |
970 |
+ |
public void testTakeLastFromEmptyAfterInterrupt() { |
971 |
+ |
final BlockingDeque q = new LinkedBlockingDeque(); |
972 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
973 |
+ |
public void realRun() { |
974 |
+ |
Thread.currentThread().interrupt(); |
975 |
+ |
try { |
976 |
+ |
q.takeLast(); |
977 |
+ |
shouldThrow(); |
978 |
+ |
} catch (InterruptedException success) {} |
979 |
+ |
assertFalse(Thread.interrupted()); |
980 |
+ |
}}); |
981 |
+ |
|
982 |
+ |
awaitTermination(t); |
983 |
+ |
} |
984 |
+ |
|
985 |
+ |
/** |
986 |
+ |
* takeFirst removes existing elements until empty, then blocks interruptibly |
987 |
+ |
*/ |
988 |
+ |
public void testBlockingTakeFirst() throws InterruptedException { |
989 |
+ |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
990 |
+ |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
991 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
992 |
+ |
public void realRun() throws InterruptedException { |
993 |
+ |
for (int i = 0; i < SIZE; ++i) { |
994 |
+ |
assertEquals(i, q.takeFirst()); |
995 |
+ |
} |
996 |
+ |
|
997 |
+ |
Thread.currentThread().interrupt(); |
998 |
+ |
try { |
999 |
+ |
q.takeFirst(); |
1000 |
+ |
shouldThrow(); |
1001 |
+ |
} catch (InterruptedException success) {} |
1002 |
+ |
assertFalse(Thread.interrupted()); |
1003 |
+ |
|
1004 |
+ |
pleaseInterrupt.countDown(); |
1005 |
+ |
try { |
1006 |
+ |
q.takeFirst(); |
1007 |
+ |
shouldThrow(); |
1008 |
+ |
} catch (InterruptedException success) {} |
1009 |
+ |
assertFalse(Thread.interrupted()); |
1010 |
+ |
}}); |
1011 |
+ |
|
1012 |
+ |
await(pleaseInterrupt); |
1013 |
+ |
assertThreadStaysAlive(t); |
1014 |
+ |
t.interrupt(); |
1015 |
+ |
awaitTermination(t); |
1016 |
+ |
} |
1017 |
|
|
1018 |
|
/** |
1019 |
|
* timed pollFirst with zero timeout succeeds when non-empty, else times out |
1020 |
|
*/ |
1021 |
< |
public void testTimedPollFirst0() { |
1022 |
< |
try { |
1023 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1024 |
< |
for (int i = 0; i < SIZE; ++i) { |
1025 |
< |
assertEquals(i, ((Integer)q.pollFirst(0, TimeUnit.MILLISECONDS)).intValue()); |
1026 |
< |
} |
1047 |
< |
assertNull(q.pollFirst(0, TimeUnit.MILLISECONDS)); |
1048 |
< |
} catch (InterruptedException e){ |
1049 |
< |
unexpectedException(); |
1050 |
< |
} |
1021 |
> |
public void testTimedPollFirst0() throws InterruptedException { |
1022 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1023 |
> |
for (int i = 0; i < SIZE; ++i) { |
1024 |
> |
assertEquals(i, q.pollFirst(0, MILLISECONDS)); |
1025 |
> |
} |
1026 |
> |
assertNull(q.pollFirst(0, MILLISECONDS)); |
1027 |
|
} |
1028 |
|
|
1029 |
|
/** |
1030 |
|
* timed pollFirst with nonzero timeout succeeds when non-empty, else times out |
1031 |
|
*/ |
1032 |
< |
public void testTimedPollFirst() { |
1033 |
< |
try { |
1034 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1035 |
< |
for (int i = 0; i < SIZE; ++i) { |
1036 |
< |
assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1037 |
< |
} |
1038 |
< |
assertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1039 |
< |
} catch (InterruptedException e){ |
1040 |
< |
unexpectedException(); |
1041 |
< |
} |
1032 |
> |
public void testTimedPollFirst() throws InterruptedException { |
1033 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1034 |
> |
for (int i = 0; i < SIZE; ++i) { |
1035 |
> |
long startTime = System.nanoTime(); |
1036 |
> |
assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
1037 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1038 |
> |
} |
1039 |
> |
long startTime = System.nanoTime(); |
1040 |
> |
assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS)); |
1041 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1042 |
> |
checkEmpty(q); |
1043 |
|
} |
1044 |
|
|
1045 |
|
/** |
1046 |
|
* Interrupted timed pollFirst throws InterruptedException instead of |
1047 |
|
* returning timeout status |
1048 |
|
*/ |
1049 |
< |
public void testInterruptedTimedPollFirst() { |
1050 |
< |
Thread t = new Thread(new Runnable() { |
1051 |
< |
public void run() { |
1052 |
< |
try { |
1053 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1054 |
< |
for (int i = 0; i < SIZE; ++i) { |
1055 |
< |
threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1056 |
< |
} |
1057 |
< |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1058 |
< |
} catch (InterruptedException success){ |
1059 |
< |
} |
1060 |
< |
}}); |
1061 |
< |
t.start(); |
1062 |
< |
try { |
1063 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1064 |
< |
t.interrupt(); |
1065 |
< |
t.join(); |
1066 |
< |
} |
1067 |
< |
catch (InterruptedException ie) { |
1068 |
< |
unexpectedException(); |
1069 |
< |
} |
1049 |
> |
public void testInterruptedTimedPollFirst() throws InterruptedException { |
1050 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1051 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1052 |
> |
public void realRun() throws InterruptedException { |
1053 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1054 |
> |
for (int i = 0; i < SIZE; ++i) { |
1055 |
> |
assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
1056 |
> |
} |
1057 |
> |
|
1058 |
> |
Thread.currentThread().interrupt(); |
1059 |
> |
try { |
1060 |
> |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
1061 |
> |
shouldThrow(); |
1062 |
> |
} catch (InterruptedException success) {} |
1063 |
> |
assertFalse(Thread.interrupted()); |
1064 |
> |
|
1065 |
> |
pleaseInterrupt.countDown(); |
1066 |
> |
try { |
1067 |
> |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
1068 |
> |
shouldThrow(); |
1069 |
> |
} catch (InterruptedException success) {} |
1070 |
> |
assertFalse(Thread.interrupted()); |
1071 |
> |
}}); |
1072 |
> |
|
1073 |
> |
await(pleaseInterrupt); |
1074 |
> |
assertThreadStaysAlive(t); |
1075 |
> |
t.interrupt(); |
1076 |
> |
awaitTermination(t); |
1077 |
|
} |
1078 |
|
|
1079 |
|
/** |
1080 |
< |
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
1081 |
< |
* on interruption throws |
1080 |
> |
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
1081 |
> |
* on interruption throws |
1082 |
|
*/ |
1083 |
< |
public void testTimedPollFirstWithOfferFirst() { |
1083 |
> |
public void testTimedPollFirstWithOfferFirst() throws InterruptedException { |
1084 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1085 |
< |
Thread t = new Thread(new Runnable() { |
1086 |
< |
public void run() { |
1087 |
< |
try { |
1088 |
< |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1089 |
< |
q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1090 |
< |
q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1091 |
< |
threadShouldThrow(); |
1092 |
< |
} catch (InterruptedException success) { } |
1093 |
< |
} |
1094 |
< |
}); |
1095 |
< |
try { |
1096 |
< |
t.start(); |
1097 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1098 |
< |
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1099 |
< |
t.interrupt(); |
1100 |
< |
t.join(); |
1101 |
< |
} catch (Exception e){ |
1102 |
< |
unexpectedException(); |
1103 |
< |
} |
1104 |
< |
} |
1085 |
> |
final CheckedBarrier barrier = new CheckedBarrier(2); |
1086 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1087 |
> |
public void realRun() throws InterruptedException { |
1088 |
> |
long startTime = System.nanoTime(); |
1089 |
> |
assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS)); |
1090 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1091 |
> |
|
1092 |
> |
barrier.await(); |
1093 |
> |
|
1094 |
> |
assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
1095 |
> |
|
1096 |
> |
Thread.currentThread().interrupt(); |
1097 |
> |
try { |
1098 |
> |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1099 |
> |
shouldThrow(); |
1100 |
> |
} catch (InterruptedException success) {} |
1101 |
> |
|
1102 |
> |
barrier.await(); |
1103 |
> |
try { |
1104 |
> |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1105 |
> |
shouldThrow(); |
1106 |
> |
} catch (InterruptedException success) {} |
1107 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1108 |
> |
}}); |
1109 |
> |
|
1110 |
> |
barrier.await(); |
1111 |
> |
long startTime = System.nanoTime(); |
1112 |
> |
assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS)); |
1113 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1114 |
> |
barrier.await(); |
1115 |
> |
assertThreadStaysAlive(t); |
1116 |
> |
t.interrupt(); |
1117 |
> |
awaitTermination(t); |
1118 |
> |
} |
1119 |
|
|
1120 |
|
/** |
1121 |
|
* putLast(null) throws NPE |
1122 |
|
*/ |
1123 |
< |
public void testPutLastNull() { |
1124 |
< |
try { |
1125 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1123 |
> |
public void testPutLastNull() throws InterruptedException { |
1124 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1125 |
> |
try { |
1126 |
|
q.putLast(null); |
1127 |
|
shouldThrow(); |
1128 |
< |
} |
1129 |
< |
catch (NullPointerException success){ |
1132 |
< |
} |
1133 |
< |
catch (InterruptedException ie) { |
1134 |
< |
unexpectedException(); |
1135 |
< |
} |
1136 |
< |
} |
1128 |
> |
} catch (NullPointerException success) {} |
1129 |
> |
} |
1130 |
|
|
1131 |
|
/** |
1132 |
|
* all elements successfully putLast are contained |
1133 |
|
*/ |
1134 |
< |
public void testPutLast() { |
1135 |
< |
try { |
1136 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1137 |
< |
for (int i = 0; i < SIZE; ++i) { |
1138 |
< |
Integer I = new Integer(i); |
1139 |
< |
q.putLast(I); |
1147 |
< |
assertTrue(q.contains(I)); |
1148 |
< |
} |
1149 |
< |
assertEquals(0, q.remainingCapacity()); |
1150 |
< |
} |
1151 |
< |
catch (InterruptedException ie) { |
1152 |
< |
unexpectedException(); |
1134 |
> |
public void testPutLast() throws InterruptedException { |
1135 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1136 |
> |
for (int i = 0; i < SIZE; ++i) { |
1137 |
> |
Integer I = new Integer(i); |
1138 |
> |
q.putLast(I); |
1139 |
> |
assertTrue(q.contains(I)); |
1140 |
|
} |
1141 |
+ |
assertEquals(0, q.remainingCapacity()); |
1142 |
|
} |
1143 |
|
|
1144 |
|
/** |
1145 |
|
* putLast blocks interruptibly if full |
1146 |
|
*/ |
1147 |
< |
public void testBlockingPutLast() { |
1148 |
< |
Thread t = new Thread(new Runnable() { |
1149 |
< |
public void run() { |
1150 |
< |
int added = 0; |
1151 |
< |
try { |
1152 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1153 |
< |
for (int i = 0; i < SIZE; ++i) { |
1154 |
< |
q.putLast(new Integer(i)); |
1155 |
< |
++added; |
1156 |
< |
} |
1157 |
< |
q.putLast(new Integer(SIZE)); |
1158 |
< |
threadShouldThrow(); |
1159 |
< |
} catch (InterruptedException ie){ |
1160 |
< |
threadAssertEquals(added, SIZE); |
1161 |
< |
} |
1162 |
< |
}}); |
1163 |
< |
t.start(); |
1164 |
< |
try { |
1165 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1166 |
< |
t.interrupt(); |
1167 |
< |
t.join(); |
1168 |
< |
} |
1169 |
< |
catch (InterruptedException ie) { |
1170 |
< |
unexpectedException(); |
1171 |
< |
} |
1147 |
> |
public void testBlockingPutLast() throws InterruptedException { |
1148 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1149 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1150 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1151 |
> |
public void realRun() throws InterruptedException { |
1152 |
> |
for (int i = 0; i < SIZE; ++i) |
1153 |
> |
q.putLast(i); |
1154 |
> |
assertEquals(SIZE, q.size()); |
1155 |
> |
assertEquals(0, q.remainingCapacity()); |
1156 |
> |
|
1157 |
> |
Thread.currentThread().interrupt(); |
1158 |
> |
try { |
1159 |
> |
q.putLast(99); |
1160 |
> |
shouldThrow(); |
1161 |
> |
} catch (InterruptedException success) {} |
1162 |
> |
assertFalse(Thread.interrupted()); |
1163 |
> |
|
1164 |
> |
pleaseInterrupt.countDown(); |
1165 |
> |
try { |
1166 |
> |
q.putLast(99); |
1167 |
> |
shouldThrow(); |
1168 |
> |
} catch (InterruptedException success) {} |
1169 |
> |
assertFalse(Thread.interrupted()); |
1170 |
> |
}}); |
1171 |
> |
|
1172 |
> |
await(pleaseInterrupt); |
1173 |
> |
assertThreadStaysAlive(t); |
1174 |
> |
t.interrupt(); |
1175 |
> |
awaitTermination(t); |
1176 |
> |
assertEquals(SIZE, q.size()); |
1177 |
> |
assertEquals(0, q.remainingCapacity()); |
1178 |
|
} |
1179 |
|
|
1180 |
|
/** |
1181 |
< |
* putLast blocks waiting for take when full |
1181 |
> |
* putLast blocks interruptibly waiting for take when full |
1182 |
|
*/ |
1183 |
< |
public void testPutLastWithTake() { |
1184 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1185 |
< |
Thread t = new Thread(new Runnable() { |
1186 |
< |
public void run() { |
1187 |
< |
int added = 0; |
1188 |
< |
try { |
1189 |
< |
q.putLast(new Object()); |
1190 |
< |
++added; |
1191 |
< |
q.putLast(new Object()); |
1192 |
< |
++added; |
1193 |
< |
q.putLast(new Object()); |
1194 |
< |
++added; |
1195 |
< |
q.putLast(new Object()); |
1196 |
< |
++added; |
1197 |
< |
threadShouldThrow(); |
1198 |
< |
} catch (InterruptedException e){ |
1199 |
< |
threadAssertTrue(added >= 2); |
1200 |
< |
} |
1201 |
< |
} |
1202 |
< |
}); |
1203 |
< |
try { |
1204 |
< |
t.start(); |
1205 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1206 |
< |
q.take(); |
1207 |
< |
t.interrupt(); |
1208 |
< |
t.join(); |
1209 |
< |
} catch (Exception e){ |
1210 |
< |
unexpectedException(); |
1211 |
< |
} |
1183 |
> |
public void testPutLastWithTake() throws InterruptedException { |
1184 |
> |
final int capacity = 2; |
1185 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
1186 |
> |
final CountDownLatch pleaseTake = new CountDownLatch(1); |
1187 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1188 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1189 |
> |
public void realRun() throws InterruptedException { |
1190 |
> |
for (int i = 0; i < capacity; i++) |
1191 |
> |
q.putLast(i); |
1192 |
> |
pleaseTake.countDown(); |
1193 |
> |
q.putLast(86); |
1194 |
> |
|
1195 |
> |
pleaseInterrupt.countDown(); |
1196 |
> |
try { |
1197 |
> |
q.putLast(99); |
1198 |
> |
shouldThrow(); |
1199 |
> |
} catch (InterruptedException success) {} |
1200 |
> |
assertFalse(Thread.interrupted()); |
1201 |
> |
}}); |
1202 |
> |
|
1203 |
> |
await(pleaseTake); |
1204 |
> |
assertEquals(0, q.remainingCapacity()); |
1205 |
> |
assertEquals(0, q.take()); |
1206 |
> |
|
1207 |
> |
await(pleaseInterrupt); |
1208 |
> |
assertThreadStaysAlive(t); |
1209 |
> |
t.interrupt(); |
1210 |
> |
awaitTermination(t); |
1211 |
> |
assertEquals(0, q.remainingCapacity()); |
1212 |
|
} |
1213 |
|
|
1214 |
|
/** |
1215 |
|
* timed offerLast times out if full and elements not taken |
1216 |
|
*/ |
1217 |
< |
public void testTimedOfferLast() { |
1217 |
> |
public void testTimedOfferLast() throws InterruptedException { |
1218 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1219 |
< |
Thread t = new Thread(new Runnable() { |
1220 |
< |
public void run() { |
1221 |
< |
try { |
1222 |
< |
q.putLast(new Object()); |
1223 |
< |
q.putLast(new Object()); |
1224 |
< |
threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1225 |
< |
q.offerLast(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1226 |
< |
threadShouldThrow(); |
1227 |
< |
} catch (InterruptedException success){} |
1228 |
< |
} |
1229 |
< |
}); |
1230 |
< |
|
1231 |
< |
try { |
1232 |
< |
t.start(); |
1233 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1234 |
< |
t.interrupt(); |
1235 |
< |
t.join(); |
1236 |
< |
} catch (Exception e){ |
1237 |
< |
unexpectedException(); |
1244 |
< |
} |
1219 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1220 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1221 |
> |
public void realRun() throws InterruptedException { |
1222 |
> |
q.putLast(new Object()); |
1223 |
> |
q.putLast(new Object()); |
1224 |
> |
long startTime = System.nanoTime(); |
1225 |
> |
assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS)); |
1226 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1227 |
> |
pleaseInterrupt.countDown(); |
1228 |
> |
try { |
1229 |
> |
q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
1230 |
> |
shouldThrow(); |
1231 |
> |
} catch (InterruptedException success) {} |
1232 |
> |
}}); |
1233 |
> |
|
1234 |
> |
await(pleaseInterrupt); |
1235 |
> |
assertThreadStaysAlive(t); |
1236 |
> |
t.interrupt(); |
1237 |
> |
awaitTermination(t); |
1238 |
|
} |
1239 |
|
|
1240 |
|
/** |
1241 |
|
* takeLast retrieves elements in FIFO order |
1242 |
|
*/ |
1243 |
< |
public void testTakeLast() { |
1244 |
< |
try { |
1245 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1246 |
< |
for (int i = 0; i < SIZE; ++i) { |
1247 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1255 |
< |
} |
1256 |
< |
} catch (InterruptedException e){ |
1257 |
< |
unexpectedException(); |
1258 |
< |
} |
1243 |
> |
public void testTakeLast() throws InterruptedException { |
1244 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1245 |
> |
for (int i = 0; i < SIZE; ++i) { |
1246 |
> |
assertEquals(SIZE-i-1, q.takeLast()); |
1247 |
> |
} |
1248 |
|
} |
1249 |
|
|
1250 |
|
/** |
1251 |
< |
* takeLast blocks interruptibly when empty |
1251 |
> |
* takeLast removes existing elements until empty, then blocks interruptibly |
1252 |
|
*/ |
1253 |
< |
public void testTakeLastFromEmpty() { |
1254 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1255 |
< |
Thread t = new Thread(new Runnable() { |
1256 |
< |
public void run() { |
1257 |
< |
try { |
1258 |
< |
q.takeLast(); |
1259 |
< |
threadShouldThrow(); |
1271 |
< |
} catch (InterruptedException success){ } |
1253 |
> |
public void testBlockingTakeLast() throws InterruptedException { |
1254 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1255 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1256 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1257 |
> |
public void realRun() throws InterruptedException { |
1258 |
> |
for (int i = 0; i < SIZE; ++i) { |
1259 |
> |
assertEquals(SIZE-i-1, q.takeLast()); |
1260 |
|
} |
1273 |
– |
}); |
1274 |
– |
try { |
1275 |
– |
t.start(); |
1276 |
– |
Thread.sleep(SHORT_DELAY_MS); |
1277 |
– |
t.interrupt(); |
1278 |
– |
t.join(); |
1279 |
– |
} catch (Exception e){ |
1280 |
– |
unexpectedException(); |
1281 |
– |
} |
1282 |
– |
} |
1261 |
|
|
1262 |
< |
/** |
1263 |
< |
* TakeLast removes existing elements until empty, then blocks interruptibly |
1264 |
< |
*/ |
1265 |
< |
public void testBlockingTakeLast() { |
1266 |
< |
Thread t = new Thread(new Runnable() { |
1267 |
< |
public void run() { |
1290 |
< |
try { |
1291 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1292 |
< |
for (int i = 0; i < SIZE; ++i) { |
1293 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1294 |
< |
} |
1295 |
< |
q.takeLast(); |
1296 |
< |
threadShouldThrow(); |
1297 |
< |
} catch (InterruptedException success){ |
1298 |
< |
} |
1299 |
< |
}}); |
1300 |
< |
t.start(); |
1301 |
< |
try { |
1302 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1303 |
< |
t.interrupt(); |
1304 |
< |
t.join(); |
1305 |
< |
} |
1306 |
< |
catch (InterruptedException ie) { |
1307 |
< |
unexpectedException(); |
1308 |
< |
} |
1309 |
< |
} |
1262 |
> |
Thread.currentThread().interrupt(); |
1263 |
> |
try { |
1264 |
> |
q.takeLast(); |
1265 |
> |
shouldThrow(); |
1266 |
> |
} catch (InterruptedException success) {} |
1267 |
> |
assertFalse(Thread.interrupted()); |
1268 |
|
|
1269 |
+ |
pleaseInterrupt.countDown(); |
1270 |
+ |
try { |
1271 |
+ |
q.takeLast(); |
1272 |
+ |
shouldThrow(); |
1273 |
+ |
} catch (InterruptedException success) {} |
1274 |
+ |
assertFalse(Thread.interrupted()); |
1275 |
+ |
}}); |
1276 |
+ |
|
1277 |
+ |
await(pleaseInterrupt); |
1278 |
+ |
assertThreadStaysAlive(t); |
1279 |
+ |
t.interrupt(); |
1280 |
+ |
awaitTermination(t); |
1281 |
+ |
} |
1282 |
|
|
1283 |
|
/** |
1284 |
|
* timed pollLast with zero timeout succeeds when non-empty, else times out |
1285 |
|
*/ |
1286 |
< |
public void testTimedPollLast0() { |
1287 |
< |
try { |
1288 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1289 |
< |
for (int i = 0; i < SIZE; ++i) { |
1290 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, TimeUnit.MILLISECONDS)).intValue()); |
1291 |
< |
} |
1321 |
< |
assertNull(q.pollLast(0, TimeUnit.MILLISECONDS)); |
1322 |
< |
} catch (InterruptedException e){ |
1323 |
< |
unexpectedException(); |
1324 |
< |
} |
1286 |
> |
public void testTimedPollLast0() throws InterruptedException { |
1287 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1288 |
> |
for (int i = 0; i < SIZE; ++i) { |
1289 |
> |
assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS)); |
1290 |
> |
} |
1291 |
> |
assertNull(q.pollLast(0, MILLISECONDS)); |
1292 |
|
} |
1293 |
|
|
1294 |
|
/** |
1295 |
|
* timed pollLast with nonzero timeout succeeds when non-empty, else times out |
1296 |
|
*/ |
1297 |
< |
public void testTimedPollLast() { |
1298 |
< |
try { |
1299 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1300 |
< |
for (int i = 0; i < SIZE; ++i) { |
1301 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1302 |
< |
} |
1303 |
< |
assertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1304 |
< |
} catch (InterruptedException e){ |
1305 |
< |
unexpectedException(); |
1306 |
< |
} |
1297 |
> |
public void testTimedPollLast() throws InterruptedException { |
1298 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1299 |
> |
for (int i = 0; i < SIZE; ++i) { |
1300 |
> |
long startTime = System.nanoTime(); |
1301 |
> |
assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1302 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1303 |
> |
} |
1304 |
> |
long startTime = System.nanoTime(); |
1305 |
> |
assertNull(q.pollLast(timeoutMillis(), MILLISECONDS)); |
1306 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1307 |
> |
checkEmpty(q); |
1308 |
|
} |
1309 |
|
|
1310 |
|
/** |
1311 |
|
* Interrupted timed pollLast throws InterruptedException instead of |
1312 |
|
* returning timeout status |
1313 |
|
*/ |
1314 |
< |
public void testInterruptedTimedPollLast() { |
1315 |
< |
Thread t = new Thread(new Runnable() { |
1316 |
< |
public void run() { |
1317 |
< |
try { |
1318 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1319 |
< |
for (int i = 0; i < SIZE; ++i) { |
1320 |
< |
threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1321 |
< |
} |
1322 |
< |
threadAssertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1323 |
< |
} catch (InterruptedException success){ |
1324 |
< |
} |
1325 |
< |
}}); |
1326 |
< |
t.start(); |
1327 |
< |
try { |
1328 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1329 |
< |
t.interrupt(); |
1330 |
< |
t.join(); |
1331 |
< |
} |
1332 |
< |
catch (InterruptedException ie) { |
1333 |
< |
unexpectedException(); |
1334 |
< |
} |
1314 |
> |
public void testInterruptedTimedPollLast() throws InterruptedException { |
1315 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1316 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1317 |
> |
public void realRun() throws InterruptedException { |
1318 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1319 |
> |
for (int i = 0; i < SIZE; ++i) { |
1320 |
> |
assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1321 |
> |
} |
1322 |
> |
|
1323 |
> |
Thread.currentThread().interrupt(); |
1324 |
> |
try { |
1325 |
> |
q.pollLast(LONG_DELAY_MS, MILLISECONDS); |
1326 |
> |
shouldThrow(); |
1327 |
> |
} catch (InterruptedException success) {} |
1328 |
> |
assertFalse(Thread.interrupted()); |
1329 |
> |
|
1330 |
> |
pleaseInterrupt.countDown(); |
1331 |
> |
try { |
1332 |
> |
q.pollLast(LONG_DELAY_MS, MILLISECONDS); |
1333 |
> |
shouldThrow(); |
1334 |
> |
} catch (InterruptedException success) {} |
1335 |
> |
assertFalse(Thread.interrupted()); |
1336 |
> |
}}); |
1337 |
> |
|
1338 |
> |
await(pleaseInterrupt); |
1339 |
> |
assertThreadStaysAlive(t); |
1340 |
> |
t.interrupt(); |
1341 |
> |
awaitTermination(t); |
1342 |
|
} |
1343 |
|
|
1344 |
|
/** |
1345 |
< |
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1346 |
< |
* on interruption throws |
1345 |
> |
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1346 |
> |
* on interruption throws |
1347 |
|
*/ |
1348 |
< |
public void testTimedPollWithOfferLast() { |
1348 |
> |
public void testTimedPollWithOfferLast() throws InterruptedException { |
1349 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1350 |
< |
Thread t = new Thread(new Runnable() { |
1351 |
< |
public void run() { |
1352 |
< |
try { |
1353 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1354 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1355 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1356 |
< |
threadShouldThrow(); |
1357 |
< |
} catch (InterruptedException success) { } |
1383 |
< |
} |
1384 |
< |
}); |
1385 |
< |
try { |
1386 |
< |
t.start(); |
1387 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1388 |
< |
assertTrue(q.offerLast(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1389 |
< |
t.interrupt(); |
1390 |
< |
t.join(); |
1391 |
< |
} catch (Exception e){ |
1392 |
< |
unexpectedException(); |
1393 |
< |
} |
1394 |
< |
} |
1350 |
> |
final CheckedBarrier barrier = new CheckedBarrier(2); |
1351 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1352 |
> |
public void realRun() throws InterruptedException { |
1353 |
> |
long startTime = System.nanoTime(); |
1354 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
1355 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1356 |
> |
|
1357 |
> |
barrier.await(); |
1358 |
|
|
1359 |
+ |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1360 |
+ |
|
1361 |
+ |
Thread.currentThread().interrupt(); |
1362 |
+ |
try { |
1363 |
+ |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1364 |
+ |
shouldThrow(); |
1365 |
+ |
} catch (InterruptedException success) {} |
1366 |
+ |
assertFalse(Thread.interrupted()); |
1367 |
+ |
|
1368 |
+ |
barrier.await(); |
1369 |
+ |
try { |
1370 |
+ |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1371 |
+ |
shouldThrow(); |
1372 |
+ |
} catch (InterruptedException success) {} |
1373 |
+ |
assertFalse(Thread.interrupted()); |
1374 |
+ |
}}); |
1375 |
+ |
|
1376 |
+ |
barrier.await(); |
1377 |
+ |
long startTime = System.nanoTime(); |
1378 |
+ |
assertTrue(q.offerLast(zero, LONG_DELAY_MS, MILLISECONDS)); |
1379 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1380 |
+ |
|
1381 |
+ |
barrier.await(); |
1382 |
+ |
assertThreadStaysAlive(t); |
1383 |
+ |
t.interrupt(); |
1384 |
+ |
awaitTermination(t); |
1385 |
+ |
} |
1386 |
|
|
1387 |
|
/** |
1388 |
|
* element returns next element, or throws NSEE if empty |
1390 |
|
public void testElement() { |
1391 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1392 |
|
for (int i = 0; i < SIZE; ++i) { |
1393 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
1393 |
> |
assertEquals(i, q.element()); |
1394 |
|
q.poll(); |
1395 |
|
} |
1396 |
|
try { |
1397 |
|
q.element(); |
1398 |
|
shouldThrow(); |
1399 |
< |
} |
1410 |
< |
catch (NoSuchElementException success) {} |
1399 |
> |
} catch (NoSuchElementException success) {} |
1400 |
|
} |
1401 |
|
|
1402 |
|
/** |
1414 |
– |
* remove(x) removes x and returns true if present |
1415 |
– |
*/ |
1416 |
– |
public void testRemoveElement() { |
1417 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1418 |
– |
for (int i = 1; i < SIZE; i+=2) { |
1419 |
– |
assertTrue(q.remove(new Integer(i))); |
1420 |
– |
} |
1421 |
– |
for (int i = 0; i < SIZE; i+=2) { |
1422 |
– |
assertTrue(q.remove(new Integer(i))); |
1423 |
– |
assertFalse(q.remove(new Integer(i+1))); |
1424 |
– |
} |
1425 |
– |
assertTrue(q.isEmpty()); |
1426 |
– |
} |
1427 |
– |
|
1428 |
– |
/** |
1403 |
|
* contains(x) reports true when elements added but not yet removed |
1404 |
|
*/ |
1405 |
|
public void testContains() { |
1477 |
|
} |
1478 |
|
|
1479 |
|
/** |
1480 |
< |
* toArray contains all elements |
1480 |
> |
* toArray contains all elements in FIFO order |
1481 |
|
*/ |
1482 |
< |
public void testToArray() { |
1482 |
> |
public void testToArray() throws InterruptedException { |
1483 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1484 |
< |
Object[] o = q.toArray(); |
1485 |
< |
try { |
1486 |
< |
for(int i = 0; i < o.length; i++) |
1513 |
< |
assertEquals(o[i], q.take()); |
1514 |
< |
} catch (InterruptedException e){ |
1515 |
< |
unexpectedException(); |
1516 |
< |
} |
1484 |
> |
Object[] o = q.toArray(); |
1485 |
> |
for (int i = 0; i < o.length; i++) |
1486 |
> |
assertSame(o[i], q.poll()); |
1487 |
|
} |
1488 |
|
|
1489 |
|
/** |
1490 |
< |
* toArray(a) contains all elements |
1490 |
> |
* toArray(a) contains all elements in FIFO order |
1491 |
|
*/ |
1492 |
|
public void testToArray2() { |
1493 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1494 |
< |
Integer[] ints = new Integer[SIZE]; |
1495 |
< |
ints = (Integer[])q.toArray(ints); |
1496 |
< |
try { |
1497 |
< |
for(int i = 0; i < ints.length; i++) |
1498 |
< |
assertEquals(ints[i], q.take()); |
1529 |
< |
} catch (InterruptedException e){ |
1530 |
< |
unexpectedException(); |
1531 |
< |
} |
1532 |
< |
} |
1533 |
< |
|
1534 |
< |
/** |
1535 |
< |
* toArray(null) throws NPE |
1536 |
< |
*/ |
1537 |
< |
public void testToArray_BadArg() { |
1538 |
< |
try { |
1539 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1540 |
< |
Object o[] = q.toArray(null); |
1541 |
< |
shouldThrow(); |
1542 |
< |
} catch(NullPointerException success){} |
1493 |
> |
LinkedBlockingDeque<Integer> q = populatedDeque(SIZE); |
1494 |
> |
Integer[] ints = new Integer[SIZE]; |
1495 |
> |
Integer[] array = q.toArray(ints); |
1496 |
> |
assertSame(ints, array); |
1497 |
> |
for (int i = 0; i < ints.length; i++) |
1498 |
> |
assertSame(ints[i], q.remove()); |
1499 |
|
} |
1500 |
|
|
1501 |
|
/** |
1502 |
< |
* toArray with incompatible array type throws CCE |
1502 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
1503 |
|
*/ |
1504 |
|
public void testToArray1_BadArg() { |
1505 |
< |
try { |
1506 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1507 |
< |
Object o[] = q.toArray(new String[10] ); |
1508 |
< |
shouldThrow(); |
1509 |
< |
} catch(ArrayStoreException success){} |
1505 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1506 |
> |
try { |
1507 |
> |
q.toArray(new String[10]); |
1508 |
> |
shouldThrow(); |
1509 |
> |
} catch (ArrayStoreException success) {} |
1510 |
|
} |
1511 |
|
|
1556 |
– |
|
1512 |
|
/** |
1513 |
|
* iterator iterates through all elements |
1514 |
|
*/ |
1515 |
< |
public void testIterator() { |
1515 |
> |
public void testIterator() throws InterruptedException { |
1516 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1517 |
< |
Iterator it = q.iterator(); |
1518 |
< |
try { |
1519 |
< |
while(it.hasNext()){ |
1520 |
< |
assertEquals(it.next(), q.take()); |
1566 |
< |
} |
1567 |
< |
} catch (InterruptedException e){ |
1568 |
< |
unexpectedException(); |
1569 |
< |
} |
1517 |
> |
Iterator it = q.iterator(); |
1518 |
> |
while (it.hasNext()) { |
1519 |
> |
assertEquals(it.next(), q.take()); |
1520 |
> |
} |
1521 |
|
} |
1522 |
|
|
1523 |
|
/** |
1524 |
|
* iterator.remove removes current element |
1525 |
|
*/ |
1526 |
< |
public void testIteratorRemove () { |
1526 |
> |
public void testIteratorRemove() { |
1527 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
1528 |
|
q.add(two); |
1529 |
|
q.add(one); |
1532 |
|
Iterator it = q.iterator(); |
1533 |
|
it.next(); |
1534 |
|
it.remove(); |
1535 |
< |
|
1535 |
> |
|
1536 |
|
it = q.iterator(); |
1537 |
< |
assertEquals(it.next(), one); |
1538 |
< |
assertEquals(it.next(), three); |
1537 |
> |
assertSame(it.next(), one); |
1538 |
> |
assertSame(it.next(), three); |
1539 |
|
assertFalse(it.hasNext()); |
1540 |
|
} |
1541 |
|
|
1591 |
– |
|
1542 |
|
/** |
1543 |
|
* iterator ordering is FIFO |
1544 |
|
*/ |
1550 |
|
assertEquals(0, q.remainingCapacity()); |
1551 |
|
int k = 0; |
1552 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
1553 |
< |
int i = ((Integer)(it.next())).intValue(); |
1604 |
< |
assertEquals(++k, i); |
1553 |
> |
assertEquals(++k, it.next()); |
1554 |
|
} |
1555 |
|
assertEquals(3, k); |
1556 |
|
} |
1558 |
|
/** |
1559 |
|
* Modifications do not cause iterators to fail |
1560 |
|
*/ |
1561 |
< |
public void testWeaklyConsistentIteration () { |
1561 |
> |
public void testWeaklyConsistentIteration() { |
1562 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
1563 |
|
q.add(one); |
1564 |
|
q.add(two); |
1565 |
|
q.add(three); |
1566 |
+ |
for (Iterator it = q.iterator(); it.hasNext();) { |
1567 |
+ |
q.remove(); |
1568 |
+ |
it.next(); |
1569 |
+ |
} |
1570 |
+ |
assertEquals(0, q.size()); |
1571 |
+ |
} |
1572 |
+ |
|
1573 |
+ |
/** |
1574 |
+ |
* Descending iterator iterates through all elements |
1575 |
+ |
*/ |
1576 |
+ |
public void testDescendingIterator() { |
1577 |
+ |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1578 |
+ |
int i = 0; |
1579 |
+ |
Iterator it = q.descendingIterator(); |
1580 |
+ |
while (it.hasNext()) { |
1581 |
+ |
assertTrue(q.contains(it.next())); |
1582 |
+ |
++i; |
1583 |
+ |
} |
1584 |
+ |
assertEquals(i, SIZE); |
1585 |
+ |
assertFalse(it.hasNext()); |
1586 |
|
try { |
1587 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
1588 |
< |
q.remove(); |
1589 |
< |
it.next(); |
1587 |
> |
it.next(); |
1588 |
> |
shouldThrow(); |
1589 |
> |
} catch (NoSuchElementException success) {} |
1590 |
> |
} |
1591 |
> |
|
1592 |
> |
/** |
1593 |
> |
* Descending iterator ordering is reverse FIFO |
1594 |
> |
*/ |
1595 |
> |
public void testDescendingIteratorOrdering() { |
1596 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1597 |
> |
for (int iters = 0; iters < 100; ++iters) { |
1598 |
> |
q.add(new Integer(3)); |
1599 |
> |
q.add(new Integer(2)); |
1600 |
> |
q.add(new Integer(1)); |
1601 |
> |
int k = 0; |
1602 |
> |
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
1603 |
> |
assertEquals(++k, it.next()); |
1604 |
|
} |
1605 |
+ |
|
1606 |
+ |
assertEquals(3, k); |
1607 |
+ |
q.remove(); |
1608 |
+ |
q.remove(); |
1609 |
+ |
q.remove(); |
1610 |
|
} |
1623 |
– |
catch (ConcurrentModificationException e) { |
1624 |
– |
unexpectedException(); |
1625 |
– |
} |
1626 |
– |
assertEquals(0, q.size()); |
1611 |
|
} |
1612 |
|
|
1613 |
+ |
/** |
1614 |
+ |
* descendingIterator.remove removes current element |
1615 |
+ |
*/ |
1616 |
+ |
public void testDescendingIteratorRemove() { |
1617 |
+ |
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1618 |
+ |
for (int iters = 0; iters < 100; ++iters) { |
1619 |
+ |
q.add(new Integer(3)); |
1620 |
+ |
q.add(new Integer(2)); |
1621 |
+ |
q.add(new Integer(1)); |
1622 |
+ |
Iterator it = q.descendingIterator(); |
1623 |
+ |
assertEquals(it.next(), new Integer(1)); |
1624 |
+ |
it.remove(); |
1625 |
+ |
assertEquals(it.next(), new Integer(2)); |
1626 |
+ |
it = q.descendingIterator(); |
1627 |
+ |
assertEquals(it.next(), new Integer(2)); |
1628 |
+ |
assertEquals(it.next(), new Integer(3)); |
1629 |
+ |
it.remove(); |
1630 |
+ |
assertFalse(it.hasNext()); |
1631 |
+ |
q.remove(); |
1632 |
+ |
} |
1633 |
+ |
} |
1634 |
|
|
1635 |
|
/** |
1636 |
|
* toString contains toStrings of elements |
1639 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1640 |
|
String s = q.toString(); |
1641 |
|
for (int i = 0; i < SIZE; ++i) { |
1642 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
1642 |
> |
assertTrue(s.contains(String.valueOf(i))); |
1643 |
|
} |
1644 |
< |
} |
1640 |
< |
|
1644 |
> |
} |
1645 |
|
|
1646 |
|
/** |
1647 |
|
* offer transfers elements across Executor tasks |
1651 |
|
q.add(one); |
1652 |
|
q.add(two); |
1653 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1654 |
< |
executor.execute(new Runnable() { |
1655 |
< |
public void run() { |
1656 |
< |
threadAssertFalse(q.offer(three)); |
1657 |
< |
try { |
1658 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
1659 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
1660 |
< |
} |
1661 |
< |
catch (InterruptedException e) { |
1662 |
< |
threadUnexpectedException(); |
1663 |
< |
} |
1664 |
< |
} |
1665 |
< |
}); |
1654 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1655 |
> |
executor.execute(new CheckedRunnable() { |
1656 |
> |
public void realRun() throws InterruptedException { |
1657 |
> |
assertFalse(q.offer(three)); |
1658 |
> |
threadsStarted.await(); |
1659 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
1660 |
> |
assertEquals(0, q.remainingCapacity()); |
1661 |
> |
}}); |
1662 |
> |
|
1663 |
> |
executor.execute(new CheckedRunnable() { |
1664 |
> |
public void realRun() throws InterruptedException { |
1665 |
> |
threadsStarted.await(); |
1666 |
> |
assertSame(one, q.take()); |
1667 |
> |
}}); |
1668 |
|
|
1663 |
– |
executor.execute(new Runnable() { |
1664 |
– |
public void run() { |
1665 |
– |
try { |
1666 |
– |
Thread.sleep(SMALL_DELAY_MS); |
1667 |
– |
threadAssertEquals(one, q.take()); |
1668 |
– |
} |
1669 |
– |
catch (InterruptedException e) { |
1670 |
– |
threadUnexpectedException(); |
1671 |
– |
} |
1672 |
– |
} |
1673 |
– |
}); |
1674 |
– |
|
1669 |
|
joinPool(executor); |
1670 |
|
} |
1671 |
|
|
1672 |
|
/** |
1673 |
< |
* poll retrieves elements across Executor threads |
1673 |
> |
* timed poll retrieves elements across Executor threads |
1674 |
|
*/ |
1675 |
|
public void testPollInExecutor() { |
1676 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1677 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1678 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1679 |
< |
executor.execute(new Runnable() { |
1680 |
< |
public void run() { |
1681 |
< |
threadAssertNull(q.poll()); |
1682 |
< |
try { |
1683 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
1684 |
< |
threadAssertTrue(q.isEmpty()); |
1685 |
< |
} |
1686 |
< |
catch (InterruptedException e) { |
1687 |
< |
threadUnexpectedException(); |
1688 |
< |
} |
1689 |
< |
} |
1690 |
< |
}); |
1679 |
> |
executor.execute(new CheckedRunnable() { |
1680 |
> |
public void realRun() throws InterruptedException { |
1681 |
> |
assertNull(q.poll()); |
1682 |
> |
threadsStarted.await(); |
1683 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1684 |
> |
checkEmpty(q); |
1685 |
> |
}}); |
1686 |
> |
|
1687 |
> |
executor.execute(new CheckedRunnable() { |
1688 |
> |
public void realRun() throws InterruptedException { |
1689 |
> |
threadsStarted.await(); |
1690 |
> |
q.put(one); |
1691 |
> |
}}); |
1692 |
|
|
1697 |
– |
executor.execute(new Runnable() { |
1698 |
– |
public void run() { |
1699 |
– |
try { |
1700 |
– |
Thread.sleep(SMALL_DELAY_MS); |
1701 |
– |
q.put(one); |
1702 |
– |
} |
1703 |
– |
catch (InterruptedException e) { |
1704 |
– |
threadUnexpectedException(); |
1705 |
– |
} |
1706 |
– |
} |
1707 |
– |
}); |
1708 |
– |
|
1693 |
|
joinPool(executor); |
1694 |
|
} |
1695 |
|
|
1696 |
|
/** |
1697 |
|
* A deserialized serialized deque has same elements in same order |
1698 |
|
*/ |
1699 |
< |
public void testSerialization() { |
1700 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1701 |
< |
|
1702 |
< |
try { |
1703 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1704 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1705 |
< |
out.writeObject(q); |
1706 |
< |
out.close(); |
1707 |
< |
|
1708 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1709 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1726 |
< |
LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject(); |
1727 |
< |
assertEquals(q.size(), r.size()); |
1728 |
< |
while (!q.isEmpty()) |
1729 |
< |
assertEquals(q.remove(), r.remove()); |
1730 |
< |
} catch(Exception e){ |
1731 |
< |
unexpectedException(); |
1732 |
< |
} |
1733 |
< |
} |
1734 |
< |
|
1735 |
< |
/** |
1736 |
< |
* drainTo(null) throws NPE |
1737 |
< |
*/ |
1738 |
< |
public void testDrainToNull() { |
1739 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1740 |
< |
try { |
1741 |
< |
q.drainTo(null); |
1742 |
< |
shouldThrow(); |
1743 |
< |
} catch(NullPointerException success) { |
1744 |
< |
} |
1745 |
< |
} |
1746 |
< |
|
1747 |
< |
/** |
1748 |
< |
* drainTo(this) throws IAE |
1749 |
< |
*/ |
1750 |
< |
public void testDrainToSelf() { |
1751 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1752 |
< |
try { |
1753 |
< |
q.drainTo(q); |
1754 |
< |
shouldThrow(); |
1755 |
< |
} catch(IllegalArgumentException success) { |
1699 |
> |
public void testSerialization() throws Exception { |
1700 |
> |
Queue x = populatedDeque(SIZE); |
1701 |
> |
Queue y = serialClone(x); |
1702 |
> |
|
1703 |
> |
assertTrue(x != y); |
1704 |
> |
assertEquals(x.size(), y.size()); |
1705 |
> |
assertEquals(x.toString(), y.toString()); |
1706 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
1707 |
> |
while (!x.isEmpty()) { |
1708 |
> |
assertFalse(y.isEmpty()); |
1709 |
> |
assertEquals(x.remove(), y.remove()); |
1710 |
|
} |
1711 |
+ |
assertTrue(y.isEmpty()); |
1712 |
|
} |
1713 |
|
|
1714 |
|
/** |
1715 |
|
* drainTo(c) empties deque into another collection c |
1716 |
< |
*/ |
1716 |
> |
*/ |
1717 |
|
public void testDrainTo() { |
1718 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1719 |
|
ArrayList l = new ArrayList(); |
1720 |
|
q.drainTo(l); |
1721 |
< |
assertEquals(q.size(), 0); |
1722 |
< |
assertEquals(l.size(), SIZE); |
1723 |
< |
for (int i = 0; i < SIZE; ++i) |
1721 |
> |
assertEquals(0, q.size()); |
1722 |
> |
assertEquals(SIZE, l.size()); |
1723 |
> |
for (int i = 0; i < SIZE; ++i) |
1724 |
|
assertEquals(l.get(i), new Integer(i)); |
1725 |
|
q.add(zero); |
1726 |
|
q.add(one); |
1729 |
|
assertTrue(q.contains(one)); |
1730 |
|
l.clear(); |
1731 |
|
q.drainTo(l); |
1732 |
< |
assertEquals(q.size(), 0); |
1733 |
< |
assertEquals(l.size(), 2); |
1734 |
< |
for (int i = 0; i < 2; ++i) |
1732 |
> |
assertEquals(0, q.size()); |
1733 |
> |
assertEquals(2, l.size()); |
1734 |
> |
for (int i = 0; i < 2; ++i) |
1735 |
|
assertEquals(l.get(i), new Integer(i)); |
1736 |
|
} |
1737 |
|
|
1738 |
|
/** |
1739 |
|
* drainTo empties full deque, unblocking a waiting put. |
1740 |
< |
*/ |
1741 |
< |
public void testDrainToWithActivePut() { |
1740 |
> |
*/ |
1741 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
1742 |
|
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1743 |
< |
Thread t = new Thread(new Runnable() { |
1744 |
< |
public void run() { |
1745 |
< |
try { |
1746 |
< |
q.put(new Integer(SIZE+1)); |
1792 |
< |
} catch (InterruptedException ie){ |
1793 |
< |
threadUnexpectedException(); |
1794 |
< |
} |
1795 |
< |
} |
1796 |
< |
}); |
1797 |
< |
try { |
1798 |
< |
t.start(); |
1799 |
< |
ArrayList l = new ArrayList(); |
1800 |
< |
q.drainTo(l); |
1801 |
< |
assertTrue(l.size() >= SIZE); |
1802 |
< |
for (int i = 0; i < SIZE; ++i) |
1803 |
< |
assertEquals(l.get(i), new Integer(i)); |
1804 |
< |
t.join(); |
1805 |
< |
assertTrue(q.size() + l.size() >= SIZE); |
1806 |
< |
} catch(Exception e){ |
1807 |
< |
unexpectedException(); |
1808 |
< |
} |
1809 |
< |
} |
1743 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1744 |
> |
public void realRun() throws InterruptedException { |
1745 |
> |
q.put(new Integer(SIZE+1)); |
1746 |
> |
}}); |
1747 |
|
|
1748 |
< |
/** |
1749 |
< |
* drainTo(null, n) throws NPE |
1750 |
< |
*/ |
1751 |
< |
public void testDrainToNullN() { |
1752 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1753 |
< |
try { |
1754 |
< |
q.drainTo(null, 0); |
1755 |
< |
shouldThrow(); |
1819 |
< |
} catch(NullPointerException success) { |
1820 |
< |
} |
1821 |
< |
} |
1822 |
< |
|
1823 |
< |
/** |
1824 |
< |
* drainTo(this, n) throws IAE |
1825 |
< |
*/ |
1826 |
< |
public void testDrainToSelfN() { |
1827 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1828 |
< |
try { |
1829 |
< |
q.drainTo(q, 0); |
1830 |
< |
shouldThrow(); |
1831 |
< |
} catch(IllegalArgumentException success) { |
1832 |
< |
} |
1748 |
> |
t.start(); |
1749 |
> |
ArrayList l = new ArrayList(); |
1750 |
> |
q.drainTo(l); |
1751 |
> |
assertTrue(l.size() >= SIZE); |
1752 |
> |
for (int i = 0; i < SIZE; ++i) |
1753 |
> |
assertEquals(l.get(i), new Integer(i)); |
1754 |
> |
t.join(); |
1755 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
1756 |
|
} |
1757 |
|
|
1758 |
|
/** |
1759 |
< |
* drainTo(c, n) empties first max {n, size} elements of deque into c |
1760 |
< |
*/ |
1759 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
1760 |
> |
*/ |
1761 |
|
public void testDrainToN() { |
1762 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1763 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
1764 |
< |
for(int j = 0; j < SIZE; j++) |
1764 |
> |
for (int j = 0; j < SIZE; j++) |
1765 |
|
assertTrue(q.offer(new Integer(j))); |
1766 |
|
ArrayList l = new ArrayList(); |
1767 |
|
q.drainTo(l, i); |
1768 |
< |
int k = (i < SIZE)? i : SIZE; |
1769 |
< |
assertEquals(l.size(), k); |
1770 |
< |
assertEquals(q.size(), SIZE-k); |
1771 |
< |
for (int j = 0; j < k; ++j) |
1768 |
> |
int k = (i < SIZE) ? i : SIZE; |
1769 |
> |
assertEquals(k, l.size()); |
1770 |
> |
assertEquals(SIZE-k, q.size()); |
1771 |
> |
for (int j = 0; j < k; ++j) |
1772 |
|
assertEquals(l.get(j), new Integer(j)); |
1773 |
|
while (q.poll() != null) ; |
1774 |
|
} |