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