1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
jsr166 |
1.44 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.1 |
* Other contributors include John Vint |
6 |
|
|
*/ |
7 |
|
|
|
8 |
jsr166 |
1.55 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
9 |
|
|
|
10 |
|
|
import java.util.ArrayList; |
11 |
jsr166 |
1.48 |
import java.util.Arrays; |
12 |
jsr166 |
1.47 |
import java.util.Collection; |
13 |
dl |
1.1 |
import java.util.Iterator; |
14 |
jsr166 |
1.12 |
import java.util.List; |
15 |
dl |
1.1 |
import java.util.NoSuchElementException; |
16 |
jsr166 |
1.48 |
import java.util.Queue; |
17 |
jsr166 |
1.47 |
import java.util.concurrent.BlockingQueue; |
18 |
jsr166 |
1.70 |
import java.util.concurrent.Callable; |
19 |
jsr166 |
1.47 |
import java.util.concurrent.CountDownLatch; |
20 |
|
|
import java.util.concurrent.Executors; |
21 |
|
|
import java.util.concurrent.ExecutorService; |
22 |
|
|
import java.util.concurrent.LinkedTransferQueue; |
23 |
jsr166 |
1.55 |
|
24 |
|
|
import junit.framework.Test; |
25 |
dl |
1.1 |
|
26 |
jsr166 |
1.10 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
27 |
dl |
1.1 |
public class LinkedTransferQueueTest extends JSR166TestCase { |
28 |
jsr166 |
1.25 |
public static class Generic extends BlockingQueueTest { |
29 |
|
|
protected BlockingQueue emptyCollection() { |
30 |
|
|
return new LinkedTransferQueue(); |
31 |
|
|
} |
32 |
|
|
} |
33 |
|
|
|
34 |
dl |
1.1 |
public static void main(String[] args) { |
35 |
jsr166 |
1.59 |
main(suite(), args); |
36 |
dl |
1.1 |
} |
37 |
|
|
|
38 |
|
|
public static Test suite() { |
39 |
jsr166 |
1.67 |
class Implementation implements CollectionImplementation { |
40 |
|
|
public Class<?> klazz() { return LinkedTransferQueue.class; } |
41 |
|
|
public Collection emptyCollection() { return new LinkedTransferQueue(); } |
42 |
dl |
1.86 |
public Object makeElement(int i) { return JSR166TestCase.itemFor(i); } |
43 |
jsr166 |
1.67 |
public boolean isConcurrent() { return true; } |
44 |
|
|
public boolean permitsNulls() { return false; } |
45 |
|
|
} |
46 |
jsr166 |
1.25 |
return newTestSuite(LinkedTransferQueueTest.class, |
47 |
jsr166 |
1.62 |
new Generic().testSuite(), |
48 |
|
|
CollectionTest.testSuite(new Implementation())); |
49 |
dl |
1.1 |
} |
50 |
|
|
|
51 |
jsr166 |
1.3 |
/** |
52 |
jsr166 |
1.7 |
* Constructor builds new queue with size being zero and empty |
53 |
|
|
* being true |
54 |
dl |
1.1 |
*/ |
55 |
|
|
public void testConstructor1() { |
56 |
dl |
1.86 |
mustEqual(0, new LinkedTransferQueue().size()); |
57 |
dl |
1.1 |
assertTrue(new LinkedTransferQueue().isEmpty()); |
58 |
|
|
} |
59 |
|
|
|
60 |
jsr166 |
1.3 |
/** |
61 |
jsr166 |
1.7 |
* Initializing constructor with null collection throws |
62 |
|
|
* NullPointerException |
63 |
dl |
1.1 |
*/ |
64 |
|
|
public void testConstructor2() { |
65 |
|
|
try { |
66 |
|
|
new LinkedTransferQueue(null); |
67 |
|
|
shouldThrow(); |
68 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
69 |
dl |
1.1 |
} |
70 |
|
|
|
71 |
|
|
/** |
72 |
jsr166 |
1.7 |
* Initializing from Collection of null elements throws |
73 |
|
|
* NullPointerException |
74 |
dl |
1.1 |
*/ |
75 |
|
|
public void testConstructor3() { |
76 |
dl |
1.86 |
Collection<Item> elements = Arrays.asList(new Item[SIZE]); |
77 |
dl |
1.1 |
try { |
78 |
jsr166 |
1.47 |
new LinkedTransferQueue(elements); |
79 |
dl |
1.1 |
shouldThrow(); |
80 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
81 |
dl |
1.1 |
} |
82 |
jsr166 |
1.3 |
|
83 |
|
|
/** |
84 |
dl |
1.1 |
* Initializing constructor with a collection containing some null elements |
85 |
jsr166 |
1.5 |
* throws NullPointerException |
86 |
dl |
1.1 |
*/ |
87 |
|
|
public void testConstructor4() { |
88 |
dl |
1.86 |
Item[] items = new Item[2]; |
89 |
|
|
items[0] = zero; |
90 |
|
|
Collection<Item> elements = Arrays.asList(items); |
91 |
dl |
1.1 |
try { |
92 |
jsr166 |
1.47 |
new LinkedTransferQueue(elements); |
93 |
dl |
1.1 |
shouldThrow(); |
94 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
95 |
dl |
1.1 |
} |
96 |
|
|
|
97 |
jsr166 |
1.3 |
/** |
98 |
dl |
1.1 |
* Queue contains all elements of the collection it is initialized by |
99 |
|
|
*/ |
100 |
|
|
public void testConstructor5() { |
101 |
dl |
1.86 |
Item[] items = defaultItems; |
102 |
|
|
List<Item> intList = Arrays.asList(items); |
103 |
jsr166 |
1.87 |
LinkedTransferQueue<Item> q = new LinkedTransferQueue<>(intList); |
104 |
dl |
1.86 |
mustEqual(q.size(), intList.size()); |
105 |
|
|
mustEqual(q.toString(), intList.toString()); |
106 |
jsr166 |
1.12 |
assertTrue(Arrays.equals(q.toArray(), |
107 |
|
|
intList.toArray())); |
108 |
|
|
assertTrue(Arrays.equals(q.toArray(new Object[0]), |
109 |
|
|
intList.toArray(new Object[0]))); |
110 |
|
|
assertTrue(Arrays.equals(q.toArray(new Object[SIZE]), |
111 |
|
|
intList.toArray(new Object[SIZE]))); |
112 |
|
|
for (int i = 0; i < SIZE; ++i) { |
113 |
dl |
1.86 |
mustEqual(items[i], q.poll()); |
114 |
dl |
1.1 |
} |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
/** |
118 |
jsr166 |
1.12 |
* remainingCapacity() always returns Integer.MAX_VALUE |
119 |
dl |
1.1 |
*/ |
120 |
|
|
public void testRemainingCapacity() { |
121 |
dl |
1.86 |
BlockingQueue<Item> q = populatedQueue(SIZE); |
122 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
123 |
dl |
1.86 |
mustEqual(Integer.MAX_VALUE, q.remainingCapacity()); |
124 |
|
|
mustEqual(SIZE - i, q.size()); |
125 |
|
|
mustEqual(i, q.remove()); |
126 |
dl |
1.1 |
} |
127 |
|
|
for (int i = 0; i < SIZE; ++i) { |
128 |
dl |
1.86 |
mustEqual(Integer.MAX_VALUE, q.remainingCapacity()); |
129 |
|
|
mustEqual(i, q.size()); |
130 |
|
|
mustAdd(q, i); |
131 |
dl |
1.1 |
} |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
/** |
135 |
jsr166 |
1.5 |
* addAll(this) throws IllegalArgumentException |
136 |
dl |
1.1 |
*/ |
137 |
|
|
public void testAddAllSelf() { |
138 |
dl |
1.86 |
LinkedTransferQueue<Item> q = populatedQueue(SIZE); |
139 |
dl |
1.1 |
try { |
140 |
|
|
q.addAll(q); |
141 |
|
|
shouldThrow(); |
142 |
jsr166 |
1.17 |
} catch (IllegalArgumentException success) {} |
143 |
dl |
1.1 |
} |
144 |
|
|
|
145 |
|
|
/** |
146 |
jsr166 |
1.7 |
* addAll of a collection with any null elements throws |
147 |
|
|
* NullPointerException after possibly adding some elements |
148 |
dl |
1.1 |
*/ |
149 |
|
|
public void testAddAll3() { |
150 |
jsr166 |
1.87 |
LinkedTransferQueue<Item> q = new LinkedTransferQueue<>(); |
151 |
dl |
1.86 |
Item[] items = new Item[2]; items[0] = zero; |
152 |
dl |
1.1 |
try { |
153 |
dl |
1.86 |
q.addAll(Arrays.asList(items)); |
154 |
dl |
1.1 |
shouldThrow(); |
155 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
156 |
dl |
1.1 |
} |
157 |
|
|
|
158 |
|
|
/** |
159 |
|
|
* Queue contains all elements, in traversal order, of successful addAll |
160 |
|
|
*/ |
161 |
|
|
public void testAddAll5() { |
162 |
dl |
1.86 |
Item[] empty = new Item[0]; |
163 |
|
|
Item[] items = defaultItems; |
164 |
jsr166 |
1.5 |
LinkedTransferQueue q = new LinkedTransferQueue(); |
165 |
|
|
assertFalse(q.addAll(Arrays.asList(empty))); |
166 |
dl |
1.86 |
assertTrue(q.addAll(Arrays.asList(items))); |
167 |
jsr166 |
1.5 |
for (int i = 0; i < SIZE; ++i) { |
168 |
dl |
1.86 |
mustEqual(items[i], q.poll()); |
169 |
dl |
1.1 |
} |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
/** |
173 |
|
|
* all elements successfully put are contained |
174 |
|
|
*/ |
175 |
|
|
public void testPut() { |
176 |
dl |
1.86 |
LinkedTransferQueue<Item> q = new LinkedTransferQueue<>(); |
177 |
|
|
Item[] items = defaultItems; |
178 |
jsr166 |
1.5 |
for (int i = 0; i < SIZE; ++i) { |
179 |
dl |
1.86 |
mustEqual(i, q.size()); |
180 |
|
|
q.put(items[i]); |
181 |
|
|
mustContain(q, items[i]); |
182 |
dl |
1.1 |
} |
183 |
|
|
} |
184 |
|
|
|
185 |
|
|
/** |
186 |
|
|
* take retrieves elements in FIFO order |
187 |
|
|
*/ |
188 |
jsr166 |
1.5 |
public void testTake() throws InterruptedException { |
189 |
dl |
1.86 |
LinkedTransferQueue<Item> q = populatedQueue(SIZE); |
190 |
jsr166 |
1.5 |
for (int i = 0; i < SIZE; ++i) { |
191 |
dl |
1.86 |
mustEqual(i, q.take()); |
192 |
dl |
1.1 |
} |
193 |
|
|
} |
194 |
|
|
|
195 |
|
|
/** |
196 |
jsr166 |
1.28 |
* take removes existing elements until empty, then blocks interruptibly |
197 |
dl |
1.1 |
*/ |
198 |
jsr166 |
1.5 |
public void testBlockingTake() throws InterruptedException { |
199 |
dl |
1.86 |
final BlockingQueue<Item> q = populatedQueue(SIZE); |
200 |
jsr166 |
1.46 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
201 |
jsr166 |
1.31 |
Thread t = newStartedThread(new CheckedRunnable() { |
202 |
jsr166 |
1.19 |
public void realRun() throws InterruptedException { |
203 |
dl |
1.86 |
for (int i = 0; i < SIZE; i++) mustEqual(i, q.take()); |
204 |
jsr166 |
1.46 |
|
205 |
|
|
Thread.currentThread().interrupt(); |
206 |
|
|
try { |
207 |
|
|
q.take(); |
208 |
|
|
shouldThrow(); |
209 |
|
|
} catch (InterruptedException success) {} |
210 |
|
|
assertFalse(Thread.interrupted()); |
211 |
|
|
|
212 |
|
|
pleaseInterrupt.countDown(); |
213 |
jsr166 |
1.19 |
try { |
214 |
|
|
q.take(); |
215 |
|
|
shouldThrow(); |
216 |
|
|
} catch (InterruptedException success) {} |
217 |
jsr166 |
1.46 |
assertFalse(Thread.interrupted()); |
218 |
jsr166 |
1.5 |
}}); |
219 |
jsr166 |
1.19 |
|
220 |
jsr166 |
1.46 |
await(pleaseInterrupt); |
221 |
jsr166 |
1.82 |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
222 |
jsr166 |
1.5 |
t.interrupt(); |
223 |
jsr166 |
1.46 |
awaitTermination(t); |
224 |
dl |
1.1 |
} |
225 |
|
|
|
226 |
|
|
/** |
227 |
|
|
* poll succeeds unless empty |
228 |
|
|
*/ |
229 |
jsr166 |
1.12 |
public void testPoll() throws InterruptedException { |
230 |
dl |
1.86 |
LinkedTransferQueue<Item> q = populatedQueue(SIZE); |
231 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
232 |
dl |
1.86 |
mustEqual(i, q.poll()); |
233 |
dl |
1.1 |
} |
234 |
|
|
assertNull(q.poll()); |
235 |
jsr166 |
1.12 |
checkEmpty(q); |
236 |
dl |
1.1 |
} |
237 |
|
|
|
238 |
|
|
/** |
239 |
jsr166 |
1.30 |
* timed poll with zero timeout succeeds when non-empty, else times out |
240 |
dl |
1.1 |
*/ |
241 |
jsr166 |
1.5 |
public void testTimedPoll0() throws InterruptedException { |
242 |
dl |
1.86 |
LinkedTransferQueue<Item> q = populatedQueue(SIZE); |
243 |
jsr166 |
1.5 |
for (int i = 0; i < SIZE; ++i) { |
244 |
dl |
1.86 |
mustEqual(i, q.poll(0, MILLISECONDS)); |
245 |
dl |
1.1 |
} |
246 |
jsr166 |
1.7 |
assertNull(q.poll(0, MILLISECONDS)); |
247 |
jsr166 |
1.12 |
checkEmpty(q); |
248 |
dl |
1.1 |
} |
249 |
|
|
|
250 |
|
|
/** |
251 |
jsr166 |
1.30 |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
252 |
dl |
1.1 |
*/ |
253 |
jsr166 |
1.5 |
public void testTimedPoll() throws InterruptedException { |
254 |
dl |
1.86 |
LinkedTransferQueue<Item> q = populatedQueue(SIZE); |
255 |
jsr166 |
1.66 |
long startTime = System.nanoTime(); |
256 |
|
|
for (int i = 0; i < SIZE; ++i) |
257 |
dl |
1.86 |
mustEqual(i, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
258 |
jsr166 |
1.66 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
259 |
|
|
|
260 |
|
|
startTime = System.nanoTime(); |
261 |
jsr166 |
1.46 |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
262 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
263 |
jsr166 |
1.12 |
checkEmpty(q); |
264 |
dl |
1.1 |
} |
265 |
|
|
|
266 |
|
|
/** |
267 |
|
|
* Interrupted timed poll throws InterruptedException instead of |
268 |
|
|
* returning timeout status |
269 |
|
|
*/ |
270 |
jsr166 |
1.5 |
public void testInterruptedTimedPoll() throws InterruptedException { |
271 |
dl |
1.86 |
final BlockingQueue<Item> q = populatedQueue(SIZE); |
272 |
jsr166 |
1.76 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
273 |
jsr166 |
1.16 |
Thread t = newStartedThread(new CheckedRunnable() { |
274 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
275 |
jsr166 |
1.78 |
for (int i = 0; i < SIZE; i++) |
276 |
dl |
1.86 |
mustEqual(i, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
277 |
jsr166 |
1.76 |
|
278 |
jsr166 |
1.78 |
Thread.currentThread().interrupt(); |
279 |
|
|
try { |
280 |
jsr166 |
1.82 |
q.poll(randomTimeout(), randomTimeUnit()); |
281 |
jsr166 |
1.78 |
shouldThrow(); |
282 |
|
|
} catch (InterruptedException success) {} |
283 |
|
|
assertFalse(Thread.interrupted()); |
284 |
|
|
|
285 |
jsr166 |
1.76 |
pleaseInterrupt.countDown(); |
286 |
jsr166 |
1.16 |
try { |
287 |
jsr166 |
1.83 |
q.poll(LONGER_DELAY_MS, MILLISECONDS); |
288 |
jsr166 |
1.17 |
shouldThrow(); |
289 |
jsr166 |
1.66 |
} catch (InterruptedException success) {} |
290 |
jsr166 |
1.76 |
assertFalse(Thread.interrupted()); |
291 |
jsr166 |
1.5 |
}}); |
292 |
jsr166 |
1.17 |
|
293 |
jsr166 |
1.76 |
await(pleaseInterrupt); |
294 |
jsr166 |
1.82 |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
295 |
jsr166 |
1.5 |
t.interrupt(); |
296 |
jsr166 |
1.64 |
awaitTermination(t); |
297 |
jsr166 |
1.12 |
checkEmpty(q); |
298 |
dl |
1.1 |
} |
299 |
|
|
|
300 |
|
|
/** |
301 |
jsr166 |
1.31 |
* timed poll after thread interrupted throws InterruptedException |
302 |
|
|
* instead of returning timeout status |
303 |
|
|
*/ |
304 |
|
|
public void testTimedPollAfterInterrupt() throws InterruptedException { |
305 |
dl |
1.86 |
final BlockingQueue<Item> q = populatedQueue(SIZE); |
306 |
jsr166 |
1.31 |
Thread t = newStartedThread(new CheckedRunnable() { |
307 |
|
|
public void realRun() throws InterruptedException { |
308 |
|
|
Thread.currentThread().interrupt(); |
309 |
jsr166 |
1.66 |
for (int i = 0; i < SIZE; ++i) |
310 |
dl |
1.86 |
mustEqual(i, q.poll(randomTimeout(), randomTimeUnit())); |
311 |
jsr166 |
1.31 |
try { |
312 |
jsr166 |
1.82 |
q.poll(randomTimeout(), randomTimeUnit()); |
313 |
jsr166 |
1.31 |
shouldThrow(); |
314 |
|
|
} catch (InterruptedException success) {} |
315 |
jsr166 |
1.77 |
assertFalse(Thread.interrupted()); |
316 |
jsr166 |
1.31 |
}}); |
317 |
|
|
|
318 |
jsr166 |
1.66 |
awaitTermination(t); |
319 |
jsr166 |
1.31 |
checkEmpty(q); |
320 |
|
|
} |
321 |
|
|
|
322 |
|
|
/** |
323 |
dl |
1.1 |
* peek returns next element, or null if empty |
324 |
|
|
*/ |
325 |
jsr166 |
1.12 |
public void testPeek() throws InterruptedException { |
326 |
dl |
1.86 |
LinkedTransferQueue<Item> q = populatedQueue(SIZE); |
327 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
328 |
dl |
1.86 |
mustEqual(i, q.peek()); |
329 |
|
|
mustEqual(i, q.poll()); |
330 |
dl |
1.1 |
assertTrue(q.peek() == null || |
331 |
dl |
1.86 |
i != q.peek().value); |
332 |
dl |
1.1 |
} |
333 |
|
|
assertNull(q.peek()); |
334 |
jsr166 |
1.12 |
checkEmpty(q); |
335 |
dl |
1.1 |
} |
336 |
|
|
|
337 |
|
|
/** |
338 |
jsr166 |
1.5 |
* element returns next element, or throws NoSuchElementException if empty |
339 |
dl |
1.1 |
*/ |
340 |
jsr166 |
1.12 |
public void testElement() throws InterruptedException { |
341 |
dl |
1.86 |
LinkedTransferQueue<Item> q = populatedQueue(SIZE); |
342 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
343 |
dl |
1.86 |
mustEqual(i, q.element()); |
344 |
|
|
mustEqual(i, q.poll()); |
345 |
dl |
1.1 |
} |
346 |
|
|
try { |
347 |
|
|
q.element(); |
348 |
|
|
shouldThrow(); |
349 |
jsr166 |
1.17 |
} catch (NoSuchElementException success) {} |
350 |
jsr166 |
1.12 |
checkEmpty(q); |
351 |
dl |
1.1 |
} |
352 |
|
|
|
353 |
|
|
/** |
354 |
jsr166 |
1.5 |
* remove removes next element, or throws NoSuchElementException if empty |
355 |
dl |
1.1 |
*/ |
356 |
jsr166 |
1.12 |
public void testRemove() throws InterruptedException { |
357 |
dl |
1.86 |
LinkedTransferQueue<Item> q = populatedQueue(SIZE); |
358 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
359 |
dl |
1.86 |
mustEqual(i, q.remove()); |
360 |
dl |
1.1 |
} |
361 |
|
|
try { |
362 |
|
|
q.remove(); |
363 |
|
|
shouldThrow(); |
364 |
jsr166 |
1.17 |
} catch (NoSuchElementException success) {} |
365 |
jsr166 |
1.12 |
checkEmpty(q); |
366 |
dl |
1.1 |
} |
367 |
|
|
|
368 |
|
|
/** |
369 |
|
|
* An add following remove(x) succeeds |
370 |
|
|
*/ |
371 |
jsr166 |
1.5 |
public void testRemoveElementAndAdd() throws InterruptedException { |
372 |
jsr166 |
1.87 |
LinkedTransferQueue<Item> q = new LinkedTransferQueue<>(); |
373 |
dl |
1.86 |
mustAdd(q, one); |
374 |
|
|
mustAdd(q, two); |
375 |
|
|
mustRemove(q, one); |
376 |
|
|
mustRemove(q, two); |
377 |
|
|
mustAdd(q, three); |
378 |
|
|
mustEqual(q.take(), three); |
379 |
dl |
1.1 |
} |
380 |
|
|
|
381 |
|
|
/** |
382 |
|
|
* contains(x) reports true when elements added but not yet removed |
383 |
|
|
*/ |
384 |
|
|
public void testContains() { |
385 |
dl |
1.86 |
LinkedTransferQueue<Item> q = populatedQueue(SIZE); |
386 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
387 |
dl |
1.86 |
mustContain(q, i); |
388 |
|
|
mustEqual(i, q.poll()); |
389 |
|
|
mustNotContain(q, i); |
390 |
dl |
1.1 |
} |
391 |
|
|
} |
392 |
|
|
|
393 |
|
|
/** |
394 |
|
|
* clear removes all elements |
395 |
|
|
*/ |
396 |
jsr166 |
1.12 |
public void testClear() throws InterruptedException { |
397 |
dl |
1.86 |
LinkedTransferQueue<Item> q = populatedQueue(SIZE); |
398 |
dl |
1.1 |
q.clear(); |
399 |
jsr166 |
1.12 |
checkEmpty(q); |
400 |
dl |
1.86 |
mustEqual(Integer.MAX_VALUE, q.remainingCapacity()); |
401 |
dl |
1.1 |
q.add(one); |
402 |
|
|
assertFalse(q.isEmpty()); |
403 |
dl |
1.86 |
mustEqual(1, q.size()); |
404 |
|
|
mustContain(q, one); |
405 |
dl |
1.1 |
q.clear(); |
406 |
jsr166 |
1.12 |
checkEmpty(q); |
407 |
dl |
1.1 |
} |
408 |
|
|
|
409 |
|
|
/** |
410 |
|
|
* containsAll(c) is true when c contains a subset of elements |
411 |
|
|
*/ |
412 |
|
|
public void testContainsAll() { |
413 |
dl |
1.86 |
LinkedTransferQueue<Item> q = populatedQueue(SIZE); |
414 |
|
|
LinkedTransferQueue<Item> p = new LinkedTransferQueue<>(); |
415 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
416 |
|
|
assertTrue(q.containsAll(p)); |
417 |
|
|
assertFalse(p.containsAll(q)); |
418 |
dl |
1.86 |
mustAdd(p, i); |
419 |
dl |
1.1 |
} |
420 |
|
|
assertTrue(p.containsAll(q)); |
421 |
|
|
} |
422 |
|
|
|
423 |
|
|
/** |
424 |
jsr166 |
1.7 |
* retainAll(c) retains only those elements of c and reports true |
425 |
|
|
* if changed |
426 |
dl |
1.1 |
*/ |
427 |
|
|
public void testRetainAll() { |
428 |
dl |
1.86 |
LinkedTransferQueue<Item> q = populatedQueue(SIZE); |
429 |
|
|
LinkedTransferQueue<Item> p = populatedQueue(SIZE); |
430 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
431 |
|
|
boolean changed = q.retainAll(p); |
432 |
|
|
if (i == 0) { |
433 |
|
|
assertFalse(changed); |
434 |
|
|
} else { |
435 |
|
|
assertTrue(changed); |
436 |
|
|
} |
437 |
|
|
assertTrue(q.containsAll(p)); |
438 |
dl |
1.86 |
mustEqual(SIZE - i, q.size()); |
439 |
dl |
1.1 |
p.remove(); |
440 |
|
|
} |
441 |
|
|
} |
442 |
|
|
|
443 |
|
|
/** |
444 |
jsr166 |
1.7 |
* removeAll(c) removes only those elements of c and reports true |
445 |
|
|
* if changed |
446 |
dl |
1.1 |
*/ |
447 |
|
|
public void testRemoveAll() { |
448 |
|
|
for (int i = 1; i < SIZE; ++i) { |
449 |
dl |
1.86 |
LinkedTransferQueue<Item> q = populatedQueue(SIZE); |
450 |
|
|
LinkedTransferQueue<Item> p = populatedQueue(i); |
451 |
dl |
1.1 |
assertTrue(q.removeAll(p)); |
452 |
dl |
1.86 |
mustEqual(SIZE - i, q.size()); |
453 |
dl |
1.1 |
for (int j = 0; j < i; ++j) { |
454 |
dl |
1.86 |
mustNotContain(q, p.remove()); |
455 |
dl |
1.1 |
} |
456 |
|
|
} |
457 |
|
|
} |
458 |
|
|
|
459 |
|
|
/** |
460 |
jsr166 |
1.38 |
* toArray() contains all elements in FIFO order |
461 |
dl |
1.1 |
*/ |
462 |
jsr166 |
1.38 |
public void testToArray() { |
463 |
dl |
1.86 |
LinkedTransferQueue<Item> q = populatedQueue(SIZE); |
464 |
jsr166 |
1.81 |
Object[] a = q.toArray(); |
465 |
|
|
assertSame(Object[].class, a.getClass()); |
466 |
|
|
for (Object o : a) |
467 |
|
|
assertSame(o, q.poll()); |
468 |
|
|
assertTrue(q.isEmpty()); |
469 |
dl |
1.1 |
} |
470 |
|
|
|
471 |
|
|
/** |
472 |
jsr166 |
1.38 |
* toArray(a) contains all elements in FIFO order |
473 |
dl |
1.1 |
*/ |
474 |
jsr166 |
1.38 |
public void testToArray2() { |
475 |
dl |
1.86 |
LinkedTransferQueue<Item> q = populatedQueue(SIZE); |
476 |
|
|
Item[] items = new Item[SIZE]; |
477 |
|
|
Item[] array = q.toArray(items); |
478 |
|
|
assertSame(items, array); |
479 |
|
|
for (Item o : items) |
480 |
jsr166 |
1.81 |
assertSame(o, q.poll()); |
481 |
|
|
assertTrue(q.isEmpty()); |
482 |
dl |
1.1 |
} |
483 |
|
|
|
484 |
|
|
/** |
485 |
jsr166 |
1.36 |
* toArray(incompatible array type) throws ArrayStoreException |
486 |
dl |
1.1 |
*/ |
487 |
|
|
public void testToArray1_BadArg() { |
488 |
dl |
1.86 |
LinkedTransferQueue<Item> q = populatedQueue(SIZE); |
489 |
dl |
1.1 |
try { |
490 |
jsr166 |
1.36 |
q.toArray(new String[10]); |
491 |
dl |
1.1 |
shouldThrow(); |
492 |
jsr166 |
1.17 |
} catch (ArrayStoreException success) {} |
493 |
dl |
1.1 |
} |
494 |
|
|
|
495 |
|
|
/** |
496 |
|
|
* iterator iterates through all elements |
497 |
|
|
*/ |
498 |
jsr166 |
1.5 |
public void testIterator() throws InterruptedException { |
499 |
dl |
1.86 |
LinkedTransferQueue<Item> q = populatedQueue(SIZE); |
500 |
|
|
Iterator<? extends Item> it = q.iterator(); |
501 |
jsr166 |
1.57 |
int i; |
502 |
|
|
for (i = 0; it.hasNext(); i++) |
503 |
dl |
1.86 |
mustContain(q, it.next()); |
504 |
|
|
mustEqual(i, SIZE); |
505 |
jsr166 |
1.57 |
assertIteratorExhausted(it); |
506 |
|
|
|
507 |
|
|
it = q.iterator(); |
508 |
|
|
for (i = 0; it.hasNext(); i++) |
509 |
dl |
1.86 |
mustEqual(it.next(), q.take()); |
510 |
|
|
mustEqual(i, SIZE); |
511 |
jsr166 |
1.57 |
assertIteratorExhausted(it); |
512 |
|
|
} |
513 |
|
|
|
514 |
|
|
/** |
515 |
|
|
* iterator of empty collection has no elements |
516 |
|
|
*/ |
517 |
|
|
public void testEmptyIterator() { |
518 |
|
|
assertIteratorExhausted(new LinkedTransferQueue().iterator()); |
519 |
dl |
1.1 |
} |
520 |
|
|
|
521 |
|
|
/** |
522 |
jsr166 |
1.12 |
* iterator.remove() removes current element |
523 |
dl |
1.1 |
*/ |
524 |
|
|
public void testIteratorRemove() { |
525 |
jsr166 |
1.87 |
final LinkedTransferQueue<Item> q = new LinkedTransferQueue<>(); |
526 |
dl |
1.1 |
q.add(two); |
527 |
|
|
q.add(one); |
528 |
|
|
q.add(three); |
529 |
|
|
|
530 |
dl |
1.86 |
Iterator<? extends Item> it = q.iterator(); |
531 |
dl |
1.1 |
it.next(); |
532 |
|
|
it.remove(); |
533 |
|
|
|
534 |
|
|
it = q.iterator(); |
535 |
jsr166 |
1.24 |
assertSame(it.next(), one); |
536 |
|
|
assertSame(it.next(), three); |
537 |
dl |
1.1 |
assertFalse(it.hasNext()); |
538 |
|
|
} |
539 |
|
|
|
540 |
|
|
/** |
541 |
|
|
* iterator ordering is FIFO |
542 |
|
|
*/ |
543 |
|
|
public void testIteratorOrdering() { |
544 |
dl |
1.86 |
final LinkedTransferQueue<Item> q = new LinkedTransferQueue<>(); |
545 |
|
|
mustEqual(Integer.MAX_VALUE, q.remainingCapacity()); |
546 |
dl |
1.1 |
q.add(one); |
547 |
|
|
q.add(two); |
548 |
|
|
q.add(three); |
549 |
dl |
1.86 |
mustEqual(Integer.MAX_VALUE, q.remainingCapacity()); |
550 |
dl |
1.1 |
int k = 0; |
551 |
dl |
1.86 |
for (Item n : q) { |
552 |
|
|
mustEqual(++k, n); |
553 |
dl |
1.1 |
} |
554 |
dl |
1.86 |
mustEqual(3, k); |
555 |
dl |
1.1 |
} |
556 |
|
|
|
557 |
|
|
/** |
558 |
|
|
* Modifications do not cause iterators to fail |
559 |
|
|
*/ |
560 |
|
|
public void testWeaklyConsistentIteration() { |
561 |
jsr166 |
1.87 |
final LinkedTransferQueue<Item> q = new LinkedTransferQueue<>(); |
562 |
dl |
1.1 |
q.add(one); |
563 |
|
|
q.add(two); |
564 |
|
|
q.add(three); |
565 |
dl |
1.86 |
for (Iterator<? extends Item> it = q.iterator(); it.hasNext();) { |
566 |
jsr166 |
1.5 |
q.remove(); |
567 |
|
|
it.next(); |
568 |
dl |
1.1 |
} |
569 |
dl |
1.86 |
mustEqual(0, q.size()); |
570 |
dl |
1.1 |
} |
571 |
|
|
|
572 |
|
|
/** |
573 |
|
|
* toString contains toStrings of elements |
574 |
|
|
*/ |
575 |
|
|
public void testToString() { |
576 |
dl |
1.86 |
LinkedTransferQueue<Item> q = populatedQueue(SIZE); |
577 |
dl |
1.1 |
String s = q.toString(); |
578 |
|
|
for (int i = 0; i < SIZE; ++i) { |
579 |
jsr166 |
1.46 |
assertTrue(s.contains(String.valueOf(i))); |
580 |
dl |
1.1 |
} |
581 |
|
|
} |
582 |
|
|
|
583 |
|
|
/** |
584 |
|
|
* offer transfers elements across Executor tasks |
585 |
|
|
*/ |
586 |
|
|
public void testOfferInExecutor() { |
587 |
jsr166 |
1.87 |
final LinkedTransferQueue<Item> q = new LinkedTransferQueue<>(); |
588 |
jsr166 |
1.46 |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
589 |
jsr166 |
1.63 |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
590 |
|
|
try (PoolCleaner cleaner = cleaner(executor)) { |
591 |
jsr166 |
1.7 |
|
592 |
jsr166 |
1.63 |
executor.execute(new CheckedRunnable() { |
593 |
|
|
public void realRun() throws InterruptedException { |
594 |
|
|
threadsStarted.await(); |
595 |
jsr166 |
1.66 |
long startTime = System.nanoTime(); |
596 |
jsr166 |
1.63 |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
597 |
jsr166 |
1.66 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
598 |
jsr166 |
1.63 |
}}); |
599 |
dl |
1.1 |
|
600 |
jsr166 |
1.63 |
executor.execute(new CheckedRunnable() { |
601 |
|
|
public void realRun() throws InterruptedException { |
602 |
|
|
threadsStarted.await(); |
603 |
|
|
assertSame(one, q.take()); |
604 |
|
|
checkEmpty(q); |
605 |
|
|
}}); |
606 |
|
|
} |
607 |
dl |
1.1 |
} |
608 |
|
|
|
609 |
|
|
/** |
610 |
jsr166 |
1.13 |
* timed poll retrieves elements across Executor threads |
611 |
dl |
1.1 |
*/ |
612 |
|
|
public void testPollInExecutor() { |
613 |
jsr166 |
1.87 |
final LinkedTransferQueue<Item> q = new LinkedTransferQueue<>(); |
614 |
jsr166 |
1.46 |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
615 |
jsr166 |
1.63 |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
616 |
|
|
try (PoolCleaner cleaner = cleaner(executor)) { |
617 |
jsr166 |
1.7 |
|
618 |
jsr166 |
1.63 |
executor.execute(new CheckedRunnable() { |
619 |
|
|
public void realRun() throws InterruptedException { |
620 |
|
|
assertNull(q.poll()); |
621 |
|
|
threadsStarted.await(); |
622 |
jsr166 |
1.66 |
long startTime = System.nanoTime(); |
623 |
jsr166 |
1.63 |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
624 |
jsr166 |
1.66 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
625 |
jsr166 |
1.63 |
checkEmpty(q); |
626 |
|
|
}}); |
627 |
dl |
1.1 |
|
628 |
jsr166 |
1.63 |
executor.execute(new CheckedRunnable() { |
629 |
|
|
public void realRun() throws InterruptedException { |
630 |
|
|
threadsStarted.await(); |
631 |
|
|
q.put(one); |
632 |
|
|
}}); |
633 |
|
|
} |
634 |
dl |
1.1 |
} |
635 |
|
|
|
636 |
|
|
/** |
637 |
jsr166 |
1.79 |
* A deserialized/reserialized queue has same elements in same order |
638 |
dl |
1.1 |
*/ |
639 |
jsr166 |
1.5 |
public void testSerialization() throws Exception { |
640 |
dl |
1.86 |
Queue<Item> x = populatedQueue(SIZE); |
641 |
|
|
Queue<Item> y = serialClone(x); |
642 |
dl |
1.1 |
|
643 |
jsr166 |
1.52 |
assertNotSame(y, x); |
644 |
dl |
1.86 |
mustEqual(x.size(), y.size()); |
645 |
|
|
mustEqual(x.toString(), y.toString()); |
646 |
jsr166 |
1.48 |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
647 |
|
|
while (!x.isEmpty()) { |
648 |
|
|
assertFalse(y.isEmpty()); |
649 |
dl |
1.86 |
mustEqual(x.remove(), y.remove()); |
650 |
dl |
1.1 |
} |
651 |
jsr166 |
1.48 |
assertTrue(y.isEmpty()); |
652 |
dl |
1.1 |
} |
653 |
|
|
|
654 |
|
|
/** |
655 |
|
|
* drainTo(c) empties queue into another collection c |
656 |
|
|
*/ |
657 |
|
|
public void testDrainTo() { |
658 |
dl |
1.86 |
LinkedTransferQueue<Item> q = populatedQueue(SIZE); |
659 |
dl |
1.1 |
ArrayList l = new ArrayList(); |
660 |
|
|
q.drainTo(l); |
661 |
dl |
1.86 |
mustEqual(0, q.size()); |
662 |
|
|
mustEqual(SIZE, l.size()); |
663 |
dl |
1.1 |
for (int i = 0; i < SIZE; ++i) { |
664 |
dl |
1.86 |
mustEqual(i, l.get(i)); |
665 |
dl |
1.1 |
} |
666 |
|
|
q.add(zero); |
667 |
|
|
q.add(one); |
668 |
|
|
assertFalse(q.isEmpty()); |
669 |
dl |
1.86 |
mustContain(q, zero); |
670 |
|
|
mustContain(q, one); |
671 |
dl |
1.1 |
l.clear(); |
672 |
|
|
q.drainTo(l); |
673 |
dl |
1.86 |
mustEqual(0, q.size()); |
674 |
|
|
mustEqual(2, l.size()); |
675 |
dl |
1.1 |
for (int i = 0; i < 2; ++i) { |
676 |
dl |
1.86 |
mustEqual(i, l.get(i)); |
677 |
dl |
1.1 |
} |
678 |
|
|
} |
679 |
|
|
|
680 |
|
|
/** |
681 |
jsr166 |
1.13 |
* drainTo(c) empties full queue, unblocking a waiting put. |
682 |
dl |
1.1 |
*/ |
683 |
jsr166 |
1.5 |
public void testDrainToWithActivePut() throws InterruptedException { |
684 |
dl |
1.86 |
final LinkedTransferQueue<Item> q = populatedQueue(SIZE); |
685 |
jsr166 |
1.9 |
Thread t = newStartedThread(new CheckedRunnable() { |
686 |
jsr166 |
1.21 |
public void realRun() { |
687 |
dl |
1.86 |
q.put(new Item(SIZE + 1)); |
688 |
jsr166 |
1.5 |
}}); |
689 |
|
|
ArrayList l = new ArrayList(); |
690 |
|
|
q.drainTo(l); |
691 |
|
|
assertTrue(l.size() >= SIZE); |
692 |
jsr166 |
1.51 |
for (int i = 0; i < SIZE; ++i) |
693 |
dl |
1.86 |
mustEqual(i, l.get(i)); |
694 |
jsr166 |
1.66 |
awaitTermination(t); |
695 |
jsr166 |
1.5 |
assertTrue(q.size() + l.size() >= SIZE); |
696 |
dl |
1.1 |
} |
697 |
|
|
|
698 |
|
|
/** |
699 |
jsr166 |
1.26 |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
700 |
dl |
1.1 |
*/ |
701 |
|
|
public void testDrainToN() { |
702 |
jsr166 |
1.87 |
LinkedTransferQueue<Item> q = new LinkedTransferQueue<>(); |
703 |
dl |
1.1 |
for (int i = 0; i < SIZE + 2; ++i) { |
704 |
|
|
for (int j = 0; j < SIZE; j++) { |
705 |
dl |
1.86 |
mustOffer(q, j); |
706 |
dl |
1.1 |
} |
707 |
|
|
ArrayList l = new ArrayList(); |
708 |
|
|
q.drainTo(l, i); |
709 |
|
|
int k = (i < SIZE) ? i : SIZE; |
710 |
dl |
1.86 |
mustEqual(k, l.size()); |
711 |
|
|
mustEqual(SIZE - k, q.size()); |
712 |
jsr166 |
1.51 |
for (int j = 0; j < k; ++j) |
713 |
dl |
1.86 |
mustEqual(j, l.get(j)); |
714 |
jsr166 |
1.56 |
do {} while (q.poll() != null); |
715 |
dl |
1.1 |
} |
716 |
|
|
} |
717 |
|
|
|
718 |
jsr166 |
1.3 |
/** |
719 |
jsr166 |
1.13 |
* timed poll() or take() increments the waiting consumer count; |
720 |
|
|
* offer(e) decrements the waiting consumer count |
721 |
dl |
1.1 |
*/ |
722 |
jsr166 |
1.5 |
public void testWaitingConsumer() throws InterruptedException { |
723 |
jsr166 |
1.87 |
final LinkedTransferQueue<Item> q = new LinkedTransferQueue<>(); |
724 |
dl |
1.86 |
mustEqual(0, q.getWaitingConsumerCount()); |
725 |
jsr166 |
1.13 |
assertFalse(q.hasWaitingConsumer()); |
726 |
jsr166 |
1.31 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
727 |
jsr166 |
1.9 |
|
728 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
729 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
730 |
jsr166 |
1.31 |
threadStarted.countDown(); |
731 |
jsr166 |
1.66 |
long startTime = System.nanoTime(); |
732 |
jsr166 |
1.31 |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
733 |
dl |
1.86 |
mustEqual(0, q.getWaitingConsumerCount()); |
734 |
jsr166 |
1.22 |
assertFalse(q.hasWaitingConsumer()); |
735 |
jsr166 |
1.66 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
736 |
jsr166 |
1.9 |
}}); |
737 |
|
|
|
738 |
jsr166 |
1.31 |
threadStarted.await(); |
739 |
jsr166 |
1.87 |
Callable<Boolean> oneConsumer = new Callable<>() { |
740 |
|
|
public Boolean call() { |
741 |
jsr166 |
1.70 |
return q.hasWaitingConsumer() |
742 |
|
|
&& q.getWaitingConsumerCount() == 1; }}; |
743 |
|
|
waitForThreadToEnterWaitState(t, oneConsumer); |
744 |
jsr166 |
1.31 |
|
745 |
|
|
assertTrue(q.offer(one)); |
746 |
dl |
1.86 |
mustEqual(0, q.getWaitingConsumerCount()); |
747 |
jsr166 |
1.13 |
assertFalse(q.hasWaitingConsumer()); |
748 |
jsr166 |
1.31 |
|
749 |
jsr166 |
1.66 |
awaitTermination(t); |
750 |
dl |
1.1 |
} |
751 |
jsr166 |
1.3 |
|
752 |
|
|
/** |
753 |
jsr166 |
1.5 |
* transfer(null) throws NullPointerException |
754 |
dl |
1.1 |
*/ |
755 |
jsr166 |
1.5 |
public void testTransfer1() throws InterruptedException { |
756 |
dl |
1.1 |
try { |
757 |
jsr166 |
1.87 |
LinkedTransferQueue<Item> q = new LinkedTransferQueue<>(); |
758 |
dl |
1.1 |
q.transfer(null); |
759 |
|
|
shouldThrow(); |
760 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
761 |
dl |
1.1 |
} |
762 |
|
|
|
763 |
jsr166 |
1.3 |
/** |
764 |
jsr166 |
1.80 |
* transfer waits until a poll occurs. The transferred element |
765 |
jsr166 |
1.69 |
* is returned by the associated poll. |
766 |
dl |
1.1 |
*/ |
767 |
jsr166 |
1.5 |
public void testTransfer2() throws InterruptedException { |
768 |
dl |
1.86 |
final LinkedTransferQueue<Item> q = new LinkedTransferQueue<>(); |
769 |
jsr166 |
1.31 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
770 |
jsr166 |
1.5 |
|
771 |
jsr166 |
1.9 |
Thread t = newStartedThread(new CheckedRunnable() { |
772 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
773 |
jsr166 |
1.31 |
threadStarted.countDown(); |
774 |
jsr166 |
1.34 |
q.transfer(five); |
775 |
jsr166 |
1.31 |
checkEmpty(q); |
776 |
jsr166 |
1.9 |
}}); |
777 |
dl |
1.1 |
|
778 |
jsr166 |
1.31 |
threadStarted.await(); |
779 |
jsr166 |
1.87 |
Callable<Boolean> oneElement = new Callable<>() { |
780 |
|
|
public Boolean call() { |
781 |
jsr166 |
1.70 |
return !q.isEmpty() && q.size() == 1; }}; |
782 |
|
|
waitForThreadToEnterWaitState(t, oneElement); |
783 |
|
|
|
784 |
jsr166 |
1.34 |
assertSame(five, q.poll()); |
785 |
jsr166 |
1.31 |
checkEmpty(q); |
786 |
jsr166 |
1.66 |
awaitTermination(t); |
787 |
dl |
1.1 |
} |
788 |
jsr166 |
1.3 |
|
789 |
|
|
/** |
790 |
dl |
1.6 |
* transfer waits until a poll occurs, and then transfers in fifo order |
791 |
dl |
1.1 |
*/ |
792 |
jsr166 |
1.5 |
public void testTransfer3() throws InterruptedException { |
793 |
dl |
1.86 |
final LinkedTransferQueue<Item> q = new LinkedTransferQueue<>(); |
794 |
jsr166 |
1.5 |
|
795 |
jsr166 |
1.9 |
Thread first = newStartedThread(new CheckedRunnable() { |
796 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
797 |
jsr166 |
1.32 |
q.transfer(four); |
798 |
dl |
1.86 |
mustNotContain(q, four); |
799 |
|
|
mustEqual(1, q.size()); |
800 |
jsr166 |
1.7 |
}}); |
801 |
jsr166 |
1.5 |
|
802 |
jsr166 |
1.9 |
Thread interruptedThread = newStartedThread( |
803 |
|
|
new CheckedInterruptedRunnable() { |
804 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
805 |
jsr166 |
1.32 |
while (q.isEmpty()) |
806 |
jsr166 |
1.9 |
Thread.yield(); |
807 |
jsr166 |
1.32 |
q.transfer(five); |
808 |
jsr166 |
1.9 |
}}); |
809 |
jsr166 |
1.5 |
|
810 |
|
|
while (q.size() < 2) |
811 |
|
|
Thread.yield(); |
812 |
dl |
1.86 |
mustEqual(2, q.size()); |
813 |
jsr166 |
1.32 |
assertSame(four, q.poll()); |
814 |
jsr166 |
1.5 |
first.join(); |
815 |
dl |
1.86 |
mustEqual(1, q.size()); |
816 |
jsr166 |
1.5 |
interruptedThread.interrupt(); |
817 |
|
|
interruptedThread.join(); |
818 |
jsr166 |
1.31 |
checkEmpty(q); |
819 |
dl |
1.1 |
} |
820 |
|
|
|
821 |
|
|
/** |
822 |
dl |
1.6 |
* transfer waits until a poll occurs, at which point the polling |
823 |
|
|
* thread returns the element |
824 |
dl |
1.1 |
*/ |
825 |
jsr166 |
1.5 |
public void testTransfer4() throws InterruptedException { |
826 |
jsr166 |
1.87 |
final LinkedTransferQueue<Item> q = new LinkedTransferQueue<>(); |
827 |
jsr166 |
1.9 |
|
828 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
829 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
830 |
jsr166 |
1.7 |
q.transfer(four); |
831 |
dl |
1.86 |
mustNotContain(q, four); |
832 |
jsr166 |
1.24 |
assertSame(three, q.poll()); |
833 |
jsr166 |
1.9 |
}}); |
834 |
|
|
|
835 |
jsr166 |
1.31 |
while (q.isEmpty()) |
836 |
|
|
Thread.yield(); |
837 |
|
|
assertFalse(q.isEmpty()); |
838 |
dl |
1.86 |
mustEqual(1, q.size()); |
839 |
jsr166 |
1.5 |
assertTrue(q.offer(three)); |
840 |
jsr166 |
1.24 |
assertSame(four, q.poll()); |
841 |
jsr166 |
1.66 |
awaitTermination(t); |
842 |
dl |
1.1 |
} |
843 |
jsr166 |
1.3 |
|
844 |
|
|
/** |
845 |
jsr166 |
1.80 |
* transfer waits until a take occurs. The transferred element |
846 |
jsr166 |
1.69 |
* is returned by the associated take. |
847 |
dl |
1.6 |
*/ |
848 |
|
|
public void testTransfer5() throws InterruptedException { |
849 |
dl |
1.86 |
final LinkedTransferQueue<Item> q = new LinkedTransferQueue<>(); |
850 |
dl |
1.6 |
|
851 |
jsr166 |
1.9 |
Thread t = newStartedThread(new CheckedRunnable() { |
852 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
853 |
jsr166 |
1.31 |
q.transfer(four); |
854 |
jsr166 |
1.12 |
checkEmpty(q); |
855 |
jsr166 |
1.9 |
}}); |
856 |
dl |
1.6 |
|
857 |
jsr166 |
1.31 |
while (q.isEmpty()) |
858 |
|
|
Thread.yield(); |
859 |
|
|
assertFalse(q.isEmpty()); |
860 |
dl |
1.86 |
mustEqual(1, q.size()); |
861 |
jsr166 |
1.31 |
assertSame(four, q.take()); |
862 |
jsr166 |
1.12 |
checkEmpty(q); |
863 |
jsr166 |
1.66 |
awaitTermination(t); |
864 |
dl |
1.6 |
} |
865 |
|
|
|
866 |
|
|
/** |
867 |
jsr166 |
1.5 |
* tryTransfer(null) throws NullPointerException |
868 |
dl |
1.1 |
*/ |
869 |
|
|
public void testTryTransfer1() { |
870 |
jsr166 |
1.87 |
final LinkedTransferQueue<Item> q = new LinkedTransferQueue<>(); |
871 |
dl |
1.1 |
try { |
872 |
|
|
q.tryTransfer(null); |
873 |
jsr166 |
1.8 |
shouldThrow(); |
874 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
875 |
dl |
1.1 |
} |
876 |
jsr166 |
1.3 |
|
877 |
|
|
/** |
878 |
jsr166 |
1.5 |
* tryTransfer returns false and does not enqueue if there are no |
879 |
|
|
* consumers waiting to poll or take. |
880 |
dl |
1.1 |
*/ |
881 |
jsr166 |
1.12 |
public void testTryTransfer2() throws InterruptedException { |
882 |
jsr166 |
1.87 |
final LinkedTransferQueue<Object> q = new LinkedTransferQueue<>(); |
883 |
jsr166 |
1.5 |
assertFalse(q.tryTransfer(new Object())); |
884 |
|
|
assertFalse(q.hasWaitingConsumer()); |
885 |
jsr166 |
1.12 |
checkEmpty(q); |
886 |
dl |
1.1 |
} |
887 |
jsr166 |
1.3 |
|
888 |
|
|
/** |
889 |
jsr166 |
1.5 |
* If there is a consumer waiting in timed poll, tryTransfer |
890 |
|
|
* returns true while successfully transfering object. |
891 |
dl |
1.1 |
*/ |
892 |
jsr166 |
1.5 |
public void testTryTransfer3() throws InterruptedException { |
893 |
|
|
final Object hotPotato = new Object(); |
894 |
jsr166 |
1.87 |
final LinkedTransferQueue<Object> q = new LinkedTransferQueue<>(); |
895 |
jsr166 |
1.9 |
|
896 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
897 |
jsr166 |
1.21 |
public void realRun() { |
898 |
jsr166 |
1.7 |
while (! q.hasWaitingConsumer()) |
899 |
|
|
Thread.yield(); |
900 |
jsr166 |
1.24 |
assertTrue(q.hasWaitingConsumer()); |
901 |
jsr166 |
1.31 |
checkEmpty(q); |
902 |
jsr166 |
1.24 |
assertTrue(q.tryTransfer(hotPotato)); |
903 |
jsr166 |
1.9 |
}}); |
904 |
|
|
|
905 |
jsr166 |
1.66 |
long startTime = System.nanoTime(); |
906 |
|
|
assertSame(hotPotato, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
907 |
|
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
908 |
jsr166 |
1.12 |
checkEmpty(q); |
909 |
jsr166 |
1.66 |
awaitTermination(t); |
910 |
jsr166 |
1.5 |
} |
911 |
dl |
1.1 |
|
912 |
jsr166 |
1.5 |
/** |
913 |
|
|
* If there is a consumer waiting in take, tryTransfer returns |
914 |
|
|
* true while successfully transfering object. |
915 |
|
|
*/ |
916 |
|
|
public void testTryTransfer4() throws InterruptedException { |
917 |
|
|
final Object hotPotato = new Object(); |
918 |
jsr166 |
1.87 |
final LinkedTransferQueue<Object> q = new LinkedTransferQueue<>(); |
919 |
jsr166 |
1.9 |
|
920 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
921 |
jsr166 |
1.21 |
public void realRun() { |
922 |
jsr166 |
1.7 |
while (! q.hasWaitingConsumer()) |
923 |
|
|
Thread.yield(); |
924 |
jsr166 |
1.24 |
assertTrue(q.hasWaitingConsumer()); |
925 |
jsr166 |
1.31 |
checkEmpty(q); |
926 |
jsr166 |
1.24 |
assertTrue(q.tryTransfer(hotPotato)); |
927 |
jsr166 |
1.9 |
}}); |
928 |
|
|
|
929 |
jsr166 |
1.23 |
assertSame(q.take(), hotPotato); |
930 |
jsr166 |
1.12 |
checkEmpty(q); |
931 |
jsr166 |
1.66 |
awaitTermination(t); |
932 |
dl |
1.1 |
} |
933 |
|
|
|
934 |
jsr166 |
1.3 |
/** |
935 |
jsr166 |
1.46 |
* tryTransfer blocks interruptibly if no takers |
936 |
dl |
1.1 |
*/ |
937 |
jsr166 |
1.5 |
public void testTryTransfer5() throws InterruptedException { |
938 |
jsr166 |
1.87 |
final LinkedTransferQueue<Object> q = new LinkedTransferQueue<>(); |
939 |
jsr166 |
1.46 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
940 |
jsr166 |
1.40 |
assertTrue(q.isEmpty()); |
941 |
jsr166 |
1.9 |
|
942 |
jsr166 |
1.31 |
Thread t = newStartedThread(new CheckedRunnable() { |
943 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
944 |
jsr166 |
1.46 |
Thread.currentThread().interrupt(); |
945 |
|
|
try { |
946 |
jsr166 |
1.82 |
q.tryTransfer(new Object(), randomTimeout(), randomTimeUnit()); |
947 |
jsr166 |
1.46 |
shouldThrow(); |
948 |
|
|
} catch (InterruptedException success) {} |
949 |
|
|
assertFalse(Thread.interrupted()); |
950 |
|
|
|
951 |
|
|
pleaseInterrupt.countDown(); |
952 |
jsr166 |
1.31 |
try { |
953 |
jsr166 |
1.85 |
q.tryTransfer(new Object(), LONGER_DELAY_MS, MILLISECONDS); |
954 |
jsr166 |
1.31 |
shouldThrow(); |
955 |
|
|
} catch (InterruptedException success) {} |
956 |
jsr166 |
1.46 |
assertFalse(Thread.interrupted()); |
957 |
jsr166 |
1.5 |
}}); |
958 |
jsr166 |
1.9 |
|
959 |
jsr166 |
1.46 |
await(pleaseInterrupt); |
960 |
jsr166 |
1.82 |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
961 |
jsr166 |
1.31 |
t.interrupt(); |
962 |
jsr166 |
1.46 |
awaitTermination(t); |
963 |
jsr166 |
1.31 |
checkEmpty(q); |
964 |
dl |
1.1 |
} |
965 |
|
|
|
966 |
jsr166 |
1.3 |
/** |
967 |
jsr166 |
1.33 |
* tryTransfer gives up after the timeout and returns false |
968 |
dl |
1.1 |
*/ |
969 |
jsr166 |
1.5 |
public void testTryTransfer6() throws InterruptedException { |
970 |
jsr166 |
1.87 |
final LinkedTransferQueue<Object> q = new LinkedTransferQueue<>(); |
971 |
jsr166 |
1.9 |
|
972 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
973 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
974 |
jsr166 |
1.65 |
long startTime = System.nanoTime(); |
975 |
jsr166 |
1.23 |
assertFalse(q.tryTransfer(new Object(), |
976 |
jsr166 |
1.46 |
timeoutMillis(), MILLISECONDS)); |
977 |
jsr166 |
1.65 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
978 |
jsr166 |
1.32 |
checkEmpty(q); |
979 |
jsr166 |
1.9 |
}}); |
980 |
|
|
|
981 |
jsr166 |
1.46 |
awaitTermination(t); |
982 |
jsr166 |
1.27 |
checkEmpty(q); |
983 |
dl |
1.1 |
} |
984 |
|
|
|
985 |
jsr166 |
1.3 |
/** |
986 |
dl |
1.1 |
* tryTransfer waits for any elements previously in to be removed |
987 |
|
|
* before transfering to a poll or take |
988 |
|
|
*/ |
989 |
jsr166 |
1.5 |
public void testTryTransfer7() throws InterruptedException { |
990 |
jsr166 |
1.87 |
final LinkedTransferQueue<Item> q = new LinkedTransferQueue<>(); |
991 |
jsr166 |
1.5 |
assertTrue(q.offer(four)); |
992 |
jsr166 |
1.9 |
|
993 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
994 |
jsr166 |
1.21 |
public void realRun() throws InterruptedException { |
995 |
jsr166 |
1.66 |
long startTime = System.nanoTime(); |
996 |
|
|
assertTrue(q.tryTransfer(five, LONG_DELAY_MS, MILLISECONDS)); |
997 |
|
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
998 |
jsr166 |
1.31 |
checkEmpty(q); |
999 |
jsr166 |
1.9 |
}}); |
1000 |
|
|
|
1001 |
jsr166 |
1.32 |
while (q.size() != 2) |
1002 |
|
|
Thread.yield(); |
1003 |
dl |
1.86 |
mustEqual(2, q.size()); |
1004 |
jsr166 |
1.24 |
assertSame(four, q.poll()); |
1005 |
|
|
assertSame(five, q.poll()); |
1006 |
jsr166 |
1.12 |
checkEmpty(q); |
1007 |
jsr166 |
1.66 |
awaitTermination(t); |
1008 |
dl |
1.1 |
} |
1009 |
|
|
|
1010 |
jsr166 |
1.3 |
/** |
1011 |
jsr166 |
1.41 |
* tryTransfer attempts to enqueue into the queue and fails |
1012 |
|
|
* returning false not enqueueing and the successive poll is null |
1013 |
dl |
1.1 |
*/ |
1014 |
jsr166 |
1.5 |
public void testTryTransfer8() throws InterruptedException { |
1015 |
jsr166 |
1.87 |
final LinkedTransferQueue<Item> q = new LinkedTransferQueue<>(); |
1016 |
jsr166 |
1.5 |
assertTrue(q.offer(four)); |
1017 |
dl |
1.86 |
mustEqual(1, q.size()); |
1018 |
jsr166 |
1.65 |
long startTime = System.nanoTime(); |
1019 |
jsr166 |
1.46 |
assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS)); |
1020 |
jsr166 |
1.65 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1021 |
dl |
1.86 |
mustEqual(1, q.size()); |
1022 |
jsr166 |
1.24 |
assertSame(four, q.poll()); |
1023 |
jsr166 |
1.13 |
assertNull(q.poll()); |
1024 |
jsr166 |
1.12 |
checkEmpty(q); |
1025 |
dl |
1.1 |
} |
1026 |
|
|
|
1027 |
dl |
1.86 |
private LinkedTransferQueue<Item> populatedQueue(int n) { |
1028 |
|
|
LinkedTransferQueue<Item> q = new LinkedTransferQueue<>(); |
1029 |
jsr166 |
1.31 |
checkEmpty(q); |
1030 |
jsr166 |
1.5 |
for (int i = 0; i < n; i++) { |
1031 |
dl |
1.86 |
mustEqual(i, q.size()); |
1032 |
|
|
mustOffer(q, i); |
1033 |
|
|
mustEqual(Integer.MAX_VALUE, q.remainingCapacity()); |
1034 |
dl |
1.1 |
} |
1035 |
|
|
assertFalse(q.isEmpty()); |
1036 |
|
|
return q; |
1037 |
|
|
} |
1038 |
jsr166 |
1.53 |
|
1039 |
|
|
/** |
1040 |
|
|
* remove(null), contains(null) always return false |
1041 |
|
|
*/ |
1042 |
|
|
public void testNeverContainsNull() { |
1043 |
|
|
Collection<?>[] qs = { |
1044 |
jsr166 |
1.87 |
new LinkedTransferQueue<>(), |
1045 |
jsr166 |
1.53 |
populatedQueue(2), |
1046 |
|
|
}; |
1047 |
|
|
|
1048 |
|
|
for (Collection<?> q : qs) { |
1049 |
|
|
assertFalse(q.contains(null)); |
1050 |
|
|
assertFalse(q.remove(null)); |
1051 |
|
|
} |
1052 |
|
|
} |
1053 |
dl |
1.1 |
} |