9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
|
import java.io.*; |
14 |
|
|
15 |
|
public class SynchronousQueueTest extends JSR166TestCase { |
16 |
|
|
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
20 |
|
|
21 |
|
public static Test suite() { |
22 |
< |
return new TestSuite(SynchronousQueueTest.class); |
22 |
> |
return new TestSuite(SynchronousQueueTest.class); |
23 |
|
} |
24 |
|
|
25 |
|
/** |
28 |
|
public void testEmptyFull() { |
29 |
|
SynchronousQueue q = new SynchronousQueue(); |
30 |
|
assertTrue(q.isEmpty()); |
31 |
< |
assertEquals(0, q.size()); |
31 |
> |
assertEquals(0, q.size()); |
32 |
|
assertEquals(0, q.remainingCapacity()); |
33 |
|
assertFalse(q.offer(zero)); |
34 |
|
} |
39 |
|
public void testFairEmptyFull() { |
40 |
|
SynchronousQueue q = new SynchronousQueue(true); |
41 |
|
assertTrue(q.isEmpty()); |
42 |
< |
assertEquals(0, q.size()); |
42 |
> |
assertEquals(0, q.size()); |
43 |
|
assertEquals(0, q.remainingCapacity()); |
44 |
|
assertFalse(q.offer(zero)); |
45 |
|
} |
48 |
|
* offer(null) throws NPE |
49 |
|
*/ |
50 |
|
public void testOfferNull() { |
51 |
< |
try { |
51 |
> |
try { |
52 |
|
SynchronousQueue q = new SynchronousQueue(); |
53 |
|
q.offer(null); |
54 |
|
shouldThrow(); |
55 |
< |
} catch (NullPointerException success) { } |
55 |
> |
} catch (NullPointerException success) {} |
56 |
|
} |
57 |
|
|
58 |
|
/** |
59 |
|
* add(null) throws NPE |
60 |
|
*/ |
61 |
|
public void testAddNull() { |
62 |
< |
try { |
62 |
> |
try { |
63 |
|
SynchronousQueue q = new SynchronousQueue(); |
64 |
|
q.add(null); |
65 |
|
shouldThrow(); |
66 |
< |
} catch (NullPointerException success) { } |
66 |
> |
} catch (NullPointerException success) {} |
67 |
|
} |
68 |
|
|
69 |
|
/** |
78 |
|
* add throws ISE if no active taker |
79 |
|
*/ |
80 |
|
public void testAdd() { |
81 |
< |
try { |
81 |
> |
try { |
82 |
|
SynchronousQueue q = new SynchronousQueue(); |
83 |
|
assertEquals(0, q.remainingCapacity()); |
84 |
|
q.add(one); |
85 |
|
shouldThrow(); |
86 |
< |
} catch (IllegalStateException success){ |
86 |
< |
} |
86 |
> |
} catch (IllegalStateException success) {} |
87 |
|
} |
88 |
|
|
89 |
|
/** |
94 |
|
SynchronousQueue q = new SynchronousQueue(); |
95 |
|
q.addAll(null); |
96 |
|
shouldThrow(); |
97 |
< |
} |
98 |
< |
catch (NullPointerException success) {} |
97 |
> |
} catch (NullPointerException success) {} |
98 |
|
} |
99 |
|
|
100 |
|
/** |
105 |
|
SynchronousQueue q = new SynchronousQueue(); |
106 |
|
q.addAll(q); |
107 |
|
shouldThrow(); |
108 |
< |
} |
110 |
< |
catch (IllegalArgumentException success) {} |
108 |
> |
} catch (IllegalArgumentException success) {} |
109 |
|
} |
110 |
|
|
111 |
|
/** |
117 |
|
Integer[] ints = new Integer[1]; |
118 |
|
q.addAll(Arrays.asList(ints)); |
119 |
|
shouldThrow(); |
120 |
< |
} |
123 |
< |
catch (NullPointerException success) {} |
120 |
> |
} catch (NullPointerException success) {} |
121 |
|
} |
122 |
+ |
|
123 |
|
/** |
124 |
|
* addAll throws ISE if no active taker |
125 |
|
*/ |
131 |
|
ints[i] = new Integer(i); |
132 |
|
q.addAll(Arrays.asList(ints)); |
133 |
|
shouldThrow(); |
134 |
< |
} |
137 |
< |
catch (IllegalStateException success) {} |
134 |
> |
} catch (IllegalStateException success) {} |
135 |
|
} |
136 |
|
|
137 |
|
/** |
138 |
|
* put(null) throws NPE |
139 |
|
*/ |
140 |
< |
public void testPutNull() { |
141 |
< |
try { |
140 |
> |
public void testPutNull() throws InterruptedException { |
141 |
> |
try { |
142 |
|
SynchronousQueue q = new SynchronousQueue(); |
143 |
|
q.put(null); |
144 |
|
shouldThrow(); |
145 |
< |
} |
149 |
< |
catch (NullPointerException success){ |
150 |
< |
} |
151 |
< |
catch (InterruptedException ie) { |
152 |
< |
unexpectedException(); |
153 |
< |
} |
145 |
> |
} catch (NullPointerException success) {} |
146 |
|
} |
147 |
|
|
148 |
|
/** |
149 |
|
* put blocks interruptibly if no active taker |
150 |
|
*/ |
151 |
< |
public void testBlockingPut() { |
152 |
< |
Thread t = new Thread(new Runnable() { |
153 |
< |
public void run() { |
154 |
< |
try { |
155 |
< |
SynchronousQueue q = new SynchronousQueue(); |
156 |
< |
q.put(zero); |
157 |
< |
threadShouldThrow(); |
166 |
< |
} catch (InterruptedException ie){ |
167 |
< |
} |
168 |
< |
}}); |
151 |
> |
public void testBlockingPut() throws InterruptedException { |
152 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
153 |
> |
public void realRun() throws InterruptedException { |
154 |
> |
SynchronousQueue q = new SynchronousQueue(); |
155 |
> |
q.put(zero); |
156 |
> |
}}); |
157 |
> |
|
158 |
|
t.start(); |
159 |
< |
try { |
160 |
< |
Thread.sleep(SHORT_DELAY_MS); |
161 |
< |
t.interrupt(); |
173 |
< |
t.join(); |
174 |
< |
} |
175 |
< |
catch (InterruptedException ie) { |
176 |
< |
unexpectedException(); |
177 |
< |
} |
159 |
> |
Thread.sleep(SHORT_DELAY_MS); |
160 |
> |
t.interrupt(); |
161 |
> |
t.join(); |
162 |
|
} |
163 |
|
|
164 |
|
/** |
165 |
|
* put blocks waiting for take |
166 |
|
*/ |
167 |
< |
public void testPutWithTake() { |
167 |
> |
public void testPutWithTake() throws InterruptedException { |
168 |
|
final SynchronousQueue q = new SynchronousQueue(); |
169 |
< |
Thread t = new Thread(new Runnable() { |
170 |
< |
public void run() { |
171 |
< |
int added = 0; |
172 |
< |
try { |
173 |
< |
q.put(new Object()); |
174 |
< |
++added; |
191 |
< |
q.put(new Object()); |
192 |
< |
++added; |
193 |
< |
q.put(new Object()); |
194 |
< |
++added; |
195 |
< |
q.put(new Object()); |
169 |
> |
Thread t = new Thread(new CheckedRunnable() { |
170 |
> |
public void realRun() throws InterruptedException { |
171 |
> |
int added = 0; |
172 |
> |
try { |
173 |
> |
while (true) { |
174 |
> |
q.put(added); |
175 |
|
++added; |
197 |
– |
threadShouldThrow(); |
198 |
– |
} catch (InterruptedException e){ |
199 |
– |
assertTrue(added >= 1); |
176 |
|
} |
177 |
+ |
} catch (InterruptedException success) { |
178 |
+ |
assertEquals(1, added); |
179 |
|
} |
180 |
< |
}); |
181 |
< |
try { |
182 |
< |
t.start(); |
183 |
< |
Thread.sleep(SHORT_DELAY_MS); |
184 |
< |
q.take(); |
185 |
< |
Thread.sleep(SHORT_DELAY_MS); |
186 |
< |
t.interrupt(); |
187 |
< |
t.join(); |
210 |
< |
} catch (Exception e){ |
211 |
< |
unexpectedException(); |
212 |
< |
} |
180 |
> |
}}); |
181 |
> |
|
182 |
> |
t.start(); |
183 |
> |
Thread.sleep(SHORT_DELAY_MS); |
184 |
> |
assertEquals(0, q.take()); |
185 |
> |
Thread.sleep(SHORT_DELAY_MS); |
186 |
> |
t.interrupt(); |
187 |
> |
t.join(); |
188 |
|
} |
189 |
|
|
190 |
|
/** |
191 |
|
* timed offer times out if elements not taken |
192 |
|
*/ |
193 |
< |
public void testTimedOffer() { |
193 |
> |
public void testTimedOffer() throws InterruptedException { |
194 |
|
final SynchronousQueue q = new SynchronousQueue(); |
195 |
< |
Thread t = new Thread(new Runnable() { |
196 |
< |
public void run() { |
197 |
< |
try { |
198 |
< |
|
199 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
225 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
226 |
< |
threadShouldThrow(); |
227 |
< |
} catch (InterruptedException success){} |
228 |
< |
} |
229 |
< |
}); |
195 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
196 |
> |
public void realRun() throws InterruptedException { |
197 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
198 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
199 |
> |
}}); |
200 |
|
|
201 |
< |
try { |
202 |
< |
t.start(); |
203 |
< |
Thread.sleep(SMALL_DELAY_MS); |
204 |
< |
t.interrupt(); |
235 |
< |
t.join(); |
236 |
< |
} catch (Exception e){ |
237 |
< |
unexpectedException(); |
238 |
< |
} |
201 |
> |
t.start(); |
202 |
> |
Thread.sleep(SMALL_DELAY_MS); |
203 |
> |
t.interrupt(); |
204 |
> |
t.join(); |
205 |
|
} |
206 |
|
|
207 |
|
|
208 |
|
/** |
209 |
|
* take blocks interruptibly when empty |
210 |
|
*/ |
211 |
< |
public void testTakeFromEmpty() { |
211 |
> |
public void testTakeFromEmpty() throws InterruptedException { |
212 |
|
final SynchronousQueue q = new SynchronousQueue(); |
213 |
< |
Thread t = new Thread(new Runnable() { |
214 |
< |
public void run() { |
215 |
< |
try { |
216 |
< |
q.take(); |
217 |
< |
threadShouldThrow(); |
218 |
< |
} catch (InterruptedException success){ } |
219 |
< |
} |
220 |
< |
}); |
221 |
< |
try { |
256 |
< |
t.start(); |
257 |
< |
Thread.sleep(SHORT_DELAY_MS); |
258 |
< |
t.interrupt(); |
259 |
< |
t.join(); |
260 |
< |
} catch (Exception e){ |
261 |
< |
unexpectedException(); |
262 |
< |
} |
213 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
214 |
> |
public void realRun() throws InterruptedException { |
215 |
> |
q.take(); |
216 |
> |
}}); |
217 |
> |
|
218 |
> |
t.start(); |
219 |
> |
Thread.sleep(SHORT_DELAY_MS); |
220 |
> |
t.interrupt(); |
221 |
> |
t.join(); |
222 |
|
} |
223 |
|
|
224 |
|
|
225 |
|
/** |
226 |
|
* put blocks interruptibly if no active taker |
227 |
|
*/ |
228 |
< |
public void testFairBlockingPut() { |
229 |
< |
Thread t = new Thread(new Runnable() { |
230 |
< |
public void run() { |
231 |
< |
try { |
232 |
< |
SynchronousQueue q = new SynchronousQueue(true); |
233 |
< |
q.put(zero); |
234 |
< |
threadShouldThrow(); |
276 |
< |
} catch (InterruptedException ie){ |
277 |
< |
} |
278 |
< |
}}); |
228 |
> |
public void testFairBlockingPut() throws InterruptedException { |
229 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
230 |
> |
public void realRun() throws InterruptedException { |
231 |
> |
SynchronousQueue q = new SynchronousQueue(true); |
232 |
> |
q.put(zero); |
233 |
> |
}}); |
234 |
> |
|
235 |
|
t.start(); |
236 |
< |
try { |
237 |
< |
Thread.sleep(SHORT_DELAY_MS); |
238 |
< |
t.interrupt(); |
283 |
< |
t.join(); |
284 |
< |
} |
285 |
< |
catch (InterruptedException ie) { |
286 |
< |
unexpectedException(); |
287 |
< |
} |
236 |
> |
Thread.sleep(SHORT_DELAY_MS); |
237 |
> |
t.interrupt(); |
238 |
> |
t.join(); |
239 |
|
} |
240 |
|
|
241 |
|
/** |
242 |
|
* put blocks waiting for take |
243 |
|
*/ |
244 |
< |
public void testFairPutWithTake() { |
244 |
> |
public void testFairPutWithTake() throws InterruptedException { |
245 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
246 |
< |
Thread t = new Thread(new Runnable() { |
247 |
< |
public void run() { |
248 |
< |
int added = 0; |
249 |
< |
try { |
250 |
< |
q.put(new Object()); |
251 |
< |
++added; |
301 |
< |
q.put(new Object()); |
302 |
< |
++added; |
303 |
< |
q.put(new Object()); |
304 |
< |
++added; |
305 |
< |
q.put(new Object()); |
246 |
> |
Thread t = new Thread(new CheckedRunnable() { |
247 |
> |
public void realRun() throws InterruptedException { |
248 |
> |
int added = 0; |
249 |
> |
try { |
250 |
> |
while (true) { |
251 |
> |
q.put(added); |
252 |
|
++added; |
307 |
– |
threadShouldThrow(); |
308 |
– |
} catch (InterruptedException e){ |
309 |
– |
assertTrue(added >= 1); |
253 |
|
} |
254 |
+ |
} catch (InterruptedException success) { |
255 |
+ |
assertEquals(1, added); |
256 |
|
} |
257 |
< |
}); |
258 |
< |
try { |
259 |
< |
t.start(); |
260 |
< |
Thread.sleep(SHORT_DELAY_MS); |
261 |
< |
q.take(); |
262 |
< |
Thread.sleep(SHORT_DELAY_MS); |
263 |
< |
t.interrupt(); |
264 |
< |
t.join(); |
320 |
< |
} catch (Exception e){ |
321 |
< |
unexpectedException(); |
322 |
< |
} |
257 |
> |
}}); |
258 |
> |
|
259 |
> |
t.start(); |
260 |
> |
Thread.sleep(SHORT_DELAY_MS); |
261 |
> |
assertEquals(0, q.take()); |
262 |
> |
Thread.sleep(SHORT_DELAY_MS); |
263 |
> |
t.interrupt(); |
264 |
> |
t.join(); |
265 |
|
} |
266 |
|
|
267 |
|
/** |
268 |
|
* timed offer times out if elements not taken |
269 |
|
*/ |
270 |
< |
public void testFairTimedOffer() { |
270 |
> |
public void testFairTimedOffer() throws InterruptedException { |
271 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
272 |
< |
Thread t = new Thread(new Runnable() { |
273 |
< |
public void run() { |
274 |
< |
try { |
275 |
< |
|
276 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
335 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
336 |
< |
threadShouldThrow(); |
337 |
< |
} catch (InterruptedException success){} |
338 |
< |
} |
339 |
< |
}); |
272 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
273 |
> |
public void realRun() throws InterruptedException { |
274 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
275 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
276 |
> |
}}); |
277 |
|
|
278 |
< |
try { |
279 |
< |
t.start(); |
280 |
< |
Thread.sleep(SMALL_DELAY_MS); |
281 |
< |
t.interrupt(); |
345 |
< |
t.join(); |
346 |
< |
} catch (Exception e){ |
347 |
< |
unexpectedException(); |
348 |
< |
} |
278 |
> |
t.start(); |
279 |
> |
Thread.sleep(SMALL_DELAY_MS); |
280 |
> |
t.interrupt(); |
281 |
> |
t.join(); |
282 |
|
} |
283 |
|
|
284 |
|
|
285 |
|
/** |
286 |
|
* take blocks interruptibly when empty |
287 |
|
*/ |
288 |
< |
public void testFairTakeFromEmpty() { |
288 |
> |
public void testFairTakeFromEmpty() throws InterruptedException { |
289 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
290 |
< |
Thread t = new Thread(new Runnable() { |
291 |
< |
public void run() { |
292 |
< |
try { |
293 |
< |
q.take(); |
294 |
< |
threadShouldThrow(); |
295 |
< |
} catch (InterruptedException success){ } |
296 |
< |
} |
297 |
< |
}); |
298 |
< |
try { |
366 |
< |
t.start(); |
367 |
< |
Thread.sleep(SHORT_DELAY_MS); |
368 |
< |
t.interrupt(); |
369 |
< |
t.join(); |
370 |
< |
} catch (Exception e){ |
371 |
< |
unexpectedException(); |
372 |
< |
} |
290 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
291 |
> |
public void realRun() throws InterruptedException { |
292 |
> |
q.take(); |
293 |
> |
}}); |
294 |
> |
|
295 |
> |
t.start(); |
296 |
> |
Thread.sleep(SHORT_DELAY_MS); |
297 |
> |
t.interrupt(); |
298 |
> |
t.join(); |
299 |
|
} |
300 |
|
|
301 |
|
/** |
303 |
|
*/ |
304 |
|
public void testPoll() { |
305 |
|
SynchronousQueue q = new SynchronousQueue(); |
306 |
< |
assertNull(q.poll()); |
306 |
> |
assertNull(q.poll()); |
307 |
|
} |
308 |
|
|
309 |
|
/** |
310 |
|
* timed pool with zero timeout times out if no active taker |
311 |
|
*/ |
312 |
< |
public void testTimedPoll0() { |
313 |
< |
try { |
314 |
< |
SynchronousQueue q = new SynchronousQueue(); |
389 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
390 |
< |
} catch (InterruptedException e){ |
391 |
< |
unexpectedException(); |
392 |
< |
} |
312 |
> |
public void testTimedPoll0() throws InterruptedException { |
313 |
> |
SynchronousQueue q = new SynchronousQueue(); |
314 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
315 |
|
} |
316 |
|
|
317 |
|
/** |
318 |
|
* timed pool with nonzero timeout times out if no active taker |
319 |
|
*/ |
320 |
< |
public void testTimedPoll() { |
321 |
< |
try { |
322 |
< |
SynchronousQueue q = new SynchronousQueue(); |
401 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
402 |
< |
} catch (InterruptedException e){ |
403 |
< |
unexpectedException(); |
404 |
< |
} |
320 |
> |
public void testTimedPoll() throws InterruptedException { |
321 |
> |
SynchronousQueue q = new SynchronousQueue(); |
322 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
323 |
|
} |
324 |
|
|
325 |
|
/** |
326 |
|
* Interrupted timed poll throws InterruptedException instead of |
327 |
|
* returning timeout status |
328 |
|
*/ |
329 |
< |
public void testInterruptedTimedPoll() { |
330 |
< |
Thread t = new Thread(new Runnable() { |
331 |
< |
public void run() { |
332 |
< |
try { |
333 |
< |
SynchronousQueue q = new SynchronousQueue(); |
334 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
335 |
< |
} catch (InterruptedException success){ |
418 |
< |
} |
419 |
< |
}}); |
329 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
330 |
> |
final SynchronousQueue q = new SynchronousQueue(); |
331 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
332 |
> |
public void realRun() throws InterruptedException { |
333 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
334 |
> |
}}); |
335 |
> |
|
336 |
|
t.start(); |
337 |
< |
try { |
338 |
< |
Thread.sleep(SHORT_DELAY_MS); |
339 |
< |
t.interrupt(); |
424 |
< |
t.join(); |
425 |
< |
} |
426 |
< |
catch (InterruptedException ie) { |
427 |
< |
unexpectedException(); |
428 |
< |
} |
337 |
> |
Thread.sleep(SHORT_DELAY_MS); |
338 |
> |
t.interrupt(); |
339 |
> |
t.join(); |
340 |
|
} |
341 |
|
|
342 |
|
/** |
343 |
|
* timed poll before a delayed offer fails; after offer succeeds; |
344 |
|
* on interruption throws |
345 |
|
*/ |
346 |
< |
public void testTimedPollWithOffer() { |
346 |
> |
public void testTimedPollWithOffer() throws InterruptedException { |
347 |
|
final SynchronousQueue q = new SynchronousQueue(); |
348 |
< |
Thread t = new Thread(new Runnable() { |
349 |
< |
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 |
< |
try { |
359 |
< |
t.start(); |
360 |
< |
Thread.sleep(SMALL_DELAY_MS); |
361 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
362 |
< |
t.interrupt(); |
452 |
< |
t.join(); |
453 |
< |
} catch (Exception e){ |
454 |
< |
unexpectedException(); |
455 |
< |
} |
348 |
> |
Thread t = new Thread(new CheckedRunnable() { |
349 |
> |
public void realRun() throws InterruptedException { |
350 |
> |
try { |
351 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
352 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
353 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
354 |
> |
shouldThrow(); |
355 |
> |
} catch (InterruptedException success) {} |
356 |
> |
}}); |
357 |
> |
|
358 |
> |
t.start(); |
359 |
> |
Thread.sleep(SMALL_DELAY_MS); |
360 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
361 |
> |
t.interrupt(); |
362 |
> |
t.join(); |
363 |
|
} |
364 |
|
|
365 |
|
/** |
366 |
|
* Interrupted timed poll throws InterruptedException instead of |
367 |
|
* returning timeout status |
368 |
|
*/ |
369 |
< |
public void testFairInterruptedTimedPoll() { |
370 |
< |
Thread t = new Thread(new Runnable() { |
371 |
< |
public void run() { |
372 |
< |
try { |
373 |
< |
SynchronousQueue q = new SynchronousQueue(true); |
374 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
375 |
< |
} catch (InterruptedException success){ |
469 |
< |
} |
470 |
< |
}}); |
369 |
> |
public void testFairInterruptedTimedPoll() throws InterruptedException { |
370 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
371 |
> |
public void realRun() throws InterruptedException { |
372 |
> |
SynchronousQueue q = new SynchronousQueue(true); |
373 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
374 |
> |
}}); |
375 |
> |
|
376 |
|
t.start(); |
377 |
< |
try { |
378 |
< |
Thread.sleep(SHORT_DELAY_MS); |
379 |
< |
t.interrupt(); |
475 |
< |
t.join(); |
476 |
< |
} |
477 |
< |
catch (InterruptedException ie) { |
478 |
< |
unexpectedException(); |
479 |
< |
} |
377 |
> |
Thread.sleep(SHORT_DELAY_MS); |
378 |
> |
t.interrupt(); |
379 |
> |
t.join(); |
380 |
|
} |
381 |
|
|
382 |
|
/** |
383 |
|
* timed poll before a delayed offer fails; after offer succeeds; |
384 |
|
* on interruption throws |
385 |
|
*/ |
386 |
< |
public void testFairTimedPollWithOffer() { |
386 |
> |
public void testFairTimedPollWithOffer() throws InterruptedException { |
387 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
388 |
< |
Thread t = new Thread(new Runnable() { |
389 |
< |
public void run() { |
390 |
< |
try { |
391 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
392 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
393 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
394 |
< |
threadShouldThrow(); |
395 |
< |
} catch (InterruptedException success) { } |
396 |
< |
} |
397 |
< |
}); |
398 |
< |
try { |
399 |
< |
t.start(); |
400 |
< |
Thread.sleep(SMALL_DELAY_MS); |
401 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
402 |
< |
t.interrupt(); |
503 |
< |
t.join(); |
504 |
< |
} catch (Exception e){ |
505 |
< |
unexpectedException(); |
506 |
< |
} |
388 |
> |
Thread t = new Thread(new CheckedRunnable() { |
389 |
> |
public void realRun() throws InterruptedException { |
390 |
> |
try { |
391 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
392 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
393 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
394 |
> |
threadShouldThrow(); |
395 |
> |
} catch (InterruptedException success) {} |
396 |
> |
}}); |
397 |
> |
|
398 |
> |
t.start(); |
399 |
> |
Thread.sleep(SMALL_DELAY_MS); |
400 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
401 |
> |
t.interrupt(); |
402 |
> |
t.join(); |
403 |
|
} |
404 |
|
|
405 |
|
|
408 |
|
*/ |
409 |
|
public void testPeek() { |
410 |
|
SynchronousQueue q = new SynchronousQueue(); |
411 |
< |
assertNull(q.peek()); |
411 |
> |
assertNull(q.peek()); |
412 |
|
} |
413 |
|
|
414 |
|
/** |
419 |
|
try { |
420 |
|
q.element(); |
421 |
|
shouldThrow(); |
422 |
< |
} |
527 |
< |
catch (NoSuchElementException success) {} |
422 |
> |
} catch (NoSuchElementException success) {} |
423 |
|
} |
424 |
|
|
425 |
|
/** |
430 |
|
try { |
431 |
|
q.remove(); |
432 |
|
shouldThrow(); |
433 |
< |
} catch (NoSuchElementException success){ |
539 |
< |
} |
433 |
> |
} catch (NoSuchElementException success) {} |
434 |
|
} |
435 |
|
|
436 |
|
/** |
498 |
|
*/ |
499 |
|
public void testToArray() { |
500 |
|
SynchronousQueue q = new SynchronousQueue(); |
501 |
< |
Object[] o = q.toArray(); |
501 |
> |
Object[] o = q.toArray(); |
502 |
|
assertEquals(o.length, 0); |
503 |
|
} |
504 |
|
|
507 |
|
*/ |
508 |
|
public void testToArray2() { |
509 |
|
SynchronousQueue q = new SynchronousQueue(); |
510 |
< |
Integer[] ints = new Integer[1]; |
510 |
> |
Integer[] ints = new Integer[1]; |
511 |
|
assertNull(ints[0]); |
512 |
|
} |
513 |
|
|
515 |
|
* toArray(null) throws NPE |
516 |
|
*/ |
517 |
|
public void testToArray_BadArg() { |
518 |
< |
try { |
519 |
< |
SynchronousQueue q = new SynchronousQueue(); |
520 |
< |
Object o[] = q.toArray(null); |
521 |
< |
shouldThrow(); |
522 |
< |
} catch (NullPointerException success){} |
518 |
> |
SynchronousQueue q = new SynchronousQueue(); |
519 |
> |
try { |
520 |
> |
Object o[] = q.toArray(null); |
521 |
> |
shouldThrow(); |
522 |
> |
} catch (NullPointerException success) {} |
523 |
|
} |
524 |
|
|
525 |
|
|
528 |
|
*/ |
529 |
|
public void testIterator() { |
530 |
|
SynchronousQueue q = new SynchronousQueue(); |
531 |
< |
Iterator it = q.iterator(); |
531 |
> |
Iterator it = q.iterator(); |
532 |
|
assertFalse(it.hasNext()); |
533 |
|
try { |
534 |
|
Object x = it.next(); |
535 |
|
shouldThrow(); |
536 |
< |
} |
643 |
< |
catch (NoSuchElementException success) {} |
536 |
> |
} catch (NoSuchElementException success) {} |
537 |
|
} |
538 |
|
|
539 |
|
/** |
541 |
|
*/ |
542 |
|
public void testIteratorRemove() { |
543 |
|
SynchronousQueue q = new SynchronousQueue(); |
544 |
< |
Iterator it = q.iterator(); |
544 |
> |
Iterator it = q.iterator(); |
545 |
|
try { |
546 |
|
it.remove(); |
547 |
|
shouldThrow(); |
548 |
< |
} |
656 |
< |
catch (IllegalStateException success) {} |
548 |
> |
} catch (IllegalStateException success) {} |
549 |
|
} |
550 |
|
|
551 |
|
/** |
564 |
|
public void testOfferInExecutor() { |
565 |
|
final SynchronousQueue q = new SynchronousQueue(); |
566 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
675 |
– |
final Integer one = new Integer(1); |
567 |
|
|
568 |
< |
executor.execute(new Runnable() { |
569 |
< |
public void run() { |
570 |
< |
threadAssertFalse(q.offer(one)); |
571 |
< |
try { |
572 |
< |
threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
573 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
574 |
< |
} |
575 |
< |
catch (InterruptedException e) { |
576 |
< |
threadUnexpectedException(); |
577 |
< |
} |
578 |
< |
} |
579 |
< |
}); |
689 |
< |
|
690 |
< |
executor.execute(new Runnable() { |
691 |
< |
public void run() { |
692 |
< |
try { |
693 |
< |
Thread.sleep(SMALL_DELAY_MS); |
694 |
< |
threadAssertEquals(one, q.take()); |
695 |
< |
} |
696 |
< |
catch (InterruptedException e) { |
697 |
< |
threadUnexpectedException(); |
698 |
< |
} |
699 |
< |
} |
700 |
< |
}); |
568 |
> |
executor.execute(new CheckedRunnable() { |
569 |
> |
public void realRun() throws InterruptedException { |
570 |
> |
assertFalse(q.offer(one)); |
571 |
> |
assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); |
572 |
> |
assertEquals(0, q.remainingCapacity()); |
573 |
> |
}}); |
574 |
> |
|
575 |
> |
executor.execute(new CheckedRunnable() { |
576 |
> |
public void realRun() throws InterruptedException { |
577 |
> |
Thread.sleep(SMALL_DELAY_MS); |
578 |
> |
assertSame(one, q.take()); |
579 |
> |
}}); |
580 |
|
|
581 |
|
joinPool(executor); |
703 |
– |
|
582 |
|
} |
583 |
|
|
584 |
|
/** |
587 |
|
public void testPollInExecutor() { |
588 |
|
final SynchronousQueue q = new SynchronousQueue(); |
589 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
590 |
< |
executor.execute(new Runnable() { |
591 |
< |
public void run() { |
592 |
< |
threadAssertNull(q.poll()); |
593 |
< |
try { |
594 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
595 |
< |
threadAssertTrue(q.isEmpty()); |
596 |
< |
} |
597 |
< |
catch (InterruptedException e) { |
598 |
< |
threadUnexpectedException(); |
599 |
< |
} |
600 |
< |
} |
601 |
< |
}); |
724 |
< |
|
725 |
< |
executor.execute(new Runnable() { |
726 |
< |
public void run() { |
727 |
< |
try { |
728 |
< |
Thread.sleep(SMALL_DELAY_MS); |
729 |
< |
q.put(new Integer(1)); |
730 |
< |
} |
731 |
< |
catch (InterruptedException e) { |
732 |
< |
threadUnexpectedException(); |
733 |
< |
} |
734 |
< |
} |
735 |
< |
}); |
590 |
> |
executor.execute(new CheckedRunnable() { |
591 |
> |
public void realRun() throws InterruptedException { |
592 |
> |
assertNull(q.poll()); |
593 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
594 |
> |
assertTrue(q.isEmpty()); |
595 |
> |
}}); |
596 |
> |
|
597 |
> |
executor.execute(new CheckedRunnable() { |
598 |
> |
public void realRun() throws InterruptedException { |
599 |
> |
Thread.sleep(SHORT_DELAY_MS); |
600 |
> |
q.put(one); |
601 |
> |
}}); |
602 |
|
|
603 |
|
joinPool(executor); |
604 |
|
} |
606 |
|
/** |
607 |
|
* a deserialized serialized queue is usable |
608 |
|
*/ |
609 |
< |
public void testSerialization() { |
609 |
> |
public void testSerialization() throws Exception { |
610 |
|
SynchronousQueue q = new SynchronousQueue(); |
611 |
< |
try { |
612 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
613 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
614 |
< |
out.writeObject(q); |
615 |
< |
out.close(); |
616 |
< |
|
617 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
618 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
619 |
< |
SynchronousQueue r = (SynchronousQueue)in.readObject(); |
620 |
< |
assertEquals(q.size(), r.size()); |
621 |
< |
while (!q.isEmpty()) |
756 |
< |
assertEquals(q.remove(), r.remove()); |
757 |
< |
} catch (Exception e){ |
758 |
< |
e.printStackTrace(); |
759 |
< |
unexpectedException(); |
760 |
< |
} |
611 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
612 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
613 |
> |
out.writeObject(q); |
614 |
> |
out.close(); |
615 |
> |
|
616 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
617 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
618 |
> |
SynchronousQueue r = (SynchronousQueue)in.readObject(); |
619 |
> |
assertEquals(q.size(), r.size()); |
620 |
> |
while (!q.isEmpty()) |
621 |
> |
assertEquals(q.remove(), r.remove()); |
622 |
|
} |
623 |
|
|
624 |
|
/** |
629 |
|
try { |
630 |
|
q.drainTo(null); |
631 |
|
shouldThrow(); |
632 |
< |
} catch (NullPointerException success) { |
772 |
< |
} |
632 |
> |
} catch (NullPointerException success) {} |
633 |
|
} |
634 |
|
|
635 |
|
/** |
640 |
|
try { |
641 |
|
q.drainTo(q); |
642 |
|
shouldThrow(); |
643 |
< |
} catch (IllegalArgumentException success) { |
784 |
< |
} |
643 |
> |
} catch (IllegalArgumentException success) {} |
644 |
|
} |
645 |
|
|
646 |
|
/** |
657 |
|
/** |
658 |
|
* drainTo empties queue, unblocking a waiting put. |
659 |
|
*/ |
660 |
< |
public void testDrainToWithActivePut() { |
660 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
661 |
|
final SynchronousQueue q = new SynchronousQueue(); |
662 |
< |
Thread t = new Thread(new Runnable() { |
663 |
< |
public void run() { |
664 |
< |
try { |
665 |
< |
q.put(new Integer(1)); |
666 |
< |
} catch (InterruptedException ie){ |
667 |
< |
threadUnexpectedException(); |
668 |
< |
} |
669 |
< |
} |
670 |
< |
}); |
671 |
< |
try { |
672 |
< |
t.start(); |
673 |
< |
ArrayList l = new ArrayList(); |
674 |
< |
Thread.sleep(SHORT_DELAY_MS); |
675 |
< |
q.drainTo(l); |
817 |
< |
assertTrue(l.size() <= 1); |
818 |
< |
if (l.size() > 0) |
819 |
< |
assertEquals(l.get(0), new Integer(1)); |
820 |
< |
t.join(); |
821 |
< |
assertTrue(l.size() <= 1); |
822 |
< |
} catch (Exception e){ |
823 |
< |
unexpectedException(); |
824 |
< |
} |
662 |
> |
Thread t = new Thread(new CheckedRunnable() { |
663 |
> |
public void realRun() throws InterruptedException { |
664 |
> |
q.put(new Integer(1)); |
665 |
> |
}}); |
666 |
> |
|
667 |
> |
t.start(); |
668 |
> |
ArrayList l = new ArrayList(); |
669 |
> |
Thread.sleep(SHORT_DELAY_MS); |
670 |
> |
q.drainTo(l); |
671 |
> |
assertTrue(l.size() <= 1); |
672 |
> |
if (l.size() > 0) |
673 |
> |
assertEquals(l.get(0), new Integer(1)); |
674 |
> |
t.join(); |
675 |
> |
assertTrue(l.size() <= 1); |
676 |
|
} |
677 |
|
|
678 |
|
/** |
683 |
|
try { |
684 |
|
q.drainTo(null, 0); |
685 |
|
shouldThrow(); |
686 |
< |
} catch (NullPointerException success) { |
836 |
< |
} |
686 |
> |
} catch (NullPointerException success) {} |
687 |
|
} |
688 |
|
|
689 |
|
/** |
694 |
|
try { |
695 |
|
q.drainTo(q, 0); |
696 |
|
shouldThrow(); |
697 |
< |
} catch (IllegalArgumentException success) { |
848 |
< |
} |
697 |
> |
} catch (IllegalArgumentException success) {} |
698 |
|
} |
699 |
|
|
700 |
|
/** |
701 |
|
* drainTo(c, n) empties up to n elements of queue into c |
702 |
|
*/ |
703 |
< |
public void testDrainToN() { |
703 |
> |
public void testDrainToN() throws InterruptedException { |
704 |
|
final SynchronousQueue q = new SynchronousQueue(); |
705 |
< |
Thread t1 = new Thread(new Runnable() { |
706 |
< |
public void run() { |
707 |
< |
try { |
708 |
< |
q.put(one); |
709 |
< |
} catch (InterruptedException ie){ |
710 |
< |
threadUnexpectedException(); |
711 |
< |
} |
712 |
< |
} |
713 |
< |
}); |
865 |
< |
Thread t2 = new Thread(new Runnable() { |
866 |
< |
public void run() { |
867 |
< |
try { |
868 |
< |
q.put(two); |
869 |
< |
} catch (InterruptedException ie){ |
870 |
< |
threadUnexpectedException(); |
871 |
< |
} |
872 |
< |
} |
873 |
< |
}); |
705 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
706 |
> |
public void realRun() throws InterruptedException { |
707 |
> |
q.put(one); |
708 |
> |
}}); |
709 |
> |
|
710 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
711 |
> |
public void realRun() throws InterruptedException { |
712 |
> |
q.put(two); |
713 |
> |
}}); |
714 |
|
|
715 |
< |
try { |
716 |
< |
t1.start(); |
717 |
< |
t2.start(); |
718 |
< |
ArrayList l = new ArrayList(); |
719 |
< |
Thread.sleep(SHORT_DELAY_MS); |
720 |
< |
q.drainTo(l, 1); |
721 |
< |
assertTrue(l.size() == 1); |
722 |
< |
q.drainTo(l, 1); |
723 |
< |
assertTrue(l.size() == 2); |
724 |
< |
assertTrue(l.contains(one)); |
725 |
< |
assertTrue(l.contains(two)); |
726 |
< |
t1.join(); |
887 |
< |
t2.join(); |
888 |
< |
} catch (Exception e){ |
889 |
< |
unexpectedException(); |
890 |
< |
} |
715 |
> |
t1.start(); |
716 |
> |
t2.start(); |
717 |
> |
ArrayList l = new ArrayList(); |
718 |
> |
Thread.sleep(SHORT_DELAY_MS); |
719 |
> |
q.drainTo(l, 1); |
720 |
> |
assertEquals(1, l.size()); |
721 |
> |
q.drainTo(l, 1); |
722 |
> |
assertEquals(2, l.size()); |
723 |
> |
assertTrue(l.contains(one)); |
724 |
> |
assertTrue(l.contains(two)); |
725 |
> |
t1.join(); |
726 |
> |
t2.join(); |
727 |
|
} |
728 |
|
|
893 |
– |
|
729 |
|
} |