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