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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
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; |
8 |
> |
import junit.framework.*; |
9 |
|
import java.util.Arrays; |
10 |
< |
import java.util.ConcurrentModificationException; |
10 |
> |
import java.util.ArrayList; |
11 |
> |
import java.util.Collection; |
12 |
|
import java.util.Iterator; |
13 |
+ |
import java.util.List; |
14 |
|
import java.util.NoSuchElementException; |
15 |
< |
import java.util.concurrent.*; |
15 |
> |
import java.util.Queue; |
16 |
> |
import java.util.concurrent.BlockingQueue; |
17 |
> |
import java.util.concurrent.CountDownLatch; |
18 |
> |
import java.util.concurrent.Executors; |
19 |
> |
import java.util.concurrent.ExecutorService; |
20 |
> |
import java.util.concurrent.LinkedTransferQueue; |
21 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
21 |
– |
import junit.framework.Test; |
22 |
– |
import junit.framework.TestSuite; |
22 |
|
|
23 |
+ |
@SuppressWarnings({"unchecked", "rawtypes"}) |
24 |
|
public class LinkedTransferQueueTest extends JSR166TestCase { |
25 |
|
|
26 |
+ |
public static class Generic extends BlockingQueueTest { |
27 |
+ |
protected BlockingQueue emptyCollection() { |
28 |
+ |
return new LinkedTransferQueue(); |
29 |
+ |
} |
30 |
+ |
} |
31 |
+ |
|
32 |
|
public static void main(String[] args) { |
33 |
|
junit.textui.TestRunner.run(suite()); |
34 |
|
} |
35 |
|
|
36 |
|
public static Test suite() { |
37 |
< |
return new TestSuite(LinkedTransferQueueTest.class); |
37 |
> |
return newTestSuite(LinkedTransferQueueTest.class, |
38 |
> |
new Generic().testSuite()); |
39 |
|
} |
40 |
|
|
41 |
|
/** |
55 |
|
try { |
56 |
|
new LinkedTransferQueue(null); |
57 |
|
shouldThrow(); |
58 |
< |
} catch (NullPointerException success) { |
52 |
< |
} |
58 |
> |
} catch (NullPointerException success) {} |
59 |
|
} |
60 |
|
|
61 |
|
/** |
63 |
|
* NullPointerException |
64 |
|
*/ |
65 |
|
public void testConstructor3() { |
66 |
+ |
Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
67 |
|
try { |
68 |
< |
Integer[] ints = new Integer[SIZE]; |
62 |
< |
new LinkedTransferQueue(Arrays.asList(ints)); |
68 |
> |
new LinkedTransferQueue(elements); |
69 |
|
shouldThrow(); |
70 |
< |
} catch (NullPointerException success) { |
65 |
< |
} |
70 |
> |
} catch (NullPointerException success) {} |
71 |
|
} |
72 |
|
|
73 |
|
/** |
75 |
|
* throws NullPointerException |
76 |
|
*/ |
77 |
|
public void testConstructor4() { |
78 |
+ |
Integer[] ints = new Integer[SIZE]; |
79 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
80 |
+ |
ints[i] = i; |
81 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
82 |
|
try { |
83 |
< |
Integer[] ints = new Integer[SIZE]; |
75 |
< |
for (int i = 0; i < SIZE - 1; ++i) { |
76 |
< |
ints[i] = i; |
77 |
< |
} |
78 |
< |
new LinkedTransferQueue(Arrays.asList(ints)); |
83 |
> |
new LinkedTransferQueue(elements); |
84 |
|
shouldThrow(); |
85 |
< |
} catch (NullPointerException success) { |
81 |
< |
} |
85 |
> |
} catch (NullPointerException success) {} |
86 |
|
} |
87 |
|
|
88 |
|
/** |
89 |
|
* Queue contains all elements of the collection it is initialized by |
90 |
|
*/ |
91 |
|
public void testConstructor5() { |
92 |
< |
try { |
93 |
< |
Integer[] ints = new Integer[SIZE]; |
94 |
< |
for (int i = 0; i < SIZE; ++i) { |
95 |
< |
ints[i] = i; |
96 |
< |
} |
97 |
< |
LinkedTransferQueue q |
98 |
< |
= new LinkedTransferQueue(Arrays.asList(ints)); |
99 |
< |
for (int i = 0; i < SIZE; ++i) { |
100 |
< |
assertEquals(ints[i], q.poll()); |
101 |
< |
} |
102 |
< |
} finally { |
92 |
> |
Integer[] ints = new Integer[SIZE]; |
93 |
> |
for (int i = 0; i < SIZE; ++i) { |
94 |
> |
ints[i] = i; |
95 |
> |
} |
96 |
> |
List intList = Arrays.asList(ints); |
97 |
> |
LinkedTransferQueue q |
98 |
> |
= new LinkedTransferQueue(intList); |
99 |
> |
assertEquals(q.size(), intList.size()); |
100 |
> |
assertEquals(q.toString(), intList.toString()); |
101 |
> |
assertTrue(Arrays.equals(q.toArray(), |
102 |
> |
intList.toArray())); |
103 |
> |
assertTrue(Arrays.equals(q.toArray(new Object[0]), |
104 |
> |
intList.toArray(new Object[0]))); |
105 |
> |
assertTrue(Arrays.equals(q.toArray(new Object[SIZE]), |
106 |
> |
intList.toArray(new Object[SIZE]))); |
107 |
> |
for (int i = 0; i < SIZE; ++i) { |
108 |
> |
assertEquals(ints[i], q.poll()); |
109 |
|
} |
110 |
|
} |
111 |
|
|
112 |
|
/** |
113 |
< |
* Remaining capacity never decrease nor increase on add or remove |
113 |
> |
* remainingCapacity() always returns Integer.MAX_VALUE |
114 |
|
*/ |
115 |
|
public void testRemainingCapacity() { |
116 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
127 |
|
} |
128 |
|
|
129 |
|
/** |
120 |
– |
* offer(null) throws NullPointerException |
121 |
– |
*/ |
122 |
– |
public void testOfferNull() { |
123 |
– |
try { |
124 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
125 |
– |
q.offer(null); |
126 |
– |
shouldThrow(); |
127 |
– |
} catch (NullPointerException success) { |
128 |
– |
} |
129 |
– |
} |
130 |
– |
|
131 |
– |
/** |
132 |
– |
* add(null) throws NullPointerException |
133 |
– |
*/ |
134 |
– |
public void testAddNull() { |
135 |
– |
try { |
136 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
137 |
– |
q.add(null); |
138 |
– |
shouldThrow(); |
139 |
– |
} catch (NullPointerException success) { |
140 |
– |
} |
141 |
– |
} |
142 |
– |
|
143 |
– |
/** |
144 |
– |
* addAll(null) throws NullPointerException |
145 |
– |
*/ |
146 |
– |
public void testAddAll1() { |
147 |
– |
try { |
148 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
149 |
– |
q.addAll(null); |
150 |
– |
shouldThrow(); |
151 |
– |
} catch (NullPointerException success) { |
152 |
– |
} |
153 |
– |
} |
154 |
– |
|
155 |
– |
/** |
130 |
|
* addAll(this) throws IllegalArgumentException |
131 |
|
*/ |
132 |
|
public void testAddAllSelf() { |
134 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
135 |
|
q.addAll(q); |
136 |
|
shouldThrow(); |
137 |
< |
} catch (IllegalArgumentException success) { |
164 |
< |
} |
165 |
< |
} |
166 |
< |
|
167 |
< |
/** |
168 |
< |
* addAll of a collection with null elements throws NullPointerException |
169 |
< |
*/ |
170 |
< |
public void testAddAll2() { |
171 |
< |
try { |
172 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
173 |
< |
Integer[] ints = new Integer[SIZE]; |
174 |
< |
q.addAll(Arrays.asList(ints)); |
175 |
< |
shouldThrow(); |
176 |
< |
} catch (NullPointerException success) { |
177 |
< |
} |
137 |
> |
} catch (IllegalArgumentException success) {} |
138 |
|
} |
139 |
|
|
140 |
|
/** |
150 |
|
} |
151 |
|
q.addAll(Arrays.asList(ints)); |
152 |
|
shouldThrow(); |
153 |
< |
} catch (NullPointerException success) { |
194 |
< |
} |
153 |
> |
} catch (NullPointerException success) {} |
154 |
|
} |
155 |
|
|
156 |
|
/** |
171 |
|
} |
172 |
|
|
173 |
|
/** |
215 |
– |
* put(null) throws NullPointerException |
216 |
– |
*/ |
217 |
– |
public void testPutNull() throws InterruptedException { |
218 |
– |
try { |
219 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
220 |
– |
q.put(null); |
221 |
– |
shouldThrow(); |
222 |
– |
} catch (NullPointerException success) {} |
223 |
– |
} |
224 |
– |
|
225 |
– |
/** |
174 |
|
* all elements successfully put are contained |
175 |
|
*/ |
176 |
|
public void testPut() { |
177 |
|
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
178 |
|
for (int i = 0; i < SIZE; ++i) { |
179 |
+ |
assertEquals(i, q.size()); |
180 |
|
q.put(i); |
181 |
|
assertTrue(q.contains(i)); |
182 |
|
} |
234 |
– |
assertEquals(q.size(), SIZE); |
183 |
|
} |
184 |
|
|
185 |
|
/** |
193 |
|
} |
194 |
|
|
195 |
|
/** |
196 |
< |
* take blocks interruptibly when empty |
249 |
< |
*/ |
250 |
< |
public void testTakeFromEmpty() throws InterruptedException { |
251 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
252 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
253 |
< |
void realRun() throws InterruptedException { |
254 |
< |
q.take(); |
255 |
< |
}}); |
256 |
< |
t.start(); |
257 |
< |
Thread.sleep(SHORT_DELAY_MS); |
258 |
< |
t.interrupt(); |
259 |
< |
t.join(); |
260 |
< |
} |
261 |
< |
|
262 |
< |
/** |
263 |
< |
* Take removes existing elements until empty, then blocks interruptibly |
196 |
> |
* take removes existing elements until empty, then blocks interruptibly |
197 |
|
*/ |
198 |
|
public void testBlockingTake() throws InterruptedException { |
199 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
200 |
< |
void realRun() throws InterruptedException { |
201 |
< |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
199 |
> |
final BlockingQueue q = populatedQueue(SIZE); |
200 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
201 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
202 |
> |
public void realRun() throws InterruptedException { |
203 |
|
for (int i = 0; i < SIZE; ++i) { |
204 |
< |
threadAssertEquals(i, (int) q.take()); |
204 |
> |
assertEquals(i, q.take()); |
205 |
|
} |
206 |
< |
q.take(); |
206 |
> |
|
207 |
> |
Thread.currentThread().interrupt(); |
208 |
> |
try { |
209 |
> |
q.take(); |
210 |
> |
shouldThrow(); |
211 |
> |
} catch (InterruptedException success) {} |
212 |
> |
assertFalse(Thread.interrupted()); |
213 |
> |
|
214 |
> |
pleaseInterrupt.countDown(); |
215 |
> |
try { |
216 |
> |
q.take(); |
217 |
> |
shouldThrow(); |
218 |
> |
} catch (InterruptedException success) {} |
219 |
> |
assertFalse(Thread.interrupted()); |
220 |
|
}}); |
221 |
< |
t.start(); |
222 |
< |
Thread.sleep(SHORT_DELAY_MS); |
221 |
> |
|
222 |
> |
await(pleaseInterrupt); |
223 |
> |
assertThreadStaysAlive(t); |
224 |
|
t.interrupt(); |
225 |
< |
t.join(); |
225 |
> |
awaitTermination(t); |
226 |
|
} |
227 |
|
|
228 |
|
/** |
229 |
|
* poll succeeds unless empty |
230 |
|
*/ |
231 |
< |
public void testPoll() { |
231 |
> |
public void testPoll() throws InterruptedException { |
232 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
233 |
|
for (int i = 0; i < SIZE; ++i) { |
234 |
|
assertEquals(i, (int) q.poll()); |
235 |
|
} |
236 |
|
assertNull(q.poll()); |
237 |
+ |
checkEmpty(q); |
238 |
|
} |
239 |
|
|
240 |
|
/** |
241 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
241 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
242 |
|
*/ |
243 |
|
public void testTimedPoll0() throws InterruptedException { |
244 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
246 |
|
assertEquals(i, (int) q.poll(0, MILLISECONDS)); |
247 |
|
} |
248 |
|
assertNull(q.poll(0, MILLISECONDS)); |
249 |
+ |
checkEmpty(q); |
250 |
|
} |
251 |
|
|
252 |
|
/** |
253 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
253 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
254 |
|
*/ |
255 |
|
public void testTimedPoll() throws InterruptedException { |
256 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
257 |
|
for (int i = 0; i < SIZE; ++i) { |
258 |
< |
assertEquals(i, (int) q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
259 |
< |
} |
260 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
258 |
> |
long startTime = System.nanoTime(); |
259 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
260 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
261 |
> |
} |
262 |
> |
long startTime = System.nanoTime(); |
263 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
264 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
265 |
> |
checkEmpty(q); |
266 |
|
} |
267 |
|
|
268 |
|
/** |
270 |
|
* returning timeout status |
271 |
|
*/ |
272 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
273 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
274 |
< |
void realRun() throws InterruptedException { |
275 |
< |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
273 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
274 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
275 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
276 |
> |
public void realRun() throws InterruptedException { |
277 |
|
for (int i = 0; i < SIZE; ++i) { |
278 |
< |
threadAssertEquals(i, (int) q.poll(SHORT_DELAY_MS, |
279 |
< |
MILLISECONDS)); |
278 |
> |
long t0 = System.nanoTime(); |
279 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
280 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
281 |
> |
} |
282 |
> |
long t0 = System.nanoTime(); |
283 |
> |
aboutToWait.countDown(); |
284 |
> |
try { |
285 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
286 |
> |
shouldThrow(); |
287 |
> |
} catch (InterruptedException success) { |
288 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
289 |
|
} |
325 |
– |
q.poll(SHORT_DELAY_MS, MILLISECONDS); |
290 |
|
}}); |
291 |
< |
t.start(); |
292 |
< |
Thread.sleep(SHORT_DELAY_MS); |
291 |
> |
|
292 |
> |
aboutToWait.await(); |
293 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
294 |
|
t.interrupt(); |
295 |
< |
t.join(); |
295 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
296 |
> |
checkEmpty(q); |
297 |
|
} |
298 |
|
|
299 |
|
/** |
300 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
301 |
< |
* on interruption throws |
300 |
> |
* timed poll after thread interrupted throws InterruptedException |
301 |
> |
* instead of returning timeout status |
302 |
|
*/ |
303 |
< |
public void testTimedPollWithOffer() throws InterruptedException { |
304 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
305 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
306 |
< |
void realRun() throws InterruptedException { |
307 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
308 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
309 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
303 |
> |
public void testTimedPollAfterInterrupt() throws InterruptedException { |
304 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
305 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
306 |
> |
public void realRun() throws InterruptedException { |
307 |
> |
Thread.currentThread().interrupt(); |
308 |
> |
for (int i = 0; i < SIZE; ++i) { |
309 |
> |
long t0 = System.nanoTime(); |
310 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
311 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
312 |
> |
} |
313 |
> |
try { |
314 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
315 |
> |
shouldThrow(); |
316 |
> |
} catch (InterruptedException success) {} |
317 |
|
}}); |
318 |
< |
t.start(); |
319 |
< |
Thread.sleep(SMALL_DELAY_MS); |
320 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
348 |
< |
t.interrupt(); |
349 |
< |
t.join(); |
318 |
> |
|
319 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
320 |
> |
checkEmpty(q); |
321 |
|
} |
322 |
|
|
323 |
|
/** |
324 |
|
* peek returns next element, or null if empty |
325 |
|
*/ |
326 |
< |
public void testPeek() { |
326 |
> |
public void testPeek() throws InterruptedException { |
327 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
328 |
|
for (int i = 0; i < SIZE; ++i) { |
329 |
|
assertEquals(i, (int) q.peek()); |
332 |
|
i != (int) q.peek()); |
333 |
|
} |
334 |
|
assertNull(q.peek()); |
335 |
+ |
checkEmpty(q); |
336 |
|
} |
337 |
|
|
338 |
|
/** |
339 |
|
* element returns next element, or throws NoSuchElementException if empty |
340 |
|
*/ |
341 |
< |
public void testElement() { |
341 |
> |
public void testElement() throws InterruptedException { |
342 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
343 |
|
for (int i = 0; i < SIZE; ++i) { |
344 |
|
assertEquals(i, (int) q.element()); |
347 |
|
try { |
348 |
|
q.element(); |
349 |
|
shouldThrow(); |
350 |
< |
} catch (NoSuchElementException success) { |
351 |
< |
} |
350 |
> |
} catch (NoSuchElementException success) {} |
351 |
> |
checkEmpty(q); |
352 |
|
} |
353 |
|
|
354 |
|
/** |
355 |
|
* remove removes next element, or throws NoSuchElementException if empty |
356 |
|
*/ |
357 |
< |
public void testRemove() { |
357 |
> |
public void testRemove() throws InterruptedException { |
358 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
359 |
|
for (int i = 0; i < SIZE; ++i) { |
360 |
|
assertEquals(i, (int) q.remove()); |
362 |
|
try { |
363 |
|
q.remove(); |
364 |
|
shouldThrow(); |
365 |
< |
} catch (NoSuchElementException success) { |
366 |
< |
} |
395 |
< |
} |
396 |
< |
|
397 |
< |
/** |
398 |
< |
* remove(x) removes x and returns true if present |
399 |
< |
*/ |
400 |
< |
public void testRemoveElement() { |
401 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
402 |
< |
for (int i = 1; i < SIZE; i += 2) { |
403 |
< |
assertTrue(q.remove(i)); |
404 |
< |
} |
405 |
< |
for (int i = 0; i < SIZE; i += 2) { |
406 |
< |
assertTrue(q.remove(i)); |
407 |
< |
assertFalse(q.remove(i + 1)); |
408 |
< |
} |
409 |
< |
assertTrue(q.isEmpty()); |
365 |
> |
} catch (NoSuchElementException success) {} |
366 |
> |
checkEmpty(q); |
367 |
|
} |
368 |
|
|
369 |
|
/** |
376 |
|
assertTrue(q.remove(one)); |
377 |
|
assertTrue(q.remove(two)); |
378 |
|
assertTrue(q.add(three)); |
379 |
< |
assertTrue(q.take() != null); |
379 |
> |
assertSame(q.take(), three); |
380 |
|
} |
381 |
|
|
382 |
|
/** |
394 |
|
/** |
395 |
|
* clear removes all elements |
396 |
|
*/ |
397 |
< |
public void testClear() { |
397 |
> |
public void testClear() throws InterruptedException { |
398 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
399 |
|
q.clear(); |
400 |
< |
assertTrue(q.isEmpty()); |
444 |
< |
assertEquals(0, q.size()); |
400 |
> |
checkEmpty(q); |
401 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
402 |
|
q.add(one); |
403 |
|
assertFalse(q.isEmpty()); |
404 |
+ |
assertEquals(1, q.size()); |
405 |
|
assertTrue(q.contains(one)); |
406 |
|
q.clear(); |
407 |
< |
assertTrue(q.isEmpty()); |
407 |
> |
checkEmpty(q); |
408 |
|
} |
409 |
|
|
410 |
|
/** |
458 |
|
} |
459 |
|
|
460 |
|
/** |
461 |
< |
* toArray contains all elements |
461 |
> |
* toArray() contains all elements in FIFO order |
462 |
|
*/ |
463 |
< |
public void testToArray() throws InterruptedException { |
463 |
> |
public void testToArray() { |
464 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
465 |
|
Object[] o = q.toArray(); |
466 |
|
for (int i = 0; i < o.length; i++) { |
467 |
< |
assertEquals(o[i], q.take()); |
467 |
> |
assertSame(o[i], q.poll()); |
468 |
|
} |
469 |
|
} |
470 |
|
|
471 |
|
/** |
472 |
< |
* toArray(a) contains all elements |
472 |
> |
* toArray(a) contains all elements in FIFO order |
473 |
|
*/ |
474 |
< |
public void testToArray2() throws InterruptedException { |
474 |
> |
public void testToArray2() { |
475 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
476 |
|
Integer[] ints = new Integer[SIZE]; |
477 |
< |
ints = q.toArray(ints); |
477 |
> |
Integer[] array = q.toArray(ints); |
478 |
> |
assertSame(ints, array); |
479 |
|
for (int i = 0; i < ints.length; i++) { |
480 |
< |
assertEquals(ints[i], q.take()); |
523 |
< |
} |
524 |
< |
} |
525 |
< |
|
526 |
< |
/** |
527 |
< |
* toArray(null) throws NullPointerException |
528 |
< |
*/ |
529 |
< |
public void testToArray_BadArg() { |
530 |
< |
try { |
531 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
532 |
< |
Object o[] = q.toArray(null); |
533 |
< |
shouldThrow(); |
534 |
< |
} catch (NullPointerException success) { |
480 |
> |
assertSame(ints[i], q.poll()); |
481 |
|
} |
482 |
|
} |
483 |
|
|
484 |
|
/** |
485 |
< |
* toArray with incompatible array type throws CCE |
485 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
486 |
|
*/ |
487 |
|
public void testToArray1_BadArg() { |
488 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
489 |
|
try { |
490 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
544 |
< |
Object o[] = q.toArray(new String[10]); |
490 |
> |
q.toArray(new String[10]); |
491 |
|
shouldThrow(); |
492 |
< |
} catch (ArrayStoreException success) { |
547 |
< |
} |
492 |
> |
} catch (ArrayStoreException success) {} |
493 |
|
} |
494 |
|
|
495 |
|
/** |
498 |
|
public void testIterator() throws InterruptedException { |
499 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
500 |
|
Iterator it = q.iterator(); |
501 |
+ |
int i = 0; |
502 |
|
while (it.hasNext()) { |
503 |
< |
assertEquals(it.next(), q.take()); |
503 |
> |
assertEquals(it.next(), i++); |
504 |
|
} |
505 |
+ |
assertEquals(i, SIZE); |
506 |
|
} |
507 |
|
|
508 |
|
/** |
509 |
< |
* iterator.remove removes current element |
509 |
> |
* iterator.remove() removes current element |
510 |
|
*/ |
511 |
|
public void testIteratorRemove() { |
512 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
519 |
|
it.remove(); |
520 |
|
|
521 |
|
it = q.iterator(); |
522 |
< |
assertEquals(it.next(), one); |
523 |
< |
assertEquals(it.next(), three); |
522 |
> |
assertSame(it.next(), one); |
523 |
> |
assertSame(it.next(), three); |
524 |
|
assertFalse(it.hasNext()); |
525 |
|
} |
526 |
|
|
564 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
565 |
|
String s = q.toString(); |
566 |
|
for (int i = 0; i < SIZE; ++i) { |
567 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
567 |
> |
assertTrue(s.contains(String.valueOf(i))); |
568 |
|
} |
569 |
|
} |
570 |
|
|
573 |
|
*/ |
574 |
|
public void testOfferInExecutor() { |
575 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
576 |
< |
q.add(one); |
630 |
< |
q.add(two); |
576 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
577 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
578 |
|
|
579 |
|
executor.execute(new CheckedRunnable() { |
580 |
< |
void realRun() { |
581 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, |
582 |
< |
MILLISECONDS)); |
580 |
> |
public void realRun() throws InterruptedException { |
581 |
> |
threadsStarted.await(); |
582 |
> |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
583 |
|
}}); |
584 |
|
|
585 |
|
executor.execute(new CheckedRunnable() { |
586 |
< |
void realRun() throws InterruptedException { |
587 |
< |
Thread.sleep(SMALL_DELAY_MS); |
588 |
< |
threadAssertEquals(one, q.take()); |
586 |
> |
public void realRun() throws InterruptedException { |
587 |
> |
threadsStarted.await(); |
588 |
> |
assertSame(one, q.take()); |
589 |
> |
checkEmpty(q); |
590 |
|
}}); |
591 |
|
|
592 |
|
joinPool(executor); |
593 |
|
} |
594 |
|
|
595 |
|
/** |
596 |
< |
* poll retrieves elements across Executor threads |
596 |
> |
* timed poll retrieves elements across Executor threads |
597 |
|
*/ |
598 |
|
public void testPollInExecutor() { |
599 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
600 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
601 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
602 |
|
|
603 |
|
executor.execute(new CheckedRunnable() { |
604 |
< |
void realRun() throws InterruptedException { |
605 |
< |
threadAssertNull(q.poll()); |
606 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, |
607 |
< |
MILLISECONDS)); |
608 |
< |
threadAssertTrue(q.isEmpty()); |
604 |
> |
public void realRun() throws InterruptedException { |
605 |
> |
assertNull(q.poll()); |
606 |
> |
threadsStarted.await(); |
607 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
608 |
> |
checkEmpty(q); |
609 |
|
}}); |
610 |
|
|
611 |
|
executor.execute(new CheckedRunnable() { |
612 |
< |
void realRun() throws InterruptedException { |
613 |
< |
Thread.sleep(SMALL_DELAY_MS); |
612 |
> |
public void realRun() throws InterruptedException { |
613 |
> |
threadsStarted.await(); |
614 |
|
q.put(one); |
615 |
|
}}); |
616 |
|
|
621 |
|
* A deserialized serialized queue has same elements in same order |
622 |
|
*/ |
623 |
|
public void testSerialization() throws Exception { |
624 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
625 |
< |
|
678 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
679 |
< |
ObjectOutputStream out |
680 |
< |
= new ObjectOutputStream(new BufferedOutputStream(bout)); |
681 |
< |
out.writeObject(q); |
682 |
< |
out.close(); |
683 |
< |
|
684 |
< |
ByteArrayInputStream bin |
685 |
< |
= new ByteArrayInputStream(bout.toByteArray()); |
686 |
< |
ObjectInputStream in |
687 |
< |
= new ObjectInputStream(new BufferedInputStream(bin)); |
688 |
< |
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
689 |
< |
|
690 |
< |
assertEquals(q.size(), r.size()); |
691 |
< |
while (!q.isEmpty()) { |
692 |
< |
assertEquals(q.remove(), r.remove()); |
693 |
< |
} |
694 |
< |
} |
624 |
> |
Queue x = populatedQueue(SIZE); |
625 |
> |
Queue y = serialClone(x); |
626 |
|
|
627 |
< |
/** |
628 |
< |
* drainTo(null) throws NullPointerException |
629 |
< |
*/ |
630 |
< |
public void testDrainToNull() { |
631 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
632 |
< |
try { |
633 |
< |
q.drainTo(null); |
703 |
< |
shouldThrow(); |
704 |
< |
} catch (NullPointerException success) { |
705 |
< |
} |
706 |
< |
} |
707 |
< |
|
708 |
< |
/** |
709 |
< |
* drainTo(this) throws IllegalArgumentException |
710 |
< |
*/ |
711 |
< |
public void testDrainToSelf() { |
712 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
713 |
< |
try { |
714 |
< |
q.drainTo(q); |
715 |
< |
shouldThrow(); |
716 |
< |
} catch (IllegalArgumentException success) { |
627 |
> |
assertNotSame(y, x); |
628 |
> |
assertEquals(x.size(), y.size()); |
629 |
> |
assertEquals(x.toString(), y.toString()); |
630 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
631 |
> |
while (!x.isEmpty()) { |
632 |
> |
assertFalse(y.isEmpty()); |
633 |
> |
assertEquals(x.remove(), y.remove()); |
634 |
|
} |
635 |
+ |
assertTrue(y.isEmpty()); |
636 |
|
} |
637 |
|
|
638 |
|
/** |
642 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
643 |
|
ArrayList l = new ArrayList(); |
644 |
|
q.drainTo(l); |
645 |
< |
assertEquals(q.size(), 0); |
646 |
< |
assertEquals(l.size(), SIZE); |
645 |
> |
assertEquals(0, q.size()); |
646 |
> |
assertEquals(SIZE, l.size()); |
647 |
|
for (int i = 0; i < SIZE; ++i) { |
648 |
< |
assertEquals(l.get(i), i); |
648 |
> |
assertEquals(i, l.get(i)); |
649 |
|
} |
650 |
|
q.add(zero); |
651 |
|
q.add(one); |
654 |
|
assertTrue(q.contains(one)); |
655 |
|
l.clear(); |
656 |
|
q.drainTo(l); |
657 |
< |
assertEquals(q.size(), 0); |
658 |
< |
assertEquals(l.size(), 2); |
657 |
> |
assertEquals(0, q.size()); |
658 |
> |
assertEquals(2, l.size()); |
659 |
|
for (int i = 0; i < 2; ++i) { |
660 |
< |
assertEquals(l.get(i), i); |
660 |
> |
assertEquals(i, l.get(i)); |
661 |
|
} |
662 |
|
} |
663 |
|
|
664 |
|
/** |
665 |
< |
* drainTo empties full queue, unblocking a waiting put. |
665 |
> |
* drainTo(c) empties full queue, unblocking a waiting put. |
666 |
|
*/ |
667 |
|
public void testDrainToWithActivePut() throws InterruptedException { |
668 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
669 |
< |
Thread t = new Thread(new CheckedRunnable() { |
670 |
< |
void realRun() { |
669 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
670 |
> |
public void realRun() { |
671 |
|
q.put(SIZE + 1); |
672 |
|
}}); |
755 |
– |
t.start(); |
673 |
|
ArrayList l = new ArrayList(); |
674 |
|
q.drainTo(l); |
675 |
|
assertTrue(l.size() >= SIZE); |
676 |
< |
for (int i = 0; i < SIZE; ++i) { |
677 |
< |
assertEquals(l.get(i), i); |
678 |
< |
} |
762 |
< |
t.join(); |
676 |
> |
for (int i = 0; i < SIZE; ++i) |
677 |
> |
assertEquals(i, l.get(i)); |
678 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
679 |
|
assertTrue(q.size() + l.size() >= SIZE); |
680 |
|
} |
681 |
|
|
682 |
|
/** |
683 |
< |
* drainTo(null, n) throws NullPointerException |
768 |
< |
*/ |
769 |
< |
public void testDrainToNullN() { |
770 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
771 |
< |
try { |
772 |
< |
q.drainTo(null, 0); |
773 |
< |
shouldThrow(); |
774 |
< |
} catch (NullPointerException success) { |
775 |
< |
} |
776 |
< |
} |
777 |
< |
|
778 |
< |
/** |
779 |
< |
* drainTo(this, n) throws IllegalArgumentException |
780 |
< |
*/ |
781 |
< |
public void testDrainToSelfN() { |
782 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
783 |
< |
try { |
784 |
< |
q.drainTo(q, 0); |
785 |
< |
shouldThrow(); |
786 |
< |
} catch (IllegalArgumentException success) { |
787 |
< |
} |
788 |
< |
} |
789 |
< |
|
790 |
< |
/** |
791 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
683 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
684 |
|
*/ |
685 |
|
public void testDrainToN() { |
686 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
691 |
|
ArrayList l = new ArrayList(); |
692 |
|
q.drainTo(l, i); |
693 |
|
int k = (i < SIZE) ? i : SIZE; |
694 |
< |
assertEquals(l.size(), k); |
695 |
< |
assertEquals(q.size(), SIZE - k); |
696 |
< |
for (int j = 0; j < k; ++j) { |
697 |
< |
assertEquals(l.get(j), j); |
806 |
< |
} |
694 |
> |
assertEquals(k, l.size()); |
695 |
> |
assertEquals(SIZE - k, q.size()); |
696 |
> |
for (int j = 0; j < k; ++j) |
697 |
> |
assertEquals(j, l.get(j)); |
698 |
|
while (q.poll() != null) |
699 |
|
; |
700 |
|
} |
701 |
|
} |
702 |
|
|
703 |
|
/** |
704 |
< |
* poll and take decrement the waiting consumer count |
704 |
> |
* timed poll() or take() increments the waiting consumer count; |
705 |
> |
* offer(e) decrements the waiting consumer count |
706 |
|
*/ |
707 |
|
public void testWaitingConsumer() throws InterruptedException { |
708 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
709 |
< |
final ConsumerObserver waiting = new ConsumerObserver(); |
710 |
< |
new Thread(new CheckedRunnable() { |
711 |
< |
void realRun() { |
712 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
713 |
< |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
714 |
< |
threadAssertTrue(q.offer(new Object())); |
715 |
< |
}}).start(); |
716 |
< |
assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); |
717 |
< |
assertTrue(q.getWaitingConsumerCount() |
718 |
< |
< waiting.getWaitingConsumers()); |
709 |
> |
assertEquals(0, q.getWaitingConsumerCount()); |
710 |
> |
assertFalse(q.hasWaitingConsumer()); |
711 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
712 |
> |
|
713 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
714 |
> |
public void realRun() throws InterruptedException { |
715 |
> |
threadStarted.countDown(); |
716 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
717 |
> |
assertEquals(0, q.getWaitingConsumerCount()); |
718 |
> |
assertFalse(q.hasWaitingConsumer()); |
719 |
> |
}}); |
720 |
> |
|
721 |
> |
threadStarted.await(); |
722 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
723 |
> |
assertEquals(1, q.getWaitingConsumerCount()); |
724 |
> |
assertTrue(q.hasWaitingConsumer()); |
725 |
> |
|
726 |
> |
assertTrue(q.offer(one)); |
727 |
> |
assertEquals(0, q.getWaitingConsumerCount()); |
728 |
> |
assertFalse(q.hasWaitingConsumer()); |
729 |
> |
|
730 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
731 |
|
} |
732 |
|
|
733 |
|
/** |
738 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
739 |
|
q.transfer(null); |
740 |
|
shouldThrow(); |
741 |
< |
} catch (NullPointerException ex) { |
838 |
< |
} |
741 |
> |
} catch (NullPointerException success) {} |
742 |
|
} |
743 |
|
|
744 |
|
/** |
748 |
|
public void testTransfer2() throws InterruptedException { |
749 |
|
final LinkedTransferQueue<Integer> q |
750 |
|
= new LinkedTransferQueue<Integer>(); |
751 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
752 |
|
|
753 |
< |
new Thread(new CheckedRunnable() { |
754 |
< |
void realRun() throws InterruptedException { |
755 |
< |
q.transfer(SIZE); |
756 |
< |
threadAssertTrue(q.isEmpty()); |
757 |
< |
}}).start(); |
753 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
754 |
> |
public void realRun() throws InterruptedException { |
755 |
> |
threadStarted.countDown(); |
756 |
> |
q.transfer(five); |
757 |
> |
checkEmpty(q); |
758 |
> |
}}); |
759 |
|
|
760 |
< |
Thread.sleep(SHORT_DELAY_MS); |
760 |
> |
threadStarted.await(); |
761 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
762 |
|
assertEquals(1, q.size()); |
763 |
< |
assertEquals(SIZE, (int) q.poll()); |
764 |
< |
assertTrue(q.isEmpty()); |
763 |
> |
assertSame(five, q.poll()); |
764 |
> |
checkEmpty(q); |
765 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
766 |
|
} |
767 |
|
|
768 |
|
/** |
772 |
|
final LinkedTransferQueue<Integer> q |
773 |
|
= new LinkedTransferQueue<Integer>(); |
774 |
|
|
775 |
< |
Thread first = new Thread(new CheckedRunnable() { |
776 |
< |
void realRun() throws InterruptedException { |
777 |
< |
Integer i = SIZE + 1; |
778 |
< |
q.transfer(i); |
779 |
< |
threadAssertTrue(!q.contains(i)); |
873 |
< |
threadAssertEquals(1, q.size()); |
775 |
> |
Thread first = newStartedThread(new CheckedRunnable() { |
776 |
> |
public void realRun() throws InterruptedException { |
777 |
> |
q.transfer(four); |
778 |
> |
assertTrue(!q.contains(four)); |
779 |
> |
assertEquals(1, q.size()); |
780 |
|
}}); |
875 |
– |
first.start(); |
781 |
|
|
782 |
< |
Thread interruptedThread = new Thread(new CheckedInterruptedRunnable() { |
783 |
< |
void realRun() throws InterruptedException { |
784 |
< |
while (q.size() == 0) |
785 |
< |
Thread.yield(); |
786 |
< |
q.transfer(SIZE); |
787 |
< |
}}); |
788 |
< |
interruptedThread.start(); |
782 |
> |
Thread interruptedThread = newStartedThread( |
783 |
> |
new CheckedInterruptedRunnable() { |
784 |
> |
public void realRun() throws InterruptedException { |
785 |
> |
while (q.isEmpty()) |
786 |
> |
Thread.yield(); |
787 |
> |
q.transfer(five); |
788 |
> |
}}); |
789 |
|
|
790 |
|
while (q.size() < 2) |
791 |
|
Thread.yield(); |
792 |
|
assertEquals(2, q.size()); |
793 |
< |
assertEquals(SIZE + 1, (int) q.poll()); |
793 |
> |
assertSame(four, q.poll()); |
794 |
|
first.join(); |
795 |
|
assertEquals(1, q.size()); |
796 |
|
interruptedThread.interrupt(); |
797 |
|
interruptedThread.join(); |
798 |
< |
assertEquals(0, q.size()); |
894 |
< |
assertTrue(q.isEmpty()); |
798 |
> |
checkEmpty(q); |
799 |
|
} |
800 |
|
|
801 |
|
/** |
804 |
|
*/ |
805 |
|
public void testTransfer4() throws InterruptedException { |
806 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
807 |
< |
new Thread(new CheckedRunnable() { |
808 |
< |
void realRun() throws InterruptedException { |
807 |
> |
|
808 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
809 |
> |
public void realRun() throws InterruptedException { |
810 |
|
q.transfer(four); |
811 |
< |
threadAssertFalse(q.contains(four)); |
812 |
< |
threadAssertEquals(three, q.poll()); |
813 |
< |
}}).start(); |
814 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
811 |
> |
assertFalse(q.contains(four)); |
812 |
> |
assertSame(three, q.poll()); |
813 |
> |
}}); |
814 |
> |
|
815 |
> |
while (q.isEmpty()) |
816 |
> |
Thread.yield(); |
817 |
> |
assertFalse(q.isEmpty()); |
818 |
> |
assertEquals(1, q.size()); |
819 |
|
assertTrue(q.offer(three)); |
820 |
< |
assertEquals(four, q.poll()); |
820 |
> |
assertSame(four, q.poll()); |
821 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
822 |
|
} |
823 |
|
|
824 |
|
/** |
829 |
|
final LinkedTransferQueue<Integer> q |
830 |
|
= new LinkedTransferQueue<Integer>(); |
831 |
|
|
832 |
< |
new Thread(new CheckedRunnable() { |
833 |
< |
void realRun() throws InterruptedException { |
834 |
< |
q.transfer(SIZE); |
835 |
< |
threadAssertTrue(q.isEmpty()); |
836 |
< |
}}).start(); |
832 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
833 |
> |
public void realRun() throws InterruptedException { |
834 |
> |
q.transfer(four); |
835 |
> |
checkEmpty(q); |
836 |
> |
}}); |
837 |
|
|
838 |
< |
Thread.sleep(SHORT_DELAY_MS); |
839 |
< |
assertEquals(SIZE, (int) q.take()); |
840 |
< |
assertTrue(q.isEmpty()); |
838 |
> |
while (q.isEmpty()) |
839 |
> |
Thread.yield(); |
840 |
> |
assertFalse(q.isEmpty()); |
841 |
> |
assertEquals(1, q.size()); |
842 |
> |
assertSame(four, q.take()); |
843 |
> |
checkEmpty(q); |
844 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
845 |
|
} |
846 |
|
|
847 |
|
/** |
851 |
|
try { |
852 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
853 |
|
q.tryTransfer(null); |
854 |
< |
this.shouldThrow(); |
855 |
< |
} catch (NullPointerException ex) { |
942 |
< |
} |
854 |
> |
shouldThrow(); |
855 |
> |
} catch (NullPointerException success) {} |
856 |
|
} |
857 |
|
|
858 |
|
/** |
859 |
|
* tryTransfer returns false and does not enqueue if there are no |
860 |
|
* consumers waiting to poll or take. |
861 |
|
*/ |
862 |
< |
public void testTryTransfer2() { |
862 |
> |
public void testTryTransfer2() throws InterruptedException { |
863 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
864 |
|
assertFalse(q.tryTransfer(new Object())); |
865 |
|
assertFalse(q.hasWaitingConsumer()); |
866 |
< |
assertTrue(q.isEmpty()); |
954 |
< |
assertEquals(0, q.size()); |
866 |
> |
checkEmpty(q); |
867 |
|
} |
868 |
|
|
869 |
|
/** |
873 |
|
public void testTryTransfer3() throws InterruptedException { |
874 |
|
final Object hotPotato = new Object(); |
875 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
876 |
< |
new Thread(new CheckedRunnable() { |
877 |
< |
void realRun() { |
876 |
> |
|
877 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
878 |
> |
public void realRun() { |
879 |
|
while (! q.hasWaitingConsumer()) |
880 |
|
Thread.yield(); |
881 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
882 |
< |
threadAssertTrue(q.isEmpty()); |
883 |
< |
threadAssertTrue(q.size() == 0); |
884 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
885 |
< |
}}).start(); |
886 |
< |
assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) == hotPotato); |
887 |
< |
assertTrue(q.isEmpty()); |
881 |
> |
assertTrue(q.hasWaitingConsumer()); |
882 |
> |
checkEmpty(q); |
883 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
884 |
> |
}}); |
885 |
> |
|
886 |
> |
assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
887 |
> |
checkEmpty(q); |
888 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
889 |
|
} |
890 |
|
|
891 |
|
/** |
895 |
|
public void testTryTransfer4() throws InterruptedException { |
896 |
|
final Object hotPotato = new Object(); |
897 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
898 |
< |
new Thread(new CheckedRunnable() { |
899 |
< |
void realRun() { |
898 |
> |
|
899 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
900 |
> |
public void realRun() { |
901 |
|
while (! q.hasWaitingConsumer()) |
902 |
|
Thread.yield(); |
903 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
904 |
< |
threadAssertTrue(q.isEmpty()); |
905 |
< |
threadAssertTrue(q.size() == 0); |
906 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
907 |
< |
}}).start(); |
908 |
< |
assertTrue(q.take() == hotPotato); |
909 |
< |
assertTrue(q.isEmpty()); |
903 |
> |
assertTrue(q.hasWaitingConsumer()); |
904 |
> |
checkEmpty(q); |
905 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
906 |
> |
}}); |
907 |
> |
|
908 |
> |
assertSame(q.take(), hotPotato); |
909 |
> |
checkEmpty(q); |
910 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
911 |
|
} |
912 |
|
|
913 |
|
/** |
914 |
< |
* tryTransfer waits the amount given if interrupted, and |
999 |
< |
* throws interrupted exception |
914 |
> |
* tryTransfer blocks interruptibly if no takers |
915 |
|
*/ |
916 |
|
public void testTryTransfer5() throws InterruptedException { |
917 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
918 |
< |
Thread toInterrupt = new Thread(new CheckedInterruptedRunnable() { |
919 |
< |
void realRun() throws InterruptedException { |
920 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
918 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
919 |
> |
assertTrue(q.isEmpty()); |
920 |
> |
|
921 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
922 |
> |
public void realRun() throws InterruptedException { |
923 |
> |
Thread.currentThread().interrupt(); |
924 |
> |
try { |
925 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
926 |
> |
shouldThrow(); |
927 |
> |
} catch (InterruptedException success) {} |
928 |
> |
assertFalse(Thread.interrupted()); |
929 |
> |
|
930 |
> |
pleaseInterrupt.countDown(); |
931 |
> |
try { |
932 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
933 |
> |
shouldThrow(); |
934 |
> |
} catch (InterruptedException success) {} |
935 |
> |
assertFalse(Thread.interrupted()); |
936 |
|
}}); |
937 |
< |
toInterrupt.start(); |
938 |
< |
Thread.sleep(SMALL_DELAY_MS); |
939 |
< |
toInterrupt.interrupt(); |
937 |
> |
|
938 |
> |
await(pleaseInterrupt); |
939 |
> |
assertThreadStaysAlive(t); |
940 |
> |
t.interrupt(); |
941 |
> |
awaitTermination(t); |
942 |
> |
checkEmpty(q); |
943 |
|
} |
944 |
|
|
945 |
|
/** |
946 |
< |
* tryTransfer gives up after the timeout and return false |
946 |
> |
* tryTransfer gives up after the timeout and returns false |
947 |
|
*/ |
948 |
|
public void testTryTransfer6() throws InterruptedException { |
949 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
950 |
< |
new Thread(new CheckedRunnable() { |
951 |
< |
void realRun() throws InterruptedException { |
952 |
< |
threadAssertFalse |
953 |
< |
(q.tryTransfer(new Object(), |
954 |
< |
SHORT_DELAY_MS, MILLISECONDS)); |
955 |
< |
}}).start(); |
956 |
< |
Thread.sleep(LONG_DELAY_MS); |
957 |
< |
assertTrue(q.isEmpty()); |
950 |
> |
|
951 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
952 |
> |
public void realRun() throws InterruptedException { |
953 |
> |
long t0 = System.nanoTime(); |
954 |
> |
assertFalse(q.tryTransfer(new Object(), |
955 |
> |
timeoutMillis(), MILLISECONDS)); |
956 |
> |
assertTrue(millisElapsedSince(t0) >= timeoutMillis()); |
957 |
> |
checkEmpty(q); |
958 |
> |
}}); |
959 |
> |
|
960 |
> |
awaitTermination(t); |
961 |
> |
checkEmpty(q); |
962 |
|
} |
963 |
|
|
964 |
|
/** |
968 |
|
public void testTryTransfer7() throws InterruptedException { |
969 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
970 |
|
assertTrue(q.offer(four)); |
971 |
< |
new Thread(new CheckedRunnable() { |
972 |
< |
void realRun() throws InterruptedException { |
973 |
< |
threadAssertTrue(q.tryTransfer(five, |
974 |
< |
LONG_DELAY_MS, MILLISECONDS)); |
975 |
< |
threadAssertTrue(q.isEmpty()); |
976 |
< |
}}).start(); |
977 |
< |
Thread.sleep(SHORT_DELAY_MS); |
971 |
> |
|
972 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
973 |
> |
public void realRun() throws InterruptedException { |
974 |
> |
assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); |
975 |
> |
checkEmpty(q); |
976 |
> |
}}); |
977 |
> |
|
978 |
> |
while (q.size() != 2) |
979 |
> |
Thread.yield(); |
980 |
|
assertEquals(2, q.size()); |
981 |
< |
assertEquals(four, q.poll()); |
982 |
< |
assertEquals(five, q.poll()); |
983 |
< |
assertTrue(q.isEmpty()); |
981 |
> |
assertSame(four, q.poll()); |
982 |
> |
assertSame(five, q.poll()); |
983 |
> |
checkEmpty(q); |
984 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
985 |
|
} |
986 |
|
|
987 |
|
/** |
988 |
< |
* tryTransfer attempts to enqueue into the q and fails returning |
989 |
< |
* false not enqueueing and the successive poll is null |
988 |
> |
* tryTransfer attempts to enqueue into the queue and fails |
989 |
> |
* returning false not enqueueing and the successive poll is null |
990 |
|
*/ |
991 |
|
public void testTryTransfer8() throws InterruptedException { |
992 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
993 |
|
assertTrue(q.offer(four)); |
994 |
|
assertEquals(1, q.size()); |
995 |
< |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
995 |
> |
long t0 = System.nanoTime(); |
996 |
> |
assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS)); |
997 |
> |
assertTrue(millisElapsedSince(t0) >= timeoutMillis()); |
998 |
|
assertEquals(1, q.size()); |
999 |
< |
assertEquals(four, q.poll()); |
1058 |
< |
threadAssertTrue(q.isEmpty()); |
999 |
> |
assertSame(four, q.poll()); |
1000 |
|
assertNull(q.poll()); |
1001 |
+ |
checkEmpty(q); |
1002 |
|
} |
1003 |
|
|
1004 |
|
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1005 |
|
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
1006 |
< |
assertTrue(q.isEmpty()); |
1006 |
> |
checkEmpty(q); |
1007 |
|
for (int i = 0; i < n; i++) { |
1008 |
|
assertEquals(i, q.size()); |
1009 |
|
assertTrue(q.offer(i)); |
1013 |
|
return q; |
1014 |
|
} |
1015 |
|
|
1016 |
< |
private static class ConsumerObserver { |
1017 |
< |
|
1018 |
< |
private int waitingConsumers; |
1019 |
< |
|
1020 |
< |
private ConsumerObserver() { |
1021 |
< |
} |
1022 |
< |
|
1023 |
< |
private void setWaitingConsumer(int i) { |
1024 |
< |
this.waitingConsumers = i; |
1025 |
< |
} |
1026 |
< |
|
1027 |
< |
private int getWaitingConsumers() { |
1086 |
< |
return waitingConsumers; |
1016 |
> |
/** |
1017 |
> |
* remove(null), contains(null) always return false |
1018 |
> |
*/ |
1019 |
> |
public void testNeverContainsNull() { |
1020 |
> |
Collection<?>[] qs = { |
1021 |
> |
new LinkedTransferQueue<Object>(), |
1022 |
> |
populatedQueue(2), |
1023 |
> |
}; |
1024 |
> |
|
1025 |
> |
for (Collection<?> q : qs) { |
1026 |
> |
assertFalse(q.contains(null)); |
1027 |
> |
assertFalse(q.remove(null)); |
1028 |
|
} |
1029 |
|
} |
1030 |
|
} |