1 |
– |
|
1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
17 |
|
import java.util.Iterator; |
18 |
|
import java.util.NoSuchElementException; |
19 |
|
import java.util.concurrent.*; |
20 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
21 |
|
import junit.framework.Test; |
22 |
|
import junit.framework.TestSuite; |
23 |
|
|
24 |
+ |
@SuppressWarnings({"unchecked", "rawtypes"}) |
25 |
|
public class LinkedTransferQueueTest extends JSR166TestCase { |
26 |
|
|
27 |
|
public static void main(String[] args) { |
32 |
|
return new TestSuite(LinkedTransferQueueTest.class); |
33 |
|
} |
34 |
|
|
35 |
< |
/* |
36 |
< |
*Constructor builds new queue with size being zero and empty being true |
35 |
> |
/** |
36 |
> |
* Constructor builds new queue with size being zero and empty |
37 |
> |
* being true |
38 |
|
*/ |
39 |
|
public void testConstructor1() { |
40 |
|
assertEquals(0, new LinkedTransferQueue().size()); |
41 |
|
assertTrue(new LinkedTransferQueue().isEmpty()); |
42 |
|
} |
43 |
|
|
44 |
< |
/* |
45 |
< |
* Initizialing constructor with null collection throws NPE |
44 |
> |
/** |
45 |
> |
* Initializing constructor with null collection throws |
46 |
> |
* NullPointerException |
47 |
|
*/ |
48 |
|
public void testConstructor2() { |
49 |
|
try { |
54 |
|
} |
55 |
|
|
56 |
|
/** |
57 |
< |
* Initializing from Collection of null elements throws NPE |
57 |
> |
* Initializing from Collection of null elements throws |
58 |
> |
* NullPointerException |
59 |
|
*/ |
60 |
|
public void testConstructor3() { |
61 |
|
try { |
62 |
|
Integer[] ints = new Integer[SIZE]; |
63 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); |
63 |
> |
new LinkedTransferQueue(Arrays.asList(ints)); |
64 |
|
shouldThrow(); |
65 |
|
} catch (NullPointerException success) { |
66 |
|
} |
67 |
|
} |
68 |
< |
/* |
68 |
> |
|
69 |
> |
/** |
70 |
|
* Initializing constructor with a collection containing some null elements |
71 |
< |
* throws NPE |
71 |
> |
* throws NullPointerException |
72 |
|
*/ |
68 |
– |
|
73 |
|
public void testConstructor4() { |
74 |
|
try { |
75 |
|
Integer[] ints = new Integer[SIZE]; |
76 |
|
for (int i = 0; i < SIZE - 1; ++i) { |
77 |
< |
ints[i] = new Integer(i); |
77 |
> |
ints[i] = i; |
78 |
|
} |
79 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); |
79 |
> |
new LinkedTransferQueue(Arrays.asList(ints)); |
80 |
|
shouldThrow(); |
81 |
|
} catch (NullPointerException success) { |
82 |
|
} |
83 |
|
} |
84 |
|
|
85 |
< |
/* |
85 |
> |
/** |
86 |
|
* Queue contains all elements of the collection it is initialized by |
87 |
|
*/ |
88 |
|
public void testConstructor5() { |
89 |
|
try { |
90 |
|
Integer[] ints = new Integer[SIZE]; |
91 |
|
for (int i = 0; i < SIZE; ++i) { |
92 |
< |
ints[i] = new Integer(i); |
92 |
> |
ints[i] = i; |
93 |
|
} |
94 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); |
94 |
> |
LinkedTransferQueue q |
95 |
> |
= new LinkedTransferQueue(Arrays.asList(ints)); |
96 |
|
for (int i = 0; i < SIZE; ++i) { |
97 |
|
assertEquals(ints[i], q.poll()); |
98 |
|
} |
104 |
|
* Remaining capacity never decrease nor increase on add or remove |
105 |
|
*/ |
106 |
|
public void testRemainingCapacity() { |
107 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
103 |
< |
int remainingCapacity = q.remainingCapacity(); |
107 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
108 |
|
for (int i = 0; i < SIZE; ++i) { |
109 |
< |
assertEquals(remainingCapacity, q.remainingCapacity()); |
109 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
110 |
|
assertEquals(SIZE - i, q.size()); |
111 |
|
q.remove(); |
112 |
|
} |
113 |
|
for (int i = 0; i < SIZE; ++i) { |
114 |
< |
assertEquals(remainingCapacity, q.remainingCapacity()); |
114 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
115 |
|
assertEquals(i, q.size()); |
116 |
< |
q.add(new Integer(i)); |
116 |
> |
q.add(i); |
117 |
|
} |
118 |
|
} |
119 |
|
|
120 |
|
/** |
121 |
< |
* offer(null) throws NPE |
121 |
> |
* offer(null) throws NullPointerException |
122 |
|
*/ |
123 |
|
public void testOfferNull() { |
124 |
|
try { |
130 |
|
} |
131 |
|
|
132 |
|
/** |
133 |
< |
* add(null) throws NPE |
133 |
> |
* add(null) throws NullPointerException |
134 |
|
*/ |
135 |
|
public void testAddNull() { |
136 |
|
try { |
142 |
|
} |
143 |
|
|
144 |
|
/** |
145 |
< |
* addAll(null) throws NPE |
145 |
> |
* addAll(null) throws NullPointerException |
146 |
|
*/ |
147 |
|
public void testAddAll1() { |
148 |
|
try { |
154 |
|
} |
155 |
|
|
156 |
|
/** |
157 |
< |
* addAll(this) throws IAE |
157 |
> |
* addAll(this) throws IllegalArgumentException |
158 |
|
*/ |
159 |
|
public void testAddAllSelf() { |
160 |
|
try { |
166 |
|
} |
167 |
|
|
168 |
|
/** |
169 |
< |
* addAll of a collection with null elements throws NPE |
169 |
> |
* addAll of a collection with null elements throws NullPointerException |
170 |
|
*/ |
171 |
|
public void testAddAll2() { |
172 |
|
try { |
179 |
|
} |
180 |
|
|
181 |
|
/** |
182 |
< |
* addAll of a collection with any null elements throws NPE after |
183 |
< |
* possibly adding some elements |
182 |
> |
* addAll of a collection with any null elements throws |
183 |
> |
* NullPointerException after possibly adding some elements |
184 |
|
*/ |
185 |
|
public void testAddAll3() { |
186 |
|
try { |
187 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
188 |
|
Integer[] ints = new Integer[SIZE]; |
189 |
|
for (int i = 0; i < SIZE - 1; ++i) { |
190 |
< |
ints[i] = new Integer(i); |
190 |
> |
ints[i] = i; |
191 |
|
} |
192 |
|
q.addAll(Arrays.asList(ints)); |
193 |
|
shouldThrow(); |
199 |
|
* Queue contains all elements, in traversal order, of successful addAll |
200 |
|
*/ |
201 |
|
public void testAddAll5() { |
202 |
< |
try { |
203 |
< |
Integer[] empty = new Integer[0]; |
204 |
< |
Integer[] ints = new Integer[SIZE]; |
205 |
< |
for (int i = 0; i < SIZE; ++i) { |
206 |
< |
ints[i] = new Integer(i); |
207 |
< |
} |
208 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
209 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
210 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
211 |
< |
for (int i = 0; i < SIZE; ++i) { |
208 |
< |
assertEquals(ints[i], q.poll()); |
209 |
< |
} |
210 |
< |
} finally { |
202 |
> |
Integer[] empty = new Integer[0]; |
203 |
> |
Integer[] ints = new Integer[SIZE]; |
204 |
> |
for (int i = 0; i < SIZE; ++i) { |
205 |
> |
ints[i] = i; |
206 |
> |
} |
207 |
> |
LinkedTransferQueue q = new LinkedTransferQueue(); |
208 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
209 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
210 |
> |
for (int i = 0; i < SIZE; ++i) { |
211 |
> |
assertEquals(ints[i], q.poll()); |
212 |
|
} |
213 |
|
} |
214 |
|
|
215 |
|
/** |
216 |
< |
* put(null) throws NPE |
216 |
> |
* put(null) throws NullPointerException |
217 |
|
*/ |
218 |
< |
public void testPutNull() { |
218 |
> |
public void testPutNull() throws InterruptedException { |
219 |
|
try { |
220 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
221 |
|
q.put(null); |
222 |
|
shouldThrow(); |
223 |
< |
} catch (NullPointerException success) { |
223 |
< |
} catch (Exception ie) { |
224 |
< |
unexpectedException(); |
225 |
< |
} |
223 |
> |
} catch (NullPointerException success) {} |
224 |
|
} |
225 |
|
|
226 |
|
/** |
227 |
|
* all elements successfully put are contained |
228 |
|
*/ |
229 |
|
public void testPut() { |
230 |
< |
try { |
231 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
232 |
< |
for (int i = 0; i < SIZE; ++i) { |
233 |
< |
Integer I = new Integer(i); |
236 |
< |
q.put(I); |
237 |
< |
assertTrue(q.contains(I)); |
238 |
< |
} |
239 |
< |
} catch (Exception ie) { |
240 |
< |
unexpectedException(); |
230 |
> |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
231 |
> |
for (int i = 0; i < SIZE; ++i) { |
232 |
> |
q.put(i); |
233 |
> |
assertTrue(q.contains(i)); |
234 |
|
} |
235 |
+ |
assertEquals(q.size(), SIZE); |
236 |
|
} |
237 |
|
|
238 |
|
/** |
239 |
|
* take retrieves elements in FIFO order |
240 |
|
*/ |
241 |
< |
public void testTake() { |
242 |
< |
try { |
243 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
244 |
< |
for (int i = 0; i < SIZE; ++i) { |
251 |
< |
assertEquals(i, ((Integer) q.take()).intValue()); |
252 |
< |
} |
253 |
< |
} catch (InterruptedException e) { |
254 |
< |
unexpectedException(); |
241 |
> |
public void testTake() throws InterruptedException { |
242 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
243 |
> |
for (int i = 0; i < SIZE; ++i) { |
244 |
> |
assertEquals(i, (int) q.take()); |
245 |
|
} |
246 |
|
} |
247 |
|
|
248 |
|
/** |
249 |
|
* take blocks interruptibly when empty |
250 |
|
*/ |
251 |
< |
public void testTakeFromEmpty() { |
251 |
> |
public void testTakeFromEmpty() throws InterruptedException { |
252 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
253 |
< |
Thread t = new Thread(new Runnable() { |
254 |
< |
|
255 |
< |
public void run() { |
256 |
< |
try { |
257 |
< |
q.take(); |
258 |
< |
threadShouldThrow(); |
259 |
< |
} catch (InterruptedException success) { |
270 |
< |
} |
271 |
< |
} |
272 |
< |
}); |
273 |
< |
try { |
274 |
< |
t.start(); |
275 |
< |
Thread.sleep(SHORT_DELAY_MS); |
276 |
< |
t.interrupt(); |
277 |
< |
t.join(); |
278 |
< |
} catch (Exception e) { |
279 |
< |
unexpectedException(); |
280 |
< |
} |
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 |
< |
/* |
261 |
> |
|
262 |
> |
/** |
263 |
|
* Take removes existing elements until empty, then blocks interruptibly |
264 |
|
*/ |
265 |
< |
|
266 |
< |
public void testBlockingTake() { |
267 |
< |
Thread t = new Thread(new Runnable() { |
268 |
< |
|
269 |
< |
public void run() { |
270 |
< |
try { |
291 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
292 |
< |
for (int i = 0; i < SIZE; ++i) { |
293 |
< |
assertEquals(i, ((Integer) q.take()).intValue()); |
294 |
< |
} |
295 |
< |
q.take(); |
296 |
< |
threadShouldThrow(); |
297 |
< |
} catch (InterruptedException success) { |
265 |
> |
public void testBlockingTake() throws InterruptedException { |
266 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
267 |
> |
void realRun() throws InterruptedException { |
268 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
269 |
> |
for (int i = 0; i < SIZE; ++i) { |
270 |
> |
threadAssertEquals(i, (int) q.take()); |
271 |
|
} |
272 |
< |
} |
273 |
< |
}); |
274 |
< |
t.start(); |
275 |
< |
try { |
276 |
< |
Thread.sleep(SHORT_DELAY_MS); |
304 |
< |
t.interrupt(); |
305 |
< |
t.join(); |
306 |
< |
} catch (InterruptedException ie) { |
307 |
< |
unexpectedException(); |
308 |
< |
} |
272 |
> |
q.take(); |
273 |
> |
}}); |
274 |
> |
Thread.sleep(SHORT_DELAY_MS); |
275 |
> |
t.interrupt(); |
276 |
> |
t.join(); |
277 |
|
} |
278 |
|
|
279 |
|
/** |
280 |
|
* poll succeeds unless empty |
281 |
|
*/ |
282 |
|
public void testPoll() { |
283 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
283 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
284 |
|
for (int i = 0; i < SIZE; ++i) { |
285 |
< |
assertEquals(i, ((Integer) q.poll()).intValue()); |
285 |
> |
assertEquals(i, (int) q.poll()); |
286 |
|
} |
287 |
|
assertNull(q.poll()); |
288 |
|
} |
290 |
|
/** |
291 |
|
* timed pool with zero timeout succeeds when non-empty, else times out |
292 |
|
*/ |
293 |
< |
public void testTimedPoll0() { |
294 |
< |
try { |
295 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
296 |
< |
for (int i = 0; i < SIZE; ++i) { |
329 |
< |
assertEquals(i, ((Integer) q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
330 |
< |
} |
331 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
332 |
< |
} catch (InterruptedException e) { |
333 |
< |
unexpectedException(); |
293 |
> |
public void testTimedPoll0() throws InterruptedException { |
294 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
295 |
> |
for (int i = 0; i < SIZE; ++i) { |
296 |
> |
assertEquals(i, (int) q.poll(0, MILLISECONDS)); |
297 |
|
} |
298 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
299 |
|
} |
300 |
|
|
301 |
|
/** |
302 |
|
* timed pool with nonzero timeout succeeds when non-empty, else times out |
303 |
|
*/ |
304 |
< |
public void testTimedPoll() { |
305 |
< |
try { |
306 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
307 |
< |
for (int i = 0; i < SIZE; ++i) { |
344 |
< |
assertEquals(i, ((Integer) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
345 |
< |
} |
346 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
347 |
< |
} catch (InterruptedException e) { |
348 |
< |
unexpectedException(); |
304 |
> |
public void testTimedPoll() throws InterruptedException { |
305 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
306 |
> |
for (int i = 0; i < SIZE; ++i) { |
307 |
> |
assertEquals(i, (int) q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
308 |
|
} |
309 |
+ |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
310 |
|
} |
311 |
|
|
312 |
|
/** |
313 |
|
* Interrupted timed poll throws InterruptedException instead of |
314 |
|
* returning timeout status |
315 |
|
*/ |
316 |
< |
public void testInterruptedTimedPoll() { |
317 |
< |
Thread t = new Thread(new Runnable() { |
318 |
< |
|
319 |
< |
public void run() { |
320 |
< |
try { |
321 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
322 |
< |
for (int i = 0; i < SIZE; ++i) { |
363 |
< |
threadAssertEquals(i, ((Integer) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
364 |
< |
} |
365 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
366 |
< |
} catch (InterruptedException success) { |
316 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
317 |
> |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
318 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
319 |
> |
void realRun() throws InterruptedException { |
320 |
> |
for (int i = 0; i < SIZE; ++i) { |
321 |
> |
threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS, |
322 |
> |
MILLISECONDS)); |
323 |
|
} |
324 |
< |
} |
325 |
< |
}); |
326 |
< |
t.start(); |
327 |
< |
try { |
328 |
< |
Thread.sleep(SHORT_DELAY_MS); |
329 |
< |
t.interrupt(); |
330 |
< |
t.join(); |
375 |
< |
} catch (InterruptedException ie) { |
376 |
< |
unexpectedException(); |
377 |
< |
} |
324 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
325 |
> |
}}); |
326 |
> |
Thread.sleep(SMALL_DELAY_MS); |
327 |
> |
t.interrupt(); |
328 |
> |
t.join(); |
329 |
> |
assertEquals(q.size(), 0); |
330 |
> |
assertNull(q.poll()); |
331 |
|
} |
332 |
|
|
333 |
|
/** |
334 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
335 |
< |
* on interruption throws |
334 |
> |
* timed poll before a delayed offer fails; after offer succeeds; |
335 |
> |
* on interruption throws |
336 |
|
*/ |
337 |
< |
public void testTimedPollWithOffer() { |
337 |
> |
public void testTimedPollWithOffer() throws InterruptedException { |
338 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
339 |
< |
Thread t = new Thread(new Runnable() { |
340 |
< |
|
341 |
< |
public void run() { |
342 |
< |
try { |
343 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
344 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
345 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
346 |
< |
threadShouldThrow(); |
347 |
< |
} catch (InterruptedException success) { |
348 |
< |
} |
396 |
< |
} |
397 |
< |
}); |
398 |
< |
try { |
399 |
< |
t.start(); |
400 |
< |
Thread.sleep(SMALL_DELAY_MS); |
401 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
402 |
< |
t.interrupt(); |
403 |
< |
t.join(); |
404 |
< |
} catch (Exception e) { |
405 |
< |
unexpectedException(); |
406 |
< |
} |
339 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
340 |
> |
void realRun() throws InterruptedException { |
341 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
342 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
343 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
344 |
> |
}}); |
345 |
> |
Thread.sleep(SMALL_DELAY_MS); |
346 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
347 |
> |
t.interrupt(); |
348 |
> |
t.join(); |
349 |
|
} |
350 |
|
|
351 |
|
/** |
352 |
|
* peek returns next element, or null if empty |
353 |
|
*/ |
354 |
|
public void testPeek() { |
355 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
355 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
356 |
|
for (int i = 0; i < SIZE; ++i) { |
357 |
< |
assertEquals(i, ((Integer) q.peek()).intValue()); |
358 |
< |
q.poll(); |
357 |
> |
assertEquals(i, (int) q.peek()); |
358 |
> |
assertEquals(i, (int) q.poll()); |
359 |
|
assertTrue(q.peek() == null || |
360 |
< |
i != ((Integer) q.peek()).intValue()); |
360 |
> |
i != (int) q.peek()); |
361 |
|
} |
362 |
|
assertNull(q.peek()); |
363 |
|
} |
364 |
|
|
365 |
|
/** |
366 |
< |
* element returns next element, or throws NSEE if empty |
366 |
> |
* element returns next element, or throws NoSuchElementException if empty |
367 |
|
*/ |
368 |
|
public void testElement() { |
369 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
369 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
370 |
|
for (int i = 0; i < SIZE; ++i) { |
371 |
< |
assertEquals(i, ((Integer) q.element()).intValue()); |
372 |
< |
q.poll(); |
371 |
> |
assertEquals(i, (int) q.element()); |
372 |
> |
assertEquals(i, (int) q.poll()); |
373 |
|
} |
374 |
|
try { |
375 |
|
q.element(); |
379 |
|
} |
380 |
|
|
381 |
|
/** |
382 |
< |
* remove removes next element, or throws NSEE if empty |
382 |
> |
* remove removes next element, or throws NoSuchElementException if empty |
383 |
|
*/ |
384 |
|
public void testRemove() { |
385 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
385 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
386 |
|
for (int i = 0; i < SIZE; ++i) { |
387 |
< |
assertEquals(i, ((Integer) q.remove()).intValue()); |
387 |
> |
assertEquals(i, (int) q.remove()); |
388 |
|
} |
389 |
|
try { |
390 |
|
q.remove(); |
399 |
|
public void testRemoveElement() { |
400 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
401 |
|
for (int i = 1; i < SIZE; i += 2) { |
402 |
< |
assertTrue(q.remove(new Integer(i))); |
402 |
> |
assertTrue(q.remove(i)); |
403 |
|
} |
404 |
|
for (int i = 0; i < SIZE; i += 2) { |
405 |
< |
assertTrue(q.remove(new Integer(i))); |
406 |
< |
assertFalse(q.remove(new Integer(i + 1))); |
405 |
> |
assertTrue(q.remove(i)); |
406 |
> |
assertFalse(q.remove(i + 1)); |
407 |
|
} |
408 |
|
assertTrue(q.isEmpty()); |
409 |
|
} |
411 |
|
/** |
412 |
|
* An add following remove(x) succeeds |
413 |
|
*/ |
414 |
< |
public void testRemoveElementAndAdd() { |
415 |
< |
try { |
416 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
417 |
< |
assertTrue(q.add(new Integer(1))); |
418 |
< |
assertTrue(q.add(new Integer(2))); |
419 |
< |
assertTrue(q.remove(new Integer(1))); |
420 |
< |
assertTrue(q.remove(new Integer(2))); |
421 |
< |
assertTrue(q.add(new Integer(3))); |
480 |
< |
assertTrue(q.take() != null); |
481 |
< |
} catch (Exception e) { |
482 |
< |
unexpectedException(); |
483 |
< |
} |
414 |
> |
public void testRemoveElementAndAdd() throws InterruptedException { |
415 |
> |
LinkedTransferQueue q = new LinkedTransferQueue(); |
416 |
> |
assertTrue(q.add(one)); |
417 |
> |
assertTrue(q.add(two)); |
418 |
> |
assertTrue(q.remove(one)); |
419 |
> |
assertTrue(q.remove(two)); |
420 |
> |
assertTrue(q.add(three)); |
421 |
> |
assertTrue(q.take() != null); |
422 |
|
} |
423 |
|
|
424 |
|
/** |
425 |
|
* contains(x) reports true when elements added but not yet removed |
426 |
|
*/ |
427 |
|
public void testContains() { |
428 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
428 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
429 |
|
for (int i = 0; i < SIZE; ++i) { |
430 |
< |
assertTrue(q.contains(new Integer(i))); |
431 |
< |
q.poll(); |
432 |
< |
assertFalse(q.contains(new Integer(i))); |
430 |
> |
assertTrue(q.contains(i)); |
431 |
> |
assertEquals(i, (int) q.poll()); |
432 |
> |
assertFalse(q.contains(i)); |
433 |
|
} |
434 |
|
} |
435 |
|
|
438 |
|
*/ |
439 |
|
public void testClear() { |
440 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
503 |
– |
int remainingCapacity = q.remainingCapacity(); |
441 |
|
q.clear(); |
442 |
|
assertTrue(q.isEmpty()); |
443 |
|
assertEquals(0, q.size()); |
444 |
< |
assertEquals(remainingCapacity, q.remainingCapacity()); |
444 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
445 |
|
q.add(one); |
446 |
|
assertFalse(q.isEmpty()); |
447 |
|
assertTrue(q.contains(one)); |
453 |
|
* containsAll(c) is true when c contains a subset of elements |
454 |
|
*/ |
455 |
|
public void testContainsAll() { |
456 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
457 |
< |
LinkedTransferQueue p = new LinkedTransferQueue(); |
456 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
457 |
> |
LinkedTransferQueue<Integer> p = new LinkedTransferQueue<Integer>(); |
458 |
|
for (int i = 0; i < SIZE; ++i) { |
459 |
|
assertTrue(q.containsAll(p)); |
460 |
|
assertFalse(p.containsAll(q)); |
461 |
< |
p.add(new Integer(i)); |
461 |
> |
p.add(i); |
462 |
|
} |
463 |
|
assertTrue(p.containsAll(q)); |
464 |
|
} |
465 |
|
|
466 |
|
/** |
467 |
< |
* retainAll(c) retains only those elements of c and reports true if changed |
467 |
> |
* retainAll(c) retains only those elements of c and reports true |
468 |
> |
* if changed |
469 |
|
*/ |
470 |
|
public void testRetainAll() { |
471 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
484 |
|
} |
485 |
|
|
486 |
|
/** |
487 |
< |
* removeAll(c) removes only those elements of c and reports true if changed |
487 |
> |
* removeAll(c) removes only those elements of c and reports true |
488 |
> |
* if changed |
489 |
|
*/ |
490 |
|
public void testRemoveAll() { |
491 |
|
for (int i = 1; i < SIZE; ++i) { |
494 |
|
assertTrue(q.removeAll(p)); |
495 |
|
assertEquals(SIZE - i, q.size()); |
496 |
|
for (int j = 0; j < i; ++j) { |
497 |
< |
Integer I = (Integer) (p.remove()); |
559 |
< |
assertFalse(q.contains(I)); |
497 |
> |
assertFalse(q.contains(p.remove())); |
498 |
|
} |
499 |
|
} |
500 |
|
} |
502 |
|
/** |
503 |
|
* toArray contains all elements |
504 |
|
*/ |
505 |
< |
public void testToArray() { |
505 |
> |
public void testToArray() throws InterruptedException { |
506 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
507 |
|
Object[] o = q.toArray(); |
508 |
< |
try { |
509 |
< |
for (int i = 0; i < o.length; i++) { |
572 |
< |
assertEquals(o[i], q.take()); |
573 |
< |
} |
574 |
< |
} catch (InterruptedException e) { |
575 |
< |
unexpectedException(); |
508 |
> |
for (int i = 0; i < o.length; i++) { |
509 |
> |
assertEquals(o[i], q.take()); |
510 |
|
} |
511 |
|
} |
512 |
|
|
513 |
|
/** |
514 |
|
* toArray(a) contains all elements |
515 |
|
*/ |
516 |
< |
public void testToArray2() { |
517 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
516 |
> |
public void testToArray2() throws InterruptedException { |
517 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
518 |
|
Integer[] ints = new Integer[SIZE]; |
519 |
< |
ints = (Integer[]) q.toArray(ints); |
520 |
< |
try { |
521 |
< |
for (int i = 0; i < ints.length; i++) { |
588 |
< |
assertEquals(ints[i], q.take()); |
589 |
< |
} |
590 |
< |
} catch (InterruptedException e) { |
591 |
< |
unexpectedException(); |
519 |
> |
ints = q.toArray(ints); |
520 |
> |
for (int i = 0; i < ints.length; i++) { |
521 |
> |
assertEquals(ints[i], q.take()); |
522 |
|
} |
523 |
|
} |
524 |
|
|
525 |
|
/** |
526 |
< |
* toArray(null) throws NPE |
526 |
> |
* toArray(null) throws NullPointerException |
527 |
|
*/ |
528 |
|
public void testToArray_BadArg() { |
529 |
|
try { |
549 |
|
/** |
550 |
|
* iterator iterates through all elements |
551 |
|
*/ |
552 |
< |
public void testIterator() { |
552 |
> |
public void testIterator() throws InterruptedException { |
553 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
554 |
|
Iterator it = q.iterator(); |
555 |
< |
try { |
556 |
< |
while (it.hasNext()) { |
557 |
< |
assertEquals(it.next(), q.take()); |
628 |
< |
} |
629 |
< |
} catch (InterruptedException e) { |
630 |
< |
unexpectedException(); |
555 |
> |
int i = 0; |
556 |
> |
while (it.hasNext()) { |
557 |
> |
assertEquals(it.next(), i++); |
558 |
|
} |
559 |
+ |
assertEquals(i, SIZE); |
560 |
|
} |
561 |
|
|
562 |
|
/** |
582 |
|
* iterator ordering is FIFO |
583 |
|
*/ |
584 |
|
public void testIteratorOrdering() { |
585 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
586 |
< |
int remainingCapacity = q.remainingCapacity(); |
585 |
> |
final LinkedTransferQueue<Integer> q |
586 |
> |
= new LinkedTransferQueue<Integer>(); |
587 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
588 |
|
q.add(one); |
589 |
|
q.add(two); |
590 |
|
q.add(three); |
591 |
< |
assertEquals(remainingCapacity, q.remainingCapacity()); |
591 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
592 |
|
int k = 0; |
593 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
594 |
< |
int i = ((Integer) (it.next())).intValue(); |
666 |
< |
assertEquals(++k, i); |
593 |
> |
for (Integer n : q) { |
594 |
> |
assertEquals(++k, (int) n); |
595 |
|
} |
596 |
|
assertEquals(3, k); |
597 |
|
} |
604 |
|
q.add(one); |
605 |
|
q.add(two); |
606 |
|
q.add(three); |
607 |
< |
try { |
608 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
609 |
< |
q.remove(); |
682 |
< |
it.next(); |
683 |
< |
} |
684 |
< |
} catch (ConcurrentModificationException e) { |
685 |
< |
unexpectedException(); |
607 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
608 |
> |
q.remove(); |
609 |
> |
it.next(); |
610 |
|
} |
611 |
|
assertEquals(0, q.size()); |
612 |
|
} |
630 |
|
q.add(one); |
631 |
|
q.add(two); |
632 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
709 |
– |
executor.execute(new Runnable() { |
710 |
– |
|
711 |
– |
public void run() { |
712 |
– |
try { |
713 |
– |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
714 |
– |
} catch (Exception e) { |
715 |
– |
threadUnexpectedException(); |
716 |
– |
} |
717 |
– |
} |
718 |
– |
}); |
719 |
– |
|
720 |
– |
executor.execute(new Runnable() { |
633 |
|
|
634 |
< |
public void run() { |
635 |
< |
try { |
636 |
< |
Thread.sleep(SMALL_DELAY_MS); |
637 |
< |
threadAssertEquals(one, q.take()); |
638 |
< |
} catch (InterruptedException e) { |
639 |
< |
threadUnexpectedException(); |
640 |
< |
} |
641 |
< |
} |
642 |
< |
}); |
634 |
> |
executor.execute(new CheckedRunnable() { |
635 |
> |
void realRun() { |
636 |
> |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, |
637 |
> |
MILLISECONDS)); |
638 |
> |
}}); |
639 |
> |
|
640 |
> |
executor.execute(new CheckedRunnable() { |
641 |
> |
void realRun() throws InterruptedException { |
642 |
> |
Thread.sleep(SMALL_DELAY_MS); |
643 |
> |
threadAssertEquals(one, q.take()); |
644 |
> |
}}); |
645 |
|
|
646 |
|
joinPool(executor); |
647 |
|
} |
652 |
|
public void testPollInExecutor() { |
653 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
654 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
741 |
– |
executor.execute(new Runnable() { |
655 |
|
|
656 |
< |
public void run() { |
656 |
> |
executor.execute(new CheckedRunnable() { |
657 |
> |
void realRun() throws InterruptedException { |
658 |
|
threadAssertNull(q.poll()); |
659 |
< |
try { |
660 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
661 |
< |
threadAssertTrue(q.isEmpty()); |
662 |
< |
} catch (InterruptedException e) { |
663 |
< |
threadUnexpectedException(); |
664 |
< |
} |
665 |
< |
} |
666 |
< |
}); |
667 |
< |
|
668 |
< |
executor.execute(new Runnable() { |
755 |
< |
|
756 |
< |
public void run() { |
757 |
< |
try { |
758 |
< |
Thread.sleep(SMALL_DELAY_MS); |
759 |
< |
q.put(one); |
760 |
< |
} catch (InterruptedException e) { |
761 |
< |
threadUnexpectedException(); |
762 |
< |
} |
763 |
< |
} |
764 |
< |
}); |
659 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, |
660 |
> |
MILLISECONDS)); |
661 |
> |
threadAssertTrue(q.isEmpty()); |
662 |
> |
}}); |
663 |
> |
|
664 |
> |
executor.execute(new CheckedRunnable() { |
665 |
> |
void realRun() throws InterruptedException { |
666 |
> |
Thread.sleep(SMALL_DELAY_MS); |
667 |
> |
q.put(one); |
668 |
> |
}}); |
669 |
|
|
670 |
|
joinPool(executor); |
671 |
|
} |
673 |
|
/** |
674 |
|
* A deserialized serialized queue has same elements in same order |
675 |
|
*/ |
676 |
< |
public void testSerialization() { |
676 |
> |
public void testSerialization() throws Exception { |
677 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
678 |
|
|
679 |
< |
try { |
680 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
681 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
682 |
< |
out.writeObject(q); |
683 |
< |
out.close(); |
684 |
< |
|
685 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
686 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
687 |
< |
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
688 |
< |
|
689 |
< |
assertEquals(q.size(), r.size()); |
690 |
< |
while (!q.isEmpty()) { |
691 |
< |
assertEquals(q.remove(), r.remove()); |
692 |
< |
} |
693 |
< |
} catch (Exception e) { |
790 |
< |
unexpectedException(); |
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 NPE |
698 |
> |
* drainTo(null) throws NullPointerException |
699 |
|
*/ |
700 |
|
public void testDrainToNull() { |
701 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
707 |
|
} |
708 |
|
|
709 |
|
/** |
710 |
< |
* drainTo(this) throws IAE |
710 |
> |
* drainTo(this) throws IllegalArgumentException |
711 |
|
*/ |
712 |
|
public void testDrainToSelf() { |
713 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
728 |
|
assertEquals(q.size(), 0); |
729 |
|
assertEquals(l.size(), SIZE); |
730 |
|
for (int i = 0; i < SIZE; ++i) { |
731 |
< |
assertEquals(l.get(i), new Integer(i)); |
731 |
> |
assertEquals(l.get(i), i); |
732 |
|
} |
733 |
|
q.add(zero); |
734 |
|
q.add(one); |
740 |
|
assertEquals(q.size(), 0); |
741 |
|
assertEquals(l.size(), 2); |
742 |
|
for (int i = 0; i < 2; ++i) { |
743 |
< |
assertEquals(l.get(i), new Integer(i)); |
743 |
> |
assertEquals(l.get(i), i); |
744 |
|
} |
745 |
|
} |
746 |
|
|
747 |
|
/** |
748 |
|
* drainTo empties full queue, unblocking a waiting put. |
749 |
|
*/ |
750 |
< |
public void testDrainToWithActivePut() { |
750 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
751 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
752 |
< |
Thread t = new Thread(new Runnable() { |
753 |
< |
|
754 |
< |
public void run() { |
755 |
< |
try { |
756 |
< |
q.put(new Integer(SIZE + 1)); |
757 |
< |
} catch (Exception ie) { |
758 |
< |
threadUnexpectedException(); |
759 |
< |
} |
760 |
< |
} |
858 |
< |
}); |
859 |
< |
try { |
860 |
< |
t.start(); |
861 |
< |
ArrayList l = new ArrayList(); |
862 |
< |
q.drainTo(l); |
863 |
< |
assertTrue(l.size() >= SIZE); |
864 |
< |
for (int i = 0; i < SIZE; ++i) { |
865 |
< |
assertEquals(l.get(i), new Integer(i)); |
866 |
< |
} |
867 |
< |
t.join(); |
868 |
< |
assertTrue(q.size() + l.size() >= SIZE); |
869 |
< |
} catch (Exception e) { |
870 |
< |
unexpectedException(); |
752 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
753 |
> |
void realRun() { |
754 |
> |
q.put(SIZE + 1); |
755 |
> |
}}); |
756 |
> |
ArrayList l = new ArrayList(); |
757 |
> |
q.drainTo(l); |
758 |
> |
assertTrue(l.size() >= SIZE); |
759 |
> |
for (int i = 0; i < SIZE; ++i) { |
760 |
> |
assertEquals(l.get(i), i); |
761 |
|
} |
762 |
+ |
t.join(); |
763 |
+ |
assertTrue(q.size() + l.size() >= SIZE); |
764 |
|
} |
765 |
|
|
766 |
|
/** |
767 |
< |
* drainTo(null, n) throws NPE |
767 |
> |
* drainTo(null, n) throws NullPointerException |
768 |
|
*/ |
769 |
|
public void testDrainToNullN() { |
770 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
776 |
|
} |
777 |
|
|
778 |
|
/** |
779 |
< |
* drainTo(this, n) throws IAE |
779 |
> |
* drainTo(this, n) throws IllegalArgumentException |
780 |
|
*/ |
781 |
|
public void testDrainToSelfN() { |
782 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
794 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
795 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
796 |
|
for (int j = 0; j < SIZE; j++) { |
797 |
< |
assertTrue(q.offer(new Integer(j))); |
797 |
> |
assertTrue(q.offer(j)); |
798 |
|
} |
799 |
|
ArrayList l = new ArrayList(); |
800 |
|
q.drainTo(l, i); |
802 |
|
assertEquals(l.size(), k); |
803 |
|
assertEquals(q.size(), SIZE - k); |
804 |
|
for (int j = 0; j < k; ++j) { |
805 |
< |
assertEquals(l.get(j), new Integer(j)); |
805 |
> |
assertEquals(l.get(j), j); |
806 |
|
} |
807 |
< |
while (q.poll() != null); |
807 |
> |
while (q.poll() != null) |
808 |
> |
; |
809 |
|
} |
810 |
|
} |
811 |
|
|
812 |
< |
/* |
813 |
< |
* poll and take should decrement the waiting consumer count |
812 |
> |
/** |
813 |
> |
* poll and take decrement the waiting consumer count |
814 |
|
*/ |
815 |
< |
public void testWaitingConsumer() { |
816 |
< |
try { |
817 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
925 |
< |
final ConsumerObserver waiting = new ConsumerObserver(); |
926 |
< |
new Thread(new Runnable() { |
815 |
> |
public void testWaitingConsumer() throws InterruptedException { |
816 |
> |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
817 |
> |
final ConsumerObserver waiting = new ConsumerObserver(); |
818 |
|
|
819 |
< |
public void run() { |
820 |
< |
try { |
821 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
822 |
< |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
823 |
< |
threadAssertTrue(q.offer(new Object())); |
824 |
< |
} catch (Exception ex) { |
825 |
< |
threadUnexpectedException(); |
935 |
< |
} |
819 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
820 |
> |
void realRun() throws InterruptedException { |
821 |
> |
Thread.sleep(SMALL_DELAY_MS); |
822 |
> |
threadAssertTrue(q.hasWaitingConsumer()); |
823 |
> |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
824 |
> |
threadAssertTrue(q.offer(new Object())); |
825 |
> |
}}); |
826 |
|
|
827 |
< |
} |
828 |
< |
}).start(); |
829 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null); |
830 |
< |
assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers()); |
941 |
< |
} catch (Exception ex) { |
942 |
< |
this.unexpectedException(); |
943 |
< |
} |
827 |
> |
assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); |
828 |
> |
assertTrue(q.getWaitingConsumerCount() |
829 |
> |
< waiting.getWaitingConsumers()); |
830 |
> |
t.join(); |
831 |
|
} |
945 |
– |
/* |
946 |
– |
* Inserts null into transfer throws NPE |
947 |
– |
*/ |
832 |
|
|
833 |
< |
public void testTransfer1() { |
833 |
> |
/** |
834 |
> |
* transfer(null) throws NullPointerException |
835 |
> |
*/ |
836 |
> |
public void testTransfer1() throws InterruptedException { |
837 |
|
try { |
838 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
839 |
|
q.transfer(null); |
840 |
|
shouldThrow(); |
841 |
|
} catch (NullPointerException ex) { |
955 |
– |
} catch (Exception ex) { |
956 |
– |
this.unexpectedException(); |
842 |
|
} |
843 |
|
} |
844 |
|
|
845 |
< |
/* |
846 |
< |
* transfer attempts to insert into the queue then wait until that |
847 |
< |
* object is removed via take or poll. |
845 |
> |
/** |
846 |
> |
* transfer waits until a poll occurs. The transfered element |
847 |
> |
* is returned by this associated poll. |
848 |
|
*/ |
849 |
< |
public void testTransfer2() { |
850 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
851 |
< |
new Thread(new Runnable() { |
852 |
< |
|
853 |
< |
public void run() { |
854 |
< |
try { |
855 |
< |
q.transfer(new Integer(SIZE)); |
856 |
< |
threadAssertTrue(q.isEmpty()); |
857 |
< |
} catch (Exception ex) { |
858 |
< |
threadUnexpectedException(); |
859 |
< |
} |
860 |
< |
} |
861 |
< |
}).start(); |
849 |
> |
public void testTransfer2() throws InterruptedException { |
850 |
> |
final LinkedTransferQueue<Integer> q |
851 |
> |
= new LinkedTransferQueue<Integer>(); |
852 |
> |
|
853 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
854 |
> |
void realRun() throws InterruptedException { |
855 |
> |
q.transfer(SIZE); |
856 |
> |
threadAssertTrue(q.isEmpty()); |
857 |
> |
}}); |
858 |
> |
|
859 |
> |
Thread.sleep(SHORT_DELAY_MS); |
860 |
> |
assertEquals(1, q.size()); |
861 |
> |
assertEquals(SIZE, (int) q.poll()); |
862 |
> |
assertTrue(q.isEmpty()); |
863 |
> |
t.join(); |
864 |
> |
} |
865 |
|
|
866 |
< |
try { |
867 |
< |
Thread.sleep(SHORT_DELAY_MS); |
868 |
< |
assertEquals(1, q.size()); |
869 |
< |
q.poll(); |
870 |
< |
assertTrue(q.isEmpty()); |
871 |
< |
} catch (Exception ex) { |
872 |
< |
this.unexpectedException(); |
873 |
< |
} |
874 |
< |
} |
875 |
< |
/* |
876 |
< |
* transfer will attempt to transfer in fifo order and continue waiting if |
877 |
< |
* the element being transfered is not polled or taken |
878 |
< |
*/ |
879 |
< |
|
880 |
< |
public void testTransfer3() { |
881 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
882 |
< |
new Thread(new Runnable() { |
883 |
< |
public void run() { |
884 |
< |
try { |
885 |
< |
Integer i; |
886 |
< |
q.transfer((i = new Integer(SIZE + 1))); |
887 |
< |
threadAssertTrue(!q.contains(i)); |
888 |
< |
threadAssertEquals(1, q.size()); |
889 |
< |
} catch (Exception ex) { |
890 |
< |
threadUnexpectedException(); |
891 |
< |
} |
892 |
< |
} |
893 |
< |
}).start(); |
894 |
< |
Thread interruptedThread = |
895 |
< |
new Thread(new Runnable() { |
896 |
< |
public void run() { |
897 |
< |
try { |
898 |
< |
q.transfer(new Integer(SIZE)); |
1011 |
< |
threadShouldThrow(); |
1012 |
< |
} catch (InterruptedException ex) { |
1013 |
< |
} |
1014 |
< |
} |
1015 |
< |
}); |
1016 |
< |
interruptedThread.start(); |
1017 |
< |
try { |
1018 |
< |
Thread.sleep(LONG_DELAY_MS); |
1019 |
< |
assertEquals(2, q.size()); |
1020 |
< |
q.poll(); |
1021 |
< |
Thread.sleep(LONG_DELAY_MS); |
1022 |
< |
interruptedThread.interrupt(); |
1023 |
< |
assertEquals(1, q.size()); |
1024 |
< |
} catch (Exception ex) { |
1025 |
< |
this.unexpectedException(); |
1026 |
< |
} |
866 |
> |
/** |
867 |
> |
* transfer waits until a poll occurs, and then transfers in fifo order |
868 |
> |
*/ |
869 |
> |
public void testTransfer3() throws InterruptedException { |
870 |
> |
final LinkedTransferQueue<Integer> q |
871 |
> |
= new LinkedTransferQueue<Integer>(); |
872 |
> |
|
873 |
> |
Thread first = newStartedThread(new CheckedRunnable() { |
874 |
> |
void realRun() throws InterruptedException { |
875 |
> |
Integer i = SIZE + 1; |
876 |
> |
q.transfer(i); |
877 |
> |
threadAssertTrue(!q.contains(i)); |
878 |
> |
threadAssertEquals(1, q.size()); |
879 |
> |
}}); |
880 |
> |
|
881 |
> |
Thread interruptedThread = newStartedThread( |
882 |
> |
new CheckedInterruptedRunnable() { |
883 |
> |
void realRun() throws InterruptedException { |
884 |
> |
while (q.size() == 0) |
885 |
> |
Thread.yield(); |
886 |
> |
q.transfer(SIZE); |
887 |
> |
}}); |
888 |
> |
|
889 |
> |
while (q.size() < 2) |
890 |
> |
Thread.yield(); |
891 |
> |
assertEquals(2, q.size()); |
892 |
> |
assertEquals(SIZE + 1, (int) q.poll()); |
893 |
> |
first.join(); |
894 |
> |
assertEquals(1, q.size()); |
895 |
> |
interruptedThread.interrupt(); |
896 |
> |
interruptedThread.join(); |
897 |
> |
assertEquals(0, q.size()); |
898 |
> |
assertTrue(q.isEmpty()); |
899 |
|
} |
900 |
|
|
901 |
|
/** |
902 |
< |
* transfer will wait as long as a poll or take occurs if one does occur |
903 |
< |
* the waiting is finished and the thread that tries to poll/take |
1032 |
< |
* wins in retrieving the element |
902 |
> |
* transfer waits until a poll occurs, at which point the polling |
903 |
> |
* thread returns the element |
904 |
|
*/ |
905 |
< |
public void testTransfer4() { |
905 |
> |
public void testTransfer4() throws InterruptedException { |
906 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1036 |
– |
new Thread(new Runnable() { |
907 |
|
|
908 |
< |
public void run() { |
909 |
< |
try { |
910 |
< |
q.transfer(new Integer(four)); |
911 |
< |
threadAssertFalse(q.contains(new Integer(four))); |
912 |
< |
threadAssertEquals(new Integer(three), q.poll()); |
913 |
< |
} catch (Exception ex) { |
914 |
< |
threadUnexpectedException(); |
915 |
< |
} |
916 |
< |
} |
917 |
< |
}).start(); |
918 |
< |
try { |
919 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
920 |
< |
assertTrue(q.offer(three)); |
921 |
< |
assertEquals(new Integer(four), q.poll()); |
922 |
< |
} catch (Exception ex) { |
923 |
< |
this.unexpectedException(); |
924 |
< |
} |
908 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
909 |
> |
void realRun() throws InterruptedException { |
910 |
> |
q.transfer(four); |
911 |
> |
threadAssertFalse(q.contains(four)); |
912 |
> |
threadAssertEquals(three, q.poll()); |
913 |
> |
}}); |
914 |
> |
|
915 |
> |
Thread.sleep(SHORT_DELAY_MS); |
916 |
> |
assertTrue(q.offer(three)); |
917 |
> |
assertEquals(four, q.poll()); |
918 |
> |
t.join(); |
919 |
> |
} |
920 |
> |
|
921 |
> |
/** |
922 |
> |
* transfer waits until a take occurs. The transfered element |
923 |
> |
* is returned by this associated take. |
924 |
> |
*/ |
925 |
> |
public void testTransfer5() throws InterruptedException { |
926 |
> |
final LinkedTransferQueue<Integer> q |
927 |
> |
= new LinkedTransferQueue<Integer>(); |
928 |
> |
|
929 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
930 |
> |
void realRun() throws InterruptedException { |
931 |
> |
q.transfer(SIZE); |
932 |
> |
threadAssertTrue(q.isEmpty()); |
933 |
> |
}}); |
934 |
> |
|
935 |
> |
Thread.sleep(SHORT_DELAY_MS); |
936 |
> |
assertEquals(SIZE, (int) q.take()); |
937 |
> |
assertTrue(q.isEmpty()); |
938 |
> |
t.join(); |
939 |
|
} |
1056 |
– |
/* |
1057 |
– |
* Insert null into trTransfer throws NPE |
1058 |
– |
*/ |
940 |
|
|
941 |
+ |
/** |
942 |
+ |
* tryTransfer(null) throws NullPointerException |
943 |
+ |
*/ |
944 |
|
public void testTryTransfer1() { |
945 |
|
try { |
946 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
947 |
|
q.tryTransfer(null); |
948 |
< |
this.shouldThrow(); |
948 |
> |
shouldThrow(); |
949 |
|
} catch (NullPointerException ex) { |
1066 |
– |
} catch (Exception ex) { |
1067 |
– |
this.unexpectedException(); |
950 |
|
} |
951 |
|
} |
1070 |
– |
/* |
1071 |
– |
* tryTransfer returns false and does not enqueue if there are no consumers |
1072 |
– |
* waiting to poll or take. |
1073 |
– |
*/ |
952 |
|
|
953 |
+ |
/** |
954 |
+ |
* tryTransfer returns false and does not enqueue if there are no |
955 |
+ |
* consumers waiting to poll or take. |
956 |
+ |
*/ |
957 |
|
public void testTryTransfer2() { |
958 |
< |
try { |
959 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
960 |
< |
assertFalse(q.tryTransfer(new Object())); |
961 |
< |
assertEquals(0, q.size()); |
962 |
< |
} catch (Exception ex) { |
1081 |
< |
this.unexpectedException(); |
1082 |
< |
} |
958 |
> |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
959 |
> |
assertFalse(q.tryTransfer(new Object())); |
960 |
> |
assertFalse(q.hasWaitingConsumer()); |
961 |
> |
assertTrue(q.isEmpty()); |
962 |
> |
assertEquals(0, q.size()); |
963 |
|
} |
964 |
< |
/* |
965 |
< |
* if there is a consumer waiting poll or take tryTransfer returns |
966 |
< |
* true while enqueueing object |
964 |
> |
|
965 |
> |
/** |
966 |
> |
* If there is a consumer waiting in timed poll, tryTransfer |
967 |
> |
* returns true while successfully transfering object. |
968 |
|
*/ |
969 |
+ |
public void testTryTransfer3() throws InterruptedException { |
970 |
+ |
final Object hotPotato = new Object(); |
971 |
+ |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
972 |
|
|
973 |
< |
public void testTryTransfer3() { |
974 |
< |
try { |
975 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
976 |
< |
new Thread(new Runnable() { |
973 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
974 |
> |
void realRun() { |
975 |
> |
while (! q.hasWaitingConsumer()) |
976 |
> |
Thread.yield(); |
977 |
> |
threadAssertTrue(q.hasWaitingConsumer()); |
978 |
> |
threadAssertTrue(q.isEmpty()); |
979 |
> |
threadAssertTrue(q.size() == 0); |
980 |
> |
threadAssertTrue(q.tryTransfer(hotPotato)); |
981 |
> |
}}); |
982 |
|
|
983 |
< |
public void run() { |
984 |
< |
try { |
985 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
986 |
< |
threadAssertTrue(q.tryTransfer(new Object())); |
1098 |
< |
} catch (Exception ex) { |
1099 |
< |
threadUnexpectedException(); |
1100 |
< |
} |
983 |
> |
assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato); |
984 |
> |
assertTrue(q.isEmpty()); |
985 |
> |
t.join(); |
986 |
> |
} |
987 |
|
|
988 |
< |
} |
989 |
< |
}).start(); |
990 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null); |
991 |
< |
assertTrue(q.isEmpty()); |
992 |
< |
} catch (Exception ex) { |
993 |
< |
this.unexpectedException(); |
994 |
< |
} |
988 |
> |
/** |
989 |
> |
* If there is a consumer waiting in take, tryTransfer returns |
990 |
> |
* true while successfully transfering object. |
991 |
> |
*/ |
992 |
> |
public void testTryTransfer4() throws InterruptedException { |
993 |
> |
final Object hotPotato = new Object(); |
994 |
> |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
995 |
> |
|
996 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
997 |
> |
void realRun() { |
998 |
> |
while (! q.hasWaitingConsumer()) |
999 |
> |
Thread.yield(); |
1000 |
> |
threadAssertTrue(q.hasWaitingConsumer()); |
1001 |
> |
threadAssertTrue(q.isEmpty()); |
1002 |
> |
threadAssertTrue(q.size() == 0); |
1003 |
> |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1004 |
> |
}}); |
1005 |
> |
|
1006 |
> |
assertTrue(q.take() == hotPotato); |
1007 |
> |
assertTrue(q.isEmpty()); |
1008 |
> |
t.join(); |
1009 |
|
} |
1010 |
|
|
1011 |
< |
/* |
1012 |
< |
* tryTransfer waits the amount given if interrupted, show an |
1013 |
< |
* interrupted exception |
1011 |
> |
/** |
1012 |
> |
* tryTransfer waits the amount given if interrupted, and |
1013 |
> |
* throws interrupted exception |
1014 |
|
*/ |
1015 |
< |
public void testTryTransfer4() { |
1015 |
> |
public void testTryTransfer5() throws InterruptedException { |
1016 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1117 |
– |
Thread toInterrupt = new Thread(new Runnable() { |
1017 |
|
|
1018 |
< |
public void run() { |
1019 |
< |
try { |
1020 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1021 |
< |
threadShouldThrow(); |
1022 |
< |
} catch (InterruptedException ex) { |
1023 |
< |
} |
1024 |
< |
} |
1025 |
< |
}); |
1127 |
< |
try { |
1128 |
< |
toInterrupt.start(); |
1129 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1130 |
< |
toInterrupt.interrupt(); |
1131 |
< |
} catch (Exception ex) { |
1132 |
< |
this.unexpectedException(); |
1133 |
< |
} |
1018 |
> |
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
1019 |
> |
void realRun() throws InterruptedException { |
1020 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1021 |
> |
}}); |
1022 |
> |
|
1023 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1024 |
> |
toInterrupt.interrupt(); |
1025 |
> |
toInterrupt.join(); |
1026 |
|
} |
1027 |
|
|
1028 |
< |
/* |
1028 |
> |
/** |
1029 |
|
* tryTransfer gives up after the timeout and return false |
1030 |
|
*/ |
1031 |
< |
public void testTryTransfer5() { |
1031 |
> |
public void testTryTransfer6() throws InterruptedException { |
1032 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1141 |
– |
try { |
1142 |
– |
new Thread(new Runnable() { |
1033 |
|
|
1034 |
< |
public void run() { |
1035 |
< |
try { |
1036 |
< |
threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1037 |
< |
} catch (InterruptedException ex) { |
1038 |
< |
threadUnexpectedException(); |
1039 |
< |
} |
1040 |
< |
} |
1041 |
< |
}).start(); |
1042 |
< |
Thread.sleep(LONG_DELAY_MS); |
1043 |
< |
assertTrue(q.isEmpty()); |
1154 |
< |
} catch (Exception ex) { |
1155 |
< |
this.unexpectedException(); |
1156 |
< |
} |
1034 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1035 |
> |
void realRun() throws InterruptedException { |
1036 |
> |
threadAssertFalse |
1037 |
> |
(q.tryTransfer(new Object(), |
1038 |
> |
SHORT_DELAY_MS, MILLISECONDS)); |
1039 |
> |
}}); |
1040 |
> |
|
1041 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1042 |
> |
assertTrue(q.isEmpty()); |
1043 |
> |
t.join(); |
1044 |
|
} |
1045 |
|
|
1046 |
< |
/* |
1046 |
> |
/** |
1047 |
|
* tryTransfer waits for any elements previously in to be removed |
1048 |
|
* before transfering to a poll or take |
1049 |
|
*/ |
1050 |
< |
public void testTryTransfer6() { |
1050 |
> |
public void testTryTransfer7() throws InterruptedException { |
1051 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1052 |
< |
q.offer(new Integer(four)); |
1166 |
< |
new Thread(new Runnable() { |
1052 |
> |
assertTrue(q.offer(four)); |
1053 |
|
|
1054 |
< |
public void run() { |
1055 |
< |
try { |
1056 |
< |
threadAssertTrue(q.tryTransfer(new Integer(five), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
1057 |
< |
threadAssertTrue(q.isEmpty()); |
1058 |
< |
} catch (InterruptedException ex) { |
1059 |
< |
threadUnexpectedException(); |
1060 |
< |
} |
1061 |
< |
} |
1062 |
< |
}).start(); |
1063 |
< |
try { |
1064 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1065 |
< |
assertEquals(2, q.size()); |
1066 |
< |
assertEquals(new Integer(four), q.poll()); |
1181 |
< |
assertEquals(new Integer(five), q.poll()); |
1182 |
< |
assertTrue(q.isEmpty()); |
1183 |
< |
} catch (Exception ex) { |
1184 |
< |
this.unexpectedException(); |
1185 |
< |
} |
1054 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1055 |
> |
void realRun() throws InterruptedException { |
1056 |
> |
threadAssertTrue(q.tryTransfer(five, |
1057 |
> |
MEDIUM_DELAY_MS, MILLISECONDS)); |
1058 |
> |
threadAssertTrue(q.isEmpty()); |
1059 |
> |
}}); |
1060 |
> |
|
1061 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1062 |
> |
assertEquals(2, q.size()); |
1063 |
> |
assertEquals(four, q.poll()); |
1064 |
> |
assertEquals(five, q.poll()); |
1065 |
> |
assertTrue(q.isEmpty()); |
1066 |
> |
t.join(); |
1067 |
|
} |
1068 |
|
|
1069 |
< |
/* |
1070 |
< |
* tryTransfer attempts to enqueue into the q and fails returning false not |
1071 |
< |
* enqueueing and the successing poll is null |
1069 |
> |
/** |
1070 |
> |
* tryTransfer attempts to enqueue into the q and fails returning |
1071 |
> |
* false not enqueueing and the successive poll is null |
1072 |
|
*/ |
1073 |
< |
public void testTryTransfer7() { |
1073 |
> |
public void testTryTransfer8() throws InterruptedException { |
1074 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1075 |
< |
q.offer(new Integer(four)); |
1076 |
< |
new Thread(new Runnable() { |
1077 |
< |
|
1078 |
< |
public void run() { |
1079 |
< |
try { |
1080 |
< |
threadAssertFalse(q.tryTransfer(new Integer(five), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1081 |
< |
threadAssertTrue(q.isEmpty()); |
1201 |
< |
} catch (InterruptedException ex) { |
1202 |
< |
threadUnexpectedException(); |
1203 |
< |
} |
1204 |
< |
} |
1205 |
< |
}).start(); |
1206 |
< |
try { |
1207 |
< |
assertEquals(1, q.size()); |
1208 |
< |
assertEquals(new Integer(four), q.poll()); |
1209 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1210 |
< |
assertNull(q.poll()); |
1211 |
< |
} catch (Exception ex) { |
1212 |
< |
this.unexpectedException(); |
1213 |
< |
} |
1075 |
> |
assertTrue(q.offer(four)); |
1076 |
> |
assertEquals(1, q.size()); |
1077 |
> |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1078 |
> |
assertEquals(1, q.size()); |
1079 |
> |
assertEquals(four, q.poll()); |
1080 |
> |
threadAssertTrue(q.isEmpty()); |
1081 |
> |
assertNull(q.poll()); |
1082 |
|
} |
1083 |
|
|
1084 |
< |
private LinkedTransferQueue populatedQueue( |
1085 |
< |
int n) { |
1218 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
1084 |
> |
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1085 |
> |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
1086 |
|
assertTrue(q.isEmpty()); |
1087 |
< |
int remainingCapacity = q.remainingCapacity(); |
1088 |
< |
for (int i = 0; i < |
1222 |
< |
n; i++) { |
1087 |
> |
for (int i = 0; i < n; i++) { |
1088 |
> |
assertEquals(i, q.size()); |
1089 |
|
assertTrue(q.offer(i)); |
1090 |
+ |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
1091 |
|
} |
1225 |
– |
|
1092 |
|
assertFalse(q.isEmpty()); |
1227 |
– |
assertEquals(remainingCapacity, q.remainingCapacity()); |
1228 |
– |
assertEquals(n, q.size()); |
1093 |
|
return q; |
1094 |
|
} |
1095 |
|
|