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