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