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