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