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 |
jsr166 |
1.25 |
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 |
dl |
1.1 |
public static void main(String[] args) { |
30 |
jsr166 |
1.21 |
junit.textui.TestRunner.run(suite()); |
31 |
dl |
1.1 |
} |
32 |
|
|
|
33 |
|
|
public static Test suite() { |
34 |
jsr166 |
1.25 |
return newTestSuite(SynchronousQueueTest.class, |
35 |
|
|
new Fair().testSuite(), |
36 |
|
|
new NonFair().testSuite()); |
37 |
dl |
1.1 |
} |
38 |
|
|
|
39 |
dl |
1.4 |
/** |
40 |
dl |
1.5 |
* A SynchronousQueue is both empty and full |
41 |
dl |
1.4 |
*/ |
42 |
dl |
1.1 |
public void testEmptyFull() { |
43 |
|
|
SynchronousQueue q = new SynchronousQueue(); |
44 |
|
|
assertTrue(q.isEmpty()); |
45 |
jsr166 |
1.14 |
assertEquals(0, q.size()); |
46 |
dl |
1.1 |
assertEquals(0, q.remainingCapacity()); |
47 |
dl |
1.5 |
assertFalse(q.offer(zero)); |
48 |
dl |
1.1 |
} |
49 |
|
|
|
50 |
dl |
1.4 |
/** |
51 |
dl |
1.8 |
* A fair SynchronousQueue is both empty and full |
52 |
|
|
*/ |
53 |
|
|
public void testFairEmptyFull() { |
54 |
|
|
SynchronousQueue q = new SynchronousQueue(true); |
55 |
|
|
assertTrue(q.isEmpty()); |
56 |
jsr166 |
1.14 |
assertEquals(0, q.size()); |
57 |
dl |
1.8 |
assertEquals(0, q.remainingCapacity()); |
58 |
|
|
assertFalse(q.offer(zero)); |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
/** |
62 |
dl |
1.5 |
* offer(null) throws NPE |
63 |
dl |
1.4 |
*/ |
64 |
|
|
public void testOfferNull() { |
65 |
jsr166 |
1.14 |
try { |
66 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
67 |
|
|
q.offer(null); |
68 |
dl |
1.4 |
shouldThrow(); |
69 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
70 |
dl |
1.1 |
} |
71 |
|
|
|
72 |
dl |
1.4 |
/** |
73 |
dl |
1.6 |
* add(null) throws NPE |
74 |
|
|
*/ |
75 |
|
|
public void testAddNull() { |
76 |
jsr166 |
1.14 |
try { |
77 |
dl |
1.6 |
SynchronousQueue q = new SynchronousQueue(); |
78 |
|
|
q.add(null); |
79 |
|
|
shouldThrow(); |
80 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
81 |
dl |
1.6 |
} |
82 |
|
|
|
83 |
|
|
/** |
84 |
dl |
1.5 |
* offer fails if no active taker |
85 |
dl |
1.4 |
*/ |
86 |
|
|
public void testOffer() { |
87 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
88 |
dl |
1.5 |
assertFalse(q.offer(one)); |
89 |
dl |
1.1 |
} |
90 |
|
|
|
91 |
dl |
1.4 |
/** |
92 |
dl |
1.5 |
* add throws ISE if no active taker |
93 |
dl |
1.4 |
*/ |
94 |
|
|
public void testAdd() { |
95 |
jsr166 |
1.14 |
try { |
96 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
97 |
|
|
assertEquals(0, q.remainingCapacity()); |
98 |
dl |
1.5 |
q.add(one); |
99 |
|
|
shouldThrow(); |
100 |
jsr166 |
1.13 |
} catch (IllegalStateException success) {} |
101 |
dl |
1.1 |
} |
102 |
|
|
|
103 |
dl |
1.4 |
/** |
104 |
dl |
1.5 |
* addAll(null) throws NPE |
105 |
dl |
1.4 |
*/ |
106 |
|
|
public void testAddAll1() { |
107 |
dl |
1.1 |
try { |
108 |
|
|
SynchronousQueue q = new SynchronousQueue(); |
109 |
|
|
q.addAll(null); |
110 |
dl |
1.4 |
shouldThrow(); |
111 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
112 |
dl |
1.1 |
} |
113 |
dl |
1.6 |
|
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 |
jsr166 |
1.13 |
} catch (IllegalArgumentException success) {} |
123 |
dl |
1.6 |
} |
124 |
|
|
|
125 |
dl |
1.4 |
/** |
126 |
dl |
1.5 |
* addAll of a collection with null elements throws NPE |
127 |
dl |
1.4 |
*/ |
128 |
|
|
public void testAddAll2() { |
129 |
dl |
1.1 |
try { |
130 |
|
|
SynchronousQueue q = new SynchronousQueue(); |
131 |
dl |
1.3 |
Integer[] ints = new Integer[1]; |
132 |
dl |
1.1 |
q.addAll(Arrays.asList(ints)); |
133 |
dl |
1.4 |
shouldThrow(); |
134 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
135 |
dl |
1.1 |
} |
136 |
jsr166 |
1.22 |
|
137 |
dl |
1.4 |
/** |
138 |
dl |
1.5 |
* addAll throws ISE if no active taker |
139 |
dl |
1.4 |
*/ |
140 |
|
|
public void testAddAll4() { |
141 |
dl |
1.1 |
try { |
142 |
|
|
SynchronousQueue q = new SynchronousQueue(); |
143 |
dl |
1.3 |
Integer[] ints = new Integer[1]; |
144 |
|
|
for (int i = 0; i < 1; ++i) |
145 |
dl |
1.1 |
ints[i] = new Integer(i); |
146 |
|
|
q.addAll(Arrays.asList(ints)); |
147 |
dl |
1.4 |
shouldThrow(); |
148 |
jsr166 |
1.13 |
} catch (IllegalStateException success) {} |
149 |
dl |
1.1 |
} |
150 |
|
|
|
151 |
dl |
1.4 |
/** |
152 |
dl |
1.5 |
* put(null) throws NPE |
153 |
dl |
1.4 |
*/ |
154 |
jsr166 |
1.13 |
public void testPutNull() throws InterruptedException { |
155 |
jsr166 |
1.14 |
try { |
156 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
157 |
|
|
q.put(null); |
158 |
dl |
1.4 |
shouldThrow(); |
159 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
160 |
dl |
1.1 |
} |
161 |
|
|
|
162 |
dl |
1.4 |
/** |
163 |
dl |
1.5 |
* put blocks interruptibly if no active taker |
164 |
dl |
1.4 |
*/ |
165 |
jsr166 |
1.13 |
public void testBlockingPut() throws InterruptedException { |
166 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
167 |
|
|
public void realRun() throws InterruptedException { |
168 |
jsr166 |
1.13 |
SynchronousQueue q = new SynchronousQueue(); |
169 |
|
|
q.put(zero); |
170 |
|
|
}}); |
171 |
|
|
|
172 |
dl |
1.1 |
t.start(); |
173 |
jsr166 |
1.13 |
Thread.sleep(SHORT_DELAY_MS); |
174 |
|
|
t.interrupt(); |
175 |
|
|
t.join(); |
176 |
dl |
1.1 |
} |
177 |
|
|
|
178 |
dl |
1.4 |
/** |
179 |
jsr166 |
1.10 |
* put blocks waiting for take |
180 |
dl |
1.4 |
*/ |
181 |
jsr166 |
1.13 |
public void testPutWithTake() throws InterruptedException { |
182 |
dl |
1.1 |
final SynchronousQueue q = new SynchronousQueue(); |
183 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedRunnable() { |
184 |
|
|
public void realRun() throws InterruptedException { |
185 |
jsr166 |
1.13 |
int added = 0; |
186 |
|
|
try { |
187 |
jsr166 |
1.19 |
while (true) { |
188 |
|
|
q.put(added); |
189 |
|
|
++added; |
190 |
|
|
} |
191 |
jsr166 |
1.13 |
} catch (InterruptedException success) { |
192 |
jsr166 |
1.23 |
assertEquals(1, added); |
193 |
dl |
1.1 |
} |
194 |
jsr166 |
1.13 |
}}); |
195 |
|
|
|
196 |
|
|
t.start(); |
197 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
198 |
jsr166 |
1.19 |
assertEquals(0, q.take()); |
199 |
jsr166 |
1.13 |
Thread.sleep(SHORT_DELAY_MS); |
200 |
|
|
t.interrupt(); |
201 |
|
|
t.join(); |
202 |
dl |
1.1 |
} |
203 |
|
|
|
204 |
dl |
1.4 |
/** |
205 |
dl |
1.5 |
* timed offer times out if elements not taken |
206 |
dl |
1.4 |
*/ |
207 |
jsr166 |
1.13 |
public void testTimedOffer() throws InterruptedException { |
208 |
dl |
1.1 |
final SynchronousQueue q = new SynchronousQueue(); |
209 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
210 |
|
|
public void realRun() throws InterruptedException { |
211 |
jsr166 |
1.17 |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
212 |
jsr166 |
1.13 |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
213 |
|
|
}}); |
214 |
jsr166 |
1.10 |
|
215 |
jsr166 |
1.13 |
t.start(); |
216 |
|
|
Thread.sleep(SMALL_DELAY_MS); |
217 |
|
|
t.interrupt(); |
218 |
|
|
t.join(); |
219 |
dl |
1.1 |
} |
220 |
|
|
|
221 |
|
|
|
222 |
dl |
1.4 |
/** |
223 |
dl |
1.5 |
* take blocks interruptibly when empty |
224 |
dl |
1.4 |
*/ |
225 |
jsr166 |
1.13 |
public void testTakeFromEmpty() throws InterruptedException { |
226 |
dl |
1.1 |
final SynchronousQueue q = new SynchronousQueue(); |
227 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
228 |
|
|
public void realRun() throws InterruptedException { |
229 |
jsr166 |
1.13 |
q.take(); |
230 |
|
|
}}); |
231 |
|
|
|
232 |
|
|
t.start(); |
233 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
234 |
|
|
t.interrupt(); |
235 |
|
|
t.join(); |
236 |
dl |
1.1 |
} |
237 |
|
|
|
238 |
dl |
1.8 |
|
239 |
|
|
/** |
240 |
|
|
* put blocks interruptibly if no active taker |
241 |
|
|
*/ |
242 |
jsr166 |
1.13 |
public void testFairBlockingPut() throws InterruptedException { |
243 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
244 |
|
|
public void realRun() throws InterruptedException { |
245 |
jsr166 |
1.13 |
SynchronousQueue q = new SynchronousQueue(true); |
246 |
|
|
q.put(zero); |
247 |
|
|
}}); |
248 |
|
|
|
249 |
dl |
1.8 |
t.start(); |
250 |
jsr166 |
1.13 |
Thread.sleep(SHORT_DELAY_MS); |
251 |
|
|
t.interrupt(); |
252 |
|
|
t.join(); |
253 |
dl |
1.8 |
} |
254 |
|
|
|
255 |
|
|
/** |
256 |
jsr166 |
1.10 |
* put blocks waiting for take |
257 |
dl |
1.8 |
*/ |
258 |
jsr166 |
1.13 |
public void testFairPutWithTake() throws InterruptedException { |
259 |
dl |
1.8 |
final SynchronousQueue q = new SynchronousQueue(true); |
260 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedRunnable() { |
261 |
|
|
public void realRun() throws InterruptedException { |
262 |
jsr166 |
1.13 |
int added = 0; |
263 |
|
|
try { |
264 |
jsr166 |
1.19 |
while (true) { |
265 |
|
|
q.put(added); |
266 |
|
|
++added; |
267 |
|
|
} |
268 |
jsr166 |
1.13 |
} catch (InterruptedException success) { |
269 |
jsr166 |
1.23 |
assertEquals(1, added); |
270 |
dl |
1.8 |
} |
271 |
jsr166 |
1.13 |
}}); |
272 |
|
|
|
273 |
|
|
t.start(); |
274 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
275 |
jsr166 |
1.19 |
assertEquals(0, q.take()); |
276 |
jsr166 |
1.13 |
Thread.sleep(SHORT_DELAY_MS); |
277 |
|
|
t.interrupt(); |
278 |
|
|
t.join(); |
279 |
dl |
1.8 |
} |
280 |
|
|
|
281 |
|
|
/** |
282 |
|
|
* timed offer times out if elements not taken |
283 |
|
|
*/ |
284 |
jsr166 |
1.13 |
public void testFairTimedOffer() throws InterruptedException { |
285 |
dl |
1.8 |
final SynchronousQueue q = new SynchronousQueue(true); |
286 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
287 |
|
|
public void realRun() throws InterruptedException { |
288 |
jsr166 |
1.19 |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
289 |
jsr166 |
1.13 |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
290 |
|
|
}}); |
291 |
jsr166 |
1.10 |
|
292 |
jsr166 |
1.13 |
t.start(); |
293 |
|
|
Thread.sleep(SMALL_DELAY_MS); |
294 |
|
|
t.interrupt(); |
295 |
|
|
t.join(); |
296 |
dl |
1.8 |
} |
297 |
|
|
|
298 |
|
|
|
299 |
|
|
/** |
300 |
|
|
* take blocks interruptibly when empty |
301 |
|
|
*/ |
302 |
jsr166 |
1.13 |
public void testFairTakeFromEmpty() throws InterruptedException { |
303 |
dl |
1.8 |
final SynchronousQueue q = new SynchronousQueue(true); |
304 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
305 |
|
|
public void realRun() throws InterruptedException { |
306 |
jsr166 |
1.13 |
q.take(); |
307 |
|
|
}}); |
308 |
|
|
|
309 |
|
|
t.start(); |
310 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
311 |
|
|
t.interrupt(); |
312 |
|
|
t.join(); |
313 |
dl |
1.8 |
} |
314 |
|
|
|
315 |
dl |
1.4 |
/** |
316 |
dl |
1.5 |
* poll fails unless active taker |
317 |
dl |
1.4 |
*/ |
318 |
|
|
public void testPoll() { |
319 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
320 |
jsr166 |
1.14 |
assertNull(q.poll()); |
321 |
dl |
1.1 |
} |
322 |
|
|
|
323 |
dl |
1.4 |
/** |
324 |
dl |
1.5 |
* timed pool with zero timeout times out if no active taker |
325 |
dl |
1.4 |
*/ |
326 |
jsr166 |
1.13 |
public void testTimedPoll0() throws InterruptedException { |
327 |
|
|
SynchronousQueue q = new SynchronousQueue(); |
328 |
|
|
assertNull(q.poll(0, MILLISECONDS)); |
329 |
dl |
1.1 |
} |
330 |
|
|
|
331 |
dl |
1.4 |
/** |
332 |
dl |
1.5 |
* timed pool with nonzero timeout times out if no active taker |
333 |
dl |
1.4 |
*/ |
334 |
jsr166 |
1.13 |
public void testTimedPoll() throws InterruptedException { |
335 |
|
|
SynchronousQueue q = new SynchronousQueue(); |
336 |
|
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
337 |
dl |
1.1 |
} |
338 |
|
|
|
339 |
dl |
1.4 |
/** |
340 |
dl |
1.5 |
* Interrupted timed poll throws InterruptedException instead of |
341 |
|
|
* returning timeout status |
342 |
dl |
1.4 |
*/ |
343 |
jsr166 |
1.13 |
public void testInterruptedTimedPoll() throws InterruptedException { |
344 |
jsr166 |
1.15 |
final SynchronousQueue q = new SynchronousQueue(); |
345 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
346 |
|
|
public void realRun() throws InterruptedException { |
347 |
jsr166 |
1.13 |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
348 |
|
|
}}); |
349 |
|
|
|
350 |
dl |
1.1 |
t.start(); |
351 |
jsr166 |
1.13 |
Thread.sleep(SHORT_DELAY_MS); |
352 |
|
|
t.interrupt(); |
353 |
|
|
t.join(); |
354 |
dl |
1.1 |
} |
355 |
|
|
|
356 |
dl |
1.4 |
/** |
357 |
dl |
1.8 |
* Interrupted timed poll throws InterruptedException instead of |
358 |
|
|
* returning timeout status |
359 |
|
|
*/ |
360 |
jsr166 |
1.13 |
public void testFairInterruptedTimedPoll() throws InterruptedException { |
361 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
362 |
|
|
public void realRun() throws InterruptedException { |
363 |
jsr166 |
1.13 |
SynchronousQueue q = new SynchronousQueue(true); |
364 |
|
|
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
365 |
|
|
}}); |
366 |
|
|
|
367 |
dl |
1.8 |
t.start(); |
368 |
jsr166 |
1.13 |
Thread.sleep(SHORT_DELAY_MS); |
369 |
|
|
t.interrupt(); |
370 |
|
|
t.join(); |
371 |
dl |
1.8 |
} |
372 |
|
|
|
373 |
|
|
/** |
374 |
|
|
* timed poll before a delayed offer fails; after offer succeeds; |
375 |
|
|
* on interruption throws |
376 |
|
|
*/ |
377 |
jsr166 |
1.13 |
public void testFairTimedPollWithOffer() throws InterruptedException { |
378 |
dl |
1.8 |
final SynchronousQueue q = new SynchronousQueue(true); |
379 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedRunnable() { |
380 |
|
|
public void realRun() throws InterruptedException { |
381 |
jsr166 |
1.13 |
try { |
382 |
dl |
1.24 |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
383 |
|
|
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
384 |
jsr166 |
1.13 |
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 |
jsr166 |
1.10 |
} |
395 |
dl |
1.1 |
|
396 |
|
|
|
397 |
dl |
1.4 |
/** |
398 |
dl |
1.5 |
* peek returns null |
399 |
dl |
1.4 |
*/ |
400 |
|
|
public void testPeek() { |
401 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
402 |
jsr166 |
1.14 |
assertNull(q.peek()); |
403 |
dl |
1.1 |
} |
404 |
|
|
|
405 |
dl |
1.4 |
/** |
406 |
dl |
1.5 |
* element throws NSEE |
407 |
dl |
1.4 |
*/ |
408 |
|
|
public void testElement() { |
409 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
410 |
|
|
try { |
411 |
|
|
q.element(); |
412 |
dl |
1.4 |
shouldThrow(); |
413 |
jsr166 |
1.13 |
} catch (NoSuchElementException success) {} |
414 |
dl |
1.1 |
} |
415 |
|
|
|
416 |
dl |
1.4 |
/** |
417 |
dl |
1.5 |
* remove throws NSEE if no active taker |
418 |
dl |
1.4 |
*/ |
419 |
|
|
public void testRemove() { |
420 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
421 |
|
|
try { |
422 |
|
|
q.remove(); |
423 |
dl |
1.4 |
shouldThrow(); |
424 |
jsr166 |
1.16 |
} catch (NoSuchElementException success) {} |
425 |
dl |
1.1 |
} |
426 |
|
|
|
427 |
dl |
1.4 |
/** |
428 |
dl |
1.5 |
* remove(x) returns false |
429 |
dl |
1.4 |
*/ |
430 |
|
|
public void testRemoveElement() { |
431 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
432 |
dl |
1.5 |
assertFalse(q.remove(zero)); |
433 |
dl |
1.2 |
assertTrue(q.isEmpty()); |
434 |
dl |
1.1 |
} |
435 |
jsr166 |
1.10 |
|
436 |
dl |
1.4 |
/** |
437 |
dl |
1.5 |
* contains returns false |
438 |
dl |
1.4 |
*/ |
439 |
|
|
public void testContains() { |
440 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
441 |
dl |
1.5 |
assertFalse(q.contains(zero)); |
442 |
dl |
1.1 |
} |
443 |
|
|
|
444 |
dl |
1.4 |
/** |
445 |
dl |
1.5 |
* clear ensures isEmpty |
446 |
dl |
1.4 |
*/ |
447 |
|
|
public void testClear() { |
448 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
449 |
|
|
q.clear(); |
450 |
|
|
assertTrue(q.isEmpty()); |
451 |
|
|
} |
452 |
|
|
|
453 |
dl |
1.4 |
/** |
454 |
dl |
1.5 |
* containsAll returns false unless empty |
455 |
dl |
1.4 |
*/ |
456 |
|
|
public void testContainsAll() { |
457 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
458 |
|
|
Integer[] empty = new Integer[0]; |
459 |
dl |
1.5 |
assertTrue(q.containsAll(Arrays.asList(empty))); |
460 |
|
|
Integer[] ints = new Integer[1]; ints[0] = zero; |
461 |
dl |
1.1 |
assertFalse(q.containsAll(Arrays.asList(ints))); |
462 |
|
|
} |
463 |
|
|
|
464 |
dl |
1.4 |
/** |
465 |
dl |
1.5 |
* retainAll returns false |
466 |
dl |
1.4 |
*/ |
467 |
|
|
public void testRetainAll() { |
468 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
469 |
|
|
Integer[] empty = new Integer[0]; |
470 |
dl |
1.5 |
assertFalse(q.retainAll(Arrays.asList(empty))); |
471 |
|
|
Integer[] ints = new Integer[1]; ints[0] = zero; |
472 |
|
|
assertFalse(q.retainAll(Arrays.asList(ints))); |
473 |
dl |
1.1 |
} |
474 |
|
|
|
475 |
dl |
1.4 |
/** |
476 |
dl |
1.5 |
* removeAll returns false |
477 |
dl |
1.4 |
*/ |
478 |
|
|
public void testRemoveAll() { |
479 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
480 |
|
|
Integer[] empty = new Integer[0]; |
481 |
dl |
1.5 |
assertFalse(q.removeAll(Arrays.asList(empty))); |
482 |
|
|
Integer[] ints = new Integer[1]; ints[0] = zero; |
483 |
dl |
1.1 |
assertFalse(q.containsAll(Arrays.asList(ints))); |
484 |
|
|
} |
485 |
|
|
|
486 |
|
|
|
487 |
dl |
1.4 |
/** |
488 |
dl |
1.5 |
* toArray is empty |
489 |
dl |
1.4 |
*/ |
490 |
|
|
public void testToArray() { |
491 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
492 |
jsr166 |
1.14 |
Object[] o = q.toArray(); |
493 |
dl |
1.1 |
assertEquals(o.length, 0); |
494 |
|
|
} |
495 |
|
|
|
496 |
dl |
1.4 |
/** |
497 |
dl |
1.5 |
* toArray(a) is nulled at position 0 |
498 |
dl |
1.4 |
*/ |
499 |
|
|
public void testToArray2() { |
500 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
501 |
jsr166 |
1.14 |
Integer[] ints = new Integer[1]; |
502 |
dl |
1.1 |
assertNull(ints[0]); |
503 |
|
|
} |
504 |
jsr166 |
1.10 |
|
505 |
dl |
1.4 |
/** |
506 |
dl |
1.6 |
* toArray(null) throws NPE |
507 |
|
|
*/ |
508 |
|
|
public void testToArray_BadArg() { |
509 |
jsr166 |
1.18 |
SynchronousQueue q = new SynchronousQueue(); |
510 |
jsr166 |
1.14 |
try { |
511 |
|
|
Object o[] = q.toArray(null); |
512 |
|
|
shouldThrow(); |
513 |
|
|
} catch (NullPointerException success) {} |
514 |
dl |
1.6 |
} |
515 |
|
|
|
516 |
|
|
|
517 |
|
|
/** |
518 |
dl |
1.5 |
* iterator does not traverse any elements |
519 |
dl |
1.4 |
*/ |
520 |
|
|
public void testIterator() { |
521 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
522 |
jsr166 |
1.14 |
Iterator it = q.iterator(); |
523 |
dl |
1.1 |
assertFalse(it.hasNext()); |
524 |
|
|
try { |
525 |
|
|
Object x = it.next(); |
526 |
dl |
1.4 |
shouldThrow(); |
527 |
jsr166 |
1.13 |
} catch (NoSuchElementException success) {} |
528 |
dl |
1.1 |
} |
529 |
|
|
|
530 |
dl |
1.4 |
/** |
531 |
dl |
1.5 |
* iterator remove throws ISE |
532 |
dl |
1.4 |
*/ |
533 |
|
|
public void testIteratorRemove() { |
534 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
535 |
jsr166 |
1.14 |
Iterator it = q.iterator(); |
536 |
dl |
1.1 |
try { |
537 |
|
|
it.remove(); |
538 |
dl |
1.4 |
shouldThrow(); |
539 |
jsr166 |
1.13 |
} catch (IllegalStateException success) {} |
540 |
dl |
1.1 |
} |
541 |
|
|
|
542 |
dl |
1.4 |
/** |
543 |
dl |
1.5 |
* toString returns a non-null string |
544 |
dl |
1.4 |
*/ |
545 |
|
|
public void testToString() { |
546 |
dl |
1.1 |
SynchronousQueue q = new SynchronousQueue(); |
547 |
|
|
String s = q.toString(); |
548 |
dl |
1.5 |
assertNotNull(s); |
549 |
jsr166 |
1.10 |
} |
550 |
dl |
1.1 |
|
551 |
|
|
|
552 |
dl |
1.4 |
/** |
553 |
dl |
1.5 |
* offer transfers elements across Executor tasks |
554 |
dl |
1.4 |
*/ |
555 |
dl |
1.1 |
public void testOfferInExecutor() { |
556 |
|
|
final SynchronousQueue q = new SynchronousQueue(); |
557 |
|
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
558 |
|
|
|
559 |
jsr166 |
1.13 |
executor.execute(new CheckedRunnable() { |
560 |
jsr166 |
1.14 |
public void realRun() throws InterruptedException { |
561 |
jsr166 |
1.19 |
assertFalse(q.offer(one)); |
562 |
|
|
assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); |
563 |
|
|
assertEquals(0, q.remainingCapacity()); |
564 |
jsr166 |
1.13 |
}}); |
565 |
|
|
|
566 |
|
|
executor.execute(new CheckedRunnable() { |
567 |
jsr166 |
1.14 |
public void realRun() throws InterruptedException { |
568 |
jsr166 |
1.13 |
Thread.sleep(SMALL_DELAY_MS); |
569 |
jsr166 |
1.19 |
assertSame(one, q.take()); |
570 |
jsr166 |
1.13 |
}}); |
571 |
jsr166 |
1.10 |
|
572 |
dl |
1.3 |
joinPool(executor); |
573 |
dl |
1.1 |
} |
574 |
|
|
|
575 |
dl |
1.4 |
/** |
576 |
dl |
1.5 |
* poll retrieves elements across Executor threads |
577 |
dl |
1.4 |
*/ |
578 |
dl |
1.1 |
public void testPollInExecutor() { |
579 |
|
|
final SynchronousQueue q = new SynchronousQueue(); |
580 |
|
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
581 |
jsr166 |
1.13 |
executor.execute(new CheckedRunnable() { |
582 |
jsr166 |
1.14 |
public void realRun() throws InterruptedException { |
583 |
jsr166 |
1.19 |
assertNull(q.poll()); |
584 |
|
|
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
585 |
|
|
assertTrue(q.isEmpty()); |
586 |
jsr166 |
1.13 |
}}); |
587 |
|
|
|
588 |
|
|
executor.execute(new CheckedRunnable() { |
589 |
jsr166 |
1.14 |
public void realRun() throws InterruptedException { |
590 |
jsr166 |
1.19 |
Thread.sleep(SHORT_DELAY_MS); |
591 |
|
|
q.put(one); |
592 |
jsr166 |
1.13 |
}}); |
593 |
jsr166 |
1.10 |
|
594 |
dl |
1.3 |
joinPool(executor); |
595 |
dl |
1.2 |
} |
596 |
|
|
|
597 |
dl |
1.4 |
/** |
598 |
dl |
1.5 |
* a deserialized serialized queue is usable |
599 |
dl |
1.4 |
*/ |
600 |
jsr166 |
1.13 |
public void testSerialization() throws Exception { |
601 |
dl |
1.2 |
SynchronousQueue q = new SynchronousQueue(); |
602 |
jsr166 |
1.13 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
603 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
604 |
|
|
out.writeObject(q); |
605 |
|
|
out.close(); |
606 |
|
|
|
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 |
dl |
1.1 |
} |
614 |
dl |
1.6 |
|
615 |
|
|
/** |
616 |
|
|
* drainTo(null) throws NPE |
617 |
jsr166 |
1.10 |
*/ |
618 |
dl |
1.6 |
public void testDrainToNull() { |
619 |
|
|
SynchronousQueue q = new SynchronousQueue(); |
620 |
|
|
try { |
621 |
|
|
q.drainTo(null); |
622 |
|
|
shouldThrow(); |
623 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
624 |
dl |
1.6 |
} |
625 |
|
|
|
626 |
|
|
/** |
627 |
|
|
* drainTo(this) throws IAE |
628 |
jsr166 |
1.10 |
*/ |
629 |
dl |
1.6 |
public void testDrainToSelf() { |
630 |
|
|
SynchronousQueue q = new SynchronousQueue(); |
631 |
|
|
try { |
632 |
|
|
q.drainTo(q); |
633 |
|
|
shouldThrow(); |
634 |
jsr166 |
1.13 |
} catch (IllegalArgumentException success) {} |
635 |
dl |
1.6 |
} |
636 |
|
|
|
637 |
|
|
/** |
638 |
|
|
* drainTo(c) of empty queue doesn't transfer elements |
639 |
jsr166 |
1.10 |
*/ |
640 |
dl |
1.6 |
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 |
jsr166 |
1.10 |
*/ |
651 |
jsr166 |
1.13 |
public void testDrainToWithActivePut() throws InterruptedException { |
652 |
dl |
1.6 |
final SynchronousQueue q = new SynchronousQueue(); |
653 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedRunnable() { |
654 |
|
|
public void realRun() throws InterruptedException { |
655 |
jsr166 |
1.13 |
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 |
dl |
1.6 |
} |
668 |
|
|
|
669 |
|
|
/** |
670 |
|
|
* drainTo(null, n) throws NPE |
671 |
jsr166 |
1.10 |
*/ |
672 |
dl |
1.6 |
public void testDrainToNullN() { |
673 |
|
|
SynchronousQueue q = new SynchronousQueue(); |
674 |
|
|
try { |
675 |
|
|
q.drainTo(null, 0); |
676 |
|
|
shouldThrow(); |
677 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
678 |
dl |
1.6 |
} |
679 |
|
|
|
680 |
|
|
/** |
681 |
|
|
* drainTo(this, n) throws IAE |
682 |
jsr166 |
1.10 |
*/ |
683 |
dl |
1.6 |
public void testDrainToSelfN() { |
684 |
|
|
SynchronousQueue q = new SynchronousQueue(); |
685 |
|
|
try { |
686 |
|
|
q.drainTo(q, 0); |
687 |
|
|
shouldThrow(); |
688 |
jsr166 |
1.16 |
} catch (IllegalArgumentException success) {} |
689 |
dl |
1.6 |
} |
690 |
|
|
|
691 |
|
|
/** |
692 |
|
|
* drainTo(c, n) empties up to n elements of queue into c |
693 |
jsr166 |
1.10 |
*/ |
694 |
jsr166 |
1.13 |
public void testDrainToN() throws InterruptedException { |
695 |
dl |
1.6 |
final SynchronousQueue q = new SynchronousQueue(); |
696 |
jsr166 |
1.14 |
Thread t1 = new Thread(new CheckedRunnable() { |
697 |
|
|
public void realRun() throws InterruptedException { |
698 |
jsr166 |
1.13 |
q.put(one); |
699 |
|
|
}}); |
700 |
|
|
|
701 |
jsr166 |
1.14 |
Thread t2 = new Thread(new CheckedRunnable() { |
702 |
|
|
public void realRun() throws InterruptedException { |
703 |
jsr166 |
1.13 |
q.put(two); |
704 |
|
|
}}); |
705 |
dl |
1.6 |
|
706 |
jsr166 |
1.13 |
t1.start(); |
707 |
|
|
t2.start(); |
708 |
|
|
ArrayList l = new ArrayList(); |
709 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
710 |
|
|
q.drainTo(l, 1); |
711 |
jsr166 |
1.23 |
assertEquals(1, l.size()); |
712 |
jsr166 |
1.13 |
q.drainTo(l, 1); |
713 |
jsr166 |
1.23 |
assertEquals(2, l.size()); |
714 |
jsr166 |
1.13 |
assertTrue(l.contains(one)); |
715 |
|
|
assertTrue(l.contains(two)); |
716 |
|
|
t1.join(); |
717 |
|
|
t2.join(); |
718 |
dl |
1.6 |
} |
719 |
|
|
|
720 |
dl |
1.1 |
} |