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 LinkedBlockingQueueTest extends TestCase { |
15 |
> |
public class LinkedBlockingQueueTest extends JSR166TestCase { |
16 |
|
|
17 |
< |
private static int N = 10; |
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 Unbounded extends BlockingQueueTest { |
18 |
> |
protected BlockingQueue emptyCollection() { |
19 |
> |
return new LinkedBlockingQueue(); |
20 |
> |
} |
21 |
> |
} |
22 |
> |
|
23 |
> |
public static class Bounded extends BlockingQueueTest { |
24 |
> |
protected BlockingQueue emptyCollection() { |
25 |
> |
return new LinkedBlockingQueue(20); |
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(LinkedBlockingQueueTest.class); |
34 |
> |
return newTestSuite(LinkedBlockingQueueTest.class, |
35 |
> |
new Unbounded().testSuite(), |
36 |
> |
new Bounded().testSuite()); |
37 |
|
} |
38 |
|
|
39 |
+ |
|
40 |
|
/** |
41 |
|
* Create a queue of given size containing consecutive |
42 |
|
* Integers 0 ... n. |
43 |
|
*/ |
44 |
< |
private LinkedBlockingQueue fullQueue(int n) { |
45 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(n); |
44 |
> |
private LinkedBlockingQueue<Integer> populatedQueue(int n) { |
45 |
> |
LinkedBlockingQueue<Integer> q = |
46 |
> |
new LinkedBlockingQueue<Integer>(n); |
47 |
|
assertTrue(q.isEmpty()); |
48 |
< |
for(int i = 0; i < n; i++) |
49 |
< |
assertTrue(q.offer(new Integer(i))); |
48 |
> |
for (int i = 0; i < n; i++) |
49 |
> |
assertTrue(q.offer(new Integer(i))); |
50 |
|
assertFalse(q.isEmpty()); |
51 |
|
assertEquals(0, q.remainingCapacity()); |
52 |
< |
assertEquals(n, q.size()); |
52 |
> |
assertEquals(n, q.size()); |
53 |
|
return q; |
54 |
|
} |
55 |
< |
|
56 |
< |
public void testConstructor1(){ |
57 |
< |
assertEquals(N, new LinkedBlockingQueue(N).remainingCapacity()); |
55 |
> |
|
56 |
> |
/** |
57 |
> |
* A new queue has the indicated capacity, or Integer.MAX_VALUE if |
58 |
> |
* none given |
59 |
> |
*/ |
60 |
> |
public void testConstructor1() { |
61 |
> |
assertEquals(SIZE, new LinkedBlockingQueue(SIZE).remainingCapacity()); |
62 |
> |
assertEquals(Integer.MAX_VALUE, new LinkedBlockingQueue().remainingCapacity()); |
63 |
|
} |
64 |
|
|
65 |
< |
public void testConstructor2(){ |
65 |
> |
/** |
66 |
> |
* Constructor throws IAE if capacity argument nonpositive |
67 |
> |
*/ |
68 |
> |
public void testConstructor2() { |
69 |
|
try { |
70 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(0); |
71 |
< |
fail("Cannot make zero-sized"); |
72 |
< |
} |
51 |
< |
catch (IllegalArgumentException success) {} |
71 |
> |
shouldThrow(); |
72 |
> |
} catch (IllegalArgumentException success) {} |
73 |
|
} |
74 |
|
|
75 |
< |
public void testConstructor3(){ |
76 |
< |
|
75 |
> |
/** |
76 |
> |
* Initializing from null Collection throws NPE |
77 |
> |
*/ |
78 |
> |
public void testConstructor3() { |
79 |
|
try { |
80 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(null); |
81 |
< |
fail("Cannot make from null collection"); |
82 |
< |
} |
60 |
< |
catch (NullPointerException success) {} |
81 |
> |
shouldThrow(); |
82 |
> |
} catch (NullPointerException success) {} |
83 |
|
} |
84 |
|
|
85 |
< |
public void testConstructor4(){ |
85 |
> |
/** |
86 |
> |
* Initializing from Collection of null elements throws NPE |
87 |
> |
*/ |
88 |
> |
public void testConstructor4() { |
89 |
|
try { |
90 |
< |
Integer[] ints = new Integer[N]; |
90 |
> |
Integer[] ints = new Integer[SIZE]; |
91 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
92 |
< |
fail("Cannot make with null elements"); |
93 |
< |
} |
69 |
< |
catch (NullPointerException success) {} |
92 |
> |
shouldThrow(); |
93 |
> |
} catch (NullPointerException success) {} |
94 |
|
} |
95 |
|
|
96 |
< |
public void testConstructor5(){ |
96 |
> |
/** |
97 |
> |
* Initializing from Collection with some null elements throws NPE |
98 |
> |
*/ |
99 |
> |
public void testConstructor5() { |
100 |
|
try { |
101 |
< |
Integer[] ints = new Integer[N]; |
102 |
< |
for (int i = 0; i < N-1; ++i) |
101 |
> |
Integer[] ints = new Integer[SIZE]; |
102 |
> |
for (int i = 0; i < SIZE-1; ++i) |
103 |
|
ints[i] = new Integer(i); |
104 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
105 |
< |
fail("Cannot make with null elements"); |
106 |
< |
} |
80 |
< |
catch (NullPointerException success) {} |
105 |
> |
shouldThrow(); |
106 |
> |
} catch (NullPointerException success) {} |
107 |
|
} |
108 |
|
|
109 |
< |
public void testConstructor6(){ |
110 |
< |
try { |
111 |
< |
Integer[] ints = new Integer[N]; |
112 |
< |
for (int i = 0; i < N; ++i) |
113 |
< |
ints[i] = new Integer(i); |
114 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
115 |
< |
for (int i = 0; i < N; ++i) |
116 |
< |
assertEquals(ints[i], q.poll()); |
117 |
< |
} |
118 |
< |
finally {} |
109 |
> |
/** |
110 |
> |
* Queue contains all elements of collection used to initialize |
111 |
> |
*/ |
112 |
> |
public void testConstructor6() { |
113 |
> |
Integer[] ints = new Integer[SIZE]; |
114 |
> |
for (int i = 0; i < SIZE; ++i) |
115 |
> |
ints[i] = new Integer(i); |
116 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
117 |
> |
for (int i = 0; i < SIZE; ++i) |
118 |
> |
assertEquals(ints[i], q.poll()); |
119 |
|
} |
120 |
|
|
121 |
+ |
/** |
122 |
+ |
* Queue transitions from empty to full when elements added |
123 |
+ |
*/ |
124 |
|
public void testEmptyFull() { |
125 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
126 |
|
assertTrue(q.isEmpty()); |
127 |
|
assertEquals("should have room for 2", 2, q.remainingCapacity()); |
128 |
< |
q.add(new Integer(1)); |
128 |
> |
q.add(one); |
129 |
|
assertFalse(q.isEmpty()); |
130 |
< |
q.add(new Integer(2)); |
130 |
> |
q.add(two); |
131 |
|
assertFalse(q.isEmpty()); |
132 |
< |
assertEquals("queue should be full", 0, q.remainingCapacity()); |
133 |
< |
assertFalse("offer should be rejected", q.offer(new Integer(3))); |
132 |
> |
assertEquals(0, q.remainingCapacity()); |
133 |
> |
assertFalse(q.offer(three)); |
134 |
|
} |
135 |
|
|
136 |
< |
public void testRemainingCapacity(){ |
137 |
< |
LinkedBlockingQueue q = fullQueue(N); |
138 |
< |
for (int i = 0; i < N; ++i) { |
136 |
> |
/** |
137 |
> |
* remainingCapacity decreases on add, increases on remove |
138 |
> |
*/ |
139 |
> |
public void testRemainingCapacity() { |
140 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
141 |
> |
for (int i = 0; i < SIZE; ++i) { |
142 |
|
assertEquals(i, q.remainingCapacity()); |
143 |
< |
assertEquals(N-i, q.size()); |
143 |
> |
assertEquals(SIZE-i, q.size()); |
144 |
|
q.remove(); |
145 |
|
} |
146 |
< |
for (int i = 0; i < N; ++i) { |
147 |
< |
assertEquals(N-i, q.remainingCapacity()); |
146 |
> |
for (int i = 0; i < SIZE; ++i) { |
147 |
> |
assertEquals(SIZE-i, q.remainingCapacity()); |
148 |
|
assertEquals(i, q.size()); |
149 |
|
q.add(new Integer(i)); |
150 |
|
} |
151 |
|
} |
152 |
|
|
153 |
< |
public void testOfferNull(){ |
154 |
< |
try { |
153 |
> |
/** |
154 |
> |
* offer(null) throws NPE |
155 |
> |
*/ |
156 |
> |
public void testOfferNull() { |
157 |
> |
try { |
158 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
159 |
|
q.offer(null); |
160 |
< |
fail("should throw NPE"); |
161 |
< |
} catch (NullPointerException success) { } |
160 |
> |
shouldThrow(); |
161 |
> |
} catch (NullPointerException success) {} |
162 |
|
} |
163 |
|
|
164 |
< |
public void testOffer(){ |
164 |
> |
/** |
165 |
> |
* add(null) throws NPE |
166 |
> |
*/ |
167 |
> |
public void testAddNull() { |
168 |
> |
try { |
169 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
170 |
> |
q.add(null); |
171 |
> |
shouldThrow(); |
172 |
> |
} catch (NullPointerException success) {} |
173 |
> |
} |
174 |
> |
|
175 |
> |
/** |
176 |
> |
* Offer succeeds if not full; fails if full |
177 |
> |
*/ |
178 |
> |
public void testOffer() { |
179 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
180 |
< |
assertTrue(q.offer(new Integer(0))); |
181 |
< |
assertFalse(q.offer(new Integer(1))); |
180 |
> |
assertTrue(q.offer(zero)); |
181 |
> |
assertFalse(q.offer(one)); |
182 |
|
} |
183 |
|
|
184 |
< |
public void testAdd(){ |
185 |
< |
try { |
186 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
187 |
< |
for (int i = 0; i < N; ++i) { |
184 |
> |
/** |
185 |
> |
* add succeeds if not full; throws ISE if full |
186 |
> |
*/ |
187 |
> |
public void testAdd() { |
188 |
> |
try { |
189 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
190 |
> |
for (int i = 0; i < SIZE; ++i) { |
191 |
|
assertTrue(q.add(new Integer(i))); |
192 |
|
} |
193 |
|
assertEquals(0, q.remainingCapacity()); |
194 |
< |
q.add(new Integer(N)); |
195 |
< |
} catch (IllegalStateException success){ |
196 |
< |
} |
194 |
> |
q.add(new Integer(SIZE)); |
195 |
> |
shouldThrow(); |
196 |
> |
} catch (IllegalStateException success) {} |
197 |
|
} |
198 |
|
|
199 |
< |
public void testAddAll1(){ |
199 |
> |
/** |
200 |
> |
* addAll(null) throws NPE |
201 |
> |
*/ |
202 |
> |
public void testAddAll1() { |
203 |
|
try { |
204 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
205 |
|
q.addAll(null); |
206 |
< |
fail("Cannot add null collection"); |
207 |
< |
} |
208 |
< |
catch (NullPointerException success) {} |
206 |
> |
shouldThrow(); |
207 |
> |
} catch (NullPointerException success) {} |
208 |
> |
} |
209 |
> |
|
210 |
> |
/** |
211 |
> |
* addAll(this) throws IAE |
212 |
> |
*/ |
213 |
> |
public void testAddAllSelf() { |
214 |
> |
try { |
215 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
216 |
> |
q.addAll(q); |
217 |
> |
shouldThrow(); |
218 |
> |
} catch (IllegalArgumentException success) {} |
219 |
|
} |
220 |
< |
public void testAddAll2(){ |
220 |
> |
|
221 |
> |
/** |
222 |
> |
* addAll of a collection with null elements throws NPE |
223 |
> |
*/ |
224 |
> |
public void testAddAll2() { |
225 |
|
try { |
226 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
227 |
< |
Integer[] ints = new Integer[N]; |
226 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
227 |
> |
Integer[] ints = new Integer[SIZE]; |
228 |
|
q.addAll(Arrays.asList(ints)); |
229 |
< |
fail("Cannot add null elements"); |
230 |
< |
} |
162 |
< |
catch (NullPointerException success) {} |
229 |
> |
shouldThrow(); |
230 |
> |
} catch (NullPointerException success) {} |
231 |
|
} |
232 |
< |
public void testAddAll3(){ |
232 |
> |
|
233 |
> |
/** |
234 |
> |
* addAll of a collection with any null elements throws NPE after |
235 |
> |
* possibly adding some elements |
236 |
> |
*/ |
237 |
> |
public void testAddAll3() { |
238 |
|
try { |
239 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
240 |
< |
Integer[] ints = new Integer[N]; |
241 |
< |
for (int i = 0; i < N-1; ++i) |
239 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
240 |
> |
Integer[] ints = new Integer[SIZE]; |
241 |
> |
for (int i = 0; i < SIZE-1; ++i) |
242 |
|
ints[i] = new Integer(i); |
243 |
|
q.addAll(Arrays.asList(ints)); |
244 |
< |
fail("Cannot add null elements"); |
245 |
< |
} |
173 |
< |
catch (NullPointerException success) {} |
244 |
> |
shouldThrow(); |
245 |
> |
} catch (NullPointerException success) {} |
246 |
|
} |
247 |
< |
public void testAddAll4(){ |
247 |
> |
|
248 |
> |
/** |
249 |
> |
* addAll throws ISE if not enough room |
250 |
> |
*/ |
251 |
> |
public void testAddAll4() { |
252 |
|
try { |
253 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
254 |
< |
Integer[] ints = new Integer[N]; |
255 |
< |
for (int i = 0; i < N; ++i) |
254 |
> |
Integer[] ints = new Integer[SIZE]; |
255 |
> |
for (int i = 0; i < SIZE; ++i) |
256 |
|
ints[i] = new Integer(i); |
257 |
|
q.addAll(Arrays.asList(ints)); |
258 |
< |
fail("Cannot add with insufficient capacity"); |
259 |
< |
} |
184 |
< |
catch (IllegalStateException success) {} |
258 |
> |
shouldThrow(); |
259 |
> |
} catch (IllegalStateException success) {} |
260 |
|
} |
261 |
< |
public void testAddAll5(){ |
262 |
< |
try { |
263 |
< |
Integer[] empty = new Integer[0]; |
264 |
< |
Integer[] ints = new Integer[N]; |
265 |
< |
for (int i = 0; i < N; ++i) |
266 |
< |
ints[i] = new Integer(i); |
267 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
268 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
269 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
270 |
< |
for (int i = 0; i < N; ++i) |
271 |
< |
assertEquals(ints[i], q.poll()); |
272 |
< |
} |
273 |
< |
finally {} |
261 |
> |
|
262 |
> |
/** |
263 |
> |
* Queue contains all elements, in traversal order, of successful addAll |
264 |
> |
*/ |
265 |
> |
public void testAddAll5() { |
266 |
> |
Integer[] empty = new Integer[0]; |
267 |
> |
Integer[] ints = new Integer[SIZE]; |
268 |
> |
for (int i = 0; i < SIZE; ++i) |
269 |
> |
ints[i] = new Integer(i); |
270 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
271 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
272 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
273 |
> |
for (int i = 0; i < SIZE; ++i) |
274 |
> |
assertEquals(ints[i], q.poll()); |
275 |
|
} |
276 |
|
|
277 |
< |
public void testPutNull() { |
278 |
< |
try { |
279 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
277 |
> |
/** |
278 |
> |
* put(null) throws NPE |
279 |
> |
*/ |
280 |
> |
public void testPutNull() throws InterruptedException { |
281 |
> |
try { |
282 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
283 |
|
q.put(null); |
284 |
< |
fail("put should throw NPE"); |
285 |
< |
} |
286 |
< |
catch (NullPointerException success){ |
287 |
< |
} |
288 |
< |
catch (InterruptedException ie) { |
289 |
< |
fail("Unexpected exception"); |
290 |
< |
} |
291 |
< |
} |
292 |
< |
|
293 |
< |
public void testPut() { |
294 |
< |
try { |
295 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
296 |
< |
for (int i = 0; i < N; ++i) { |
218 |
< |
Integer I = new Integer(i); |
219 |
< |
q.put(I); |
220 |
< |
assertTrue(q.contains(I)); |
221 |
< |
} |
222 |
< |
assertEquals(0, q.remainingCapacity()); |
223 |
< |
} |
224 |
< |
catch (InterruptedException ie) { |
225 |
< |
fail("Unexpected exception"); |
284 |
> |
shouldThrow(); |
285 |
> |
} catch (NullPointerException success) {} |
286 |
> |
} |
287 |
> |
|
288 |
> |
/** |
289 |
> |
* all elements successfully put are contained |
290 |
> |
*/ |
291 |
> |
public void testPut() throws InterruptedException { |
292 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
293 |
> |
for (int i = 0; i < SIZE; ++i) { |
294 |
> |
Integer I = new Integer(i); |
295 |
> |
q.put(I); |
296 |
> |
assertTrue(q.contains(I)); |
297 |
|
} |
298 |
+ |
assertEquals(0, q.remainingCapacity()); |
299 |
|
} |
300 |
|
|
301 |
< |
public void testBlockingPut(){ |
302 |
< |
Thread t = new Thread(new Runnable() { |
303 |
< |
public void run() { |
304 |
< |
int added = 0; |
305 |
< |
try { |
306 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
307 |
< |
for (int i = 0; i < N; ++i) { |
308 |
< |
q.put(new Integer(i)); |
309 |
< |
++added; |
310 |
< |
} |
311 |
< |
q.put(new Integer(N)); |
312 |
< |
fail("put should block"); |
313 |
< |
} catch (InterruptedException ie){ |
314 |
< |
assertEquals(added, N); |
315 |
< |
} |
316 |
< |
}}); |
301 |
> |
/** |
302 |
> |
* put blocks interruptibly if full |
303 |
> |
*/ |
304 |
> |
public void testBlockingPut() throws InterruptedException { |
305 |
> |
final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
306 |
> |
Thread t = new Thread(new CheckedRunnable() { |
307 |
> |
public void realRun() throws InterruptedException { |
308 |
> |
for (int i = 0; i < SIZE; ++i) |
309 |
> |
q.put(i); |
310 |
> |
assertEquals(SIZE, q.size()); |
311 |
> |
assertEquals(0, q.remainingCapacity()); |
312 |
> |
try { |
313 |
> |
q.put(99); |
314 |
> |
shouldThrow(); |
315 |
> |
} catch (InterruptedException success) {} |
316 |
> |
}}); |
317 |
> |
|
318 |
|
t.start(); |
319 |
< |
try { |
320 |
< |
Thread.sleep(SHORT_DELAY_MS); |
321 |
< |
t.interrupt(); |
322 |
< |
t.join(); |
323 |
< |
} |
251 |
< |
catch (InterruptedException ie) { |
252 |
< |
fail("Unexpected exception"); |
253 |
< |
} |
319 |
> |
delay(SHORT_DELAY_MS); |
320 |
> |
t.interrupt(); |
321 |
> |
t.join(); |
322 |
> |
assertEquals(SIZE, q.size()); |
323 |
> |
assertEquals(0, q.remainingCapacity()); |
324 |
|
} |
325 |
|
|
326 |
< |
public void testPutWithTake() { |
326 |
> |
/** |
327 |
> |
* put blocks waiting for take when full |
328 |
> |
*/ |
329 |
> |
public void testPutWithTake() throws InterruptedException { |
330 |
> |
final int capacity = 2; |
331 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
332 |
< |
Thread t = new Thread(new Runnable() { |
333 |
< |
public void run(){ |
334 |
< |
int added = 0; |
335 |
< |
try { |
336 |
< |
q.put(new Object()); |
337 |
< |
++added; |
338 |
< |
q.put(new Object()); |
339 |
< |
++added; |
340 |
< |
q.put(new Object()); |
341 |
< |
++added; |
342 |
< |
q.put(new Object()); |
343 |
< |
++added; |
344 |
< |
fail("Should block"); |
345 |
< |
} catch (InterruptedException e){ |
346 |
< |
assertTrue(added >= 2); |
347 |
< |
} |
348 |
< |
} |
349 |
< |
}); |
276 |
< |
try { |
277 |
< |
t.start(); |
278 |
< |
Thread.sleep(SHORT_DELAY_MS); |
279 |
< |
q.take(); |
280 |
< |
t.interrupt(); |
281 |
< |
t.join(); |
282 |
< |
} catch (Exception e){ |
283 |
< |
fail("Unexpected exception"); |
284 |
< |
} |
332 |
> |
Thread t = new Thread(new CheckedRunnable() { |
333 |
> |
public void realRun() throws InterruptedException { |
334 |
> |
for (int i = 0; i < capacity + 1; i++) |
335 |
> |
q.put(i); |
336 |
> |
try { |
337 |
> |
q.put(99); |
338 |
> |
shouldThrow(); |
339 |
> |
} catch (InterruptedException success) {} |
340 |
> |
}}); |
341 |
> |
|
342 |
> |
t.start(); |
343 |
> |
delay(SHORT_DELAY_MS); |
344 |
> |
assertEquals(q.remainingCapacity(), 0); |
345 |
> |
assertEquals(0, q.take()); |
346 |
> |
delay(SHORT_DELAY_MS); |
347 |
> |
t.interrupt(); |
348 |
> |
t.join(); |
349 |
> |
assertEquals(q.remainingCapacity(), 0); |
350 |
|
} |
351 |
|
|
352 |
+ |
/** |
353 |
+ |
* timed offer times out if full and elements not taken |
354 |
+ |
*/ |
355 |
|
public void testTimedOffer() { |
356 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
357 |
< |
Thread t = new Thread(new Runnable() { |
358 |
< |
public void run(){ |
359 |
< |
try { |
360 |
< |
q.put(new Object()); |
361 |
< |
q.put(new Object()); |
362 |
< |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); |
363 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
364 |
< |
fail("Should block"); |
365 |
< |
} catch (InterruptedException success){} |
366 |
< |
} |
367 |
< |
}); |
368 |
< |
|
369 |
< |
try { |
370 |
< |
t.start(); |
371 |
< |
Thread.sleep(SHORT_DELAY_MS); |
372 |
< |
t.interrupt(); |
373 |
< |
t.join(); |
374 |
< |
} catch (Exception e){ |
307 |
< |
fail("Unexpected exception"); |
308 |
< |
} |
357 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
358 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
359 |
> |
public void realRun() throws InterruptedException { |
360 |
> |
q.put(new Object()); |
361 |
> |
q.put(new Object()); |
362 |
> |
long startTime = System.nanoTime(); |
363 |
> |
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
364 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
365 |
> |
pleaseInterrupt.countDown(); |
366 |
> |
try { |
367 |
> |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
368 |
> |
shouldThrow(); |
369 |
> |
} catch (InterruptedException success) {} |
370 |
> |
}}); |
371 |
> |
|
372 |
> |
await(pleaseInterrupt); |
373 |
> |
t.interrupt(); |
374 |
> |
awaitTermination(t); |
375 |
|
} |
376 |
|
|
377 |
< |
public void testTake(){ |
378 |
< |
try { |
379 |
< |
LinkedBlockingQueue q = fullQueue(N); |
380 |
< |
for (int i = 0; i < N; ++i) { |
381 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
382 |
< |
} |
383 |
< |
} catch (InterruptedException e){ |
384 |
< |
fail("Unexpected exception"); |
319 |
< |
} |
377 |
> |
/** |
378 |
> |
* take retrieves elements in FIFO order |
379 |
> |
*/ |
380 |
> |
public void testTake() throws InterruptedException { |
381 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
382 |
> |
for (int i = 0; i < SIZE; ++i) { |
383 |
> |
assertEquals(i, q.take()); |
384 |
> |
} |
385 |
|
} |
386 |
|
|
387 |
< |
public void testTakeFromEmpty() { |
388 |
< |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
389 |
< |
Thread t = new Thread(new Runnable() { |
390 |
< |
public void run(){ |
391 |
< |
try { |
392 |
< |
q.take(); |
393 |
< |
fail("Should block"); |
394 |
< |
} catch (InterruptedException success){ } |
387 |
> |
/** |
388 |
> |
* Take removes existing elements until empty, then blocks interruptibly |
389 |
> |
*/ |
390 |
> |
public void testBlockingTake() throws InterruptedException { |
391 |
> |
final LinkedBlockingQueue q = populatedQueue(SIZE); |
392 |
> |
Thread t = new Thread(new CheckedRunnable() { |
393 |
> |
public void realRun() throws InterruptedException { |
394 |
> |
for (int i = 0; i < SIZE; ++i) { |
395 |
> |
assertEquals(i, q.take()); |
396 |
|
} |
397 |
< |
}); |
398 |
< |
try { |
399 |
< |
t.start(); |
400 |
< |
Thread.sleep(SHORT_DELAY_MS); |
401 |
< |
t.interrupt(); |
336 |
< |
t.join(); |
337 |
< |
} catch (Exception e){ |
338 |
< |
fail("Unexpected exception"); |
339 |
< |
} |
340 |
< |
} |
397 |
> |
try { |
398 |
> |
q.take(); |
399 |
> |
shouldThrow(); |
400 |
> |
} catch (InterruptedException success) {} |
401 |
> |
}}); |
402 |
|
|
342 |
– |
public void testBlockingTake(){ |
343 |
– |
Thread t = new Thread(new Runnable() { |
344 |
– |
public void run() { |
345 |
– |
try { |
346 |
– |
LinkedBlockingQueue q = fullQueue(N); |
347 |
– |
for (int i = 0; i < N; ++i) { |
348 |
– |
assertEquals(i, ((Integer)q.take()).intValue()); |
349 |
– |
} |
350 |
– |
q.take(); |
351 |
– |
fail("take should block"); |
352 |
– |
} catch (InterruptedException success){ |
353 |
– |
} |
354 |
– |
}}); |
403 |
|
t.start(); |
404 |
< |
try { |
405 |
< |
Thread.sleep(SHORT_DELAY_MS); |
406 |
< |
t.interrupt(); |
359 |
< |
t.join(); |
360 |
< |
} |
361 |
< |
catch (InterruptedException ie) { |
362 |
< |
fail("Unexpected exception"); |
363 |
< |
} |
404 |
> |
delay(SHORT_DELAY_MS); |
405 |
> |
t.interrupt(); |
406 |
> |
t.join(); |
407 |
|
} |
408 |
|
|
409 |
< |
|
410 |
< |
public void testPoll(){ |
411 |
< |
LinkedBlockingQueue q = fullQueue(N); |
412 |
< |
for (int i = 0; i < N; ++i) { |
413 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
409 |
> |
/** |
410 |
> |
* poll succeeds unless empty |
411 |
> |
*/ |
412 |
> |
public void testPoll() { |
413 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
414 |
> |
for (int i = 0; i < SIZE; ++i) { |
415 |
> |
assertEquals(i, q.poll()); |
416 |
|
} |
417 |
< |
assertNull(q.poll()); |
417 |
> |
assertNull(q.poll()); |
418 |
|
} |
419 |
|
|
420 |
< |
public void testTimedPoll0() { |
421 |
< |
try { |
422 |
< |
LinkedBlockingQueue q = fullQueue(N); |
423 |
< |
for (int i = 0; i < N; ++i) { |
424 |
< |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
425 |
< |
} |
426 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
427 |
< |
} catch (InterruptedException e){ |
428 |
< |
fail("Unexpected exception"); |
384 |
< |
} |
420 |
> |
/** |
421 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
422 |
> |
*/ |
423 |
> |
public void testTimedPoll0() throws InterruptedException { |
424 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
425 |
> |
for (int i = 0; i < SIZE; ++i) { |
426 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
427 |
> |
} |
428 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
429 |
|
} |
430 |
|
|
431 |
< |
public void testTimedPoll() { |
432 |
< |
try { |
433 |
< |
LinkedBlockingQueue q = fullQueue(N); |
434 |
< |
for (int i = 0; i < N; ++i) { |
435 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
436 |
< |
} |
437 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
394 |
< |
} catch (InterruptedException e){ |
395 |
< |
fail("Unexpected exception"); |
396 |
< |
} |
397 |
< |
} |
398 |
< |
|
399 |
< |
public void testInterruptedTimedPoll(){ |
400 |
< |
Thread t = new Thread(new Runnable() { |
401 |
< |
public void run() { |
402 |
< |
try { |
403 |
< |
LinkedBlockingQueue q = fullQueue(N); |
404 |
< |
for (int i = 0; i < N; ++i) { |
405 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
406 |
< |
} |
407 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
408 |
< |
} catch (InterruptedException success){ |
409 |
< |
} |
410 |
< |
}}); |
411 |
< |
t.start(); |
412 |
< |
try { |
413 |
< |
Thread.sleep(SHORT_DELAY_MS); |
414 |
< |
t.interrupt(); |
415 |
< |
t.join(); |
416 |
< |
} |
417 |
< |
catch (InterruptedException ie) { |
418 |
< |
fail("Unexpected exception"); |
431 |
> |
/** |
432 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
433 |
> |
*/ |
434 |
> |
public void testTimedPoll() throws InterruptedException { |
435 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
436 |
> |
for (int i = 0; i < SIZE; ++i) { |
437 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
438 |
|
} |
439 |
+ |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
440 |
|
} |
441 |
|
|
442 |
< |
public void testTimedPollWithOffer(){ |
443 |
< |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
444 |
< |
Thread t = new Thread(new Runnable() { |
445 |
< |
public void run(){ |
446 |
< |
try { |
447 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
448 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
449 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
450 |
< |
fail("Should block"); |
451 |
< |
} catch (InterruptedException success) { } |
442 |
> |
/** |
443 |
> |
* Interrupted timed poll throws InterruptedException instead of |
444 |
> |
* returning timeout status |
445 |
> |
*/ |
446 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
447 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
448 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
449 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
450 |
> |
public void realRun() throws InterruptedException { |
451 |
> |
for (int i = 0; i < SIZE; ++i) { |
452 |
> |
long t0 = System.nanoTime(); |
453 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
454 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
455 |
|
} |
456 |
< |
}); |
457 |
< |
try { |
458 |
< |
t.start(); |
459 |
< |
Thread.sleep(SHORT_DELAY_MS * 2); |
460 |
< |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
461 |
< |
t.interrupt(); |
462 |
< |
t.join(); |
463 |
< |
} catch (Exception e){ |
464 |
< |
fail("Unexpected exception"); |
465 |
< |
} |
466 |
< |
} |
467 |
< |
|
468 |
< |
|
469 |
< |
public void testPeek(){ |
470 |
< |
LinkedBlockingQueue q = fullQueue(N); |
471 |
< |
for (int i = 0; i < N; ++i) { |
472 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
473 |
< |
q.poll(); |
456 |
> |
long t0 = System.nanoTime(); |
457 |
> |
aboutToWait.countDown(); |
458 |
> |
try { |
459 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
460 |
> |
shouldThrow(); |
461 |
> |
} catch (InterruptedException success) { |
462 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
463 |
> |
} |
464 |
> |
}}); |
465 |
> |
|
466 |
> |
aboutToWait.await(); |
467 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
468 |
> |
t.interrupt(); |
469 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
470 |
> |
checkEmpty(q); |
471 |
> |
} |
472 |
> |
|
473 |
> |
/** |
474 |
> |
* peek returns next element, or null if empty |
475 |
> |
*/ |
476 |
> |
public void testPeek() { |
477 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
478 |
> |
for (int i = 0; i < SIZE; ++i) { |
479 |
> |
assertEquals(i, q.peek()); |
480 |
> |
assertEquals(i, q.poll()); |
481 |
|
assertTrue(q.peek() == null || |
482 |
< |
i != ((Integer)q.peek()).intValue()); |
482 |
> |
!q.peek().equals(i)); |
483 |
|
} |
484 |
< |
assertNull(q.peek()); |
484 |
> |
assertNull(q.peek()); |
485 |
|
} |
486 |
|
|
487 |
< |
public void testElement(){ |
488 |
< |
LinkedBlockingQueue q = fullQueue(N); |
489 |
< |
for (int i = 0; i < N; ++i) { |
490 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
491 |
< |
q.poll(); |
487 |
> |
/** |
488 |
> |
* element returns next element, or throws NSEE if empty |
489 |
> |
*/ |
490 |
> |
public void testElement() { |
491 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
492 |
> |
for (int i = 0; i < SIZE; ++i) { |
493 |
> |
assertEquals(i, q.element()); |
494 |
> |
assertEquals(i, q.poll()); |
495 |
|
} |
496 |
|
try { |
497 |
|
q.element(); |
498 |
< |
fail("no such element"); |
499 |
< |
} |
467 |
< |
catch (NoSuchElementException success) {} |
498 |
> |
shouldThrow(); |
499 |
> |
} catch (NoSuchElementException success) {} |
500 |
|
} |
501 |
|
|
502 |
< |
public void testRemove(){ |
503 |
< |
LinkedBlockingQueue q = fullQueue(N); |
504 |
< |
for (int i = 0; i < N; ++i) { |
505 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
502 |
> |
/** |
503 |
> |
* remove removes next element, or throws NSEE if empty |
504 |
> |
*/ |
505 |
> |
public void testRemove() { |
506 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
507 |
> |
for (int i = 0; i < SIZE; ++i) { |
508 |
> |
assertEquals(i, q.remove()); |
509 |
|
} |
510 |
|
try { |
511 |
|
q.remove(); |
512 |
< |
fail("remove should throw"); |
513 |
< |
} catch (NoSuchElementException success){ |
514 |
< |
} |
515 |
< |
} |
516 |
< |
|
517 |
< |
public void testRemoveElement(){ |
518 |
< |
LinkedBlockingQueue q = fullQueue(N); |
519 |
< |
for (int i = 1; i < N; i+=2) { |
520 |
< |
assertTrue(q.remove(new Integer(i))); |
521 |
< |
} |
522 |
< |
for (int i = 0; i < N; i+=2) { |
523 |
< |
assertTrue(q.remove(new Integer(i))); |
524 |
< |
assertFalse(q.remove(new Integer(i+1))); |
525 |
< |
} |
526 |
< |
assert(q.isEmpty()); |
527 |
< |
} |
528 |
< |
|
529 |
< |
public void testContains(){ |
530 |
< |
LinkedBlockingQueue q = fullQueue(N); |
531 |
< |
for (int i = 0; i < N; ++i) { |
512 |
> |
shouldThrow(); |
513 |
> |
} catch (NoSuchElementException success) {} |
514 |
> |
} |
515 |
> |
|
516 |
> |
/** |
517 |
> |
* remove(x) removes x and returns true if present |
518 |
> |
*/ |
519 |
> |
public void testRemoveElement() { |
520 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
521 |
> |
for (int i = 1; i < SIZE; i+=2) { |
522 |
> |
assertTrue(q.contains(i)); |
523 |
> |
assertTrue(q.remove(i)); |
524 |
> |
assertFalse(q.contains(i)); |
525 |
> |
assertTrue(q.contains(i-1)); |
526 |
> |
} |
527 |
> |
for (int i = 0; i < SIZE; i+=2) { |
528 |
> |
assertTrue(q.contains(i)); |
529 |
> |
assertTrue(q.remove(i)); |
530 |
> |
assertFalse(q.contains(i)); |
531 |
> |
assertFalse(q.remove(i+1)); |
532 |
> |
assertFalse(q.contains(i+1)); |
533 |
> |
} |
534 |
> |
assertTrue(q.isEmpty()); |
535 |
> |
} |
536 |
> |
|
537 |
> |
/** |
538 |
> |
* An add following remove(x) succeeds |
539 |
> |
*/ |
540 |
> |
public void testRemoveElementAndAdd() throws InterruptedException { |
541 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
542 |
> |
assertTrue(q.add(new Integer(1))); |
543 |
> |
assertTrue(q.add(new Integer(2))); |
544 |
> |
assertTrue(q.remove(new Integer(1))); |
545 |
> |
assertTrue(q.remove(new Integer(2))); |
546 |
> |
assertTrue(q.add(new Integer(3))); |
547 |
> |
assertTrue(q.take() != null); |
548 |
> |
} |
549 |
> |
|
550 |
> |
/** |
551 |
> |
* contains(x) reports true when elements added but not yet removed |
552 |
> |
*/ |
553 |
> |
public void testContains() { |
554 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
555 |
> |
for (int i = 0; i < SIZE; ++i) { |
556 |
|
assertTrue(q.contains(new Integer(i))); |
557 |
|
q.poll(); |
558 |
|
assertFalse(q.contains(new Integer(i))); |
559 |
|
} |
560 |
|
} |
561 |
|
|
562 |
< |
public void testClear(){ |
563 |
< |
LinkedBlockingQueue q = fullQueue(N); |
562 |
> |
/** |
563 |
> |
* clear removes all elements |
564 |
> |
*/ |
565 |
> |
public void testClear() { |
566 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
567 |
|
q.clear(); |
568 |
|
assertTrue(q.isEmpty()); |
569 |
|
assertEquals(0, q.size()); |
570 |
< |
assertEquals(N, q.remainingCapacity()); |
571 |
< |
q.add(new Integer(1)); |
570 |
> |
assertEquals(SIZE, q.remainingCapacity()); |
571 |
> |
q.add(one); |
572 |
|
assertFalse(q.isEmpty()); |
573 |
+ |
assertTrue(q.contains(one)); |
574 |
|
q.clear(); |
575 |
|
assertTrue(q.isEmpty()); |
576 |
|
} |
577 |
|
|
578 |
< |
public void testContainsAll(){ |
579 |
< |
LinkedBlockingQueue q = fullQueue(N); |
580 |
< |
LinkedBlockingQueue p = new LinkedBlockingQueue(N); |
581 |
< |
for (int i = 0; i < N; ++i) { |
578 |
> |
/** |
579 |
> |
* containsAll(c) is true when c contains a subset of elements |
580 |
> |
*/ |
581 |
> |
public void testContainsAll() { |
582 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
583 |
> |
LinkedBlockingQueue p = new LinkedBlockingQueue(SIZE); |
584 |
> |
for (int i = 0; i < SIZE; ++i) { |
585 |
|
assertTrue(q.containsAll(p)); |
586 |
|
assertFalse(p.containsAll(q)); |
587 |
|
p.add(new Integer(i)); |
589 |
|
assertTrue(p.containsAll(q)); |
590 |
|
} |
591 |
|
|
592 |
< |
public void testRetainAll(){ |
593 |
< |
LinkedBlockingQueue q = fullQueue(N); |
594 |
< |
LinkedBlockingQueue p = fullQueue(N); |
595 |
< |
for (int i = 0; i < N; ++i) { |
592 |
> |
/** |
593 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
594 |
> |
*/ |
595 |
> |
public void testRetainAll() { |
596 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
597 |
> |
LinkedBlockingQueue p = populatedQueue(SIZE); |
598 |
> |
for (int i = 0; i < SIZE; ++i) { |
599 |
|
boolean changed = q.retainAll(p); |
600 |
|
if (i == 0) |
601 |
|
assertFalse(changed); |
603 |
|
assertTrue(changed); |
604 |
|
|
605 |
|
assertTrue(q.containsAll(p)); |
606 |
< |
assertEquals(N-i, q.size()); |
606 |
> |
assertEquals(SIZE-i, q.size()); |
607 |
|
p.remove(); |
608 |
|
} |
609 |
|
} |
610 |
|
|
611 |
< |
public void testRemoveAll(){ |
612 |
< |
for (int i = 1; i < N; ++i) { |
613 |
< |
LinkedBlockingQueue q = fullQueue(N); |
614 |
< |
LinkedBlockingQueue p = fullQueue(i); |
611 |
> |
/** |
612 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
613 |
> |
*/ |
614 |
> |
public void testRemoveAll() { |
615 |
> |
for (int i = 1; i < SIZE; ++i) { |
616 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
617 |
> |
LinkedBlockingQueue p = populatedQueue(i); |
618 |
|
assertTrue(q.removeAll(p)); |
619 |
< |
assertEquals(N-i, q.size()); |
619 |
> |
assertEquals(SIZE-i, q.size()); |
620 |
|
for (int j = 0; j < i; ++j) { |
621 |
|
Integer I = (Integer)(p.remove()); |
622 |
|
assertFalse(q.contains(I)); |
624 |
|
} |
625 |
|
} |
626 |
|
|
627 |
< |
|
628 |
< |
public void testToArray(){ |
629 |
< |
LinkedBlockingQueue q = fullQueue(N); |
630 |
< |
Object[] o = q.toArray(); |
631 |
< |
try { |
632 |
< |
for(int i = 0; i < o.length; i++) |
633 |
< |
assertEquals(o[i], q.take()); |
634 |
< |
} catch (InterruptedException e){ |
563 |
< |
fail("Unexpected exception"); |
564 |
< |
} |
565 |
< |
} |
566 |
< |
|
567 |
< |
public void testToArray2(){ |
568 |
< |
LinkedBlockingQueue q = fullQueue(N); |
569 |
< |
Integer[] ints = new Integer[N]; |
570 |
< |
ints = (Integer[])q.toArray(ints); |
571 |
< |
try { |
572 |
< |
for(int i = 0; i < ints.length; i++) |
573 |
< |
assertEquals(ints[i], q.take()); |
574 |
< |
} catch (InterruptedException e){ |
575 |
< |
fail("Unexpected exception"); |
576 |
< |
} |
577 |
< |
} |
578 |
< |
|
579 |
< |
public void testIterator(){ |
580 |
< |
LinkedBlockingQueue q = fullQueue(N); |
581 |
< |
Iterator it = q.iterator(); |
582 |
< |
try { |
583 |
< |
while(it.hasNext()){ |
584 |
< |
assertEquals(it.next(), q.take()); |
585 |
< |
} |
586 |
< |
} catch (InterruptedException e){ |
587 |
< |
fail("Unexpected exception"); |
588 |
< |
} |
627 |
> |
/** |
628 |
> |
* toArray contains all elements in FIFO order |
629 |
> |
*/ |
630 |
> |
public void testToArray() { |
631 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
632 |
> |
Object[] o = q.toArray(); |
633 |
> |
for (int i = 0; i < o.length; i++) |
634 |
> |
assertSame(o[i], q.poll()); |
635 |
|
} |
636 |
|
|
637 |
< |
public void testIteratorOrdering() { |
637 |
> |
/** |
638 |
> |
* toArray(a) contains all elements in FIFO order |
639 |
> |
*/ |
640 |
> |
public void testToArray2() throws InterruptedException { |
641 |
> |
LinkedBlockingQueue<Integer> q = populatedQueue(SIZE); |
642 |
> |
Integer[] ints = new Integer[SIZE]; |
643 |
> |
Integer[] array = q.toArray(ints); |
644 |
> |
assertSame(ints, array); |
645 |
> |
for (int i = 0; i < ints.length; i++) |
646 |
> |
assertSame(ints[i], q.poll()); |
647 |
> |
} |
648 |
|
|
649 |
< |
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
649 |
> |
/** |
650 |
> |
* toArray(null) throws NullPointerException |
651 |
> |
*/ |
652 |
> |
public void testToArray_NullArg() { |
653 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
654 |
> |
try { |
655 |
> |
q.toArray(null); |
656 |
> |
shouldThrow(); |
657 |
> |
} catch (NullPointerException success) {} |
658 |
> |
} |
659 |
|
|
660 |
< |
q.add(new Integer(1)); |
661 |
< |
q.add(new Integer(2)); |
662 |
< |
q.add(new Integer(3)); |
660 |
> |
/** |
661 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
662 |
> |
*/ |
663 |
> |
public void testToArray1_BadArg() { |
664 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
665 |
> |
try { |
666 |
> |
q.toArray(new String[10]); |
667 |
> |
shouldThrow(); |
668 |
> |
} catch (ArrayStoreException success) {} |
669 |
> |
} |
670 |
|
|
599 |
– |
assertEquals("queue should be full", 0, q.remainingCapacity()); |
671 |
|
|
672 |
< |
int k = 0; |
673 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
674 |
< |
int i = ((Integer)(it.next())).intValue(); |
675 |
< |
assertEquals("items should come out in order", ++k, i); |
672 |
> |
/** |
673 |
> |
* iterator iterates through all elements |
674 |
> |
*/ |
675 |
> |
public void testIterator() throws InterruptedException { |
676 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
677 |
> |
Iterator it = q.iterator(); |
678 |
> |
while (it.hasNext()) { |
679 |
> |
assertEquals(it.next(), q.take()); |
680 |
|
} |
606 |
– |
|
607 |
– |
assertEquals("should go through 3 elements", 3, k); |
681 |
|
} |
682 |
|
|
683 |
< |
public void testWeaklyConsistentIteration () { |
684 |
< |
|
683 |
> |
/** |
684 |
> |
* iterator.remove removes current element |
685 |
> |
*/ |
686 |
> |
public void testIteratorRemove() { |
687 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
688 |
+ |
q.add(two); |
689 |
+ |
q.add(one); |
690 |
+ |
q.add(three); |
691 |
+ |
|
692 |
+ |
Iterator it = q.iterator(); |
693 |
+ |
it.next(); |
694 |
+ |
it.remove(); |
695 |
+ |
|
696 |
+ |
it = q.iterator(); |
697 |
+ |
assertSame(it.next(), one); |
698 |
+ |
assertSame(it.next(), three); |
699 |
+ |
assertFalse(it.hasNext()); |
700 |
+ |
} |
701 |
|
|
614 |
– |
q.add(new Integer(1)); |
615 |
– |
q.add(new Integer(2)); |
616 |
– |
q.add(new Integer(3)); |
702 |
|
|
703 |
< |
try { |
704 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
705 |
< |
q.remove(); |
706 |
< |
it.next(); |
707 |
< |
} |
708 |
< |
} |
709 |
< |
catch (ConcurrentModificationException e) { |
710 |
< |
fail("weakly consistent iterator; should not get CME"); |
703 |
> |
/** |
704 |
> |
* iterator ordering is FIFO |
705 |
> |
*/ |
706 |
> |
public void testIteratorOrdering() { |
707 |
> |
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
708 |
> |
q.add(one); |
709 |
> |
q.add(two); |
710 |
> |
q.add(three); |
711 |
> |
assertEquals(0, q.remainingCapacity()); |
712 |
> |
int k = 0; |
713 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
714 |
> |
assertEquals(++k, it.next()); |
715 |
|
} |
716 |
+ |
assertEquals(3, k); |
717 |
+ |
} |
718 |
|
|
719 |
< |
assertEquals("queue should be empty again", 0, q.size()); |
719 |
> |
/** |
720 |
> |
* Modifications do not cause iterators to fail |
721 |
> |
*/ |
722 |
> |
public void testWeaklyConsistentIteration() { |
723 |
> |
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
724 |
> |
q.add(one); |
725 |
> |
q.add(two); |
726 |
> |
q.add(three); |
727 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
728 |
> |
q.remove(); |
729 |
> |
it.next(); |
730 |
> |
} |
731 |
> |
assertEquals(0, q.size()); |
732 |
|
} |
733 |
|
|
734 |
|
|
735 |
< |
public void testToString(){ |
736 |
< |
LinkedBlockingQueue q = fullQueue(N); |
735 |
> |
/** |
736 |
> |
* toString contains toStrings of elements |
737 |
> |
*/ |
738 |
> |
public void testToString() { |
739 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
740 |
|
String s = q.toString(); |
741 |
< |
for (int i = 0; i < N; ++i) { |
741 |
> |
for (int i = 0; i < SIZE; ++i) { |
742 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
743 |
|
} |
744 |
< |
} |
744 |
> |
} |
745 |
|
|
746 |
|
|
747 |
+ |
/** |
748 |
+ |
* offer transfers elements across Executor tasks |
749 |
+ |
*/ |
750 |
|
public void testOfferInExecutor() { |
642 |
– |
|
751 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
752 |
+ |
q.add(one); |
753 |
+ |
q.add(two); |
754 |
+ |
ExecutorService executor = Executors.newFixedThreadPool(2); |
755 |
+ |
executor.execute(new CheckedRunnable() { |
756 |
+ |
public void realRun() throws InterruptedException { |
757 |
+ |
assertFalse(q.offer(three)); |
758 |
+ |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
759 |
+ |
assertEquals(0, q.remainingCapacity()); |
760 |
+ |
}}); |
761 |
+ |
|
762 |
+ |
executor.execute(new CheckedRunnable() { |
763 |
+ |
public void realRun() throws InterruptedException { |
764 |
+ |
delay(SMALL_DELAY_MS); |
765 |
+ |
assertSame(one, q.take()); |
766 |
+ |
}}); |
767 |
|
|
768 |
< |
q.add(new Integer(1)); |
769 |
< |
q.add(new Integer(2)); |
768 |
> |
joinPool(executor); |
769 |
> |
} |
770 |
|
|
771 |
+ |
/** |
772 |
+ |
* poll retrieves elements across Executor threads |
773 |
+ |
*/ |
774 |
+ |
public void testPollInExecutor() { |
775 |
+ |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
776 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
777 |
+ |
executor.execute(new CheckedRunnable() { |
778 |
+ |
public void realRun() throws InterruptedException { |
779 |
+ |
assertNull(q.poll()); |
780 |
+ |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
781 |
+ |
assertTrue(q.isEmpty()); |
782 |
+ |
}}); |
783 |
+ |
|
784 |
+ |
executor.execute(new CheckedRunnable() { |
785 |
+ |
public void realRun() throws InterruptedException { |
786 |
+ |
delay(SMALL_DELAY_MS); |
787 |
+ |
q.put(one); |
788 |
+ |
}}); |
789 |
|
|
790 |
< |
executor.execute(new Runnable() { |
791 |
< |
public void run() { |
652 |
< |
assertFalse("offer should be rejected", q.offer(new Integer(3))); |
653 |
< |
try { |
654 |
< |
assertTrue("offer should be accepted", q.offer(new Integer(3), MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); |
655 |
< |
assertEquals(0, q.remainingCapacity()); |
656 |
< |
} |
657 |
< |
catch (InterruptedException e) { |
658 |
< |
fail("should not be interrupted"); |
659 |
< |
} |
660 |
< |
} |
661 |
< |
}); |
790 |
> |
joinPool(executor); |
791 |
> |
} |
792 |
|
|
793 |
< |
executor.execute(new Runnable() { |
794 |
< |
public void run() { |
795 |
< |
try { |
796 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
797 |
< |
assertEquals("first item in queue should be 1", new Integer(1), q.take()); |
668 |
< |
} |
669 |
< |
catch (InterruptedException e) { |
670 |
< |
fail("should not be interrupted"); |
671 |
< |
} |
672 |
< |
} |
673 |
< |
}); |
674 |
< |
|
675 |
< |
executor.shutdown(); |
793 |
> |
/** |
794 |
> |
* A deserialized serialized queue has same elements in same order |
795 |
> |
*/ |
796 |
> |
public void testSerialization() throws Exception { |
797 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
798 |
|
|
799 |
+ |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
800 |
+ |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
801 |
+ |
out.writeObject(q); |
802 |
+ |
out.close(); |
803 |
+ |
|
804 |
+ |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
805 |
+ |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
806 |
+ |
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
807 |
+ |
assertEquals(q.size(), r.size()); |
808 |
+ |
while (!q.isEmpty()) |
809 |
+ |
assertEquals(q.remove(), r.remove()); |
810 |
|
} |
811 |
|
|
812 |
< |
public void testPollInExecutor() { |
812 |
> |
/** |
813 |
> |
* drainTo(null) throws NPE |
814 |
> |
*/ |
815 |
> |
public void testDrainToNull() { |
816 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
817 |
> |
try { |
818 |
> |
q.drainTo(null); |
819 |
> |
shouldThrow(); |
820 |
> |
} catch (NullPointerException success) {} |
821 |
> |
} |
822 |
|
|
823 |
< |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
823 |
> |
/** |
824 |
> |
* drainTo(this) throws IAE |
825 |
> |
*/ |
826 |
> |
public void testDrainToSelf() { |
827 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
828 |
> |
try { |
829 |
> |
q.drainTo(q); |
830 |
> |
shouldThrow(); |
831 |
> |
} catch (IllegalArgumentException success) {} |
832 |
> |
} |
833 |
|
|
834 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
834 |
> |
/** |
835 |
> |
* drainTo(c) empties queue into another collection c |
836 |
> |
*/ |
837 |
> |
public void testDrainTo() { |
838 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
839 |
> |
ArrayList l = new ArrayList(); |
840 |
> |
q.drainTo(l); |
841 |
> |
assertEquals(q.size(), 0); |
842 |
> |
assertEquals(l.size(), SIZE); |
843 |
> |
for (int i = 0; i < SIZE; ++i) |
844 |
> |
assertEquals(l.get(i), new Integer(i)); |
845 |
> |
q.add(zero); |
846 |
> |
q.add(one); |
847 |
> |
assertFalse(q.isEmpty()); |
848 |
> |
assertTrue(q.contains(zero)); |
849 |
> |
assertTrue(q.contains(one)); |
850 |
> |
l.clear(); |
851 |
> |
q.drainTo(l); |
852 |
> |
assertEquals(q.size(), 0); |
853 |
> |
assertEquals(l.size(), 2); |
854 |
> |
for (int i = 0; i < 2; ++i) |
855 |
> |
assertEquals(l.get(i), new Integer(i)); |
856 |
> |
} |
857 |
|
|
858 |
< |
executor.execute(new Runnable() { |
859 |
< |
public void run() { |
860 |
< |
assertNull("poll should fail", q.poll()); |
861 |
< |
try { |
862 |
< |
assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); |
863 |
< |
assertTrue(q.isEmpty()); |
864 |
< |
} |
865 |
< |
catch (InterruptedException e) { |
866 |
< |
fail("should not be interrupted"); |
694 |
< |
} |
695 |
< |
} |
696 |
< |
}); |
858 |
> |
/** |
859 |
> |
* drainTo empties full queue, unblocking a waiting put. |
860 |
> |
*/ |
861 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
862 |
> |
final LinkedBlockingQueue q = populatedQueue(SIZE); |
863 |
> |
Thread t = new Thread(new CheckedRunnable() { |
864 |
> |
public void realRun() throws InterruptedException { |
865 |
> |
q.put(new Integer(SIZE+1)); |
866 |
> |
}}); |
867 |
|
|
868 |
< |
executor.execute(new Runnable() { |
869 |
< |
public void run() { |
870 |
< |
try { |
871 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
872 |
< |
q.put(new Integer(1)); |
873 |
< |
} |
874 |
< |
catch (InterruptedException e) { |
875 |
< |
fail("should not be interrupted"); |
876 |
< |
} |
877 |
< |
} |
878 |
< |
}); |
879 |
< |
|
880 |
< |
executor.shutdown(); |
868 |
> |
t.start(); |
869 |
> |
ArrayList l = new ArrayList(); |
870 |
> |
q.drainTo(l); |
871 |
> |
assertTrue(l.size() >= SIZE); |
872 |
> |
for (int i = 0; i < SIZE; ++i) |
873 |
> |
assertEquals(l.get(i), new Integer(i)); |
874 |
> |
t.join(); |
875 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
876 |
> |
} |
877 |
> |
|
878 |
> |
/** |
879 |
> |
* drainTo(null, n) throws NPE |
880 |
> |
*/ |
881 |
> |
public void testDrainToNullN() { |
882 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
883 |
> |
try { |
884 |
> |
q.drainTo(null, 0); |
885 |
> |
shouldThrow(); |
886 |
> |
} catch (NullPointerException success) {} |
887 |
> |
} |
888 |
|
|
889 |
+ |
/** |
890 |
+ |
* drainTo(this, n) throws IAE |
891 |
+ |
*/ |
892 |
+ |
public void testDrainToSelfN() { |
893 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
894 |
+ |
try { |
895 |
+ |
q.drainTo(q, 0); |
896 |
+ |
shouldThrow(); |
897 |
+ |
} catch (IllegalArgumentException success) {} |
898 |
+ |
} |
899 |
+ |
|
900 |
+ |
/** |
901 |
+ |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
902 |
+ |
*/ |
903 |
+ |
public void testDrainToN() { |
904 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
905 |
+ |
for (int i = 0; i < SIZE + 2; ++i) { |
906 |
+ |
for (int j = 0; j < SIZE; j++) |
907 |
+ |
assertTrue(q.offer(new Integer(j))); |
908 |
+ |
ArrayList l = new ArrayList(); |
909 |
+ |
q.drainTo(l, i); |
910 |
+ |
int k = (i < SIZE) ? i : SIZE; |
911 |
+ |
assertEquals(l.size(), k); |
912 |
+ |
assertEquals(q.size(), SIZE-k); |
913 |
+ |
for (int j = 0; j < k; ++j) |
914 |
+ |
assertEquals(l.get(j), new Integer(j)); |
915 |
+ |
while (q.poll() != null) ; |
916 |
+ |
} |
917 |
|
} |
918 |
|
|
919 |
|
} |