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