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