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