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 class Fair extends BlockingQueueTest { |
18 |
+ |
protected BlockingQueue emptyCollection() { |
19 |
+ |
return new SynchronousQueue(true); |
20 |
+ |
} |
21 |
+ |
} |
22 |
+ |
|
23 |
+ |
public static class NonFair extends BlockingQueueTest { |
24 |
+ |
protected BlockingQueue emptyCollection() { |
25 |
+ |
return new SynchronousQueue(false); |
26 |
+ |
} |
27 |
+ |
} |
28 |
+ |
|
29 |
|
public static void main(String[] args) { |
30 |
< |
junit.textui.TestRunner.run (suite()); |
30 |
> |
junit.textui.TestRunner.run(suite()); |
31 |
|
} |
32 |
|
|
33 |
|
public static Test suite() { |
34 |
< |
return new TestSuite(SynchronousQueueTest.class); |
34 |
> |
return newTestSuite(SynchronousQueueTest.class, |
35 |
> |
new Fair().testSuite(), |
36 |
> |
new NonFair().testSuite()); |
37 |
|
} |
38 |
|
|
39 |
|
/** |
42 |
|
public void testEmptyFull() { |
43 |
|
SynchronousQueue q = new SynchronousQueue(); |
44 |
|
assertTrue(q.isEmpty()); |
45 |
< |
assertEquals(0, q.size()); |
45 |
> |
assertEquals(0, q.size()); |
46 |
|
assertEquals(0, q.remainingCapacity()); |
47 |
|
assertFalse(q.offer(zero)); |
48 |
|
} |
53 |
|
public void testFairEmptyFull() { |
54 |
|
SynchronousQueue q = new SynchronousQueue(true); |
55 |
|
assertTrue(q.isEmpty()); |
56 |
< |
assertEquals(0, q.size()); |
56 |
> |
assertEquals(0, q.size()); |
57 |
|
assertEquals(0, q.remainingCapacity()); |
58 |
|
assertFalse(q.offer(zero)); |
59 |
|
} |
62 |
|
* offer(null) throws NPE |
63 |
|
*/ |
64 |
|
public void testOfferNull() { |
65 |
< |
try { |
65 |
> |
try { |
66 |
|
SynchronousQueue q = new SynchronousQueue(); |
67 |
|
q.offer(null); |
68 |
|
shouldThrow(); |
69 |
< |
} catch (NullPointerException success) { } |
69 |
> |
} catch (NullPointerException success) {} |
70 |
|
} |
71 |
|
|
72 |
|
/** |
73 |
|
* add(null) throws NPE |
74 |
|
*/ |
75 |
|
public void testAddNull() { |
76 |
< |
try { |
76 |
> |
try { |
77 |
|
SynchronousQueue q = new SynchronousQueue(); |
78 |
|
q.add(null); |
79 |
|
shouldThrow(); |
80 |
< |
} catch (NullPointerException success) { } |
80 |
> |
} catch (NullPointerException success) {} |
81 |
|
} |
82 |
|
|
83 |
|
/** |
92 |
|
* add throws ISE if no active taker |
93 |
|
*/ |
94 |
|
public void testAdd() { |
95 |
< |
try { |
95 |
> |
try { |
96 |
|
SynchronousQueue q = new SynchronousQueue(); |
97 |
|
assertEquals(0, q.remainingCapacity()); |
98 |
|
q.add(one); |
99 |
|
shouldThrow(); |
100 |
< |
} catch (IllegalStateException success){ |
86 |
< |
} |
100 |
> |
} catch (IllegalStateException success) {} |
101 |
|
} |
102 |
|
|
103 |
|
/** |
108 |
|
SynchronousQueue q = new SynchronousQueue(); |
109 |
|
q.addAll(null); |
110 |
|
shouldThrow(); |
111 |
< |
} |
98 |
< |
catch (NullPointerException success) {} |
111 |
> |
} catch (NullPointerException success) {} |
112 |
|
} |
113 |
|
|
114 |
|
/** |
119 |
|
SynchronousQueue q = new SynchronousQueue(); |
120 |
|
q.addAll(q); |
121 |
|
shouldThrow(); |
122 |
< |
} |
110 |
< |
catch (IllegalArgumentException success) {} |
122 |
> |
} catch (IllegalArgumentException success) {} |
123 |
|
} |
124 |
|
|
125 |
|
/** |
131 |
|
Integer[] ints = new Integer[1]; |
132 |
|
q.addAll(Arrays.asList(ints)); |
133 |
|
shouldThrow(); |
134 |
< |
} |
123 |
< |
catch (NullPointerException success) {} |
134 |
> |
} catch (NullPointerException success) {} |
135 |
|
} |
136 |
+ |
|
137 |
|
/** |
138 |
|
* addAll throws ISE if no active taker |
139 |
|
*/ |
145 |
|
ints[i] = new Integer(i); |
146 |
|
q.addAll(Arrays.asList(ints)); |
147 |
|
shouldThrow(); |
148 |
< |
} |
137 |
< |
catch (IllegalStateException success) {} |
148 |
> |
} catch (IllegalStateException success) {} |
149 |
|
} |
150 |
|
|
151 |
|
/** |
152 |
|
* put(null) throws NPE |
153 |
|
*/ |
154 |
< |
public void testPutNull() { |
155 |
< |
try { |
154 |
> |
public void testPutNull() throws InterruptedException { |
155 |
> |
try { |
156 |
|
SynchronousQueue q = new SynchronousQueue(); |
157 |
|
q.put(null); |
158 |
|
shouldThrow(); |
159 |
< |
} |
149 |
< |
catch (NullPointerException success){ |
150 |
< |
} |
151 |
< |
catch (InterruptedException ie) { |
152 |
< |
unexpectedException(); |
153 |
< |
} |
159 |
> |
} catch (NullPointerException success) {} |
160 |
|
} |
161 |
|
|
162 |
|
/** |
163 |
|
* put blocks interruptibly if no active taker |
164 |
|
*/ |
165 |
< |
public void testBlockingPut() { |
166 |
< |
Thread t = new Thread(new Runnable() { |
167 |
< |
public void run() { |
168 |
< |
try { |
169 |
< |
SynchronousQueue q = new SynchronousQueue(); |
170 |
< |
q.put(zero); |
171 |
< |
threadShouldThrow(); |
166 |
< |
} catch (InterruptedException ie){ |
167 |
< |
} |
168 |
< |
}}); |
165 |
> |
public void testBlockingPut() throws InterruptedException { |
166 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
167 |
> |
public void realRun() throws InterruptedException { |
168 |
> |
SynchronousQueue q = new SynchronousQueue(); |
169 |
> |
q.put(zero); |
170 |
> |
}}); |
171 |
> |
|
172 |
|
t.start(); |
173 |
< |
try { |
174 |
< |
Thread.sleep(SHORT_DELAY_MS); |
175 |
< |
t.interrupt(); |
173 |
< |
t.join(); |
174 |
< |
} |
175 |
< |
catch (InterruptedException ie) { |
176 |
< |
unexpectedException(); |
177 |
< |
} |
173 |
> |
Thread.sleep(SHORT_DELAY_MS); |
174 |
> |
t.interrupt(); |
175 |
> |
t.join(); |
176 |
|
} |
177 |
|
|
178 |
|
/** |
179 |
|
* put blocks waiting for take |
180 |
|
*/ |
181 |
< |
public void testPutWithTake() { |
181 |
> |
public void testPutWithTake() throws InterruptedException { |
182 |
|
final SynchronousQueue q = new SynchronousQueue(); |
183 |
< |
Thread t = new Thread(new Runnable() { |
184 |
< |
public void run() { |
185 |
< |
int added = 0; |
186 |
< |
try { |
187 |
< |
q.put(new Object()); |
188 |
< |
++added; |
191 |
< |
q.put(new Object()); |
192 |
< |
++added; |
193 |
< |
q.put(new Object()); |
194 |
< |
++added; |
195 |
< |
q.put(new Object()); |
183 |
> |
Thread t = new Thread(new CheckedRunnable() { |
184 |
> |
public void realRun() throws InterruptedException { |
185 |
> |
int added = 0; |
186 |
> |
try { |
187 |
> |
while (true) { |
188 |
> |
q.put(added); |
189 |
|
++added; |
197 |
– |
threadShouldThrow(); |
198 |
– |
} catch (InterruptedException e){ |
199 |
– |
assertTrue(added >= 1); |
190 |
|
} |
191 |
+ |
} catch (InterruptedException success) { |
192 |
+ |
assertEquals(1, added); |
193 |
|
} |
194 |
< |
}); |
195 |
< |
try { |
196 |
< |
t.start(); |
197 |
< |
Thread.sleep(SHORT_DELAY_MS); |
198 |
< |
q.take(); |
199 |
< |
Thread.sleep(SHORT_DELAY_MS); |
200 |
< |
t.interrupt(); |
201 |
< |
t.join(); |
210 |
< |
} catch (Exception e){ |
211 |
< |
unexpectedException(); |
212 |
< |
} |
194 |
> |
}}); |
195 |
> |
|
196 |
> |
t.start(); |
197 |
> |
Thread.sleep(SHORT_DELAY_MS); |
198 |
> |
assertEquals(0, q.take()); |
199 |
> |
Thread.sleep(SHORT_DELAY_MS); |
200 |
> |
t.interrupt(); |
201 |
> |
t.join(); |
202 |
|
} |
203 |
|
|
204 |
|
/** |
205 |
|
* timed offer times out if elements not taken |
206 |
|
*/ |
207 |
< |
public void testTimedOffer() { |
207 |
> |
public void testTimedOffer() throws InterruptedException { |
208 |
|
final SynchronousQueue q = new SynchronousQueue(); |
209 |
< |
Thread t = new Thread(new Runnable() { |
210 |
< |
public void run() { |
211 |
< |
try { |
212 |
< |
|
213 |
< |
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 |
< |
}); |
230 |
< |
|
231 |
< |
try { |
232 |
< |
t.start(); |
233 |
< |
Thread.sleep(SMALL_DELAY_MS); |
234 |
< |
t.interrupt(); |
235 |
< |
t.join(); |
236 |
< |
} catch (Exception e){ |
237 |
< |
unexpectedException(); |
238 |
< |
} |
239 |
< |
} |
240 |
< |
|
209 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
210 |
> |
public void realRun() throws InterruptedException { |
211 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
212 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
213 |
> |
}}); |
214 |
|
|
215 |
< |
/** |
216 |
< |
* take blocks interruptibly when empty |
217 |
< |
*/ |
218 |
< |
public void testTakeFromEmpty() { |
246 |
< |
final SynchronousQueue q = new SynchronousQueue(); |
247 |
< |
Thread t = new Thread(new Runnable() { |
248 |
< |
public void run() { |
249 |
< |
try { |
250 |
< |
q.take(); |
251 |
< |
threadShouldThrow(); |
252 |
< |
} catch (InterruptedException success){ } |
253 |
< |
} |
254 |
< |
}); |
255 |
< |
try { |
256 |
< |
t.start(); |
257 |
< |
Thread.sleep(SHORT_DELAY_MS); |
258 |
< |
t.interrupt(); |
259 |
< |
t.join(); |
260 |
< |
} catch (Exception e){ |
261 |
< |
unexpectedException(); |
262 |
< |
} |
215 |
> |
t.start(); |
216 |
> |
Thread.sleep(SMALL_DELAY_MS); |
217 |
> |
t.interrupt(); |
218 |
> |
t.join(); |
219 |
|
} |
220 |
|
|
265 |
– |
|
221 |
|
/** |
222 |
|
* put blocks interruptibly if no active taker |
223 |
|
*/ |
224 |
< |
public void testFairBlockingPut() { |
225 |
< |
Thread t = new Thread(new Runnable() { |
226 |
< |
public void run() { |
227 |
< |
try { |
228 |
< |
SynchronousQueue q = new SynchronousQueue(true); |
229 |
< |
q.put(zero); |
230 |
< |
threadShouldThrow(); |
276 |
< |
} catch (InterruptedException ie){ |
277 |
< |
} |
278 |
< |
}}); |
224 |
> |
public void testFairBlockingPut() throws InterruptedException { |
225 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
226 |
> |
public void realRun() throws InterruptedException { |
227 |
> |
SynchronousQueue q = new SynchronousQueue(true); |
228 |
> |
q.put(zero); |
229 |
> |
}}); |
230 |
> |
|
231 |
|
t.start(); |
232 |
< |
try { |
233 |
< |
Thread.sleep(SHORT_DELAY_MS); |
234 |
< |
t.interrupt(); |
283 |
< |
t.join(); |
284 |
< |
} |
285 |
< |
catch (InterruptedException ie) { |
286 |
< |
unexpectedException(); |
287 |
< |
} |
232 |
> |
Thread.sleep(SHORT_DELAY_MS); |
233 |
> |
t.interrupt(); |
234 |
> |
t.join(); |
235 |
|
} |
236 |
|
|
237 |
|
/** |
238 |
|
* put blocks waiting for take |
239 |
|
*/ |
240 |
< |
public void testFairPutWithTake() { |
240 |
> |
public void testFairPutWithTake() throws InterruptedException { |
241 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
242 |
< |
Thread t = new Thread(new Runnable() { |
243 |
< |
public void run() { |
244 |
< |
int added = 0; |
245 |
< |
try { |
246 |
< |
q.put(new Object()); |
247 |
< |
++added; |
301 |
< |
q.put(new Object()); |
302 |
< |
++added; |
303 |
< |
q.put(new Object()); |
304 |
< |
++added; |
305 |
< |
q.put(new Object()); |
242 |
> |
Thread t = new Thread(new CheckedRunnable() { |
243 |
> |
public void realRun() throws InterruptedException { |
244 |
> |
int added = 0; |
245 |
> |
try { |
246 |
> |
while (true) { |
247 |
> |
q.put(added); |
248 |
|
++added; |
307 |
– |
threadShouldThrow(); |
308 |
– |
} catch (InterruptedException e){ |
309 |
– |
assertTrue(added >= 1); |
249 |
|
} |
250 |
+ |
} catch (InterruptedException success) { |
251 |
+ |
assertEquals(1, added); |
252 |
|
} |
253 |
< |
}); |
254 |
< |
try { |
255 |
< |
t.start(); |
256 |
< |
Thread.sleep(SHORT_DELAY_MS); |
257 |
< |
q.take(); |
258 |
< |
Thread.sleep(SHORT_DELAY_MS); |
259 |
< |
t.interrupt(); |
260 |
< |
t.join(); |
320 |
< |
} catch (Exception e){ |
321 |
< |
unexpectedException(); |
322 |
< |
} |
253 |
> |
}}); |
254 |
> |
|
255 |
> |
t.start(); |
256 |
> |
Thread.sleep(SHORT_DELAY_MS); |
257 |
> |
assertEquals(0, q.take()); |
258 |
> |
Thread.sleep(SHORT_DELAY_MS); |
259 |
> |
t.interrupt(); |
260 |
> |
t.join(); |
261 |
|
} |
262 |
|
|
263 |
|
/** |
264 |
|
* timed offer times out if elements not taken |
265 |
|
*/ |
266 |
< |
public void testFairTimedOffer() { |
266 |
> |
public void testFairTimedOffer() throws InterruptedException { |
267 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
268 |
< |
Thread t = new Thread(new Runnable() { |
269 |
< |
public void run() { |
270 |
< |
try { |
271 |
< |
|
272 |
< |
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 |
< |
}); |
268 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
269 |
> |
public void realRun() throws InterruptedException { |
270 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
271 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
272 |
> |
}}); |
273 |
|
|
274 |
< |
try { |
275 |
< |
t.start(); |
276 |
< |
Thread.sleep(SMALL_DELAY_MS); |
277 |
< |
t.interrupt(); |
345 |
< |
t.join(); |
346 |
< |
} catch (Exception e){ |
347 |
< |
unexpectedException(); |
348 |
< |
} |
274 |
> |
t.start(); |
275 |
> |
Thread.sleep(SMALL_DELAY_MS); |
276 |
> |
t.interrupt(); |
277 |
> |
t.join(); |
278 |
|
} |
279 |
|
|
280 |
|
|
281 |
|
/** |
282 |
|
* take blocks interruptibly when empty |
283 |
|
*/ |
284 |
< |
public void testFairTakeFromEmpty() { |
284 |
> |
public void testFairTakeFromEmpty() throws InterruptedException { |
285 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
286 |
< |
Thread t = new Thread(new Runnable() { |
287 |
< |
public void run() { |
288 |
< |
try { |
289 |
< |
q.take(); |
290 |
< |
threadShouldThrow(); |
291 |
< |
} catch (InterruptedException success){ } |
292 |
< |
} |
293 |
< |
}); |
294 |
< |
try { |
366 |
< |
t.start(); |
367 |
< |
Thread.sleep(SHORT_DELAY_MS); |
368 |
< |
t.interrupt(); |
369 |
< |
t.join(); |
370 |
< |
} catch (Exception e){ |
371 |
< |
unexpectedException(); |
372 |
< |
} |
286 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
287 |
> |
public void realRun() throws InterruptedException { |
288 |
> |
q.take(); |
289 |
> |
}}); |
290 |
> |
|
291 |
> |
t.start(); |
292 |
> |
Thread.sleep(SHORT_DELAY_MS); |
293 |
> |
t.interrupt(); |
294 |
> |
t.join(); |
295 |
|
} |
296 |
|
|
297 |
|
/** |
299 |
|
*/ |
300 |
|
public void testPoll() { |
301 |
|
SynchronousQueue q = new SynchronousQueue(); |
302 |
< |
assertNull(q.poll()); |
302 |
> |
assertNull(q.poll()); |
303 |
|
} |
304 |
|
|
305 |
|
/** |
306 |
|
* timed pool with zero timeout times out if no active taker |
307 |
|
*/ |
308 |
< |
public void testTimedPoll0() { |
309 |
< |
try { |
310 |
< |
SynchronousQueue q = new SynchronousQueue(); |
389 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
390 |
< |
} catch (InterruptedException e){ |
391 |
< |
unexpectedException(); |
392 |
< |
} |
308 |
> |
public void testTimedPoll0() throws InterruptedException { |
309 |
> |
SynchronousQueue q = new SynchronousQueue(); |
310 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
311 |
|
} |
312 |
|
|
313 |
|
/** |
314 |
|
* timed pool with nonzero timeout times out if no active taker |
315 |
|
*/ |
316 |
< |
public void testTimedPoll() { |
317 |
< |
try { |
318 |
< |
SynchronousQueue q = new SynchronousQueue(); |
401 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
402 |
< |
} catch (InterruptedException e){ |
403 |
< |
unexpectedException(); |
404 |
< |
} |
316 |
> |
public void testTimedPoll() throws InterruptedException { |
317 |
> |
SynchronousQueue q = new SynchronousQueue(); |
318 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
319 |
|
} |
320 |
|
|
321 |
|
/** |
322 |
|
* Interrupted timed poll throws InterruptedException instead of |
323 |
|
* returning timeout status |
324 |
|
*/ |
325 |
< |
public void testInterruptedTimedPoll() { |
412 |
< |
Thread t = new Thread(new Runnable() { |
413 |
< |
public void run() { |
414 |
< |
try { |
415 |
< |
SynchronousQueue q = new SynchronousQueue(); |
416 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
417 |
< |
} catch (InterruptedException success){ |
418 |
< |
} |
419 |
< |
}}); |
420 |
< |
t.start(); |
421 |
< |
try { |
422 |
< |
Thread.sleep(SHORT_DELAY_MS); |
423 |
< |
t.interrupt(); |
424 |
< |
t.join(); |
425 |
< |
} |
426 |
< |
catch (InterruptedException ie) { |
427 |
< |
unexpectedException(); |
428 |
< |
} |
429 |
< |
} |
430 |
< |
|
431 |
< |
/** |
432 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
433 |
< |
* on interruption throws |
434 |
< |
*/ |
435 |
< |
public void testTimedPollWithOffer() { |
325 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
326 |
|
final SynchronousQueue q = new SynchronousQueue(); |
327 |
< |
Thread t = new Thread(new Runnable() { |
328 |
< |
public void run() { |
329 |
< |
try { |
330 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
331 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
332 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
333 |
< |
threadShouldThrow(); |
334 |
< |
} catch (InterruptedException success) { } |
335 |
< |
} |
446 |
< |
}); |
447 |
< |
try { |
448 |
< |
t.start(); |
449 |
< |
Thread.sleep(SMALL_DELAY_MS); |
450 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
451 |
< |
t.interrupt(); |
452 |
< |
t.join(); |
453 |
< |
} catch (Exception e){ |
454 |
< |
unexpectedException(); |
455 |
< |
} |
327 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
328 |
> |
public void realRun() throws InterruptedException { |
329 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
330 |
> |
}}); |
331 |
> |
|
332 |
> |
t.start(); |
333 |
> |
Thread.sleep(SHORT_DELAY_MS); |
334 |
> |
t.interrupt(); |
335 |
> |
t.join(); |
336 |
|
} |
337 |
|
|
338 |
|
/** |
339 |
|
* Interrupted timed poll throws InterruptedException instead of |
340 |
|
* returning timeout status |
341 |
|
*/ |
342 |
< |
public void testFairInterruptedTimedPoll() { |
343 |
< |
Thread t = new Thread(new Runnable() { |
344 |
< |
public void run() { |
345 |
< |
try { |
346 |
< |
SynchronousQueue q = new SynchronousQueue(true); |
347 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
348 |
< |
} catch (InterruptedException success){ |
469 |
< |
} |
470 |
< |
}}); |
342 |
> |
public void testFairInterruptedTimedPoll() throws InterruptedException { |
343 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
344 |
> |
public void realRun() throws InterruptedException { |
345 |
> |
SynchronousQueue q = new SynchronousQueue(true); |
346 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
347 |
> |
}}); |
348 |
> |
|
349 |
|
t.start(); |
350 |
< |
try { |
351 |
< |
Thread.sleep(SHORT_DELAY_MS); |
352 |
< |
t.interrupt(); |
475 |
< |
t.join(); |
476 |
< |
} |
477 |
< |
catch (InterruptedException ie) { |
478 |
< |
unexpectedException(); |
479 |
< |
} |
350 |
> |
Thread.sleep(SHORT_DELAY_MS); |
351 |
> |
t.interrupt(); |
352 |
> |
t.join(); |
353 |
|
} |
354 |
|
|
355 |
|
/** |
356 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
357 |
< |
* on interruption throws |
356 |
> |
* timed poll before a delayed offer fails; after offer succeeds; |
357 |
> |
* on interruption throws |
358 |
|
*/ |
359 |
< |
public void testFairTimedPollWithOffer() { |
359 |
> |
public void testFairTimedPollWithOffer() throws InterruptedException { |
360 |
|
final SynchronousQueue q = new SynchronousQueue(true); |
361 |
< |
Thread t = new Thread(new Runnable() { |
362 |
< |
public void run() { |
363 |
< |
try { |
364 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
365 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
366 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
367 |
< |
threadShouldThrow(); |
368 |
< |
} catch (InterruptedException success) { } |
369 |
< |
} |
370 |
< |
}); |
371 |
< |
try { |
372 |
< |
t.start(); |
373 |
< |
Thread.sleep(SMALL_DELAY_MS); |
374 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
375 |
< |
t.interrupt(); |
503 |
< |
t.join(); |
504 |
< |
} catch (Exception e){ |
505 |
< |
unexpectedException(); |
506 |
< |
} |
361 |
> |
Thread t = new Thread(new CheckedRunnable() { |
362 |
> |
public void realRun() throws InterruptedException { |
363 |
> |
try { |
364 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
365 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
366 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
367 |
> |
threadShouldThrow(); |
368 |
> |
} catch (InterruptedException success) {} |
369 |
> |
}}); |
370 |
> |
|
371 |
> |
t.start(); |
372 |
> |
Thread.sleep(SMALL_DELAY_MS); |
373 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
374 |
> |
t.interrupt(); |
375 |
> |
t.join(); |
376 |
|
} |
377 |
|
|
378 |
|
|
381 |
|
*/ |
382 |
|
public void testPeek() { |
383 |
|
SynchronousQueue q = new SynchronousQueue(); |
384 |
< |
assertNull(q.peek()); |
384 |
> |
assertNull(q.peek()); |
385 |
|
} |
386 |
|
|
387 |
|
/** |
392 |
|
try { |
393 |
|
q.element(); |
394 |
|
shouldThrow(); |
395 |
< |
} |
527 |
< |
catch (NoSuchElementException success) {} |
395 |
> |
} catch (NoSuchElementException success) {} |
396 |
|
} |
397 |
|
|
398 |
|
/** |
403 |
|
try { |
404 |
|
q.remove(); |
405 |
|
shouldThrow(); |
406 |
< |
} catch (NoSuchElementException success){ |
539 |
< |
} |
406 |
> |
} catch (NoSuchElementException success) {} |
407 |
|
} |
408 |
|
|
409 |
|
/** |
471 |
|
*/ |
472 |
|
public void testToArray() { |
473 |
|
SynchronousQueue q = new SynchronousQueue(); |
474 |
< |
Object[] o = q.toArray(); |
474 |
> |
Object[] o = q.toArray(); |
475 |
|
assertEquals(o.length, 0); |
476 |
|
} |
477 |
|
|
480 |
|
*/ |
481 |
|
public void testToArray2() { |
482 |
|
SynchronousQueue q = new SynchronousQueue(); |
483 |
< |
Integer[] ints = new Integer[1]; |
483 |
> |
Integer[] ints = new Integer[1]; |
484 |
|
assertNull(ints[0]); |
485 |
|
} |
486 |
|
|
488 |
|
* toArray(null) throws NPE |
489 |
|
*/ |
490 |
|
public void testToArray_BadArg() { |
491 |
< |
try { |
492 |
< |
SynchronousQueue q = new SynchronousQueue(); |
493 |
< |
Object o[] = q.toArray(null); |
494 |
< |
shouldThrow(); |
495 |
< |
} catch (NullPointerException success){} |
491 |
> |
SynchronousQueue q = new SynchronousQueue(); |
492 |
> |
try { |
493 |
> |
Object o[] = q.toArray(null); |
494 |
> |
shouldThrow(); |
495 |
> |
} catch (NullPointerException success) {} |
496 |
|
} |
497 |
|
|
498 |
|
|
501 |
|
*/ |
502 |
|
public void testIterator() { |
503 |
|
SynchronousQueue q = new SynchronousQueue(); |
504 |
< |
Iterator it = q.iterator(); |
504 |
> |
Iterator it = q.iterator(); |
505 |
|
assertFalse(it.hasNext()); |
506 |
|
try { |
507 |
|
Object x = it.next(); |
508 |
|
shouldThrow(); |
509 |
< |
} |
643 |
< |
catch (NoSuchElementException success) {} |
509 |
> |
} catch (NoSuchElementException success) {} |
510 |
|
} |
511 |
|
|
512 |
|
/** |
514 |
|
*/ |
515 |
|
public void testIteratorRemove() { |
516 |
|
SynchronousQueue q = new SynchronousQueue(); |
517 |
< |
Iterator it = q.iterator(); |
517 |
> |
Iterator it = q.iterator(); |
518 |
|
try { |
519 |
|
it.remove(); |
520 |
|
shouldThrow(); |
521 |
< |
} |
656 |
< |
catch (IllegalStateException success) {} |
521 |
> |
} catch (IllegalStateException success) {} |
522 |
|
} |
523 |
|
|
524 |
|
/** |
537 |
|
public void testOfferInExecutor() { |
538 |
|
final SynchronousQueue q = new SynchronousQueue(); |
539 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
675 |
– |
final Integer one = new Integer(1); |
540 |
|
|
541 |
< |
executor.execute(new Runnable() { |
542 |
< |
public void run() { |
543 |
< |
threadAssertFalse(q.offer(one)); |
544 |
< |
try { |
545 |
< |
threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
546 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
547 |
< |
} |
548 |
< |
catch (InterruptedException e) { |
549 |
< |
threadUnexpectedException(); |
550 |
< |
} |
551 |
< |
} |
552 |
< |
}); |
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 |
< |
}); |
541 |
> |
executor.execute(new CheckedRunnable() { |
542 |
> |
public void realRun() throws InterruptedException { |
543 |
> |
assertFalse(q.offer(one)); |
544 |
> |
assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); |
545 |
> |
assertEquals(0, q.remainingCapacity()); |
546 |
> |
}}); |
547 |
> |
|
548 |
> |
executor.execute(new CheckedRunnable() { |
549 |
> |
public void realRun() throws InterruptedException { |
550 |
> |
Thread.sleep(SMALL_DELAY_MS); |
551 |
> |
assertSame(one, q.take()); |
552 |
> |
}}); |
553 |
|
|
554 |
|
joinPool(executor); |
703 |
– |
|
555 |
|
} |
556 |
|
|
557 |
|
/** |
560 |
|
public void testPollInExecutor() { |
561 |
|
final SynchronousQueue q = new SynchronousQueue(); |
562 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
563 |
< |
executor.execute(new Runnable() { |
564 |
< |
public void run() { |
565 |
< |
threadAssertNull(q.poll()); |
566 |
< |
try { |
567 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
568 |
< |
threadAssertTrue(q.isEmpty()); |
569 |
< |
} |
570 |
< |
catch (InterruptedException e) { |
571 |
< |
threadUnexpectedException(); |
572 |
< |
} |
573 |
< |
} |
574 |
< |
}); |
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 |
< |
}); |
563 |
> |
executor.execute(new CheckedRunnable() { |
564 |
> |
public void realRun() throws InterruptedException { |
565 |
> |
assertNull(q.poll()); |
566 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
567 |
> |
assertTrue(q.isEmpty()); |
568 |
> |
}}); |
569 |
> |
|
570 |
> |
executor.execute(new CheckedRunnable() { |
571 |
> |
public void realRun() throws InterruptedException { |
572 |
> |
Thread.sleep(SHORT_DELAY_MS); |
573 |
> |
q.put(one); |
574 |
> |
}}); |
575 |
|
|
576 |
|
joinPool(executor); |
577 |
|
} |
579 |
|
/** |
580 |
|
* a deserialized serialized queue is usable |
581 |
|
*/ |
582 |
< |
public void testSerialization() { |
582 |
> |
public void testSerialization() throws Exception { |
583 |
|
SynchronousQueue q = new SynchronousQueue(); |
584 |
< |
try { |
585 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
586 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
587 |
< |
out.writeObject(q); |
588 |
< |
out.close(); |
589 |
< |
|
590 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
591 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
592 |
< |
SynchronousQueue r = (SynchronousQueue)in.readObject(); |
593 |
< |
assertEquals(q.size(), r.size()); |
594 |
< |
while (!q.isEmpty()) |
756 |
< |
assertEquals(q.remove(), r.remove()); |
757 |
< |
} catch (Exception e){ |
758 |
< |
e.printStackTrace(); |
759 |
< |
unexpectedException(); |
760 |
< |
} |
584 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
585 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
586 |
> |
out.writeObject(q); |
587 |
> |
out.close(); |
588 |
> |
|
589 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
590 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
591 |
> |
SynchronousQueue r = (SynchronousQueue)in.readObject(); |
592 |
> |
assertEquals(q.size(), r.size()); |
593 |
> |
while (!q.isEmpty()) |
594 |
> |
assertEquals(q.remove(), r.remove()); |
595 |
|
} |
596 |
|
|
597 |
|
/** |
602 |
|
try { |
603 |
|
q.drainTo(null); |
604 |
|
shouldThrow(); |
605 |
< |
} catch (NullPointerException success) { |
772 |
< |
} |
605 |
> |
} catch (NullPointerException success) {} |
606 |
|
} |
607 |
|
|
608 |
|
/** |
613 |
|
try { |
614 |
|
q.drainTo(q); |
615 |
|
shouldThrow(); |
616 |
< |
} catch (IllegalArgumentException success) { |
784 |
< |
} |
616 |
> |
} catch (IllegalArgumentException success) {} |
617 |
|
} |
618 |
|
|
619 |
|
/** |
630 |
|
/** |
631 |
|
* drainTo empties queue, unblocking a waiting put. |
632 |
|
*/ |
633 |
< |
public void testDrainToWithActivePut() { |
633 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
634 |
|
final SynchronousQueue q = new SynchronousQueue(); |
635 |
< |
Thread t = new Thread(new Runnable() { |
636 |
< |
public void run() { |
637 |
< |
try { |
638 |
< |
q.put(new Integer(1)); |
639 |
< |
} catch (InterruptedException ie){ |
640 |
< |
threadUnexpectedException(); |
641 |
< |
} |
642 |
< |
} |
643 |
< |
}); |
644 |
< |
try { |
645 |
< |
t.start(); |
646 |
< |
ArrayList l = new ArrayList(); |
647 |
< |
Thread.sleep(SHORT_DELAY_MS); |
648 |
< |
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 |
< |
} |
635 |
> |
Thread t = new Thread(new CheckedRunnable() { |
636 |
> |
public void realRun() throws InterruptedException { |
637 |
> |
q.put(new Integer(1)); |
638 |
> |
}}); |
639 |
> |
|
640 |
> |
t.start(); |
641 |
> |
ArrayList l = new ArrayList(); |
642 |
> |
Thread.sleep(SHORT_DELAY_MS); |
643 |
> |
q.drainTo(l); |
644 |
> |
assertTrue(l.size() <= 1); |
645 |
> |
if (l.size() > 0) |
646 |
> |
assertEquals(l.get(0), new Integer(1)); |
647 |
> |
t.join(); |
648 |
> |
assertTrue(l.size() <= 1); |
649 |
|
} |
650 |
|
|
651 |
|
/** |
656 |
|
try { |
657 |
|
q.drainTo(null, 0); |
658 |
|
shouldThrow(); |
659 |
< |
} catch (NullPointerException success) { |
836 |
< |
} |
659 |
> |
} catch (NullPointerException success) {} |
660 |
|
} |
661 |
|
|
662 |
|
/** |
667 |
|
try { |
668 |
|
q.drainTo(q, 0); |
669 |
|
shouldThrow(); |
670 |
< |
} catch (IllegalArgumentException success) { |
848 |
< |
} |
670 |
> |
} catch (IllegalArgumentException success) {} |
671 |
|
} |
672 |
|
|
673 |
|
/** |
674 |
|
* drainTo(c, n) empties up to n elements of queue into c |
675 |
|
*/ |
676 |
< |
public void testDrainToN() { |
676 |
> |
public void testDrainToN() throws InterruptedException { |
677 |
|
final SynchronousQueue q = new SynchronousQueue(); |
678 |
< |
Thread t1 = new Thread(new Runnable() { |
679 |
< |
public void run() { |
680 |
< |
try { |
681 |
< |
q.put(one); |
682 |
< |
} catch (InterruptedException ie){ |
683 |
< |
threadUnexpectedException(); |
684 |
< |
} |
685 |
< |
} |
686 |
< |
}); |
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 |
< |
}); |
678 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
679 |
> |
public void realRun() throws InterruptedException { |
680 |
> |
q.put(one); |
681 |
> |
}}); |
682 |
> |
|
683 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
684 |
> |
public void realRun() throws InterruptedException { |
685 |
> |
q.put(two); |
686 |
> |
}}); |
687 |
|
|
688 |
< |
try { |
689 |
< |
t1.start(); |
690 |
< |
t2.start(); |
691 |
< |
ArrayList l = new ArrayList(); |
692 |
< |
Thread.sleep(SHORT_DELAY_MS); |
693 |
< |
q.drainTo(l, 1); |
694 |
< |
assertTrue(l.size() == 1); |
695 |
< |
q.drainTo(l, 1); |
696 |
< |
assertTrue(l.size() == 2); |
697 |
< |
assertTrue(l.contains(one)); |
698 |
< |
assertTrue(l.contains(two)); |
699 |
< |
t1.join(); |
887 |
< |
t2.join(); |
888 |
< |
} catch (Exception e){ |
889 |
< |
unexpectedException(); |
890 |
< |
} |
688 |
> |
t1.start(); |
689 |
> |
t2.start(); |
690 |
> |
ArrayList l = new ArrayList(); |
691 |
> |
Thread.sleep(SHORT_DELAY_MS); |
692 |
> |
q.drainTo(l, 1); |
693 |
> |
assertEquals(1, l.size()); |
694 |
> |
q.drainTo(l, 1); |
695 |
> |
assertEquals(2, l.size()); |
696 |
> |
assertTrue(l.contains(one)); |
697 |
> |
assertTrue(l.contains(two)); |
698 |
> |
t1.join(); |
699 |
> |
t2.join(); |
700 |
|
} |
701 |
|
|
893 |
– |
|
702 |
|
} |