13 |
|
import java.io.ObjectOutputStream; |
14 |
|
import java.util.ArrayList; |
15 |
|
import java.util.Arrays; |
16 |
– |
import java.util.ConcurrentModificationException; |
16 |
|
import java.util.Iterator; |
17 |
+ |
import java.util.List; |
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 |
+ |
void checkEmpty(LinkedTransferQueue q) throws InterruptedException { |
36 |
+ |
assertTrue(q.isEmpty()); |
37 |
+ |
assertEquals(0, q.size()); |
38 |
+ |
assertNull(q.peek()); |
39 |
+ |
assertNull(q.poll()); |
40 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
41 |
+ |
assertEquals(q.toString(), "[]"); |
42 |
+ |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
43 |
+ |
assertFalse(q.iterator().hasNext()); |
44 |
+ |
try { |
45 |
+ |
q.element(); |
46 |
+ |
shouldThrow(); |
47 |
+ |
} catch (NoSuchElementException success) { |
48 |
+ |
} |
49 |
+ |
try { |
50 |
+ |
q.iterator().next(); |
51 |
+ |
shouldThrow(); |
52 |
+ |
} catch (NoSuchElementException success) { |
53 |
+ |
} |
54 |
+ |
try { |
55 |
+ |
q.remove(); |
56 |
+ |
shouldThrow(); |
57 |
+ |
} catch (NoSuchElementException success) { |
58 |
+ |
} |
59 |
+ |
} |
60 |
+ |
|
61 |
|
/** |
62 |
< |
* Constructor builds new queue with size being zero and empty being true |
62 |
> |
* Constructor builds new queue with size being zero and empty |
63 |
> |
* being true |
64 |
|
*/ |
65 |
|
public void testConstructor1() { |
66 |
|
assertEquals(0, new LinkedTransferQueue().size()); |
68 |
|
} |
69 |
|
|
70 |
|
/** |
71 |
< |
* Initizialing constructor with null collection throws NPE |
71 |
> |
* Initializing constructor with null collection throws |
72 |
> |
* NullPointerException |
73 |
|
*/ |
74 |
|
public void testConstructor2() { |
75 |
|
try { |
80 |
|
} |
81 |
|
|
82 |
|
/** |
83 |
< |
* Initializing from Collection of null elements throws NPE |
83 |
> |
* Initializing from Collection of null elements throws |
84 |
> |
* NullPointerException |
85 |
|
*/ |
86 |
|
public void testConstructor3() { |
87 |
|
try { |
88 |
|
Integer[] ints = new Integer[SIZE]; |
89 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); |
89 |
> |
new LinkedTransferQueue(Arrays.asList(ints)); |
90 |
|
shouldThrow(); |
91 |
|
} catch (NullPointerException success) { |
92 |
|
} |
94 |
|
|
95 |
|
/** |
96 |
|
* Initializing constructor with a collection containing some null elements |
97 |
< |
* throws NPE |
97 |
> |
* throws NullPointerException |
98 |
|
*/ |
99 |
|
public void testConstructor4() { |
100 |
|
try { |
101 |
|
Integer[] ints = new Integer[SIZE]; |
102 |
|
for (int i = 0; i < SIZE - 1; ++i) { |
103 |
< |
ints[i] = new Integer(i); |
103 |
> |
ints[i] = i; |
104 |
|
} |
105 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); |
105 |
> |
new LinkedTransferQueue(Arrays.asList(ints)); |
106 |
|
shouldThrow(); |
107 |
|
} catch (NullPointerException success) { |
108 |
|
} |
112 |
|
* Queue contains all elements of the collection it is initialized by |
113 |
|
*/ |
114 |
|
public void testConstructor5() { |
115 |
< |
try { |
116 |
< |
Integer[] ints = new Integer[SIZE]; |
117 |
< |
for (int i = 0; i < SIZE; ++i) { |
118 |
< |
ints[i] = new Integer(i); |
119 |
< |
} |
120 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); |
121 |
< |
for (int i = 0; i < SIZE; ++i) { |
122 |
< |
assertEquals(ints[i], q.poll()); |
123 |
< |
} |
124 |
< |
} finally { |
115 |
> |
Integer[] ints = new Integer[SIZE]; |
116 |
> |
for (int i = 0; i < SIZE; ++i) { |
117 |
> |
ints[i] = i; |
118 |
> |
} |
119 |
> |
List intList = Arrays.asList(ints); |
120 |
> |
LinkedTransferQueue q |
121 |
> |
= new LinkedTransferQueue(intList); |
122 |
> |
assertEquals(q.size(), intList.size()); |
123 |
> |
assertEquals(q.toString(), intList.toString()); |
124 |
> |
assertTrue(Arrays.equals(q.toArray(), |
125 |
> |
intList.toArray())); |
126 |
> |
assertTrue(Arrays.equals(q.toArray(new Object[0]), |
127 |
> |
intList.toArray(new Object[0]))); |
128 |
> |
assertTrue(Arrays.equals(q.toArray(new Object[SIZE]), |
129 |
> |
intList.toArray(new Object[SIZE]))); |
130 |
> |
for (int i = 0; i < SIZE; ++i) { |
131 |
> |
assertEquals(ints[i], q.poll()); |
132 |
|
} |
133 |
|
} |
134 |
|
|
135 |
|
/** |
136 |
< |
* Remaining capacity never decrease nor increase on add or remove |
136 |
> |
* remainingCapacity() always returns Integer.MAX_VALUE |
137 |
|
*/ |
138 |
|
public void testRemainingCapacity() { |
139 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
102 |
< |
int remainingCapacity = q.remainingCapacity(); |
139 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
140 |
|
for (int i = 0; i < SIZE; ++i) { |
141 |
< |
assertEquals(remainingCapacity, q.remainingCapacity()); |
141 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
142 |
|
assertEquals(SIZE - i, q.size()); |
143 |
|
q.remove(); |
144 |
|
} |
145 |
|
for (int i = 0; i < SIZE; ++i) { |
146 |
< |
assertEquals(remainingCapacity, q.remainingCapacity()); |
146 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
147 |
|
assertEquals(i, q.size()); |
148 |
< |
q.add(new Integer(i)); |
148 |
> |
q.add(i); |
149 |
|
} |
150 |
|
} |
151 |
|
|
152 |
|
/** |
153 |
< |
* offer(null) throws NPE |
153 |
> |
* offer(null) throws NullPointerException |
154 |
|
*/ |
155 |
|
public void testOfferNull() { |
156 |
|
try { |
162 |
|
} |
163 |
|
|
164 |
|
/** |
165 |
< |
* add(null) throws NPE |
165 |
> |
* add(null) throws NullPointerException |
166 |
|
*/ |
167 |
|
public void testAddNull() { |
168 |
|
try { |
174 |
|
} |
175 |
|
|
176 |
|
/** |
177 |
< |
* addAll(null) throws NPE |
177 |
> |
* addAll(null) throws NullPointerException |
178 |
|
*/ |
179 |
|
public void testAddAll1() { |
180 |
|
try { |
186 |
|
} |
187 |
|
|
188 |
|
/** |
189 |
< |
* addAll(this) throws IAE |
189 |
> |
* addAll(this) throws IllegalArgumentException |
190 |
|
*/ |
191 |
|
public void testAddAllSelf() { |
192 |
|
try { |
198 |
|
} |
199 |
|
|
200 |
|
/** |
201 |
< |
* addAll of a collection with null elements throws NPE |
201 |
> |
* addAll of a collection with null elements throws NullPointerException |
202 |
|
*/ |
203 |
|
public void testAddAll2() { |
204 |
|
try { |
211 |
|
} |
212 |
|
|
213 |
|
/** |
214 |
< |
* addAll of a collection with any null elements throws NPE after |
215 |
< |
* possibly adding some elements |
214 |
> |
* addAll of a collection with any null elements throws |
215 |
> |
* NullPointerException after possibly adding some elements |
216 |
|
*/ |
217 |
|
public void testAddAll3() { |
218 |
|
try { |
219 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
220 |
|
Integer[] ints = new Integer[SIZE]; |
221 |
|
for (int i = 0; i < SIZE - 1; ++i) { |
222 |
< |
ints[i] = new Integer(i); |
222 |
> |
ints[i] = i; |
223 |
|
} |
224 |
|
q.addAll(Arrays.asList(ints)); |
225 |
|
shouldThrow(); |
231 |
|
* Queue contains all elements, in traversal order, of successful addAll |
232 |
|
*/ |
233 |
|
public void testAddAll5() { |
234 |
< |
try { |
235 |
< |
Integer[] empty = new Integer[0]; |
236 |
< |
Integer[] ints = new Integer[SIZE]; |
237 |
< |
for (int i = 0; i < SIZE; ++i) { |
238 |
< |
ints[i] = new Integer(i); |
239 |
< |
} |
240 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
241 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
242 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
243 |
< |
for (int i = 0; i < SIZE; ++i) { |
207 |
< |
assertEquals(ints[i], q.poll()); |
208 |
< |
} |
209 |
< |
} finally { |
234 |
> |
Integer[] empty = new Integer[0]; |
235 |
> |
Integer[] ints = new Integer[SIZE]; |
236 |
> |
for (int i = 0; i < SIZE; ++i) { |
237 |
> |
ints[i] = i; |
238 |
> |
} |
239 |
> |
LinkedTransferQueue q = new LinkedTransferQueue(); |
240 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
241 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
242 |
> |
for (int i = 0; i < SIZE; ++i) { |
243 |
> |
assertEquals(ints[i], q.poll()); |
244 |
|
} |
245 |
|
} |
246 |
|
|
247 |
|
/** |
248 |
< |
* put(null) throws NPE |
248 |
> |
* put(null) throws NullPointerException |
249 |
|
*/ |
250 |
< |
public void testPutNull() { |
250 |
> |
public void testPutNull() throws InterruptedException { |
251 |
|
try { |
252 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
253 |
|
q.put(null); |
254 |
|
shouldThrow(); |
255 |
< |
} catch (NullPointerException success) { |
222 |
< |
} catch (Exception ie) { |
223 |
< |
unexpectedException(); |
224 |
< |
} |
255 |
> |
} catch (NullPointerException success) {} |
256 |
|
} |
257 |
|
|
258 |
|
/** |
259 |
|
* all elements successfully put are contained |
260 |
|
*/ |
261 |
|
public void testPut() { |
262 |
< |
try { |
263 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
264 |
< |
for (int i = 0; i < SIZE; ++i) { |
265 |
< |
Integer I = new Integer(i); |
266 |
< |
q.put(I); |
236 |
< |
assertTrue(q.contains(I)); |
237 |
< |
} |
238 |
< |
} catch (Exception ie) { |
239 |
< |
unexpectedException(); |
262 |
> |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
263 |
> |
for (int i = 0; i < SIZE; ++i) { |
264 |
> |
assertEquals(q.size(), i); |
265 |
> |
q.put(i); |
266 |
> |
assertTrue(q.contains(i)); |
267 |
|
} |
268 |
|
} |
269 |
|
|
270 |
|
/** |
271 |
|
* take retrieves elements in FIFO order |
272 |
|
*/ |
273 |
< |
public void testTake() { |
274 |
< |
try { |
275 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
276 |
< |
for (int i = 0; i < SIZE; ++i) { |
250 |
< |
assertEquals(i, ((Integer) q.take()).intValue()); |
251 |
< |
} |
252 |
< |
} catch (InterruptedException e) { |
253 |
< |
unexpectedException(); |
273 |
> |
public void testTake() throws InterruptedException { |
274 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
275 |
> |
for (int i = 0; i < SIZE; ++i) { |
276 |
> |
assertEquals(i, (int) q.take()); |
277 |
|
} |
278 |
|
} |
279 |
|
|
280 |
|
/** |
281 |
|
* take blocks interruptibly when empty |
282 |
|
*/ |
283 |
< |
public void testTakeFromEmpty() { |
283 |
> |
public void testTakeFromEmpty() throws InterruptedException { |
284 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
285 |
< |
Thread t = new Thread(new Runnable() { |
286 |
< |
|
287 |
< |
public void run() { |
288 |
< |
try { |
289 |
< |
q.take(); |
290 |
< |
threadShouldThrow(); |
291 |
< |
} catch (InterruptedException success) { |
269 |
< |
} |
270 |
< |
} |
271 |
< |
}); |
272 |
< |
try { |
273 |
< |
t.start(); |
274 |
< |
Thread.sleep(SHORT_DELAY_MS); |
275 |
< |
t.interrupt(); |
276 |
< |
t.join(); |
277 |
< |
} catch (Exception e) { |
278 |
< |
unexpectedException(); |
279 |
< |
} |
285 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
286 |
> |
void realRun() throws InterruptedException { |
287 |
> |
q.take(); |
288 |
> |
}}); |
289 |
> |
Thread.sleep(SHORT_DELAY_MS); |
290 |
> |
t.interrupt(); |
291 |
> |
t.join(); |
292 |
|
} |
293 |
|
|
294 |
|
/** |
295 |
|
* Take removes existing elements until empty, then blocks interruptibly |
296 |
|
*/ |
297 |
< |
public void testBlockingTake() { |
298 |
< |
Thread t = new Thread(new Runnable() { |
299 |
< |
|
300 |
< |
public void run() { |
301 |
< |
try { |
302 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
291 |
< |
for (int i = 0; i < SIZE; ++i) { |
292 |
< |
assertEquals(i, ((Integer) q.take()).intValue()); |
293 |
< |
} |
294 |
< |
q.take(); |
295 |
< |
threadShouldThrow(); |
296 |
< |
} catch (InterruptedException success) { |
297 |
> |
public void testBlockingTake() throws InterruptedException { |
298 |
> |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
299 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
300 |
> |
void realRun() throws InterruptedException { |
301 |
> |
for (int i = 0; i < SIZE; ++i) { |
302 |
> |
threadAssertEquals(i, (int) q.take()); |
303 |
|
} |
304 |
< |
} |
305 |
< |
}); |
306 |
< |
t.start(); |
307 |
< |
try { |
308 |
< |
Thread.sleep(SHORT_DELAY_MS); |
309 |
< |
t.interrupt(); |
304 |
< |
t.join(); |
305 |
< |
} catch (InterruptedException ie) { |
306 |
< |
unexpectedException(); |
307 |
< |
} |
304 |
> |
q.take(); |
305 |
> |
}}); |
306 |
> |
Thread.sleep(SMALL_DELAY_MS); |
307 |
> |
t.interrupt(); |
308 |
> |
t.join(); |
309 |
> |
checkEmpty(q); |
310 |
|
} |
311 |
|
|
312 |
|
/** |
313 |
|
* poll succeeds unless empty |
314 |
|
*/ |
315 |
< |
public void testPoll() { |
316 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
315 |
> |
public void testPoll() throws InterruptedException { |
316 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
317 |
|
for (int i = 0; i < SIZE; ++i) { |
318 |
< |
assertEquals(i, ((Integer) q.poll()).intValue()); |
318 |
> |
assertEquals(i, (int) q.poll()); |
319 |
|
} |
320 |
|
assertNull(q.poll()); |
321 |
+ |
checkEmpty(q); |
322 |
|
} |
323 |
|
|
324 |
|
/** |
325 |
|
* timed pool with zero timeout succeeds when non-empty, else times out |
326 |
|
*/ |
327 |
< |
public void testTimedPoll0() { |
328 |
< |
try { |
329 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
330 |
< |
for (int i = 0; i < SIZE; ++i) { |
328 |
< |
assertEquals(i, ((Integer) q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
329 |
< |
} |
330 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
331 |
< |
} catch (InterruptedException e) { |
332 |
< |
unexpectedException(); |
327 |
> |
public void testTimedPoll0() throws InterruptedException { |
328 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
329 |
> |
for (int i = 0; i < SIZE; ++i) { |
330 |
> |
assertEquals(i, (int) q.poll(0, MILLISECONDS)); |
331 |
|
} |
332 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
333 |
+ |
checkEmpty(q); |
334 |
|
} |
335 |
|
|
336 |
|
/** |
337 |
|
* timed pool with nonzero timeout succeeds when non-empty, else times out |
338 |
|
*/ |
339 |
< |
public void testTimedPoll() { |
340 |
< |
try { |
341 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
342 |
< |
for (int i = 0; i < SIZE; ++i) { |
343 |
< |
assertEquals(i, ((Integer) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
344 |
< |
} |
345 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
346 |
< |
} catch (InterruptedException e) { |
347 |
< |
unexpectedException(); |
339 |
> |
public void testTimedPoll() throws InterruptedException { |
340 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
341 |
> |
for (int i = 0; i < SIZE; ++i) { |
342 |
> |
long t0 = System.nanoTime(); |
343 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
344 |
> |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
345 |
> |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
346 |
|
} |
347 |
+ |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
348 |
+ |
checkEmpty(q); |
349 |
|
} |
350 |
|
|
351 |
|
/** |
352 |
|
* Interrupted timed poll throws InterruptedException instead of |
353 |
|
* returning timeout status |
354 |
|
*/ |
355 |
< |
public void testInterruptedTimedPoll() { |
356 |
< |
Thread t = new Thread(new Runnable() { |
357 |
< |
|
358 |
< |
public void run() { |
359 |
< |
try { |
360 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
361 |
< |
for (int i = 0; i < SIZE; ++i) { |
362 |
< |
threadAssertEquals(i, ((Integer) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
363 |
< |
} |
364 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
365 |
< |
} catch (InterruptedException success) { |
355 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
356 |
> |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
357 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
358 |
> |
void realRun() throws InterruptedException { |
359 |
> |
for (int i = 0; i < SIZE; ++i) { |
360 |
> |
long t0 = System.nanoTime(); |
361 |
> |
threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS, |
362 |
> |
MILLISECONDS)); |
363 |
> |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
364 |
> |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
365 |
|
} |
366 |
< |
} |
367 |
< |
}); |
368 |
< |
t.start(); |
369 |
< |
try { |
370 |
< |
Thread.sleep(SHORT_DELAY_MS); |
371 |
< |
t.interrupt(); |
372 |
< |
t.join(); |
373 |
< |
} catch (InterruptedException ie) { |
375 |
< |
unexpectedException(); |
376 |
< |
} |
366 |
> |
try { |
367 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
368 |
> |
} catch (InterruptedException success) {} |
369 |
> |
}}); |
370 |
> |
Thread.sleep(SMALL_DELAY_MS); |
371 |
> |
t.interrupt(); |
372 |
> |
t.join(); |
373 |
> |
checkEmpty(q); |
374 |
|
} |
375 |
|
|
376 |
|
/** |
377 |
|
* timed poll before a delayed offer fails; after offer succeeds; |
378 |
|
* on interruption throws |
379 |
|
*/ |
380 |
< |
public void testTimedPollWithOffer() { |
380 |
> |
public void testTimedPollWithOffer() throws InterruptedException { |
381 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
382 |
< |
Thread t = new Thread(new Runnable() { |
383 |
< |
|
384 |
< |
public void run() { |
385 |
< |
try { |
386 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
387 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
388 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
389 |
< |
threadShouldThrow(); |
390 |
< |
} catch (InterruptedException success) { |
391 |
< |
} |
395 |
< |
} |
396 |
< |
}); |
397 |
< |
try { |
398 |
< |
t.start(); |
399 |
< |
Thread.sleep(SMALL_DELAY_MS); |
400 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
401 |
< |
t.interrupt(); |
402 |
< |
t.join(); |
403 |
< |
} catch (Exception e) { |
404 |
< |
unexpectedException(); |
405 |
< |
} |
382 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
383 |
> |
void realRun() throws InterruptedException { |
384 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
385 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
386 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
387 |
> |
}}); |
388 |
> |
Thread.sleep(SMALL_DELAY_MS); |
389 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
390 |
> |
t.interrupt(); |
391 |
> |
t.join(); |
392 |
|
} |
393 |
|
|
394 |
|
/** |
395 |
|
* peek returns next element, or null if empty |
396 |
|
*/ |
397 |
< |
public void testPeek() { |
398 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
397 |
> |
public void testPeek() throws InterruptedException { |
398 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
399 |
|
for (int i = 0; i < SIZE; ++i) { |
400 |
< |
assertEquals(i, ((Integer) q.peek()).intValue()); |
401 |
< |
q.poll(); |
400 |
> |
assertEquals(i, (int) q.peek()); |
401 |
> |
assertEquals(i, (int) q.poll()); |
402 |
|
assertTrue(q.peek() == null || |
403 |
< |
i != ((Integer) q.peek()).intValue()); |
403 |
> |
i != (int) q.peek()); |
404 |
|
} |
405 |
|
assertNull(q.peek()); |
406 |
+ |
checkEmpty(q); |
407 |
|
} |
408 |
|
|
409 |
|
/** |
410 |
< |
* element returns next element, or throws NSEE if empty |
410 |
> |
* element returns next element, or throws NoSuchElementException if empty |
411 |
|
*/ |
412 |
< |
public void testElement() { |
413 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
412 |
> |
public void testElement() throws InterruptedException { |
413 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
414 |
|
for (int i = 0; i < SIZE; ++i) { |
415 |
< |
assertEquals(i, ((Integer) q.element()).intValue()); |
416 |
< |
q.poll(); |
415 |
> |
assertEquals(i, (int) q.element()); |
416 |
> |
assertEquals(i, (int) q.poll()); |
417 |
|
} |
418 |
|
try { |
419 |
|
q.element(); |
420 |
|
shouldThrow(); |
421 |
|
} catch (NoSuchElementException success) { |
422 |
|
} |
423 |
+ |
checkEmpty(q); |
424 |
|
} |
425 |
|
|
426 |
|
/** |
427 |
< |
* remove removes next element, or throws NSEE if empty |
427 |
> |
* remove removes next element, or throws NoSuchElementException if empty |
428 |
|
*/ |
429 |
< |
public void testRemove() { |
430 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
429 |
> |
public void testRemove() throws InterruptedException { |
430 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
431 |
|
for (int i = 0; i < SIZE; ++i) { |
432 |
< |
assertEquals(i, ((Integer) q.remove()).intValue()); |
432 |
> |
assertEquals(i, (int) q.remove()); |
433 |
|
} |
434 |
|
try { |
435 |
|
q.remove(); |
436 |
|
shouldThrow(); |
437 |
|
} catch (NoSuchElementException success) { |
438 |
|
} |
439 |
+ |
checkEmpty(q); |
440 |
|
} |
441 |
|
|
442 |
|
/** |
443 |
|
* remove(x) removes x and returns true if present |
444 |
|
*/ |
445 |
< |
public void testRemoveElement() { |
445 |
> |
public void testRemoveElement() throws InterruptedException { |
446 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
447 |
|
for (int i = 1; i < SIZE; i += 2) { |
448 |
< |
assertTrue(q.remove(new Integer(i))); |
448 |
> |
assertTrue(q.remove(i)); |
449 |
|
} |
450 |
|
for (int i = 0; i < SIZE; i += 2) { |
451 |
< |
assertTrue(q.remove(new Integer(i))); |
452 |
< |
assertFalse(q.remove(new Integer(i + 1))); |
451 |
> |
assertTrue(q.remove(i)); |
452 |
> |
assertFalse(q.remove(i + 1)); |
453 |
|
} |
454 |
< |
assertTrue(q.isEmpty()); |
454 |
> |
checkEmpty(q); |
455 |
|
} |
456 |
|
|
457 |
|
/** |
458 |
|
* An add following remove(x) succeeds |
459 |
|
*/ |
460 |
< |
public void testRemoveElementAndAdd() { |
461 |
< |
try { |
462 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
463 |
< |
assertTrue(q.add(new Integer(1))); |
464 |
< |
assertTrue(q.add(new Integer(2))); |
465 |
< |
assertTrue(q.remove(new Integer(1))); |
466 |
< |
assertTrue(q.remove(new Integer(2))); |
467 |
< |
assertTrue(q.add(new Integer(3))); |
479 |
< |
assertTrue(q.take() != null); |
480 |
< |
} catch (Exception e) { |
481 |
< |
unexpectedException(); |
482 |
< |
} |
460 |
> |
public void testRemoveElementAndAdd() throws InterruptedException { |
461 |
> |
LinkedTransferQueue q = new LinkedTransferQueue(); |
462 |
> |
assertTrue(q.add(one)); |
463 |
> |
assertTrue(q.add(two)); |
464 |
> |
assertTrue(q.remove(one)); |
465 |
> |
assertTrue(q.remove(two)); |
466 |
> |
assertTrue(q.add(three)); |
467 |
> |
assertTrue(q.take() == three); |
468 |
|
} |
469 |
|
|
470 |
|
/** |
471 |
|
* contains(x) reports true when elements added but not yet removed |
472 |
|
*/ |
473 |
|
public void testContains() { |
474 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
474 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
475 |
|
for (int i = 0; i < SIZE; ++i) { |
476 |
< |
assertTrue(q.contains(new Integer(i))); |
477 |
< |
q.poll(); |
478 |
< |
assertFalse(q.contains(new Integer(i))); |
476 |
> |
assertTrue(q.contains(i)); |
477 |
> |
assertEquals(i, (int) q.poll()); |
478 |
> |
assertFalse(q.contains(i)); |
479 |
|
} |
480 |
|
} |
481 |
|
|
482 |
|
/** |
483 |
|
* clear removes all elements |
484 |
|
*/ |
485 |
< |
public void testClear() { |
485 |
> |
public void testClear() throws InterruptedException { |
486 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
502 |
– |
int remainingCapacity = q.remainingCapacity(); |
487 |
|
q.clear(); |
488 |
< |
assertTrue(q.isEmpty()); |
489 |
< |
assertEquals(0, q.size()); |
506 |
< |
assertEquals(remainingCapacity, q.remainingCapacity()); |
488 |
> |
checkEmpty(q); |
489 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
490 |
|
q.add(one); |
491 |
|
assertFalse(q.isEmpty()); |
492 |
+ |
assertEquals(1, q.size()); |
493 |
|
assertTrue(q.contains(one)); |
494 |
|
q.clear(); |
495 |
< |
assertTrue(q.isEmpty()); |
495 |
> |
checkEmpty(q); |
496 |
|
} |
497 |
|
|
498 |
|
/** |
499 |
|
* containsAll(c) is true when c contains a subset of elements |
500 |
|
*/ |
501 |
|
public void testContainsAll() { |
502 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
503 |
< |
LinkedTransferQueue p = new LinkedTransferQueue(); |
502 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
503 |
> |
LinkedTransferQueue<Integer> p = new LinkedTransferQueue<Integer>(); |
504 |
|
for (int i = 0; i < SIZE; ++i) { |
505 |
|
assertTrue(q.containsAll(p)); |
506 |
|
assertFalse(p.containsAll(q)); |
507 |
< |
p.add(new Integer(i)); |
507 |
> |
p.add(i); |
508 |
|
} |
509 |
|
assertTrue(p.containsAll(q)); |
510 |
|
} |
511 |
|
|
512 |
|
/** |
513 |
< |
* retainAll(c) retains only those elements of c and reports true if changed |
513 |
> |
* retainAll(c) retains only those elements of c and reports true |
514 |
> |
* if changed |
515 |
|
*/ |
516 |
|
public void testRetainAll() { |
517 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
530 |
|
} |
531 |
|
|
532 |
|
/** |
533 |
< |
* removeAll(c) removes only those elements of c and reports true if changed |
533 |
> |
* removeAll(c) removes only those elements of c and reports true |
534 |
> |
* if changed |
535 |
|
*/ |
536 |
|
public void testRemoveAll() { |
537 |
|
for (int i = 1; i < SIZE; ++i) { |
540 |
|
assertTrue(q.removeAll(p)); |
541 |
|
assertEquals(SIZE - i, q.size()); |
542 |
|
for (int j = 0; j < i; ++j) { |
543 |
< |
Integer I = (Integer) (p.remove()); |
558 |
< |
assertFalse(q.contains(I)); |
543 |
> |
assertFalse(q.contains(p.remove())); |
544 |
|
} |
545 |
|
} |
546 |
|
} |
547 |
|
|
548 |
|
/** |
549 |
< |
* toArray contains all elements |
549 |
> |
* toArray() contains all elements |
550 |
|
*/ |
551 |
< |
public void testToArray() { |
551 |
> |
public void testToArray() throws InterruptedException { |
552 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
553 |
|
Object[] o = q.toArray(); |
554 |
< |
try { |
555 |
< |
for (int i = 0; i < o.length; i++) { |
571 |
< |
assertEquals(o[i], q.take()); |
572 |
< |
} |
573 |
< |
} catch (InterruptedException e) { |
574 |
< |
unexpectedException(); |
554 |
> |
for (int i = 0; i < o.length; i++) { |
555 |
> |
assertEquals(o[i], q.take()); |
556 |
|
} |
557 |
|
} |
558 |
|
|
559 |
|
/** |
560 |
|
* toArray(a) contains all elements |
561 |
|
*/ |
562 |
< |
public void testToArray2() { |
563 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
562 |
> |
public void testToArray2() throws InterruptedException { |
563 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
564 |
|
Integer[] ints = new Integer[SIZE]; |
565 |
< |
ints = (Integer[]) q.toArray(ints); |
566 |
< |
try { |
567 |
< |
for (int i = 0; i < ints.length; i++) { |
587 |
< |
assertEquals(ints[i], q.take()); |
588 |
< |
} |
589 |
< |
} catch (InterruptedException e) { |
590 |
< |
unexpectedException(); |
565 |
> |
ints = q.toArray(ints); |
566 |
> |
for (int i = 0; i < ints.length; i++) { |
567 |
> |
assertEquals(ints[i], q.take()); |
568 |
|
} |
569 |
|
} |
570 |
|
|
571 |
|
/** |
572 |
< |
* toArray(null) throws NPE |
572 |
> |
* toArray(null) throws NullPointerException |
573 |
|
*/ |
574 |
|
public void testToArray_BadArg() { |
575 |
|
try { |
581 |
|
} |
582 |
|
|
583 |
|
/** |
584 |
< |
* toArray with incompatible array type throws CCE |
584 |
> |
* toArray(incompatible array type) throws CCE |
585 |
|
*/ |
586 |
|
public void testToArray1_BadArg() { |
587 |
|
try { |
595 |
|
/** |
596 |
|
* iterator iterates through all elements |
597 |
|
*/ |
598 |
< |
public void testIterator() { |
598 |
> |
public void testIterator() throws InterruptedException { |
599 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
600 |
|
Iterator it = q.iterator(); |
601 |
< |
try { |
602 |
< |
while (it.hasNext()) { |
603 |
< |
assertEquals(it.next(), q.take()); |
627 |
< |
} |
628 |
< |
} catch (InterruptedException e) { |
629 |
< |
unexpectedException(); |
601 |
> |
int i = 0; |
602 |
> |
while (it.hasNext()) { |
603 |
> |
assertEquals(it.next(), i++); |
604 |
|
} |
605 |
+ |
assertEquals(i, SIZE); |
606 |
|
} |
607 |
|
|
608 |
|
/** |
609 |
< |
* iterator.remove removes current element |
609 |
> |
* iterator.remove() removes current element |
610 |
|
*/ |
611 |
|
public void testIteratorRemove() { |
612 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
628 |
|
* iterator ordering is FIFO |
629 |
|
*/ |
630 |
|
public void testIteratorOrdering() { |
631 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
632 |
< |
int remainingCapacity = q.remainingCapacity(); |
631 |
> |
final LinkedTransferQueue<Integer> q |
632 |
> |
= new LinkedTransferQueue<Integer>(); |
633 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
634 |
|
q.add(one); |
635 |
|
q.add(two); |
636 |
|
q.add(three); |
637 |
< |
assertEquals(remainingCapacity, q.remainingCapacity()); |
637 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
638 |
|
int k = 0; |
639 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
640 |
< |
int i = ((Integer) (it.next())).intValue(); |
665 |
< |
assertEquals(++k, i); |
639 |
> |
for (Integer n : q) { |
640 |
> |
assertEquals(++k, (int) n); |
641 |
|
} |
642 |
|
assertEquals(3, k); |
643 |
|
} |
650 |
|
q.add(one); |
651 |
|
q.add(two); |
652 |
|
q.add(three); |
653 |
< |
try { |
654 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
655 |
< |
q.remove(); |
681 |
< |
it.next(); |
682 |
< |
} |
683 |
< |
} catch (ConcurrentModificationException e) { |
684 |
< |
unexpectedException(); |
653 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
654 |
> |
q.remove(); |
655 |
> |
it.next(); |
656 |
|
} |
657 |
|
assertEquals(0, q.size()); |
658 |
|
} |
676 |
|
q.add(one); |
677 |
|
q.add(two); |
678 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
708 |
– |
executor.execute(new Runnable() { |
679 |
|
|
680 |
< |
public void run() { |
681 |
< |
try { |
682 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
683 |
< |
} catch (Exception e) { |
684 |
< |
threadUnexpectedException(); |
685 |
< |
} |
686 |
< |
} |
687 |
< |
}); |
688 |
< |
|
689 |
< |
executor.execute(new Runnable() { |
690 |
< |
|
721 |
< |
public void run() { |
722 |
< |
try { |
723 |
< |
Thread.sleep(SMALL_DELAY_MS); |
724 |
< |
threadAssertEquals(one, q.take()); |
725 |
< |
} catch (InterruptedException e) { |
726 |
< |
threadUnexpectedException(); |
727 |
< |
} |
728 |
< |
} |
729 |
< |
}); |
680 |
> |
executor.execute(new CheckedRunnable() { |
681 |
> |
void realRun() { |
682 |
> |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, |
683 |
> |
MILLISECONDS)); |
684 |
> |
}}); |
685 |
> |
|
686 |
> |
executor.execute(new CheckedRunnable() { |
687 |
> |
void realRun() throws InterruptedException { |
688 |
> |
Thread.sleep(SMALL_DELAY_MS); |
689 |
> |
threadAssertEquals(one, q.take()); |
690 |
> |
}}); |
691 |
|
|
692 |
|
joinPool(executor); |
693 |
|
} |
694 |
|
|
695 |
|
/** |
696 |
< |
* poll retrieves elements across Executor threads |
696 |
> |
* timed poll retrieves elements across Executor threads |
697 |
|
*/ |
698 |
|
public void testPollInExecutor() { |
699 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
700 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
740 |
– |
executor.execute(new Runnable() { |
701 |
|
|
702 |
< |
public void run() { |
702 |
> |
executor.execute(new CheckedRunnable() { |
703 |
> |
void realRun() throws InterruptedException { |
704 |
|
threadAssertNull(q.poll()); |
705 |
< |
try { |
706 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
707 |
< |
threadAssertTrue(q.isEmpty()); |
708 |
< |
} catch (InterruptedException e) { |
709 |
< |
threadUnexpectedException(); |
710 |
< |
} |
711 |
< |
} |
712 |
< |
}); |
713 |
< |
|
714 |
< |
executor.execute(new Runnable() { |
754 |
< |
|
755 |
< |
public void run() { |
756 |
< |
try { |
757 |
< |
Thread.sleep(SMALL_DELAY_MS); |
758 |
< |
q.put(one); |
759 |
< |
} catch (InterruptedException e) { |
760 |
< |
threadUnexpectedException(); |
761 |
< |
} |
762 |
< |
} |
763 |
< |
}); |
705 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, |
706 |
> |
MILLISECONDS)); |
707 |
> |
threadAssertTrue(q.isEmpty()); |
708 |
> |
}}); |
709 |
> |
|
710 |
> |
executor.execute(new CheckedRunnable() { |
711 |
> |
void realRun() throws InterruptedException { |
712 |
> |
Thread.sleep(SMALL_DELAY_MS); |
713 |
> |
q.put(one); |
714 |
> |
}}); |
715 |
|
|
716 |
|
joinPool(executor); |
717 |
|
} |
719 |
|
/** |
720 |
|
* A deserialized serialized queue has same elements in same order |
721 |
|
*/ |
722 |
< |
public void testSerialization() { |
722 |
> |
public void testSerialization() throws Exception { |
723 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
724 |
|
|
725 |
< |
try { |
726 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
727 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
728 |
< |
out.writeObject(q); |
729 |
< |
out.close(); |
730 |
< |
|
731 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
732 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
733 |
< |
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
734 |
< |
|
735 |
< |
assertEquals(q.size(), r.size()); |
736 |
< |
while (!q.isEmpty()) { |
737 |
< |
assertEquals(q.remove(), r.remove()); |
738 |
< |
} |
739 |
< |
} catch (Exception e) { |
789 |
< |
unexpectedException(); |
725 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
726 |
> |
ObjectOutputStream out |
727 |
> |
= new ObjectOutputStream(new BufferedOutputStream(bout)); |
728 |
> |
out.writeObject(q); |
729 |
> |
out.close(); |
730 |
> |
|
731 |
> |
ByteArrayInputStream bin |
732 |
> |
= new ByteArrayInputStream(bout.toByteArray()); |
733 |
> |
ObjectInputStream in |
734 |
> |
= new ObjectInputStream(new BufferedInputStream(bin)); |
735 |
> |
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
736 |
> |
|
737 |
> |
assertEquals(q.size(), r.size()); |
738 |
> |
while (!q.isEmpty()) { |
739 |
> |
assertEquals(q.remove(), r.remove()); |
740 |
|
} |
741 |
|
} |
742 |
|
|
743 |
|
/** |
744 |
< |
* drainTo(null) throws NPE |
744 |
> |
* drainTo(null) throws NullPointerException |
745 |
|
*/ |
746 |
|
public void testDrainToNull() { |
747 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
753 |
|
} |
754 |
|
|
755 |
|
/** |
756 |
< |
* drainTo(this) throws IAE |
756 |
> |
* drainTo(this) throws IllegalArgumentException |
757 |
|
*/ |
758 |
|
public void testDrainToSelf() { |
759 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
774 |
|
assertEquals(q.size(), 0); |
775 |
|
assertEquals(l.size(), SIZE); |
776 |
|
for (int i = 0; i < SIZE; ++i) { |
777 |
< |
assertEquals(l.get(i), new Integer(i)); |
777 |
> |
assertEquals(l.get(i), i); |
778 |
|
} |
779 |
|
q.add(zero); |
780 |
|
q.add(one); |
786 |
|
assertEquals(q.size(), 0); |
787 |
|
assertEquals(l.size(), 2); |
788 |
|
for (int i = 0; i < 2; ++i) { |
789 |
< |
assertEquals(l.get(i), new Integer(i)); |
789 |
> |
assertEquals(l.get(i), i); |
790 |
|
} |
791 |
|
} |
792 |
|
|
793 |
|
/** |
794 |
< |
* drainTo empties full queue, unblocking a waiting put. |
794 |
> |
* drainTo(c) empties full queue, unblocking a waiting put. |
795 |
|
*/ |
796 |
< |
public void testDrainToWithActivePut() { |
796 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
797 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
798 |
< |
Thread t = new Thread(new Runnable() { |
799 |
< |
|
800 |
< |
public void run() { |
801 |
< |
try { |
802 |
< |
q.put(new Integer(SIZE + 1)); |
803 |
< |
} catch (Exception ie) { |
804 |
< |
threadUnexpectedException(); |
805 |
< |
} |
806 |
< |
} |
857 |
< |
}); |
858 |
< |
try { |
859 |
< |
t.start(); |
860 |
< |
ArrayList l = new ArrayList(); |
861 |
< |
q.drainTo(l); |
862 |
< |
assertTrue(l.size() >= SIZE); |
863 |
< |
for (int i = 0; i < SIZE; ++i) { |
864 |
< |
assertEquals(l.get(i), new Integer(i)); |
865 |
< |
} |
866 |
< |
t.join(); |
867 |
< |
assertTrue(q.size() + l.size() >= SIZE); |
868 |
< |
} catch (Exception e) { |
869 |
< |
unexpectedException(); |
798 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
799 |
> |
void realRun() { |
800 |
> |
q.put(SIZE + 1); |
801 |
> |
}}); |
802 |
> |
ArrayList l = new ArrayList(); |
803 |
> |
q.drainTo(l); |
804 |
> |
assertTrue(l.size() >= SIZE); |
805 |
> |
for (int i = 0; i < SIZE; ++i) { |
806 |
> |
assertEquals(l.get(i), i); |
807 |
|
} |
808 |
+ |
t.join(); |
809 |
+ |
assertTrue(q.size() + l.size() >= SIZE); |
810 |
|
} |
811 |
|
|
812 |
|
/** |
813 |
< |
* drainTo(null, n) throws NPE |
813 |
> |
* drainTo(null, n) throws NullPointerException |
814 |
|
*/ |
815 |
|
public void testDrainToNullN() { |
816 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
817 |
|
try { |
818 |
< |
q.drainTo(null, 0); |
818 |
> |
q.drainTo(null, SIZE); |
819 |
|
shouldThrow(); |
820 |
|
} catch (NullPointerException success) { |
821 |
|
} |
822 |
|
} |
823 |
|
|
824 |
|
/** |
825 |
< |
* drainTo(this, n) throws IAE |
825 |
> |
* drainTo(this, n) throws IllegalArgumentException |
826 |
|
*/ |
827 |
|
public void testDrainToSelfN() { |
828 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
829 |
|
try { |
830 |
< |
q.drainTo(q, 0); |
830 |
> |
q.drainTo(q, SIZE); |
831 |
|
shouldThrow(); |
832 |
|
} catch (IllegalArgumentException success) { |
833 |
|
} |
840 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
841 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
842 |
|
for (int j = 0; j < SIZE; j++) { |
843 |
< |
assertTrue(q.offer(new Integer(j))); |
843 |
> |
assertTrue(q.offer(j)); |
844 |
|
} |
845 |
|
ArrayList l = new ArrayList(); |
846 |
|
q.drainTo(l, i); |
848 |
|
assertEquals(l.size(), k); |
849 |
|
assertEquals(q.size(), SIZE - k); |
850 |
|
for (int j = 0; j < k; ++j) { |
851 |
< |
assertEquals(l.get(j), new Integer(j)); |
851 |
> |
assertEquals(l.get(j), j); |
852 |
|
} |
853 |
< |
while (q.poll() != null); |
853 |
> |
while (q.poll() != null) |
854 |
> |
; |
855 |
|
} |
856 |
|
} |
857 |
|
|
858 |
|
/** |
859 |
< |
* poll and take should decrement the waiting consumer count |
859 |
> |
* timed poll() or take() increments the waiting consumer count; |
860 |
> |
* offer(e) decrements the waiting consumer count |
861 |
|
*/ |
862 |
< |
public void testWaitingConsumer() { |
863 |
< |
try { |
864 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
865 |
< |
final ConsumerObserver waiting = new ConsumerObserver(); |
925 |
< |
new Thread(new Runnable() { |
862 |
> |
public void testWaitingConsumer() throws InterruptedException { |
863 |
> |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
864 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
865 |
> |
assertFalse(q.hasWaitingConsumer()); |
866 |
|
|
867 |
< |
public void run() { |
868 |
< |
try { |
869 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
870 |
< |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
871 |
< |
threadAssertTrue(q.offer(new Object())); |
872 |
< |
} catch (Exception ex) { |
873 |
< |
threadUnexpectedException(); |
874 |
< |
} |
867 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
868 |
> |
void realRun() throws InterruptedException { |
869 |
> |
Thread.sleep(SMALL_DELAY_MS); |
870 |
> |
threadAssertTrue(q.hasWaitingConsumer()); |
871 |
> |
threadAssertEquals(q.getWaitingConsumerCount(), 1); |
872 |
> |
threadAssertTrue(q.offer(new Object())); |
873 |
> |
threadAssertFalse(q.hasWaitingConsumer()); |
874 |
> |
threadAssertEquals(q.getWaitingConsumerCount(), 0); |
875 |
> |
}}); |
876 |
|
|
877 |
< |
} |
878 |
< |
}).start(); |
879 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null); |
880 |
< |
assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers()); |
940 |
< |
} catch (Exception ex) { |
941 |
< |
this.unexpectedException(); |
942 |
< |
} |
877 |
> |
assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); |
878 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
879 |
> |
assertFalse(q.hasWaitingConsumer()); |
880 |
> |
t.join(); |
881 |
|
} |
882 |
|
|
883 |
|
/** |
884 |
< |
* Inserts null into transfer throws NPE |
884 |
> |
* transfer(null) throws NullPointerException |
885 |
|
*/ |
886 |
< |
public void testTransfer1() { |
886 |
> |
public void testTransfer1() throws InterruptedException { |
887 |
|
try { |
888 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
889 |
|
q.transfer(null); |
890 |
|
shouldThrow(); |
891 |
< |
} catch (NullPointerException ex) { |
954 |
< |
} catch (Exception ex) { |
955 |
< |
this.unexpectedException(); |
956 |
< |
} |
891 |
> |
} catch (NullPointerException success) {} |
892 |
|
} |
893 |
|
|
894 |
|
/** |
895 |
< |
* transfer attempts to insert into the queue then wait until that |
896 |
< |
* object is removed via take or poll. |
895 |
> |
* transfer waits until a poll occurs. The transfered element |
896 |
> |
* is returned by this associated poll. |
897 |
|
*/ |
898 |
< |
public void testTransfer2() { |
899 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
900 |
< |
new Thread(new Runnable() { |
898 |
> |
public void testTransfer2() throws InterruptedException { |
899 |
> |
final LinkedTransferQueue<Integer> q |
900 |
> |
= new LinkedTransferQueue<Integer>(); |
901 |
|
|
902 |
< |
public void run() { |
903 |
< |
try { |
904 |
< |
q.transfer(new Integer(SIZE)); |
905 |
< |
threadAssertTrue(q.isEmpty()); |
906 |
< |
} catch (Exception ex) { |
972 |
< |
threadUnexpectedException(); |
973 |
< |
} |
974 |
< |
} |
975 |
< |
}).start(); |
902 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
903 |
> |
void realRun() throws InterruptedException { |
904 |
> |
q.transfer(SIZE); |
905 |
> |
threadAssertTrue(q.isEmpty()); |
906 |
> |
}}); |
907 |
|
|
908 |
< |
try { |
909 |
< |
Thread.sleep(SHORT_DELAY_MS); |
910 |
< |
assertEquals(1, q.size()); |
911 |
< |
q.poll(); |
912 |
< |
assertTrue(q.isEmpty()); |
982 |
< |
} catch (Exception ex) { |
983 |
< |
this.unexpectedException(); |
984 |
< |
} |
908 |
> |
Thread.sleep(SHORT_DELAY_MS); |
909 |
> |
assertEquals(1, q.size()); |
910 |
> |
assertEquals(SIZE, (int) q.poll()); |
911 |
> |
assertTrue(q.isEmpty()); |
912 |
> |
t.join(); |
913 |
|
} |
914 |
|
|
915 |
|
/** |
916 |
< |
* transfer will attempt to transfer in fifo order and continue |
917 |
< |
* waiting if the element being transfered is not polled or taken |
918 |
< |
*/ |
919 |
< |
public void testTransfer3() { |
920 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
921 |
< |
new Thread(new Runnable() { |
922 |
< |
public void run() { |
923 |
< |
try { |
924 |
< |
Integer i; |
925 |
< |
q.transfer((i = new Integer(SIZE + 1))); |
926 |
< |
threadAssertTrue(!q.contains(i)); |
927 |
< |
threadAssertEquals(1, q.size()); |
928 |
< |
} catch (Exception ex) { |
929 |
< |
threadUnexpectedException(); |
930 |
< |
} |
931 |
< |
} |
932 |
< |
}).start(); |
933 |
< |
Thread interruptedThread = |
934 |
< |
new Thread(new Runnable() { |
935 |
< |
public void run() { |
936 |
< |
try { |
937 |
< |
q.transfer(new Integer(SIZE)); |
938 |
< |
threadShouldThrow(); |
939 |
< |
} catch (InterruptedException ex) { |
940 |
< |
} |
941 |
< |
} |
942 |
< |
}); |
943 |
< |
interruptedThread.start(); |
944 |
< |
try { |
945 |
< |
Thread.sleep(LONG_DELAY_MS); |
946 |
< |
assertEquals(2, q.size()); |
947 |
< |
q.poll(); |
1020 |
< |
Thread.sleep(LONG_DELAY_MS); |
1021 |
< |
interruptedThread.interrupt(); |
1022 |
< |
assertEquals(1, q.size()); |
1023 |
< |
} catch (Exception ex) { |
1024 |
< |
this.unexpectedException(); |
1025 |
< |
} |
916 |
> |
* transfer waits until a poll occurs, and then transfers in fifo order |
917 |
> |
*/ |
918 |
> |
public void testTransfer3() throws InterruptedException { |
919 |
> |
final LinkedTransferQueue<Integer> q |
920 |
> |
= new LinkedTransferQueue<Integer>(); |
921 |
> |
|
922 |
> |
Thread first = newStartedThread(new CheckedRunnable() { |
923 |
> |
void realRun() throws InterruptedException { |
924 |
> |
Integer i = SIZE + 1; |
925 |
> |
q.transfer(i); |
926 |
> |
threadAssertTrue(!q.contains(i)); |
927 |
> |
threadAssertEquals(1, q.size()); |
928 |
> |
}}); |
929 |
> |
|
930 |
> |
Thread interruptedThread = newStartedThread( |
931 |
> |
new CheckedInterruptedRunnable() { |
932 |
> |
void realRun() throws InterruptedException { |
933 |
> |
while (q.size() == 0) |
934 |
> |
Thread.yield(); |
935 |
> |
q.transfer(SIZE); |
936 |
> |
}}); |
937 |
> |
|
938 |
> |
while (q.size() < 2) |
939 |
> |
Thread.yield(); |
940 |
> |
assertEquals(2, q.size()); |
941 |
> |
assertEquals(SIZE + 1, (int) q.poll()); |
942 |
> |
first.join(); |
943 |
> |
assertEquals(1, q.size()); |
944 |
> |
interruptedThread.interrupt(); |
945 |
> |
interruptedThread.join(); |
946 |
> |
assertEquals(0, q.size()); |
947 |
> |
assertTrue(q.isEmpty()); |
948 |
|
} |
949 |
|
|
950 |
|
/** |
951 |
< |
* transfer will wait as long as a poll or take occurs if one does occur |
952 |
< |
* the waiting is finished and the thread that tries to poll/take |
1031 |
< |
* wins in retrieving the element |
951 |
> |
* transfer waits until a poll occurs, at which point the polling |
952 |
> |
* thread returns the element |
953 |
|
*/ |
954 |
< |
public void testTransfer4() { |
954 |
> |
public void testTransfer4() throws InterruptedException { |
955 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1035 |
– |
new Thread(new Runnable() { |
956 |
|
|
957 |
< |
public void run() { |
958 |
< |
try { |
959 |
< |
q.transfer(new Integer(four)); |
960 |
< |
threadAssertFalse(q.contains(new Integer(four))); |
961 |
< |
threadAssertEquals(new Integer(three), q.poll()); |
962 |
< |
} catch (Exception ex) { |
963 |
< |
threadUnexpectedException(); |
964 |
< |
} |
965 |
< |
} |
966 |
< |
}).start(); |
967 |
< |
try { |
1048 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1049 |
< |
assertTrue(q.offer(three)); |
1050 |
< |
assertEquals(new Integer(four), q.poll()); |
1051 |
< |
} catch (Exception ex) { |
1052 |
< |
this.unexpectedException(); |
1053 |
< |
} |
957 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
958 |
> |
void realRun() throws InterruptedException { |
959 |
> |
q.transfer(four); |
960 |
> |
threadAssertFalse(q.contains(four)); |
961 |
> |
threadAssertEquals(three, q.poll()); |
962 |
> |
}}); |
963 |
> |
|
964 |
> |
Thread.sleep(SHORT_DELAY_MS); |
965 |
> |
assertTrue(q.offer(three)); |
966 |
> |
assertEquals(four, q.poll()); |
967 |
> |
t.join(); |
968 |
|
} |
969 |
|
|
970 |
|
/** |
971 |
< |
* Insert null into tryTransfer throws NPE |
971 |
> |
* transfer waits until a take occurs. The transfered element |
972 |
> |
* is returned by this associated take. |
973 |
> |
*/ |
974 |
> |
public void testTransfer5() throws InterruptedException { |
975 |
> |
final LinkedTransferQueue<Integer> q |
976 |
> |
= new LinkedTransferQueue<Integer>(); |
977 |
> |
|
978 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
979 |
> |
void realRun() throws InterruptedException { |
980 |
> |
q.transfer(SIZE); |
981 |
> |
checkEmpty(q); |
982 |
> |
}}); |
983 |
> |
|
984 |
> |
Thread.sleep(SHORT_DELAY_MS); |
985 |
> |
assertEquals(SIZE, (int) q.take()); |
986 |
> |
checkEmpty(q); |
987 |
> |
t.join(); |
988 |
> |
} |
989 |
> |
|
990 |
> |
/** |
991 |
> |
* tryTransfer(null) throws NullPointerException |
992 |
|
*/ |
993 |
|
public void testTryTransfer1() { |
994 |
|
try { |
995 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
996 |
|
q.tryTransfer(null); |
997 |
< |
this.shouldThrow(); |
998 |
< |
} catch (NullPointerException ex) { |
1065 |
< |
} catch (Exception ex) { |
1066 |
< |
this.unexpectedException(); |
1067 |
< |
} |
997 |
> |
shouldThrow(); |
998 |
> |
} catch (NullPointerException success) {} |
999 |
|
} |
1000 |
|
|
1001 |
|
/** |
1002 |
< |
* tryTransfer returns false and does not enqueue if there are no consumers |
1003 |
< |
* waiting to poll or take. |
1002 |
> |
* tryTransfer returns false and does not enqueue if there are no |
1003 |
> |
* consumers waiting to poll or take. |
1004 |
|
*/ |
1005 |
< |
public void testTryTransfer2() { |
1006 |
< |
try { |
1007 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1008 |
< |
assertFalse(q.tryTransfer(new Object())); |
1009 |
< |
assertEquals(0, q.size()); |
1079 |
< |
} catch (Exception ex) { |
1080 |
< |
this.unexpectedException(); |
1081 |
< |
} |
1005 |
> |
public void testTryTransfer2() throws InterruptedException { |
1006 |
> |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1007 |
> |
assertFalse(q.tryTransfer(new Object())); |
1008 |
> |
assertFalse(q.hasWaitingConsumer()); |
1009 |
> |
checkEmpty(q); |
1010 |
|
} |
1011 |
|
|
1012 |
|
/** |
1013 |
< |
* if there is a consumer waiting poll or take tryTransfer returns |
1014 |
< |
* true while enqueueing object |
1013 |
> |
* If there is a consumer waiting in timed poll, tryTransfer |
1014 |
> |
* returns true while successfully transfering object. |
1015 |
|
*/ |
1016 |
< |
public void testTryTransfer3() { |
1017 |
< |
try { |
1018 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1091 |
< |
new Thread(new Runnable() { |
1016 |
> |
public void testTryTransfer3() throws InterruptedException { |
1017 |
> |
final Object hotPotato = new Object(); |
1018 |
> |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1019 |
|
|
1020 |
< |
public void run() { |
1021 |
< |
try { |
1022 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
1023 |
< |
threadAssertTrue(q.tryTransfer(new Object())); |
1024 |
< |
} catch (Exception ex) { |
1025 |
< |
threadUnexpectedException(); |
1026 |
< |
} |
1020 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1021 |
> |
void realRun() { |
1022 |
> |
while (! q.hasWaitingConsumer()) |
1023 |
> |
Thread.yield(); |
1024 |
> |
threadAssertTrue(q.hasWaitingConsumer()); |
1025 |
> |
threadAssertTrue(q.isEmpty()); |
1026 |
> |
threadAssertTrue(q.size() == 0); |
1027 |
> |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1028 |
> |
}}); |
1029 |
> |
|
1030 |
> |
assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato); |
1031 |
> |
checkEmpty(q); |
1032 |
> |
t.join(); |
1033 |
> |
} |
1034 |
|
|
1035 |
< |
} |
1036 |
< |
}).start(); |
1037 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null); |
1038 |
< |
assertTrue(q.isEmpty()); |
1039 |
< |
} catch (Exception ex) { |
1040 |
< |
this.unexpectedException(); |
1041 |
< |
} |
1035 |
> |
/** |
1036 |
> |
* If there is a consumer waiting in take, tryTransfer returns |
1037 |
> |
* true while successfully transfering object. |
1038 |
> |
*/ |
1039 |
> |
public void testTryTransfer4() throws InterruptedException { |
1040 |
> |
final Object hotPotato = new Object(); |
1041 |
> |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1042 |
> |
|
1043 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1044 |
> |
void realRun() { |
1045 |
> |
while (! q.hasWaitingConsumer()) |
1046 |
> |
Thread.yield(); |
1047 |
> |
threadAssertTrue(q.hasWaitingConsumer()); |
1048 |
> |
threadAssertTrue(q.isEmpty()); |
1049 |
> |
threadAssertTrue(q.size() == 0); |
1050 |
> |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1051 |
> |
}}); |
1052 |
> |
|
1053 |
> |
assertTrue(q.take() == hotPotato); |
1054 |
> |
checkEmpty(q); |
1055 |
> |
t.join(); |
1056 |
|
} |
1057 |
|
|
1058 |
|
/** |
1059 |
< |
* tryTransfer waits the amount given if interrupted, show an |
1060 |
< |
* interrupted exception |
1059 |
> |
* tryTransfer waits the amount given if interrupted, and |
1060 |
> |
* throws interrupted exception |
1061 |
|
*/ |
1062 |
< |
public void testTryTransfer4() { |
1062 |
> |
public void testTryTransfer5() throws InterruptedException { |
1063 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1116 |
– |
Thread toInterrupt = new Thread(new Runnable() { |
1064 |
|
|
1065 |
< |
public void run() { |
1066 |
< |
try { |
1067 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1068 |
< |
threadShouldThrow(); |
1069 |
< |
} catch (InterruptedException ex) { |
1070 |
< |
} |
1071 |
< |
} |
1072 |
< |
}); |
1126 |
< |
try { |
1127 |
< |
toInterrupt.start(); |
1128 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1129 |
< |
toInterrupt.interrupt(); |
1130 |
< |
} catch (Exception ex) { |
1131 |
< |
this.unexpectedException(); |
1132 |
< |
} |
1065 |
> |
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
1066 |
> |
void realRun() throws InterruptedException { |
1067 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1068 |
> |
}}); |
1069 |
> |
|
1070 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1071 |
> |
toInterrupt.interrupt(); |
1072 |
> |
toInterrupt.join(); |
1073 |
|
} |
1074 |
|
|
1075 |
|
/** |
1076 |
|
* tryTransfer gives up after the timeout and return false |
1077 |
|
*/ |
1078 |
< |
public void testTryTransfer5() { |
1078 |
> |
public void testTryTransfer6() throws InterruptedException { |
1079 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1140 |
– |
try { |
1141 |
– |
new Thread(new Runnable() { |
1080 |
|
|
1081 |
< |
public void run() { |
1082 |
< |
try { |
1083 |
< |
threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1084 |
< |
} catch (InterruptedException ex) { |
1085 |
< |
threadUnexpectedException(); |
1086 |
< |
} |
1087 |
< |
} |
1088 |
< |
}).start(); |
1089 |
< |
Thread.sleep(LONG_DELAY_MS); |
1090 |
< |
assertTrue(q.isEmpty()); |
1153 |
< |
} catch (Exception ex) { |
1154 |
< |
this.unexpectedException(); |
1155 |
< |
} |
1081 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1082 |
> |
void realRun() throws InterruptedException { |
1083 |
> |
threadAssertFalse |
1084 |
> |
(q.tryTransfer(new Object(), |
1085 |
> |
SHORT_DELAY_MS, MILLISECONDS)); |
1086 |
> |
}}); |
1087 |
> |
|
1088 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1089 |
> |
checkEmpty(q); |
1090 |
> |
t.join(); |
1091 |
|
} |
1092 |
|
|
1093 |
|
/** |
1094 |
|
* tryTransfer waits for any elements previously in to be removed |
1095 |
|
* before transfering to a poll or take |
1096 |
|
*/ |
1097 |
< |
public void testTryTransfer6() { |
1097 |
> |
public void testTryTransfer7() throws InterruptedException { |
1098 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1099 |
< |
q.offer(new Integer(four)); |
1165 |
< |
new Thread(new Runnable() { |
1099 |
> |
assertTrue(q.offer(four)); |
1100 |
|
|
1101 |
< |
public void run() { |
1102 |
< |
try { |
1103 |
< |
threadAssertTrue(q.tryTransfer(new Integer(five), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
1104 |
< |
threadAssertTrue(q.isEmpty()); |
1105 |
< |
} catch (InterruptedException ex) { |
1106 |
< |
threadUnexpectedException(); |
1107 |
< |
} |
1108 |
< |
} |
1109 |
< |
}).start(); |
1110 |
< |
try { |
1111 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1112 |
< |
assertEquals(2, q.size()); |
1113 |
< |
assertEquals(new Integer(four), q.poll()); |
1180 |
< |
assertEquals(new Integer(five), q.poll()); |
1181 |
< |
assertTrue(q.isEmpty()); |
1182 |
< |
} catch (Exception ex) { |
1183 |
< |
this.unexpectedException(); |
1184 |
< |
} |
1101 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1102 |
> |
void realRun() throws InterruptedException { |
1103 |
> |
threadAssertTrue(q.tryTransfer(five, |
1104 |
> |
MEDIUM_DELAY_MS, MILLISECONDS)); |
1105 |
> |
threadAssertTrue(q.isEmpty()); |
1106 |
> |
}}); |
1107 |
> |
|
1108 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1109 |
> |
assertEquals(2, q.size()); |
1110 |
> |
assertEquals(four, q.poll()); |
1111 |
> |
assertEquals(five, q.poll()); |
1112 |
> |
checkEmpty(q); |
1113 |
> |
t.join(); |
1114 |
|
} |
1115 |
|
|
1116 |
|
/** |
1117 |
< |
* tryTransfer attempts to enqueue into the q and fails returning false not |
1118 |
< |
* enqueueing and the successing poll is null |
1117 |
> |
* tryTransfer attempts to enqueue into the q and fails returning |
1118 |
> |
* false not enqueueing and the successive poll is null |
1119 |
|
*/ |
1120 |
< |
public void testTryTransfer7() { |
1120 |
> |
public void testTryTransfer8() throws InterruptedException { |
1121 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1122 |
< |
q.offer(new Integer(four)); |
1123 |
< |
new Thread(new Runnable() { |
1124 |
< |
|
1125 |
< |
public void run() { |
1126 |
< |
try { |
1127 |
< |
threadAssertFalse(q.tryTransfer(new Integer(five), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1128 |
< |
threadAssertTrue(q.isEmpty()); |
1200 |
< |
} catch (InterruptedException ex) { |
1201 |
< |
threadUnexpectedException(); |
1202 |
< |
} |
1203 |
< |
} |
1204 |
< |
}).start(); |
1205 |
< |
try { |
1206 |
< |
assertEquals(1, q.size()); |
1207 |
< |
assertEquals(new Integer(four), q.poll()); |
1208 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1209 |
< |
assertNull(q.poll()); |
1210 |
< |
} catch (Exception ex) { |
1211 |
< |
this.unexpectedException(); |
1212 |
< |
} |
1122 |
> |
assertTrue(q.offer(four)); |
1123 |
> |
assertEquals(1, q.size()); |
1124 |
> |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1125 |
> |
assertEquals(1, q.size()); |
1126 |
> |
assertEquals(four, q.poll()); |
1127 |
> |
assertNull(q.poll()); |
1128 |
> |
checkEmpty(q); |
1129 |
|
} |
1130 |
|
|
1131 |
< |
private LinkedTransferQueue populatedQueue( |
1132 |
< |
int n) { |
1217 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
1131 |
> |
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1132 |
> |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
1133 |
|
assertTrue(q.isEmpty()); |
1134 |
< |
int remainingCapacity = q.remainingCapacity(); |
1135 |
< |
for (int i = 0; i < |
1221 |
< |
n; i++) { |
1134 |
> |
for (int i = 0; i < n; i++) { |
1135 |
> |
assertEquals(i, q.size()); |
1136 |
|
assertTrue(q.offer(i)); |
1137 |
+ |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
1138 |
|
} |
1224 |
– |
|
1139 |
|
assertFalse(q.isEmpty()); |
1226 |
– |
assertEquals(remainingCapacity, q.remainingCapacity()); |
1227 |
– |
assertEquals(n, q.size()); |
1140 |
|
return q; |
1141 |
|
} |
1230 |
– |
|
1231 |
– |
private static class ConsumerObserver { |
1232 |
– |
|
1233 |
– |
private int waitingConsumers; |
1234 |
– |
|
1235 |
– |
private ConsumerObserver() { |
1236 |
– |
} |
1237 |
– |
|
1238 |
– |
private void setWaitingConsumer(int i) { |
1239 |
– |
this.waitingConsumers = i; |
1240 |
– |
} |
1241 |
– |
|
1242 |
– |
private int getWaitingConsumers() { |
1243 |
– |
return waitingConsumers; |
1244 |
– |
} |
1245 |
– |
} |
1142 |
|
} |