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