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