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