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