1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.*; |
11 |
< |
import java.util.concurrent.*; |
12 |
< |
import java.io.*; |
13 |
< |
|
14 |
< |
public class LinkedBlockingQueueTest extends TestCase { |
15 |
< |
|
16 |
< |
private static int N = 10; |
17 |
< |
private static long SHORT_DELAY_MS = 100; |
18 |
< |
private static long MEDIUM_DELAY_MS = 1000; |
19 |
< |
private static long LONG_DELAY_MS = 10000; |
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 |
< |
} |
52 |
< |
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 |
< |
} |
61 |
< |
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"); |
69 |
< |
} |
70 |
< |
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 |
< |
|
202 |
< |
public void testPutNull() { |
203 |
< |
try { |
204 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
205 |
< |
q.put(null); |
206 |
< |
fail("put should throw NPE"); |
207 |
< |
} |
208 |
< |
catch (NullPointerException success){ |
209 |
< |
} |
210 |
< |
catch (InterruptedException ie) { |
211 |
< |
fail("Unexpected exception"); |
212 |
< |
} |
213 |
< |
} |
214 |
< |
|
215 |
< |
public void testPut() { |
216 |
< |
try { |
217 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
218 |
< |
for (int i = 0; i < N; ++i) { |
219 |
< |
Integer I = new Integer(i); |
220 |
< |
q.put(I); |
221 |
< |
assertTrue(q.contains(I)); |
222 |
< |
} |
223 |
< |
assertEquals(0, q.remainingCapacity()); |
224 |
< |
} |
225 |
< |
catch (InterruptedException ie) { |
226 |
< |
fail("Unexpected exception"); |
227 |
< |
} |
228 |
< |
} |
229 |
< |
|
230 |
< |
public void testBlockingPut(){ |
231 |
< |
Thread t = new Thread(new Runnable() { |
232 |
< |
public void run() { |
233 |
< |
int added = 0; |
234 |
< |
try { |
235 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(N); |
236 |
< |
for (int i = 0; i < N; ++i) { |
237 |
< |
q.put(new Integer(i)); |
238 |
< |
++added; |
239 |
< |
} |
240 |
< |
q.put(new Integer(N)); |
241 |
< |
fail("put should block"); |
242 |
< |
} catch (InterruptedException ie){ |
243 |
< |
assertEquals(added, N); |
244 |
< |
} |
245 |
< |
}}); |
246 |
< |
t.start(); |
247 |
< |
try { |
248 |
< |
Thread.sleep(SHORT_DELAY_MS); |
249 |
< |
t.interrupt(); |
250 |
< |
t.join(); |
251 |
< |
} |
252 |
< |
catch (InterruptedException ie) { |
253 |
< |
fail("Unexpected exception"); |
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)); |
296 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
297 |
< |
fail("Should block"); |
298 |
< |
} catch (InterruptedException success){} |
299 |
< |
} |
300 |
< |
}); |
301 |
< |
|
302 |
< |
try { |
303 |
< |
t.start(); |
304 |
< |
Thread.sleep(SHORT_DELAY_MS); |
305 |
< |
t.interrupt(); |
306 |
< |
t.join(); |
307 |
< |
} catch (Exception e){ |
308 |
< |
fail("Unexpected exception"); |
309 |
< |
} |
310 |
< |
} |
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"); |
330 |
< |
} catch (InterruptedException success){ } |
331 |
< |
} |
332 |
< |
}); |
333 |
< |
try { |
334 |
< |
t.start(); |
335 |
< |
Thread.sleep(SHORT_DELAY_MS); |
336 |
< |
t.interrupt(); |
337 |
< |
t.join(); |
338 |
< |
} catch (Exception e){ |
339 |
< |
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"); |
489 |
< |
} |
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 |
> |
long startTime = System.nanoTime(); |
467 |
> |
for (int i = 0; i < SIZE; i++) |
468 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
469 |
> |
|
470 |
> |
Thread.currentThread().interrupt(); |
471 |
> |
try { |
472 |
> |
q.poll(randomTimeout(), randomTimeUnit()); |
473 |
> |
shouldThrow(); |
474 |
> |
} catch (InterruptedException success) {} |
475 |
> |
assertFalse(Thread.interrupted()); |
476 |
> |
|
477 |
> |
pleaseInterrupt.countDown(); |
478 |
> |
try { |
479 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
480 |
> |
shouldThrow(); |
481 |
> |
} catch (InterruptedException success) {} |
482 |
> |
assertFalse(Thread.interrupted()); |
483 |
> |
|
484 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
485 |
> |
}}); |
486 |
> |
|
487 |
> |
await(pleaseInterrupt); |
488 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
489 |
> |
t.interrupt(); |
490 |
> |
awaitTermination(t); |
491 |
> |
checkEmpty(q); |
492 |
|
} |
493 |
|
|
494 |
< |
public void testTimedPollWithOffer(){ |
495 |
< |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
496 |
< |
Thread t = new Thread(new Runnable() { |
497 |
< |
public void run(){ |
498 |
< |
try { |
499 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
500 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
501 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
431 |
< |
fail("Should block"); |
432 |
< |
} catch (InterruptedException success) { } |
433 |
< |
} |
434 |
< |
}); |
435 |
< |
try { |
436 |
< |
t.start(); |
437 |
< |
Thread.sleep(SHORT_DELAY_MS * 2); |
438 |
< |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
439 |
< |
t.interrupt(); |
440 |
< |
t.join(); |
441 |
< |
} catch (Exception e){ |
442 |
< |
fail("Unexpected exception"); |
443 |
< |
} |
444 |
< |
} |
445 |
< |
|
446 |
< |
|
447 |
< |
public void testPeek(){ |
448 |
< |
LinkedBlockingQueue q = fullQueue(N); |
449 |
< |
for (int i = 0; i < N; ++i) { |
450 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
451 |
< |
q.poll(); |
494 |
> |
/** |
495 |
> |
* peek returns next element, or null if empty |
496 |
> |
*/ |
497 |
> |
public void testPeek() { |
498 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
499 |
> |
for (int i = 0; i < SIZE; ++i) { |
500 |
> |
assertEquals(i, q.peek()); |
501 |
> |
assertEquals(i, q.poll()); |
502 |
|
assertTrue(q.peek() == null || |
503 |
< |
i != ((Integer)q.peek()).intValue()); |
503 |
> |
!q.peek().equals(i)); |
504 |
|
} |
505 |
< |
assertNull(q.peek()); |
505 |
> |
assertNull(q.peek()); |
506 |
|
} |
507 |
|
|
508 |
< |
public void testElement(){ |
509 |
< |
LinkedBlockingQueue q = fullQueue(N); |
510 |
< |
for (int i = 0; i < N; ++i) { |
511 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
512 |
< |
q.poll(); |
508 |
> |
/** |
509 |
> |
* element returns next element, or throws NSEE if empty |
510 |
> |
*/ |
511 |
> |
public void testElement() { |
512 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
513 |
> |
for (int i = 0; i < SIZE; ++i) { |
514 |
> |
assertEquals(i, q.element()); |
515 |
> |
assertEquals(i, q.poll()); |
516 |
|
} |
517 |
|
try { |
518 |
|
q.element(); |
519 |
< |
fail("no such element"); |
520 |
< |
} |
468 |
< |
catch (NoSuchElementException success) {} |
519 |
> |
shouldThrow(); |
520 |
> |
} catch (NoSuchElementException success) {} |
521 |
|
} |
522 |
|
|
523 |
< |
public void testRemove(){ |
524 |
< |
LinkedBlockingQueue q = fullQueue(N); |
525 |
< |
for (int i = 0; i < N; ++i) { |
526 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
523 |
> |
/** |
524 |
> |
* remove removes next element, or throws NSEE if empty |
525 |
> |
*/ |
526 |
> |
public void testRemove() { |
527 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
528 |
> |
for (int i = 0; i < SIZE; ++i) { |
529 |
> |
assertEquals(i, q.remove()); |
530 |
|
} |
531 |
|
try { |
532 |
|
q.remove(); |
533 |
< |
fail("remove should throw"); |
534 |
< |
} catch (NoSuchElementException success){ |
480 |
< |
} |
533 |
> |
shouldThrow(); |
534 |
> |
} catch (NoSuchElementException success) {} |
535 |
|
} |
536 |
|
|
537 |
< |
public void testRemoveElement(){ |
538 |
< |
LinkedBlockingQueue q = fullQueue(N); |
539 |
< |
for (int i = 1; i < N; i+=2) { |
540 |
< |
assertTrue(q.remove(new Integer(i))); |
541 |
< |
} |
542 |
< |
for (int i = 0; i < N; i+=2) { |
543 |
< |
assertTrue(q.remove(new Integer(i))); |
544 |
< |
assertFalse(q.remove(new Integer(i+1))); |
545 |
< |
} |
546 |
< |
assertTrue(q.isEmpty()); |
537 |
> |
/** |
538 |
> |
* An add following remove(x) succeeds |
539 |
> |
*/ |
540 |
> |
public void testRemoveElementAndAdd() throws InterruptedException { |
541 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
542 |
> |
assertTrue(q.add(new Integer(1))); |
543 |
> |
assertTrue(q.add(new Integer(2))); |
544 |
> |
assertTrue(q.remove(new Integer(1))); |
545 |
> |
assertTrue(q.remove(new Integer(2))); |
546 |
> |
assertTrue(q.add(new Integer(3))); |
547 |
> |
assertNotNull(q.take()); |
548 |
|
} |
549 |
< |
|
550 |
< |
public void testContains(){ |
551 |
< |
LinkedBlockingQueue q = fullQueue(N); |
552 |
< |
for (int i = 0; i < N; ++i) { |
549 |
> |
|
550 |
> |
/** |
551 |
> |
* contains(x) reports true when elements added but not yet removed |
552 |
> |
*/ |
553 |
> |
public void testContains() { |
554 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
555 |
> |
for (int i = 0; i < SIZE; ++i) { |
556 |
|
assertTrue(q.contains(new Integer(i))); |
557 |
|
q.poll(); |
558 |
|
assertFalse(q.contains(new Integer(i))); |
559 |
|
} |
560 |
|
} |
561 |
|
|
562 |
< |
public void testClear(){ |
563 |
< |
LinkedBlockingQueue q = fullQueue(N); |
562 |
> |
/** |
563 |
> |
* clear removes all elements |
564 |
> |
*/ |
565 |
> |
public void testClear() { |
566 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
567 |
|
q.clear(); |
568 |
|
assertTrue(q.isEmpty()); |
569 |
|
assertEquals(0, q.size()); |
570 |
< |
assertEquals(N, q.remainingCapacity()); |
571 |
< |
q.add(new Integer(1)); |
570 |
> |
assertEquals(SIZE, q.remainingCapacity()); |
571 |
> |
q.add(one); |
572 |
|
assertFalse(q.isEmpty()); |
573 |
+ |
assertTrue(q.contains(one)); |
574 |
|
q.clear(); |
575 |
|
assertTrue(q.isEmpty()); |
576 |
|
} |
577 |
|
|
578 |
< |
public void testContainsAll(){ |
579 |
< |
LinkedBlockingQueue q = fullQueue(N); |
580 |
< |
LinkedBlockingQueue p = new LinkedBlockingQueue(N); |
581 |
< |
for (int i = 0; i < N; ++i) { |
578 |
> |
/** |
579 |
> |
* containsAll(c) is true when c contains a subset of elements |
580 |
> |
*/ |
581 |
> |
public void testContainsAll() { |
582 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
583 |
> |
LinkedBlockingQueue p = new LinkedBlockingQueue(SIZE); |
584 |
> |
for (int i = 0; i < SIZE; ++i) { |
585 |
|
assertTrue(q.containsAll(p)); |
586 |
|
assertFalse(p.containsAll(q)); |
587 |
|
p.add(new Integer(i)); |
589 |
|
assertTrue(p.containsAll(q)); |
590 |
|
} |
591 |
|
|
592 |
< |
public void testRetainAll(){ |
593 |
< |
LinkedBlockingQueue q = fullQueue(N); |
594 |
< |
LinkedBlockingQueue p = fullQueue(N); |
595 |
< |
for (int i = 0; i < N; ++i) { |
592 |
> |
/** |
593 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
594 |
> |
*/ |
595 |
> |
public void testRetainAll() { |
596 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
597 |
> |
LinkedBlockingQueue p = populatedQueue(SIZE); |
598 |
> |
for (int i = 0; i < SIZE; ++i) { |
599 |
|
boolean changed = q.retainAll(p); |
600 |
|
if (i == 0) |
601 |
|
assertFalse(changed); |
603 |
|
assertTrue(changed); |
604 |
|
|
605 |
|
assertTrue(q.containsAll(p)); |
606 |
< |
assertEquals(N-i, q.size()); |
606 |
> |
assertEquals(SIZE - i, q.size()); |
607 |
|
p.remove(); |
608 |
|
} |
609 |
|
} |
610 |
|
|
611 |
< |
public void testRemoveAll(){ |
612 |
< |
for (int i = 1; i < N; ++i) { |
613 |
< |
LinkedBlockingQueue q = fullQueue(N); |
614 |
< |
LinkedBlockingQueue p = fullQueue(i); |
611 |
> |
/** |
612 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
613 |
> |
*/ |
614 |
> |
public void testRemoveAll() { |
615 |
> |
for (int i = 1; i < SIZE; ++i) { |
616 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
617 |
> |
LinkedBlockingQueue p = populatedQueue(i); |
618 |
|
assertTrue(q.removeAll(p)); |
619 |
< |
assertEquals(N-i, q.size()); |
619 |
> |
assertEquals(SIZE - i, q.size()); |
620 |
|
for (int j = 0; j < i; ++j) { |
621 |
< |
Integer I = (Integer)(p.remove()); |
622 |
< |
assertFalse(q.contains(I)); |
621 |
> |
Integer x = (Integer)(p.remove()); |
622 |
> |
assertFalse(q.contains(x)); |
623 |
|
} |
624 |
|
} |
625 |
|
} |
626 |
|
|
627 |
+ |
/** |
628 |
+ |
* toArray contains all elements in FIFO order |
629 |
+ |
*/ |
630 |
+ |
public void testToArray() { |
631 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
632 |
+ |
Object[] a = q.toArray(); |
633 |
+ |
assertSame(Object[].class, a.getClass()); |
634 |
+ |
for (Object o : a) |
635 |
+ |
assertSame(o, q.poll()); |
636 |
+ |
assertTrue(q.isEmpty()); |
637 |
+ |
} |
638 |
|
|
639 |
< |
public void testToArray(){ |
640 |
< |
LinkedBlockingQueue q = fullQueue(N); |
641 |
< |
Object[] o = q.toArray(); |
642 |
< |
try { |
643 |
< |
for(int i = 0; i < o.length; i++) |
644 |
< |
assertEquals(o[i], q.take()); |
645 |
< |
} catch (InterruptedException e){ |
646 |
< |
fail("Unexpected exception"); |
647 |
< |
} |
648 |
< |
} |
649 |
< |
|
568 |
< |
public void testToArray2(){ |
569 |
< |
LinkedBlockingQueue q = fullQueue(N); |
570 |
< |
Integer[] ints = new Integer[N]; |
571 |
< |
ints = (Integer[])q.toArray(ints); |
572 |
< |
try { |
573 |
< |
for(int i = 0; i < ints.length; i++) |
574 |
< |
assertEquals(ints[i], q.take()); |
575 |
< |
} catch (InterruptedException e){ |
576 |
< |
fail("Unexpected exception"); |
577 |
< |
} |
578 |
< |
} |
579 |
< |
|
580 |
< |
public void testIterator(){ |
581 |
< |
LinkedBlockingQueue q = fullQueue(N); |
582 |
< |
Iterator it = q.iterator(); |
583 |
< |
try { |
584 |
< |
while(it.hasNext()){ |
585 |
< |
assertEquals(it.next(), q.take()); |
586 |
< |
} |
587 |
< |
} catch (InterruptedException e){ |
588 |
< |
fail("Unexpected exception"); |
589 |
< |
} |
639 |
> |
/** |
640 |
> |
* toArray(a) contains all elements in FIFO order |
641 |
> |
*/ |
642 |
> |
public void testToArray2() throws InterruptedException { |
643 |
> |
LinkedBlockingQueue<Integer> q = populatedQueue(SIZE); |
644 |
> |
Integer[] ints = new Integer[SIZE]; |
645 |
> |
Integer[] array = q.toArray(ints); |
646 |
> |
assertSame(ints, array); |
647 |
> |
for (Integer o : ints) |
648 |
> |
assertSame(o, q.poll()); |
649 |
> |
assertTrue(q.isEmpty()); |
650 |
|
} |
651 |
|
|
652 |
< |
public void testIteratorOrdering() { |
652 |
> |
/** |
653 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
654 |
> |
*/ |
655 |
> |
public void testToArray1_BadArg() { |
656 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
657 |
> |
try { |
658 |
> |
q.toArray(new String[10]); |
659 |
> |
shouldThrow(); |
660 |
> |
} catch (ArrayStoreException success) {} |
661 |
> |
} |
662 |
|
|
663 |
< |
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
663 |
> |
/** |
664 |
> |
* iterator iterates through all elements |
665 |
> |
*/ |
666 |
> |
public void testIterator() throws InterruptedException { |
667 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
668 |
> |
Iterator it = q.iterator(); |
669 |
> |
int i; |
670 |
> |
for (i = 0; it.hasNext(); i++) |
671 |
> |
assertTrue(q.contains(it.next())); |
672 |
> |
assertEquals(i, SIZE); |
673 |
> |
assertIteratorExhausted(it); |
674 |
> |
|
675 |
> |
it = q.iterator(); |
676 |
> |
for (i = 0; it.hasNext(); i++) |
677 |
> |
assertEquals(it.next(), q.take()); |
678 |
> |
assertEquals(i, SIZE); |
679 |
> |
assertIteratorExhausted(it); |
680 |
> |
} |
681 |
|
|
682 |
< |
q.add(new Integer(1)); |
683 |
< |
q.add(new Integer(2)); |
684 |
< |
q.add(new Integer(3)); |
682 |
> |
/** |
683 |
> |
* iterator of empty collection has no elements |
684 |
> |
*/ |
685 |
> |
public void testEmptyIterator() { |
686 |
> |
assertIteratorExhausted(new LinkedBlockingQueue().iterator()); |
687 |
> |
} |
688 |
|
|
689 |
< |
assertEquals("queue should be full", 0, q.remainingCapacity()); |
689 |
> |
/** |
690 |
> |
* iterator.remove removes current element |
691 |
> |
*/ |
692 |
> |
public void testIteratorRemove() { |
693 |
> |
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
694 |
> |
q.add(two); |
695 |
> |
q.add(one); |
696 |
> |
q.add(three); |
697 |
> |
|
698 |
> |
Iterator it = q.iterator(); |
699 |
> |
it.next(); |
700 |
> |
it.remove(); |
701 |
> |
|
702 |
> |
it = q.iterator(); |
703 |
> |
assertSame(it.next(), one); |
704 |
> |
assertSame(it.next(), three); |
705 |
> |
assertFalse(it.hasNext()); |
706 |
> |
} |
707 |
|
|
708 |
+ |
/** |
709 |
+ |
* iterator ordering is FIFO |
710 |
+ |
*/ |
711 |
+ |
public void testIteratorOrdering() { |
712 |
+ |
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
713 |
+ |
q.add(one); |
714 |
+ |
q.add(two); |
715 |
+ |
q.add(three); |
716 |
+ |
assertEquals(0, q.remainingCapacity()); |
717 |
|
int k = 0; |
718 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
719 |
< |
int i = ((Integer)(it.next())).intValue(); |
605 |
< |
assertEquals("items should come out in order", ++k, i); |
719 |
> |
assertEquals(++k, it.next()); |
720 |
|
} |
721 |
< |
|
608 |
< |
assertEquals("should go through 3 elements", 3, k); |
721 |
> |
assertEquals(3, k); |
722 |
|
} |
723 |
|
|
724 |
< |
public void testWeaklyConsistentIteration () { |
725 |
< |
|
724 |
> |
/** |
725 |
> |
* Modifications do not cause iterators to fail |
726 |
> |
*/ |
727 |
> |
public void testWeaklyConsistentIteration() { |
728 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
729 |
< |
|
730 |
< |
q.add(new Integer(1)); |
731 |
< |
q.add(new Integer(2)); |
732 |
< |
q.add(new Integer(3)); |
733 |
< |
|
734 |
< |
try { |
620 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
621 |
< |
q.remove(); |
622 |
< |
it.next(); |
623 |
< |
} |
624 |
< |
} |
625 |
< |
catch (ConcurrentModificationException e) { |
626 |
< |
fail("weakly consistent iterator; should not get CME"); |
729 |
> |
q.add(one); |
730 |
> |
q.add(two); |
731 |
> |
q.add(three); |
732 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
733 |
> |
q.remove(); |
734 |
> |
it.next(); |
735 |
|
} |
736 |
< |
|
629 |
< |
assertEquals("queue should be empty again", 0, q.size()); |
736 |
> |
assertEquals(0, q.size()); |
737 |
|
} |
738 |
|
|
739 |
< |
|
740 |
< |
public void testToString(){ |
741 |
< |
LinkedBlockingQueue q = fullQueue(N); |
739 |
> |
/** |
740 |
> |
* toString contains toStrings of elements |
741 |
> |
*/ |
742 |
> |
public void testToString() { |
743 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
744 |
|
String s = q.toString(); |
745 |
< |
for (int i = 0; i < N; ++i) { |
746 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
745 |
> |
for (int i = 0; i < SIZE; ++i) { |
746 |
> |
assertTrue(s.contains(String.valueOf(i))); |
747 |
|
} |
748 |
< |
} |
640 |
< |
|
748 |
> |
} |
749 |
|
|
750 |
+ |
/** |
751 |
+ |
* offer transfers elements across Executor tasks |
752 |
+ |
*/ |
753 |
|
public void testOfferInExecutor() { |
643 |
– |
|
754 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
755 |
< |
|
756 |
< |
q.add(new Integer(1)); |
757 |
< |
q.add(new Integer(2)); |
758 |
< |
|
759 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
760 |
< |
|
761 |
< |
executor.execute(new Runnable() { |
762 |
< |
public void run() { |
763 |
< |
assertFalse("offer should be rejected", q.offer(new Integer(3))); |
764 |
< |
try { |
655 |
< |
assertTrue("offer should be accepted", q.offer(new Integer(3), MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); |
755 |
> |
q.add(one); |
756 |
> |
q.add(two); |
757 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
758 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
759 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
760 |
> |
executor.execute(new CheckedRunnable() { |
761 |
> |
public void realRun() throws InterruptedException { |
762 |
> |
assertFalse(q.offer(three)); |
763 |
> |
threadsStarted.await(); |
764 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
765 |
|
assertEquals(0, q.remainingCapacity()); |
766 |
< |
} |
658 |
< |
catch (InterruptedException e) { |
659 |
< |
fail("should not be interrupted"); |
660 |
< |
} |
661 |
< |
} |
662 |
< |
}); |
663 |
< |
|
664 |
< |
executor.execute(new Runnable() { |
665 |
< |
public void run() { |
666 |
< |
try { |
667 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
668 |
< |
assertEquals("first item in queue should be 1", new Integer(1), q.take()); |
669 |
< |
} |
670 |
< |
catch (InterruptedException e) { |
671 |
< |
fail("should not be interrupted"); |
672 |
< |
} |
673 |
< |
} |
674 |
< |
}); |
675 |
< |
|
676 |
< |
executor.shutdown(); |
766 |
> |
}}); |
767 |
|
|
768 |
+ |
executor.execute(new CheckedRunnable() { |
769 |
+ |
public void realRun() throws InterruptedException { |
770 |
+ |
threadsStarted.await(); |
771 |
+ |
assertSame(one, q.take()); |
772 |
+ |
}}); |
773 |
+ |
} |
774 |
|
} |
775 |
|
|
776 |
+ |
/** |
777 |
+ |
* timed poll retrieves elements across Executor threads |
778 |
+ |
*/ |
779 |
|
public void testPollInExecutor() { |
681 |
– |
|
780 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
781 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
782 |
+ |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
783 |
+ |
try (PoolCleaner cleaner = cleaner(executor)) { |
784 |
+ |
executor.execute(new CheckedRunnable() { |
785 |
+ |
public void realRun() throws InterruptedException { |
786 |
+ |
assertNull(q.poll()); |
787 |
+ |
threadsStarted.await(); |
788 |
+ |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
789 |
+ |
checkEmpty(q); |
790 |
+ |
}}); |
791 |
|
|
792 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
792 |
> |
executor.execute(new CheckedRunnable() { |
793 |
> |
public void realRun() throws InterruptedException { |
794 |
> |
threadsStarted.await(); |
795 |
> |
q.put(one); |
796 |
> |
}}); |
797 |
> |
} |
798 |
> |
} |
799 |
|
|
800 |
< |
executor.execute(new Runnable() { |
801 |
< |
public void run() { |
802 |
< |
assertNull("poll should fail", q.poll()); |
803 |
< |
try { |
804 |
< |
assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); |
805 |
< |
assertTrue(q.isEmpty()); |
806 |
< |
} |
807 |
< |
catch (InterruptedException e) { |
808 |
< |
fail("should not be interrupted"); |
809 |
< |
} |
810 |
< |
} |
811 |
< |
}); |
800 |
> |
/** |
801 |
> |
* A deserialized/reserialized queue has same elements in same order |
802 |
> |
*/ |
803 |
> |
public void testSerialization() throws Exception { |
804 |
> |
Queue x = populatedQueue(SIZE); |
805 |
> |
Queue y = serialClone(x); |
806 |
> |
|
807 |
> |
assertNotSame(x, y); |
808 |
> |
assertEquals(x.size(), y.size()); |
809 |
> |
assertEquals(x.toString(), y.toString()); |
810 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
811 |
> |
while (!x.isEmpty()) { |
812 |
> |
assertFalse(y.isEmpty()); |
813 |
> |
assertEquals(x.remove(), y.remove()); |
814 |
> |
} |
815 |
> |
assertTrue(y.isEmpty()); |
816 |
> |
} |
817 |
|
|
818 |
< |
executor.execute(new Runnable() { |
819 |
< |
public void run() { |
820 |
< |
try { |
821 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
822 |
< |
q.put(new Integer(1)); |
823 |
< |
} |
824 |
< |
catch (InterruptedException e) { |
825 |
< |
fail("should not be interrupted"); |
826 |
< |
} |
827 |
< |
} |
828 |
< |
}); |
829 |
< |
|
830 |
< |
executor.shutdown(); |
818 |
> |
/** |
819 |
> |
* drainTo(c) empties queue into another collection c |
820 |
> |
*/ |
821 |
> |
public void testDrainTo() { |
822 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
823 |
> |
ArrayList l = new ArrayList(); |
824 |
> |
q.drainTo(l); |
825 |
> |
assertEquals(0, q.size()); |
826 |
> |
assertEquals(SIZE, l.size()); |
827 |
> |
for (int i = 0; i < SIZE; ++i) |
828 |
> |
assertEquals(l.get(i), new Integer(i)); |
829 |
> |
q.add(zero); |
830 |
> |
q.add(one); |
831 |
> |
assertFalse(q.isEmpty()); |
832 |
> |
assertTrue(q.contains(zero)); |
833 |
> |
assertTrue(q.contains(one)); |
834 |
> |
l.clear(); |
835 |
> |
q.drainTo(l); |
836 |
> |
assertEquals(0, q.size()); |
837 |
> |
assertEquals(2, l.size()); |
838 |
> |
for (int i = 0; i < 2; ++i) |
839 |
> |
assertEquals(l.get(i), new Integer(i)); |
840 |
> |
} |
841 |
|
|
842 |
+ |
/** |
843 |
+ |
* drainTo empties full queue, unblocking a waiting put. |
844 |
+ |
*/ |
845 |
+ |
public void testDrainToWithActivePut() throws InterruptedException { |
846 |
+ |
final LinkedBlockingQueue q = populatedQueue(SIZE); |
847 |
+ |
Thread t = new Thread(new CheckedRunnable() { |
848 |
+ |
public void realRun() throws InterruptedException { |
849 |
+ |
q.put(new Integer(SIZE + 1)); |
850 |
+ |
}}); |
851 |
+ |
|
852 |
+ |
t.start(); |
853 |
+ |
ArrayList l = new ArrayList(); |
854 |
+ |
q.drainTo(l); |
855 |
+ |
assertTrue(l.size() >= SIZE); |
856 |
+ |
for (int i = 0; i < SIZE; ++i) |
857 |
+ |
assertEquals(l.get(i), new Integer(i)); |
858 |
+ |
t.join(); |
859 |
+ |
assertTrue(q.size() + l.size() >= SIZE); |
860 |
|
} |
861 |
|
|
862 |
< |
public void testSerialization() { |
863 |
< |
LinkedBlockingQueue q = fullQueue(N); |
864 |
< |
|
865 |
< |
try { |
866 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
867 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
868 |
< |
out.writeObject(q); |
869 |
< |
out.close(); |
870 |
< |
|
871 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
872 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
873 |
< |
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
874 |
< |
assertEquals(q.size(), r.size()); |
875 |
< |
while (!q.isEmpty()) |
876 |
< |
assertEquals(q.remove(), r.remove()); |
877 |
< |
} catch(Exception e){ |
878 |
< |
e.printStackTrace(); |
879 |
< |
fail("unexpected exception"); |
862 |
> |
/** |
863 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
864 |
> |
*/ |
865 |
> |
public void testDrainToN() { |
866 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
867 |
> |
for (int i = 0; i < SIZE + 2; ++i) { |
868 |
> |
for (int j = 0; j < SIZE; j++) |
869 |
> |
assertTrue(q.offer(new Integer(j))); |
870 |
> |
ArrayList l = new ArrayList(); |
871 |
> |
q.drainTo(l, i); |
872 |
> |
int k = (i < SIZE) ? i : SIZE; |
873 |
> |
assertEquals(k, l.size()); |
874 |
> |
assertEquals(SIZE - k, q.size()); |
875 |
> |
for (int j = 0; j < k; ++j) |
876 |
> |
assertEquals(l.get(j), new Integer(j)); |
877 |
> |
do {} while (q.poll() != null); |
878 |
> |
} |
879 |
> |
} |
880 |
> |
|
881 |
> |
/** |
882 |
> |
* remove(null), contains(null) always return false |
883 |
> |
*/ |
884 |
> |
public void testNeverContainsNull() { |
885 |
> |
Collection<?>[] qs = { |
886 |
> |
new LinkedBlockingQueue<Object>(), |
887 |
> |
populatedQueue(2), |
888 |
> |
}; |
889 |
> |
|
890 |
> |
for (Collection<?> q : qs) { |
891 |
> |
assertFalse(q.contains(null)); |
892 |
> |
assertFalse(q.remove(null)); |
893 |
|
} |
894 |
|
} |
895 |
|
|