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