1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
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 TestCase { |
15 |
> |
public class SynchronousQueueTest extends JSR166TestCase { |
16 |
|
|
17 |
< |
private final static int N = 1; |
18 |
< |
private static long SHORT_DELAY_MS = 100; |
19 |
< |
private static long MEDIUM_DELAY_MS = 1000; |
20 |
< |
private static long LONG_DELAY_MS = 10000; |
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 |
+ |
/** |
40 |
+ |
* A SynchronousQueue is both empty and full |
41 |
+ |
*/ |
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 |
> |
} |
49 |
> |
|
50 |
> |
/** |
51 |
> |
* A fair SynchronousQueue is both empty and full |
52 |
> |
*/ |
53 |
> |
public void testFairEmptyFull() { |
54 |
> |
SynchronousQueue q = new SynchronousQueue(true); |
55 |
> |
assertTrue(q.isEmpty()); |
56 |
> |
assertEquals(0, q.size()); |
57 |
|
assertEquals(0, q.remainingCapacity()); |
58 |
< |
assertFalse(q.offer(new Integer(3))); |
58 |
> |
assertFalse(q.offer(zero)); |
59 |
|
} |
60 |
|
|
61 |
< |
public void testOfferNull(){ |
62 |
< |
try { |
61 |
> |
/** |
62 |
> |
* offer(null) throws NPE |
63 |
> |
*/ |
64 |
> |
public void testOfferNull() { |
65 |
> |
try { |
66 |
|
SynchronousQueue q = new SynchronousQueue(); |
67 |
|
q.offer(null); |
68 |
< |
fail("should throw NPE"); |
69 |
< |
} catch (NullPointerException success) { } |
68 |
> |
shouldThrow(); |
69 |
> |
} catch (NullPointerException success) {} |
70 |
> |
} |
71 |
> |
|
72 |
> |
/** |
73 |
> |
* add(null) throws NPE |
74 |
> |
*/ |
75 |
> |
public void testAddNull() { |
76 |
> |
try { |
77 |
> |
SynchronousQueue q = new SynchronousQueue(); |
78 |
> |
q.add(null); |
79 |
> |
shouldThrow(); |
80 |
> |
} catch (NullPointerException success) {} |
81 |
|
} |
82 |
|
|
83 |
< |
public void testOffer(){ |
83 |
> |
/** |
84 |
> |
* offer fails if no active taker |
85 |
> |
*/ |
86 |
> |
public void testOffer() { |
87 |
|
SynchronousQueue q = new SynchronousQueue(); |
88 |
< |
assertFalse(q.offer(new Integer(1))); |
88 |
> |
assertFalse(q.offer(one)); |
89 |
|
} |
90 |
|
|
91 |
< |
public void testAdd(){ |
92 |
< |
try { |
91 |
> |
/** |
92 |
> |
* add throws ISE if no active taker |
93 |
> |
*/ |
94 |
> |
public void testAdd() { |
95 |
> |
try { |
96 |
|
SynchronousQueue q = new SynchronousQueue(); |
97 |
|
assertEquals(0, q.remainingCapacity()); |
98 |
< |
q.add(new Integer(0)); |
99 |
< |
} catch (IllegalStateException success){ |
100 |
< |
} |
98 |
> |
q.add(one); |
99 |
> |
shouldThrow(); |
100 |
> |
} catch (IllegalStateException success) {} |
101 |
|
} |
102 |
|
|
103 |
< |
public void testAddAll1(){ |
103 |
> |
/** |
104 |
> |
* addAll(null) throws NPE |
105 |
> |
*/ |
106 |
> |
public void testAddAll1() { |
107 |
|
try { |
108 |
|
SynchronousQueue q = new SynchronousQueue(); |
109 |
|
q.addAll(null); |
110 |
< |
fail("Cannot add null collection"); |
111 |
< |
} |
112 |
< |
catch (NullPointerException success) {} |
110 |
> |
shouldThrow(); |
111 |
> |
} catch (NullPointerException success) {} |
112 |
> |
} |
113 |
> |
|
114 |
> |
/** |
115 |
> |
* addAll(this) throws IAE |
116 |
> |
*/ |
117 |
> |
public void testAddAllSelf() { |
118 |
> |
try { |
119 |
> |
SynchronousQueue q = new SynchronousQueue(); |
120 |
> |
q.addAll(q); |
121 |
> |
shouldThrow(); |
122 |
> |
} catch (IllegalArgumentException success) {} |
123 |
|
} |
124 |
< |
public void testAddAll2(){ |
124 |
> |
|
125 |
> |
/** |
126 |
> |
* addAll of a collection with null elements throws NPE |
127 |
> |
*/ |
128 |
> |
public void testAddAll2() { |
129 |
|
try { |
130 |
|
SynchronousQueue q = new SynchronousQueue(); |
131 |
< |
Integer[] ints = new Integer[N]; |
131 |
> |
Integer[] ints = new Integer[1]; |
132 |
|
q.addAll(Arrays.asList(ints)); |
133 |
< |
fail("Cannot add null elements"); |
134 |
< |
} |
73 |
< |
catch (NullPointerException success) {} |
133 |
> |
shouldThrow(); |
134 |
> |
} catch (NullPointerException success) {} |
135 |
|
} |
136 |
< |
public void testAddAll4(){ |
136 |
> |
|
137 |
> |
/** |
138 |
> |
* addAll throws ISE if no active taker |
139 |
> |
*/ |
140 |
> |
public void testAddAll4() { |
141 |
|
try { |
142 |
|
SynchronousQueue q = new SynchronousQueue(); |
143 |
< |
Integer[] ints = new Integer[N]; |
144 |
< |
for (int i = 0; i < N; ++i) |
143 |
> |
Integer[] ints = new Integer[1]; |
144 |
> |
for (int i = 0; i < 1; ++i) |
145 |
|
ints[i] = new Integer(i); |
146 |
|
q.addAll(Arrays.asList(ints)); |
147 |
< |
fail("Cannot add with insufficient capacity"); |
148 |
< |
} |
84 |
< |
catch (IllegalStateException success) {} |
147 |
> |
shouldThrow(); |
148 |
> |
} catch (IllegalStateException success) {} |
149 |
|
} |
150 |
|
|
151 |
< |
public void testPutNull() { |
152 |
< |
try { |
151 |
> |
/** |
152 |
> |
* put(null) throws NPE |
153 |
> |
*/ |
154 |
> |
public void testPutNull() throws InterruptedException { |
155 |
> |
try { |
156 |
|
SynchronousQueue q = new SynchronousQueue(); |
157 |
|
q.put(null); |
158 |
< |
fail("put should throw NPE"); |
159 |
< |
} |
93 |
< |
catch (NullPointerException success){ |
94 |
< |
} |
95 |
< |
catch (InterruptedException ie) { |
96 |
< |
fail("Unexpected exception"); |
97 |
< |
} |
158 |
> |
shouldThrow(); |
159 |
> |
} catch (NullPointerException success) {} |
160 |
|
} |
161 |
|
|
162 |
< |
public void testBlockingPut(){ |
163 |
< |
Thread t = new Thread(new Runnable() { |
164 |
< |
public void run() { |
165 |
< |
try { |
166 |
< |
SynchronousQueue q = new SynchronousQueue(); |
167 |
< |
q.put(new Integer(0)); |
168 |
< |
fail("put should block"); |
169 |
< |
} catch (InterruptedException ie){ |
170 |
< |
} |
171 |
< |
}}); |
172 |
< |
t.start(); |
173 |
< |
try { |
174 |
< |
Thread.sleep(SHORT_DELAY_MS); |
175 |
< |
t.interrupt(); |
114 |
< |
t.join(); |
115 |
< |
} |
116 |
< |
catch (InterruptedException ie) { |
117 |
< |
fail("Unexpected exception"); |
118 |
< |
} |
162 |
> |
/** |
163 |
> |
* put blocks interruptibly if no active taker |
164 |
> |
*/ |
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 |
> |
Thread.sleep(SHORT_DELAY_MS); |
174 |
> |
t.interrupt(); |
175 |
> |
t.join(); |
176 |
|
} |
177 |
|
|
178 |
< |
public void testPutWithTake() { |
178 |
> |
/** |
179 |
> |
* put blocks waiting for take |
180 |
> |
*/ |
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; |
129 |
< |
q.put(new Object()); |
130 |
< |
++added; |
131 |
< |
q.put(new Object()); |
132 |
< |
++added; |
133 |
< |
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; |
135 |
– |
fail("Should block"); |
136 |
– |
} catch (InterruptedException e){ |
137 |
– |
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(); |
148 |
< |
} catch (Exception e){ |
149 |
< |
fail("Unexpected exception"); |
150 |
< |
} |
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 |
< |
public void testTimedOffer() { |
204 |
> |
/** |
205 |
> |
* timed offer times out if elements not taken |
206 |
> |
*/ |
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 |
< |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); |
214 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
215 |
< |
fail("Should block"); |
216 |
< |
} catch (InterruptedException success){} |
217 |
< |
} |
218 |
< |
}); |
165 |
< |
|
166 |
< |
try { |
167 |
< |
t.start(); |
168 |
< |
Thread.sleep(SHORT_DELAY_MS); |
169 |
< |
t.interrupt(); |
170 |
< |
t.join(); |
171 |
< |
} catch (Exception e){ |
172 |
< |
fail("Unexpected exception"); |
173 |
< |
} |
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 |
> |
t.start(); |
216 |
> |
Thread.sleep(SMALL_DELAY_MS); |
217 |
> |
t.interrupt(); |
218 |
> |
t.join(); |
219 |
|
} |
220 |
|
|
221 |
|
|
222 |
< |
public void testTakeFromEmpty() { |
222 |
> |
/** |
223 |
> |
* take blocks interruptibly when empty |
224 |
> |
*/ |
225 |
> |
public void testTakeFromEmpty() throws InterruptedException { |
226 |
|
final SynchronousQueue q = new SynchronousQueue(); |
227 |
< |
Thread t = new Thread(new Runnable() { |
228 |
< |
public void run(){ |
229 |
< |
try { |
230 |
< |
q.take(); |
231 |
< |
fail("Should block"); |
232 |
< |
} catch (InterruptedException success){ } |
227 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
228 |
> |
public void realRun() throws InterruptedException { |
229 |
> |
q.take(); |
230 |
> |
}}); |
231 |
> |
|
232 |
> |
t.start(); |
233 |
> |
Thread.sleep(SHORT_DELAY_MS); |
234 |
> |
t.interrupt(); |
235 |
> |
t.join(); |
236 |
> |
} |
237 |
> |
|
238 |
> |
|
239 |
> |
/** |
240 |
> |
* put blocks interruptibly if no active taker |
241 |
> |
*/ |
242 |
> |
public void testFairBlockingPut() throws InterruptedException { |
243 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
244 |
> |
public void realRun() throws InterruptedException { |
245 |
> |
SynchronousQueue q = new SynchronousQueue(true); |
246 |
> |
q.put(zero); |
247 |
> |
}}); |
248 |
> |
|
249 |
> |
t.start(); |
250 |
> |
Thread.sleep(SHORT_DELAY_MS); |
251 |
> |
t.interrupt(); |
252 |
> |
t.join(); |
253 |
> |
} |
254 |
> |
|
255 |
> |
/** |
256 |
> |
* put blocks waiting for take |
257 |
> |
*/ |
258 |
> |
public void testFairPutWithTake() throws InterruptedException { |
259 |
> |
final SynchronousQueue q = new SynchronousQueue(true); |
260 |
> |
Thread t = new Thread(new CheckedRunnable() { |
261 |
> |
public void realRun() throws InterruptedException { |
262 |
> |
int added = 0; |
263 |
> |
try { |
264 |
> |
while (true) { |
265 |
> |
q.put(added); |
266 |
> |
++added; |
267 |
> |
} |
268 |
> |
} catch (InterruptedException success) { |
269 |
> |
assertEquals(1, added); |
270 |
|
} |
271 |
< |
}); |
272 |
< |
try { |
273 |
< |
t.start(); |
274 |
< |
Thread.sleep(SHORT_DELAY_MS); |
275 |
< |
t.interrupt(); |
276 |
< |
t.join(); |
277 |
< |
} catch (Exception e){ |
278 |
< |
fail("Unexpected exception"); |
279 |
< |
} |
271 |
> |
}}); |
272 |
> |
|
273 |
> |
t.start(); |
274 |
> |
Thread.sleep(SHORT_DELAY_MS); |
275 |
> |
assertEquals(0, q.take()); |
276 |
> |
Thread.sleep(SHORT_DELAY_MS); |
277 |
> |
t.interrupt(); |
278 |
> |
t.join(); |
279 |
> |
} |
280 |
> |
|
281 |
> |
/** |
282 |
> |
* timed offer times out if elements not taken |
283 |
> |
*/ |
284 |
> |
public void testFairTimedOffer() throws InterruptedException { |
285 |
> |
final SynchronousQueue q = new SynchronousQueue(true); |
286 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
287 |
> |
public void realRun() throws InterruptedException { |
288 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
289 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
290 |
> |
}}); |
291 |
> |
|
292 |
> |
t.start(); |
293 |
> |
Thread.sleep(SMALL_DELAY_MS); |
294 |
> |
t.interrupt(); |
295 |
> |
t.join(); |
296 |
> |
} |
297 |
> |
|
298 |
> |
|
299 |
> |
/** |
300 |
> |
* take blocks interruptibly when empty |
301 |
> |
*/ |
302 |
> |
public void testFairTakeFromEmpty() throws InterruptedException { |
303 |
> |
final SynchronousQueue q = new SynchronousQueue(true); |
304 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
305 |
> |
public void realRun() throws InterruptedException { |
306 |
> |
q.take(); |
307 |
> |
}}); |
308 |
> |
|
309 |
> |
t.start(); |
310 |
> |
Thread.sleep(SHORT_DELAY_MS); |
311 |
> |
t.interrupt(); |
312 |
> |
t.join(); |
313 |
|
} |
314 |
|
|
315 |
< |
public void testPoll(){ |
315 |
> |
/** |
316 |
> |
* poll fails unless active taker |
317 |
> |
*/ |
318 |
> |
public void testPoll() { |
319 |
|
SynchronousQueue q = new SynchronousQueue(); |
320 |
< |
assertNull(q.poll()); |
320 |
> |
assertNull(q.poll()); |
321 |
|
} |
322 |
|
|
323 |
< |
public void testTimedPoll0() { |
324 |
< |
try { |
325 |
< |
SynchronousQueue q = new SynchronousQueue(); |
326 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
327 |
< |
} catch (InterruptedException e){ |
328 |
< |
fail("Unexpected exception"); |
208 |
< |
} |
323 |
> |
/** |
324 |
> |
* timed pool with zero timeout times out if no active taker |
325 |
> |
*/ |
326 |
> |
public void testTimedPoll0() throws InterruptedException { |
327 |
> |
SynchronousQueue q = new SynchronousQueue(); |
328 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
329 |
|
} |
330 |
|
|
331 |
< |
public void testTimedPoll() { |
332 |
< |
try { |
333 |
< |
SynchronousQueue q = new SynchronousQueue(); |
334 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
335 |
< |
} catch (InterruptedException e){ |
336 |
< |
fail("Unexpected exception"); |
217 |
< |
} |
218 |
< |
} |
219 |
< |
|
220 |
< |
public void testInterruptedTimedPoll(){ |
221 |
< |
Thread t = new Thread(new Runnable() { |
222 |
< |
public void run() { |
223 |
< |
try { |
224 |
< |
SynchronousQueue q = new SynchronousQueue(); |
225 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
226 |
< |
} catch (InterruptedException success){ |
227 |
< |
} |
228 |
< |
}}); |
229 |
< |
t.start(); |
230 |
< |
try { |
231 |
< |
Thread.sleep(SHORT_DELAY_MS); |
232 |
< |
t.interrupt(); |
233 |
< |
t.join(); |
234 |
< |
} |
235 |
< |
catch (InterruptedException ie) { |
236 |
< |
fail("Unexpected exception"); |
237 |
< |
} |
331 |
> |
/** |
332 |
> |
* timed pool with nonzero timeout times out if no active taker |
333 |
> |
*/ |
334 |
> |
public void testTimedPoll() throws InterruptedException { |
335 |
> |
SynchronousQueue q = new SynchronousQueue(); |
336 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
337 |
|
} |
338 |
|
|
339 |
< |
public void testTimedPollWithOffer(){ |
339 |
> |
/** |
340 |
> |
* Interrupted timed poll throws InterruptedException instead of |
341 |
> |
* returning timeout status |
342 |
> |
*/ |
343 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
344 |
|
final SynchronousQueue q = new SynchronousQueue(); |
345 |
< |
Thread t = new Thread(new Runnable() { |
346 |
< |
public void run(){ |
347 |
< |
try { |
348 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
349 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
350 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
351 |
< |
fail("Should block"); |
352 |
< |
} catch (InterruptedException success) { } |
353 |
< |
} |
354 |
< |
}); |
355 |
< |
try { |
356 |
< |
t.start(); |
357 |
< |
Thread.sleep(SHORT_DELAY_MS * 2); |
358 |
< |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
359 |
< |
t.interrupt(); |
360 |
< |
t.join(); |
361 |
< |
} catch (Exception e){ |
362 |
< |
fail("Unexpected exception"); |
363 |
< |
} |
364 |
< |
} |
345 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
346 |
> |
public void realRun() throws InterruptedException { |
347 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
348 |
> |
}}); |
349 |
> |
|
350 |
> |
t.start(); |
351 |
> |
Thread.sleep(SHORT_DELAY_MS); |
352 |
> |
t.interrupt(); |
353 |
> |
t.join(); |
354 |
> |
} |
355 |
> |
|
356 |
> |
/** |
357 |
> |
* Interrupted timed poll throws InterruptedException instead of |
358 |
> |
* returning timeout status |
359 |
> |
*/ |
360 |
> |
public void testFairInterruptedTimedPoll() throws InterruptedException { |
361 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
362 |
> |
public void realRun() throws InterruptedException { |
363 |
> |
SynchronousQueue q = new SynchronousQueue(true); |
364 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
365 |
> |
}}); |
366 |
|
|
367 |
+ |
t.start(); |
368 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
369 |
+ |
t.interrupt(); |
370 |
+ |
t.join(); |
371 |
+ |
} |
372 |
|
|
373 |
< |
public void testPeek(){ |
373 |
> |
/** |
374 |
> |
* timed poll before a delayed offer fails; after offer succeeds; |
375 |
> |
* on interruption throws |
376 |
> |
*/ |
377 |
> |
public void testFairTimedPollWithOffer() throws InterruptedException { |
378 |
> |
final SynchronousQueue q = new SynchronousQueue(true); |
379 |
> |
Thread t = new Thread(new CheckedRunnable() { |
380 |
> |
public void realRun() throws InterruptedException { |
381 |
> |
try { |
382 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
383 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
384 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
385 |
> |
threadShouldThrow(); |
386 |
> |
} catch (InterruptedException success) {} |
387 |
> |
}}); |
388 |
> |
|
389 |
> |
t.start(); |
390 |
> |
Thread.sleep(SMALL_DELAY_MS); |
391 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
392 |
> |
t.interrupt(); |
393 |
> |
t.join(); |
394 |
> |
} |
395 |
> |
|
396 |
> |
|
397 |
> |
/** |
398 |
> |
* peek returns null |
399 |
> |
*/ |
400 |
> |
public void testPeek() { |
401 |
|
SynchronousQueue q = new SynchronousQueue(); |
402 |
< |
assertNull(q.peek()); |
402 |
> |
assertNull(q.peek()); |
403 |
|
} |
404 |
|
|
405 |
< |
public void testElement(){ |
405 |
> |
/** |
406 |
> |
* element throws NSEE |
407 |
> |
*/ |
408 |
> |
public void testElement() { |
409 |
|
SynchronousQueue q = new SynchronousQueue(); |
410 |
|
try { |
411 |
|
q.element(); |
412 |
< |
fail("no such element"); |
413 |
< |
} |
275 |
< |
catch (NoSuchElementException success) {} |
412 |
> |
shouldThrow(); |
413 |
> |
} catch (NoSuchElementException success) {} |
414 |
|
} |
415 |
|
|
416 |
< |
public void testRemove(){ |
416 |
> |
/** |
417 |
> |
* remove throws NSEE if no active taker |
418 |
> |
*/ |
419 |
> |
public void testRemove() { |
420 |
|
SynchronousQueue q = new SynchronousQueue(); |
421 |
|
try { |
422 |
|
q.remove(); |
423 |
< |
fail("remove should throw"); |
424 |
< |
} catch (NoSuchElementException success){ |
284 |
< |
} |
423 |
> |
shouldThrow(); |
424 |
> |
} catch (NoSuchElementException success) {} |
425 |
|
} |
426 |
|
|
427 |
< |
public void testRemoveElement(){ |
427 |
> |
/** |
428 |
> |
* remove(x) returns false |
429 |
> |
*/ |
430 |
> |
public void testRemoveElement() { |
431 |
|
SynchronousQueue q = new SynchronousQueue(); |
432 |
< |
for (int i = 1; i < N; i+=2) { |
290 |
< |
assertFalse(q.remove(new Integer(i))); |
291 |
< |
} |
432 |
> |
assertFalse(q.remove(zero)); |
433 |
|
assertTrue(q.isEmpty()); |
434 |
|
} |
435 |
< |
|
436 |
< |
public void testContains(){ |
437 |
< |
SynchronousQueue q = new SynchronousQueue(); |
438 |
< |
for (int i = 0; i < N; ++i) { |
439 |
< |
assertFalse(q.contains(new Integer(i))); |
440 |
< |
} |
435 |
> |
|
436 |
> |
/** |
437 |
> |
* contains returns false |
438 |
> |
*/ |
439 |
> |
public void testContains() { |
440 |
> |
SynchronousQueue q = new SynchronousQueue(); |
441 |
> |
assertFalse(q.contains(zero)); |
442 |
|
} |
443 |
|
|
444 |
< |
public void testClear(){ |
444 |
> |
/** |
445 |
> |
* clear ensures isEmpty |
446 |
> |
*/ |
447 |
> |
public void testClear() { |
448 |
|
SynchronousQueue q = new SynchronousQueue(); |
449 |
|
q.clear(); |
450 |
|
assertTrue(q.isEmpty()); |
451 |
|
} |
452 |
|
|
453 |
< |
public void testContainsAll(){ |
453 |
> |
/** |
454 |
> |
* containsAll returns false unless empty |
455 |
> |
*/ |
456 |
> |
public void testContainsAll() { |
457 |
|
SynchronousQueue q = new SynchronousQueue(); |
458 |
|
Integer[] empty = new Integer[0]; |
459 |
< |
Integer[] ints = new Integer[1]; ints[0] = new Integer(0); |
460 |
< |
// assertTrue(q.containsAll(Arrays.asList(empty))); |
459 |
> |
assertTrue(q.containsAll(Arrays.asList(empty))); |
460 |
> |
Integer[] ints = new Integer[1]; ints[0] = zero; |
461 |
|
assertFalse(q.containsAll(Arrays.asList(ints))); |
462 |
|
} |
463 |
|
|
464 |
< |
public void testRetainAll(){ |
464 |
> |
/** |
465 |
> |
* retainAll returns false |
466 |
> |
*/ |
467 |
> |
public void testRetainAll() { |
468 |
|
SynchronousQueue q = new SynchronousQueue(); |
469 |
|
Integer[] empty = new Integer[0]; |
470 |
< |
Integer[] ints = new Integer[1]; ints[0] = new Integer(0); |
471 |
< |
q.retainAll(Arrays.asList(ints)); |
472 |
< |
// assertTrue(q.containsAll(Arrays.asList(empty))); |
322 |
< |
assertFalse(q.containsAll(Arrays.asList(ints))); |
470 |
> |
assertFalse(q.retainAll(Arrays.asList(empty))); |
471 |
> |
Integer[] ints = new Integer[1]; ints[0] = zero; |
472 |
> |
assertFalse(q.retainAll(Arrays.asList(ints))); |
473 |
|
} |
474 |
|
|
475 |
< |
public void testRemoveAll(){ |
475 |
> |
/** |
476 |
> |
* removeAll returns false |
477 |
> |
*/ |
478 |
> |
public void testRemoveAll() { |
479 |
|
SynchronousQueue q = new SynchronousQueue(); |
480 |
|
Integer[] empty = new Integer[0]; |
481 |
< |
Integer[] ints = new Integer[1]; ints[0] = new Integer(0); |
482 |
< |
q.removeAll(Arrays.asList(ints)); |
330 |
< |
// assertTrue(q.containsAll(Arrays.asList(empty))); |
481 |
> |
assertFalse(q.removeAll(Arrays.asList(empty))); |
482 |
> |
Integer[] ints = new Integer[1]; ints[0] = zero; |
483 |
|
assertFalse(q.containsAll(Arrays.asList(ints))); |
484 |
|
} |
485 |
|
|
486 |
|
|
487 |
< |
public void testToArray(){ |
487 |
> |
/** |
488 |
> |
* toArray is empty |
489 |
> |
*/ |
490 |
> |
public void testToArray() { |
491 |
|
SynchronousQueue q = new SynchronousQueue(); |
492 |
< |
Object[] o = q.toArray(); |
492 |
> |
Object[] o = q.toArray(); |
493 |
|
assertEquals(o.length, 0); |
494 |
|
} |
495 |
|
|
496 |
< |
public void testToArray2(){ |
496 |
> |
/** |
497 |
> |
* toArray(a) is nulled at position 0 |
498 |
> |
*/ |
499 |
> |
public void testToArray2() { |
500 |
|
SynchronousQueue q = new SynchronousQueue(); |
501 |
< |
Integer[] ints = new Integer[1]; |
501 |
> |
Integer[] ints = new Integer[1]; |
502 |
|
assertNull(ints[0]); |
503 |
|
} |
504 |
< |
|
505 |
< |
public void testIterator(){ |
504 |
> |
|
505 |
> |
/** |
506 |
> |
* toArray(null) throws NPE |
507 |
> |
*/ |
508 |
> |
public void testToArray_BadArg() { |
509 |
> |
SynchronousQueue q = new SynchronousQueue(); |
510 |
> |
try { |
511 |
> |
Object o[] = q.toArray(null); |
512 |
> |
shouldThrow(); |
513 |
> |
} catch (NullPointerException success) {} |
514 |
> |
} |
515 |
> |
|
516 |
> |
|
517 |
> |
/** |
518 |
> |
* iterator does not traverse any elements |
519 |
> |
*/ |
520 |
> |
public void testIterator() { |
521 |
|
SynchronousQueue q = new SynchronousQueue(); |
522 |
< |
Iterator it = q.iterator(); |
522 |
> |
Iterator it = q.iterator(); |
523 |
|
assertFalse(it.hasNext()); |
524 |
|
try { |
525 |
|
Object x = it.next(); |
526 |
< |
fail("should throw"); |
527 |
< |
} |
355 |
< |
catch (NoSuchElementException success) {} |
526 |
> |
shouldThrow(); |
527 |
> |
} catch (NoSuchElementException success) {} |
528 |
|
} |
529 |
|
|
530 |
< |
public void testIteratorRemove(){ |
530 |
> |
/** |
531 |
> |
* iterator remove throws ISE |
532 |
> |
*/ |
533 |
> |
public void testIteratorRemove() { |
534 |
|
SynchronousQueue q = new SynchronousQueue(); |
535 |
< |
Iterator it = q.iterator(); |
535 |
> |
Iterator it = q.iterator(); |
536 |
|
try { |
537 |
|
it.remove(); |
538 |
< |
fail("should throw"); |
539 |
< |
} |
365 |
< |
catch (IllegalStateException success) {} |
538 |
> |
shouldThrow(); |
539 |
> |
} catch (IllegalStateException success) {} |
540 |
|
} |
541 |
|
|
542 |
< |
public void testToString(){ |
542 |
> |
/** |
543 |
> |
* toString returns a non-null string |
544 |
> |
*/ |
545 |
> |
public void testToString() { |
546 |
|
SynchronousQueue q = new SynchronousQueue(); |
547 |
|
String s = q.toString(); |
548 |
< |
assertTrue(s != null); |
549 |
< |
} |
548 |
> |
assertNotNull(s); |
549 |
> |
} |
550 |
|
|
551 |
|
|
552 |
+ |
/** |
553 |
+ |
* offer transfers elements across Executor tasks |
554 |
+ |
*/ |
555 |
|
public void testOfferInExecutor() { |
556 |
|
final SynchronousQueue q = new SynchronousQueue(); |
557 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
378 |
– |
final Integer one = new Integer(1); |
558 |
|
|
559 |
< |
executor.execute(new Runnable() { |
560 |
< |
public void run() { |
559 |
> |
executor.execute(new CheckedRunnable() { |
560 |
> |
public void realRun() throws InterruptedException { |
561 |
|
assertFalse(q.offer(one)); |
562 |
< |
try { |
563 |
< |
assertTrue(q.offer(one, MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); |
564 |
< |
assertEquals(0, q.remainingCapacity()); |
565 |
< |
} |
566 |
< |
catch (InterruptedException e) { |
567 |
< |
fail("should not be interrupted"); |
568 |
< |
} |
569 |
< |
} |
570 |
< |
}); |
562 |
> |
assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); |
563 |
> |
assertEquals(0, q.remainingCapacity()); |
564 |
> |
}}); |
565 |
> |
|
566 |
> |
executor.execute(new CheckedRunnable() { |
567 |
> |
public void realRun() throws InterruptedException { |
568 |
> |
Thread.sleep(SMALL_DELAY_MS); |
569 |
> |
assertSame(one, q.take()); |
570 |
> |
}}); |
571 |
> |
|
572 |
> |
joinPool(executor); |
573 |
> |
} |
574 |
> |
|
575 |
> |
/** |
576 |
> |
* poll retrieves elements across Executor threads |
577 |
> |
*/ |
578 |
> |
public void testPollInExecutor() { |
579 |
> |
final SynchronousQueue q = new SynchronousQueue(); |
580 |
> |
ExecutorService executor = Executors.newFixedThreadPool(2); |
581 |
> |
executor.execute(new CheckedRunnable() { |
582 |
> |
public void realRun() throws InterruptedException { |
583 |
> |
assertNull(q.poll()); |
584 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
585 |
> |
assertTrue(q.isEmpty()); |
586 |
> |
}}); |
587 |
|
|
588 |
< |
executor.execute(new Runnable() { |
589 |
< |
public void run() { |
590 |
< |
try { |
591 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
592 |
< |
assertEquals(one, q.take()); |
398 |
< |
} |
399 |
< |
catch (InterruptedException e) { |
400 |
< |
fail("should not be interrupted"); |
401 |
< |
} |
402 |
< |
} |
403 |
< |
}); |
404 |
< |
|
405 |
< |
executor.shutdown(); |
588 |
> |
executor.execute(new CheckedRunnable() { |
589 |
> |
public void realRun() throws InterruptedException { |
590 |
> |
Thread.sleep(SHORT_DELAY_MS); |
591 |
> |
q.put(one); |
592 |
> |
}}); |
593 |
|
|
594 |
+ |
joinPool(executor); |
595 |
|
} |
596 |
|
|
597 |
< |
public void testPollInExecutor() { |
597 |
> |
/** |
598 |
> |
* a deserialized serialized queue is usable |
599 |
> |
*/ |
600 |
> |
public void testSerialization() throws Exception { |
601 |
> |
SynchronousQueue q = new SynchronousQueue(); |
602 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
603 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
604 |
> |
out.writeObject(q); |
605 |
> |
out.close(); |
606 |
|
|
607 |
< |
final SynchronousQueue q = new SynchronousQueue(); |
607 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
608 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
609 |
> |
SynchronousQueue r = (SynchronousQueue)in.readObject(); |
610 |
> |
assertEquals(q.size(), r.size()); |
611 |
> |
while (!q.isEmpty()) |
612 |
> |
assertEquals(q.remove(), r.remove()); |
613 |
> |
} |
614 |
|
|
615 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
615 |
> |
/** |
616 |
> |
* drainTo(null) throws NPE |
617 |
> |
*/ |
618 |
> |
public void testDrainToNull() { |
619 |
> |
SynchronousQueue q = new SynchronousQueue(); |
620 |
> |
try { |
621 |
> |
q.drainTo(null); |
622 |
> |
shouldThrow(); |
623 |
> |
} catch (NullPointerException success) {} |
624 |
> |
} |
625 |
|
|
626 |
< |
executor.execute(new Runnable() { |
627 |
< |
public void run() { |
628 |
< |
assertNull(q.poll()); |
629 |
< |
try { |
630 |
< |
assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); |
631 |
< |
assertTrue(q.isEmpty()); |
632 |
< |
} |
633 |
< |
catch (InterruptedException e) { |
634 |
< |
fail("should not be interrupted"); |
635 |
< |
} |
425 |
< |
} |
426 |
< |
}); |
626 |
> |
/** |
627 |
> |
* drainTo(this) throws IAE |
628 |
> |
*/ |
629 |
> |
public void testDrainToSelf() { |
630 |
> |
SynchronousQueue q = new SynchronousQueue(); |
631 |
> |
try { |
632 |
> |
q.drainTo(q); |
633 |
> |
shouldThrow(); |
634 |
> |
} catch (IllegalArgumentException success) {} |
635 |
> |
} |
636 |
|
|
637 |
< |
executor.execute(new Runnable() { |
638 |
< |
public void run() { |
639 |
< |
try { |
640 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
641 |
< |
q.put(new Integer(1)); |
642 |
< |
} |
643 |
< |
catch (InterruptedException e) { |
644 |
< |
fail("should not be interrupted"); |
645 |
< |
} |
646 |
< |
} |
647 |
< |
}); |
648 |
< |
|
649 |
< |
executor.shutdown(); |
637 |
> |
/** |
638 |
> |
* drainTo(c) of empty queue doesn't transfer elements |
639 |
> |
*/ |
640 |
> |
public void testDrainTo() { |
641 |
> |
SynchronousQueue q = new SynchronousQueue(); |
642 |
> |
ArrayList l = new ArrayList(); |
643 |
> |
q.drainTo(l); |
644 |
> |
assertEquals(q.size(), 0); |
645 |
> |
assertEquals(l.size(), 0); |
646 |
> |
} |
647 |
> |
|
648 |
> |
/** |
649 |
> |
* drainTo empties queue, unblocking a waiting put. |
650 |
> |
*/ |
651 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
652 |
> |
final SynchronousQueue q = new SynchronousQueue(); |
653 |
> |
Thread t = new Thread(new CheckedRunnable() { |
654 |
> |
public void realRun() throws InterruptedException { |
655 |
> |
q.put(new Integer(1)); |
656 |
> |
}}); |
657 |
|
|
658 |
+ |
t.start(); |
659 |
+ |
ArrayList l = new ArrayList(); |
660 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
661 |
+ |
q.drainTo(l); |
662 |
+ |
assertTrue(l.size() <= 1); |
663 |
+ |
if (l.size() > 0) |
664 |
+ |
assertEquals(l.get(0), new Integer(1)); |
665 |
+ |
t.join(); |
666 |
+ |
assertTrue(l.size() <= 1); |
667 |
|
} |
668 |
|
|
669 |
< |
public void testSerialization() { |
669 |
> |
/** |
670 |
> |
* drainTo(null, n) throws NPE |
671 |
> |
*/ |
672 |
> |
public void testDrainToNullN() { |
673 |
|
SynchronousQueue q = new SynchronousQueue(); |
674 |
|
try { |
675 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
676 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
677 |
< |
out.writeObject(q); |
678 |
< |
out.close(); |
675 |
> |
q.drainTo(null, 0); |
676 |
> |
shouldThrow(); |
677 |
> |
} catch (NullPointerException success) {} |
678 |
> |
} |
679 |
|
|
680 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
681 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
682 |
< |
SynchronousQueue r = (SynchronousQueue)in.readObject(); |
683 |
< |
assertEquals(q.size(), r.size()); |
684 |
< |
while (!q.isEmpty()) |
685 |
< |
assertEquals(q.remove(), r.remove()); |
686 |
< |
} catch(Exception e){ |
687 |
< |
fail("unexpected exception"); |
688 |
< |
} |
680 |
> |
/** |
681 |
> |
* drainTo(this, n) throws IAE |
682 |
> |
*/ |
683 |
> |
public void testDrainToSelfN() { |
684 |
> |
SynchronousQueue q = new SynchronousQueue(); |
685 |
> |
try { |
686 |
> |
q.drainTo(q, 0); |
687 |
> |
shouldThrow(); |
688 |
> |
} catch (IllegalArgumentException success) {} |
689 |
> |
} |
690 |
> |
|
691 |
> |
/** |
692 |
> |
* drainTo(c, n) empties up to n elements of queue into c |
693 |
> |
*/ |
694 |
> |
public void testDrainToN() throws InterruptedException { |
695 |
> |
final SynchronousQueue q = new SynchronousQueue(); |
696 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
697 |
> |
public void realRun() throws InterruptedException { |
698 |
> |
q.put(one); |
699 |
> |
}}); |
700 |
> |
|
701 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
702 |
> |
public void realRun() throws InterruptedException { |
703 |
> |
q.put(two); |
704 |
> |
}}); |
705 |
> |
|
706 |
> |
t1.start(); |
707 |
> |
t2.start(); |
708 |
> |
ArrayList l = new ArrayList(); |
709 |
> |
Thread.sleep(SHORT_DELAY_MS); |
710 |
> |
q.drainTo(l, 1); |
711 |
> |
assertEquals(1, l.size()); |
712 |
> |
q.drainTo(l, 1); |
713 |
> |
assertEquals(2, l.size()); |
714 |
> |
assertTrue(l.contains(one)); |
715 |
> |
assertTrue(l.contains(two)); |
716 |
> |
t1.join(); |
717 |
> |
t2.join(); |
718 |
|
} |
719 |
|
|
720 |
|
} |