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