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 { |
14 |
< |
|
15 |
< |
private final static int N = 1; |
16 |
< |
private static long SHORT_DELAY_MS = 100; |
17 |
< |
private static long MEDIUM_DELAY_MS = 1000; |
18 |
< |
private static long LONG_DELAY_MS = 10000; |
15 |
> |
public class SynchronousQueueTest extends JSR166TestCase { |
16 |
|
|
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run (suite()); |
19 |
|
} |
20 |
|
|
21 |
|
public static Test suite() { |
22 |
< |
return new TestSuite(SynchronousQueueTest.class); |
22 |
> |
return new TestSuite(SynchronousQueueTest.class); |
23 |
|
} |
24 |
|
|
25 |
+ |
/** |
26 |
+ |
* A SynchronousQueue is both empty and full |
27 |
+ |
*/ |
28 |
|
public void testEmptyFull() { |
29 |
|
SynchronousQueue q = new SynchronousQueue(); |
30 |
|
assertTrue(q.isEmpty()); |
31 |
< |
assertEquals(0, q.size()); |
31 |
> |
assertEquals(0, q.size()); |
32 |
> |
assertEquals(0, q.remainingCapacity()); |
33 |
> |
assertFalse(q.offer(zero)); |
34 |
> |
} |
35 |
> |
|
36 |
> |
/** |
37 |
> |
* A fair SynchronousQueue is both empty and full |
38 |
> |
*/ |
39 |
> |
public void testFairEmptyFull() { |
40 |
> |
SynchronousQueue q = new SynchronousQueue(true); |
41 |
> |
assertTrue(q.isEmpty()); |
42 |
> |
assertEquals(0, q.size()); |
43 |
|
assertEquals(0, q.remainingCapacity()); |
44 |
< |
assertFalse(q.offer(new Integer(3))); |
44 |
> |
assertFalse(q.offer(zero)); |
45 |
|
} |
46 |
|
|
47 |
< |
public void testOfferNull(){ |
48 |
< |
try { |
47 |
> |
/** |
48 |
> |
* offer(null) throws NPE |
49 |
> |
*/ |
50 |
> |
public void testOfferNull() { |
51 |
> |
try { |
52 |
|
SynchronousQueue q = new SynchronousQueue(); |
53 |
|
q.offer(null); |
54 |
< |
fail("should throw NPE"); |
55 |
< |
} catch (NullPointerException success) { } |
54 |
> |
shouldThrow(); |
55 |
> |
} catch (NullPointerException success) {} |
56 |
> |
} |
57 |
> |
|
58 |
> |
/** |
59 |
> |
* add(null) throws NPE |
60 |
> |
*/ |
61 |
> |
public void testAddNull() { |
62 |
> |
try { |
63 |
> |
SynchronousQueue q = new SynchronousQueue(); |
64 |
> |
q.add(null); |
65 |
> |
shouldThrow(); |
66 |
> |
} catch (NullPointerException success) {} |
67 |
|
} |
68 |
|
|
69 |
< |
public void testOffer(){ |
69 |
> |
/** |
70 |
> |
* offer fails if no active taker |
71 |
> |
*/ |
72 |
> |
public void testOffer() { |
73 |
|
SynchronousQueue q = new SynchronousQueue(); |
74 |
< |
assertFalse(q.offer(new Integer(1))); |
74 |
> |
assertFalse(q.offer(one)); |
75 |
|
} |
76 |
|
|
77 |
< |
public void testAdd(){ |
78 |
< |
try { |
77 |
> |
/** |
78 |
> |
* add throws ISE if no active taker |
79 |
> |
*/ |
80 |
> |
public void testAdd() { |
81 |
> |
try { |
82 |
|
SynchronousQueue q = new SynchronousQueue(); |
83 |
|
assertEquals(0, q.remainingCapacity()); |
84 |
< |
q.add(new Integer(0)); |
85 |
< |
} catch (IllegalStateException success){ |
86 |
< |
} |
84 |
> |
q.add(one); |
85 |
> |
shouldThrow(); |
86 |
> |
} catch (IllegalStateException success) {} |
87 |
|
} |
88 |
|
|
89 |
< |
public void testAddAll1(){ |
89 |
> |
/** |
90 |
> |
* addAll(null) throws NPE |
91 |
> |
*/ |
92 |
> |
public void testAddAll1() { |
93 |
|
try { |
94 |
|
SynchronousQueue q = new SynchronousQueue(); |
95 |
|
q.addAll(null); |
96 |
< |
fail("Cannot add null collection"); |
97 |
< |
} |
98 |
< |
catch (NullPointerException success) {} |
96 |
> |
shouldThrow(); |
97 |
> |
} catch (NullPointerException success) {} |
98 |
> |
} |
99 |
> |
|
100 |
> |
/** |
101 |
> |
* addAll(this) throws IAE |
102 |
> |
*/ |
103 |
> |
public void testAddAllSelf() { |
104 |
> |
try { |
105 |
> |
SynchronousQueue q = new SynchronousQueue(); |
106 |
> |
q.addAll(q); |
107 |
> |
shouldThrow(); |
108 |
> |
} catch (IllegalArgumentException success) {} |
109 |
|
} |
110 |
< |
public void testAddAll2(){ |
110 |
> |
|
111 |
> |
/** |
112 |
> |
* addAll of a collection with null elements throws NPE |
113 |
> |
*/ |
114 |
> |
public void testAddAll2() { |
115 |
|
try { |
116 |
|
SynchronousQueue q = new SynchronousQueue(); |
117 |
< |
Integer[] ints = new Integer[N]; |
117 |
> |
Integer[] ints = new Integer[1]; |
118 |
|
q.addAll(Arrays.asList(ints)); |
119 |
< |
fail("Cannot add null elements"); |
120 |
< |
} |
73 |
< |
catch (NullPointerException success) {} |
119 |
> |
shouldThrow(); |
120 |
> |
} catch (NullPointerException success) {} |
121 |
|
} |
122 |
< |
public void testAddAll4(){ |
122 |
> |
/** |
123 |
> |
* addAll throws ISE if no active taker |
124 |
> |
*/ |
125 |
> |
public void testAddAll4() { |
126 |
|
try { |
127 |
|
SynchronousQueue q = new SynchronousQueue(); |
128 |
< |
Integer[] ints = new Integer[N]; |
129 |
< |
for (int i = 0; i < N; ++i) |
128 |
> |
Integer[] ints = new Integer[1]; |
129 |
> |
for (int i = 0; i < 1; ++i) |
130 |
|
ints[i] = new Integer(i); |
131 |
|
q.addAll(Arrays.asList(ints)); |
132 |
< |
fail("Cannot add with insufficient capacity"); |
133 |
< |
} |
84 |
< |
catch (IllegalStateException success) {} |
132 |
> |
shouldThrow(); |
133 |
> |
} catch (IllegalStateException success) {} |
134 |
|
} |
135 |
|
|
136 |
< |
public void testPutNull() { |
137 |
< |
try { |
136 |
> |
/** |
137 |
> |
* put(null) throws NPE |
138 |
> |
*/ |
139 |
> |
public void testPutNull() throws InterruptedException { |
140 |
> |
try { |
141 |
|
SynchronousQueue q = new SynchronousQueue(); |
142 |
|
q.put(null); |
143 |
< |
fail("put should throw NPE"); |
144 |
< |
} |
93 |
< |
catch (NullPointerException success){ |
94 |
< |
} |
95 |
< |
catch (InterruptedException ie) { |
96 |
< |
fail("Unexpected exception"); |
97 |
< |
} |
143 |
> |
shouldThrow(); |
144 |
> |
} catch (NullPointerException success) {} |
145 |
|
} |
146 |
|
|
147 |
< |
public void testBlockingPut(){ |
148 |
< |
Thread t = new Thread(new Runnable() { |
149 |
< |
public void run() { |
150 |
< |
try { |
151 |
< |
SynchronousQueue q = new SynchronousQueue(); |
152 |
< |
q.put(new Integer(0)); |
153 |
< |
fail("put should block"); |
154 |
< |
} catch (InterruptedException ie){ |
155 |
< |
} |
156 |
< |
}}); |
157 |
< |
t.start(); |
158 |
< |
try { |
159 |
< |
Thread.sleep(SHORT_DELAY_MS); |
160 |
< |
t.interrupt(); |
114 |
< |
t.join(); |
115 |
< |
} |
116 |
< |
catch (InterruptedException ie) { |
117 |
< |
fail("Unexpected exception"); |
118 |
< |
} |
147 |
> |
/** |
148 |
> |
* put blocks interruptibly if no active taker |
149 |
> |
*/ |
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 |
> |
Thread.sleep(SHORT_DELAY_MS); |
159 |
> |
t.interrupt(); |
160 |
> |
t.join(); |
161 |
|
} |
162 |
|
|
163 |
< |
public void testPutWithTake() { |
163 |
> |
/** |
164 |
> |
* put blocks waiting for take |
165 |
> |
*/ |
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 |
< |
fail("Should block"); |
181 |
< |
} catch (InterruptedException e){ |
182 |
< |
assertTrue(added >= 1); |
138 |
< |
} |
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(); |
148 |
< |
} catch (Exception e){ |
149 |
< |
fail("Unexpected exception"); |
150 |
< |
} |
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 |
< |
public void testTimedOffer() { |
194 |
> |
/** |
195 |
> |
* timed offer times out if elements not taken |
196 |
> |
*/ |
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 |
< |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); |
204 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
205 |
< |
fail("Should block"); |
206 |
< |
} catch (InterruptedException success){} |
207 |
< |
} |
208 |
< |
}); |
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 |
< |
} |
199 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
200 |
> |
public void realRun() throws InterruptedException { |
201 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
202 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
203 |
> |
}}); |
204 |
> |
|
205 |
> |
t.start(); |
206 |
> |
Thread.sleep(SMALL_DELAY_MS); |
207 |
> |
t.interrupt(); |
208 |
> |
t.join(); |
209 |
|
} |
210 |
|
|
211 |
|
|
212 |
< |
public void testTakeFromEmpty() { |
212 |
> |
/** |
213 |
> |
* take blocks interruptibly when empty |
214 |
> |
*/ |
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 |
< |
fail("Should block"); |
222 |
< |
} catch (InterruptedException success){ } |
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() 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 |
> |
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() throws InterruptedException { |
249 |
> |
final SynchronousQueue q = new SynchronousQueue(true); |
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 |
< |
t.interrupt(); |
271 |
< |
t.join(); |
272 |
< |
} catch (Exception e){ |
273 |
< |
fail("Unexpected exception"); |
274 |
< |
} |
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() throws InterruptedException { |
280 |
> |
final SynchronousQueue q = new SynchronousQueue(true); |
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 |
> |
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() throws InterruptedException { |
298 |
> |
final SynchronousQueue q = new SynchronousQueue(true); |
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 |
< |
public void testPoll(){ |
310 |
> |
/** |
311 |
> |
* poll fails unless active taker |
312 |
> |
*/ |
313 |
> |
public void testPoll() { |
314 |
|
SynchronousQueue q = new SynchronousQueue(); |
315 |
< |
assertNull(q.poll()); |
315 |
> |
assertNull(q.poll()); |
316 |
|
} |
317 |
|
|
318 |
< |
public void testTimedPoll0() { |
319 |
< |
try { |
320 |
< |
SynchronousQueue q = new SynchronousQueue(); |
321 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
322 |
< |
} catch (InterruptedException e){ |
323 |
< |
fail("Unexpected exception"); |
208 |
< |
} |
318 |
> |
/** |
319 |
> |
* timed pool with zero timeout times out if no active taker |
320 |
> |
*/ |
321 |
> |
public void testTimedPoll0() throws InterruptedException { |
322 |
> |
SynchronousQueue q = new SynchronousQueue(); |
323 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
324 |
|
} |
325 |
|
|
326 |
< |
public void testTimedPoll() { |
327 |
< |
try { |
328 |
< |
SynchronousQueue q = new SynchronousQueue(); |
329 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
330 |
< |
} catch (InterruptedException e){ |
331 |
< |
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 |
< |
} |
326 |
> |
/** |
327 |
> |
* timed pool with nonzero timeout times out if no active taker |
328 |
> |
*/ |
329 |
> |
public void testTimedPoll() throws InterruptedException { |
330 |
> |
SynchronousQueue q = new SynchronousQueue(); |
331 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
332 |
|
} |
333 |
|
|
334 |
< |
public void testTimedPollWithOffer(){ |
334 |
> |
/** |
335 |
> |
* Interrupted timed poll throws InterruptedException instead of |
336 |
> |
* returning timeout status |
337 |
> |
*/ |
338 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
339 |
|
final SynchronousQueue q = new SynchronousQueue(); |
340 |
< |
Thread t = new Thread(new Runnable() { |
341 |
< |
public void run(){ |
342 |
< |
try { |
343 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
344 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
345 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
346 |
< |
fail("Should block"); |
347 |
< |
} catch (InterruptedException success) { } |
348 |
< |
} |
349 |
< |
}); |
350 |
< |
try { |
351 |
< |
t.start(); |
352 |
< |
Thread.sleep(SHORT_DELAY_MS * 2); |
353 |
< |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
354 |
< |
t.interrupt(); |
355 |
< |
t.join(); |
356 |
< |
} catch (Exception e){ |
357 |
< |
fail("Unexpected exception"); |
358 |
< |
} |
359 |
< |
} |
340 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
341 |
> |
public void realRun() throws InterruptedException { |
342 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
343 |
> |
}}); |
344 |
> |
|
345 |
> |
t.start(); |
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() throws InterruptedException { |
356 |
> |
final SynchronousQueue q = new SynchronousQueue(); |
357 |
> |
Thread t = new Thread(new CheckedRunnable() { |
358 |
> |
public void realRun() throws InterruptedException { |
359 |
> |
assertNull(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 |
> |
shouldThrow(); |
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() 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 |
> |
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() throws InterruptedException { |
396 |
> |
final SynchronousQueue q = new SynchronousQueue(true); |
397 |
> |
Thread t = new Thread(new CheckedRunnable() { |
398 |
> |
public void realRun() throws InterruptedException { |
399 |
> |
assertNull(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 |
|
|
415 |
< |
public void testPeek(){ |
415 |
> |
/** |
416 |
> |
* peek returns null |
417 |
> |
*/ |
418 |
> |
public void testPeek() { |
419 |
|
SynchronousQueue q = new SynchronousQueue(); |
420 |
< |
assertNull(q.peek()); |
420 |
> |
assertNull(q.peek()); |
421 |
|
} |
422 |
|
|
423 |
< |
public void testElement(){ |
423 |
> |
/** |
424 |
> |
* element throws NSEE |
425 |
> |
*/ |
426 |
> |
public void testElement() { |
427 |
|
SynchronousQueue q = new SynchronousQueue(); |
428 |
|
try { |
429 |
|
q.element(); |
430 |
< |
fail("no such element"); |
431 |
< |
} |
275 |
< |
catch (NoSuchElementException success) {} |
430 |
> |
shouldThrow(); |
431 |
> |
} catch (NoSuchElementException success) {} |
432 |
|
} |
433 |
|
|
434 |
< |
public void testRemove(){ |
434 |
> |
/** |
435 |
> |
* remove throws NSEE if no active taker |
436 |
> |
*/ |
437 |
> |
public void testRemove() { |
438 |
|
SynchronousQueue q = new SynchronousQueue(); |
439 |
|
try { |
440 |
|
q.remove(); |
441 |
< |
fail("remove should throw"); |
442 |
< |
} catch (NoSuchElementException success){ |
284 |
< |
} |
441 |
> |
shouldThrow(); |
442 |
> |
} catch (NoSuchElementException success) {} |
443 |
|
} |
444 |
|
|
445 |
< |
public void testRemoveElement(){ |
445 |
> |
/** |
446 |
> |
* remove(x) returns false |
447 |
> |
*/ |
448 |
> |
public void testRemoveElement() { |
449 |
|
SynchronousQueue q = new SynchronousQueue(); |
450 |
< |
for (int i = 1; i < N; i+=2) { |
290 |
< |
assertFalse(q.remove(new Integer(i))); |
291 |
< |
} |
450 |
> |
assertFalse(q.remove(zero)); |
451 |
|
assertTrue(q.isEmpty()); |
452 |
|
} |
453 |
< |
|
454 |
< |
public void testContains(){ |
455 |
< |
SynchronousQueue q = new SynchronousQueue(); |
456 |
< |
for (int i = 0; i < N; ++i) { |
457 |
< |
assertFalse(q.contains(new Integer(i))); |
458 |
< |
} |
453 |
> |
|
454 |
> |
/** |
455 |
> |
* contains returns false |
456 |
> |
*/ |
457 |
> |
public void testContains() { |
458 |
> |
SynchronousQueue q = new SynchronousQueue(); |
459 |
> |
assertFalse(q.contains(zero)); |
460 |
|
} |
461 |
|
|
462 |
< |
public void testClear(){ |
462 |
> |
/** |
463 |
> |
* clear ensures isEmpty |
464 |
> |
*/ |
465 |
> |
public void testClear() { |
466 |
|
SynchronousQueue q = new SynchronousQueue(); |
467 |
|
q.clear(); |
468 |
|
assertTrue(q.isEmpty()); |
469 |
|
} |
470 |
|
|
471 |
< |
public void testContainsAll(){ |
471 |
> |
/** |
472 |
> |
* containsAll returns false unless empty |
473 |
> |
*/ |
474 |
> |
public void testContainsAll() { |
475 |
|
SynchronousQueue q = new SynchronousQueue(); |
476 |
|
Integer[] empty = new Integer[0]; |
477 |
< |
Integer[] ints = new Integer[1]; ints[0] = new Integer(0); |
478 |
< |
// assertTrue(q.containsAll(Arrays.asList(empty))); |
477 |
> |
assertTrue(q.containsAll(Arrays.asList(empty))); |
478 |
> |
Integer[] ints = new Integer[1]; ints[0] = zero; |
479 |
|
assertFalse(q.containsAll(Arrays.asList(ints))); |
480 |
|
} |
481 |
|
|
482 |
< |
public void testRetainAll(){ |
482 |
> |
/** |
483 |
> |
* retainAll returns false |
484 |
> |
*/ |
485 |
> |
public void testRetainAll() { |
486 |
|
SynchronousQueue q = new SynchronousQueue(); |
487 |
|
Integer[] empty = new Integer[0]; |
488 |
< |
Integer[] ints = new Integer[1]; ints[0] = new Integer(0); |
489 |
< |
q.retainAll(Arrays.asList(ints)); |
490 |
< |
// assertTrue(q.containsAll(Arrays.asList(empty))); |
322 |
< |
assertFalse(q.containsAll(Arrays.asList(ints))); |
488 |
> |
assertFalse(q.retainAll(Arrays.asList(empty))); |
489 |
> |
Integer[] ints = new Integer[1]; ints[0] = zero; |
490 |
> |
assertFalse(q.retainAll(Arrays.asList(ints))); |
491 |
|
} |
492 |
|
|
493 |
< |
public void testRemoveAll(){ |
493 |
> |
/** |
494 |
> |
* removeAll returns false |
495 |
> |
*/ |
496 |
> |
public void testRemoveAll() { |
497 |
|
SynchronousQueue q = new SynchronousQueue(); |
498 |
|
Integer[] empty = new Integer[0]; |
499 |
< |
Integer[] ints = new Integer[1]; ints[0] = new Integer(0); |
500 |
< |
q.removeAll(Arrays.asList(ints)); |
330 |
< |
// assertTrue(q.containsAll(Arrays.asList(empty))); |
499 |
> |
assertFalse(q.removeAll(Arrays.asList(empty))); |
500 |
> |
Integer[] ints = new Integer[1]; ints[0] = zero; |
501 |
|
assertFalse(q.containsAll(Arrays.asList(ints))); |
502 |
|
} |
503 |
|
|
504 |
|
|
505 |
< |
public void testToArray(){ |
505 |
> |
/** |
506 |
> |
* toArray is empty |
507 |
> |
*/ |
508 |
> |
public void testToArray() { |
509 |
|
SynchronousQueue q = new SynchronousQueue(); |
510 |
< |
Object[] o = q.toArray(); |
510 |
> |
Object[] o = q.toArray(); |
511 |
|
assertEquals(o.length, 0); |
512 |
|
} |
513 |
|
|
514 |
< |
public void testToArray2(){ |
514 |
> |
/** |
515 |
> |
* toArray(a) is nulled at position 0 |
516 |
> |
*/ |
517 |
> |
public void testToArray2() { |
518 |
|
SynchronousQueue q = new SynchronousQueue(); |
519 |
< |
Integer[] ints = new Integer[1]; |
519 |
> |
Integer[] ints = new Integer[1]; |
520 |
|
assertNull(ints[0]); |
521 |
|
} |
522 |
< |
|
523 |
< |
public void testIterator(){ |
522 |
> |
|
523 |
> |
/** |
524 |
> |
* toArray(null) throws NPE |
525 |
> |
*/ |
526 |
> |
public void testToArray_BadArg() { |
527 |
|
SynchronousQueue q = new SynchronousQueue(); |
528 |
< |
Iterator it = q.iterator(); |
528 |
> |
try { |
529 |
> |
Object o[] = q.toArray(null); |
530 |
> |
shouldThrow(); |
531 |
> |
} catch (NullPointerException success) {} |
532 |
> |
} |
533 |
> |
|
534 |
> |
|
535 |
> |
/** |
536 |
> |
* iterator does not traverse any elements |
537 |
> |
*/ |
538 |
> |
public void testIterator() { |
539 |
> |
SynchronousQueue q = new SynchronousQueue(); |
540 |
> |
Iterator it = q.iterator(); |
541 |
|
assertFalse(it.hasNext()); |
542 |
|
try { |
543 |
|
Object x = it.next(); |
544 |
< |
fail("should throw"); |
545 |
< |
} |
355 |
< |
catch (NoSuchElementException success) {} |
544 |
> |
shouldThrow(); |
545 |
> |
} catch (NoSuchElementException success) {} |
546 |
|
} |
547 |
|
|
548 |
< |
public void testIteratorRemove(){ |
548 |
> |
/** |
549 |
> |
* iterator remove throws ISE |
550 |
> |
*/ |
551 |
> |
public void testIteratorRemove() { |
552 |
|
SynchronousQueue q = new SynchronousQueue(); |
553 |
< |
Iterator it = q.iterator(); |
553 |
> |
Iterator it = q.iterator(); |
554 |
|
try { |
555 |
|
it.remove(); |
556 |
< |
fail("should throw"); |
557 |
< |
} |
365 |
< |
catch (IllegalStateException success) {} |
556 |
> |
shouldThrow(); |
557 |
> |
} catch (IllegalStateException success) {} |
558 |
|
} |
559 |
|
|
560 |
< |
public void testToString(){ |
560 |
> |
/** |
561 |
> |
* toString returns a non-null string |
562 |
> |
*/ |
563 |
> |
public void testToString() { |
564 |
|
SynchronousQueue q = new SynchronousQueue(); |
565 |
|
String s = q.toString(); |
566 |
< |
assertTrue(s != null); |
567 |
< |
} |
566 |
> |
assertNotNull(s); |
567 |
> |
} |
568 |
|
|
569 |
|
|
570 |
+ |
/** |
571 |
+ |
* offer transfers elements across Executor tasks |
572 |
+ |
*/ |
573 |
|
public void testOfferInExecutor() { |
574 |
|
final SynchronousQueue q = new SynchronousQueue(); |
575 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
576 |
|
final Integer one = new Integer(1); |
577 |
|
|
578 |
< |
executor.execute(new Runnable() { |
579 |
< |
public void run() { |
580 |
< |
assertFalse(q.offer(one)); |
581 |
< |
try { |
582 |
< |
assertTrue(q.offer(one, MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); |
583 |
< |
assertEquals(0, q.remainingCapacity()); |
584 |
< |
} |
585 |
< |
catch (InterruptedException e) { |
586 |
< |
fail("should not be interrupted"); |
587 |
< |
} |
588 |
< |
} |
589 |
< |
}); |
590 |
< |
|
591 |
< |
executor.execute(new Runnable() { |
394 |
< |
public void run() { |
395 |
< |
try { |
396 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
397 |
< |
assertEquals(one, q.take()); |
398 |
< |
} |
399 |
< |
catch (InterruptedException e) { |
400 |
< |
fail("should not be interrupted"); |
401 |
< |
} |
402 |
< |
} |
403 |
< |
}); |
404 |
< |
|
405 |
< |
executor.shutdown(); |
578 |
> |
executor.execute(new CheckedRunnable() { |
579 |
> |
public void realRun() throws InterruptedException { |
580 |
> |
threadAssertFalse(q.offer(one)); |
581 |
> |
threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); |
582 |
> |
threadAssertEquals(0, q.remainingCapacity()); |
583 |
> |
}}); |
584 |
> |
|
585 |
> |
executor.execute(new CheckedRunnable() { |
586 |
> |
public void realRun() throws InterruptedException { |
587 |
> |
Thread.sleep(SMALL_DELAY_MS); |
588 |
> |
threadAssertEquals(one, q.take()); |
589 |
> |
}}); |
590 |
> |
|
591 |
> |
joinPool(executor); |
592 |
|
|
593 |
|
} |
594 |
|
|
595 |
+ |
/** |
596 |
+ |
* poll retrieves elements across Executor threads |
597 |
+ |
*/ |
598 |
|
public void testPollInExecutor() { |
410 |
– |
|
599 |
|
final SynchronousQueue q = new SynchronousQueue(); |
412 |
– |
|
600 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
601 |
+ |
executor.execute(new CheckedRunnable() { |
602 |
+ |
public void realRun() throws InterruptedException { |
603 |
+ |
threadAssertNull(q.poll()); |
604 |
+ |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
605 |
+ |
threadAssertTrue(q.isEmpty()); |
606 |
+ |
}}); |
607 |
|
|
608 |
< |
executor.execute(new Runnable() { |
609 |
< |
public void run() { |
610 |
< |
assertNull(q.poll()); |
611 |
< |
try { |
612 |
< |
assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); |
420 |
< |
assertTrue(q.isEmpty()); |
421 |
< |
} |
422 |
< |
catch (InterruptedException e) { |
423 |
< |
fail("should not be interrupted"); |
424 |
< |
} |
425 |
< |
} |
426 |
< |
}); |
608 |
> |
executor.execute(new CheckedRunnable() { |
609 |
> |
public void realRun() throws InterruptedException { |
610 |
> |
Thread.sleep(SMALL_DELAY_MS); |
611 |
> |
q.put(new Integer(1)); |
612 |
> |
}}); |
613 |
|
|
614 |
< |
executor.execute(new Runnable() { |
615 |
< |
public void run() { |
616 |
< |
try { |
617 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
618 |
< |
q.put(new Integer(1)); |
619 |
< |
} |
620 |
< |
catch (InterruptedException e) { |
621 |
< |
fail("should not be interrupted"); |
622 |
< |
} |
623 |
< |
} |
624 |
< |
}); |
625 |
< |
|
626 |
< |
executor.shutdown(); |
614 |
> |
joinPool(executor); |
615 |
> |
} |
616 |
> |
|
617 |
> |
/** |
618 |
> |
* a deserialized serialized queue is usable |
619 |
> |
*/ |
620 |
> |
public void testSerialization() throws Exception { |
621 |
> |
SynchronousQueue q = new SynchronousQueue(); |
622 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
623 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
624 |
> |
out.writeObject(q); |
625 |
> |
out.close(); |
626 |
> |
|
627 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
628 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
629 |
> |
SynchronousQueue r = (SynchronousQueue)in.readObject(); |
630 |
> |
assertEquals(q.size(), r.size()); |
631 |
> |
while (!q.isEmpty()) |
632 |
> |
assertEquals(q.remove(), r.remove()); |
633 |
> |
} |
634 |
|
|
635 |
+ |
/** |
636 |
+ |
* drainTo(null) throws NPE |
637 |
+ |
*/ |
638 |
+ |
public void testDrainToNull() { |
639 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
640 |
+ |
try { |
641 |
+ |
q.drainTo(null); |
642 |
+ |
shouldThrow(); |
643 |
+ |
} catch (NullPointerException success) {} |
644 |
+ |
} |
645 |
+ |
|
646 |
+ |
/** |
647 |
+ |
* drainTo(this) throws IAE |
648 |
+ |
*/ |
649 |
+ |
public void testDrainToSelf() { |
650 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
651 |
+ |
try { |
652 |
+ |
q.drainTo(q); |
653 |
+ |
shouldThrow(); |
654 |
+ |
} catch (IllegalArgumentException success) {} |
655 |
|
} |
656 |
|
|
657 |
< |
public void testSerialization() { |
658 |
< |
SynchronousQueue q = new SynchronousQueue(); |
659 |
< |
try { |
660 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
661 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
662 |
< |
out.writeObject(q); |
663 |
< |
out.close(); |
664 |
< |
|
665 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
666 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
667 |
< |
SynchronousQueue r = (SynchronousQueue)in.readObject(); |
668 |
< |
assertEquals(q.size(), r.size()); |
669 |
< |
while (!q.isEmpty()) |
670 |
< |
assertEquals(q.remove(), r.remove()); |
671 |
< |
} catch(Exception e){ |
672 |
< |
fail("unexpected exception"); |
673 |
< |
} |
657 |
> |
/** |
658 |
> |
* drainTo(c) of empty queue doesn't transfer elements |
659 |
> |
*/ |
660 |
> |
public void testDrainTo() { |
661 |
> |
SynchronousQueue q = new SynchronousQueue(); |
662 |
> |
ArrayList l = new ArrayList(); |
663 |
> |
q.drainTo(l); |
664 |
> |
assertEquals(q.size(), 0); |
665 |
> |
assertEquals(l.size(), 0); |
666 |
> |
} |
667 |
> |
|
668 |
> |
/** |
669 |
> |
* drainTo empties queue, unblocking a waiting put. |
670 |
> |
*/ |
671 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
672 |
> |
final SynchronousQueue q = new SynchronousQueue(); |
673 |
> |
Thread t = new Thread(new CheckedRunnable() { |
674 |
> |
public void realRun() throws InterruptedException { |
675 |
> |
q.put(new Integer(1)); |
676 |
> |
}}); |
677 |
> |
|
678 |
> |
t.start(); |
679 |
> |
ArrayList l = new ArrayList(); |
680 |
> |
Thread.sleep(SHORT_DELAY_MS); |
681 |
> |
q.drainTo(l); |
682 |
> |
assertTrue(l.size() <= 1); |
683 |
> |
if (l.size() > 0) |
684 |
> |
assertEquals(l.get(0), new Integer(1)); |
685 |
> |
t.join(); |
686 |
> |
assertTrue(l.size() <= 1); |
687 |
> |
} |
688 |
> |
|
689 |
> |
/** |
690 |
> |
* drainTo(null, n) throws NPE |
691 |
> |
*/ |
692 |
> |
public void testDrainToNullN() { |
693 |
> |
SynchronousQueue q = new SynchronousQueue(); |
694 |
> |
try { |
695 |
> |
q.drainTo(null, 0); |
696 |
> |
shouldThrow(); |
697 |
> |
} catch (NullPointerException success) {} |
698 |
> |
} |
699 |
> |
|
700 |
> |
/** |
701 |
> |
* drainTo(this, n) throws IAE |
702 |
> |
*/ |
703 |
> |
public void testDrainToSelfN() { |
704 |
> |
SynchronousQueue q = new SynchronousQueue(); |
705 |
> |
try { |
706 |
> |
q.drainTo(q, 0); |
707 |
> |
shouldThrow(); |
708 |
> |
} catch (IllegalArgumentException success) {} |
709 |
> |
} |
710 |
> |
|
711 |
> |
/** |
712 |
> |
* drainTo(c, n) empties up to n elements of queue into c |
713 |
> |
*/ |
714 |
> |
public void testDrainToN() throws InterruptedException { |
715 |
> |
final SynchronousQueue q = new SynchronousQueue(); |
716 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
717 |
> |
public void realRun() throws InterruptedException { |
718 |
> |
q.put(one); |
719 |
> |
}}); |
720 |
> |
|
721 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
722 |
> |
public void realRun() throws InterruptedException { |
723 |
> |
q.put(two); |
724 |
> |
}}); |
725 |
> |
|
726 |
> |
t1.start(); |
727 |
> |
t2.start(); |
728 |
> |
ArrayList l = new ArrayList(); |
729 |
> |
Thread.sleep(SHORT_DELAY_MS); |
730 |
> |
q.drainTo(l, 1); |
731 |
> |
assertTrue(l.size() == 1); |
732 |
> |
q.drainTo(l, 1); |
733 |
> |
assertTrue(l.size() == 2); |
734 |
> |
assertTrue(l.contains(one)); |
735 |
> |
assertTrue(l.contains(two)); |
736 |
> |
t1.join(); |
737 |
> |
t2.join(); |
738 |
|
} |
739 |
|
|
740 |
|
} |