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