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 { |
52 |
|
SynchronousQueue q = new SynchronousQueue(); |
53 |
|
q.offer(null); |
54 |
|
shouldThrow(); |
55 |
< |
} catch (NullPointerException success) { } |
55 |
> |
} catch (NullPointerException success) {} |
56 |
|
} |
57 |
|
|
58 |
|
/** |
63 |
|
SynchronousQueue q = new SynchronousQueue(); |
64 |
|
q.add(null); |
65 |
|
shouldThrow(); |
66 |
< |
} catch (NullPointerException success) { } |
66 |
> |
} catch (NullPointerException success) {} |
67 |
|
} |
68 |
|
|
69 |
|
/** |
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 |
|
* addAll throws ISE if no active taker |
130 |
|
ints[i] = new Integer(i); |
131 |
|
q.addAll(Arrays.asList(ints)); |
132 |
|
shouldThrow(); |
133 |
< |
} |
137 |
< |
catch (IllegalStateException success) {} |
133 |
> |
} catch (IllegalStateException success) {} |
134 |
|
} |
135 |
|
|
136 |
|
/** |
137 |
|
* put(null) throws NPE |
138 |
|
*/ |
139 |
< |
public void testPutNull() { |
139 |
> |
public void testPutNull() throws InterruptedException { |
140 |
|
try { |
141 |
|
SynchronousQueue q = new SynchronousQueue(); |
142 |
|
q.put(null); |
143 |
|
shouldThrow(); |
144 |
< |
} |
149 |
< |
catch (NullPointerException success) { |
150 |
< |
} |
151 |
< |
catch (InterruptedException ie) { |
152 |
< |
unexpectedException(); |
153 |
< |
} |
144 |
> |
} catch (NullPointerException success) {} |
145 |
|
} |
146 |
|
|
147 |
|
/** |
148 |
|
* put blocks interruptibly if no active taker |
149 |
|
*/ |
150 |
< |
public void testBlockingPut() { |
151 |
< |
Thread t = new Thread(new Runnable() { |
152 |
< |
public void run() { |
153 |
< |
try { |
154 |
< |
SynchronousQueue q = new SynchronousQueue(); |
155 |
< |
q.put(zero); |
156 |
< |
threadShouldThrow(); |
166 |
< |
} catch (InterruptedException ie) { |
167 |
< |
} |
168 |
< |
}}); |
150 |
> |
public void testBlockingPut() throws InterruptedException { |
151 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
152 |
> |
public void realRun() throws InterruptedException { |
153 |
> |
SynchronousQueue q = new SynchronousQueue(); |
154 |
> |
q.put(zero); |
155 |
> |
}}); |
156 |
> |
|
157 |
|
t.start(); |
158 |
< |
try { |
159 |
< |
Thread.sleep(SHORT_DELAY_MS); |
160 |
< |
t.interrupt(); |
173 |
< |
t.join(); |
174 |
< |
} |
175 |
< |
catch (InterruptedException ie) { |
176 |
< |
unexpectedException(); |
177 |
< |
} |
158 |
> |
Thread.sleep(SHORT_DELAY_MS); |
159 |
> |
t.interrupt(); |
160 |
> |
t.join(); |
161 |
|
} |
162 |
|
|
163 |
|
/** |
164 |
|
* put blocks waiting for take |
165 |
|
*/ |
166 |
< |
public void testPutWithTake() { |
166 |
> |
public void testPutWithTake() throws InterruptedException { |
167 |
|
final SynchronousQueue q = new SynchronousQueue(); |
168 |
< |
Thread t = new Thread(new Runnable() { |
169 |
< |
public void run() { |
170 |
< |
int added = 0; |
171 |
< |
try { |
172 |
< |
q.put(new Object()); |
173 |
< |
++added; |
174 |
< |
q.put(new Object()); |
175 |
< |
++added; |
176 |
< |
q.put(new Object()); |
177 |
< |
++added; |
178 |
< |
q.put(new Object()); |
179 |
< |
++added; |
180 |
< |
threadShouldThrow(); |
181 |
< |
} catch (InterruptedException e) { |
182 |
< |
assertTrue(added >= 1); |
200 |
< |
} |
168 |
> |
Thread t = new Thread(new CheckedRunnable() { |
169 |
> |
public void realRun() throws InterruptedException { |
170 |
> |
int added = 0; |
171 |
> |
try { |
172 |
> |
q.put(new Object()); |
173 |
> |
++added; |
174 |
> |
q.put(new Object()); |
175 |
> |
++added; |
176 |
> |
q.put(new Object()); |
177 |
> |
++added; |
178 |
> |
q.put(new Object()); |
179 |
> |
++added; |
180 |
> |
threadShouldThrow(); |
181 |
> |
} catch (InterruptedException success) { |
182 |
> |
assertTrue(added >= 1); |
183 |
|
} |
184 |
< |
}); |
185 |
< |
try { |
186 |
< |
t.start(); |
187 |
< |
Thread.sleep(SHORT_DELAY_MS); |
188 |
< |
q.take(); |
189 |
< |
Thread.sleep(SHORT_DELAY_MS); |
190 |
< |
t.interrupt(); |
191 |
< |
t.join(); |
210 |
< |
} catch (Exception e) { |
211 |
< |
unexpectedException(); |
212 |
< |
} |
184 |
> |
}}); |
185 |
> |
|
186 |
> |
t.start(); |
187 |
> |
Thread.sleep(SHORT_DELAY_MS); |
188 |
> |
q.take(); |
189 |
> |
Thread.sleep(SHORT_DELAY_MS); |
190 |
> |
t.interrupt(); |
191 |
> |
t.join(); |
192 |
|
} |
193 |
|
|
194 |
|
/** |
195 |
|
* timed offer times out if elements not taken |
196 |
|
*/ |
197 |
< |
public void testTimedOffer() { |
197 |
> |
public void testTimedOffer() throws InterruptedException { |
198 |
|
final SynchronousQueue q = new SynchronousQueue(); |
199 |
< |
Thread t = new Thread(new Runnable() { |
200 |
< |
public void run() { |
201 |
< |
try { |
202 |
< |
|
203 |
< |
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 |
< |
}); |
199 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
200 |
> |
public void realRun() throws InterruptedException { |
201 |
> |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
202 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
203 |
> |
}}); |
204 |
|
|
205 |
< |
try { |
206 |
< |
t.start(); |
207 |
< |
Thread.sleep(SMALL_DELAY_MS); |
208 |
< |
t.interrupt(); |
235 |
< |
t.join(); |
236 |
< |
} catch (Exception e) { |
237 |
< |
unexpectedException(); |
238 |
< |
} |
205 |
> |
t.start(); |
206 |
> |
Thread.sleep(SMALL_DELAY_MS); |
207 |
> |
t.interrupt(); |
208 |
> |
t.join(); |
209 |
|
} |
210 |
|
|
211 |
|
|
212 |
|
/** |
213 |
|
* take blocks interruptibly when empty |
214 |
|
*/ |
215 |
< |
public void testTakeFromEmpty() { |
215 |
> |
public void testTakeFromEmpty() throws InterruptedException { |
216 |
|
final SynchronousQueue q = new SynchronousQueue(); |
217 |
< |
Thread t = new Thread(new Runnable() { |
218 |
< |
public void run() { |
219 |
< |
try { |
220 |
< |
q.take(); |
221 |
< |
threadShouldThrow(); |
222 |
< |
} catch (InterruptedException success) { } |
223 |
< |
} |
224 |
< |
}); |
225 |
< |
try { |
256 |
< |
t.start(); |
257 |
< |
Thread.sleep(SHORT_DELAY_MS); |
258 |
< |
t.interrupt(); |
259 |
< |
t.join(); |
260 |
< |
} catch (Exception e) { |
261 |
< |
unexpectedException(); |
262 |
< |
} |
217 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
218 |
> |
public void realRun() throws InterruptedException { |
219 |
> |
q.take(); |
220 |
> |
}}); |
221 |
> |
|
222 |
> |
t.start(); |
223 |
> |
Thread.sleep(SHORT_DELAY_MS); |
224 |
> |
t.interrupt(); |
225 |
> |
t.join(); |
226 |
|
} |
227 |
|
|
228 |
|
|
229 |
|
/** |
230 |
|
* put blocks interruptibly if no active taker |
231 |
|
*/ |
232 |
< |
public void testFairBlockingPut() { |
233 |
< |
Thread t = new Thread(new Runnable() { |
234 |
< |
public void run() { |
235 |
< |
try { |
236 |
< |
SynchronousQueue q = new SynchronousQueue(true); |
237 |
< |
q.put(zero); |
238 |
< |
threadShouldThrow(); |
276 |
< |
} catch (InterruptedException ie) { |
277 |
< |
} |
278 |
< |
}}); |
232 |
> |
public void testFairBlockingPut() throws InterruptedException { |
233 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
234 |
> |
public void realRun() throws InterruptedException { |
235 |
> |
SynchronousQueue q = new SynchronousQueue(true); |
236 |
> |
q.put(zero); |
237 |
> |
}}); |
238 |
> |
|
239 |
|
t.start(); |
240 |
< |
try { |
241 |
< |
Thread.sleep(SHORT_DELAY_MS); |
242 |
< |
t.interrupt(); |
283 |
< |
t.join(); |
284 |
< |
} |
285 |
< |
catch (InterruptedException ie) { |
286 |
< |
unexpectedException(); |
287 |
< |
} |
240 |
> |
Thread.sleep(SHORT_DELAY_MS); |
241 |
> |
t.interrupt(); |
242 |
> |
t.join(); |
243 |
|
} |
244 |
|
|
245 |
|
/** |
246 |
|
* put blocks waiting for take |
247 |
|
*/ |
248 |
< |
public void testFairPutWithTake() { |
248 |
> |
public void testFairPutWithTake() throws InterruptedException { |
249 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
250 |
< |
Thread t = new Thread(new Runnable() { |
251 |
< |
public void run() { |
252 |
< |
int added = 0; |
253 |
< |
try { |
254 |
< |
q.put(new Object()); |
255 |
< |
++added; |
256 |
< |
q.put(new Object()); |
257 |
< |
++added; |
258 |
< |
q.put(new Object()); |
259 |
< |
++added; |
260 |
< |
q.put(new Object()); |
261 |
< |
++added; |
262 |
< |
threadShouldThrow(); |
263 |
< |
} catch (InterruptedException e) { |
264 |
< |
assertTrue(added >= 1); |
310 |
< |
} |
250 |
> |
Thread t = new Thread(new CheckedRunnable() { |
251 |
> |
public void realRun() throws InterruptedException { |
252 |
> |
int added = 0; |
253 |
> |
try { |
254 |
> |
q.put(new Object()); |
255 |
> |
++added; |
256 |
> |
q.put(new Object()); |
257 |
> |
++added; |
258 |
> |
q.put(new Object()); |
259 |
> |
++added; |
260 |
> |
q.put(new Object()); |
261 |
> |
++added; |
262 |
> |
threadShouldThrow(); |
263 |
> |
} catch (InterruptedException success) { |
264 |
> |
assertTrue(added >= 1); |
265 |
|
} |
266 |
< |
}); |
267 |
< |
try { |
268 |
< |
t.start(); |
269 |
< |
Thread.sleep(SHORT_DELAY_MS); |
270 |
< |
q.take(); |
271 |
< |
Thread.sleep(SHORT_DELAY_MS); |
272 |
< |
t.interrupt(); |
273 |
< |
t.join(); |
320 |
< |
} catch (Exception e) { |
321 |
< |
unexpectedException(); |
322 |
< |
} |
266 |
> |
}}); |
267 |
> |
|
268 |
> |
t.start(); |
269 |
> |
Thread.sleep(SHORT_DELAY_MS); |
270 |
> |
q.take(); |
271 |
> |
Thread.sleep(SHORT_DELAY_MS); |
272 |
> |
t.interrupt(); |
273 |
> |
t.join(); |
274 |
|
} |
275 |
|
|
276 |
|
/** |
277 |
|
* timed offer times out if elements not taken |
278 |
|
*/ |
279 |
< |
public void testFairTimedOffer() { |
279 |
> |
public void testFairTimedOffer() throws InterruptedException { |
280 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
281 |
< |
Thread t = new Thread(new Runnable() { |
282 |
< |
public void run() { |
283 |
< |
try { |
284 |
< |
|
285 |
< |
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 |
< |
}); |
281 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
282 |
> |
public void realRun() throws InterruptedException { |
283 |
> |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
284 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
285 |
> |
}}); |
286 |
|
|
287 |
< |
try { |
288 |
< |
t.start(); |
289 |
< |
Thread.sleep(SMALL_DELAY_MS); |
290 |
< |
t.interrupt(); |
345 |
< |
t.join(); |
346 |
< |
} catch (Exception e) { |
347 |
< |
unexpectedException(); |
348 |
< |
} |
287 |
> |
t.start(); |
288 |
> |
Thread.sleep(SMALL_DELAY_MS); |
289 |
> |
t.interrupt(); |
290 |
> |
t.join(); |
291 |
|
} |
292 |
|
|
293 |
|
|
294 |
|
/** |
295 |
|
* take blocks interruptibly when empty |
296 |
|
*/ |
297 |
< |
public void testFairTakeFromEmpty() { |
297 |
> |
public void testFairTakeFromEmpty() throws InterruptedException { |
298 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
299 |
< |
Thread t = new Thread(new Runnable() { |
300 |
< |
public void run() { |
301 |
< |
try { |
302 |
< |
q.take(); |
303 |
< |
threadShouldThrow(); |
304 |
< |
} catch (InterruptedException success) { } |
305 |
< |
} |
306 |
< |
}); |
307 |
< |
try { |
366 |
< |
t.start(); |
367 |
< |
Thread.sleep(SHORT_DELAY_MS); |
368 |
< |
t.interrupt(); |
369 |
< |
t.join(); |
370 |
< |
} catch (Exception e) { |
371 |
< |
unexpectedException(); |
372 |
< |
} |
299 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
300 |
> |
public void realRun() throws InterruptedException { |
301 |
> |
q.take(); |
302 |
> |
}}); |
303 |
> |
|
304 |
> |
t.start(); |
305 |
> |
Thread.sleep(SHORT_DELAY_MS); |
306 |
> |
t.interrupt(); |
307 |
> |
t.join(); |
308 |
|
} |
309 |
|
|
310 |
|
/** |
318 |
|
/** |
319 |
|
* timed pool with zero timeout times out if no active taker |
320 |
|
*/ |
321 |
< |
public void testTimedPoll0() { |
322 |
< |
try { |
323 |
< |
SynchronousQueue q = new SynchronousQueue(); |
389 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
390 |
< |
} catch (InterruptedException e) { |
391 |
< |
unexpectedException(); |
392 |
< |
} |
321 |
> |
public void testTimedPoll0() throws InterruptedException { |
322 |
> |
SynchronousQueue q = new SynchronousQueue(); |
323 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
324 |
|
} |
325 |
|
|
326 |
|
/** |
327 |
|
* timed pool with nonzero timeout times out if no active taker |
328 |
|
*/ |
329 |
< |
public void testTimedPoll() { |
330 |
< |
try { |
331 |
< |
SynchronousQueue q = new SynchronousQueue(); |
401 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
402 |
< |
} catch (InterruptedException e) { |
403 |
< |
unexpectedException(); |
404 |
< |
} |
329 |
> |
public void testTimedPoll() throws InterruptedException { |
330 |
> |
SynchronousQueue q = new SynchronousQueue(); |
331 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
332 |
|
} |
333 |
|
|
334 |
|
/** |
335 |
|
* Interrupted timed poll throws InterruptedException instead of |
336 |
|
* returning timeout status |
337 |
|
*/ |
338 |
< |
public void testInterruptedTimedPoll() { |
339 |
< |
Thread t = new Thread(new Runnable() { |
340 |
< |
public void run() { |
341 |
< |
try { |
342 |
< |
SynchronousQueue q = new SynchronousQueue(); |
343 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
344 |
< |
} catch (InterruptedException success) { |
418 |
< |
} |
419 |
< |
}}); |
338 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
339 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
340 |
> |
public void realRun() throws InterruptedException { |
341 |
> |
SynchronousQueue q = new SynchronousQueue(); |
342 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
343 |
> |
}}); |
344 |
> |
|
345 |
|
t.start(); |
346 |
< |
try { |
347 |
< |
Thread.sleep(SHORT_DELAY_MS); |
348 |
< |
t.interrupt(); |
424 |
< |
t.join(); |
425 |
< |
} |
426 |
< |
catch (InterruptedException ie) { |
427 |
< |
unexpectedException(); |
428 |
< |
} |
346 |
> |
Thread.sleep(SHORT_DELAY_MS); |
347 |
> |
t.interrupt(); |
348 |
> |
t.join(); |
349 |
|
} |
350 |
|
|
351 |
|
/** |
352 |
|
* timed poll before a delayed offer fails; after offer succeeds; |
353 |
|
* on interruption throws |
354 |
|
*/ |
355 |
< |
public void testTimedPollWithOffer() { |
355 |
> |
public void testTimedPollWithOffer() throws InterruptedException { |
356 |
|
final SynchronousQueue q = new SynchronousQueue(); |
357 |
< |
Thread t = new Thread(new Runnable() { |
358 |
< |
public void run() { |
359 |
< |
try { |
360 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
361 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
362 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
363 |
< |
threadShouldThrow(); |
364 |
< |
} catch (InterruptedException success) { } |
365 |
< |
} |
366 |
< |
}); |
367 |
< |
try { |
368 |
< |
t.start(); |
369 |
< |
Thread.sleep(SMALL_DELAY_MS); |
370 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
371 |
< |
t.interrupt(); |
452 |
< |
t.join(); |
453 |
< |
} catch (Exception e) { |
454 |
< |
unexpectedException(); |
455 |
< |
} |
357 |
> |
Thread t = new Thread(new CheckedRunnable() { |
358 |
> |
public void realRun() throws InterruptedException { |
359 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
360 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
361 |
> |
try { |
362 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
363 |
> |
threadShouldThrow(); |
364 |
> |
} catch (InterruptedException success) {} |
365 |
> |
}}); |
366 |
> |
|
367 |
> |
t.start(); |
368 |
> |
Thread.sleep(SMALL_DELAY_MS); |
369 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
370 |
> |
t.interrupt(); |
371 |
> |
t.join(); |
372 |
|
} |
373 |
|
|
374 |
|
/** |
375 |
|
* Interrupted timed poll throws InterruptedException instead of |
376 |
|
* returning timeout status |
377 |
|
*/ |
378 |
< |
public void testFairInterruptedTimedPoll() { |
379 |
< |
Thread t = new Thread(new Runnable() { |
380 |
< |
public void run() { |
381 |
< |
try { |
382 |
< |
SynchronousQueue q = new SynchronousQueue(true); |
383 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
384 |
< |
} catch (InterruptedException success) { |
469 |
< |
} |
470 |
< |
}}); |
378 |
> |
public void testFairInterruptedTimedPoll() throws InterruptedException { |
379 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
380 |
> |
public void realRun() throws InterruptedException { |
381 |
> |
SynchronousQueue q = new SynchronousQueue(true); |
382 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
383 |
> |
}}); |
384 |
> |
|
385 |
|
t.start(); |
386 |
< |
try { |
387 |
< |
Thread.sleep(SHORT_DELAY_MS); |
388 |
< |
t.interrupt(); |
475 |
< |
t.join(); |
476 |
< |
} |
477 |
< |
catch (InterruptedException ie) { |
478 |
< |
unexpectedException(); |
479 |
< |
} |
386 |
> |
Thread.sleep(SHORT_DELAY_MS); |
387 |
> |
t.interrupt(); |
388 |
> |
t.join(); |
389 |
|
} |
390 |
|
|
391 |
|
/** |
392 |
|
* timed poll before a delayed offer fails; after offer succeeds; |
393 |
|
* on interruption throws |
394 |
|
*/ |
395 |
< |
public void testFairTimedPollWithOffer() { |
395 |
> |
public void testFairTimedPollWithOffer() throws InterruptedException { |
396 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
397 |
< |
Thread t = new Thread(new Runnable() { |
398 |
< |
public void run() { |
399 |
< |
try { |
400 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
401 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
402 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
403 |
< |
threadShouldThrow(); |
404 |
< |
} catch (InterruptedException success) { } |
405 |
< |
} |
406 |
< |
}); |
407 |
< |
try { |
408 |
< |
t.start(); |
409 |
< |
Thread.sleep(SMALL_DELAY_MS); |
410 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
411 |
< |
t.interrupt(); |
503 |
< |
t.join(); |
504 |
< |
} catch (Exception e) { |
505 |
< |
unexpectedException(); |
506 |
< |
} |
397 |
> |
Thread t = new Thread(new CheckedRunnable() { |
398 |
> |
public void realRun() throws InterruptedException { |
399 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
400 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
401 |
> |
try { |
402 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
403 |
> |
threadShouldThrow(); |
404 |
> |
} catch (InterruptedException success) {} |
405 |
> |
}}); |
406 |
> |
|
407 |
> |
t.start(); |
408 |
> |
Thread.sleep(SMALL_DELAY_MS); |
409 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
410 |
> |
t.interrupt(); |
411 |
> |
t.join(); |
412 |
|
} |
413 |
|
|
414 |
|
|
428 |
|
try { |
429 |
|
q.element(); |
430 |
|
shouldThrow(); |
431 |
< |
} |
527 |
< |
catch (NoSuchElementException success) {} |
431 |
> |
} catch (NoSuchElementException success) {} |
432 |
|
} |
433 |
|
|
434 |
|
/** |
543 |
|
try { |
544 |
|
Object x = it.next(); |
545 |
|
shouldThrow(); |
546 |
< |
} |
643 |
< |
catch (NoSuchElementException success) {} |
546 |
> |
} catch (NoSuchElementException success) {} |
547 |
|
} |
548 |
|
|
549 |
|
/** |
555 |
|
try { |
556 |
|
it.remove(); |
557 |
|
shouldThrow(); |
558 |
< |
} |
656 |
< |
catch (IllegalStateException success) {} |
558 |
> |
} catch (IllegalStateException success) {} |
559 |
|
} |
560 |
|
|
561 |
|
/** |
576 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
577 |
|
final Integer one = new Integer(1); |
578 |
|
|
579 |
< |
executor.execute(new Runnable() { |
580 |
< |
public void run() { |
579 |
> |
executor.execute(new CheckedRunnable() { |
580 |
> |
public void realRun() throws InterruptedException { |
581 |
|
threadAssertFalse(q.offer(one)); |
582 |
< |
try { |
583 |
< |
threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
584 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
585 |
< |
} |
586 |
< |
catch (InterruptedException e) { |
587 |
< |
threadUnexpectedException(); |
588 |
< |
} |
589 |
< |
} |
590 |
< |
}); |
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 |
< |
}); |
582 |
> |
threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); |
583 |
> |
threadAssertEquals(0, q.remainingCapacity()); |
584 |
> |
}}); |
585 |
> |
|
586 |
> |
executor.execute(new CheckedRunnable() { |
587 |
> |
public void realRun() throws InterruptedException { |
588 |
> |
Thread.sleep(SMALL_DELAY_MS); |
589 |
> |
threadAssertEquals(one, q.take()); |
590 |
> |
}}); |
591 |
|
|
592 |
|
joinPool(executor); |
593 |
|
|
599 |
|
public void testPollInExecutor() { |
600 |
|
final SynchronousQueue q = new SynchronousQueue(); |
601 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
602 |
< |
executor.execute(new Runnable() { |
603 |
< |
public void run() { |
602 |
> |
executor.execute(new CheckedRunnable() { |
603 |
> |
public void realRun() throws InterruptedException { |
604 |
|
threadAssertNull(q.poll()); |
605 |
< |
try { |
606 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
607 |
< |
threadAssertTrue(q.isEmpty()); |
608 |
< |
} |
609 |
< |
catch (InterruptedException e) { |
610 |
< |
threadUnexpectedException(); |
611 |
< |
} |
612 |
< |
} |
613 |
< |
}); |
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 |
< |
}); |
605 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
606 |
> |
threadAssertTrue(q.isEmpty()); |
607 |
> |
}}); |
608 |
> |
|
609 |
> |
executor.execute(new CheckedRunnable() { |
610 |
> |
public void realRun() throws InterruptedException { |
611 |
> |
Thread.sleep(SMALL_DELAY_MS); |
612 |
> |
q.put(new Integer(1)); |
613 |
> |
}}); |
614 |
|
|
615 |
|
joinPool(executor); |
616 |
|
} |
618 |
|
/** |
619 |
|
* a deserialized serialized queue is usable |
620 |
|
*/ |
621 |
< |
public void testSerialization() { |
621 |
> |
public void testSerialization() throws Exception { |
622 |
|
SynchronousQueue q = new SynchronousQueue(); |
623 |
< |
try { |
624 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
625 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
626 |
< |
out.writeObject(q); |
627 |
< |
out.close(); |
628 |
< |
|
629 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
630 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
631 |
< |
SynchronousQueue r = (SynchronousQueue)in.readObject(); |
632 |
< |
assertEquals(q.size(), r.size()); |
633 |
< |
while (!q.isEmpty()) |
756 |
< |
assertEquals(q.remove(), r.remove()); |
757 |
< |
} catch (Exception e) { |
758 |
< |
e.printStackTrace(); |
759 |
< |
unexpectedException(); |
760 |
< |
} |
623 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
624 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
625 |
> |
out.writeObject(q); |
626 |
> |
out.close(); |
627 |
> |
|
628 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
629 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
630 |
> |
SynchronousQueue r = (SynchronousQueue)in.readObject(); |
631 |
> |
assertEquals(q.size(), r.size()); |
632 |
> |
while (!q.isEmpty()) |
633 |
> |
assertEquals(q.remove(), r.remove()); |
634 |
|
} |
635 |
|
|
636 |
|
/** |
641 |
|
try { |
642 |
|
q.drainTo(null); |
643 |
|
shouldThrow(); |
644 |
< |
} catch (NullPointerException success) { |
772 |
< |
} |
644 |
> |
} catch (NullPointerException success) {} |
645 |
|
} |
646 |
|
|
647 |
|
/** |
652 |
|
try { |
653 |
|
q.drainTo(q); |
654 |
|
shouldThrow(); |
655 |
< |
} catch (IllegalArgumentException success) { |
784 |
< |
} |
655 |
> |
} catch (IllegalArgumentException success) {} |
656 |
|
} |
657 |
|
|
658 |
|
/** |
669 |
|
/** |
670 |
|
* drainTo empties queue, unblocking a waiting put. |
671 |
|
*/ |
672 |
< |
public void testDrainToWithActivePut() { |
672 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
673 |
|
final SynchronousQueue q = new SynchronousQueue(); |
674 |
< |
Thread t = new Thread(new Runnable() { |
675 |
< |
public void run() { |
676 |
< |
try { |
677 |
< |
q.put(new Integer(1)); |
678 |
< |
} catch (InterruptedException ie) { |
679 |
< |
threadUnexpectedException(); |
680 |
< |
} |
681 |
< |
} |
682 |
< |
}); |
683 |
< |
try { |
684 |
< |
t.start(); |
685 |
< |
ArrayList l = new ArrayList(); |
686 |
< |
Thread.sleep(SHORT_DELAY_MS); |
687 |
< |
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 |
< |
} |
674 |
> |
Thread t = new Thread(new CheckedRunnable() { |
675 |
> |
public void realRun() throws InterruptedException { |
676 |
> |
q.put(new Integer(1)); |
677 |
> |
}}); |
678 |
> |
|
679 |
> |
t.start(); |
680 |
> |
ArrayList l = new ArrayList(); |
681 |
> |
Thread.sleep(SHORT_DELAY_MS); |
682 |
> |
q.drainTo(l); |
683 |
> |
assertTrue(l.size() <= 1); |
684 |
> |
if (l.size() > 0) |
685 |
> |
assertEquals(l.get(0), new Integer(1)); |
686 |
> |
t.join(); |
687 |
> |
assertTrue(l.size() <= 1); |
688 |
|
} |
689 |
|
|
690 |
|
/** |
695 |
|
try { |
696 |
|
q.drainTo(null, 0); |
697 |
|
shouldThrow(); |
698 |
< |
} catch (NullPointerException success) { |
836 |
< |
} |
698 |
> |
} catch (NullPointerException success) {} |
699 |
|
} |
700 |
|
|
701 |
|
/** |
713 |
|
/** |
714 |
|
* drainTo(c, n) empties up to n elements of queue into c |
715 |
|
*/ |
716 |
< |
public void testDrainToN() { |
716 |
> |
public void testDrainToN() throws InterruptedException { |
717 |
|
final SynchronousQueue q = new SynchronousQueue(); |
718 |
< |
Thread t1 = new Thread(new Runnable() { |
719 |
< |
public void run() { |
720 |
< |
try { |
721 |
< |
q.put(one); |
722 |
< |
} catch (InterruptedException ie) { |
723 |
< |
threadUnexpectedException(); |
724 |
< |
} |
725 |
< |
} |
726 |
< |
}); |
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 |
< |
}); |
718 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
719 |
> |
public void realRun() throws InterruptedException { |
720 |
> |
q.put(one); |
721 |
> |
}}); |
722 |
> |
|
723 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
724 |
> |
public void realRun() throws InterruptedException { |
725 |
> |
q.put(two); |
726 |
> |
}}); |
727 |
|
|
728 |
< |
try { |
729 |
< |
t1.start(); |
730 |
< |
t2.start(); |
731 |
< |
ArrayList l = new ArrayList(); |
732 |
< |
Thread.sleep(SHORT_DELAY_MS); |
733 |
< |
q.drainTo(l, 1); |
734 |
< |
assertTrue(l.size() == 1); |
735 |
< |
q.drainTo(l, 1); |
736 |
< |
assertTrue(l.size() == 2); |
737 |
< |
assertTrue(l.contains(one)); |
738 |
< |
assertTrue(l.contains(two)); |
739 |
< |
t1.join(); |
887 |
< |
t2.join(); |
888 |
< |
} catch (Exception e) { |
889 |
< |
unexpectedException(); |
890 |
< |
} |
728 |
> |
t1.start(); |
729 |
> |
t2.start(); |
730 |
> |
ArrayList l = new ArrayList(); |
731 |
> |
Thread.sleep(SHORT_DELAY_MS); |
732 |
> |
q.drainTo(l, 1); |
733 |
> |
assertTrue(l.size() == 1); |
734 |
> |
q.drainTo(l, 1); |
735 |
> |
assertTrue(l.size() == 2); |
736 |
> |
assertTrue(l.contains(one)); |
737 |
> |
assertTrue(l.contains(two)); |
738 |
> |
t1.join(); |
739 |
> |
t2.join(); |
740 |
|
} |
741 |
|
|
893 |
– |
|
742 |
|
} |