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