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