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