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