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