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 |
< |
/* |
44 |
< |
*Constructor builds new queue with size being zero and empty being true |
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 |
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 |
277 |
> |
* take removes existing elements until empty, then blocks interruptibly |
278 |
|
*/ |
279 |
< |
public void testTakeFromEmpty() { |
280 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
281 |
< |
Thread t = new Thread(new Runnable() { |
282 |
< |
|
283 |
< |
public void run() { |
284 |
< |
try { |
267 |
< |
q.take(); |
268 |
< |
threadShouldThrow(); |
269 |
< |
} catch (InterruptedException success) { |
279 |
> |
public void testBlockingTake() throws InterruptedException { |
280 |
> |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
281 |
> |
Thread t = new Thread(new CheckedRunnable() { |
282 |
> |
public void realRun() throws InterruptedException { |
283 |
> |
for (int i = 0; i < SIZE; ++i) { |
284 |
> |
assertEquals(i, (int) q.take()); |
285 |
|
} |
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 |
– |
} |
282 |
– |
/* |
283 |
– |
* Take removes existing elements until empty, then blocks interruptibly |
284 |
– |
*/ |
285 |
– |
|
286 |
– |
public void testBlockingTake() { |
287 |
– |
Thread t = new Thread(new Runnable() { |
288 |
– |
|
289 |
– |
public void run() { |
286 |
|
try { |
291 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
292 |
– |
for (int i = 0; i < SIZE; ++i) { |
293 |
– |
assertEquals(i, ((Integer) q.take()).intValue()); |
294 |
– |
} |
287 |
|
q.take(); |
288 |
< |
threadShouldThrow(); |
289 |
< |
} catch (InterruptedException success) { |
290 |
< |
} |
291 |
< |
} |
300 |
< |
}); |
288 |
> |
shouldThrow(); |
289 |
> |
} catch (InterruptedException success) {} |
290 |
> |
}}); |
291 |
> |
|
292 |
|
t.start(); |
293 |
< |
try { |
294 |
< |
Thread.sleep(SHORT_DELAY_MS); |
295 |
< |
t.interrupt(); |
296 |
< |
t.join(); |
306 |
< |
} catch (InterruptedException ie) { |
307 |
< |
unexpectedException(); |
308 |
< |
} |
293 |
> |
Thread.sleep(SHORT_DELAY_MS); |
294 |
> |
t.interrupt(); |
295 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
296 |
> |
checkEmpty(q); |
297 |
|
} |
298 |
|
|
299 |
|
/** |
300 |
|
* poll succeeds unless empty |
301 |
|
*/ |
302 |
< |
public void testPoll() { |
303 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
302 |
> |
public void testPoll() throws InterruptedException { |
303 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
304 |
|
for (int i = 0; i < SIZE; ++i) { |
305 |
< |
assertEquals(i, ((Integer) q.poll()).intValue()); |
305 |
> |
assertEquals(i, (int) q.poll()); |
306 |
|
} |
307 |
|
assertNull(q.poll()); |
308 |
+ |
checkEmpty(q); |
309 |
|
} |
310 |
|
|
311 |
|
/** |
312 |
|
* timed pool with zero timeout succeeds when non-empty, else times out |
313 |
|
*/ |
314 |
< |
public void testTimedPoll0() { |
315 |
< |
try { |
316 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
317 |
< |
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(); |
314 |
> |
public void testTimedPoll0() throws InterruptedException { |
315 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
316 |
> |
for (int i = 0; i < SIZE; ++i) { |
317 |
> |
assertEquals(i, (int) q.poll(0, MILLISECONDS)); |
318 |
|
} |
319 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
320 |
+ |
checkEmpty(q); |
321 |
|
} |
322 |
|
|
323 |
|
/** |
324 |
|
* timed pool with nonzero timeout succeeds when non-empty, else times out |
325 |
|
*/ |
326 |
< |
public void testTimedPoll() { |
327 |
< |
try { |
328 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
329 |
< |
for (int i = 0; i < SIZE; ++i) { |
330 |
< |
assertEquals(i, ((Integer) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
331 |
< |
} |
346 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
347 |
< |
} catch (InterruptedException e) { |
348 |
< |
unexpectedException(); |
326 |
> |
public void testTimedPoll() throws InterruptedException { |
327 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
328 |
> |
for (int i = 0; i < SIZE; ++i) { |
329 |
> |
long t0 = System.nanoTime(); |
330 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
331 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
332 |
|
} |
333 |
+ |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
334 |
+ |
checkEmpty(q); |
335 |
|
} |
336 |
|
|
337 |
|
/** |
338 |
|
* Interrupted timed poll throws InterruptedException instead of |
339 |
|
* returning timeout status |
340 |
|
*/ |
341 |
< |
public void testInterruptedTimedPoll() { |
342 |
< |
Thread t = new Thread(new Runnable() { |
343 |
< |
|
344 |
< |
public void run() { |
345 |
< |
try { |
346 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
347 |
< |
for (int i = 0; i < SIZE; ++i) { |
348 |
< |
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) { |
341 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
342 |
> |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
343 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
344 |
> |
public void realRun() throws InterruptedException { |
345 |
> |
for (int i = 0; i < SIZE; ++i) { |
346 |
> |
long t0 = System.nanoTime(); |
347 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
348 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
349 |
|
} |
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() { |
350 |
|
try { |
351 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
352 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
353 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
354 |
< |
threadShouldThrow(); |
355 |
< |
} catch (InterruptedException success) { |
356 |
< |
} |
357 |
< |
} |
358 |
< |
}); |
359 |
< |
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 |
< |
} |
351 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
352 |
> |
shouldThrow(); |
353 |
> |
} catch (InterruptedException success) {} |
354 |
> |
}}); |
355 |
> |
|
356 |
> |
Thread.sleep(SMALL_DELAY_MS); |
357 |
> |
t.interrupt(); |
358 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
359 |
> |
checkEmpty(q); |
360 |
|
} |
361 |
|
|
362 |
|
/** |
363 |
|
* peek returns next element, or null if empty |
364 |
|
*/ |
365 |
< |
public void testPeek() { |
366 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
365 |
> |
public void testPeek() throws InterruptedException { |
366 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
367 |
|
for (int i = 0; i < SIZE; ++i) { |
368 |
< |
assertEquals(i, ((Integer) q.peek()).intValue()); |
369 |
< |
q.poll(); |
368 |
> |
assertEquals(i, (int) q.peek()); |
369 |
> |
assertEquals(i, (int) q.poll()); |
370 |
|
assertTrue(q.peek() == null || |
371 |
< |
i != ((Integer) q.peek()).intValue()); |
371 |
> |
i != (int) q.peek()); |
372 |
|
} |
373 |
|
assertNull(q.peek()); |
374 |
+ |
checkEmpty(q); |
375 |
|
} |
376 |
|
|
377 |
|
/** |
378 |
< |
* element returns next element, or throws NSEE if empty |
378 |
> |
* element returns next element, or throws NoSuchElementException if empty |
379 |
|
*/ |
380 |
< |
public void testElement() { |
381 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
380 |
> |
public void testElement() throws InterruptedException { |
381 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
382 |
|
for (int i = 0; i < SIZE; ++i) { |
383 |
< |
assertEquals(i, ((Integer) q.element()).intValue()); |
384 |
< |
q.poll(); |
383 |
> |
assertEquals(i, (int) q.element()); |
384 |
> |
assertEquals(i, (int) q.poll()); |
385 |
|
} |
386 |
|
try { |
387 |
|
q.element(); |
388 |
|
shouldThrow(); |
389 |
< |
} catch (NoSuchElementException success) { |
390 |
< |
} |
389 |
> |
} catch (NoSuchElementException success) {} |
390 |
> |
checkEmpty(q); |
391 |
|
} |
392 |
|
|
393 |
|
/** |
394 |
< |
* remove removes next element, or throws NSEE if empty |
394 |
> |
* remove removes next element, or throws NoSuchElementException if empty |
395 |
|
*/ |
396 |
< |
public void testRemove() { |
397 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
396 |
> |
public void testRemove() throws InterruptedException { |
397 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
398 |
|
for (int i = 0; i < SIZE; ++i) { |
399 |
< |
assertEquals(i, ((Integer) q.remove()).intValue()); |
399 |
> |
assertEquals(i, (int) q.remove()); |
400 |
|
} |
401 |
|
try { |
402 |
|
q.remove(); |
403 |
|
shouldThrow(); |
404 |
< |
} catch (NoSuchElementException success) { |
405 |
< |
} |
404 |
> |
} catch (NoSuchElementException success) {} |
405 |
> |
checkEmpty(q); |
406 |
|
} |
407 |
|
|
408 |
|
/** |
409 |
|
* remove(x) removes x and returns true if present |
410 |
|
*/ |
411 |
< |
public void testRemoveElement() { |
411 |
> |
public void testRemoveElement() throws InterruptedException { |
412 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
413 |
|
for (int i = 1; i < SIZE; i += 2) { |
414 |
< |
assertTrue(q.remove(new Integer(i))); |
414 |
> |
assertTrue(q.remove(i)); |
415 |
|
} |
416 |
|
for (int i = 0; i < SIZE; i += 2) { |
417 |
< |
assertTrue(q.remove(new Integer(i))); |
418 |
< |
assertFalse(q.remove(new Integer(i + 1))); |
417 |
> |
assertTrue(q.remove(i)); |
418 |
> |
assertFalse(q.remove(i + 1)); |
419 |
|
} |
420 |
< |
assertTrue(q.isEmpty()); |
420 |
> |
checkEmpty(q); |
421 |
|
} |
422 |
|
|
423 |
|
/** |
424 |
|
* An add following remove(x) succeeds |
425 |
|
*/ |
426 |
< |
public void testRemoveElementAndAdd() { |
427 |
< |
try { |
428 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
429 |
< |
assertTrue(q.add(new Integer(1))); |
430 |
< |
assertTrue(q.add(new Integer(2))); |
431 |
< |
assertTrue(q.remove(new Integer(1))); |
432 |
< |
assertTrue(q.remove(new Integer(2))); |
433 |
< |
assertTrue(q.add(new Integer(3))); |
480 |
< |
assertTrue(q.take() != null); |
481 |
< |
} catch (Exception e) { |
482 |
< |
unexpectedException(); |
483 |
< |
} |
426 |
> |
public void testRemoveElementAndAdd() throws InterruptedException { |
427 |
> |
LinkedTransferQueue q = new LinkedTransferQueue(); |
428 |
> |
assertTrue(q.add(one)); |
429 |
> |
assertTrue(q.add(two)); |
430 |
> |
assertTrue(q.remove(one)); |
431 |
> |
assertTrue(q.remove(two)); |
432 |
> |
assertTrue(q.add(three)); |
433 |
> |
assertSame(q.take(), three); |
434 |
|
} |
435 |
|
|
436 |
|
/** |
437 |
|
* contains(x) reports true when elements added but not yet removed |
438 |
|
*/ |
439 |
|
public void testContains() { |
440 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
440 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
441 |
|
for (int i = 0; i < SIZE; ++i) { |
442 |
< |
assertTrue(q.contains(new Integer(i))); |
443 |
< |
q.poll(); |
444 |
< |
assertFalse(q.contains(new Integer(i))); |
442 |
> |
assertTrue(q.contains(i)); |
443 |
> |
assertEquals(i, (int) q.poll()); |
444 |
> |
assertFalse(q.contains(i)); |
445 |
|
} |
446 |
|
} |
447 |
|
|
448 |
|
/** |
449 |
|
* clear removes all elements |
450 |
|
*/ |
451 |
< |
public void testClear() { |
451 |
> |
public void testClear() throws InterruptedException { |
452 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
503 |
– |
int remainingCapacity = q.remainingCapacity(); |
453 |
|
q.clear(); |
454 |
< |
assertTrue(q.isEmpty()); |
455 |
< |
assertEquals(0, q.size()); |
507 |
< |
assertEquals(remainingCapacity, q.remainingCapacity()); |
454 |
> |
checkEmpty(q); |
455 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
456 |
|
q.add(one); |
457 |
|
assertFalse(q.isEmpty()); |
458 |
+ |
assertEquals(1, q.size()); |
459 |
|
assertTrue(q.contains(one)); |
460 |
|
q.clear(); |
461 |
< |
assertTrue(q.isEmpty()); |
461 |
> |
checkEmpty(q); |
462 |
|
} |
463 |
|
|
464 |
|
/** |
465 |
|
* containsAll(c) is true when c contains a subset of elements |
466 |
|
*/ |
467 |
|
public void testContainsAll() { |
468 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
469 |
< |
LinkedTransferQueue p = new LinkedTransferQueue(); |
468 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
469 |
> |
LinkedTransferQueue<Integer> p = new LinkedTransferQueue<Integer>(); |
470 |
|
for (int i = 0; i < SIZE; ++i) { |
471 |
|
assertTrue(q.containsAll(p)); |
472 |
|
assertFalse(p.containsAll(q)); |
473 |
< |
p.add(new Integer(i)); |
473 |
> |
p.add(i); |
474 |
|
} |
475 |
|
assertTrue(p.containsAll(q)); |
476 |
|
} |
477 |
|
|
478 |
|
/** |
479 |
< |
* retainAll(c) retains only those elements of c and reports true if changed |
479 |
> |
* retainAll(c) retains only those elements of c and reports true |
480 |
> |
* if changed |
481 |
|
*/ |
482 |
|
public void testRetainAll() { |
483 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
496 |
|
} |
497 |
|
|
498 |
|
/** |
499 |
< |
* removeAll(c) removes only those elements of c and reports true if changed |
499 |
> |
* removeAll(c) removes only those elements of c and reports true |
500 |
> |
* if changed |
501 |
|
*/ |
502 |
|
public void testRemoveAll() { |
503 |
|
for (int i = 1; i < SIZE; ++i) { |
506 |
|
assertTrue(q.removeAll(p)); |
507 |
|
assertEquals(SIZE - i, q.size()); |
508 |
|
for (int j = 0; j < i; ++j) { |
509 |
< |
Integer I = (Integer) (p.remove()); |
559 |
< |
assertFalse(q.contains(I)); |
509 |
> |
assertFalse(q.contains(p.remove())); |
510 |
|
} |
511 |
|
} |
512 |
|
} |
513 |
|
|
514 |
|
/** |
515 |
< |
* toArray contains all elements |
515 |
> |
* toArray() contains all elements |
516 |
|
*/ |
517 |
< |
public void testToArray() { |
517 |
> |
public void testToArray() throws InterruptedException { |
518 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
519 |
|
Object[] o = q.toArray(); |
520 |
< |
try { |
521 |
< |
for (int i = 0; i < o.length; i++) { |
572 |
< |
assertEquals(o[i], q.take()); |
573 |
< |
} |
574 |
< |
} catch (InterruptedException e) { |
575 |
< |
unexpectedException(); |
520 |
> |
for (int i = 0; i < o.length; i++) { |
521 |
> |
assertEquals(o[i], q.take()); |
522 |
|
} |
523 |
|
} |
524 |
|
|
525 |
|
/** |
526 |
|
* toArray(a) contains all elements |
527 |
|
*/ |
528 |
< |
public void testToArray2() { |
529 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
528 |
> |
public void testToArray2() throws InterruptedException { |
529 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
530 |
|
Integer[] ints = new Integer[SIZE]; |
531 |
< |
ints = (Integer[]) q.toArray(ints); |
532 |
< |
try { |
533 |
< |
for (int i = 0; i < ints.length; i++) { |
588 |
< |
assertEquals(ints[i], q.take()); |
589 |
< |
} |
590 |
< |
} catch (InterruptedException e) { |
591 |
< |
unexpectedException(); |
531 |
> |
ints = q.toArray(ints); |
532 |
> |
for (int i = 0; i < ints.length; i++) { |
533 |
> |
assertEquals(ints[i], q.take()); |
534 |
|
} |
535 |
|
} |
536 |
|
|
537 |
|
/** |
538 |
< |
* toArray(null) throws NPE |
538 |
> |
* toArray(null) throws NullPointerException |
539 |
|
*/ |
540 |
|
public void testToArray_BadArg() { |
541 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
542 |
|
try { |
600 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
543 |
|
Object o[] = q.toArray(null); |
544 |
|
shouldThrow(); |
545 |
< |
} catch (NullPointerException success) { |
604 |
< |
} |
545 |
> |
} catch (NullPointerException success) {} |
546 |
|
} |
547 |
|
|
548 |
|
/** |
549 |
< |
* toArray with incompatible array type throws CCE |
549 |
> |
* toArray(incompatible array type) throws CCE |
550 |
|
*/ |
551 |
|
public void testToArray1_BadArg() { |
552 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
553 |
|
try { |
612 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
554 |
|
Object o[] = q.toArray(new String[10]); |
555 |
|
shouldThrow(); |
556 |
< |
} catch (ArrayStoreException success) { |
616 |
< |
} |
556 |
> |
} catch (ArrayStoreException success) {} |
557 |
|
} |
558 |
|
|
559 |
|
/** |
560 |
|
* iterator iterates through all elements |
561 |
|
*/ |
562 |
< |
public void testIterator() { |
562 |
> |
public void testIterator() throws InterruptedException { |
563 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
564 |
|
Iterator it = q.iterator(); |
565 |
< |
try { |
566 |
< |
while (it.hasNext()) { |
567 |
< |
assertEquals(it.next(), q.take()); |
628 |
< |
} |
629 |
< |
} catch (InterruptedException e) { |
630 |
< |
unexpectedException(); |
565 |
> |
int i = 0; |
566 |
> |
while (it.hasNext()) { |
567 |
> |
assertEquals(it.next(), i++); |
568 |
|
} |
569 |
+ |
assertEquals(i, SIZE); |
570 |
|
} |
571 |
|
|
572 |
|
/** |
573 |
< |
* iterator.remove removes current element |
573 |
> |
* iterator.remove() removes current element |
574 |
|
*/ |
575 |
|
public void testIteratorRemove() { |
576 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
583 |
|
it.remove(); |
584 |
|
|
585 |
|
it = q.iterator(); |
586 |
< |
assertEquals(it.next(), one); |
587 |
< |
assertEquals(it.next(), three); |
586 |
> |
assertSame(it.next(), one); |
587 |
> |
assertSame(it.next(), three); |
588 |
|
assertFalse(it.hasNext()); |
589 |
|
} |
590 |
|
|
592 |
|
* iterator ordering is FIFO |
593 |
|
*/ |
594 |
|
public void testIteratorOrdering() { |
595 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
596 |
< |
int remainingCapacity = q.remainingCapacity(); |
595 |
> |
final LinkedTransferQueue<Integer> q |
596 |
> |
= new LinkedTransferQueue<Integer>(); |
597 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
598 |
|
q.add(one); |
599 |
|
q.add(two); |
600 |
|
q.add(three); |
601 |
< |
assertEquals(remainingCapacity, q.remainingCapacity()); |
601 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
602 |
|
int k = 0; |
603 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
604 |
< |
int i = ((Integer) (it.next())).intValue(); |
666 |
< |
assertEquals(++k, i); |
603 |
> |
for (Integer n : q) { |
604 |
> |
assertEquals(++k, (int) n); |
605 |
|
} |
606 |
|
assertEquals(3, k); |
607 |
|
} |
614 |
|
q.add(one); |
615 |
|
q.add(two); |
616 |
|
q.add(three); |
617 |
< |
try { |
618 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
619 |
< |
q.remove(); |
682 |
< |
it.next(); |
683 |
< |
} |
684 |
< |
} catch (ConcurrentModificationException e) { |
685 |
< |
unexpectedException(); |
617 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
618 |
> |
q.remove(); |
619 |
> |
it.next(); |
620 |
|
} |
621 |
|
assertEquals(0, q.size()); |
622 |
|
} |
640 |
|
q.add(one); |
641 |
|
q.add(two); |
642 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
709 |
– |
executor.execute(new Runnable() { |
643 |
|
|
644 |
< |
public void run() { |
645 |
< |
try { |
646 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
647 |
< |
} catch (Exception e) { |
648 |
< |
threadUnexpectedException(); |
649 |
< |
} |
650 |
< |
} |
651 |
< |
}); |
652 |
< |
|
653 |
< |
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 |
< |
}); |
644 |
> |
executor.execute(new CheckedRunnable() { |
645 |
> |
public void realRun() { |
646 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
647 |
> |
}}); |
648 |
> |
|
649 |
> |
executor.execute(new CheckedRunnable() { |
650 |
> |
public void realRun() throws InterruptedException { |
651 |
> |
Thread.sleep(SMALL_DELAY_MS); |
652 |
> |
assertSame(one, q.take()); |
653 |
> |
}}); |
654 |
|
|
655 |
|
joinPool(executor); |
656 |
|
} |
657 |
|
|
658 |
|
/** |
659 |
< |
* poll retrieves elements across Executor threads |
659 |
> |
* timed poll retrieves elements across Executor threads |
660 |
|
*/ |
661 |
|
public void testPollInExecutor() { |
662 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
663 |
|
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 |
– |
}); |
664 |
|
|
665 |
< |
executor.execute(new Runnable() { |
666 |
< |
|
667 |
< |
public void run() { |
668 |
< |
try { |
669 |
< |
Thread.sleep(SMALL_DELAY_MS); |
670 |
< |
q.put(one); |
671 |
< |
} catch (InterruptedException e) { |
672 |
< |
threadUnexpectedException(); |
673 |
< |
} |
674 |
< |
} |
675 |
< |
}); |
665 |
> |
executor.execute(new CheckedRunnable() { |
666 |
> |
public void realRun() throws InterruptedException { |
667 |
> |
assertNull(q.poll()); |
668 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
669 |
> |
assertTrue(q.isEmpty()); |
670 |
> |
}}); |
671 |
> |
|
672 |
> |
executor.execute(new CheckedRunnable() { |
673 |
> |
public void realRun() throws InterruptedException { |
674 |
> |
Thread.sleep(SMALL_DELAY_MS); |
675 |
> |
q.put(one); |
676 |
> |
}}); |
677 |
|
|
678 |
|
joinPool(executor); |
679 |
|
} |
681 |
|
/** |
682 |
|
* A deserialized serialized queue has same elements in same order |
683 |
|
*/ |
684 |
< |
public void testSerialization() { |
684 |
> |
public void testSerialization() throws Exception { |
685 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
686 |
|
|
687 |
< |
try { |
688 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
689 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
690 |
< |
out.writeObject(q); |
691 |
< |
out.close(); |
692 |
< |
|
693 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
694 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
695 |
< |
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
696 |
< |
|
697 |
< |
assertEquals(q.size(), r.size()); |
698 |
< |
while (!q.isEmpty()) { |
699 |
< |
assertEquals(q.remove(), r.remove()); |
700 |
< |
} |
701 |
< |
} catch (Exception e) { |
790 |
< |
unexpectedException(); |
687 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
688 |
> |
ObjectOutputStream out |
689 |
> |
= new ObjectOutputStream(new BufferedOutputStream(bout)); |
690 |
> |
out.writeObject(q); |
691 |
> |
out.close(); |
692 |
> |
|
693 |
> |
ByteArrayInputStream bin |
694 |
> |
= new ByteArrayInputStream(bout.toByteArray()); |
695 |
> |
ObjectInputStream in |
696 |
> |
= new ObjectInputStream(new BufferedInputStream(bin)); |
697 |
> |
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
698 |
> |
|
699 |
> |
assertEquals(q.size(), r.size()); |
700 |
> |
while (!q.isEmpty()) { |
701 |
> |
assertEquals(q.remove(), r.remove()); |
702 |
|
} |
703 |
|
} |
704 |
|
|
705 |
|
/** |
706 |
< |
* drainTo(null) throws NPE |
706 |
> |
* drainTo(null) throws NullPointerException |
707 |
|
*/ |
708 |
|
public void testDrainToNull() { |
709 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
710 |
|
try { |
711 |
|
q.drainTo(null); |
712 |
|
shouldThrow(); |
713 |
< |
} catch (NullPointerException success) { |
803 |
< |
} |
713 |
> |
} catch (NullPointerException success) {} |
714 |
|
} |
715 |
|
|
716 |
|
/** |
717 |
< |
* drainTo(this) throws IAE |
717 |
> |
* drainTo(this) throws IllegalArgumentException |
718 |
|
*/ |
719 |
|
public void testDrainToSelf() { |
720 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
721 |
|
try { |
722 |
|
q.drainTo(q); |
723 |
|
shouldThrow(); |
724 |
< |
} catch (IllegalArgumentException success) { |
815 |
< |
} |
724 |
> |
} catch (IllegalArgumentException success) {} |
725 |
|
} |
726 |
|
|
727 |
|
/** |
734 |
|
assertEquals(q.size(), 0); |
735 |
|
assertEquals(l.size(), SIZE); |
736 |
|
for (int i = 0; i < SIZE; ++i) { |
737 |
< |
assertEquals(l.get(i), new Integer(i)); |
737 |
> |
assertEquals(l.get(i), i); |
738 |
|
} |
739 |
|
q.add(zero); |
740 |
|
q.add(one); |
746 |
|
assertEquals(q.size(), 0); |
747 |
|
assertEquals(l.size(), 2); |
748 |
|
for (int i = 0; i < 2; ++i) { |
749 |
< |
assertEquals(l.get(i), new Integer(i)); |
749 |
> |
assertEquals(l.get(i), i); |
750 |
|
} |
751 |
|
} |
752 |
|
|
753 |
|
/** |
754 |
< |
* drainTo empties full queue, unblocking a waiting put. |
754 |
> |
* drainTo(c) empties full queue, unblocking a waiting put. |
755 |
|
*/ |
756 |
< |
public void testDrainToWithActivePut() { |
756 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
757 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
758 |
< |
Thread t = new Thread(new Runnable() { |
759 |
< |
|
760 |
< |
public void run() { |
761 |
< |
try { |
762 |
< |
q.put(new Integer(SIZE + 1)); |
763 |
< |
} catch (Exception ie) { |
764 |
< |
threadUnexpectedException(); |
765 |
< |
} |
766 |
< |
} |
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(); |
758 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
759 |
> |
public void realRun() { |
760 |
> |
q.put(SIZE + 1); |
761 |
> |
}}); |
762 |
> |
ArrayList l = new ArrayList(); |
763 |
> |
q.drainTo(l); |
764 |
> |
assertTrue(l.size() >= SIZE); |
765 |
> |
for (int i = 0; i < SIZE; ++i) { |
766 |
> |
assertEquals(l.get(i), i); |
767 |
|
} |
768 |
+ |
awaitTermination(t, MEDIUM_DELAY_MS); |
769 |
+ |
assertTrue(q.size() + l.size() >= SIZE); |
770 |
|
} |
771 |
|
|
772 |
|
/** |
773 |
< |
* drainTo(null, n) throws NPE |
773 |
> |
* drainTo(null, n) throws NullPointerException |
774 |
|
*/ |
775 |
|
public void testDrainToNullN() { |
776 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
777 |
|
try { |
778 |
< |
q.drainTo(null, 0); |
778 |
> |
q.drainTo(null, SIZE); |
779 |
|
shouldThrow(); |
780 |
< |
} catch (NullPointerException success) { |
883 |
< |
} |
780 |
> |
} catch (NullPointerException success) {} |
781 |
|
} |
782 |
|
|
783 |
|
/** |
784 |
< |
* drainTo(this, n) throws IAE |
784 |
> |
* drainTo(this, n) throws IllegalArgumentException |
785 |
|
*/ |
786 |
|
public void testDrainToSelfN() { |
787 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
788 |
|
try { |
789 |
< |
q.drainTo(q, 0); |
789 |
> |
q.drainTo(q, SIZE); |
790 |
|
shouldThrow(); |
791 |
< |
} catch (IllegalArgumentException success) { |
895 |
< |
} |
791 |
> |
} catch (IllegalArgumentException success) {} |
792 |
|
} |
793 |
|
|
794 |
|
/** |
795 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
795 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
796 |
|
*/ |
797 |
|
public void testDrainToN() { |
798 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
799 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
800 |
|
for (int j = 0; j < SIZE; j++) { |
801 |
< |
assertTrue(q.offer(new Integer(j))); |
801 |
> |
assertTrue(q.offer(j)); |
802 |
|
} |
803 |
|
ArrayList l = new ArrayList(); |
804 |
|
q.drainTo(l, i); |
806 |
|
assertEquals(l.size(), k); |
807 |
|
assertEquals(q.size(), SIZE - k); |
808 |
|
for (int j = 0; j < k; ++j) { |
809 |
< |
assertEquals(l.get(j), new Integer(j)); |
809 |
> |
assertEquals(l.get(j), j); |
810 |
|
} |
811 |
< |
while (q.poll() != null); |
811 |
> |
while (q.poll() != null) |
812 |
> |
; |
813 |
|
} |
814 |
|
} |
815 |
|
|
816 |
< |
/* |
817 |
< |
* poll and take should decrement the waiting consumer count |
816 |
> |
/** |
817 |
> |
* timed poll() or take() increments the waiting consumer count; |
818 |
> |
* offer(e) decrements the waiting consumer count |
819 |
|
*/ |
820 |
< |
public void testWaitingConsumer() { |
821 |
< |
try { |
822 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
823 |
< |
final ConsumerObserver waiting = new ConsumerObserver(); |
926 |
< |
new Thread(new Runnable() { |
820 |
> |
public void testWaitingConsumer() throws InterruptedException { |
821 |
> |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
822 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
823 |
> |
assertFalse(q.hasWaitingConsumer()); |
824 |
|
|
825 |
< |
public void run() { |
826 |
< |
try { |
827 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
828 |
< |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
829 |
< |
threadAssertTrue(q.offer(new Object())); |
830 |
< |
} catch (Exception ex) { |
831 |
< |
threadUnexpectedException(); |
832 |
< |
} |
825 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
826 |
> |
public void realRun() throws InterruptedException { |
827 |
> |
Thread.sleep(SMALL_DELAY_MS); |
828 |
> |
assertTrue(q.hasWaitingConsumer()); |
829 |
> |
assertEquals(q.getWaitingConsumerCount(), 1); |
830 |
> |
assertTrue(q.offer(one)); |
831 |
> |
assertFalse(q.hasWaitingConsumer()); |
832 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
833 |
> |
}}); |
834 |
|
|
835 |
< |
} |
836 |
< |
}).start(); |
837 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null); |
838 |
< |
assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers()); |
941 |
< |
} catch (Exception ex) { |
942 |
< |
this.unexpectedException(); |
943 |
< |
} |
835 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
836 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
837 |
> |
assertFalse(q.hasWaitingConsumer()); |
838 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
839 |
|
} |
945 |
– |
/* |
946 |
– |
* Inserts null into transfer throws NPE |
947 |
– |
*/ |
840 |
|
|
841 |
< |
public void testTransfer1() { |
841 |
> |
/** |
842 |
> |
* transfer(null) throws NullPointerException |
843 |
> |
*/ |
844 |
> |
public void testTransfer1() throws InterruptedException { |
845 |
|
try { |
846 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
847 |
|
q.transfer(null); |
848 |
|
shouldThrow(); |
849 |
< |
} catch (NullPointerException ex) { |
955 |
< |
} catch (Exception ex) { |
956 |
< |
this.unexpectedException(); |
957 |
< |
} |
849 |
> |
} catch (NullPointerException success) {} |
850 |
|
} |
851 |
|
|
852 |
< |
/* |
853 |
< |
* transfer attempts to insert into the queue then wait until that |
854 |
< |
* object is removed via take or poll. |
852 |
> |
/** |
853 |
> |
* transfer waits until a poll occurs. The transfered element |
854 |
> |
* is returned by this associated poll. |
855 |
|
*/ |
856 |
< |
public void testTransfer2() { |
857 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
858 |
< |
new Thread(new Runnable() { |
856 |
> |
public void testTransfer2() throws InterruptedException { |
857 |
> |
final LinkedTransferQueue<Integer> q |
858 |
> |
= new LinkedTransferQueue<Integer>(); |
859 |
|
|
860 |
< |
public void run() { |
861 |
< |
try { |
862 |
< |
q.transfer(new Integer(SIZE)); |
863 |
< |
threadAssertTrue(q.isEmpty()); |
864 |
< |
} catch (Exception ex) { |
973 |
< |
threadUnexpectedException(); |
974 |
< |
} |
975 |
< |
} |
976 |
< |
}).start(); |
860 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
861 |
> |
public void realRun() throws InterruptedException { |
862 |
> |
q.transfer(SIZE); |
863 |
> |
assertTrue(q.isEmpty()); |
864 |
> |
}}); |
865 |
|
|
866 |
< |
try { |
867 |
< |
Thread.sleep(SHORT_DELAY_MS); |
868 |
< |
assertEquals(1, q.size()); |
869 |
< |
q.poll(); |
870 |
< |
assertTrue(q.isEmpty()); |
871 |
< |
} catch (Exception ex) { |
872 |
< |
this.unexpectedException(); |
873 |
< |
} |
874 |
< |
} |
875 |
< |
/* |
876 |
< |
* transfer will attempt to transfer in fifo order and continue waiting if |
877 |
< |
* the element being transfered is not polled or taken |
878 |
< |
*/ |
879 |
< |
|
880 |
< |
public void testTransfer3() { |
881 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
882 |
< |
new Thread(new Runnable() { |
883 |
< |
public void run() { |
884 |
< |
try { |
885 |
< |
Integer i; |
886 |
< |
q.transfer((i = new Integer(SIZE + 1))); |
887 |
< |
threadAssertTrue(!q.contains(i)); |
888 |
< |
threadAssertEquals(1, q.size()); |
889 |
< |
} catch (Exception ex) { |
890 |
< |
threadUnexpectedException(); |
891 |
< |
} |
892 |
< |
} |
893 |
< |
}).start(); |
894 |
< |
Thread interruptedThread = |
895 |
< |
new Thread(new Runnable() { |
896 |
< |
public void run() { |
897 |
< |
try { |
898 |
< |
q.transfer(new Integer(SIZE)); |
899 |
< |
threadShouldThrow(); |
900 |
< |
} catch (InterruptedException ex) { |
901 |
< |
} |
902 |
< |
} |
903 |
< |
}); |
904 |
< |
interruptedThread.start(); |
905 |
< |
try { |
1018 |
< |
Thread.sleep(LONG_DELAY_MS); |
1019 |
< |
assertEquals(2, q.size()); |
1020 |
< |
q.poll(); |
1021 |
< |
Thread.sleep(LONG_DELAY_MS); |
1022 |
< |
interruptedThread.interrupt(); |
1023 |
< |
assertEquals(1, q.size()); |
1024 |
< |
} catch (Exception ex) { |
1025 |
< |
this.unexpectedException(); |
1026 |
< |
} |
866 |
> |
Thread.sleep(SHORT_DELAY_MS); |
867 |
> |
assertEquals(1, q.size()); |
868 |
> |
assertEquals(SIZE, (int) q.poll()); |
869 |
> |
assertTrue(q.isEmpty()); |
870 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
871 |
> |
} |
872 |
> |
|
873 |
> |
/** |
874 |
> |
* transfer waits until a poll occurs, and then transfers in fifo order |
875 |
> |
*/ |
876 |
> |
public void testTransfer3() throws InterruptedException { |
877 |
> |
final LinkedTransferQueue<Integer> q |
878 |
> |
= new LinkedTransferQueue<Integer>(); |
879 |
> |
|
880 |
> |
Thread first = newStartedThread(new CheckedRunnable() { |
881 |
> |
public void realRun() throws InterruptedException { |
882 |
> |
Integer i = SIZE + 1; |
883 |
> |
q.transfer(i); |
884 |
> |
assertTrue(!q.contains(i)); |
885 |
> |
assertEquals(1, q.size()); |
886 |
> |
}}); |
887 |
> |
|
888 |
> |
Thread interruptedThread = newStartedThread( |
889 |
> |
new CheckedInterruptedRunnable() { |
890 |
> |
public void realRun() throws InterruptedException { |
891 |
> |
while (q.size() == 0) |
892 |
> |
Thread.yield(); |
893 |
> |
q.transfer(SIZE); |
894 |
> |
}}); |
895 |
> |
|
896 |
> |
while (q.size() < 2) |
897 |
> |
Thread.yield(); |
898 |
> |
assertEquals(2, q.size()); |
899 |
> |
assertEquals(SIZE + 1, (int) q.poll()); |
900 |
> |
first.join(); |
901 |
> |
assertEquals(1, q.size()); |
902 |
> |
interruptedThread.interrupt(); |
903 |
> |
interruptedThread.join(); |
904 |
> |
assertEquals(0, q.size()); |
905 |
> |
assertTrue(q.isEmpty()); |
906 |
|
} |
907 |
|
|
908 |
|
/** |
909 |
< |
* transfer will wait as long as a poll or take occurs if one does occur |
910 |
< |
* the waiting is finished and the thread that tries to poll/take |
1032 |
< |
* wins in retrieving the element |
909 |
> |
* transfer waits until a poll occurs, at which point the polling |
910 |
> |
* thread returns the element |
911 |
|
*/ |
912 |
< |
public void testTransfer4() { |
912 |
> |
public void testTransfer4() throws InterruptedException { |
913 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1036 |
– |
new Thread(new Runnable() { |
914 |
|
|
915 |
< |
public void run() { |
916 |
< |
try { |
917 |
< |
q.transfer(new Integer(four)); |
918 |
< |
threadAssertFalse(q.contains(new Integer(four))); |
919 |
< |
threadAssertEquals(new Integer(three), q.poll()); |
920 |
< |
} catch (Exception ex) { |
921 |
< |
threadUnexpectedException(); |
922 |
< |
} |
923 |
< |
} |
924 |
< |
}).start(); |
925 |
< |
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 |
< |
} |
915 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
916 |
> |
public void realRun() throws InterruptedException { |
917 |
> |
q.transfer(four); |
918 |
> |
assertFalse(q.contains(four)); |
919 |
> |
assertSame(three, q.poll()); |
920 |
> |
}}); |
921 |
> |
|
922 |
> |
Thread.sleep(SHORT_DELAY_MS); |
923 |
> |
assertTrue(q.offer(three)); |
924 |
> |
assertSame(four, q.poll()); |
925 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
926 |
|
} |
927 |
< |
/* |
928 |
< |
* Insert null into trTransfer throws NPE |
927 |
> |
|
928 |
> |
/** |
929 |
> |
* transfer waits until a take occurs. The transfered element |
930 |
> |
* is returned by this associated take. |
931 |
|
*/ |
932 |
+ |
public void testTransfer5() throws InterruptedException { |
933 |
+ |
final LinkedTransferQueue<Integer> q |
934 |
+ |
= new LinkedTransferQueue<Integer>(); |
935 |
+ |
|
936 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
937 |
+ |
public void realRun() throws InterruptedException { |
938 |
+ |
q.transfer(SIZE); |
939 |
+ |
checkEmpty(q); |
940 |
+ |
}}); |
941 |
|
|
942 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
943 |
+ |
assertEquals(SIZE, (int) q.take()); |
944 |
+ |
checkEmpty(q); |
945 |
+ |
awaitTermination(t, MEDIUM_DELAY_MS); |
946 |
+ |
} |
947 |
+ |
|
948 |
+ |
/** |
949 |
+ |
* tryTransfer(null) throws NullPointerException |
950 |
+ |
*/ |
951 |
|
public void testTryTransfer1() { |
952 |
|
try { |
953 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
954 |
|
q.tryTransfer(null); |
955 |
< |
this.shouldThrow(); |
956 |
< |
} catch (NullPointerException ex) { |
1066 |
< |
} catch (Exception ex) { |
1067 |
< |
this.unexpectedException(); |
1068 |
< |
} |
955 |
> |
shouldThrow(); |
956 |
> |
} catch (NullPointerException success) {} |
957 |
|
} |
1070 |
– |
/* |
1071 |
– |
* tryTransfer returns false and does not enqueue if there are no consumers |
1072 |
– |
* waiting to poll or take. |
1073 |
– |
*/ |
958 |
|
|
959 |
< |
public void testTryTransfer2() { |
960 |
< |
try { |
961 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
962 |
< |
assertFalse(q.tryTransfer(new Object())); |
963 |
< |
assertEquals(0, q.size()); |
964 |
< |
} catch (Exception ex) { |
965 |
< |
this.unexpectedException(); |
966 |
< |
} |
959 |
> |
/** |
960 |
> |
* tryTransfer returns false and does not enqueue if there are no |
961 |
> |
* consumers waiting to poll or take. |
962 |
> |
*/ |
963 |
> |
public void testTryTransfer2() throws InterruptedException { |
964 |
> |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
965 |
> |
assertFalse(q.tryTransfer(new Object())); |
966 |
> |
assertFalse(q.hasWaitingConsumer()); |
967 |
> |
checkEmpty(q); |
968 |
|
} |
969 |
< |
/* |
970 |
< |
* if there is a consumer waiting poll or take tryTransfer returns |
971 |
< |
* true while enqueueing object |
969 |
> |
|
970 |
> |
/** |
971 |
> |
* If there is a consumer waiting in timed poll, tryTransfer |
972 |
> |
* returns true while successfully transfering object. |
973 |
|
*/ |
974 |
+ |
public void testTryTransfer3() throws InterruptedException { |
975 |
+ |
final Object hotPotato = new Object(); |
976 |
+ |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
977 |
|
|
978 |
< |
public void testTryTransfer3() { |
979 |
< |
try { |
980 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
981 |
< |
new Thread(new Runnable() { |
978 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
979 |
> |
public void realRun() { |
980 |
> |
while (! q.hasWaitingConsumer()) |
981 |
> |
Thread.yield(); |
982 |
> |
assertTrue(q.hasWaitingConsumer()); |
983 |
> |
assertTrue(q.isEmpty()); |
984 |
> |
assertEquals(q.size(), 0); |
985 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
986 |
> |
}}); |
987 |
> |
|
988 |
> |
assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
989 |
> |
checkEmpty(q); |
990 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
991 |
> |
} |
992 |
|
|
993 |
< |
public void run() { |
994 |
< |
try { |
995 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
996 |
< |
threadAssertTrue(q.tryTransfer(new Object())); |
997 |
< |
} catch (Exception ex) { |
998 |
< |
threadUnexpectedException(); |
999 |
< |
} |
993 |
> |
/** |
994 |
> |
* If there is a consumer waiting in take, tryTransfer returns |
995 |
> |
* true while successfully transfering object. |
996 |
> |
*/ |
997 |
> |
public void testTryTransfer4() throws InterruptedException { |
998 |
> |
final Object hotPotato = new Object(); |
999 |
> |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1000 |
|
|
1001 |
< |
} |
1002 |
< |
}).start(); |
1003 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null); |
1004 |
< |
assertTrue(q.isEmpty()); |
1005 |
< |
} catch (Exception ex) { |
1006 |
< |
this.unexpectedException(); |
1007 |
< |
} |
1001 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1002 |
> |
public void realRun() { |
1003 |
> |
while (! q.hasWaitingConsumer()) |
1004 |
> |
Thread.yield(); |
1005 |
> |
assertTrue(q.hasWaitingConsumer()); |
1006 |
> |
assertTrue(q.isEmpty()); |
1007 |
> |
assertEquals(q.size(), 0); |
1008 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
1009 |
> |
}}); |
1010 |
> |
|
1011 |
> |
assertSame(q.take(), hotPotato); |
1012 |
> |
checkEmpty(q); |
1013 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1014 |
|
} |
1015 |
|
|
1016 |
< |
/* |
1017 |
< |
* tryTransfer waits the amount given if interrupted, show an |
1018 |
< |
* interrupted exception |
1016 |
> |
/** |
1017 |
> |
* tryTransfer waits the amount given if interrupted, and |
1018 |
> |
* throws interrupted exception |
1019 |
|
*/ |
1020 |
< |
public void testTryTransfer4() { |
1020 |
> |
public void testTryTransfer5() throws InterruptedException { |
1021 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1117 |
– |
Thread toInterrupt = new Thread(new Runnable() { |
1022 |
|
|
1023 |
< |
public void run() { |
1024 |
< |
try { |
1025 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1026 |
< |
threadShouldThrow(); |
1027 |
< |
} catch (InterruptedException ex) { |
1028 |
< |
} |
1029 |
< |
} |
1030 |
< |
}); |
1127 |
< |
try { |
1128 |
< |
toInterrupt.start(); |
1129 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1130 |
< |
toInterrupt.interrupt(); |
1131 |
< |
} catch (Exception ex) { |
1132 |
< |
this.unexpectedException(); |
1133 |
< |
} |
1023 |
> |
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
1024 |
> |
public void realRun() throws InterruptedException { |
1025 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1026 |
> |
}}); |
1027 |
> |
|
1028 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1029 |
> |
toInterrupt.interrupt(); |
1030 |
> |
toInterrupt.join(); |
1031 |
|
} |
1032 |
|
|
1033 |
< |
/* |
1033 |
> |
/** |
1034 |
|
* tryTransfer gives up after the timeout and return false |
1035 |
|
*/ |
1036 |
< |
public void testTryTransfer5() { |
1036 |
> |
public void testTryTransfer6() throws InterruptedException { |
1037 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1141 |
– |
try { |
1142 |
– |
new Thread(new Runnable() { |
1038 |
|
|
1039 |
< |
public void run() { |
1040 |
< |
try { |
1041 |
< |
threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1042 |
< |
} catch (InterruptedException ex) { |
1043 |
< |
threadUnexpectedException(); |
1044 |
< |
} |
1045 |
< |
} |
1046 |
< |
}).start(); |
1047 |
< |
Thread.sleep(LONG_DELAY_MS); |
1048 |
< |
assertTrue(q.isEmpty()); |
1049 |
< |
} catch (Exception ex) { |
1155 |
< |
this.unexpectedException(); |
1156 |
< |
} |
1039 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1040 |
> |
public void realRun() throws InterruptedException { |
1041 |
> |
long t0 = System.nanoTime(); |
1042 |
> |
assertFalse(q.tryTransfer(new Object(), |
1043 |
> |
SHORT_DELAY_MS, MILLISECONDS)); |
1044 |
> |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1045 |
> |
}}); |
1046 |
> |
|
1047 |
> |
checkEmpty(q); |
1048 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1049 |
> |
checkEmpty(q); |
1050 |
|
} |
1051 |
|
|
1052 |
< |
/* |
1052 |
> |
/** |
1053 |
|
* tryTransfer waits for any elements previously in to be removed |
1054 |
|
* before transfering to a poll or take |
1055 |
|
*/ |
1056 |
< |
public void testTryTransfer6() { |
1056 |
> |
public void testTryTransfer7() throws InterruptedException { |
1057 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1058 |
< |
q.offer(new Integer(four)); |
1166 |
< |
new Thread(new Runnable() { |
1058 |
> |
assertTrue(q.offer(four)); |
1059 |
|
|
1060 |
< |
public void run() { |
1061 |
< |
try { |
1062 |
< |
threadAssertTrue(q.tryTransfer(new Integer(five), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
1063 |
< |
threadAssertTrue(q.isEmpty()); |
1064 |
< |
} catch (InterruptedException ex) { |
1065 |
< |
threadUnexpectedException(); |
1066 |
< |
} |
1067 |
< |
} |
1068 |
< |
}).start(); |
1069 |
< |
try { |
1070 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1071 |
< |
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 |
< |
} |
1060 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1061 |
> |
public void realRun() throws InterruptedException { |
1062 |
> |
assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); |
1063 |
> |
assertTrue(q.isEmpty()); |
1064 |
> |
}}); |
1065 |
> |
|
1066 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1067 |
> |
assertEquals(2, q.size()); |
1068 |
> |
assertSame(four, q.poll()); |
1069 |
> |
assertSame(five, q.poll()); |
1070 |
> |
checkEmpty(q); |
1071 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1072 |
|
} |
1073 |
|
|
1074 |
< |
/* |
1075 |
< |
* tryTransfer attempts to enqueue into the q and fails returning false not |
1076 |
< |
* enqueueing and the successing poll is null |
1074 |
> |
/** |
1075 |
> |
* tryTransfer attempts to enqueue into the q and fails returning |
1076 |
> |
* false not enqueueing and the successive poll is null |
1077 |
|
*/ |
1078 |
< |
public void testTryTransfer7() { |
1078 |
> |
public void testTryTransfer8() throws InterruptedException { |
1079 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1080 |
< |
q.offer(new Integer(four)); |
1081 |
< |
new Thread(new Runnable() { |
1082 |
< |
|
1083 |
< |
public void run() { |
1084 |
< |
try { |
1085 |
< |
threadAssertFalse(q.tryTransfer(new Integer(five), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1086 |
< |
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 |
< |
} |
1080 |
> |
assertTrue(q.offer(four)); |
1081 |
> |
assertEquals(1, q.size()); |
1082 |
> |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1083 |
> |
assertEquals(1, q.size()); |
1084 |
> |
assertSame(four, q.poll()); |
1085 |
> |
assertNull(q.poll()); |
1086 |
> |
checkEmpty(q); |
1087 |
|
} |
1088 |
|
|
1089 |
< |
private LinkedTransferQueue populatedQueue( |
1090 |
< |
int n) { |
1218 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
1089 |
> |
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1090 |
> |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
1091 |
|
assertTrue(q.isEmpty()); |
1092 |
< |
int remainingCapacity = q.remainingCapacity(); |
1093 |
< |
for (int i = 0; i < |
1222 |
< |
n; i++) { |
1092 |
> |
for (int i = 0; i < n; i++) { |
1093 |
> |
assertEquals(i, q.size()); |
1094 |
|
assertTrue(q.offer(i)); |
1095 |
+ |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
1096 |
|
} |
1225 |
– |
|
1097 |
|
assertFalse(q.isEmpty()); |
1227 |
– |
assertEquals(remainingCapacity, q.remainingCapacity()); |
1228 |
– |
assertEquals(n, q.size()); |
1098 |
|
return q; |
1099 |
|
} |
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 |
– |
} |
1100 |
|
} |