7 |
|
import junit.framework.*; |
8 |
|
import java.util.*; |
9 |
|
import java.util.concurrent.*; |
10 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
11 |
|
import java.io.*; |
12 |
|
|
13 |
|
public class LinkedBlockingDequeTest extends JSR166TestCase { |
14 |
+ |
|
15 |
+ |
public static class Unbounded extends BlockingQueueTest { |
16 |
+ |
protected BlockingQueue emptyCollection() { |
17 |
+ |
return new LinkedBlockingDeque(); |
18 |
+ |
} |
19 |
+ |
} |
20 |
+ |
|
21 |
+ |
public static class Bounded extends BlockingQueueTest { |
22 |
+ |
protected BlockingQueue emptyCollection() { |
23 |
+ |
return new LinkedBlockingDeque(20); |
24 |
+ |
} |
25 |
+ |
} |
26 |
+ |
|
27 |
|
public static void main(String[] args) { |
28 |
< |
junit.textui.TestRunner.run (suite()); |
28 |
> |
junit.textui.TestRunner.run(suite()); |
29 |
|
} |
30 |
|
|
31 |
|
public static Test suite() { |
32 |
< |
return new TestSuite(LinkedBlockingDequeTest.class); |
32 |
> |
return newTestSuite(LinkedBlockingDequeTest.class, |
33 |
> |
new Unbounded().testSuite(), |
34 |
> |
new Bounded().testSuite()); |
35 |
|
} |
36 |
|
|
37 |
|
/** |
41 |
|
private LinkedBlockingDeque populatedDeque(int n) { |
42 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(n); |
43 |
|
assertTrue(q.isEmpty()); |
44 |
< |
for(int i = 0; i < n; i++) |
45 |
< |
assertTrue(q.offer(new Integer(i))); |
44 |
> |
for (int i = 0; i < n; i++) |
45 |
> |
assertTrue(q.offer(new Integer(i))); |
46 |
|
assertFalse(q.isEmpty()); |
47 |
|
assertEquals(0, q.remainingCapacity()); |
48 |
< |
assertEquals(n, q.size()); |
48 |
> |
assertEquals(n, q.size()); |
49 |
|
return q; |
50 |
|
} |
51 |
|
|
82 |
|
* offer(null) throws NPE |
83 |
|
*/ |
84 |
|
public void testOfferFirstNull() { |
85 |
< |
try { |
85 |
> |
try { |
86 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
87 |
|
q.offerFirst(null); |
88 |
|
shouldThrow(); |
89 |
< |
} catch (NullPointerException success) { |
74 |
< |
} |
89 |
> |
} catch (NullPointerException success) {} |
90 |
|
} |
91 |
|
|
92 |
|
/** |
93 |
< |
* OfferFirst succeeds |
93 |
> |
* OfferFirst succeeds |
94 |
|
*/ |
95 |
|
public void testOfferFirst() { |
96 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
99 |
|
} |
100 |
|
|
101 |
|
/** |
102 |
< |
* OfferLast succeeds |
102 |
> |
* OfferLast succeeds |
103 |
|
*/ |
104 |
|
public void testOfferLast() { |
105 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
108 |
|
} |
109 |
|
|
110 |
|
/** |
111 |
< |
* pollFirst succeeds unless empty |
111 |
> |
* pollFirst succeeds unless empty |
112 |
|
*/ |
113 |
|
public void testPollFirst() { |
114 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
115 |
|
for (int i = 0; i < SIZE; ++i) { |
116 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
116 |
> |
assertEquals(i, q.pollFirst()); |
117 |
|
} |
118 |
< |
assertNull(q.pollFirst()); |
118 |
> |
assertNull(q.pollFirst()); |
119 |
|
} |
120 |
|
|
121 |
|
/** |
122 |
< |
* pollLast succeeds unless empty |
122 |
> |
* pollLast succeeds unless empty |
123 |
|
*/ |
124 |
|
public void testPollLast() { |
125 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
126 |
|
for (int i = SIZE-1; i >= 0; --i) { |
127 |
< |
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
127 |
> |
assertEquals(i, q.pollLast()); |
128 |
|
} |
129 |
< |
assertNull(q.pollLast()); |
129 |
> |
assertNull(q.pollLast()); |
130 |
|
} |
131 |
|
|
132 |
|
/** |
133 |
< |
* peekFirst returns next element, or null if empty |
133 |
> |
* peekFirst returns next element, or null if empty |
134 |
|
*/ |
135 |
|
public void testPeekFirst() { |
136 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
137 |
|
for (int i = 0; i < SIZE; ++i) { |
138 |
< |
assertEquals(i, ((Integer)q.peekFirst()).intValue()); |
139 |
< |
q.pollFirst(); |
138 |
> |
assertEquals(i, q.peekFirst()); |
139 |
> |
assertEquals(i, q.pollFirst()); |
140 |
|
assertTrue(q.peekFirst() == null || |
141 |
< |
i != ((Integer)q.peekFirst()).intValue()); |
141 |
> |
!q.peekFirst().equals(i)); |
142 |
|
} |
143 |
< |
assertNull(q.peekFirst()); |
143 |
> |
assertNull(q.peekFirst()); |
144 |
|
} |
145 |
|
|
146 |
|
/** |
147 |
< |
* peek returns next element, or null if empty |
147 |
> |
* peek returns next element, or null if empty |
148 |
|
*/ |
149 |
|
public void testPeek() { |
150 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
151 |
|
for (int i = 0; i < SIZE; ++i) { |
152 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
153 |
< |
q.pollFirst(); |
152 |
> |
assertEquals(i, q.peek()); |
153 |
> |
assertEquals(i, q.pollFirst()); |
154 |
|
assertTrue(q.peek() == null || |
155 |
< |
i != ((Integer)q.peek()).intValue()); |
155 |
> |
!q.peek().equals(i)); |
156 |
|
} |
157 |
< |
assertNull(q.peek()); |
157 |
> |
assertNull(q.peek()); |
158 |
|
} |
159 |
|
|
160 |
|
/** |
161 |
< |
* peekLast returns next element, or null if empty |
161 |
> |
* peekLast returns next element, or null if empty |
162 |
|
*/ |
163 |
|
public void testPeekLast() { |
164 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
165 |
|
for (int i = SIZE-1; i >= 0; --i) { |
166 |
< |
assertEquals(i, ((Integer)q.peekLast()).intValue()); |
167 |
< |
q.pollLast(); |
166 |
> |
assertEquals(i, q.peekLast()); |
167 |
> |
assertEquals(i, q.pollLast()); |
168 |
|
assertTrue(q.peekLast() == null || |
169 |
< |
i != ((Integer)q.peekLast()).intValue()); |
169 |
> |
!q.peekLast().equals(i)); |
170 |
|
} |
171 |
< |
assertNull(q.peekLast()); |
171 |
> |
assertNull(q.peekLast()); |
172 |
|
} |
173 |
|
|
174 |
|
/** |
175 |
< |
* getFirst returns next getFirst, or throws NSEE if empty |
175 |
> |
* getFirst() returns first element, or throws NSEE if empty |
176 |
|
*/ |
177 |
|
public void testFirstElement() { |
178 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
179 |
|
for (int i = 0; i < SIZE; ++i) { |
180 |
< |
assertEquals(i, ((Integer)q.getFirst()).intValue()); |
181 |
< |
q.pollFirst(); |
180 |
> |
assertEquals(i, q.getFirst()); |
181 |
> |
assertEquals(i, q.pollFirst()); |
182 |
|
} |
183 |
|
try { |
184 |
|
q.getFirst(); |
185 |
|
shouldThrow(); |
186 |
< |
} |
187 |
< |
catch (NoSuchElementException success) {} |
186 |
> |
} catch (NoSuchElementException success) {} |
187 |
> |
assertNull(q.peekFirst()); |
188 |
|
} |
189 |
|
|
190 |
|
/** |
191 |
< |
* getLast returns next element, or throws NSEE if empty |
191 |
> |
* getLast() returns last element, or throws NSEE if empty |
192 |
|
*/ |
193 |
|
public void testLastElement() { |
194 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
195 |
|
for (int i = SIZE-1; i >= 0; --i) { |
196 |
< |
assertEquals(i, ((Integer)q.getLast()).intValue()); |
197 |
< |
q.pollLast(); |
196 |
> |
assertEquals(i, q.getLast()); |
197 |
> |
assertEquals(i, q.pollLast()); |
198 |
|
} |
199 |
|
try { |
200 |
|
q.getLast(); |
201 |
|
shouldThrow(); |
202 |
< |
} |
203 |
< |
catch (NoSuchElementException success) {} |
189 |
< |
assertNull(q.peekLast()); |
202 |
> |
} catch (NoSuchElementException success) {} |
203 |
> |
assertNull(q.peekLast()); |
204 |
|
} |
205 |
|
|
206 |
|
/** |
207 |
< |
* removeFirst removes next element, or throws NSEE if empty |
207 |
> |
* removeFirst() removes first element, or throws NSEE if empty |
208 |
|
*/ |
209 |
|
public void testRemoveFirst() { |
210 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
211 |
|
for (int i = 0; i < SIZE; ++i) { |
212 |
< |
assertEquals(i, ((Integer)q.removeFirst()).intValue()); |
212 |
> |
assertEquals(i, q.removeFirst()); |
213 |
|
} |
214 |
|
try { |
215 |
|
q.removeFirst(); |
216 |
|
shouldThrow(); |
217 |
< |
} catch (NoSuchElementException success){ |
218 |
< |
} |
217 |
> |
} catch (NoSuchElementException success) {} |
218 |
> |
assertNull(q.peekFirst()); |
219 |
|
} |
220 |
|
|
221 |
|
/** |
222 |
< |
* remove removes next element, or throws NSEE if empty |
222 |
> |
* removeLast() removes last element, or throws NSEE if empty |
223 |
> |
*/ |
224 |
> |
public void testRemoveLast() { |
225 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
226 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
227 |
> |
assertEquals(i, q.removeLast()); |
228 |
> |
} |
229 |
> |
try { |
230 |
> |
q.removeLast(); |
231 |
> |
shouldThrow(); |
232 |
> |
} catch (NoSuchElementException success) {} |
233 |
> |
assertNull(q.peekLast()); |
234 |
> |
} |
235 |
> |
|
236 |
> |
/** |
237 |
> |
* remove removes next element, or throws NSEE if empty |
238 |
|
*/ |
239 |
|
public void testRemove() { |
240 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
241 |
|
for (int i = 0; i < SIZE; ++i) { |
242 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
242 |
> |
assertEquals(i, q.remove()); |
243 |
|
} |
244 |
|
try { |
245 |
|
q.remove(); |
246 |
|
shouldThrow(); |
247 |
< |
} catch (NoSuchElementException success){ |
219 |
< |
} |
247 |
> |
} catch (NoSuchElementException success) {} |
248 |
|
} |
249 |
|
|
250 |
|
/** |
283 |
|
public void testAddFirst() { |
284 |
|
LinkedBlockingDeque q = populatedDeque(3); |
285 |
|
q.pollLast(); |
286 |
< |
q.addFirst(four); |
287 |
< |
assertEquals(four,q.peekFirst()); |
288 |
< |
} |
286 |
> |
q.addFirst(four); |
287 |
> |
assertSame(four, q.peekFirst()); |
288 |
> |
} |
289 |
|
|
290 |
|
/** |
291 |
|
* peekLast returns element inserted with addLast |
293 |
|
public void testAddLast() { |
294 |
|
LinkedBlockingDeque q = populatedDeque(3); |
295 |
|
q.pollLast(); |
296 |
< |
q.addLast(four); |
297 |
< |
assertEquals(four,q.peekLast()); |
298 |
< |
} |
296 |
> |
q.addLast(four); |
297 |
> |
assertSame(four, q.peekLast()); |
298 |
> |
} |
299 |
|
|
300 |
|
|
301 |
|
/** |
308 |
|
} |
309 |
|
|
310 |
|
/** |
311 |
< |
* Constructor throws IAE if capacity argument nonpositive |
311 |
> |
* Constructor throws IAE if capacity argument nonpositive |
312 |
|
*/ |
313 |
|
public void testConstructor2() { |
314 |
|
try { |
315 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(0); |
316 |
|
shouldThrow(); |
317 |
< |
} |
290 |
< |
catch (IllegalArgumentException success) {} |
317 |
> |
} catch (IllegalArgumentException success) {} |
318 |
|
} |
319 |
|
|
320 |
|
/** |
324 |
|
try { |
325 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(null); |
326 |
|
shouldThrow(); |
327 |
< |
} |
301 |
< |
catch (NullPointerException success) {} |
327 |
> |
} catch (NullPointerException success) {} |
328 |
|
} |
329 |
|
|
330 |
|
/** |
335 |
|
Integer[] ints = new Integer[SIZE]; |
336 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
337 |
|
shouldThrow(); |
338 |
< |
} |
313 |
< |
catch (NullPointerException success) {} |
338 |
> |
} catch (NullPointerException success) {} |
339 |
|
} |
340 |
|
|
341 |
|
/** |
348 |
|
ints[i] = new Integer(i); |
349 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
350 |
|
shouldThrow(); |
351 |
< |
} |
327 |
< |
catch (NullPointerException success) {} |
351 |
> |
} catch (NullPointerException success) {} |
352 |
|
} |
353 |
|
|
354 |
|
/** |
355 |
|
* Deque contains all elements of collection used to initialize |
356 |
|
*/ |
357 |
|
public void testConstructor6() { |
358 |
< |
try { |
359 |
< |
Integer[] ints = new Integer[SIZE]; |
360 |
< |
for (int i = 0; i < SIZE; ++i) |
361 |
< |
ints[i] = new Integer(i); |
362 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
363 |
< |
for (int i = 0; i < SIZE; ++i) |
340 |
< |
assertEquals(ints[i], q.poll()); |
341 |
< |
} |
342 |
< |
finally {} |
358 |
> |
Integer[] ints = new Integer[SIZE]; |
359 |
> |
for (int i = 0; i < SIZE; ++i) |
360 |
> |
ints[i] = new Integer(i); |
361 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
362 |
> |
for (int i = 0; i < SIZE; ++i) |
363 |
> |
assertEquals(ints[i], q.poll()); |
364 |
|
} |
365 |
|
|
366 |
|
/** |
399 |
|
* offer(null) throws NPE |
400 |
|
*/ |
401 |
|
public void testOfferNull() { |
402 |
< |
try { |
402 |
> |
try { |
403 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
404 |
|
q.offer(null); |
405 |
|
shouldThrow(); |
406 |
< |
} catch (NullPointerException success) { } |
406 |
> |
} catch (NullPointerException success) {} |
407 |
|
} |
408 |
|
|
409 |
|
/** |
410 |
|
* add(null) throws NPE |
411 |
|
*/ |
412 |
|
public void testAddNull() { |
413 |
< |
try { |
413 |
> |
try { |
414 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
415 |
|
q.add(null); |
416 |
|
shouldThrow(); |
417 |
< |
} catch (NullPointerException success) { } |
417 |
> |
} catch (NullPointerException success) {} |
418 |
|
} |
419 |
|
|
420 |
|
/** |
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 |
< |
} |
454 |
> |
q.push(four); |
455 |
> |
assertSame(four, q.peekFirst()); |
456 |
> |
} |
457 |
|
|
458 |
|
|
459 |
|
/** |
460 |
< |
* pop removes next element, or throws NSEE if empty |
460 |
> |
* pop removes next element, or throws NSEE if empty |
461 |
|
*/ |
462 |
|
public void testPop() { |
463 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
464 |
|
for (int i = 0; i < SIZE; ++i) { |
465 |
< |
assertEquals(i, ((Integer)q.pop()).intValue()); |
465 |
> |
assertEquals(i, q.pop()); |
466 |
|
} |
467 |
|
try { |
468 |
|
q.pop(); |
469 |
|
shouldThrow(); |
470 |
< |
} catch (NoSuchElementException success){ |
450 |
< |
} |
470 |
> |
} catch (NoSuchElementException success) {} |
471 |
|
} |
472 |
|
|
473 |
|
|
484 |
|
* add succeeds if not full; throws ISE if full |
485 |
|
*/ |
486 |
|
public void testAdd() { |
487 |
< |
try { |
487 |
> |
try { |
488 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
489 |
|
for (int i = 0; i < SIZE; ++i) { |
490 |
|
assertTrue(q.add(new Integer(i))); |
491 |
|
} |
492 |
|
assertEquals(0, q.remainingCapacity()); |
493 |
|
q.add(new Integer(SIZE)); |
494 |
< |
} catch (IllegalStateException success){ |
495 |
< |
} |
494 |
> |
shouldThrow(); |
495 |
> |
} catch (IllegalStateException success) {} |
496 |
|
} |
497 |
|
|
498 |
|
/** |
503 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
504 |
|
q.addAll(null); |
505 |
|
shouldThrow(); |
506 |
< |
} |
487 |
< |
catch (NullPointerException success) {} |
506 |
> |
} catch (NullPointerException success) {} |
507 |
|
} |
508 |
|
|
509 |
|
/** |
514 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
515 |
|
q.addAll(q); |
516 |
|
shouldThrow(); |
517 |
< |
} |
499 |
< |
catch (IllegalArgumentException success) {} |
517 |
> |
} catch (IllegalArgumentException success) {} |
518 |
|
} |
519 |
|
|
520 |
|
/** |
526 |
|
Integer[] ints = new Integer[SIZE]; |
527 |
|
q.addAll(Arrays.asList(ints)); |
528 |
|
shouldThrow(); |
529 |
< |
} |
512 |
< |
catch (NullPointerException success) {} |
529 |
> |
} catch (NullPointerException success) {} |
530 |
|
} |
531 |
+ |
|
532 |
|
/** |
533 |
|
* addAll of a collection with any null elements throws NPE after |
534 |
|
* possibly adding some elements |
541 |
|
ints[i] = new Integer(i); |
542 |
|
q.addAll(Arrays.asList(ints)); |
543 |
|
shouldThrow(); |
544 |
< |
} |
527 |
< |
catch (NullPointerException success) {} |
544 |
> |
} catch (NullPointerException success) {} |
545 |
|
} |
546 |
+ |
|
547 |
|
/** |
548 |
|
* addAll throws ISE if not enough room |
549 |
|
*/ |
555 |
|
ints[i] = new Integer(i); |
556 |
|
q.addAll(Arrays.asList(ints)); |
557 |
|
shouldThrow(); |
558 |
< |
} |
541 |
< |
catch (IllegalStateException success) {} |
558 |
> |
} catch (IllegalStateException success) {} |
559 |
|
} |
560 |
+ |
|
561 |
|
/** |
562 |
|
* Deque contains all elements, in traversal order, of successful addAll |
563 |
|
*/ |
564 |
|
public void testAddAll5() { |
565 |
< |
try { |
566 |
< |
Integer[] empty = new Integer[0]; |
567 |
< |
Integer[] ints = new Integer[SIZE]; |
568 |
< |
for (int i = 0; i < SIZE; ++i) |
569 |
< |
ints[i] = new Integer(i); |
570 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
571 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
572 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
573 |
< |
for (int i = 0; i < SIZE; ++i) |
556 |
< |
assertEquals(ints[i], q.poll()); |
557 |
< |
} |
558 |
< |
finally {} |
565 |
> |
Integer[] empty = new Integer[0]; |
566 |
> |
Integer[] ints = new Integer[SIZE]; |
567 |
> |
for (int i = 0; i < SIZE; ++i) |
568 |
> |
ints[i] = new Integer(i); |
569 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
570 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
571 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
572 |
> |
for (int i = 0; i < SIZE; ++i) |
573 |
> |
assertEquals(ints[i], q.poll()); |
574 |
|
} |
575 |
|
|
576 |
|
|
577 |
|
/** |
578 |
|
* put(null) throws NPE |
579 |
|
*/ |
580 |
< |
public void testPutNull() { |
581 |
< |
try { |
580 |
> |
public void testPutNull() throws InterruptedException { |
581 |
> |
try { |
582 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
583 |
|
q.put(null); |
584 |
|
shouldThrow(); |
585 |
< |
} |
586 |
< |
catch (NullPointerException success){ |
572 |
< |
} |
573 |
< |
catch (InterruptedException ie) { |
574 |
< |
unexpectedException(); |
575 |
< |
} |
576 |
< |
} |
585 |
> |
} catch (NullPointerException success) {} |
586 |
> |
} |
587 |
|
|
588 |
|
/** |
589 |
|
* all elements successfully put are contained |
590 |
|
*/ |
591 |
< |
public void testPut() { |
592 |
< |
try { |
593 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
594 |
< |
for (int i = 0; i < SIZE; ++i) { |
595 |
< |
Integer I = new Integer(i); |
596 |
< |
q.put(I); |
587 |
< |
assertTrue(q.contains(I)); |
588 |
< |
} |
589 |
< |
assertEquals(0, q.remainingCapacity()); |
590 |
< |
} |
591 |
< |
catch (InterruptedException ie) { |
592 |
< |
unexpectedException(); |
591 |
> |
public void testPut() throws InterruptedException { |
592 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
593 |
> |
for (int i = 0; i < SIZE; ++i) { |
594 |
> |
Integer I = new Integer(i); |
595 |
> |
q.put(I); |
596 |
> |
assertTrue(q.contains(I)); |
597 |
|
} |
598 |
+ |
assertEquals(0, q.remainingCapacity()); |
599 |
|
} |
600 |
|
|
601 |
|
/** |
602 |
|
* put blocks interruptibly if full |
603 |
|
*/ |
604 |
< |
public void testBlockingPut() { |
605 |
< |
Thread t = new Thread(new Runnable() { |
606 |
< |
public void run() { |
607 |
< |
int added = 0; |
608 |
< |
try { |
609 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
610 |
< |
for (int i = 0; i < SIZE; ++i) { |
611 |
< |
q.put(new Integer(i)); |
612 |
< |
++added; |
613 |
< |
} |
614 |
< |
q.put(new Integer(SIZE)); |
615 |
< |
threadShouldThrow(); |
616 |
< |
} catch (InterruptedException ie){ |
617 |
< |
threadAssertEquals(added, SIZE); |
613 |
< |
} |
614 |
< |
}}); |
604 |
> |
public void testBlockingPut() throws InterruptedException { |
605 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
606 |
> |
Thread t = new Thread(new CheckedRunnable() { |
607 |
> |
public void realRun() throws InterruptedException { |
608 |
> |
for (int i = 0; i < SIZE; ++i) |
609 |
> |
q.put(i); |
610 |
> |
assertEquals(SIZE, q.size()); |
611 |
> |
assertEquals(0, q.remainingCapacity()); |
612 |
> |
try { |
613 |
> |
q.put(99); |
614 |
> |
shouldThrow(); |
615 |
> |
} catch (InterruptedException success) {} |
616 |
> |
}}); |
617 |
> |
|
618 |
|
t.start(); |
619 |
< |
try { |
620 |
< |
Thread.sleep(SHORT_DELAY_MS); |
621 |
< |
t.interrupt(); |
622 |
< |
t.join(); |
623 |
< |
} |
621 |
< |
catch (InterruptedException ie) { |
622 |
< |
unexpectedException(); |
623 |
< |
} |
619 |
> |
Thread.sleep(SHORT_DELAY_MS); |
620 |
> |
t.interrupt(); |
621 |
> |
t.join(); |
622 |
> |
assertEquals(SIZE, q.size()); |
623 |
> |
assertEquals(0, q.remainingCapacity()); |
624 |
|
} |
625 |
|
|
626 |
|
/** |
627 |
|
* put blocks waiting for take when full |
628 |
|
*/ |
629 |
< |
public void testPutWithTake() { |
630 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
631 |
< |
Thread t = new Thread(new Runnable() { |
632 |
< |
public void run() { |
633 |
< |
int added = 0; |
634 |
< |
try { |
635 |
< |
q.put(new Object()); |
636 |
< |
++added; |
637 |
< |
q.put(new Object()); |
638 |
< |
++added; |
639 |
< |
q.put(new Object()); |
640 |
< |
++added; |
641 |
< |
q.put(new Object()); |
642 |
< |
++added; |
643 |
< |
threadShouldThrow(); |
644 |
< |
} catch (InterruptedException e){ |
645 |
< |
threadAssertTrue(added >= 2); |
646 |
< |
} |
647 |
< |
} |
648 |
< |
}); |
649 |
< |
try { |
650 |
< |
t.start(); |
651 |
< |
Thread.sleep(SHORT_DELAY_MS); |
652 |
< |
q.take(); |
653 |
< |
t.interrupt(); |
654 |
< |
t.join(); |
655 |
< |
} catch (Exception e){ |
656 |
< |
unexpectedException(); |
657 |
< |
} |
629 |
> |
public void testPutWithTake() throws InterruptedException { |
630 |
> |
final int capacity = 2; |
631 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
632 |
> |
Thread t = new Thread(new CheckedRunnable() { |
633 |
> |
public void realRun() throws InterruptedException { |
634 |
> |
for (int i = 0; i < capacity + 1; i++) |
635 |
> |
q.put(i); |
636 |
> |
try { |
637 |
> |
q.put(99); |
638 |
> |
shouldThrow(); |
639 |
> |
} catch (InterruptedException success) {} |
640 |
> |
}}); |
641 |
> |
|
642 |
> |
t.start(); |
643 |
> |
Thread.sleep(SHORT_DELAY_MS); |
644 |
> |
assertEquals(q.remainingCapacity(), 0); |
645 |
> |
assertEquals(0, q.take()); |
646 |
> |
Thread.sleep(SHORT_DELAY_MS); |
647 |
> |
t.interrupt(); |
648 |
> |
t.join(); |
649 |
> |
assertEquals(q.remainingCapacity(), 0); |
650 |
|
} |
651 |
|
|
652 |
|
/** |
653 |
|
* timed offer times out if full and elements not taken |
654 |
|
*/ |
655 |
< |
public void testTimedOffer() { |
655 |
> |
public void testTimedOffer() throws InterruptedException { |
656 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
657 |
< |
Thread t = new Thread(new Runnable() { |
658 |
< |
public void run() { |
659 |
< |
try { |
660 |
< |
q.put(new Object()); |
661 |
< |
q.put(new Object()); |
662 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
663 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
664 |
< |
threadShouldThrow(); |
665 |
< |
} catch (InterruptedException success){} |
666 |
< |
} |
675 |
< |
}); |
676 |
< |
|
677 |
< |
try { |
678 |
< |
t.start(); |
679 |
< |
Thread.sleep(SMALL_DELAY_MS); |
680 |
< |
t.interrupt(); |
681 |
< |
t.join(); |
682 |
< |
} catch (Exception e){ |
683 |
< |
unexpectedException(); |
684 |
< |
} |
685 |
< |
} |
657 |
> |
Thread t = new Thread(new CheckedRunnable() { |
658 |
> |
public void realRun() throws InterruptedException { |
659 |
> |
q.put(new Object()); |
660 |
> |
q.put(new Object()); |
661 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
662 |
> |
try { |
663 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
664 |
> |
shouldThrow(); |
665 |
> |
} catch (InterruptedException success) {} |
666 |
> |
}}); |
667 |
|
|
668 |
< |
/** |
669 |
< |
* take retrieves elements in FIFO order |
670 |
< |
*/ |
671 |
< |
public void testTake() { |
691 |
< |
try { |
692 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
693 |
< |
for (int i = 0; i < SIZE; ++i) { |
694 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
695 |
< |
} |
696 |
< |
} catch (InterruptedException e){ |
697 |
< |
unexpectedException(); |
698 |
< |
} |
668 |
> |
t.start(); |
669 |
> |
Thread.sleep(SMALL_DELAY_MS); |
670 |
> |
t.interrupt(); |
671 |
> |
t.join(); |
672 |
|
} |
673 |
|
|
674 |
|
/** |
675 |
< |
* take blocks interruptibly when empty |
675 |
> |
* take retrieves elements in FIFO order |
676 |
|
*/ |
677 |
< |
public void testTakeFromEmpty() { |
678 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
679 |
< |
Thread t = new Thread(new Runnable() { |
680 |
< |
public void run() { |
708 |
< |
try { |
709 |
< |
q.take(); |
710 |
< |
threadShouldThrow(); |
711 |
< |
} catch (InterruptedException success){ } |
712 |
< |
} |
713 |
< |
}); |
714 |
< |
try { |
715 |
< |
t.start(); |
716 |
< |
Thread.sleep(SHORT_DELAY_MS); |
717 |
< |
t.interrupt(); |
718 |
< |
t.join(); |
719 |
< |
} catch (Exception e){ |
720 |
< |
unexpectedException(); |
677 |
> |
public void testTake() throws InterruptedException { |
678 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
679 |
> |
for (int i = 0; i < SIZE; ++i) { |
680 |
> |
assertEquals(i, q.take()); |
681 |
|
} |
682 |
|
} |
683 |
|
|
684 |
|
/** |
685 |
|
* Take removes existing elements until empty, then blocks interruptibly |
686 |
|
*/ |
687 |
< |
public void testBlockingTake() { |
688 |
< |
Thread t = new Thread(new Runnable() { |
689 |
< |
public void run() { |
690 |
< |
try { |
691 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
692 |
< |
for (int i = 0; i < SIZE; ++i) { |
693 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
694 |
< |
} |
695 |
< |
q.take(); |
696 |
< |
threadShouldThrow(); |
697 |
< |
} catch (InterruptedException success){ |
698 |
< |
} |
699 |
< |
}}); |
687 |
> |
public void testBlockingTake() throws InterruptedException { |
688 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
689 |
> |
Thread t = new Thread(new CheckedRunnable() { |
690 |
> |
public void realRun() throws InterruptedException { |
691 |
> |
for (int i = 0; i < SIZE; ++i) { |
692 |
> |
assertEquals(i, q.take()); |
693 |
> |
} |
694 |
> |
try { |
695 |
> |
q.take(); |
696 |
> |
shouldThrow(); |
697 |
> |
} catch (InterruptedException success) {} |
698 |
> |
}}); |
699 |
> |
|
700 |
|
t.start(); |
701 |
< |
try { |
702 |
< |
Thread.sleep(SHORT_DELAY_MS); |
703 |
< |
t.interrupt(); |
744 |
< |
t.join(); |
745 |
< |
} |
746 |
< |
catch (InterruptedException ie) { |
747 |
< |
unexpectedException(); |
748 |
< |
} |
701 |
> |
Thread.sleep(SHORT_DELAY_MS); |
702 |
> |
t.interrupt(); |
703 |
> |
t.join(); |
704 |
|
} |
705 |
|
|
706 |
|
|
710 |
|
public void testPoll() { |
711 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
712 |
|
for (int i = 0; i < SIZE; ++i) { |
713 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
713 |
> |
assertEquals(i, q.poll()); |
714 |
|
} |
715 |
< |
assertNull(q.poll()); |
715 |
> |
assertNull(q.poll()); |
716 |
|
} |
717 |
|
|
718 |
|
/** |
719 |
|
* timed poll with zero timeout succeeds when non-empty, else times out |
720 |
|
*/ |
721 |
< |
public void testTimedPoll0() { |
722 |
< |
try { |
723 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
724 |
< |
for (int i = 0; i < SIZE; ++i) { |
725 |
< |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
726 |
< |
} |
772 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
773 |
< |
} catch (InterruptedException e){ |
774 |
< |
unexpectedException(); |
775 |
< |
} |
721 |
> |
public void testTimedPoll0() throws InterruptedException { |
722 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
723 |
> |
for (int i = 0; i < SIZE; ++i) { |
724 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
725 |
> |
} |
726 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
727 |
|
} |
728 |
|
|
729 |
|
/** |
730 |
|
* timed poll with nonzero timeout succeeds when non-empty, else times out |
731 |
|
*/ |
732 |
< |
public void testTimedPoll() { |
733 |
< |
try { |
734 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
735 |
< |
for (int i = 0; i < SIZE; ++i) { |
736 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
737 |
< |
} |
787 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
788 |
< |
} catch (InterruptedException e){ |
789 |
< |
unexpectedException(); |
790 |
< |
} |
732 |
> |
public void testTimedPoll() throws InterruptedException { |
733 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
734 |
> |
for (int i = 0; i < SIZE; ++i) { |
735 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
736 |
> |
} |
737 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
738 |
|
} |
739 |
|
|
740 |
|
/** |
741 |
|
* Interrupted timed poll throws InterruptedException instead of |
742 |
|
* returning timeout status |
743 |
|
*/ |
744 |
< |
public void testInterruptedTimedPoll() { |
745 |
< |
Thread t = new Thread(new Runnable() { |
746 |
< |
public void run() { |
747 |
< |
try { |
748 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
749 |
< |
for (int i = 0; i < SIZE; ++i) { |
803 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
804 |
< |
} |
805 |
< |
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) { } |
744 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
745 |
> |
Thread t = new Thread(new CheckedRunnable() { |
746 |
> |
public void realRun() throws InterruptedException { |
747 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
748 |
> |
for (int i = 0; i < SIZE; ++i) { |
749 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
750 |
|
} |
751 |
< |
}); |
752 |
< |
try { |
753 |
< |
t.start(); |
754 |
< |
Thread.sleep(SMALL_DELAY_MS); |
755 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
840 |
< |
t.interrupt(); |
841 |
< |
t.join(); |
842 |
< |
} catch (Exception e){ |
843 |
< |
unexpectedException(); |
844 |
< |
} |
845 |
< |
} |
751 |
> |
try { |
752 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
753 |
> |
shouldThrow(); |
754 |
> |
} catch (InterruptedException success) {} |
755 |
> |
}}); |
756 |
|
|
757 |
+ |
t.start(); |
758 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
759 |
+ |
t.interrupt(); |
760 |
+ |
t.join(); |
761 |
+ |
} |
762 |
|
|
763 |
|
/** |
764 |
|
* putFirst(null) throws NPE |
765 |
|
*/ |
766 |
< |
public void testPutFirstNull() { |
767 |
< |
try { |
766 |
> |
public void testPutFirstNull() throws InterruptedException { |
767 |
> |
try { |
768 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
769 |
|
q.putFirst(null); |
770 |
|
shouldThrow(); |
771 |
< |
} |
857 |
< |
catch (NullPointerException success){ |
858 |
< |
} |
859 |
< |
catch (InterruptedException ie) { |
860 |
< |
unexpectedException(); |
861 |
< |
} |
771 |
> |
} catch (NullPointerException success) {} |
772 |
|
} |
773 |
|
|
774 |
|
/** |
775 |
|
* all elements successfully putFirst are contained |
776 |
|
*/ |
777 |
< |
public void testPutFirst() { |
778 |
< |
try { |
779 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
780 |
< |
for (int i = 0; i < SIZE; ++i) { |
781 |
< |
Integer I = new Integer(i); |
782 |
< |
q.putFirst(I); |
873 |
< |
assertTrue(q.contains(I)); |
874 |
< |
} |
875 |
< |
assertEquals(0, q.remainingCapacity()); |
777 |
> |
public void testPutFirst() throws InterruptedException { |
778 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
779 |
> |
for (int i = 0; i < SIZE; ++i) { |
780 |
> |
Integer I = new Integer(i); |
781 |
> |
q.putFirst(I); |
782 |
> |
assertTrue(q.contains(I)); |
783 |
|
} |
784 |
< |
catch (InterruptedException ie) { |
878 |
< |
unexpectedException(); |
879 |
< |
} |
784 |
> |
assertEquals(0, q.remainingCapacity()); |
785 |
|
} |
786 |
|
|
787 |
|
/** |
788 |
|
* putFirst blocks interruptibly if full |
789 |
|
*/ |
790 |
< |
public void testBlockingPutFirst() { |
791 |
< |
Thread t = new Thread(new Runnable() { |
792 |
< |
public void run() { |
793 |
< |
int added = 0; |
794 |
< |
try { |
795 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
796 |
< |
for (int i = 0; i < SIZE; ++i) { |
797 |
< |
q.putFirst(new Integer(i)); |
798 |
< |
++added; |
799 |
< |
} |
800 |
< |
q.putFirst(new Integer(SIZE)); |
801 |
< |
threadShouldThrow(); |
802 |
< |
} catch (InterruptedException ie){ |
803 |
< |
threadAssertEquals(added, SIZE); |
899 |
< |
} |
900 |
< |
}}); |
790 |
> |
public void testBlockingPutFirst() throws InterruptedException { |
791 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
792 |
> |
Thread t = new Thread(new CheckedRunnable() { |
793 |
> |
public void realRun() throws InterruptedException { |
794 |
> |
for (int i = 0; i < SIZE; ++i) |
795 |
> |
q.putFirst(i); |
796 |
> |
assertEquals(SIZE, q.size()); |
797 |
> |
assertEquals(0, q.remainingCapacity()); |
798 |
> |
try { |
799 |
> |
q.putFirst(99); |
800 |
> |
shouldThrow(); |
801 |
> |
} catch (InterruptedException success) {} |
802 |
> |
}}); |
803 |
> |
|
804 |
|
t.start(); |
805 |
< |
try { |
806 |
< |
Thread.sleep(SHORT_DELAY_MS); |
807 |
< |
t.interrupt(); |
808 |
< |
t.join(); |
809 |
< |
} |
907 |
< |
catch (InterruptedException ie) { |
908 |
< |
unexpectedException(); |
909 |
< |
} |
805 |
> |
Thread.sleep(SHORT_DELAY_MS); |
806 |
> |
t.interrupt(); |
807 |
> |
t.join(); |
808 |
> |
assertEquals(SIZE, q.size()); |
809 |
> |
assertEquals(0, q.remainingCapacity()); |
810 |
|
} |
811 |
|
|
812 |
|
/** |
813 |
|
* putFirst blocks waiting for take when full |
814 |
|
*/ |
815 |
< |
public void testPutFirstWithTake() { |
816 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
817 |
< |
Thread t = new Thread(new Runnable() { |
818 |
< |
public void run() { |
819 |
< |
int added = 0; |
820 |
< |
try { |
821 |
< |
q.putFirst(new Object()); |
822 |
< |
++added; |
823 |
< |
q.putFirst(new Object()); |
824 |
< |
++added; |
825 |
< |
q.putFirst(new Object()); |
826 |
< |
++added; |
827 |
< |
q.putFirst(new Object()); |
828 |
< |
++added; |
829 |
< |
threadShouldThrow(); |
830 |
< |
} catch (InterruptedException e){ |
831 |
< |
threadAssertTrue(added >= 2); |
832 |
< |
} |
833 |
< |
} |
834 |
< |
}); |
835 |
< |
try { |
936 |
< |
t.start(); |
937 |
< |
Thread.sleep(SHORT_DELAY_MS); |
938 |
< |
q.take(); |
939 |
< |
t.interrupt(); |
940 |
< |
t.join(); |
941 |
< |
} catch (Exception e){ |
942 |
< |
unexpectedException(); |
943 |
< |
} |
815 |
> |
public void testPutFirstWithTake() throws InterruptedException { |
816 |
> |
final int capacity = 2; |
817 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
818 |
> |
Thread t = new Thread(new CheckedRunnable() { |
819 |
> |
public void realRun() throws InterruptedException { |
820 |
> |
for (int i = 0; i < capacity + 1; i++) |
821 |
> |
q.putFirst(i); |
822 |
> |
try { |
823 |
> |
q.putFirst(99); |
824 |
> |
shouldThrow(); |
825 |
> |
} catch (InterruptedException success) {} |
826 |
> |
}}); |
827 |
> |
|
828 |
> |
t.start(); |
829 |
> |
Thread.sleep(SHORT_DELAY_MS); |
830 |
> |
assertEquals(q.remainingCapacity(), 0); |
831 |
> |
assertEquals(capacity - 1, q.take()); |
832 |
> |
Thread.sleep(SHORT_DELAY_MS); |
833 |
> |
t.interrupt(); |
834 |
> |
t.join(); |
835 |
> |
assertEquals(q.remainingCapacity(), 0); |
836 |
|
} |
837 |
|
|
838 |
|
/** |
839 |
|
* timed offerFirst times out if full and elements not taken |
840 |
|
*/ |
841 |
< |
public void testTimedOfferFirst() { |
841 |
> |
public void testTimedOfferFirst() throws InterruptedException { |
842 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
843 |
< |
Thread t = new Thread(new Runnable() { |
844 |
< |
public void run() { |
845 |
< |
try { |
846 |
< |
q.putFirst(new Object()); |
847 |
< |
q.putFirst(new Object()); |
848 |
< |
threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
849 |
< |
q.offerFirst(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
850 |
< |
threadShouldThrow(); |
851 |
< |
} catch (InterruptedException success){} |
852 |
< |
} |
853 |
< |
}); |
854 |
< |
|
855 |
< |
try { |
856 |
< |
t.start(); |
857 |
< |
Thread.sleep(SMALL_DELAY_MS); |
966 |
< |
t.interrupt(); |
967 |
< |
t.join(); |
968 |
< |
} catch (Exception e){ |
969 |
< |
unexpectedException(); |
970 |
< |
} |
843 |
> |
Thread t = new Thread(new CheckedRunnable() { |
844 |
> |
public void realRun() throws InterruptedException { |
845 |
> |
q.putFirst(new Object()); |
846 |
> |
q.putFirst(new Object()); |
847 |
> |
assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
848 |
> |
try { |
849 |
> |
q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); |
850 |
> |
shouldThrow(); |
851 |
> |
} catch (InterruptedException success) {} |
852 |
> |
}}); |
853 |
> |
|
854 |
> |
t.start(); |
855 |
> |
Thread.sleep(SMALL_DELAY_MS); |
856 |
> |
t.interrupt(); |
857 |
> |
t.join(); |
858 |
|
} |
859 |
|
|
860 |
|
/** |
861 |
|
* take retrieves elements in FIFO order |
862 |
|
*/ |
863 |
< |
public void testTakeFirst() { |
864 |
< |
try { |
865 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
866 |
< |
for (int i = 0; i < SIZE; ++i) { |
867 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
981 |
< |
} |
982 |
< |
} catch (InterruptedException e){ |
983 |
< |
unexpectedException(); |
984 |
< |
} |
863 |
> |
public void testTakeFirst() throws InterruptedException { |
864 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
865 |
> |
for (int i = 0; i < SIZE; ++i) { |
866 |
> |
assertEquals(i, q.takeFirst()); |
867 |
> |
} |
868 |
|
} |
869 |
|
|
870 |
|
/** |
871 |
|
* takeFirst blocks interruptibly when empty |
872 |
|
*/ |
873 |
< |
public void testTakeFirstFromEmpty() { |
873 |
> |
public void testTakeFirstFromEmpty() throws InterruptedException { |
874 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
875 |
< |
Thread t = new Thread(new Runnable() { |
876 |
< |
public void run() { |
877 |
< |
try { |
878 |
< |
q.takeFirst(); |
879 |
< |
threadShouldThrow(); |
880 |
< |
} catch (InterruptedException success){ } |
881 |
< |
} |
882 |
< |
}); |
883 |
< |
try { |
1001 |
< |
t.start(); |
1002 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1003 |
< |
t.interrupt(); |
1004 |
< |
t.join(); |
1005 |
< |
} catch (Exception e){ |
1006 |
< |
unexpectedException(); |
1007 |
< |
} |
875 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
876 |
> |
public void realRun() throws InterruptedException { |
877 |
> |
q.takeFirst(); |
878 |
> |
}}; |
879 |
> |
|
880 |
> |
t.start(); |
881 |
> |
Thread.sleep(SHORT_DELAY_MS); |
882 |
> |
t.interrupt(); |
883 |
> |
t.join(); |
884 |
|
} |
885 |
|
|
886 |
|
/** |
887 |
|
* TakeFirst removes existing elements until empty, then blocks interruptibly |
888 |
|
*/ |
889 |
< |
public void testBlockingTakeFirst() { |
890 |
< |
Thread t = new Thread(new Runnable() { |
891 |
< |
public void run() { |
892 |
< |
try { |
893 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
894 |
< |
for (int i = 0; i < SIZE; ++i) { |
895 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
896 |
< |
} |
897 |
< |
q.takeFirst(); |
898 |
< |
threadShouldThrow(); |
899 |
< |
} catch (InterruptedException success){ |
900 |
< |
} |
1025 |
< |
}}); |
889 |
> |
public void testBlockingTakeFirst() throws InterruptedException { |
890 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
891 |
> |
Thread t = new Thread(new CheckedRunnable() { |
892 |
> |
public void realRun() throws InterruptedException { |
893 |
> |
for (int i = 0; i < SIZE; ++i) |
894 |
> |
assertEquals(i, q.takeFirst()); |
895 |
> |
try { |
896 |
> |
q.takeFirst(); |
897 |
> |
shouldThrow(); |
898 |
> |
} catch (InterruptedException success) {} |
899 |
> |
}}); |
900 |
> |
|
901 |
|
t.start(); |
902 |
< |
try { |
903 |
< |
Thread.sleep(SHORT_DELAY_MS); |
904 |
< |
t.interrupt(); |
1030 |
< |
t.join(); |
1031 |
< |
} |
1032 |
< |
catch (InterruptedException ie) { |
1033 |
< |
unexpectedException(); |
1034 |
< |
} |
902 |
> |
Thread.sleep(SHORT_DELAY_MS); |
903 |
> |
t.interrupt(); |
904 |
> |
t.join(); |
905 |
|
} |
906 |
|
|
907 |
|
|
908 |
|
/** |
909 |
|
* timed pollFirst with zero timeout succeeds when non-empty, else times out |
910 |
|
*/ |
911 |
< |
public void testTimedPollFirst0() { |
912 |
< |
try { |
913 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
914 |
< |
for (int i = 0; i < SIZE; ++i) { |
915 |
< |
assertEquals(i, ((Integer)q.pollFirst(0, TimeUnit.MILLISECONDS)).intValue()); |
916 |
< |
} |
1047 |
< |
assertNull(q.pollFirst(0, TimeUnit.MILLISECONDS)); |
1048 |
< |
} catch (InterruptedException e){ |
1049 |
< |
unexpectedException(); |
1050 |
< |
} |
911 |
> |
public void testTimedPollFirst0() throws InterruptedException { |
912 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
913 |
> |
for (int i = 0; i < SIZE; ++i) { |
914 |
> |
assertEquals(i, q.pollFirst(0, MILLISECONDS)); |
915 |
> |
} |
916 |
> |
assertNull(q.pollFirst(0, MILLISECONDS)); |
917 |
|
} |
918 |
|
|
919 |
|
/** |
920 |
|
* timed pollFirst with nonzero timeout succeeds when non-empty, else times out |
921 |
|
*/ |
922 |
< |
public void testTimedPollFirst() { |
923 |
< |
try { |
924 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
925 |
< |
for (int i = 0; i < SIZE; ++i) { |
926 |
< |
assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
927 |
< |
} |
1062 |
< |
assertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1063 |
< |
} catch (InterruptedException e){ |
1064 |
< |
unexpectedException(); |
1065 |
< |
} |
922 |
> |
public void testTimedPollFirst() throws InterruptedException { |
923 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
924 |
> |
for (int i = 0; i < SIZE; ++i) { |
925 |
> |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
926 |
> |
} |
927 |
> |
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
928 |
|
} |
929 |
|
|
930 |
|
/** |
931 |
|
* Interrupted timed pollFirst throws InterruptedException instead of |
932 |
|
* returning timeout status |
933 |
|
*/ |
934 |
< |
public void testInterruptedTimedPollFirst() { |
935 |
< |
Thread t = new Thread(new Runnable() { |
936 |
< |
public void run() { |
937 |
< |
try { |
938 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
939 |
< |
for (int i = 0; i < SIZE; ++i) { |
940 |
< |
threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
941 |
< |
} |
942 |
< |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
943 |
< |
} catch (InterruptedException success){ |
944 |
< |
} |
945 |
< |
}}); |
934 |
> |
public void testInterruptedTimedPollFirst() throws InterruptedException { |
935 |
> |
Thread t = new Thread(new CheckedRunnable() { |
936 |
> |
public void realRun() throws InterruptedException { |
937 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
938 |
> |
for (int i = 0; i < SIZE; ++i) { |
939 |
> |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
940 |
> |
} |
941 |
> |
try { |
942 |
> |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
943 |
> |
shouldThrow(); |
944 |
> |
} catch (InterruptedException success) {} |
945 |
> |
}}); |
946 |
> |
|
947 |
|
t.start(); |
948 |
< |
try { |
949 |
< |
Thread.sleep(SHORT_DELAY_MS); |
950 |
< |
t.interrupt(); |
1088 |
< |
t.join(); |
1089 |
< |
} |
1090 |
< |
catch (InterruptedException ie) { |
1091 |
< |
unexpectedException(); |
1092 |
< |
} |
948 |
> |
Thread.sleep(SHORT_DELAY_MS); |
949 |
> |
t.interrupt(); |
950 |
> |
t.join(); |
951 |
|
} |
952 |
|
|
953 |
|
/** |
954 |
< |
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
955 |
< |
* on interruption throws |
954 |
> |
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
955 |
> |
* on interruption throws |
956 |
|
*/ |
957 |
< |
public void testTimedPollFirstWithOfferFirst() { |
957 |
> |
public void testTimedPollFirstWithOfferFirst() throws InterruptedException { |
958 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
959 |
< |
Thread t = new Thread(new Runnable() { |
960 |
< |
public void run() { |
961 |
< |
try { |
962 |
< |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
963 |
< |
q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
964 |
< |
q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
965 |
< |
threadShouldThrow(); |
966 |
< |
} catch (InterruptedException success) { } |
967 |
< |
} |
968 |
< |
}); |
969 |
< |
try { |
970 |
< |
t.start(); |
971 |
< |
Thread.sleep(SMALL_DELAY_MS); |
972 |
< |
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
973 |
< |
t.interrupt(); |
974 |
< |
t.join(); |
1117 |
< |
} catch (Exception e){ |
1118 |
< |
unexpectedException(); |
1119 |
< |
} |
1120 |
< |
} |
959 |
> |
Thread t = new Thread(new CheckedRunnable() { |
960 |
> |
public void realRun() throws InterruptedException { |
961 |
> |
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
962 |
> |
assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
963 |
> |
try { |
964 |
> |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
965 |
> |
shouldThrow(); |
966 |
> |
} catch (InterruptedException success) {} |
967 |
> |
}}); |
968 |
> |
|
969 |
> |
t.start(); |
970 |
> |
Thread.sleep(SMALL_DELAY_MS); |
971 |
> |
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS)); |
972 |
> |
t.interrupt(); |
973 |
> |
t.join(); |
974 |
> |
} |
975 |
|
|
976 |
|
/** |
977 |
|
* putLast(null) throws NPE |
978 |
|
*/ |
979 |
< |
public void testPutLastNull() { |
980 |
< |
try { |
979 |
> |
public void testPutLastNull() throws InterruptedException { |
980 |
> |
try { |
981 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
982 |
|
q.putLast(null); |
983 |
|
shouldThrow(); |
984 |
< |
} |
1131 |
< |
catch (NullPointerException success){ |
1132 |
< |
} |
1133 |
< |
catch (InterruptedException ie) { |
1134 |
< |
unexpectedException(); |
1135 |
< |
} |
984 |
> |
} catch (NullPointerException success) {} |
985 |
|
} |
986 |
|
|
987 |
|
/** |
988 |
|
* all elements successfully putLast are contained |
989 |
|
*/ |
990 |
< |
public void testPutLast() { |
991 |
< |
try { |
992 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
993 |
< |
for (int i = 0; i < SIZE; ++i) { |
994 |
< |
Integer I = new Integer(i); |
995 |
< |
q.putLast(I); |
1147 |
< |
assertTrue(q.contains(I)); |
1148 |
< |
} |
1149 |
< |
assertEquals(0, q.remainingCapacity()); |
990 |
> |
public void testPutLast() throws InterruptedException { |
991 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
992 |
> |
for (int i = 0; i < SIZE; ++i) { |
993 |
> |
Integer I = new Integer(i); |
994 |
> |
q.putLast(I); |
995 |
> |
assertTrue(q.contains(I)); |
996 |
|
} |
997 |
< |
catch (InterruptedException ie) { |
1152 |
< |
unexpectedException(); |
1153 |
< |
} |
997 |
> |
assertEquals(0, q.remainingCapacity()); |
998 |
|
} |
999 |
|
|
1000 |
|
/** |
1001 |
|
* putLast blocks interruptibly if full |
1002 |
|
*/ |
1003 |
< |
public void testBlockingPutLast() { |
1004 |
< |
Thread t = new Thread(new Runnable() { |
1005 |
< |
public void run() { |
1006 |
< |
int added = 0; |
1007 |
< |
try { |
1008 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1009 |
< |
for (int i = 0; i < SIZE; ++i) { |
1010 |
< |
q.putLast(new Integer(i)); |
1011 |
< |
++added; |
1012 |
< |
} |
1013 |
< |
q.putLast(new Integer(SIZE)); |
1014 |
< |
threadShouldThrow(); |
1015 |
< |
} catch (InterruptedException ie){ |
1016 |
< |
threadAssertEquals(added, SIZE); |
1173 |
< |
} |
1174 |
< |
}}); |
1003 |
> |
public void testBlockingPutLast() throws InterruptedException { |
1004 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1005 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1006 |
> |
public void realRun() throws InterruptedException { |
1007 |
> |
for (int i = 0; i < SIZE; ++i) |
1008 |
> |
q.putLast(i); |
1009 |
> |
assertEquals(SIZE, q.size()); |
1010 |
> |
assertEquals(0, q.remainingCapacity()); |
1011 |
> |
try { |
1012 |
> |
q.putLast(99); |
1013 |
> |
shouldThrow(); |
1014 |
> |
} catch (InterruptedException success) {} |
1015 |
> |
}}); |
1016 |
> |
|
1017 |
|
t.start(); |
1018 |
< |
try { |
1019 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1020 |
< |
t.interrupt(); |
1021 |
< |
t.join(); |
1022 |
< |
} |
1181 |
< |
catch (InterruptedException ie) { |
1182 |
< |
unexpectedException(); |
1183 |
< |
} |
1018 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1019 |
> |
t.interrupt(); |
1020 |
> |
t.join(); |
1021 |
> |
assertEquals(SIZE, q.size()); |
1022 |
> |
assertEquals(0, q.remainingCapacity()); |
1023 |
|
} |
1024 |
|
|
1025 |
|
/** |
1026 |
|
* putLast blocks waiting for take when full |
1027 |
|
*/ |
1028 |
< |
public void testPutLastWithTake() { |
1029 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1030 |
< |
Thread t = new Thread(new Runnable() { |
1031 |
< |
public void run() { |
1032 |
< |
int added = 0; |
1033 |
< |
try { |
1034 |
< |
q.putLast(new Object()); |
1035 |
< |
++added; |
1036 |
< |
q.putLast(new Object()); |
1037 |
< |
++added; |
1038 |
< |
q.putLast(new Object()); |
1039 |
< |
++added; |
1040 |
< |
q.putLast(new Object()); |
1041 |
< |
++added; |
1042 |
< |
threadShouldThrow(); |
1043 |
< |
} catch (InterruptedException e){ |
1044 |
< |
threadAssertTrue(added >= 2); |
1045 |
< |
} |
1046 |
< |
} |
1047 |
< |
}); |
1048 |
< |
try { |
1210 |
< |
t.start(); |
1211 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1212 |
< |
q.take(); |
1213 |
< |
t.interrupt(); |
1214 |
< |
t.join(); |
1215 |
< |
} catch (Exception e){ |
1216 |
< |
unexpectedException(); |
1217 |
< |
} |
1028 |
> |
public void testPutLastWithTake() throws InterruptedException { |
1029 |
> |
final int capacity = 2; |
1030 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
1031 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1032 |
> |
public void realRun() throws InterruptedException { |
1033 |
> |
for (int i = 0; i < capacity + 1; i++) |
1034 |
> |
q.putLast(i); |
1035 |
> |
try { |
1036 |
> |
q.putLast(99); |
1037 |
> |
shouldThrow(); |
1038 |
> |
} catch (InterruptedException success) {} |
1039 |
> |
}}); |
1040 |
> |
|
1041 |
> |
t.start(); |
1042 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1043 |
> |
assertEquals(q.remainingCapacity(), 0); |
1044 |
> |
assertEquals(0, q.take()); |
1045 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1046 |
> |
t.interrupt(); |
1047 |
> |
t.join(); |
1048 |
> |
assertEquals(q.remainingCapacity(), 0); |
1049 |
|
} |
1050 |
|
|
1051 |
|
/** |
1052 |
|
* timed offerLast times out if full and elements not taken |
1053 |
|
*/ |
1054 |
< |
public void testTimedOfferLast() { |
1054 |
> |
public void testTimedOfferLast() throws InterruptedException { |
1055 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1056 |
< |
Thread t = new Thread(new Runnable() { |
1057 |
< |
public void run() { |
1058 |
< |
try { |
1059 |
< |
q.putLast(new Object()); |
1060 |
< |
q.putLast(new Object()); |
1061 |
< |
threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1062 |
< |
q.offerLast(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1063 |
< |
threadShouldThrow(); |
1064 |
< |
} catch (InterruptedException success){} |
1065 |
< |
} |
1066 |
< |
}); |
1067 |
< |
|
1068 |
< |
try { |
1069 |
< |
t.start(); |
1070 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1240 |
< |
t.interrupt(); |
1241 |
< |
t.join(); |
1242 |
< |
} catch (Exception e){ |
1243 |
< |
unexpectedException(); |
1244 |
< |
} |
1056 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1057 |
> |
public void realRun() throws InterruptedException { |
1058 |
> |
q.putLast(new Object()); |
1059 |
> |
q.putLast(new Object()); |
1060 |
> |
assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
1061 |
> |
try { |
1062 |
> |
q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1063 |
> |
shouldThrow(); |
1064 |
> |
} catch (InterruptedException success) {} |
1065 |
> |
}}); |
1066 |
> |
|
1067 |
> |
t.start(); |
1068 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1069 |
> |
t.interrupt(); |
1070 |
> |
t.join(); |
1071 |
|
} |
1072 |
|
|
1073 |
|
/** |
1074 |
|
* takeLast retrieves elements in FIFO order |
1075 |
|
*/ |
1076 |
< |
public void testTakeLast() { |
1077 |
< |
try { |
1078 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1079 |
< |
for (int i = 0; i < SIZE; ++i) { |
1080 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1255 |
< |
} |
1256 |
< |
} catch (InterruptedException e){ |
1257 |
< |
unexpectedException(); |
1258 |
< |
} |
1076 |
> |
public void testTakeLast() throws InterruptedException { |
1077 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1078 |
> |
for (int i = 0; i < SIZE; ++i) { |
1079 |
> |
assertEquals(SIZE-i-1, q.takeLast()); |
1080 |
> |
} |
1081 |
|
} |
1082 |
|
|
1083 |
|
/** |
1084 |
|
* takeLast blocks interruptibly when empty |
1085 |
|
*/ |
1086 |
< |
public void testTakeLastFromEmpty() { |
1086 |
> |
public void testTakeLastFromEmpty() throws InterruptedException { |
1087 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1088 |
< |
Thread t = new Thread(new Runnable() { |
1089 |
< |
public void run() { |
1090 |
< |
try { |
1091 |
< |
q.takeLast(); |
1092 |
< |
threadShouldThrow(); |
1093 |
< |
} catch (InterruptedException success){ } |
1094 |
< |
} |
1095 |
< |
}); |
1096 |
< |
try { |
1275 |
< |
t.start(); |
1276 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1277 |
< |
t.interrupt(); |
1278 |
< |
t.join(); |
1279 |
< |
} catch (Exception e){ |
1280 |
< |
unexpectedException(); |
1281 |
< |
} |
1088 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1089 |
> |
public void realRun() throws InterruptedException { |
1090 |
> |
q.takeLast(); |
1091 |
> |
}}; |
1092 |
> |
|
1093 |
> |
t.start(); |
1094 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1095 |
> |
t.interrupt(); |
1096 |
> |
t.join(); |
1097 |
|
} |
1098 |
|
|
1099 |
|
/** |
1100 |
|
* TakeLast removes existing elements until empty, then blocks interruptibly |
1101 |
|
*/ |
1102 |
< |
public void testBlockingTakeLast() { |
1103 |
< |
Thread t = new Thread(new Runnable() { |
1104 |
< |
public void run() { |
1105 |
< |
try { |
1106 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1107 |
< |
for (int i = 0; i < SIZE; ++i) { |
1108 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1109 |
< |
} |
1110 |
< |
q.takeLast(); |
1111 |
< |
threadShouldThrow(); |
1112 |
< |
} catch (InterruptedException success){ |
1113 |
< |
} |
1299 |
< |
}}); |
1102 |
> |
public void testBlockingTakeLast() throws InterruptedException { |
1103 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1104 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1105 |
> |
public void realRun() throws InterruptedException { |
1106 |
> |
for (int i = 0; i < SIZE; ++i) |
1107 |
> |
assertEquals(SIZE - 1 - i, q.takeLast()); |
1108 |
> |
try { |
1109 |
> |
q.takeLast(); |
1110 |
> |
shouldThrow(); |
1111 |
> |
} catch (InterruptedException success) {} |
1112 |
> |
}}); |
1113 |
> |
|
1114 |
|
t.start(); |
1115 |
< |
try { |
1116 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1117 |
< |
t.interrupt(); |
1304 |
< |
t.join(); |
1305 |
< |
} |
1306 |
< |
catch (InterruptedException ie) { |
1307 |
< |
unexpectedException(); |
1308 |
< |
} |
1115 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1116 |
> |
t.interrupt(); |
1117 |
> |
t.join(); |
1118 |
|
} |
1119 |
|
|
1311 |
– |
|
1120 |
|
/** |
1121 |
|
* timed pollLast with zero timeout succeeds when non-empty, else times out |
1122 |
|
*/ |
1123 |
< |
public void testTimedPollLast0() { |
1124 |
< |
try { |
1125 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1126 |
< |
for (int i = 0; i < SIZE; ++i) { |
1127 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, TimeUnit.MILLISECONDS)).intValue()); |
1128 |
< |
} |
1321 |
< |
assertNull(q.pollLast(0, TimeUnit.MILLISECONDS)); |
1322 |
< |
} catch (InterruptedException e){ |
1323 |
< |
unexpectedException(); |
1324 |
< |
} |
1123 |
> |
public void testTimedPollLast0() throws InterruptedException { |
1124 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1125 |
> |
for (int i = 0; i < SIZE; ++i) { |
1126 |
> |
assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS)); |
1127 |
> |
} |
1128 |
> |
assertNull(q.pollLast(0, MILLISECONDS)); |
1129 |
|
} |
1130 |
|
|
1131 |
|
/** |
1132 |
|
* timed pollLast with nonzero timeout succeeds when non-empty, else times out |
1133 |
|
*/ |
1134 |
< |
public void testTimedPollLast() { |
1135 |
< |
try { |
1136 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1137 |
< |
for (int i = 0; i < SIZE; ++i) { |
1138 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1139 |
< |
} |
1336 |
< |
assertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1337 |
< |
} catch (InterruptedException e){ |
1338 |
< |
unexpectedException(); |
1339 |
< |
} |
1134 |
> |
public void testTimedPollLast() throws InterruptedException { |
1135 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1136 |
> |
for (int i = 0; i < SIZE; ++i) { |
1137 |
> |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1138 |
> |
} |
1139 |
> |
assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1140 |
|
} |
1141 |
|
|
1142 |
|
/** |
1143 |
|
* Interrupted timed pollLast throws InterruptedException instead of |
1144 |
|
* returning timeout status |
1145 |
|
*/ |
1146 |
< |
public void testInterruptedTimedPollLast() { |
1147 |
< |
Thread t = new Thread(new Runnable() { |
1148 |
< |
public void run() { |
1149 |
< |
try { |
1150 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1151 |
< |
for (int i = 0; i < SIZE; ++i) { |
1152 |
< |
threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1153 |
< |
} |
1154 |
< |
threadAssertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1155 |
< |
} catch (InterruptedException success){ |
1156 |
< |
} |
1157 |
< |
}}); |
1146 |
> |
public void testInterruptedTimedPollLast() throws InterruptedException { |
1147 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1148 |
> |
public void realRun() throws InterruptedException { |
1149 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1150 |
> |
for (int i = 0; i < SIZE; ++i) { |
1151 |
> |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1152 |
> |
} |
1153 |
> |
try { |
1154 |
> |
q.pollLast(SMALL_DELAY_MS, MILLISECONDS); |
1155 |
> |
shouldThrow(); |
1156 |
> |
} catch (InterruptedException success) {} |
1157 |
> |
}}); |
1158 |
> |
|
1159 |
|
t.start(); |
1160 |
< |
try { |
1161 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1162 |
< |
t.interrupt(); |
1362 |
< |
t.join(); |
1363 |
< |
} |
1364 |
< |
catch (InterruptedException ie) { |
1365 |
< |
unexpectedException(); |
1366 |
< |
} |
1160 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1161 |
> |
t.interrupt(); |
1162 |
> |
t.join(); |
1163 |
|
} |
1164 |
|
|
1165 |
|
/** |
1166 |
< |
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1167 |
< |
* on interruption throws |
1166 |
> |
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1167 |
> |
* on interruption throws |
1168 |
|
*/ |
1169 |
< |
public void testTimedPollWithOfferLast() { |
1169 |
> |
public void testTimedPollWithOfferLast() throws InterruptedException { |
1170 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1171 |
< |
Thread t = new Thread(new Runnable() { |
1172 |
< |
public void run() { |
1173 |
< |
try { |
1174 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1175 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1176 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1177 |
< |
threadShouldThrow(); |
1178 |
< |
} catch (InterruptedException success) { } |
1179 |
< |
} |
1180 |
< |
}); |
1181 |
< |
try { |
1182 |
< |
t.start(); |
1183 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1184 |
< |
assertTrue(q.offerLast(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1185 |
< |
t.interrupt(); |
1186 |
< |
t.join(); |
1391 |
< |
} catch (Exception e){ |
1392 |
< |
unexpectedException(); |
1393 |
< |
} |
1394 |
< |
} |
1171 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1172 |
> |
public void realRun() throws InterruptedException { |
1173 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
1174 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1175 |
> |
try { |
1176 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1177 |
> |
shouldThrow(); |
1178 |
> |
} catch (InterruptedException success) {} |
1179 |
> |
}}); |
1180 |
> |
|
1181 |
> |
t.start(); |
1182 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1183 |
> |
assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS)); |
1184 |
> |
t.interrupt(); |
1185 |
> |
t.join(); |
1186 |
> |
} |
1187 |
|
|
1188 |
|
|
1189 |
|
/** |
1192 |
|
public void testElement() { |
1193 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1194 |
|
for (int i = 0; i < SIZE; ++i) { |
1195 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
1195 |
> |
assertEquals(i, q.element()); |
1196 |
|
q.poll(); |
1197 |
|
} |
1198 |
|
try { |
1199 |
|
q.element(); |
1200 |
|
shouldThrow(); |
1201 |
< |
} |
1410 |
< |
catch (NoSuchElementException success) {} |
1201 |
> |
} catch (NoSuchElementException success) {} |
1202 |
|
} |
1203 |
|
|
1204 |
|
/** |
1215 |
|
} |
1216 |
|
assertTrue(q.isEmpty()); |
1217 |
|
} |
1218 |
< |
|
1218 |
> |
|
1219 |
|
/** |
1220 |
|
* contains(x) reports true when elements added but not yet removed |
1221 |
|
*/ |
1296 |
|
/** |
1297 |
|
* toArray contains all elements |
1298 |
|
*/ |
1299 |
< |
public void testToArray() { |
1299 |
> |
public void testToArray() throws InterruptedException{ |
1300 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1301 |
< |
Object[] o = q.toArray(); |
1302 |
< |
try { |
1303 |
< |
for(int i = 0; i < o.length; i++) |
1513 |
< |
assertEquals(o[i], q.take()); |
1514 |
< |
} catch (InterruptedException e){ |
1515 |
< |
unexpectedException(); |
1516 |
< |
} |
1301 |
> |
Object[] o = q.toArray(); |
1302 |
> |
for (int i = 0; i < o.length; i++) |
1303 |
> |
assertEquals(o[i], q.take()); |
1304 |
|
} |
1305 |
|
|
1306 |
|
/** |
1307 |
|
* toArray(a) contains all elements |
1308 |
|
*/ |
1309 |
< |
public void testToArray2() { |
1309 |
> |
public void testToArray2() throws InterruptedException { |
1310 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1311 |
< |
Integer[] ints = new Integer[SIZE]; |
1312 |
< |
ints = (Integer[])q.toArray(ints); |
1313 |
< |
try { |
1314 |
< |
for(int i = 0; i < ints.length; i++) |
1528 |
< |
assertEquals(ints[i], q.take()); |
1529 |
< |
} catch (InterruptedException e){ |
1530 |
< |
unexpectedException(); |
1531 |
< |
} |
1311 |
> |
Integer[] ints = new Integer[SIZE]; |
1312 |
> |
ints = (Integer[])q.toArray(ints); |
1313 |
> |
for (int i = 0; i < ints.length; i++) |
1314 |
> |
assertEquals(ints[i], q.take()); |
1315 |
|
} |
1316 |
|
|
1317 |
|
/** |
1318 |
|
* toArray(null) throws NPE |
1319 |
|
*/ |
1320 |
|
public void testToArray_BadArg() { |
1321 |
< |
try { |
1322 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1323 |
< |
Object o[] = q.toArray(null); |
1324 |
< |
shouldThrow(); |
1325 |
< |
} catch(NullPointerException success){} |
1321 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1322 |
> |
try { |
1323 |
> |
Object o[] = q.toArray(null); |
1324 |
> |
shouldThrow(); |
1325 |
> |
} catch (NullPointerException success) {} |
1326 |
|
} |
1327 |
|
|
1328 |
|
/** |
1329 |
< |
* toArray with incompatible array type throws CCE |
1329 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
1330 |
|
*/ |
1331 |
|
public void testToArray1_BadArg() { |
1332 |
< |
try { |
1333 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1334 |
< |
Object o[] = q.toArray(new String[10] ); |
1335 |
< |
shouldThrow(); |
1336 |
< |
} catch(ArrayStoreException success){} |
1332 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1333 |
> |
try { |
1334 |
> |
q.toArray(new String[10]); |
1335 |
> |
shouldThrow(); |
1336 |
> |
} catch (ArrayStoreException success) {} |
1337 |
|
} |
1338 |
|
|
1339 |
< |
|
1339 |
> |
|
1340 |
|
/** |
1341 |
|
* iterator iterates through all elements |
1342 |
|
*/ |
1343 |
< |
public void testIterator() { |
1343 |
> |
public void testIterator() throws InterruptedException { |
1344 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1345 |
< |
Iterator it = q.iterator(); |
1346 |
< |
try { |
1347 |
< |
while(it.hasNext()){ |
1348 |
< |
assertEquals(it.next(), q.take()); |
1566 |
< |
} |
1567 |
< |
} catch (InterruptedException e){ |
1568 |
< |
unexpectedException(); |
1569 |
< |
} |
1345 |
> |
Iterator it = q.iterator(); |
1346 |
> |
while (it.hasNext()) { |
1347 |
> |
assertEquals(it.next(), q.take()); |
1348 |
> |
} |
1349 |
|
} |
1350 |
|
|
1351 |
|
/** |
1352 |
|
* iterator.remove removes current element |
1353 |
|
*/ |
1354 |
< |
public void testIteratorRemove () { |
1354 |
> |
public void testIteratorRemove() { |
1355 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
1356 |
|
q.add(two); |
1357 |
|
q.add(one); |
1360 |
|
Iterator it = q.iterator(); |
1361 |
|
it.next(); |
1362 |
|
it.remove(); |
1363 |
< |
|
1363 |
> |
|
1364 |
|
it = q.iterator(); |
1365 |
< |
assertEquals(it.next(), one); |
1366 |
< |
assertEquals(it.next(), three); |
1365 |
> |
assertSame(it.next(), one); |
1366 |
> |
assertSame(it.next(), three); |
1367 |
|
assertFalse(it.hasNext()); |
1368 |
|
} |
1369 |
|
|
1379 |
|
assertEquals(0, q.remainingCapacity()); |
1380 |
|
int k = 0; |
1381 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
1382 |
< |
int i = ((Integer)(it.next())).intValue(); |
1604 |
< |
assertEquals(++k, i); |
1382 |
> |
assertEquals(++k, it.next()); |
1383 |
|
} |
1384 |
|
assertEquals(3, k); |
1385 |
|
} |
1387 |
|
/** |
1388 |
|
* Modifications do not cause iterators to fail |
1389 |
|
*/ |
1390 |
< |
public void testWeaklyConsistentIteration () { |
1390 |
> |
public void testWeaklyConsistentIteration() { |
1391 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
1392 |
|
q.add(one); |
1393 |
|
q.add(two); |
1394 |
|
q.add(three); |
1395 |
< |
try { |
1396 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
1397 |
< |
q.remove(); |
1620 |
< |
it.next(); |
1621 |
< |
} |
1622 |
< |
} |
1623 |
< |
catch (ConcurrentModificationException e) { |
1624 |
< |
unexpectedException(); |
1395 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
1396 |
> |
q.remove(); |
1397 |
> |
it.next(); |
1398 |
|
} |
1399 |
|
assertEquals(0, q.size()); |
1400 |
|
} |
1401 |
|
|
1402 |
|
|
1403 |
|
/** |
1404 |
< |
* Descending iterator iterates through all elements |
1404 |
> |
* Descending iterator iterates through all elements |
1405 |
|
*/ |
1406 |
|
public void testDescendingIterator() { |
1407 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1408 |
|
int i = 0; |
1409 |
< |
Iterator it = q.descendingIterator(); |
1410 |
< |
while(it.hasNext()) { |
1409 |
> |
Iterator it = q.descendingIterator(); |
1410 |
> |
while (it.hasNext()) { |
1411 |
|
assertTrue(q.contains(it.next())); |
1412 |
|
++i; |
1413 |
|
} |
1415 |
|
assertFalse(it.hasNext()); |
1416 |
|
try { |
1417 |
|
it.next(); |
1418 |
< |
} catch(NoSuchElementException success) { |
1419 |
< |
} |
1418 |
> |
shouldThrow(); |
1419 |
> |
} catch (NoSuchElementException success) {} |
1420 |
|
} |
1421 |
|
|
1422 |
|
/** |
1423 |
< |
* Descending iterator ordering is reverse FIFO |
1423 |
> |
* Descending iterator ordering is reverse FIFO |
1424 |
|
*/ |
1425 |
|
public void testDescendingIteratorOrdering() { |
1426 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1430 |
|
q.add(new Integer(1)); |
1431 |
|
int k = 0; |
1432 |
|
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
1433 |
< |
int i = ((Integer)(it.next())).intValue(); |
1661 |
< |
assertEquals(++k, i); |
1433 |
> |
assertEquals(++k, it.next()); |
1434 |
|
} |
1435 |
< |
|
1435 |
> |
|
1436 |
|
assertEquals(3, k); |
1437 |
|
q.remove(); |
1438 |
|
q.remove(); |
1443 |
|
/** |
1444 |
|
* descendingIterator.remove removes current element |
1445 |
|
*/ |
1446 |
< |
public void testDescendingIteratorRemove () { |
1446 |
> |
public void testDescendingIteratorRemove() { |
1447 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1448 |
|
for (int iters = 0; iters < 100; ++iters) { |
1449 |
|
q.add(new Integer(3)); |
1472 |
|
for (int i = 0; i < SIZE; ++i) { |
1473 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
1474 |
|
} |
1475 |
< |
} |
1475 |
> |
} |
1476 |
|
|
1477 |
|
|
1478 |
|
/** |
1483 |
|
q.add(one); |
1484 |
|
q.add(two); |
1485 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1486 |
< |
executor.execute(new Runnable() { |
1487 |
< |
public void run() { |
1488 |
< |
threadAssertFalse(q.offer(three)); |
1489 |
< |
try { |
1490 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
1491 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
1492 |
< |
} |
1493 |
< |
catch (InterruptedException e) { |
1494 |
< |
threadUnexpectedException(); |
1495 |
< |
} |
1496 |
< |
} |
1497 |
< |
}); |
1486 |
> |
executor.execute(new CheckedRunnable() { |
1487 |
> |
public void realRun() throws InterruptedException { |
1488 |
> |
assertFalse(q.offer(three)); |
1489 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
1490 |
> |
assertEquals(0, q.remainingCapacity()); |
1491 |
> |
}}); |
1492 |
> |
|
1493 |
> |
executor.execute(new CheckedRunnable() { |
1494 |
> |
public void realRun() throws InterruptedException { |
1495 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1496 |
> |
assertSame(one, q.take()); |
1497 |
> |
}}); |
1498 |
|
|
1727 |
– |
executor.execute(new Runnable() { |
1728 |
– |
public void run() { |
1729 |
– |
try { |
1730 |
– |
Thread.sleep(SMALL_DELAY_MS); |
1731 |
– |
threadAssertEquals(one, q.take()); |
1732 |
– |
} |
1733 |
– |
catch (InterruptedException e) { |
1734 |
– |
threadUnexpectedException(); |
1735 |
– |
} |
1736 |
– |
} |
1737 |
– |
}); |
1738 |
– |
|
1499 |
|
joinPool(executor); |
1500 |
|
} |
1501 |
|
|
1505 |
|
public void testPollInExecutor() { |
1506 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1507 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1508 |
< |
executor.execute(new Runnable() { |
1509 |
< |
public void run() { |
1510 |
< |
threadAssertNull(q.poll()); |
1511 |
< |
try { |
1512 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
1513 |
< |
threadAssertTrue(q.isEmpty()); |
1514 |
< |
} |
1515 |
< |
catch (InterruptedException e) { |
1516 |
< |
threadUnexpectedException(); |
1517 |
< |
} |
1518 |
< |
} |
1519 |
< |
}); |
1508 |
> |
executor.execute(new CheckedRunnable() { |
1509 |
> |
public void realRun() throws InterruptedException { |
1510 |
> |
assertNull(q.poll()); |
1511 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1512 |
> |
assertTrue(q.isEmpty()); |
1513 |
> |
}}); |
1514 |
> |
|
1515 |
> |
executor.execute(new CheckedRunnable() { |
1516 |
> |
public void realRun() throws InterruptedException { |
1517 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1518 |
> |
q.put(one); |
1519 |
> |
}}); |
1520 |
|
|
1761 |
– |
executor.execute(new Runnable() { |
1762 |
– |
public void run() { |
1763 |
– |
try { |
1764 |
– |
Thread.sleep(SMALL_DELAY_MS); |
1765 |
– |
q.put(one); |
1766 |
– |
} |
1767 |
– |
catch (InterruptedException e) { |
1768 |
– |
threadUnexpectedException(); |
1769 |
– |
} |
1770 |
– |
} |
1771 |
– |
}); |
1772 |
– |
|
1521 |
|
joinPool(executor); |
1522 |
|
} |
1523 |
|
|
1524 |
|
/** |
1525 |
|
* A deserialized serialized deque has same elements in same order |
1526 |
|
*/ |
1527 |
< |
public void testSerialization() { |
1527 |
> |
public void testSerialization() throws Exception { |
1528 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1529 |
|
|
1530 |
< |
try { |
1531 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1532 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1533 |
< |
out.writeObject(q); |
1534 |
< |
out.close(); |
1535 |
< |
|
1536 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1537 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1538 |
< |
LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject(); |
1539 |
< |
assertEquals(q.size(), r.size()); |
1540 |
< |
while (!q.isEmpty()) |
1793 |
< |
assertEquals(q.remove(), r.remove()); |
1794 |
< |
} catch(Exception e){ |
1795 |
< |
unexpectedException(); |
1796 |
< |
} |
1530 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1531 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1532 |
> |
out.writeObject(q); |
1533 |
> |
out.close(); |
1534 |
> |
|
1535 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1536 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1537 |
> |
LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject(); |
1538 |
> |
assertEquals(q.size(), r.size()); |
1539 |
> |
while (!q.isEmpty()) |
1540 |
> |
assertEquals(q.remove(), r.remove()); |
1541 |
|
} |
1542 |
|
|
1543 |
|
/** |
1544 |
|
* drainTo(null) throws NPE |
1545 |
< |
*/ |
1545 |
> |
*/ |
1546 |
|
public void testDrainToNull() { |
1547 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1548 |
|
try { |
1549 |
|
q.drainTo(null); |
1550 |
|
shouldThrow(); |
1551 |
< |
} catch(NullPointerException success) { |
1808 |
< |
} |
1551 |
> |
} catch (NullPointerException success) {} |
1552 |
|
} |
1553 |
|
|
1554 |
|
/** |
1555 |
|
* drainTo(this) throws IAE |
1556 |
< |
*/ |
1556 |
> |
*/ |
1557 |
|
public void testDrainToSelf() { |
1558 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1559 |
|
try { |
1560 |
|
q.drainTo(q); |
1561 |
|
shouldThrow(); |
1562 |
< |
} catch(IllegalArgumentException success) { |
1820 |
< |
} |
1562 |
> |
} catch (IllegalArgumentException success) {} |
1563 |
|
} |
1564 |
|
|
1565 |
|
/** |
1566 |
|
* drainTo(c) empties deque into another collection c |
1567 |
< |
*/ |
1567 |
> |
*/ |
1568 |
|
public void testDrainTo() { |
1569 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1570 |
|
ArrayList l = new ArrayList(); |
1571 |
|
q.drainTo(l); |
1572 |
|
assertEquals(q.size(), 0); |
1573 |
|
assertEquals(l.size(), SIZE); |
1574 |
< |
for (int i = 0; i < SIZE; ++i) |
1574 |
> |
for (int i = 0; i < SIZE; ++i) |
1575 |
|
assertEquals(l.get(i), new Integer(i)); |
1576 |
|
q.add(zero); |
1577 |
|
q.add(one); |
1582 |
|
q.drainTo(l); |
1583 |
|
assertEquals(q.size(), 0); |
1584 |
|
assertEquals(l.size(), 2); |
1585 |
< |
for (int i = 0; i < 2; ++i) |
1585 |
> |
for (int i = 0; i < 2; ++i) |
1586 |
|
assertEquals(l.get(i), new Integer(i)); |
1587 |
|
} |
1588 |
|
|
1589 |
|
/** |
1590 |
|
* drainTo empties full deque, unblocking a waiting put. |
1591 |
< |
*/ |
1592 |
< |
public void testDrainToWithActivePut() { |
1591 |
> |
*/ |
1592 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
1593 |
|
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1594 |
< |
Thread t = new Thread(new Runnable() { |
1595 |
< |
public void run() { |
1596 |
< |
try { |
1597 |
< |
q.put(new Integer(SIZE+1)); |
1598 |
< |
} catch (InterruptedException ie){ |
1599 |
< |
threadUnexpectedException(); |
1600 |
< |
} |
1601 |
< |
} |
1602 |
< |
}); |
1603 |
< |
try { |
1604 |
< |
t.start(); |
1605 |
< |
ArrayList l = new ArrayList(); |
1606 |
< |
q.drainTo(l); |
1865 |
< |
assertTrue(l.size() >= SIZE); |
1866 |
< |
for (int i = 0; i < SIZE; ++i) |
1867 |
< |
assertEquals(l.get(i), new Integer(i)); |
1868 |
< |
t.join(); |
1869 |
< |
assertTrue(q.size() + l.size() >= SIZE); |
1870 |
< |
} catch(Exception e){ |
1871 |
< |
unexpectedException(); |
1872 |
< |
} |
1594 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1595 |
> |
public void realRun() throws InterruptedException { |
1596 |
> |
q.put(new Integer(SIZE+1)); |
1597 |
> |
}}); |
1598 |
> |
|
1599 |
> |
t.start(); |
1600 |
> |
ArrayList l = new ArrayList(); |
1601 |
> |
q.drainTo(l); |
1602 |
> |
assertTrue(l.size() >= SIZE); |
1603 |
> |
for (int i = 0; i < SIZE; ++i) |
1604 |
> |
assertEquals(l.get(i), new Integer(i)); |
1605 |
> |
t.join(); |
1606 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
1607 |
|
} |
1608 |
|
|
1609 |
|
/** |
1610 |
|
* drainTo(null, n) throws NPE |
1611 |
< |
*/ |
1611 |
> |
*/ |
1612 |
|
public void testDrainToNullN() { |
1613 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1614 |
|
try { |
1615 |
|
q.drainTo(null, 0); |
1616 |
|
shouldThrow(); |
1617 |
< |
} catch(NullPointerException success) { |
1884 |
< |
} |
1617 |
> |
} catch (NullPointerException success) {} |
1618 |
|
} |
1619 |
|
|
1620 |
|
/** |
1621 |
|
* drainTo(this, n) throws IAE |
1622 |
< |
*/ |
1622 |
> |
*/ |
1623 |
|
public void testDrainToSelfN() { |
1624 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1625 |
|
try { |
1626 |
|
q.drainTo(q, 0); |
1627 |
|
shouldThrow(); |
1628 |
< |
} catch(IllegalArgumentException success) { |
1896 |
< |
} |
1628 |
> |
} catch (IllegalArgumentException success) {} |
1629 |
|
} |
1630 |
|
|
1631 |
|
/** |
1632 |
< |
* drainTo(c, n) empties first max {n, size} elements of deque into c |
1633 |
< |
*/ |
1632 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
1633 |
> |
*/ |
1634 |
|
public void testDrainToN() { |
1635 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1636 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
1637 |
< |
for(int j = 0; j < SIZE; j++) |
1637 |
> |
for (int j = 0; j < SIZE; j++) |
1638 |
|
assertTrue(q.offer(new Integer(j))); |
1639 |
|
ArrayList l = new ArrayList(); |
1640 |
|
q.drainTo(l, i); |
1641 |
< |
int k = (i < SIZE)? i : SIZE; |
1641 |
> |
int k = (i < SIZE) ? i : SIZE; |
1642 |
|
assertEquals(l.size(), k); |
1643 |
|
assertEquals(q.size(), SIZE-k); |
1644 |
< |
for (int j = 0; j < k; ++j) |
1644 |
> |
for (int j = 0; j < k; ++j) |
1645 |
|
assertEquals(l.get(j), new Integer(j)); |
1646 |
|
while (q.poll() != null) ; |
1647 |
|
} |