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