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 |
jsr166 |
1.44 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
|
|
11 |
|
|
import java.util.ArrayList; |
12 |
jsr166 |
1.37 |
import java.util.Arrays; |
13 |
|
|
import java.util.Collection; |
14 |
|
|
import java.util.Iterator; |
15 |
|
|
import java.util.NoSuchElementException; |
16 |
|
|
import java.util.concurrent.BlockingQueue; |
17 |
|
|
import java.util.concurrent.CountDownLatch; |
18 |
|
|
import java.util.concurrent.Executors; |
19 |
|
|
import java.util.concurrent.ExecutorService; |
20 |
|
|
import java.util.concurrent.SynchronousQueue; |
21 |
jsr166 |
1.44 |
|
22 |
|
|
import junit.framework.Test; |
23 |
dl |
1.1 |
|
24 |
dl |
1.3 |
public class SynchronousQueueTest extends JSR166TestCase { |
25 |
dl |
1.1 |
|
26 |
jsr166 |
1.25 |
public static class Fair extends BlockingQueueTest { |
27 |
|
|
protected BlockingQueue emptyCollection() { |
28 |
|
|
return new SynchronousQueue(true); |
29 |
|
|
} |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
public static class NonFair extends BlockingQueueTest { |
33 |
|
|
protected BlockingQueue emptyCollection() { |
34 |
|
|
return new SynchronousQueue(false); |
35 |
|
|
} |
36 |
|
|
} |
37 |
|
|
|
38 |
dl |
1.1 |
public static void main(String[] args) { |
39 |
jsr166 |
1.47 |
main(suite(), args); |
40 |
dl |
1.1 |
} |
41 |
|
|
|
42 |
|
|
public static Test suite() { |
43 |
jsr166 |
1.25 |
return newTestSuite(SynchronousQueueTest.class, |
44 |
|
|
new Fair().testSuite(), |
45 |
|
|
new NonFair().testSuite()); |
46 |
dl |
1.1 |
} |
47 |
|
|
|
48 |
dl |
1.4 |
/** |
49 |
jsr166 |
1.29 |
* Any SynchronousQueue is both empty and full |
50 |
dl |
1.4 |
*/ |
51 |
jsr166 |
1.35 |
public void testEmptyFull() { testEmptyFull(false); } |
52 |
|
|
public void testEmptyFull_fair() { testEmptyFull(true); } |
53 |
|
|
public void testEmptyFull(boolean fair) { |
54 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
55 |
dl |
1.1 |
assertTrue(q.isEmpty()); |
56 |
dl |
1.68 |
mustEqual(0, q.size()); |
57 |
|
|
mustEqual(0, q.remainingCapacity()); |
58 |
dl |
1.5 |
assertFalse(q.offer(zero)); |
59 |
dl |
1.1 |
} |
60 |
|
|
|
61 |
dl |
1.4 |
/** |
62 |
dl |
1.5 |
* offer fails if no active taker |
63 |
dl |
1.4 |
*/ |
64 |
jsr166 |
1.36 |
public void testOffer() { testOffer(false); } |
65 |
|
|
public void testOffer_fair() { testOffer(true); } |
66 |
|
|
public void testOffer(boolean fair) { |
67 |
dl |
1.68 |
SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
68 |
dl |
1.5 |
assertFalse(q.offer(one)); |
69 |
dl |
1.1 |
} |
70 |
|
|
|
71 |
dl |
1.4 |
/** |
72 |
jsr166 |
1.36 |
* add throws IllegalStateException if no active taker |
73 |
dl |
1.4 |
*/ |
74 |
jsr166 |
1.36 |
public void testAdd() { testAdd(false); } |
75 |
|
|
public void testAdd_fair() { testAdd(true); } |
76 |
|
|
public void testAdd(boolean fair) { |
77 |
dl |
1.68 |
SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
78 |
|
|
mustEqual(0, q.remainingCapacity()); |
79 |
jsr166 |
1.14 |
try { |
80 |
dl |
1.5 |
q.add(one); |
81 |
|
|
shouldThrow(); |
82 |
jsr166 |
1.13 |
} catch (IllegalStateException success) {} |
83 |
dl |
1.1 |
} |
84 |
|
|
|
85 |
dl |
1.4 |
/** |
86 |
jsr166 |
1.36 |
* addAll(this) throws IllegalArgumentException |
87 |
dl |
1.6 |
*/ |
88 |
jsr166 |
1.36 |
public void testAddAll_self() { testAddAll_self(false); } |
89 |
|
|
public void testAddAll_self_fair() { testAddAll_self(true); } |
90 |
|
|
public void testAddAll_self(boolean fair) { |
91 |
dl |
1.68 |
SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
92 |
dl |
1.6 |
try { |
93 |
|
|
q.addAll(q); |
94 |
|
|
shouldThrow(); |
95 |
jsr166 |
1.13 |
} catch (IllegalArgumentException success) {} |
96 |
dl |
1.6 |
} |
97 |
|
|
|
98 |
dl |
1.68 |
/**S |
99 |
jsr166 |
1.60 |
* addAll throws IllegalStateException if no active taker |
100 |
dl |
1.4 |
*/ |
101 |
jsr166 |
1.36 |
public void testAddAll_ISE() { testAddAll_ISE(false); } |
102 |
|
|
public void testAddAll_ISE_fair() { testAddAll_ISE(true); } |
103 |
|
|
public void testAddAll_ISE(boolean fair) { |
104 |
dl |
1.68 |
SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
105 |
|
|
Item[] items = seqItems(1); |
106 |
|
|
Collection<Item> coll = Arrays.asList(items); |
107 |
dl |
1.1 |
try { |
108 |
jsr166 |
1.36 |
q.addAll(coll); |
109 |
dl |
1.4 |
shouldThrow(); |
110 |
jsr166 |
1.13 |
} catch (IllegalStateException success) {} |
111 |
dl |
1.1 |
} |
112 |
|
|
|
113 |
dl |
1.4 |
/** |
114 |
dl |
1.5 |
* put blocks interruptibly if no active taker |
115 |
dl |
1.4 |
*/ |
116 |
jsr166 |
1.35 |
public void testBlockingPut() { testBlockingPut(false); } |
117 |
|
|
public void testBlockingPut_fair() { testBlockingPut(true); } |
118 |
|
|
public void testBlockingPut(boolean fair) { |
119 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
120 |
jsr166 |
1.35 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
121 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
122 |
jsr166 |
1.14 |
public void realRun() throws InterruptedException { |
123 |
jsr166 |
1.35 |
Thread.currentThread().interrupt(); |
124 |
|
|
try { |
125 |
dl |
1.68 |
q.put(ninetynine); |
126 |
jsr166 |
1.35 |
shouldThrow(); |
127 |
|
|
} catch (InterruptedException success) {} |
128 |
|
|
assertFalse(Thread.interrupted()); |
129 |
|
|
|
130 |
|
|
pleaseInterrupt.countDown(); |
131 |
|
|
try { |
132 |
dl |
1.68 |
q.put(ninetynine); |
133 |
jsr166 |
1.35 |
shouldThrow(); |
134 |
|
|
} catch (InterruptedException success) {} |
135 |
|
|
assertFalse(Thread.interrupted()); |
136 |
jsr166 |
1.13 |
}}); |
137 |
|
|
|
138 |
jsr166 |
1.35 |
await(pleaseInterrupt); |
139 |
jsr166 |
1.63 |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
140 |
jsr166 |
1.13 |
t.interrupt(); |
141 |
jsr166 |
1.35 |
awaitTermination(t); |
142 |
dl |
1.68 |
mustEqual(0, q.remainingCapacity()); |
143 |
dl |
1.1 |
} |
144 |
|
|
|
145 |
dl |
1.4 |
/** |
146 |
jsr166 |
1.35 |
* put blocks interruptibly waiting for take |
147 |
dl |
1.4 |
*/ |
148 |
jsr166 |
1.35 |
public void testPutWithTake() { testPutWithTake(false); } |
149 |
|
|
public void testPutWithTake_fair() { testPutWithTake(true); } |
150 |
|
|
public void testPutWithTake(boolean fair) { |
151 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
152 |
jsr166 |
1.35 |
final CountDownLatch pleaseTake = new CountDownLatch(1); |
153 |
|
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
154 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
155 |
jsr166 |
1.14 |
public void realRun() throws InterruptedException { |
156 |
jsr166 |
1.35 |
pleaseTake.countDown(); |
157 |
|
|
q.put(one); |
158 |
|
|
|
159 |
jsr166 |
1.58 |
Thread.currentThread().interrupt(); |
160 |
|
|
try { |
161 |
dl |
1.68 |
q.put(ninetynine); |
162 |
jsr166 |
1.58 |
shouldThrow(); |
163 |
|
|
} catch (InterruptedException success) {} |
164 |
|
|
assertFalse(Thread.interrupted()); |
165 |
|
|
|
166 |
jsr166 |
1.35 |
pleaseInterrupt.countDown(); |
167 |
jsr166 |
1.13 |
try { |
168 |
dl |
1.68 |
q.put(ninetynine); |
169 |
jsr166 |
1.35 |
shouldThrow(); |
170 |
|
|
} catch (InterruptedException success) {} |
171 |
|
|
assertFalse(Thread.interrupted()); |
172 |
jsr166 |
1.13 |
}}); |
173 |
|
|
|
174 |
jsr166 |
1.35 |
await(pleaseTake); |
175 |
dl |
1.68 |
mustEqual(0, q.remainingCapacity()); |
176 |
jsr166 |
1.35 |
try { assertSame(one, q.take()); } |
177 |
|
|
catch (InterruptedException e) { threadUnexpectedException(e); } |
178 |
|
|
|
179 |
|
|
await(pleaseInterrupt); |
180 |
jsr166 |
1.63 |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
181 |
jsr166 |
1.13 |
t.interrupt(); |
182 |
jsr166 |
1.35 |
awaitTermination(t); |
183 |
dl |
1.68 |
mustEqual(0, q.remainingCapacity()); |
184 |
dl |
1.1 |
} |
185 |
|
|
|
186 |
dl |
1.4 |
/** |
187 |
dl |
1.5 |
* timed offer times out if elements not taken |
188 |
dl |
1.4 |
*/ |
189 |
jsr166 |
1.57 |
public void testTimedOffer() { |
190 |
jsr166 |
1.64 |
final boolean fair = randomBoolean(); |
191 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
192 |
jsr166 |
1.29 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
193 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
194 |
jsr166 |
1.14 |
public void realRun() throws InterruptedException { |
195 |
jsr166 |
1.34 |
long startTime = System.nanoTime(); |
196 |
jsr166 |
1.63 |
|
197 |
dl |
1.68 |
assertFalse(q.offer(zero, timeoutMillis(), MILLISECONDS)); |
198 |
jsr166 |
1.34 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
199 |
jsr166 |
1.59 |
|
200 |
|
|
Thread.currentThread().interrupt(); |
201 |
|
|
try { |
202 |
dl |
1.68 |
q.offer(one, randomTimeout(), randomTimeUnit()); |
203 |
jsr166 |
1.59 |
shouldThrow(); |
204 |
|
|
} catch (InterruptedException success) {} |
205 |
|
|
assertFalse(Thread.interrupted()); |
206 |
|
|
|
207 |
jsr166 |
1.29 |
pleaseInterrupt.countDown(); |
208 |
|
|
try { |
209 |
dl |
1.68 |
q.offer(two, LONGER_DELAY_MS, MILLISECONDS); |
210 |
jsr166 |
1.29 |
shouldThrow(); |
211 |
|
|
} catch (InterruptedException success) {} |
212 |
jsr166 |
1.56 |
assertFalse(Thread.interrupted()); |
213 |
jsr166 |
1.13 |
}}); |
214 |
jsr166 |
1.10 |
|
215 |
jsr166 |
1.34 |
await(pleaseInterrupt); |
216 |
jsr166 |
1.63 |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
217 |
jsr166 |
1.13 |
t.interrupt(); |
218 |
jsr166 |
1.34 |
awaitTermination(t); |
219 |
jsr166 |
1.29 |
} |
220 |
|
|
|
221 |
|
|
/** |
222 |
|
|
* poll return null if no active putter |
223 |
dl |
1.4 |
*/ |
224 |
jsr166 |
1.36 |
public void testPoll() { testPoll(false); } |
225 |
|
|
public void testPoll_fair() { testPoll(true); } |
226 |
|
|
public void testPoll(boolean fair) { |
227 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
228 |
jsr166 |
1.14 |
assertNull(q.poll()); |
229 |
dl |
1.1 |
} |
230 |
|
|
|
231 |
dl |
1.4 |
/** |
232 |
jsr166 |
1.29 |
* timed poll with zero timeout times out if no active putter |
233 |
dl |
1.4 |
*/ |
234 |
jsr166 |
1.36 |
public void testTimedPoll0() { testTimedPoll0(false); } |
235 |
|
|
public void testTimedPoll0_fair() { testTimedPoll0(true); } |
236 |
|
|
public void testTimedPoll0(boolean fair) { |
237 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
238 |
jsr166 |
1.36 |
try { assertNull(q.poll(0, MILLISECONDS)); } |
239 |
|
|
catch (InterruptedException e) { threadUnexpectedException(e); } |
240 |
dl |
1.1 |
} |
241 |
|
|
|
242 |
dl |
1.4 |
/** |
243 |
jsr166 |
1.29 |
* timed poll with nonzero timeout times out if no active putter |
244 |
dl |
1.4 |
*/ |
245 |
jsr166 |
1.57 |
public void testTimedPoll() { |
246 |
jsr166 |
1.64 |
final boolean fair = randomBoolean(); |
247 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
248 |
jsr166 |
1.57 |
final long startTime = System.nanoTime(); |
249 |
jsr166 |
1.36 |
try { assertNull(q.poll(timeoutMillis(), MILLISECONDS)); } |
250 |
|
|
catch (InterruptedException e) { threadUnexpectedException(e); } |
251 |
jsr166 |
1.35 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
252 |
dl |
1.8 |
} |
253 |
|
|
|
254 |
|
|
/** |
255 |
jsr166 |
1.29 |
* timed poll before a delayed offer times out, returning null; |
256 |
|
|
* after offer succeeds; on interruption throws |
257 |
dl |
1.8 |
*/ |
258 |
jsr166 |
1.57 |
public void testTimedPollWithOffer() { |
259 |
jsr166 |
1.64 |
final boolean fair = randomBoolean(); |
260 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
261 |
jsr166 |
1.29 |
final CountDownLatch pleaseOffer = new CountDownLatch(1); |
262 |
jsr166 |
1.36 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
263 |
jsr166 |
1.29 |
Thread t = newStartedThread(new CheckedRunnable() { |
264 |
jsr166 |
1.14 |
public void realRun() throws InterruptedException { |
265 |
jsr166 |
1.36 |
long startTime = System.nanoTime(); |
266 |
|
|
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
267 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
268 |
jsr166 |
1.29 |
|
269 |
|
|
pleaseOffer.countDown(); |
270 |
jsr166 |
1.36 |
startTime = System.nanoTime(); |
271 |
jsr166 |
1.29 |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
272 |
|
|
|
273 |
jsr166 |
1.36 |
Thread.currentThread().interrupt(); |
274 |
jsr166 |
1.13 |
try { |
275 |
jsr166 |
1.63 |
q.poll(randomTimeout(), randomTimeUnit()); |
276 |
jsr166 |
1.29 |
shouldThrow(); |
277 |
jsr166 |
1.13 |
} catch (InterruptedException success) {} |
278 |
jsr166 |
1.36 |
assertFalse(Thread.interrupted()); |
279 |
|
|
|
280 |
|
|
pleaseInterrupt.countDown(); |
281 |
|
|
try { |
282 |
|
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
283 |
|
|
shouldThrow(); |
284 |
|
|
} catch (InterruptedException success) {} |
285 |
|
|
assertFalse(Thread.interrupted()); |
286 |
jsr166 |
1.50 |
|
287 |
|
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
288 |
jsr166 |
1.13 |
}}); |
289 |
|
|
|
290 |
jsr166 |
1.36 |
await(pleaseOffer); |
291 |
|
|
long startTime = System.nanoTime(); |
292 |
|
|
try { assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); } |
293 |
|
|
catch (InterruptedException e) { threadUnexpectedException(e); } |
294 |
jsr166 |
1.50 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
295 |
jsr166 |
1.30 |
|
296 |
jsr166 |
1.36 |
await(pleaseInterrupt); |
297 |
jsr166 |
1.63 |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
298 |
jsr166 |
1.13 |
t.interrupt(); |
299 |
jsr166 |
1.36 |
awaitTermination(t); |
300 |
jsr166 |
1.10 |
} |
301 |
dl |
1.1 |
|
302 |
dl |
1.4 |
/** |
303 |
jsr166 |
1.29 |
* peek() returns null if no active putter |
304 |
dl |
1.4 |
*/ |
305 |
jsr166 |
1.36 |
public void testPeek() { testPeek(false); } |
306 |
|
|
public void testPeek_fair() { testPeek(true); } |
307 |
|
|
public void testPeek(boolean fair) { |
308 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
309 |
jsr166 |
1.14 |
assertNull(q.peek()); |
310 |
dl |
1.1 |
} |
311 |
|
|
|
312 |
dl |
1.4 |
/** |
313 |
jsr166 |
1.36 |
* element() throws NoSuchElementException if no active putter |
314 |
dl |
1.4 |
*/ |
315 |
jsr166 |
1.36 |
public void testElement() { testElement(false); } |
316 |
|
|
public void testElement_fair() { testElement(true); } |
317 |
|
|
public void testElement(boolean fair) { |
318 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
319 |
dl |
1.1 |
try { |
320 |
|
|
q.element(); |
321 |
dl |
1.4 |
shouldThrow(); |
322 |
jsr166 |
1.13 |
} catch (NoSuchElementException success) {} |
323 |
dl |
1.1 |
} |
324 |
|
|
|
325 |
dl |
1.4 |
/** |
326 |
jsr166 |
1.36 |
* remove() throws NoSuchElementException if no active putter |
327 |
dl |
1.4 |
*/ |
328 |
jsr166 |
1.36 |
public void testRemove() { testRemove(false); } |
329 |
|
|
public void testRemove_fair() { testRemove(true); } |
330 |
|
|
public void testRemove(boolean fair) { |
331 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
332 |
dl |
1.1 |
try { |
333 |
|
|
q.remove(); |
334 |
dl |
1.4 |
shouldThrow(); |
335 |
jsr166 |
1.16 |
} catch (NoSuchElementException success) {} |
336 |
dl |
1.1 |
} |
337 |
|
|
|
338 |
dl |
1.4 |
/** |
339 |
dl |
1.5 |
* contains returns false |
340 |
dl |
1.4 |
*/ |
341 |
jsr166 |
1.36 |
public void testContains() { testContains(false); } |
342 |
|
|
public void testContains_fair() { testContains(true); } |
343 |
|
|
public void testContains(boolean fair) { |
344 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
345 |
dl |
1.5 |
assertFalse(q.contains(zero)); |
346 |
dl |
1.1 |
} |
347 |
|
|
|
348 |
dl |
1.4 |
/** |
349 |
dl |
1.5 |
* clear ensures isEmpty |
350 |
dl |
1.4 |
*/ |
351 |
jsr166 |
1.36 |
public void testClear() { testClear(false); } |
352 |
|
|
public void testClear_fair() { testClear(true); } |
353 |
|
|
public void testClear(boolean fair) { |
354 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
355 |
dl |
1.1 |
q.clear(); |
356 |
|
|
assertTrue(q.isEmpty()); |
357 |
|
|
} |
358 |
|
|
|
359 |
dl |
1.4 |
/** |
360 |
dl |
1.5 |
* containsAll returns false unless empty |
361 |
dl |
1.4 |
*/ |
362 |
jsr166 |
1.36 |
public void testContainsAll() { testContainsAll(false); } |
363 |
|
|
public void testContainsAll_fair() { testContainsAll(true); } |
364 |
|
|
public void testContainsAll(boolean fair) { |
365 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
366 |
|
|
Item[] empty = new Item[0]; |
367 |
dl |
1.5 |
assertTrue(q.containsAll(Arrays.asList(empty))); |
368 |
dl |
1.68 |
Item[] items = new Item[1]; items[0] = zero; |
369 |
|
|
assertFalse(q.containsAll(Arrays.asList(items))); |
370 |
dl |
1.1 |
} |
371 |
|
|
|
372 |
dl |
1.4 |
/** |
373 |
dl |
1.5 |
* retainAll returns false |
374 |
dl |
1.4 |
*/ |
375 |
jsr166 |
1.36 |
public void testRetainAll() { testRetainAll(false); } |
376 |
|
|
public void testRetainAll_fair() { testRetainAll(true); } |
377 |
|
|
public void testRetainAll(boolean fair) { |
378 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
379 |
|
|
Item[] empty = new Item[0]; |
380 |
dl |
1.5 |
assertFalse(q.retainAll(Arrays.asList(empty))); |
381 |
dl |
1.68 |
Item[] items = new Item[1]; items[0] = zero; |
382 |
|
|
assertFalse(q.retainAll(Arrays.asList(items))); |
383 |
dl |
1.1 |
} |
384 |
|
|
|
385 |
dl |
1.4 |
/** |
386 |
dl |
1.5 |
* removeAll returns false |
387 |
dl |
1.4 |
*/ |
388 |
jsr166 |
1.36 |
public void testRemoveAll() { testRemoveAll(false); } |
389 |
|
|
public void testRemoveAll_fair() { testRemoveAll(true); } |
390 |
|
|
public void testRemoveAll(boolean fair) { |
391 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
392 |
|
|
Item[] empty = new Item[0]; |
393 |
dl |
1.5 |
assertFalse(q.removeAll(Arrays.asList(empty))); |
394 |
dl |
1.68 |
Item[] items = new Item[1]; items[0] = zero; |
395 |
|
|
assertFalse(q.containsAll(Arrays.asList(items))); |
396 |
dl |
1.1 |
} |
397 |
|
|
|
398 |
dl |
1.4 |
/** |
399 |
dl |
1.5 |
* toArray is empty |
400 |
dl |
1.4 |
*/ |
401 |
jsr166 |
1.36 |
public void testToArray() { testToArray(false); } |
402 |
|
|
public void testToArray_fair() { testToArray(true); } |
403 |
|
|
public void testToArray(boolean fair) { |
404 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
405 |
jsr166 |
1.14 |
Object[] o = q.toArray(); |
406 |
dl |
1.68 |
mustEqual(0, o.length); |
407 |
dl |
1.1 |
} |
408 |
|
|
|
409 |
dl |
1.4 |
/** |
410 |
dl |
1.68 |
* toArray(Item array) returns its argument with the first |
411 |
jsr166 |
1.40 |
* element (if present) nulled out |
412 |
dl |
1.4 |
*/ |
413 |
jsr166 |
1.36 |
public void testToArray2() { testToArray2(false); } |
414 |
|
|
public void testToArray2_fair() { testToArray2(true); } |
415 |
|
|
public void testToArray2(boolean fair) { |
416 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
417 |
|
|
Item[] a; |
418 |
jsr166 |
1.40 |
|
419 |
dl |
1.68 |
a = new Item[0]; |
420 |
jsr166 |
1.40 |
assertSame(a, q.toArray(a)); |
421 |
jsr166 |
1.41 |
|
422 |
dl |
1.68 |
a = new Item[3]; |
423 |
|
|
Arrays.fill(a, fortytwo); |
424 |
jsr166 |
1.40 |
assertSame(a, q.toArray(a)); |
425 |
|
|
assertNull(a[0]); |
426 |
|
|
for (int i = 1; i < a.length; i++) |
427 |
dl |
1.68 |
mustEqual(42, a[i]); |
428 |
dl |
1.1 |
} |
429 |
jsr166 |
1.10 |
|
430 |
dl |
1.4 |
/** |
431 |
dl |
1.6 |
* toArray(null) throws NPE |
432 |
|
|
*/ |
433 |
jsr166 |
1.36 |
public void testToArray_null() { testToArray_null(false); } |
434 |
|
|
public void testToArray_null_fair() { testToArray_null(true); } |
435 |
|
|
public void testToArray_null(boolean fair) { |
436 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
437 |
jsr166 |
1.14 |
try { |
438 |
jsr166 |
1.67 |
Object[] unused = q.toArray((Object[])null); |
439 |
jsr166 |
1.14 |
shouldThrow(); |
440 |
|
|
} catch (NullPointerException success) {} |
441 |
dl |
1.6 |
} |
442 |
|
|
|
443 |
|
|
/** |
444 |
dl |
1.5 |
* iterator does not traverse any elements |
445 |
dl |
1.4 |
*/ |
446 |
jsr166 |
1.36 |
public void testIterator() { testIterator(false); } |
447 |
|
|
public void testIterator_fair() { testIterator(true); } |
448 |
|
|
public void testIterator(boolean fair) { |
449 |
dl |
1.68 |
assertIteratorExhausted(new SynchronousQueue<Item>(fair).iterator()); |
450 |
dl |
1.1 |
} |
451 |
|
|
|
452 |
dl |
1.4 |
/** |
453 |
jsr166 |
1.60 |
* iterator remove throws IllegalStateException |
454 |
dl |
1.4 |
*/ |
455 |
jsr166 |
1.36 |
public void testIteratorRemove() { testIteratorRemove(false); } |
456 |
|
|
public void testIteratorRemove_fair() { testIteratorRemove(true); } |
457 |
|
|
public void testIteratorRemove(boolean fair) { |
458 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
459 |
|
|
Iterator<? extends Item> it = q.iterator(); |
460 |
dl |
1.1 |
try { |
461 |
|
|
it.remove(); |
462 |
dl |
1.4 |
shouldThrow(); |
463 |
jsr166 |
1.13 |
} catch (IllegalStateException success) {} |
464 |
dl |
1.1 |
} |
465 |
|
|
|
466 |
dl |
1.4 |
/** |
467 |
dl |
1.5 |
* toString returns a non-null string |
468 |
dl |
1.4 |
*/ |
469 |
jsr166 |
1.36 |
public void testToString() { testToString(false); } |
470 |
|
|
public void testToString_fair() { testToString(true); } |
471 |
|
|
public void testToString(boolean fair) { |
472 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
473 |
dl |
1.1 |
String s = q.toString(); |
474 |
dl |
1.5 |
assertNotNull(s); |
475 |
jsr166 |
1.10 |
} |
476 |
dl |
1.1 |
|
477 |
dl |
1.4 |
/** |
478 |
dl |
1.5 |
* offer transfers elements across Executor tasks |
479 |
dl |
1.4 |
*/ |
480 |
jsr166 |
1.36 |
public void testOfferInExecutor() { testOfferInExecutor(false); } |
481 |
|
|
public void testOfferInExecutor_fair() { testOfferInExecutor(true); } |
482 |
|
|
public void testOfferInExecutor(boolean fair) { |
483 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
484 |
jsr166 |
1.35 |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
485 |
jsr166 |
1.49 |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
486 |
|
|
try (PoolCleaner cleaner = cleaner(executor)) { |
487 |
dl |
1.1 |
|
488 |
jsr166 |
1.49 |
executor.execute(new CheckedRunnable() { |
489 |
|
|
public void realRun() throws InterruptedException { |
490 |
|
|
assertFalse(q.offer(one)); |
491 |
|
|
threadsStarted.await(); |
492 |
|
|
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
493 |
dl |
1.68 |
mustEqual(0, q.remainingCapacity()); |
494 |
jsr166 |
1.49 |
}}); |
495 |
|
|
|
496 |
|
|
executor.execute(new CheckedRunnable() { |
497 |
|
|
public void realRun() throws InterruptedException { |
498 |
|
|
threadsStarted.await(); |
499 |
|
|
assertSame(one, q.take()); |
500 |
|
|
}}); |
501 |
|
|
} |
502 |
dl |
1.1 |
} |
503 |
|
|
|
504 |
dl |
1.4 |
/** |
505 |
jsr166 |
1.35 |
* timed poll retrieves elements across Executor threads |
506 |
dl |
1.4 |
*/ |
507 |
jsr166 |
1.36 |
public void testPollInExecutor() { testPollInExecutor(false); } |
508 |
|
|
public void testPollInExecutor_fair() { testPollInExecutor(true); } |
509 |
|
|
public void testPollInExecutor(boolean fair) { |
510 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
511 |
jsr166 |
1.35 |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
512 |
jsr166 |
1.49 |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
513 |
|
|
try (PoolCleaner cleaner = cleaner(executor)) { |
514 |
|
|
executor.execute(new CheckedRunnable() { |
515 |
|
|
public void realRun() throws InterruptedException { |
516 |
|
|
assertNull(q.poll()); |
517 |
|
|
threadsStarted.await(); |
518 |
|
|
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
519 |
|
|
assertTrue(q.isEmpty()); |
520 |
|
|
}}); |
521 |
|
|
|
522 |
|
|
executor.execute(new CheckedRunnable() { |
523 |
|
|
public void realRun() throws InterruptedException { |
524 |
|
|
threadsStarted.await(); |
525 |
|
|
q.put(one); |
526 |
|
|
}}); |
527 |
|
|
} |
528 |
dl |
1.2 |
} |
529 |
|
|
|
530 |
dl |
1.4 |
/** |
531 |
jsr166 |
1.61 |
* a deserialized/reserialized queue is usable |
532 |
dl |
1.4 |
*/ |
533 |
jsr166 |
1.40 |
public void testSerialization() { |
534 |
dl |
1.68 |
final SynchronousQueue<Item> x = new SynchronousQueue<Item>(); |
535 |
|
|
final SynchronousQueue<Item> y = new SynchronousQueue<Item>(false); |
536 |
|
|
final SynchronousQueue<Item> z = new SynchronousQueue<Item>(true); |
537 |
jsr166 |
1.40 |
assertSerialEquals(x, y); |
538 |
|
|
assertNotSerialEquals(x, z); |
539 |
dl |
1.68 |
SynchronousQueue[] rqs = { x, y, z }; |
540 |
|
|
@SuppressWarnings("unchecked") |
541 |
|
|
SynchronousQueue<Item>[] qs = (SynchronousQueue<Item>[])rqs; |
542 |
|
|
for (SynchronousQueue<Item> q : qs) { |
543 |
|
|
SynchronousQueue<Item> clone = serialClone(q); |
544 |
jsr166 |
1.41 |
assertNotSame(q, clone); |
545 |
jsr166 |
1.40 |
assertSerialEquals(q, clone); |
546 |
|
|
assertTrue(clone.isEmpty()); |
547 |
dl |
1.68 |
mustEqual(0, clone.size()); |
548 |
|
|
mustEqual(0, clone.remainingCapacity()); |
549 |
jsr166 |
1.40 |
assertFalse(clone.offer(zero)); |
550 |
|
|
} |
551 |
dl |
1.1 |
} |
552 |
dl |
1.6 |
|
553 |
|
|
/** |
554 |
|
|
* drainTo(c) of empty queue doesn't transfer elements |
555 |
jsr166 |
1.10 |
*/ |
556 |
jsr166 |
1.36 |
public void testDrainTo() { testDrainTo(false); } |
557 |
|
|
public void testDrainTo_fair() { testDrainTo(true); } |
558 |
|
|
public void testDrainTo(boolean fair) { |
559 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
560 |
|
|
ArrayList<Item> l = new ArrayList<Item>(); |
561 |
dl |
1.6 |
q.drainTo(l); |
562 |
dl |
1.68 |
mustEqual(0, q.size()); |
563 |
|
|
mustEqual(0, l.size()); |
564 |
dl |
1.6 |
} |
565 |
|
|
|
566 |
|
|
/** |
567 |
|
|
* drainTo empties queue, unblocking a waiting put. |
568 |
jsr166 |
1.10 |
*/ |
569 |
jsr166 |
1.36 |
public void testDrainToWithActivePut() { testDrainToWithActivePut(false); } |
570 |
|
|
public void testDrainToWithActivePut_fair() { testDrainToWithActivePut(true); } |
571 |
|
|
public void testDrainToWithActivePut(boolean fair) { |
572 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
573 |
jsr166 |
1.35 |
Thread t = newStartedThread(new CheckedRunnable() { |
574 |
jsr166 |
1.14 |
public void realRun() throws InterruptedException { |
575 |
jsr166 |
1.35 |
q.put(one); |
576 |
jsr166 |
1.13 |
}}); |
577 |
|
|
|
578 |
dl |
1.68 |
ArrayList<Item> l = new ArrayList<Item>(); |
579 |
jsr166 |
1.35 |
long startTime = System.nanoTime(); |
580 |
|
|
while (l.isEmpty()) { |
581 |
|
|
q.drainTo(l); |
582 |
|
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
583 |
|
|
fail("timed out"); |
584 |
|
|
Thread.yield(); |
585 |
|
|
} |
586 |
dl |
1.68 |
mustEqual(1, l.size()); |
587 |
jsr166 |
1.35 |
assertSame(one, l.get(0)); |
588 |
|
|
awaitTermination(t); |
589 |
dl |
1.6 |
} |
590 |
|
|
|
591 |
|
|
/** |
592 |
|
|
* drainTo(c, n) empties up to n elements of queue into c |
593 |
jsr166 |
1.10 |
*/ |
594 |
jsr166 |
1.13 |
public void testDrainToN() throws InterruptedException { |
595 |
dl |
1.68 |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(); |
596 |
jsr166 |
1.35 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
597 |
jsr166 |
1.14 |
public void realRun() throws InterruptedException { |
598 |
jsr166 |
1.13 |
q.put(one); |
599 |
|
|
}}); |
600 |
|
|
|
601 |
jsr166 |
1.35 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
602 |
jsr166 |
1.14 |
public void realRun() throws InterruptedException { |
603 |
jsr166 |
1.13 |
q.put(two); |
604 |
|
|
}}); |
605 |
dl |
1.6 |
|
606 |
dl |
1.68 |
ArrayList<Item> l = new ArrayList<Item>(); |
607 |
jsr166 |
1.48 |
int drained; |
608 |
|
|
while ((drained = q.drainTo(l, 1)) == 0) Thread.yield(); |
609 |
dl |
1.68 |
mustEqual(1, drained); |
610 |
|
|
mustEqual(1, l.size()); |
611 |
jsr166 |
1.48 |
while ((drained = q.drainTo(l, 1)) == 0) Thread.yield(); |
612 |
dl |
1.68 |
mustEqual(1, drained); |
613 |
|
|
mustEqual(2, l.size()); |
614 |
jsr166 |
1.13 |
assertTrue(l.contains(one)); |
615 |
|
|
assertTrue(l.contains(two)); |
616 |
jsr166 |
1.35 |
awaitTermination(t1); |
617 |
|
|
awaitTermination(t2); |
618 |
dl |
1.6 |
} |
619 |
|
|
|
620 |
jsr166 |
1.42 |
/** |
621 |
|
|
* remove(null), contains(null) always return false |
622 |
|
|
*/ |
623 |
|
|
public void testNeverContainsNull() { |
624 |
dl |
1.68 |
Collection<?> q = new SynchronousQueue<Item>(); |
625 |
jsr166 |
1.42 |
assertFalse(q.contains(null)); |
626 |
|
|
assertFalse(q.remove(null)); |
627 |
|
|
} |
628 |
|
|
|
629 |
dl |
1.1 |
} |