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