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