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