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