1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.*; |
11 |
< |
import java.util.concurrent.*; |
12 |
< |
import java.io.*; |
9 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
|
11 |
< |
public class SynchronousQueueTest extends JSR166TestCase { |
11 |
> |
import java.util.ArrayList; |
12 |
> |
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 |
|
|
22 |
< |
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
< |
} |
22 |
> |
import junit.framework.Test; |
23 |
|
|
24 |
< |
public static Test suite() { |
25 |
< |
return new TestSuite(SynchronousQueueTest.class); |
24 |
> |
public class SynchronousQueueTest extends JSR166TestCase { |
25 |
> |
|
26 |
> |
public static class Fair extends BlockingQueueTest { |
27 |
> |
protected BlockingQueue emptyCollection() { |
28 |
> |
return new SynchronousQueue(true); |
29 |
> |
} |
30 |
|
} |
31 |
|
|
32 |
< |
/** |
33 |
< |
* A SynchronousQueue is both empty and full |
34 |
< |
*/ |
35 |
< |
public void testEmptyFull() { |
28 |
< |
SynchronousQueue q = new SynchronousQueue(); |
29 |
< |
assertTrue(q.isEmpty()); |
30 |
< |
assertEquals(0, q.size()); |
31 |
< |
assertEquals(0, q.remainingCapacity()); |
32 |
< |
assertFalse(q.offer(zero)); |
32 |
> |
public static class NonFair extends BlockingQueueTest { |
33 |
> |
protected BlockingQueue emptyCollection() { |
34 |
> |
return new SynchronousQueue(false); |
35 |
> |
} |
36 |
|
} |
37 |
|
|
38 |
< |
/** |
39 |
< |
* A fair SynchronousQueue is both empty and full |
37 |
< |
*/ |
38 |
< |
public void testFairEmptyFull() { |
39 |
< |
SynchronousQueue q = new SynchronousQueue(true); |
40 |
< |
assertTrue(q.isEmpty()); |
41 |
< |
assertEquals(0, q.size()); |
42 |
< |
assertEquals(0, q.remainingCapacity()); |
43 |
< |
assertFalse(q.offer(zero)); |
38 |
> |
public static void main(String[] args) { |
39 |
> |
main(suite(), args); |
40 |
|
} |
41 |
|
|
42 |
< |
/** |
43 |
< |
* offer(null) throws NPE |
44 |
< |
*/ |
45 |
< |
public void testOfferNull() { |
50 |
< |
try { |
51 |
< |
SynchronousQueue q = new SynchronousQueue(); |
52 |
< |
q.offer(null); |
53 |
< |
shouldThrow(); |
54 |
< |
} catch (NullPointerException success) { } |
42 |
> |
public static Test suite() { |
43 |
> |
return newTestSuite(SynchronousQueueTest.class, |
44 |
> |
new Fair().testSuite(), |
45 |
> |
new NonFair().testSuite()); |
46 |
|
} |
47 |
|
|
48 |
|
/** |
49 |
< |
* add(null) throws NPE |
49 |
> |
* Any SynchronousQueue is both empty and full |
50 |
|
*/ |
51 |
< |
public void testAddNull() { |
52 |
< |
try { |
53 |
< |
SynchronousQueue q = new SynchronousQueue(); |
54 |
< |
q.add(null); |
55 |
< |
shouldThrow(); |
56 |
< |
} catch (NullPointerException success) { } |
51 |
> |
public void testEmptyFull() { testEmptyFull(false); } |
52 |
> |
public void testEmptyFull_fair() { testEmptyFull(true); } |
53 |
> |
public void testEmptyFull(boolean fair) { |
54 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
55 |
> |
assertTrue(q.isEmpty()); |
56 |
> |
mustEqual(0, q.size()); |
57 |
> |
mustEqual(0, q.remainingCapacity()); |
58 |
> |
assertFalse(q.offer(zero)); |
59 |
|
} |
60 |
|
|
61 |
|
/** |
62 |
|
* offer fails if no active taker |
63 |
|
*/ |
64 |
< |
public void testOffer() { |
65 |
< |
SynchronousQueue q = new SynchronousQueue(); |
64 |
> |
public void testOffer() { testOffer(false); } |
65 |
> |
public void testOffer_fair() { testOffer(true); } |
66 |
> |
public void testOffer(boolean fair) { |
67 |
> |
SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
68 |
|
assertFalse(q.offer(one)); |
69 |
|
} |
70 |
|
|
71 |
|
/** |
72 |
< |
* add throws ISE if no active taker |
72 |
> |
* add throws IllegalStateException if no active taker |
73 |
|
*/ |
74 |
< |
public void testAdd() { |
75 |
< |
try { |
76 |
< |
SynchronousQueue q = new SynchronousQueue(); |
77 |
< |
assertEquals(0, q.remainingCapacity()); |
78 |
< |
q.add(one); |
84 |
< |
shouldThrow(); |
85 |
< |
} catch (IllegalStateException success) { |
86 |
< |
} |
87 |
< |
} |
88 |
< |
|
89 |
< |
/** |
90 |
< |
* addAll(null) throws NPE |
91 |
< |
*/ |
92 |
< |
public void testAddAll1() { |
74 |
> |
public void testAdd() { testAdd(false); } |
75 |
> |
public void testAdd_fair() { testAdd(true); } |
76 |
> |
public void testAdd(boolean fair) { |
77 |
> |
SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
78 |
> |
mustEqual(0, q.remainingCapacity()); |
79 |
|
try { |
80 |
< |
SynchronousQueue q = new SynchronousQueue(); |
95 |
< |
q.addAll(null); |
80 |
> |
q.add(one); |
81 |
|
shouldThrow(); |
82 |
< |
} |
98 |
< |
catch (NullPointerException success) {} |
82 |
> |
} catch (IllegalStateException success) {} |
83 |
|
} |
84 |
|
|
85 |
|
/** |
86 |
< |
* addAll(this) throws IAE |
86 |
> |
* addAll(this) throws IllegalArgumentException |
87 |
|
*/ |
88 |
< |
public void testAddAllSelf() { |
88 |
> |
public void testAddAll_self() { testAddAll_self(false); } |
89 |
> |
public void testAddAll_self_fair() { testAddAll_self(true); } |
90 |
> |
public void testAddAll_self(boolean fair) { |
91 |
> |
SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
92 |
|
try { |
106 |
– |
SynchronousQueue q = new SynchronousQueue(); |
93 |
|
q.addAll(q); |
94 |
|
shouldThrow(); |
95 |
< |
} |
110 |
< |
catch (IllegalArgumentException success) {} |
95 |
> |
} catch (IllegalArgumentException success) {} |
96 |
|
} |
97 |
|
|
98 |
< |
/** |
99 |
< |
* addAll of a collection with null elements throws NPE |
115 |
< |
*/ |
116 |
< |
public void testAddAll2() { |
117 |
< |
try { |
118 |
< |
SynchronousQueue q = new SynchronousQueue(); |
119 |
< |
Integer[] ints = new Integer[1]; |
120 |
< |
q.addAll(Arrays.asList(ints)); |
121 |
< |
shouldThrow(); |
122 |
< |
} |
123 |
< |
catch (NullPointerException success) {} |
124 |
< |
} |
125 |
< |
/** |
126 |
< |
* addAll throws ISE if no active taker |
98 |
> |
/**S |
99 |
> |
* addAll throws IllegalStateException if no active taker |
100 |
|
*/ |
101 |
< |
public void testAddAll4() { |
101 |
> |
public void testAddAll_ISE() { testAddAll_ISE(false); } |
102 |
> |
public void testAddAll_ISE_fair() { testAddAll_ISE(true); } |
103 |
> |
public void testAddAll_ISE(boolean fair) { |
104 |
> |
SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
105 |
> |
Item[] items = seqItems(1); |
106 |
> |
Collection<Item> coll = Arrays.asList(items); |
107 |
|
try { |
108 |
< |
SynchronousQueue q = new SynchronousQueue(); |
131 |
< |
Integer[] ints = new Integer[1]; |
132 |
< |
for (int i = 0; i < 1; ++i) |
133 |
< |
ints[i] = new Integer(i); |
134 |
< |
q.addAll(Arrays.asList(ints)); |
108 |
> |
q.addAll(coll); |
109 |
|
shouldThrow(); |
110 |
< |
} |
137 |
< |
catch (IllegalStateException success) {} |
110 |
> |
} catch (IllegalStateException success) {} |
111 |
|
} |
112 |
|
|
113 |
|
/** |
141 |
– |
* put(null) throws NPE |
142 |
– |
*/ |
143 |
– |
public void testPutNull() { |
144 |
– |
try { |
145 |
– |
SynchronousQueue q = new SynchronousQueue(); |
146 |
– |
q.put(null); |
147 |
– |
shouldThrow(); |
148 |
– |
} |
149 |
– |
catch (NullPointerException success) { |
150 |
– |
} |
151 |
– |
catch (InterruptedException ie) { |
152 |
– |
unexpectedException(); |
153 |
– |
} |
154 |
– |
} |
155 |
– |
|
156 |
– |
/** |
114 |
|
* put blocks interruptibly if no active taker |
115 |
|
*/ |
116 |
< |
public void testBlockingPut() { |
117 |
< |
Thread t = new Thread(new Runnable() { |
118 |
< |
public void run() { |
119 |
< |
try { |
120 |
< |
SynchronousQueue q = new SynchronousQueue(); |
121 |
< |
q.put(zero); |
122 |
< |
threadShouldThrow(); |
123 |
< |
} catch (InterruptedException ie) { |
124 |
< |
} |
125 |
< |
}}); |
126 |
< |
t.start(); |
127 |
< |
try { |
128 |
< |
Thread.sleep(SHORT_DELAY_MS); |
172 |
< |
t.interrupt(); |
173 |
< |
t.join(); |
174 |
< |
} |
175 |
< |
catch (InterruptedException ie) { |
176 |
< |
unexpectedException(); |
177 |
< |
} |
178 |
< |
} |
179 |
< |
|
180 |
< |
/** |
181 |
< |
* put blocks waiting for take |
182 |
< |
*/ |
183 |
< |
public void testPutWithTake() { |
184 |
< |
final SynchronousQueue q = new SynchronousQueue(); |
185 |
< |
Thread t = new Thread(new Runnable() { |
186 |
< |
public void run() { |
187 |
< |
int added = 0; |
188 |
< |
try { |
189 |
< |
q.put(new Object()); |
190 |
< |
++added; |
191 |
< |
q.put(new Object()); |
192 |
< |
++added; |
193 |
< |
q.put(new Object()); |
194 |
< |
++added; |
195 |
< |
q.put(new Object()); |
196 |
< |
++added; |
197 |
< |
threadShouldThrow(); |
198 |
< |
} catch (InterruptedException e) { |
199 |
< |
assertTrue(added >= 1); |
200 |
< |
} |
201 |
< |
} |
202 |
< |
}); |
203 |
< |
try { |
204 |
< |
t.start(); |
205 |
< |
Thread.sleep(SHORT_DELAY_MS); |
206 |
< |
q.take(); |
207 |
< |
Thread.sleep(SHORT_DELAY_MS); |
208 |
< |
t.interrupt(); |
209 |
< |
t.join(); |
210 |
< |
} catch (Exception e) { |
211 |
< |
unexpectedException(); |
212 |
< |
} |
213 |
< |
} |
214 |
< |
|
215 |
< |
/** |
216 |
< |
* timed offer times out if elements not taken |
217 |
< |
*/ |
218 |
< |
public void testTimedOffer() { |
219 |
< |
final SynchronousQueue q = new SynchronousQueue(); |
220 |
< |
Thread t = new Thread(new Runnable() { |
221 |
< |
public void run() { |
222 |
< |
try { |
223 |
< |
|
224 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
225 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
226 |
< |
threadShouldThrow(); |
227 |
< |
} catch (InterruptedException success) {} |
228 |
< |
} |
229 |
< |
}); |
116 |
> |
public void testBlockingPut() { testBlockingPut(false); } |
117 |
> |
public void testBlockingPut_fair() { testBlockingPut(true); } |
118 |
> |
public void testBlockingPut(boolean fair) { |
119 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
120 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
121 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
122 |
> |
public void realRun() throws InterruptedException { |
123 |
> |
Thread.currentThread().interrupt(); |
124 |
> |
try { |
125 |
> |
q.put(ninetynine); |
126 |
> |
shouldThrow(); |
127 |
> |
} catch (InterruptedException success) {} |
128 |
> |
assertFalse(Thread.interrupted()); |
129 |
|
|
130 |
< |
try { |
131 |
< |
t.start(); |
132 |
< |
Thread.sleep(SMALL_DELAY_MS); |
133 |
< |
t.interrupt(); |
134 |
< |
t.join(); |
135 |
< |
} catch (Exception e) { |
136 |
< |
unexpectedException(); |
137 |
< |
} |
130 |
> |
pleaseInterrupt.countDown(); |
131 |
> |
try { |
132 |
> |
q.put(ninetynine); |
133 |
> |
shouldThrow(); |
134 |
> |
} catch (InterruptedException success) {} |
135 |
> |
assertFalse(Thread.interrupted()); |
136 |
> |
}}); |
137 |
> |
|
138 |
> |
await(pleaseInterrupt); |
139 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
140 |
> |
t.interrupt(); |
141 |
> |
awaitTermination(t); |
142 |
> |
mustEqual(0, q.remainingCapacity()); |
143 |
|
} |
144 |
|
|
241 |
– |
|
145 |
|
/** |
146 |
< |
* take blocks interruptibly when empty |
147 |
< |
*/ |
148 |
< |
public void testTakeFromEmpty() { |
149 |
< |
final SynchronousQueue q = new SynchronousQueue(); |
150 |
< |
Thread t = new Thread(new Runnable() { |
151 |
< |
public void run() { |
152 |
< |
try { |
153 |
< |
q.take(); |
154 |
< |
threadShouldThrow(); |
155 |
< |
} catch (InterruptedException success) { } |
156 |
< |
} |
157 |
< |
}); |
255 |
< |
try { |
256 |
< |
t.start(); |
257 |
< |
Thread.sleep(SHORT_DELAY_MS); |
258 |
< |
t.interrupt(); |
259 |
< |
t.join(); |
260 |
< |
} catch (Exception e) { |
261 |
< |
unexpectedException(); |
262 |
< |
} |
263 |
< |
} |
146 |
> |
* put blocks interruptibly waiting for take |
147 |
> |
*/ |
148 |
> |
public void testPutWithTake() { testPutWithTake(false); } |
149 |
> |
public void testPutWithTake_fair() { testPutWithTake(true); } |
150 |
> |
public void testPutWithTake(boolean fair) { |
151 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
152 |
> |
final CountDownLatch pleaseTake = new CountDownLatch(1); |
153 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
154 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
155 |
> |
public void realRun() throws InterruptedException { |
156 |
> |
pleaseTake.countDown(); |
157 |
> |
q.put(one); |
158 |
|
|
159 |
+ |
Thread.currentThread().interrupt(); |
160 |
+ |
try { |
161 |
+ |
q.put(ninetynine); |
162 |
+ |
shouldThrow(); |
163 |
+ |
} catch (InterruptedException success) {} |
164 |
+ |
assertFalse(Thread.interrupted()); |
165 |
|
|
166 |
< |
/** |
167 |
< |
* put blocks interruptibly if no active taker |
168 |
< |
*/ |
169 |
< |
public void testFairBlockingPut() { |
170 |
< |
Thread t = new Thread(new Runnable() { |
171 |
< |
public void run() { |
172 |
< |
try { |
173 |
< |
SynchronousQueue q = new SynchronousQueue(true); |
174 |
< |
q.put(zero); |
175 |
< |
threadShouldThrow(); |
176 |
< |
} catch (InterruptedException ie) { |
177 |
< |
} |
178 |
< |
}}); |
179 |
< |
t.start(); |
180 |
< |
try { |
181 |
< |
Thread.sleep(SHORT_DELAY_MS); |
182 |
< |
t.interrupt(); |
183 |
< |
t.join(); |
284 |
< |
} |
285 |
< |
catch (InterruptedException ie) { |
286 |
< |
unexpectedException(); |
287 |
< |
} |
288 |
< |
} |
289 |
< |
|
290 |
< |
/** |
291 |
< |
* put blocks waiting for take |
292 |
< |
*/ |
293 |
< |
public void testFairPutWithTake() { |
294 |
< |
final SynchronousQueue q = new SynchronousQueue(true); |
295 |
< |
Thread t = new Thread(new Runnable() { |
296 |
< |
public void run() { |
297 |
< |
int added = 0; |
298 |
< |
try { |
299 |
< |
q.put(new Object()); |
300 |
< |
++added; |
301 |
< |
q.put(new Object()); |
302 |
< |
++added; |
303 |
< |
q.put(new Object()); |
304 |
< |
++added; |
305 |
< |
q.put(new Object()); |
306 |
< |
++added; |
307 |
< |
threadShouldThrow(); |
308 |
< |
} catch (InterruptedException e) { |
309 |
< |
assertTrue(added >= 1); |
310 |
< |
} |
311 |
< |
} |
312 |
< |
}); |
313 |
< |
try { |
314 |
< |
t.start(); |
315 |
< |
Thread.sleep(SHORT_DELAY_MS); |
316 |
< |
q.take(); |
317 |
< |
Thread.sleep(SHORT_DELAY_MS); |
318 |
< |
t.interrupt(); |
319 |
< |
t.join(); |
320 |
< |
} catch (Exception e) { |
321 |
< |
unexpectedException(); |
322 |
< |
} |
166 |
> |
pleaseInterrupt.countDown(); |
167 |
> |
try { |
168 |
> |
q.put(ninetynine); |
169 |
> |
shouldThrow(); |
170 |
> |
} catch (InterruptedException success) {} |
171 |
> |
assertFalse(Thread.interrupted()); |
172 |
> |
}}); |
173 |
> |
|
174 |
> |
await(pleaseTake); |
175 |
> |
mustEqual(0, q.remainingCapacity()); |
176 |
> |
try { assertSame(one, q.take()); } |
177 |
> |
catch (InterruptedException e) { threadUnexpectedException(e); } |
178 |
> |
|
179 |
> |
await(pleaseInterrupt); |
180 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
181 |
> |
t.interrupt(); |
182 |
> |
awaitTermination(t); |
183 |
> |
mustEqual(0, q.remainingCapacity()); |
184 |
|
} |
185 |
|
|
186 |
|
/** |
187 |
|
* timed offer times out if elements not taken |
188 |
|
*/ |
189 |
< |
public void testFairTimedOffer() { |
190 |
< |
final SynchronousQueue q = new SynchronousQueue(true); |
191 |
< |
Thread t = new Thread(new Runnable() { |
192 |
< |
public void run() { |
193 |
< |
try { |
194 |
< |
|
195 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
335 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
336 |
< |
threadShouldThrow(); |
337 |
< |
} catch (InterruptedException success) {} |
338 |
< |
} |
339 |
< |
}); |
189 |
> |
public void testTimedOffer() { |
190 |
> |
final boolean fair = randomBoolean(); |
191 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
192 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
193 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
194 |
> |
public void realRun() throws InterruptedException { |
195 |
> |
long startTime = System.nanoTime(); |
196 |
|
|
197 |
< |
try { |
198 |
< |
t.start(); |
343 |
< |
Thread.sleep(SMALL_DELAY_MS); |
344 |
< |
t.interrupt(); |
345 |
< |
t.join(); |
346 |
< |
} catch (Exception e) { |
347 |
< |
unexpectedException(); |
348 |
< |
} |
349 |
< |
} |
197 |
> |
assertFalse(q.offer(zero, timeoutMillis(), MILLISECONDS)); |
198 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
199 |
|
|
200 |
+ |
Thread.currentThread().interrupt(); |
201 |
+ |
try { |
202 |
+ |
q.offer(one, randomTimeout(), randomTimeUnit()); |
203 |
+ |
shouldThrow(); |
204 |
+ |
} catch (InterruptedException success) {} |
205 |
+ |
assertFalse(Thread.interrupted()); |
206 |
|
|
207 |
< |
/** |
208 |
< |
* take blocks interruptibly when empty |
209 |
< |
*/ |
210 |
< |
public void testFairTakeFromEmpty() { |
211 |
< |
final SynchronousQueue q = new SynchronousQueue(true); |
212 |
< |
Thread t = new Thread(new Runnable() { |
213 |
< |
public void run() { |
214 |
< |
try { |
215 |
< |
q.take(); |
216 |
< |
threadShouldThrow(); |
217 |
< |
} catch (InterruptedException success) { } |
218 |
< |
} |
364 |
< |
}); |
365 |
< |
try { |
366 |
< |
t.start(); |
367 |
< |
Thread.sleep(SHORT_DELAY_MS); |
368 |
< |
t.interrupt(); |
369 |
< |
t.join(); |
370 |
< |
} catch (Exception e) { |
371 |
< |
unexpectedException(); |
372 |
< |
} |
207 |
> |
pleaseInterrupt.countDown(); |
208 |
> |
try { |
209 |
> |
q.offer(two, LONGER_DELAY_MS, MILLISECONDS); |
210 |
> |
shouldThrow(); |
211 |
> |
} catch (InterruptedException success) {} |
212 |
> |
assertFalse(Thread.interrupted()); |
213 |
> |
}}); |
214 |
> |
|
215 |
> |
await(pleaseInterrupt); |
216 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
217 |
> |
t.interrupt(); |
218 |
> |
awaitTermination(t); |
219 |
|
} |
220 |
|
|
221 |
|
/** |
222 |
< |
* poll fails unless active taker |
222 |
> |
* poll return null if no active putter |
223 |
|
*/ |
224 |
< |
public void testPoll() { |
225 |
< |
SynchronousQueue q = new SynchronousQueue(); |
226 |
< |
assertNull(q.poll()); |
224 |
> |
public void testPoll() { testPoll(false); } |
225 |
> |
public void testPoll_fair() { testPoll(true); } |
226 |
> |
public void testPoll(boolean fair) { |
227 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
228 |
> |
assertNull(q.poll()); |
229 |
|
} |
230 |
|
|
231 |
|
/** |
232 |
< |
* timed pool with zero timeout times out if no active taker |
232 |
> |
* timed poll with zero timeout times out if no active putter |
233 |
|
*/ |
234 |
< |
public void testTimedPoll0() { |
235 |
< |
try { |
236 |
< |
SynchronousQueue q = new SynchronousQueue(); |
237 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
238 |
< |
} catch (InterruptedException e) { |
239 |
< |
unexpectedException(); |
392 |
< |
} |
234 |
> |
public void testTimedPoll0() { testTimedPoll0(false); } |
235 |
> |
public void testTimedPoll0_fair() { testTimedPoll0(true); } |
236 |
> |
public void testTimedPoll0(boolean fair) { |
237 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
238 |
> |
try { assertNull(q.poll(0, MILLISECONDS)); } |
239 |
> |
catch (InterruptedException e) { threadUnexpectedException(e); } |
240 |
|
} |
241 |
|
|
242 |
|
/** |
243 |
< |
* timed pool with nonzero timeout times out if no active taker |
243 |
> |
* timed poll with nonzero timeout times out if no active putter |
244 |
|
*/ |
245 |
|
public void testTimedPoll() { |
246 |
< |
try { |
247 |
< |
SynchronousQueue q = new SynchronousQueue(); |
248 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
249 |
< |
} catch (InterruptedException e) { |
250 |
< |
unexpectedException(); |
251 |
< |
} |
405 |
< |
} |
406 |
< |
|
407 |
< |
/** |
408 |
< |
* Interrupted timed poll throws InterruptedException instead of |
409 |
< |
* returning timeout status |
410 |
< |
*/ |
411 |
< |
public void testInterruptedTimedPoll() { |
412 |
< |
Thread t = new Thread(new Runnable() { |
413 |
< |
public void run() { |
414 |
< |
try { |
415 |
< |
SynchronousQueue q = new SynchronousQueue(); |
416 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
417 |
< |
} catch (InterruptedException success) { |
418 |
< |
} |
419 |
< |
}}); |
420 |
< |
t.start(); |
421 |
< |
try { |
422 |
< |
Thread.sleep(SHORT_DELAY_MS); |
423 |
< |
t.interrupt(); |
424 |
< |
t.join(); |
425 |
< |
} |
426 |
< |
catch (InterruptedException ie) { |
427 |
< |
unexpectedException(); |
428 |
< |
} |
246 |
> |
final boolean fair = randomBoolean(); |
247 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
248 |
> |
final long startTime = System.nanoTime(); |
249 |
> |
try { assertNull(q.poll(timeoutMillis(), MILLISECONDS)); } |
250 |
> |
catch (InterruptedException e) { threadUnexpectedException(e); } |
251 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
252 |
|
} |
253 |
|
|
254 |
|
/** |
255 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
256 |
< |
* on interruption throws |
255 |
> |
* timed poll before a delayed offer times out, returning null; |
256 |
> |
* after offer succeeds; on interruption throws |
257 |
|
*/ |
258 |
|
public void testTimedPollWithOffer() { |
259 |
< |
final SynchronousQueue q = new SynchronousQueue(); |
260 |
< |
Thread t = new Thread(new Runnable() { |
261 |
< |
public void run() { |
262 |
< |
try { |
263 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
264 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
265 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
266 |
< |
threadShouldThrow(); |
267 |
< |
} catch (InterruptedException success) { } |
268 |
< |
} |
269 |
< |
}); |
270 |
< |
try { |
271 |
< |
t.start(); |
449 |
< |
Thread.sleep(SMALL_DELAY_MS); |
450 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
451 |
< |
t.interrupt(); |
452 |
< |
t.join(); |
453 |
< |
} catch (Exception e) { |
454 |
< |
unexpectedException(); |
455 |
< |
} |
456 |
< |
} |
259 |
> |
final boolean fair = randomBoolean(); |
260 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
261 |
> |
final CountDownLatch pleaseOffer = new CountDownLatch(1); |
262 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
263 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
264 |
> |
public void realRun() throws InterruptedException { |
265 |
> |
long startTime = System.nanoTime(); |
266 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
267 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
268 |
> |
|
269 |
> |
pleaseOffer.countDown(); |
270 |
> |
startTime = System.nanoTime(); |
271 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
272 |
|
|
273 |
< |
/** |
274 |
< |
* Interrupted timed poll throws InterruptedException instead of |
275 |
< |
* returning timeout status |
276 |
< |
*/ |
277 |
< |
public void testFairInterruptedTimedPoll() { |
278 |
< |
Thread t = new Thread(new Runnable() { |
464 |
< |
public void run() { |
465 |
< |
try { |
466 |
< |
SynchronousQueue q = new SynchronousQueue(true); |
467 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
468 |
< |
} catch (InterruptedException success) { |
469 |
< |
} |
470 |
< |
}}); |
471 |
< |
t.start(); |
472 |
< |
try { |
473 |
< |
Thread.sleep(SHORT_DELAY_MS); |
474 |
< |
t.interrupt(); |
475 |
< |
t.join(); |
476 |
< |
} |
477 |
< |
catch (InterruptedException ie) { |
478 |
< |
unexpectedException(); |
479 |
< |
} |
480 |
< |
} |
273 |
> |
Thread.currentThread().interrupt(); |
274 |
> |
try { |
275 |
> |
q.poll(randomTimeout(), randomTimeUnit()); |
276 |
> |
shouldThrow(); |
277 |
> |
} catch (InterruptedException success) {} |
278 |
> |
assertFalse(Thread.interrupted()); |
279 |
|
|
280 |
< |
/** |
281 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
282 |
< |
* on interruption throws |
283 |
< |
*/ |
284 |
< |
public void testFairTimedPollWithOffer() { |
285 |
< |
final SynchronousQueue q = new SynchronousQueue(true); |
286 |
< |
Thread t = new Thread(new Runnable() { |
287 |
< |
public void run() { |
288 |
< |
try { |
491 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
492 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
493 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
494 |
< |
threadShouldThrow(); |
495 |
< |
} catch (InterruptedException success) { } |
496 |
< |
} |
497 |
< |
}); |
498 |
< |
try { |
499 |
< |
t.start(); |
500 |
< |
Thread.sleep(SMALL_DELAY_MS); |
501 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
502 |
< |
t.interrupt(); |
503 |
< |
t.join(); |
504 |
< |
} catch (Exception e) { |
505 |
< |
unexpectedException(); |
506 |
< |
} |
507 |
< |
} |
280 |
> |
pleaseInterrupt.countDown(); |
281 |
> |
try { |
282 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
283 |
> |
shouldThrow(); |
284 |
> |
} catch (InterruptedException success) {} |
285 |
> |
assertFalse(Thread.interrupted()); |
286 |
> |
|
287 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
288 |
> |
}}); |
289 |
|
|
290 |
+ |
await(pleaseOffer); |
291 |
+ |
long startTime = System.nanoTime(); |
292 |
+ |
try { assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); } |
293 |
+ |
catch (InterruptedException e) { threadUnexpectedException(e); } |
294 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
295 |
+ |
|
296 |
+ |
await(pleaseInterrupt); |
297 |
+ |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
298 |
+ |
t.interrupt(); |
299 |
+ |
awaitTermination(t); |
300 |
+ |
} |
301 |
|
|
302 |
|
/** |
303 |
< |
* peek returns null |
303 |
> |
* peek() returns null if no active putter |
304 |
|
*/ |
305 |
< |
public void testPeek() { |
306 |
< |
SynchronousQueue q = new SynchronousQueue(); |
307 |
< |
assertNull(q.peek()); |
305 |
> |
public void testPeek() { testPeek(false); } |
306 |
> |
public void testPeek_fair() { testPeek(true); } |
307 |
> |
public void testPeek(boolean fair) { |
308 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
309 |
> |
assertNull(q.peek()); |
310 |
|
} |
311 |
|
|
312 |
|
/** |
313 |
< |
* element throws NSEE |
313 |
> |
* element() throws NoSuchElementException if no active putter |
314 |
|
*/ |
315 |
< |
public void testElement() { |
316 |
< |
SynchronousQueue q = new SynchronousQueue(); |
315 |
> |
public void testElement() { testElement(false); } |
316 |
> |
public void testElement_fair() { testElement(true); } |
317 |
> |
public void testElement(boolean fair) { |
318 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
319 |
|
try { |
320 |
|
q.element(); |
321 |
|
shouldThrow(); |
322 |
< |
} |
527 |
< |
catch (NoSuchElementException success) {} |
322 |
> |
} catch (NoSuchElementException success) {} |
323 |
|
} |
324 |
|
|
325 |
|
/** |
326 |
< |
* remove throws NSEE if no active taker |
326 |
> |
* remove() throws NoSuchElementException if no active putter |
327 |
|
*/ |
328 |
< |
public void testRemove() { |
329 |
< |
SynchronousQueue q = new SynchronousQueue(); |
328 |
> |
public void testRemove() { testRemove(false); } |
329 |
> |
public void testRemove_fair() { testRemove(true); } |
330 |
> |
public void testRemove(boolean fair) { |
331 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
332 |
|
try { |
333 |
|
q.remove(); |
334 |
|
shouldThrow(); |
335 |
< |
} catch (NoSuchElementException success) { |
539 |
< |
} |
540 |
< |
} |
541 |
< |
|
542 |
< |
/** |
543 |
< |
* remove(x) returns false |
544 |
< |
*/ |
545 |
< |
public void testRemoveElement() { |
546 |
< |
SynchronousQueue q = new SynchronousQueue(); |
547 |
< |
assertFalse(q.remove(zero)); |
548 |
< |
assertTrue(q.isEmpty()); |
335 |
> |
} catch (NoSuchElementException success) {} |
336 |
|
} |
337 |
|
|
338 |
|
/** |
339 |
|
* contains returns false |
340 |
|
*/ |
341 |
< |
public void testContains() { |
342 |
< |
SynchronousQueue q = new SynchronousQueue(); |
341 |
> |
public void testContains() { testContains(false); } |
342 |
> |
public void testContains_fair() { testContains(true); } |
343 |
> |
public void testContains(boolean fair) { |
344 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
345 |
|
assertFalse(q.contains(zero)); |
346 |
|
} |
347 |
|
|
348 |
|
/** |
349 |
|
* clear ensures isEmpty |
350 |
|
*/ |
351 |
< |
public void testClear() { |
352 |
< |
SynchronousQueue q = new SynchronousQueue(); |
351 |
> |
public void testClear() { testClear(false); } |
352 |
> |
public void testClear_fair() { testClear(true); } |
353 |
> |
public void testClear(boolean fair) { |
354 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
355 |
|
q.clear(); |
356 |
|
assertTrue(q.isEmpty()); |
357 |
|
} |
359 |
|
/** |
360 |
|
* containsAll returns false unless empty |
361 |
|
*/ |
362 |
< |
public void testContainsAll() { |
363 |
< |
SynchronousQueue q = new SynchronousQueue(); |
364 |
< |
Integer[] empty = new Integer[0]; |
362 |
> |
public void testContainsAll() { testContainsAll(false); } |
363 |
> |
public void testContainsAll_fair() { testContainsAll(true); } |
364 |
> |
public void testContainsAll(boolean fair) { |
365 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
366 |
> |
Item[] empty = new Item[0]; |
367 |
|
assertTrue(q.containsAll(Arrays.asList(empty))); |
368 |
< |
Integer[] ints = new Integer[1]; ints[0] = zero; |
369 |
< |
assertFalse(q.containsAll(Arrays.asList(ints))); |
368 |
> |
Item[] items = new Item[1]; items[0] = zero; |
369 |
> |
assertFalse(q.containsAll(Arrays.asList(items))); |
370 |
|
} |
371 |
|
|
372 |
|
/** |
373 |
|
* retainAll returns false |
374 |
|
*/ |
375 |
< |
public void testRetainAll() { |
376 |
< |
SynchronousQueue q = new SynchronousQueue(); |
377 |
< |
Integer[] empty = new Integer[0]; |
375 |
> |
public void testRetainAll() { testRetainAll(false); } |
376 |
> |
public void testRetainAll_fair() { testRetainAll(true); } |
377 |
> |
public void testRetainAll(boolean fair) { |
378 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
379 |
> |
Item[] empty = new Item[0]; |
380 |
|
assertFalse(q.retainAll(Arrays.asList(empty))); |
381 |
< |
Integer[] ints = new Integer[1]; ints[0] = zero; |
382 |
< |
assertFalse(q.retainAll(Arrays.asList(ints))); |
381 |
> |
Item[] items = new Item[1]; items[0] = zero; |
382 |
> |
assertFalse(q.retainAll(Arrays.asList(items))); |
383 |
|
} |
384 |
|
|
385 |
|
/** |
386 |
|
* removeAll returns false |
387 |
|
*/ |
388 |
< |
public void testRemoveAll() { |
389 |
< |
SynchronousQueue q = new SynchronousQueue(); |
390 |
< |
Integer[] empty = new Integer[0]; |
388 |
> |
public void testRemoveAll() { testRemoveAll(false); } |
389 |
> |
public void testRemoveAll_fair() { testRemoveAll(true); } |
390 |
> |
public void testRemoveAll(boolean fair) { |
391 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
392 |
> |
Item[] empty = new Item[0]; |
393 |
|
assertFalse(q.removeAll(Arrays.asList(empty))); |
394 |
< |
Integer[] ints = new Integer[1]; ints[0] = zero; |
395 |
< |
assertFalse(q.containsAll(Arrays.asList(ints))); |
394 |
> |
Item[] items = new Item[1]; items[0] = zero; |
395 |
> |
assertFalse(q.containsAll(Arrays.asList(items))); |
396 |
|
} |
397 |
|
|
601 |
– |
|
398 |
|
/** |
399 |
|
* toArray is empty |
400 |
|
*/ |
401 |
< |
public void testToArray() { |
402 |
< |
SynchronousQueue q = new SynchronousQueue(); |
403 |
< |
Object[] o = q.toArray(); |
404 |
< |
assertEquals(o.length, 0); |
401 |
> |
public void testToArray() { testToArray(false); } |
402 |
> |
public void testToArray_fair() { testToArray(true); } |
403 |
> |
public void testToArray(boolean fair) { |
404 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
405 |
> |
Object[] o = q.toArray(); |
406 |
> |
mustEqual(0, o.length); |
407 |
|
} |
408 |
|
|
409 |
|
/** |
410 |
< |
* toArray(a) is nulled at position 0 |
411 |
< |
*/ |
412 |
< |
public void testToArray2() { |
413 |
< |
SynchronousQueue q = new SynchronousQueue(); |
414 |
< |
Integer[] ints = new Integer[1]; |
415 |
< |
assertNull(ints[0]); |
410 |
> |
* toArray(Item array) returns its argument with the first |
411 |
> |
* element (if present) nulled out |
412 |
> |
*/ |
413 |
> |
public void testToArray2() { testToArray2(false); } |
414 |
> |
public void testToArray2_fair() { testToArray2(true); } |
415 |
> |
public void testToArray2(boolean fair) { |
416 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
417 |
> |
Item[] a; |
418 |
> |
|
419 |
> |
a = new Item[0]; |
420 |
> |
assertSame(a, q.toArray(a)); |
421 |
> |
|
422 |
> |
a = new Item[3]; |
423 |
> |
Arrays.fill(a, fortytwo); |
424 |
> |
assertSame(a, q.toArray(a)); |
425 |
> |
assertNull(a[0]); |
426 |
> |
for (int i = 1; i < a.length; i++) |
427 |
> |
mustEqual(42, a[i]); |
428 |
|
} |
429 |
|
|
430 |
|
/** |
431 |
|
* toArray(null) throws NPE |
432 |
|
*/ |
433 |
< |
public void testToArray_BadArg() { |
434 |
< |
try { |
435 |
< |
SynchronousQueue q = new SynchronousQueue(); |
436 |
< |
Object o[] = q.toArray(null); |
437 |
< |
shouldThrow(); |
438 |
< |
} catch (NullPointerException success) {} |
433 |
> |
public void testToArray_null() { testToArray_null(false); } |
434 |
> |
public void testToArray_null_fair() { testToArray_null(true); } |
435 |
> |
public void testToArray_null(boolean fair) { |
436 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
437 |
> |
try { |
438 |
> |
Object[] unused = q.toArray((Object[])null); |
439 |
> |
shouldThrow(); |
440 |
> |
} catch (NullPointerException success) {} |
441 |
|
} |
442 |
|
|
631 |
– |
|
443 |
|
/** |
444 |
|
* iterator does not traverse any elements |
445 |
|
*/ |
446 |
< |
public void testIterator() { |
447 |
< |
SynchronousQueue q = new SynchronousQueue(); |
448 |
< |
Iterator it = q.iterator(); |
449 |
< |
assertFalse(it.hasNext()); |
639 |
< |
try { |
640 |
< |
Object x = it.next(); |
641 |
< |
shouldThrow(); |
642 |
< |
} |
643 |
< |
catch (NoSuchElementException success) {} |
446 |
> |
public void testIterator() { testIterator(false); } |
447 |
> |
public void testIterator_fair() { testIterator(true); } |
448 |
> |
public void testIterator(boolean fair) { |
449 |
> |
assertIteratorExhausted(new SynchronousQueue<Item>(fair).iterator()); |
450 |
|
} |
451 |
|
|
452 |
|
/** |
453 |
< |
* iterator remove throws ISE |
453 |
> |
* iterator remove throws IllegalStateException |
454 |
|
*/ |
455 |
< |
public void testIteratorRemove() { |
456 |
< |
SynchronousQueue q = new SynchronousQueue(); |
457 |
< |
Iterator it = q.iterator(); |
455 |
> |
public void testIteratorRemove() { testIteratorRemove(false); } |
456 |
> |
public void testIteratorRemove_fair() { testIteratorRemove(true); } |
457 |
> |
public void testIteratorRemove(boolean fair) { |
458 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
459 |
> |
Iterator<? extends Item> it = q.iterator(); |
460 |
|
try { |
461 |
|
it.remove(); |
462 |
|
shouldThrow(); |
463 |
< |
} |
656 |
< |
catch (IllegalStateException success) {} |
463 |
> |
} catch (IllegalStateException success) {} |
464 |
|
} |
465 |
|
|
466 |
|
/** |
467 |
|
* toString returns a non-null string |
468 |
|
*/ |
469 |
< |
public void testToString() { |
470 |
< |
SynchronousQueue q = new SynchronousQueue(); |
469 |
> |
public void testToString() { testToString(false); } |
470 |
> |
public void testToString_fair() { testToString(true); } |
471 |
> |
public void testToString(boolean fair) { |
472 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
473 |
|
String s = q.toString(); |
474 |
|
assertNotNull(s); |
475 |
|
} |
476 |
|
|
668 |
– |
|
477 |
|
/** |
478 |
|
* offer transfers elements across Executor tasks |
479 |
|
*/ |
480 |
< |
public void testOfferInExecutor() { |
481 |
< |
final SynchronousQueue q = new SynchronousQueue(); |
482 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
483 |
< |
final Integer one = new Integer(1); |
484 |
< |
|
485 |
< |
executor.execute(new Runnable() { |
486 |
< |
public void run() { |
487 |
< |
threadAssertFalse(q.offer(one)); |
488 |
< |
try { |
489 |
< |
threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
490 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
491 |
< |
} |
492 |
< |
catch (InterruptedException e) { |
493 |
< |
threadUnexpectedException(); |
494 |
< |
} |
687 |
< |
} |
688 |
< |
}); |
689 |
< |
|
690 |
< |
executor.execute(new Runnable() { |
691 |
< |
public void run() { |
692 |
< |
try { |
693 |
< |
Thread.sleep(SMALL_DELAY_MS); |
694 |
< |
threadAssertEquals(one, q.take()); |
695 |
< |
} |
696 |
< |
catch (InterruptedException e) { |
697 |
< |
threadUnexpectedException(); |
698 |
< |
} |
699 |
< |
} |
700 |
< |
}); |
701 |
< |
|
702 |
< |
joinPool(executor); |
703 |
< |
|
704 |
< |
} |
705 |
< |
|
706 |
< |
/** |
707 |
< |
* poll retrieves elements across Executor threads |
708 |
< |
*/ |
709 |
< |
public void testPollInExecutor() { |
710 |
< |
final SynchronousQueue q = new SynchronousQueue(); |
711 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
712 |
< |
executor.execute(new Runnable() { |
713 |
< |
public void run() { |
714 |
< |
threadAssertNull(q.poll()); |
715 |
< |
try { |
716 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
717 |
< |
threadAssertTrue(q.isEmpty()); |
718 |
< |
} |
719 |
< |
catch (InterruptedException e) { |
720 |
< |
threadUnexpectedException(); |
721 |
< |
} |
722 |
< |
} |
723 |
< |
}); |
724 |
< |
|
725 |
< |
executor.execute(new Runnable() { |
726 |
< |
public void run() { |
727 |
< |
try { |
728 |
< |
Thread.sleep(SMALL_DELAY_MS); |
729 |
< |
q.put(new Integer(1)); |
730 |
< |
} |
731 |
< |
catch (InterruptedException e) { |
732 |
< |
threadUnexpectedException(); |
733 |
< |
} |
734 |
< |
} |
735 |
< |
}); |
736 |
< |
|
737 |
< |
joinPool(executor); |
738 |
< |
} |
480 |
> |
public void testOfferInExecutor() { testOfferInExecutor(false); } |
481 |
> |
public void testOfferInExecutor_fair() { testOfferInExecutor(true); } |
482 |
> |
public void testOfferInExecutor(boolean fair) { |
483 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
484 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
485 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
486 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
487 |
> |
|
488 |
> |
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 |
> |
mustEqual(0, q.remainingCapacity()); |
494 |
> |
}}); |
495 |
|
|
496 |
< |
/** |
497 |
< |
* a deserialized serialized queue is usable |
498 |
< |
*/ |
499 |
< |
public void testSerialization() { |
500 |
< |
SynchronousQueue q = new SynchronousQueue(); |
745 |
< |
try { |
746 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
747 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
748 |
< |
out.writeObject(q); |
749 |
< |
out.close(); |
750 |
< |
|
751 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
752 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
753 |
< |
SynchronousQueue r = (SynchronousQueue)in.readObject(); |
754 |
< |
assertEquals(q.size(), r.size()); |
755 |
< |
while (!q.isEmpty()) |
756 |
< |
assertEquals(q.remove(), r.remove()); |
757 |
< |
} catch (Exception e) { |
758 |
< |
e.printStackTrace(); |
759 |
< |
unexpectedException(); |
496 |
> |
executor.execute(new CheckedRunnable() { |
497 |
> |
public void realRun() throws InterruptedException { |
498 |
> |
threadsStarted.await(); |
499 |
> |
assertSame(one, q.take()); |
500 |
> |
}}); |
501 |
|
} |
502 |
|
} |
503 |
|
|
504 |
|
/** |
505 |
< |
* drainTo(null) throws NPE |
505 |
> |
* timed poll retrieves elements across Executor threads |
506 |
|
*/ |
507 |
< |
public void testDrainToNull() { |
508 |
< |
SynchronousQueue q = new SynchronousQueue(); |
509 |
< |
try { |
510 |
< |
q.drainTo(null); |
511 |
< |
shouldThrow(); |
512 |
< |
} catch (NullPointerException success) { |
507 |
> |
public void testPollInExecutor() { testPollInExecutor(false); } |
508 |
> |
public void testPollInExecutor_fair() { testPollInExecutor(true); } |
509 |
> |
public void testPollInExecutor(boolean fair) { |
510 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
511 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
512 |
> |
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 |
|
} |
529 |
|
|
530 |
|
/** |
531 |
< |
* drainTo(this) throws IAE |
531 |
> |
* a deserialized/reserialized queue is usable |
532 |
|
*/ |
533 |
< |
public void testDrainToSelf() { |
534 |
< |
SynchronousQueue q = new SynchronousQueue(); |
535 |
< |
try { |
536 |
< |
q.drainTo(q); |
537 |
< |
shouldThrow(); |
538 |
< |
} catch (IllegalArgumentException success) { |
533 |
> |
public void testSerialization() { |
534 |
> |
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 |
> |
assertSerialEquals(x, y); |
538 |
> |
assertNotSerialEquals(x, z); |
539 |
> |
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 |
> |
assertNotSame(q, clone); |
545 |
> |
assertSerialEquals(q, clone); |
546 |
> |
assertTrue(clone.isEmpty()); |
547 |
> |
mustEqual(0, clone.size()); |
548 |
> |
mustEqual(0, clone.remainingCapacity()); |
549 |
> |
assertFalse(clone.offer(zero)); |
550 |
|
} |
551 |
|
} |
552 |
|
|
553 |
|
/** |
554 |
|
* drainTo(c) of empty queue doesn't transfer elements |
555 |
|
*/ |
556 |
< |
public void testDrainTo() { |
557 |
< |
SynchronousQueue q = new SynchronousQueue(); |
558 |
< |
ArrayList l = new ArrayList(); |
556 |
> |
public void testDrainTo() { testDrainTo(false); } |
557 |
> |
public void testDrainTo_fair() { testDrainTo(true); } |
558 |
> |
public void testDrainTo(boolean fair) { |
559 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
560 |
> |
ArrayList<Item> l = new ArrayList<Item>(); |
561 |
|
q.drainTo(l); |
562 |
< |
assertEquals(q.size(), 0); |
563 |
< |
assertEquals(l.size(), 0); |
562 |
> |
mustEqual(0, q.size()); |
563 |
> |
mustEqual(0, l.size()); |
564 |
|
} |
565 |
|
|
566 |
|
/** |
567 |
|
* drainTo empties queue, unblocking a waiting put. |
568 |
|
*/ |
569 |
< |
public void testDrainToWithActivePut() { |
570 |
< |
final SynchronousQueue q = new SynchronousQueue(); |
571 |
< |
Thread t = new Thread(new Runnable() { |
572 |
< |
public void run() { |
573 |
< |
try { |
574 |
< |
q.put(new Integer(1)); |
575 |
< |
} catch (InterruptedException ie) { |
576 |
< |
threadUnexpectedException(); |
577 |
< |
} |
578 |
< |
} |
579 |
< |
}); |
580 |
< |
try { |
813 |
< |
t.start(); |
814 |
< |
ArrayList l = new ArrayList(); |
815 |
< |
Thread.sleep(SHORT_DELAY_MS); |
569 |
> |
public void testDrainToWithActivePut() { testDrainToWithActivePut(false); } |
570 |
> |
public void testDrainToWithActivePut_fair() { testDrainToWithActivePut(true); } |
571 |
> |
public void testDrainToWithActivePut(boolean fair) { |
572 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(fair); |
573 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
574 |
> |
public void realRun() throws InterruptedException { |
575 |
> |
q.put(one); |
576 |
> |
}}); |
577 |
> |
|
578 |
> |
ArrayList<Item> l = new ArrayList<Item>(); |
579 |
> |
long startTime = System.nanoTime(); |
580 |
> |
while (l.isEmpty()) { |
581 |
|
q.drainTo(l); |
582 |
< |
assertTrue(l.size() <= 1); |
583 |
< |
if (l.size() > 0) |
584 |
< |
assertEquals(l.get(0), new Integer(1)); |
585 |
< |
t.join(); |
586 |
< |
assertTrue(l.size() <= 1); |
587 |
< |
} catch (Exception e) { |
588 |
< |
unexpectedException(); |
824 |
< |
} |
582 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
583 |
> |
fail("timed out"); |
584 |
> |
Thread.yield(); |
585 |
> |
} |
586 |
> |
mustEqual(1, l.size()); |
587 |
> |
assertSame(one, l.get(0)); |
588 |
> |
awaitTermination(t); |
589 |
|
} |
590 |
|
|
591 |
|
/** |
592 |
< |
* drainTo(null, n) throws NPE |
829 |
< |
*/ |
830 |
< |
public void testDrainToNullN() { |
831 |
< |
SynchronousQueue q = new SynchronousQueue(); |
832 |
< |
try { |
833 |
< |
q.drainTo(null, 0); |
834 |
< |
shouldThrow(); |
835 |
< |
} catch (NullPointerException success) { |
836 |
< |
} |
837 |
< |
} |
838 |
< |
|
839 |
< |
/** |
840 |
< |
* drainTo(this, n) throws IAE |
592 |
> |
* drainTo(c, n) empties up to n elements of queue into c |
593 |
|
*/ |
594 |
< |
public void testDrainToSelfN() { |
595 |
< |
SynchronousQueue q = new SynchronousQueue(); |
596 |
< |
try { |
597 |
< |
q.drainTo(q, 0); |
598 |
< |
shouldThrow(); |
599 |
< |
} catch (IllegalArgumentException success) { |
600 |
< |
} |
594 |
> |
public void testDrainToN() throws InterruptedException { |
595 |
> |
final SynchronousQueue<Item> q = new SynchronousQueue<Item>(); |
596 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
597 |
> |
public void realRun() throws InterruptedException { |
598 |
> |
q.put(one); |
599 |
> |
}}); |
600 |
> |
|
601 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
602 |
> |
public void realRun() throws InterruptedException { |
603 |
> |
q.put(two); |
604 |
> |
}}); |
605 |
> |
|
606 |
> |
ArrayList<Item> l = new ArrayList<Item>(); |
607 |
> |
int drained; |
608 |
> |
while ((drained = q.drainTo(l, 1)) == 0) Thread.yield(); |
609 |
> |
mustEqual(1, drained); |
610 |
> |
mustEqual(1, l.size()); |
611 |
> |
while ((drained = q.drainTo(l, 1)) == 0) Thread.yield(); |
612 |
> |
mustEqual(1, drained); |
613 |
> |
mustEqual(2, l.size()); |
614 |
> |
assertTrue(l.contains(one)); |
615 |
> |
assertTrue(l.contains(two)); |
616 |
> |
awaitTermination(t1); |
617 |
> |
awaitTermination(t2); |
618 |
|
} |
619 |
|
|
620 |
|
/** |
621 |
< |
* drainTo(c, n) empties up to n elements of queue into c |
622 |
< |
*/ |
623 |
< |
public void testDrainToN() { |
624 |
< |
final SynchronousQueue q = new SynchronousQueue(); |
625 |
< |
Thread t1 = new Thread(new Runnable() { |
626 |
< |
public void run() { |
858 |
< |
try { |
859 |
< |
q.put(one); |
860 |
< |
} catch (InterruptedException ie) { |
861 |
< |
threadUnexpectedException(); |
862 |
< |
} |
863 |
< |
} |
864 |
< |
}); |
865 |
< |
Thread t2 = new Thread(new Runnable() { |
866 |
< |
public void run() { |
867 |
< |
try { |
868 |
< |
q.put(two); |
869 |
< |
} catch (InterruptedException ie) { |
870 |
< |
threadUnexpectedException(); |
871 |
< |
} |
872 |
< |
} |
873 |
< |
}); |
874 |
< |
|
875 |
< |
try { |
876 |
< |
t1.start(); |
877 |
< |
t2.start(); |
878 |
< |
ArrayList l = new ArrayList(); |
879 |
< |
Thread.sleep(SHORT_DELAY_MS); |
880 |
< |
q.drainTo(l, 1); |
881 |
< |
assertTrue(l.size() == 1); |
882 |
< |
q.drainTo(l, 1); |
883 |
< |
assertTrue(l.size() == 2); |
884 |
< |
assertTrue(l.contains(one)); |
885 |
< |
assertTrue(l.contains(two)); |
886 |
< |
t1.join(); |
887 |
< |
t2.join(); |
888 |
< |
} catch (Exception e) { |
889 |
< |
unexpectedException(); |
890 |
< |
} |
621 |
> |
* remove(null), contains(null) always return false |
622 |
> |
*/ |
623 |
> |
public void testNeverContainsNull() { |
624 |
> |
Collection<?> q = new SynchronousQueue<Item>(); |
625 |
> |
assertFalse(q.contains(null)); |
626 |
> |
assertFalse(q.remove(null)); |
627 |
|
} |
628 |
|
|
893 |
– |
|
629 |
|
} |