1 |
dl |
1.1 |
/* |
2 |
dl |
1.7 |
* 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 |
jsr166 |
1.10 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
import junit.framework.*; |
10 |
|
|
import java.util.*; |
11 |
|
|
import java.util.concurrent.*; |
12 |
jsr166 |
1.13 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
dl |
1.2 |
import java.io.*; |
14 |
dl |
1.1 |
|
15 |
dl |
1.3 |
public class SynchronousQueueTest extends JSR166TestCase { |
16 |
dl |
1.1 |
|
17 |
|
|
public static void main(String[] args) { |
18 |
jsr166 |
1.14 |
junit.textui.TestRunner.run (suite()); |
19 |
dl |
1.1 |
} |
20 |
|
|
|
21 |
|
|
public static Test suite() { |
22 |
jsr166 |
1.14 |
return new TestSuite(SynchronousQueueTest.class); |
23 |
dl |
1.1 |
} |
24 |
|
|
|
25 |
dl |
1.4 |
/** |
26 |
dl |
1.5 |
* A SynchronousQueue is both empty and full |
27 |
dl |
1.4 |
*/ |
28 |
dl |
1.1 |
public void testEmptyFull() { |
29 |
|
|
SynchronousQueue q = new SynchronousQueue(); |
30 |
|
|
assertTrue(q.isEmpty()); |
31 |
jsr166 |
1.14 |
assertEquals(0, q.size()); |
32 |
dl |
1.1 |
assertEquals(0, q.remainingCapacity()); |
33 |
dl |
1.5 |
assertFalse(q.offer(zero)); |
34 |
dl |
1.1 |
} |
35 |
|
|
|
36 |
dl |
1.4 |
/** |
37 |
dl |
1.8 |
* A fair SynchronousQueue is both empty and full |
38 |
|
|
*/ |
39 |
|
|
public void testFairEmptyFull() { |
40 |
|
|
SynchronousQueue q = new SynchronousQueue(true); |
41 |
|
|
assertTrue(q.isEmpty()); |
42 |
jsr166 |
1.14 |
assertEquals(0, q.size()); |
43 |
dl |
1.8 |
assertEquals(0, q.remainingCapacity()); |
44 |
|
|
assertFalse(q.offer(zero)); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
/** |
48 |
dl |
1.5 |
* offer(null) throws NPE |
49 |
dl |
1.4 |
*/ |
50 |
|
|
public void testOfferNull() { |
51 |
jsr166 |
1.14 |
try { |
52 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
53 |
|
|
q.offer(null); |
54 |
dl |
1.4 |
shouldThrow(); |
55 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
56 |
dl |
1.1 |
} |
57 |
|
|
|
58 |
dl |
1.4 |
/** |
59 |
dl |
1.6 |
* add(null) throws NPE |
60 |
|
|
*/ |
61 |
|
|
public void testAddNull() { |
62 |
jsr166 |
1.14 |
try { |
63 |
dl |
1.6 |
SynchronousQueue q = new SynchronousQueue(); |
64 |
|
|
q.add(null); |
65 |
|
|
shouldThrow(); |
66 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
67 |
dl |
1.6 |
} |
68 |
|
|
|
69 |
|
|
/** |
70 |
dl |
1.5 |
* offer fails if no active taker |
71 |
dl |
1.4 |
*/ |
72 |
|
|
public void testOffer() { |
73 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
74 |
dl |
1.5 |
assertFalse(q.offer(one)); |
75 |
dl |
1.1 |
} |
76 |
|
|
|
77 |
dl |
1.4 |
/** |
78 |
dl |
1.5 |
* add throws ISE if no active taker |
79 |
dl |
1.4 |
*/ |
80 |
|
|
public void testAdd() { |
81 |
jsr166 |
1.14 |
try { |
82 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
83 |
|
|
assertEquals(0, q.remainingCapacity()); |
84 |
dl |
1.5 |
q.add(one); |
85 |
|
|
shouldThrow(); |
86 |
jsr166 |
1.13 |
} catch (IllegalStateException success) {} |
87 |
dl |
1.1 |
} |
88 |
|
|
|
89 |
dl |
1.4 |
/** |
90 |
dl |
1.5 |
* addAll(null) throws NPE |
91 |
dl |
1.4 |
*/ |
92 |
|
|
public void testAddAll1() { |
93 |
dl |
1.1 |
try { |
94 |
|
|
SynchronousQueue q = new SynchronousQueue(); |
95 |
|
|
q.addAll(null); |
96 |
dl |
1.4 |
shouldThrow(); |
97 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
98 |
dl |
1.1 |
} |
99 |
dl |
1.6 |
|
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 |
jsr166 |
1.13 |
} catch (IllegalArgumentException success) {} |
109 |
dl |
1.6 |
} |
110 |
|
|
|
111 |
dl |
1.4 |
/** |
112 |
dl |
1.5 |
* addAll of a collection with null elements throws NPE |
113 |
dl |
1.4 |
*/ |
114 |
|
|
public void testAddAll2() { |
115 |
dl |
1.1 |
try { |
116 |
|
|
SynchronousQueue q = new SynchronousQueue(); |
117 |
dl |
1.3 |
Integer[] ints = new Integer[1]; |
118 |
dl |
1.1 |
q.addAll(Arrays.asList(ints)); |
119 |
dl |
1.4 |
shouldThrow(); |
120 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
121 |
dl |
1.1 |
} |
122 |
dl |
1.4 |
/** |
123 |
dl |
1.5 |
* addAll throws ISE if no active taker |
124 |
dl |
1.4 |
*/ |
125 |
|
|
public void testAddAll4() { |
126 |
dl |
1.1 |
try { |
127 |
|
|
SynchronousQueue q = new SynchronousQueue(); |
128 |
dl |
1.3 |
Integer[] ints = new Integer[1]; |
129 |
|
|
for (int i = 0; i < 1; ++i) |
130 |
dl |
1.1 |
ints[i] = new Integer(i); |
131 |
|
|
q.addAll(Arrays.asList(ints)); |
132 |
dl |
1.4 |
shouldThrow(); |
133 |
jsr166 |
1.13 |
} catch (IllegalStateException success) {} |
134 |
dl |
1.1 |
} |
135 |
|
|
|
136 |
dl |
1.4 |
/** |
137 |
dl |
1.5 |
* put(null) throws NPE |
138 |
dl |
1.4 |
*/ |
139 |
jsr166 |
1.13 |
public void testPutNull() throws InterruptedException { |
140 |
jsr166 |
1.14 |
try { |
141 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
142 |
|
|
q.put(null); |
143 |
dl |
1.4 |
shouldThrow(); |
144 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
145 |
dl |
1.1 |
} |
146 |
|
|
|
147 |
dl |
1.4 |
/** |
148 |
dl |
1.5 |
* put blocks interruptibly if no active taker |
149 |
dl |
1.4 |
*/ |
150 |
jsr166 |
1.13 |
public void testBlockingPut() throws InterruptedException { |
151 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
152 |
|
|
public void realRun() throws InterruptedException { |
153 |
jsr166 |
1.13 |
SynchronousQueue q = new SynchronousQueue(); |
154 |
|
|
q.put(zero); |
155 |
|
|
}}); |
156 |
|
|
|
157 |
dl |
1.1 |
t.start(); |
158 |
jsr166 |
1.13 |
Thread.sleep(SHORT_DELAY_MS); |
159 |
|
|
t.interrupt(); |
160 |
|
|
t.join(); |
161 |
dl |
1.1 |
} |
162 |
|
|
|
163 |
dl |
1.4 |
/** |
164 |
jsr166 |
1.10 |
* put blocks waiting for take |
165 |
dl |
1.4 |
*/ |
166 |
jsr166 |
1.13 |
public void testPutWithTake() throws InterruptedException { |
167 |
dl |
1.1 |
final SynchronousQueue q = new SynchronousQueue(); |
168 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedRunnable() { |
169 |
|
|
public void realRun() throws InterruptedException { |
170 |
jsr166 |
1.13 |
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 |
dl |
1.1 |
} |
184 |
jsr166 |
1.13 |
}}); |
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 |
dl |
1.1 |
} |
193 |
|
|
|
194 |
dl |
1.4 |
/** |
195 |
dl |
1.5 |
* timed offer times out if elements not taken |
196 |
dl |
1.4 |
*/ |
197 |
jsr166 |
1.13 |
public void testTimedOffer() throws InterruptedException { |
198 |
dl |
1.1 |
final SynchronousQueue q = new SynchronousQueue(); |
199 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
200 |
|
|
public void realRun() throws InterruptedException { |
201 |
jsr166 |
1.17 |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
202 |
jsr166 |
1.13 |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
203 |
|
|
}}); |
204 |
jsr166 |
1.10 |
|
205 |
jsr166 |
1.13 |
t.start(); |
206 |
|
|
Thread.sleep(SMALL_DELAY_MS); |
207 |
|
|
t.interrupt(); |
208 |
|
|
t.join(); |
209 |
dl |
1.1 |
} |
210 |
|
|
|
211 |
|
|
|
212 |
dl |
1.4 |
/** |
213 |
dl |
1.5 |
* take blocks interruptibly when empty |
214 |
dl |
1.4 |
*/ |
215 |
jsr166 |
1.13 |
public void testTakeFromEmpty() throws InterruptedException { |
216 |
dl |
1.1 |
final SynchronousQueue q = new SynchronousQueue(); |
217 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
218 |
|
|
public void realRun() throws InterruptedException { |
219 |
jsr166 |
1.13 |
q.take(); |
220 |
|
|
}}); |
221 |
|
|
|
222 |
|
|
t.start(); |
223 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
224 |
|
|
t.interrupt(); |
225 |
|
|
t.join(); |
226 |
dl |
1.1 |
} |
227 |
|
|
|
228 |
dl |
1.8 |
|
229 |
|
|
/** |
230 |
|
|
* put blocks interruptibly if no active taker |
231 |
|
|
*/ |
232 |
jsr166 |
1.13 |
public void testFairBlockingPut() throws InterruptedException { |
233 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
234 |
|
|
public void realRun() throws InterruptedException { |
235 |
jsr166 |
1.13 |
SynchronousQueue q = new SynchronousQueue(true); |
236 |
|
|
q.put(zero); |
237 |
|
|
}}); |
238 |
|
|
|
239 |
dl |
1.8 |
t.start(); |
240 |
jsr166 |
1.13 |
Thread.sleep(SHORT_DELAY_MS); |
241 |
|
|
t.interrupt(); |
242 |
|
|
t.join(); |
243 |
dl |
1.8 |
} |
244 |
|
|
|
245 |
|
|
/** |
246 |
jsr166 |
1.10 |
* put blocks waiting for take |
247 |
dl |
1.8 |
*/ |
248 |
jsr166 |
1.13 |
public void testFairPutWithTake() throws InterruptedException { |
249 |
dl |
1.8 |
final SynchronousQueue q = new SynchronousQueue(true); |
250 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedRunnable() { |
251 |
|
|
public void realRun() throws InterruptedException { |
252 |
jsr166 |
1.13 |
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 |
dl |
1.8 |
} |
266 |
jsr166 |
1.13 |
}}); |
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 |
dl |
1.8 |
} |
275 |
|
|
|
276 |
|
|
/** |
277 |
|
|
* timed offer times out if elements not taken |
278 |
|
|
*/ |
279 |
jsr166 |
1.13 |
public void testFairTimedOffer() throws InterruptedException { |
280 |
dl |
1.8 |
final SynchronousQueue q = new SynchronousQueue(true); |
281 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
282 |
|
|
public void realRun() throws InterruptedException { |
283 |
jsr166 |
1.13 |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
284 |
|
|
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
285 |
|
|
}}); |
286 |
jsr166 |
1.10 |
|
287 |
jsr166 |
1.13 |
t.start(); |
288 |
|
|
Thread.sleep(SMALL_DELAY_MS); |
289 |
|
|
t.interrupt(); |
290 |
|
|
t.join(); |
291 |
dl |
1.8 |
} |
292 |
|
|
|
293 |
|
|
|
294 |
|
|
/** |
295 |
|
|
* take blocks interruptibly when empty |
296 |
|
|
*/ |
297 |
jsr166 |
1.13 |
public void testFairTakeFromEmpty() throws InterruptedException { |
298 |
dl |
1.8 |
final SynchronousQueue q = new SynchronousQueue(true); |
299 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
300 |
|
|
public void realRun() throws InterruptedException { |
301 |
jsr166 |
1.13 |
q.take(); |
302 |
|
|
}}); |
303 |
|
|
|
304 |
|
|
t.start(); |
305 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
306 |
|
|
t.interrupt(); |
307 |
|
|
t.join(); |
308 |
dl |
1.8 |
} |
309 |
|
|
|
310 |
dl |
1.4 |
/** |
311 |
dl |
1.5 |
* poll fails unless active taker |
312 |
dl |
1.4 |
*/ |
313 |
|
|
public void testPoll() { |
314 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
315 |
jsr166 |
1.14 |
assertNull(q.poll()); |
316 |
dl |
1.1 |
} |
317 |
|
|
|
318 |
dl |
1.4 |
/** |
319 |
dl |
1.5 |
* timed pool with zero timeout times out if no active taker |
320 |
dl |
1.4 |
*/ |
321 |
jsr166 |
1.13 |
public void testTimedPoll0() throws InterruptedException { |
322 |
|
|
SynchronousQueue q = new SynchronousQueue(); |
323 |
|
|
assertNull(q.poll(0, MILLISECONDS)); |
324 |
dl |
1.1 |
} |
325 |
|
|
|
326 |
dl |
1.4 |
/** |
327 |
dl |
1.5 |
* timed pool with nonzero timeout times out if no active taker |
328 |
dl |
1.4 |
*/ |
329 |
jsr166 |
1.13 |
public void testTimedPoll() throws InterruptedException { |
330 |
|
|
SynchronousQueue q = new SynchronousQueue(); |
331 |
|
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
332 |
dl |
1.1 |
} |
333 |
|
|
|
334 |
dl |
1.4 |
/** |
335 |
dl |
1.5 |
* Interrupted timed poll throws InterruptedException instead of |
336 |
|
|
* returning timeout status |
337 |
dl |
1.4 |
*/ |
338 |
jsr166 |
1.13 |
public void testInterruptedTimedPoll() throws InterruptedException { |
339 |
jsr166 |
1.15 |
final SynchronousQueue q = new SynchronousQueue(); |
340 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
341 |
|
|
public void realRun() throws InterruptedException { |
342 |
jsr166 |
1.13 |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
343 |
|
|
}}); |
344 |
|
|
|
345 |
dl |
1.1 |
t.start(); |
346 |
jsr166 |
1.13 |
Thread.sleep(SHORT_DELAY_MS); |
347 |
|
|
t.interrupt(); |
348 |
|
|
t.join(); |
349 |
dl |
1.1 |
} |
350 |
|
|
|
351 |
dl |
1.4 |
/** |
352 |
dl |
1.5 |
* timed poll before a delayed offer fails; after offer succeeds; |
353 |
|
|
* on interruption throws |
354 |
dl |
1.4 |
*/ |
355 |
jsr166 |
1.13 |
public void testTimedPollWithOffer() throws InterruptedException { |
356 |
dl |
1.1 |
final SynchronousQueue q = new SynchronousQueue(); |
357 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedRunnable() { |
358 |
|
|
public void realRun() throws InterruptedException { |
359 |
jsr166 |
1.16 |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
360 |
jsr166 |
1.13 |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
361 |
|
|
try { |
362 |
|
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
363 |
jsr166 |
1.16 |
shouldThrow(); |
364 |
jsr166 |
1.13 |
} 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 |
jsr166 |
1.10 |
} |
373 |
dl |
1.8 |
|
374 |
|
|
/** |
375 |
|
|
* Interrupted timed poll throws InterruptedException instead of |
376 |
|
|
* returning timeout status |
377 |
|
|
*/ |
378 |
jsr166 |
1.13 |
public void testFairInterruptedTimedPoll() throws InterruptedException { |
379 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
380 |
|
|
public void realRun() throws InterruptedException { |
381 |
jsr166 |
1.13 |
SynchronousQueue q = new SynchronousQueue(true); |
382 |
|
|
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
383 |
|
|
}}); |
384 |
|
|
|
385 |
dl |
1.8 |
t.start(); |
386 |
jsr166 |
1.13 |
Thread.sleep(SHORT_DELAY_MS); |
387 |
|
|
t.interrupt(); |
388 |
|
|
t.join(); |
389 |
dl |
1.8 |
} |
390 |
|
|
|
391 |
|
|
/** |
392 |
|
|
* timed poll before a delayed offer fails; after offer succeeds; |
393 |
|
|
* on interruption throws |
394 |
|
|
*/ |
395 |
jsr166 |
1.13 |
public void testFairTimedPollWithOffer() throws InterruptedException { |
396 |
dl |
1.8 |
final SynchronousQueue q = new SynchronousQueue(true); |
397 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedRunnable() { |
398 |
|
|
public void realRun() throws InterruptedException { |
399 |
jsr166 |
1.16 |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
400 |
jsr166 |
1.13 |
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 |
jsr166 |
1.10 |
} |
413 |
dl |
1.1 |
|
414 |
|
|
|
415 |
dl |
1.4 |
/** |
416 |
dl |
1.5 |
* peek returns null |
417 |
dl |
1.4 |
*/ |
418 |
|
|
public void testPeek() { |
419 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
420 |
jsr166 |
1.14 |
assertNull(q.peek()); |
421 |
dl |
1.1 |
} |
422 |
|
|
|
423 |
dl |
1.4 |
/** |
424 |
dl |
1.5 |
* element throws NSEE |
425 |
dl |
1.4 |
*/ |
426 |
|
|
public void testElement() { |
427 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
428 |
|
|
try { |
429 |
|
|
q.element(); |
430 |
dl |
1.4 |
shouldThrow(); |
431 |
jsr166 |
1.13 |
} catch (NoSuchElementException success) {} |
432 |
dl |
1.1 |
} |
433 |
|
|
|
434 |
dl |
1.4 |
/** |
435 |
dl |
1.5 |
* remove throws NSEE if no active taker |
436 |
dl |
1.4 |
*/ |
437 |
|
|
public void testRemove() { |
438 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
439 |
|
|
try { |
440 |
|
|
q.remove(); |
441 |
dl |
1.4 |
shouldThrow(); |
442 |
jsr166 |
1.16 |
} catch (NoSuchElementException success) {} |
443 |
dl |
1.1 |
} |
444 |
|
|
|
445 |
dl |
1.4 |
/** |
446 |
dl |
1.5 |
* remove(x) returns false |
447 |
dl |
1.4 |
*/ |
448 |
|
|
public void testRemoveElement() { |
449 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
450 |
dl |
1.5 |
assertFalse(q.remove(zero)); |
451 |
dl |
1.2 |
assertTrue(q.isEmpty()); |
452 |
dl |
1.1 |
} |
453 |
jsr166 |
1.10 |
|
454 |
dl |
1.4 |
/** |
455 |
dl |
1.5 |
* contains returns false |
456 |
dl |
1.4 |
*/ |
457 |
|
|
public void testContains() { |
458 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
459 |
dl |
1.5 |
assertFalse(q.contains(zero)); |
460 |
dl |
1.1 |
} |
461 |
|
|
|
462 |
dl |
1.4 |
/** |
463 |
dl |
1.5 |
* clear ensures isEmpty |
464 |
dl |
1.4 |
*/ |
465 |
|
|
public void testClear() { |
466 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
467 |
|
|
q.clear(); |
468 |
|
|
assertTrue(q.isEmpty()); |
469 |
|
|
} |
470 |
|
|
|
471 |
dl |
1.4 |
/** |
472 |
dl |
1.5 |
* containsAll returns false unless empty |
473 |
dl |
1.4 |
*/ |
474 |
|
|
public void testContainsAll() { |
475 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
476 |
|
|
Integer[] empty = new Integer[0]; |
477 |
dl |
1.5 |
assertTrue(q.containsAll(Arrays.asList(empty))); |
478 |
|
|
Integer[] ints = new Integer[1]; ints[0] = zero; |
479 |
dl |
1.1 |
assertFalse(q.containsAll(Arrays.asList(ints))); |
480 |
|
|
} |
481 |
|
|
|
482 |
dl |
1.4 |
/** |
483 |
dl |
1.5 |
* retainAll returns false |
484 |
dl |
1.4 |
*/ |
485 |
|
|
public void testRetainAll() { |
486 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
487 |
|
|
Integer[] empty = new Integer[0]; |
488 |
dl |
1.5 |
assertFalse(q.retainAll(Arrays.asList(empty))); |
489 |
|
|
Integer[] ints = new Integer[1]; ints[0] = zero; |
490 |
|
|
assertFalse(q.retainAll(Arrays.asList(ints))); |
491 |
dl |
1.1 |
} |
492 |
|
|
|
493 |
dl |
1.4 |
/** |
494 |
dl |
1.5 |
* removeAll returns false |
495 |
dl |
1.4 |
*/ |
496 |
|
|
public void testRemoveAll() { |
497 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
498 |
|
|
Integer[] empty = new Integer[0]; |
499 |
dl |
1.5 |
assertFalse(q.removeAll(Arrays.asList(empty))); |
500 |
|
|
Integer[] ints = new Integer[1]; ints[0] = zero; |
501 |
dl |
1.1 |
assertFalse(q.containsAll(Arrays.asList(ints))); |
502 |
|
|
} |
503 |
|
|
|
504 |
|
|
|
505 |
dl |
1.4 |
/** |
506 |
dl |
1.5 |
* toArray is empty |
507 |
dl |
1.4 |
*/ |
508 |
|
|
public void testToArray() { |
509 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
510 |
jsr166 |
1.14 |
Object[] o = q.toArray(); |
511 |
dl |
1.1 |
assertEquals(o.length, 0); |
512 |
|
|
} |
513 |
|
|
|
514 |
dl |
1.4 |
/** |
515 |
dl |
1.5 |
* toArray(a) is nulled at position 0 |
516 |
dl |
1.4 |
*/ |
517 |
|
|
public void testToArray2() { |
518 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
519 |
jsr166 |
1.14 |
Integer[] ints = new Integer[1]; |
520 |
dl |
1.1 |
assertNull(ints[0]); |
521 |
|
|
} |
522 |
jsr166 |
1.10 |
|
523 |
dl |
1.4 |
/** |
524 |
dl |
1.6 |
* toArray(null) throws NPE |
525 |
|
|
*/ |
526 |
|
|
public void testToArray_BadArg() { |
527 |
jsr166 |
1.18 |
SynchronousQueue q = new SynchronousQueue(); |
528 |
jsr166 |
1.14 |
try { |
529 |
|
|
Object o[] = q.toArray(null); |
530 |
|
|
shouldThrow(); |
531 |
|
|
} catch (NullPointerException success) {} |
532 |
dl |
1.6 |
} |
533 |
|
|
|
534 |
|
|
|
535 |
|
|
/** |
536 |
dl |
1.5 |
* iterator does not traverse any elements |
537 |
dl |
1.4 |
*/ |
538 |
|
|
public void testIterator() { |
539 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
540 |
jsr166 |
1.14 |
Iterator it = q.iterator(); |
541 |
dl |
1.1 |
assertFalse(it.hasNext()); |
542 |
|
|
try { |
543 |
|
|
Object x = it.next(); |
544 |
dl |
1.4 |
shouldThrow(); |
545 |
jsr166 |
1.13 |
} catch (NoSuchElementException success) {} |
546 |
dl |
1.1 |
} |
547 |
|
|
|
548 |
dl |
1.4 |
/** |
549 |
dl |
1.5 |
* iterator remove throws ISE |
550 |
dl |
1.4 |
*/ |
551 |
|
|
public void testIteratorRemove() { |
552 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
553 |
jsr166 |
1.14 |
Iterator it = q.iterator(); |
554 |
dl |
1.1 |
try { |
555 |
|
|
it.remove(); |
556 |
dl |
1.4 |
shouldThrow(); |
557 |
jsr166 |
1.13 |
} catch (IllegalStateException success) {} |
558 |
dl |
1.1 |
} |
559 |
|
|
|
560 |
dl |
1.4 |
/** |
561 |
dl |
1.5 |
* toString returns a non-null string |
562 |
dl |
1.4 |
*/ |
563 |
|
|
public void testToString() { |
564 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
565 |
|
|
String s = q.toString(); |
566 |
dl |
1.5 |
assertNotNull(s); |
567 |
jsr166 |
1.10 |
} |
568 |
dl |
1.1 |
|
569 |
|
|
|
570 |
dl |
1.4 |
/** |
571 |
dl |
1.5 |
* offer transfers elements across Executor tasks |
572 |
dl |
1.4 |
*/ |
573 |
dl |
1.1 |
public void testOfferInExecutor() { |
574 |
|
|
final SynchronousQueue q = new SynchronousQueue(); |
575 |
|
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
576 |
|
|
final Integer one = new Integer(1); |
577 |
|
|
|
578 |
jsr166 |
1.13 |
executor.execute(new CheckedRunnable() { |
579 |
jsr166 |
1.14 |
public void realRun() throws InterruptedException { |
580 |
dl |
1.3 |
threadAssertFalse(q.offer(one)); |
581 |
jsr166 |
1.13 |
threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); |
582 |
|
|
threadAssertEquals(0, q.remainingCapacity()); |
583 |
|
|
}}); |
584 |
|
|
|
585 |
|
|
executor.execute(new CheckedRunnable() { |
586 |
jsr166 |
1.14 |
public void realRun() throws InterruptedException { |
587 |
jsr166 |
1.13 |
Thread.sleep(SMALL_DELAY_MS); |
588 |
|
|
threadAssertEquals(one, q.take()); |
589 |
|
|
}}); |
590 |
jsr166 |
1.10 |
|
591 |
dl |
1.3 |
joinPool(executor); |
592 |
dl |
1.1 |
|
593 |
|
|
} |
594 |
|
|
|
595 |
dl |
1.4 |
/** |
596 |
dl |
1.5 |
* poll retrieves elements across Executor threads |
597 |
dl |
1.4 |
*/ |
598 |
dl |
1.1 |
public void testPollInExecutor() { |
599 |
|
|
final SynchronousQueue q = new SynchronousQueue(); |
600 |
|
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
601 |
jsr166 |
1.13 |
executor.execute(new CheckedRunnable() { |
602 |
jsr166 |
1.14 |
public void realRun() throws InterruptedException { |
603 |
dl |
1.3 |
threadAssertNull(q.poll()); |
604 |
jsr166 |
1.13 |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
605 |
|
|
threadAssertTrue(q.isEmpty()); |
606 |
|
|
}}); |
607 |
|
|
|
608 |
|
|
executor.execute(new CheckedRunnable() { |
609 |
jsr166 |
1.14 |
public void realRun() throws InterruptedException { |
610 |
jsr166 |
1.13 |
Thread.sleep(SMALL_DELAY_MS); |
611 |
|
|
q.put(new Integer(1)); |
612 |
|
|
}}); |
613 |
jsr166 |
1.10 |
|
614 |
dl |
1.3 |
joinPool(executor); |
615 |
dl |
1.2 |
} |
616 |
|
|
|
617 |
dl |
1.4 |
/** |
618 |
dl |
1.5 |
* a deserialized serialized queue is usable |
619 |
dl |
1.4 |
*/ |
620 |
jsr166 |
1.13 |
public void testSerialization() throws Exception { |
621 |
dl |
1.2 |
SynchronousQueue q = new SynchronousQueue(); |
622 |
jsr166 |
1.13 |
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 |
dl |
1.1 |
} |
634 |
dl |
1.6 |
|
635 |
|
|
/** |
636 |
|
|
* drainTo(null) throws NPE |
637 |
jsr166 |
1.10 |
*/ |
638 |
dl |
1.6 |
public void testDrainToNull() { |
639 |
|
|
SynchronousQueue q = new SynchronousQueue(); |
640 |
|
|
try { |
641 |
|
|
q.drainTo(null); |
642 |
|
|
shouldThrow(); |
643 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
644 |
dl |
1.6 |
} |
645 |
|
|
|
646 |
|
|
/** |
647 |
|
|
* drainTo(this) throws IAE |
648 |
jsr166 |
1.10 |
*/ |
649 |
dl |
1.6 |
public void testDrainToSelf() { |
650 |
|
|
SynchronousQueue q = new SynchronousQueue(); |
651 |
|
|
try { |
652 |
|
|
q.drainTo(q); |
653 |
|
|
shouldThrow(); |
654 |
jsr166 |
1.13 |
} catch (IllegalArgumentException success) {} |
655 |
dl |
1.6 |
} |
656 |
|
|
|
657 |
|
|
/** |
658 |
|
|
* drainTo(c) of empty queue doesn't transfer elements |
659 |
jsr166 |
1.10 |
*/ |
660 |
dl |
1.6 |
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 |
jsr166 |
1.10 |
*/ |
671 |
jsr166 |
1.13 |
public void testDrainToWithActivePut() throws InterruptedException { |
672 |
dl |
1.6 |
final SynchronousQueue q = new SynchronousQueue(); |
673 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedRunnable() { |
674 |
|
|
public void realRun() throws InterruptedException { |
675 |
jsr166 |
1.13 |
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 |
dl |
1.6 |
} |
688 |
|
|
|
689 |
|
|
/** |
690 |
|
|
* drainTo(null, n) throws NPE |
691 |
jsr166 |
1.10 |
*/ |
692 |
dl |
1.6 |
public void testDrainToNullN() { |
693 |
|
|
SynchronousQueue q = new SynchronousQueue(); |
694 |
|
|
try { |
695 |
|
|
q.drainTo(null, 0); |
696 |
|
|
shouldThrow(); |
697 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
698 |
dl |
1.6 |
} |
699 |
|
|
|
700 |
|
|
/** |
701 |
|
|
* drainTo(this, n) throws IAE |
702 |
jsr166 |
1.10 |
*/ |
703 |
dl |
1.6 |
public void testDrainToSelfN() { |
704 |
|
|
SynchronousQueue q = new SynchronousQueue(); |
705 |
|
|
try { |
706 |
|
|
q.drainTo(q, 0); |
707 |
|
|
shouldThrow(); |
708 |
jsr166 |
1.16 |
} catch (IllegalArgumentException success) {} |
709 |
dl |
1.6 |
} |
710 |
|
|
|
711 |
|
|
/** |
712 |
|
|
* drainTo(c, n) empties up to n elements of queue into c |
713 |
jsr166 |
1.10 |
*/ |
714 |
jsr166 |
1.13 |
public void testDrainToN() throws InterruptedException { |
715 |
dl |
1.6 |
final SynchronousQueue q = new SynchronousQueue(); |
716 |
jsr166 |
1.14 |
Thread t1 = new Thread(new CheckedRunnable() { |
717 |
|
|
public void realRun() throws InterruptedException { |
718 |
jsr166 |
1.13 |
q.put(one); |
719 |
|
|
}}); |
720 |
|
|
|
721 |
jsr166 |
1.14 |
Thread t2 = new Thread(new CheckedRunnable() { |
722 |
|
|
public void realRun() throws InterruptedException { |
723 |
jsr166 |
1.13 |
q.put(two); |
724 |
|
|
}}); |
725 |
dl |
1.6 |
|
726 |
jsr166 |
1.13 |
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 |
dl |
1.6 |
} |
739 |
|
|
|
740 |
dl |
1.1 |
} |