1 |
– |
|
1 |
|
/* |
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/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include John Vint |
6 |
|
*/ |
7 |
|
|
8 |
< |
import java.io.BufferedInputStream; |
9 |
< |
import java.io.BufferedOutputStream; |
11 |
< |
import java.io.ByteArrayInputStream; |
12 |
< |
import java.io.ByteArrayOutputStream; |
13 |
< |
import java.io.ObjectInputStream; |
14 |
< |
import java.io.ObjectOutputStream; |
8 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
9 |
> |
|
10 |
|
import java.util.ArrayList; |
11 |
|
import java.util.Arrays; |
12 |
< |
import java.util.ConcurrentModificationException; |
12 |
> |
import java.util.Collection; |
13 |
|
import java.util.Iterator; |
14 |
+ |
import java.util.List; |
15 |
|
import java.util.NoSuchElementException; |
16 |
< |
import java.util.concurrent.*; |
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.LinkedTransferQueue; |
22 |
> |
|
23 |
|
import junit.framework.Test; |
22 |
– |
import junit.framework.TestSuite; |
24 |
|
|
25 |
+ |
@SuppressWarnings({"unchecked", "rawtypes"}) |
26 |
|
public class LinkedTransferQueueTest extends JSR166TestCase { |
27 |
+ |
static class Implementation implements CollectionImplementation { |
28 |
+ |
public Class<?> klazz() { return LinkedTransferQueue.class; } |
29 |
+ |
public Collection emptyCollection() { return new LinkedTransferQueue(); } |
30 |
+ |
public Object makeElement(int i) { return i; } |
31 |
+ |
public boolean isConcurrent() { return true; } |
32 |
+ |
public boolean permitsNulls() { return false; } |
33 |
+ |
} |
34 |
+ |
|
35 |
+ |
public static class Generic extends BlockingQueueTest { |
36 |
+ |
protected BlockingQueue emptyCollection() { |
37 |
+ |
return new LinkedTransferQueue(); |
38 |
+ |
} |
39 |
+ |
} |
40 |
|
|
41 |
|
public static void main(String[] args) { |
42 |
< |
junit.textui.TestRunner.run(suite()); |
42 |
> |
main(suite(), args); |
43 |
|
} |
44 |
|
|
45 |
|
public static Test suite() { |
46 |
< |
return new TestSuite(LinkedTransferQueueTest.class); |
46 |
> |
return newTestSuite(LinkedTransferQueueTest.class, |
47 |
> |
new Generic().testSuite(), |
48 |
> |
CollectionTest.testSuite(new Implementation())); |
49 |
|
} |
50 |
|
|
51 |
< |
/* |
52 |
< |
*Constructor builds new queue with size being zero and empty being true |
51 |
> |
/** |
52 |
> |
* Constructor builds new queue with size being zero and empty |
53 |
> |
* being true |
54 |
|
*/ |
55 |
|
public void testConstructor1() { |
56 |
|
assertEquals(0, new LinkedTransferQueue().size()); |
57 |
|
assertTrue(new LinkedTransferQueue().isEmpty()); |
58 |
|
} |
59 |
|
|
60 |
< |
/* |
61 |
< |
* Initizialing constructor with null collection throws NPE |
60 |
> |
/** |
61 |
> |
* Initializing constructor with null collection throws |
62 |
> |
* NullPointerException |
63 |
|
*/ |
64 |
|
public void testConstructor2() { |
65 |
|
try { |
66 |
|
new LinkedTransferQueue(null); |
67 |
|
shouldThrow(); |
68 |
< |
} catch (NullPointerException success) { |
50 |
< |
} |
68 |
> |
} catch (NullPointerException success) {} |
69 |
|
} |
70 |
|
|
71 |
|
/** |
72 |
< |
* Initializing from Collection of null elements throws NPE |
72 |
> |
* Initializing from Collection of null elements throws |
73 |
> |
* NullPointerException |
74 |
|
*/ |
75 |
|
public void testConstructor3() { |
76 |
+ |
Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
77 |
|
try { |
78 |
< |
Integer[] ints = new Integer[SIZE]; |
59 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); |
78 |
> |
new LinkedTransferQueue(elements); |
79 |
|
shouldThrow(); |
80 |
< |
} catch (NullPointerException success) { |
62 |
< |
} |
80 |
> |
} catch (NullPointerException success) {} |
81 |
|
} |
82 |
< |
/* |
82 |
> |
|
83 |
> |
/** |
84 |
|
* Initializing constructor with a collection containing some null elements |
85 |
< |
* throws NPE |
85 |
> |
* throws NullPointerException |
86 |
|
*/ |
68 |
– |
|
87 |
|
public void testConstructor4() { |
88 |
+ |
Integer[] ints = new Integer[SIZE]; |
89 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
90 |
+ |
ints[i] = i; |
91 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
92 |
|
try { |
93 |
< |
Integer[] ints = new Integer[SIZE]; |
72 |
< |
for (int i = 0; i < SIZE - 1; ++i) { |
73 |
< |
ints[i] = new Integer(i); |
74 |
< |
} |
75 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); |
93 |
> |
new LinkedTransferQueue(elements); |
94 |
|
shouldThrow(); |
95 |
< |
} catch (NullPointerException success) { |
78 |
< |
} |
95 |
> |
} catch (NullPointerException success) {} |
96 |
|
} |
97 |
|
|
98 |
< |
/* |
98 |
> |
/** |
99 |
|
* Queue contains all elements of the collection it is initialized by |
100 |
|
*/ |
101 |
|
public void testConstructor5() { |
102 |
< |
try { |
103 |
< |
Integer[] ints = new Integer[SIZE]; |
104 |
< |
for (int i = 0; i < SIZE; ++i) { |
105 |
< |
ints[i] = new Integer(i); |
106 |
< |
} |
107 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); |
108 |
< |
for (int i = 0; i < SIZE; ++i) { |
109 |
< |
assertEquals(ints[i], q.poll()); |
110 |
< |
} |
111 |
< |
} finally { |
102 |
> |
Integer[] ints = new Integer[SIZE]; |
103 |
> |
for (int i = 0; i < SIZE; ++i) { |
104 |
> |
ints[i] = i; |
105 |
> |
} |
106 |
> |
List intList = Arrays.asList(ints); |
107 |
> |
LinkedTransferQueue q |
108 |
> |
= new LinkedTransferQueue(intList); |
109 |
> |
assertEquals(q.size(), intList.size()); |
110 |
> |
assertEquals(q.toString(), intList.toString()); |
111 |
> |
assertTrue(Arrays.equals(q.toArray(), |
112 |
> |
intList.toArray())); |
113 |
> |
assertTrue(Arrays.equals(q.toArray(new Object[0]), |
114 |
> |
intList.toArray(new Object[0]))); |
115 |
> |
assertTrue(Arrays.equals(q.toArray(new Object[SIZE]), |
116 |
> |
intList.toArray(new Object[SIZE]))); |
117 |
> |
for (int i = 0; i < SIZE; ++i) { |
118 |
> |
assertEquals(ints[i], q.poll()); |
119 |
|
} |
120 |
|
} |
121 |
|
|
122 |
|
/** |
123 |
< |
* Remaining capacity never decrease nor increase on add or remove |
123 |
> |
* remainingCapacity() always returns Integer.MAX_VALUE |
124 |
|
*/ |
125 |
|
public void testRemainingCapacity() { |
126 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
103 |
< |
int remainingCapacity = q.remainingCapacity(); |
126 |
> |
BlockingQueue q = populatedQueue(SIZE); |
127 |
|
for (int i = 0; i < SIZE; ++i) { |
128 |
< |
assertEquals(remainingCapacity, q.remainingCapacity()); |
128 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
129 |
|
assertEquals(SIZE - i, q.size()); |
130 |
< |
q.remove(); |
130 |
> |
assertEquals(i, q.remove()); |
131 |
|
} |
132 |
|
for (int i = 0; i < SIZE; ++i) { |
133 |
< |
assertEquals(remainingCapacity, q.remainingCapacity()); |
133 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
134 |
|
assertEquals(i, q.size()); |
135 |
< |
q.add(new Integer(i)); |
113 |
< |
} |
114 |
< |
} |
115 |
< |
|
116 |
< |
/** |
117 |
< |
* offer(null) throws NPE |
118 |
< |
*/ |
119 |
< |
public void testOfferNull() { |
120 |
< |
try { |
121 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
122 |
< |
q.offer(null); |
123 |
< |
shouldThrow(); |
124 |
< |
} catch (NullPointerException success) { |
125 |
< |
} |
126 |
< |
} |
127 |
< |
|
128 |
< |
/** |
129 |
< |
* add(null) throws NPE |
130 |
< |
*/ |
131 |
< |
public void testAddNull() { |
132 |
< |
try { |
133 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
134 |
< |
q.add(null); |
135 |
< |
shouldThrow(); |
136 |
< |
} catch (NullPointerException success) { |
137 |
< |
} |
138 |
< |
} |
139 |
< |
|
140 |
< |
/** |
141 |
< |
* addAll(null) throws NPE |
142 |
< |
*/ |
143 |
< |
public void testAddAll1() { |
144 |
< |
try { |
145 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
146 |
< |
q.addAll(null); |
147 |
< |
shouldThrow(); |
148 |
< |
} catch (NullPointerException success) { |
135 |
> |
assertTrue(q.add(i)); |
136 |
|
} |
137 |
|
} |
138 |
|
|
139 |
|
/** |
140 |
< |
* addAll(this) throws IAE |
140 |
> |
* addAll(this) throws IllegalArgumentException |
141 |
|
*/ |
142 |
|
public void testAddAllSelf() { |
143 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
144 |
|
try { |
157 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
145 |
|
q.addAll(q); |
146 |
|
shouldThrow(); |
147 |
< |
} catch (IllegalArgumentException success) { |
161 |
< |
} |
147 |
> |
} catch (IllegalArgumentException success) {} |
148 |
|
} |
149 |
|
|
150 |
|
/** |
151 |
< |
* addAll of a collection with null elements throws NPE |
152 |
< |
*/ |
167 |
< |
public void testAddAll2() { |
168 |
< |
try { |
169 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
170 |
< |
Integer[] ints = new Integer[SIZE]; |
171 |
< |
q.addAll(Arrays.asList(ints)); |
172 |
< |
shouldThrow(); |
173 |
< |
} catch (NullPointerException success) { |
174 |
< |
} |
175 |
< |
} |
176 |
< |
|
177 |
< |
/** |
178 |
< |
* addAll of a collection with any null elements throws NPE after |
179 |
< |
* possibly adding some elements |
151 |
> |
* addAll of a collection with any null elements throws |
152 |
> |
* NullPointerException after possibly adding some elements |
153 |
|
*/ |
154 |
|
public void testAddAll3() { |
155 |
+ |
LinkedTransferQueue q = new LinkedTransferQueue(); |
156 |
+ |
Integer[] ints = new Integer[SIZE]; |
157 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
158 |
+ |
ints[i] = i; |
159 |
|
try { |
183 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
184 |
– |
Integer[] ints = new Integer[SIZE]; |
185 |
– |
for (int i = 0; i < SIZE - 1; ++i) { |
186 |
– |
ints[i] = new Integer(i); |
187 |
– |
} |
160 |
|
q.addAll(Arrays.asList(ints)); |
161 |
|
shouldThrow(); |
162 |
< |
} catch (NullPointerException success) { |
191 |
< |
} |
162 |
> |
} catch (NullPointerException success) {} |
163 |
|
} |
164 |
|
|
165 |
|
/** |
166 |
|
* Queue contains all elements, in traversal order, of successful addAll |
167 |
|
*/ |
168 |
|
public void testAddAll5() { |
169 |
< |
try { |
170 |
< |
Integer[] empty = new Integer[0]; |
171 |
< |
Integer[] ints = new Integer[SIZE]; |
172 |
< |
for (int i = 0; i < SIZE; ++i) { |
202 |
< |
ints[i] = new Integer(i); |
203 |
< |
} |
204 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
205 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
206 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
207 |
< |
for (int i = 0; i < SIZE; ++i) { |
208 |
< |
assertEquals(ints[i], q.poll()); |
209 |
< |
} |
210 |
< |
} finally { |
169 |
> |
Integer[] empty = new Integer[0]; |
170 |
> |
Integer[] ints = new Integer[SIZE]; |
171 |
> |
for (int i = 0; i < SIZE; ++i) { |
172 |
> |
ints[i] = i; |
173 |
|
} |
174 |
< |
} |
175 |
< |
|
176 |
< |
/** |
177 |
< |
* put(null) throws NPE |
178 |
< |
*/ |
217 |
< |
public void testPutNull() { |
218 |
< |
try { |
219 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
220 |
< |
q.put(null); |
221 |
< |
shouldThrow(); |
222 |
< |
} catch (NullPointerException success) { |
223 |
< |
} catch (Exception ie) { |
224 |
< |
unexpectedException(); |
174 |
> |
LinkedTransferQueue q = new LinkedTransferQueue(); |
175 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
176 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
177 |
> |
for (int i = 0; i < SIZE; ++i) { |
178 |
> |
assertEquals(ints[i], q.poll()); |
179 |
|
} |
180 |
|
} |
181 |
|
|
183 |
|
* all elements successfully put are contained |
184 |
|
*/ |
185 |
|
public void testPut() { |
186 |
< |
try { |
187 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
188 |
< |
for (int i = 0; i < SIZE; ++i) { |
189 |
< |
Integer I = new Integer(i); |
190 |
< |
q.put(I); |
237 |
< |
assertTrue(q.contains(I)); |
238 |
< |
} |
239 |
< |
} catch (Exception ie) { |
240 |
< |
unexpectedException(); |
186 |
> |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
187 |
> |
for (int i = 0; i < SIZE; ++i) { |
188 |
> |
assertEquals(i, q.size()); |
189 |
> |
q.put(i); |
190 |
> |
assertTrue(q.contains(i)); |
191 |
|
} |
192 |
|
} |
193 |
|
|
194 |
|
/** |
195 |
|
* take retrieves elements in FIFO order |
196 |
|
*/ |
197 |
< |
public void testTake() { |
198 |
< |
try { |
199 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
200 |
< |
for (int i = 0; i < SIZE; ++i) { |
251 |
< |
assertEquals(i, ((Integer) q.take()).intValue()); |
252 |
< |
} |
253 |
< |
} catch (InterruptedException e) { |
254 |
< |
unexpectedException(); |
197 |
> |
public void testTake() throws InterruptedException { |
198 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
199 |
> |
for (int i = 0; i < SIZE; ++i) { |
200 |
> |
assertEquals(i, (int) q.take()); |
201 |
|
} |
202 |
|
} |
203 |
|
|
204 |
|
/** |
205 |
< |
* take blocks interruptibly when empty |
205 |
> |
* take removes existing elements until empty, then blocks interruptibly |
206 |
|
*/ |
207 |
< |
public void testTakeFromEmpty() { |
208 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
209 |
< |
Thread t = new Thread(new Runnable() { |
207 |
> |
public void testBlockingTake() throws InterruptedException { |
208 |
> |
final BlockingQueue q = populatedQueue(SIZE); |
209 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
210 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
211 |
> |
public void realRun() throws InterruptedException { |
212 |
> |
for (int i = 0; i < SIZE; ++i) { |
213 |
> |
assertEquals(i, q.take()); |
214 |
> |
} |
215 |
|
|
216 |
< |
public void run() { |
216 |
> |
Thread.currentThread().interrupt(); |
217 |
|
try { |
218 |
|
q.take(); |
219 |
< |
threadShouldThrow(); |
220 |
< |
} catch (InterruptedException success) { |
221 |
< |
} |
271 |
< |
} |
272 |
< |
}); |
273 |
< |
try { |
274 |
< |
t.start(); |
275 |
< |
Thread.sleep(SHORT_DELAY_MS); |
276 |
< |
t.interrupt(); |
277 |
< |
t.join(); |
278 |
< |
} catch (Exception e) { |
279 |
< |
unexpectedException(); |
280 |
< |
} |
281 |
< |
} |
282 |
< |
/* |
283 |
< |
* Take removes existing elements until empty, then blocks interruptibly |
284 |
< |
*/ |
285 |
< |
|
286 |
< |
public void testBlockingTake() { |
287 |
< |
Thread t = new Thread(new Runnable() { |
219 |
> |
shouldThrow(); |
220 |
> |
} catch (InterruptedException success) {} |
221 |
> |
assertFalse(Thread.interrupted()); |
222 |
|
|
223 |
< |
public void run() { |
223 |
> |
pleaseInterrupt.countDown(); |
224 |
|
try { |
291 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
292 |
– |
for (int i = 0; i < SIZE; ++i) { |
293 |
– |
assertEquals(i, ((Integer) q.take()).intValue()); |
294 |
– |
} |
225 |
|
q.take(); |
226 |
< |
threadShouldThrow(); |
227 |
< |
} catch (InterruptedException success) { |
228 |
< |
} |
229 |
< |
} |
230 |
< |
}); |
231 |
< |
t.start(); |
232 |
< |
try { |
233 |
< |
Thread.sleep(SHORT_DELAY_MS); |
234 |
< |
t.interrupt(); |
305 |
< |
t.join(); |
306 |
< |
} catch (InterruptedException ie) { |
307 |
< |
unexpectedException(); |
308 |
< |
} |
226 |
> |
shouldThrow(); |
227 |
> |
} catch (InterruptedException success) {} |
228 |
> |
assertFalse(Thread.interrupted()); |
229 |
> |
}}); |
230 |
> |
|
231 |
> |
await(pleaseInterrupt); |
232 |
> |
assertThreadStaysAlive(t); |
233 |
> |
t.interrupt(); |
234 |
> |
awaitTermination(t); |
235 |
|
} |
236 |
|
|
237 |
|
/** |
238 |
|
* poll succeeds unless empty |
239 |
|
*/ |
240 |
< |
public void testPoll() { |
241 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
240 |
> |
public void testPoll() throws InterruptedException { |
241 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
242 |
|
for (int i = 0; i < SIZE; ++i) { |
243 |
< |
assertEquals(i, ((Integer) q.poll()).intValue()); |
243 |
> |
assertEquals(i, (int) q.poll()); |
244 |
|
} |
245 |
|
assertNull(q.poll()); |
246 |
+ |
checkEmpty(q); |
247 |
|
} |
248 |
|
|
249 |
|
/** |
250 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
250 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
251 |
|
*/ |
252 |
< |
public void testTimedPoll0() { |
253 |
< |
try { |
254 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
255 |
< |
for (int i = 0; i < SIZE; ++i) { |
329 |
< |
assertEquals(i, ((Integer) q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
330 |
< |
} |
331 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
332 |
< |
} catch (InterruptedException e) { |
333 |
< |
unexpectedException(); |
252 |
> |
public void testTimedPoll0() throws InterruptedException { |
253 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
254 |
> |
for (int i = 0; i < SIZE; ++i) { |
255 |
> |
assertEquals(i, (int) q.poll(0, MILLISECONDS)); |
256 |
|
} |
257 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
258 |
+ |
checkEmpty(q); |
259 |
|
} |
260 |
|
|
261 |
|
/** |
262 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
262 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
263 |
|
*/ |
264 |
< |
public void testTimedPoll() { |
265 |
< |
try { |
266 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
267 |
< |
for (int i = 0; i < SIZE; ++i) { |
268 |
< |
assertEquals(i, ((Integer) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
269 |
< |
} |
270 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
271 |
< |
} catch (InterruptedException e) { |
272 |
< |
unexpectedException(); |
273 |
< |
} |
264 |
> |
public void testTimedPoll() throws InterruptedException { |
265 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
266 |
> |
for (int i = 0; i < SIZE; ++i) { |
267 |
> |
long startTime = System.nanoTime(); |
268 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
269 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
270 |
> |
} |
271 |
> |
long startTime = System.nanoTime(); |
272 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
273 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
274 |
> |
checkEmpty(q); |
275 |
|
} |
276 |
|
|
277 |
|
/** |
278 |
|
* Interrupted timed poll throws InterruptedException instead of |
279 |
|
* returning timeout status |
280 |
|
*/ |
281 |
< |
public void testInterruptedTimedPoll() { |
282 |
< |
Thread t = new Thread(new Runnable() { |
283 |
< |
|
284 |
< |
public void run() { |
281 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
282 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
283 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
284 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
285 |
> |
public void realRun() throws InterruptedException { |
286 |
> |
long startTime = System.nanoTime(); |
287 |
> |
for (int i = 0; i < SIZE; ++i) { |
288 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
289 |
> |
} |
290 |
> |
aboutToWait.countDown(); |
291 |
|
try { |
292 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
293 |
< |
for (int i = 0; i < SIZE; ++i) { |
363 |
< |
threadAssertEquals(i, ((Integer) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
364 |
< |
} |
365 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
292 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
293 |
> |
shouldThrow(); |
294 |
|
} catch (InterruptedException success) { |
295 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
296 |
|
} |
297 |
< |
} |
298 |
< |
}); |
299 |
< |
t.start(); |
300 |
< |
try { |
301 |
< |
Thread.sleep(SHORT_DELAY_MS); |
302 |
< |
t.interrupt(); |
303 |
< |
t.join(); |
375 |
< |
} catch (InterruptedException ie) { |
376 |
< |
unexpectedException(); |
377 |
< |
} |
297 |
> |
}}); |
298 |
> |
|
299 |
> |
aboutToWait.await(); |
300 |
> |
waitForThreadToEnterWaitState(t, LONG_DELAY_MS); |
301 |
> |
t.interrupt(); |
302 |
> |
awaitTermination(t); |
303 |
> |
checkEmpty(q); |
304 |
|
} |
305 |
|
|
306 |
|
/** |
307 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
308 |
< |
* on interruption throws |
309 |
< |
*/ |
310 |
< |
public void testTimedPollWithOffer() { |
311 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
312 |
< |
Thread t = new Thread(new Runnable() { |
313 |
< |
|
314 |
< |
public void run() { |
315 |
< |
try { |
316 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
317 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
318 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
393 |
< |
threadShouldThrow(); |
394 |
< |
} catch (InterruptedException success) { |
307 |
> |
* timed poll after thread interrupted throws InterruptedException |
308 |
> |
* instead of returning timeout status |
309 |
> |
*/ |
310 |
> |
public void testTimedPollAfterInterrupt() throws InterruptedException { |
311 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
312 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
313 |
> |
public void realRun() throws InterruptedException { |
314 |
> |
Thread.currentThread().interrupt(); |
315 |
> |
for (int i = 0; i < SIZE; ++i) { |
316 |
> |
long t0 = System.nanoTime(); |
317 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
318 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
319 |
|
} |
320 |
< |
} |
321 |
< |
}); |
322 |
< |
try { |
323 |
< |
t.start(); |
324 |
< |
Thread.sleep(SMALL_DELAY_MS); |
325 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
326 |
< |
t.interrupt(); |
327 |
< |
t.join(); |
404 |
< |
} catch (Exception e) { |
405 |
< |
unexpectedException(); |
406 |
< |
} |
320 |
> |
try { |
321 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
322 |
> |
shouldThrow(); |
323 |
> |
} catch (InterruptedException success) {} |
324 |
> |
}}); |
325 |
> |
|
326 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
327 |
> |
checkEmpty(q); |
328 |
|
} |
329 |
|
|
330 |
|
/** |
331 |
|
* peek returns next element, or null if empty |
332 |
|
*/ |
333 |
< |
public void testPeek() { |
334 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
333 |
> |
public void testPeek() throws InterruptedException { |
334 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
335 |
|
for (int i = 0; i < SIZE; ++i) { |
336 |
< |
assertEquals(i, ((Integer) q.peek()).intValue()); |
337 |
< |
q.poll(); |
336 |
> |
assertEquals(i, (int) q.peek()); |
337 |
> |
assertEquals(i, (int) q.poll()); |
338 |
|
assertTrue(q.peek() == null || |
339 |
< |
i != ((Integer) q.peek()).intValue()); |
339 |
> |
i != (int) q.peek()); |
340 |
|
} |
341 |
|
assertNull(q.peek()); |
342 |
+ |
checkEmpty(q); |
343 |
|
} |
344 |
|
|
345 |
|
/** |
346 |
< |
* element returns next element, or throws NSEE if empty |
346 |
> |
* element returns next element, or throws NoSuchElementException if empty |
347 |
|
*/ |
348 |
< |
public void testElement() { |
349 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
348 |
> |
public void testElement() throws InterruptedException { |
349 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
350 |
|
for (int i = 0; i < SIZE; ++i) { |
351 |
< |
assertEquals(i, ((Integer) q.element()).intValue()); |
352 |
< |
q.poll(); |
351 |
> |
assertEquals(i, (int) q.element()); |
352 |
> |
assertEquals(i, (int) q.poll()); |
353 |
|
} |
354 |
|
try { |
355 |
|
q.element(); |
356 |
|
shouldThrow(); |
357 |
< |
} catch (NoSuchElementException success) { |
358 |
< |
} |
357 |
> |
} catch (NoSuchElementException success) {} |
358 |
> |
checkEmpty(q); |
359 |
|
} |
360 |
|
|
361 |
|
/** |
362 |
< |
* remove removes next element, or throws NSEE if empty |
362 |
> |
* remove removes next element, or throws NoSuchElementException if empty |
363 |
|
*/ |
364 |
< |
public void testRemove() { |
365 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
364 |
> |
public void testRemove() throws InterruptedException { |
365 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
366 |
|
for (int i = 0; i < SIZE; ++i) { |
367 |
< |
assertEquals(i, ((Integer) q.remove()).intValue()); |
367 |
> |
assertEquals(i, (int) q.remove()); |
368 |
|
} |
369 |
|
try { |
370 |
|
q.remove(); |
371 |
|
shouldThrow(); |
372 |
< |
} catch (NoSuchElementException success) { |
373 |
< |
} |
452 |
< |
} |
453 |
< |
|
454 |
< |
/** |
455 |
< |
* remove(x) removes x and returns true if present |
456 |
< |
*/ |
457 |
< |
public void testRemoveElement() { |
458 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
459 |
< |
for (int i = 1; i < SIZE; i += 2) { |
460 |
< |
assertTrue(q.remove(new Integer(i))); |
461 |
< |
} |
462 |
< |
for (int i = 0; i < SIZE; i += 2) { |
463 |
< |
assertTrue(q.remove(new Integer(i))); |
464 |
< |
assertFalse(q.remove(new Integer(i + 1))); |
465 |
< |
} |
466 |
< |
assertTrue(q.isEmpty()); |
372 |
> |
} catch (NoSuchElementException success) {} |
373 |
> |
checkEmpty(q); |
374 |
|
} |
375 |
|
|
376 |
|
/** |
377 |
|
* An add following remove(x) succeeds |
378 |
|
*/ |
379 |
< |
public void testRemoveElementAndAdd() { |
380 |
< |
try { |
381 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
382 |
< |
assertTrue(q.add(new Integer(1))); |
383 |
< |
assertTrue(q.add(new Integer(2))); |
384 |
< |
assertTrue(q.remove(new Integer(1))); |
385 |
< |
assertTrue(q.remove(new Integer(2))); |
386 |
< |
assertTrue(q.add(new Integer(3))); |
480 |
< |
assertTrue(q.take() != null); |
481 |
< |
} catch (Exception e) { |
482 |
< |
unexpectedException(); |
483 |
< |
} |
379 |
> |
public void testRemoveElementAndAdd() throws InterruptedException { |
380 |
> |
LinkedTransferQueue q = new LinkedTransferQueue(); |
381 |
> |
assertTrue(q.add(one)); |
382 |
> |
assertTrue(q.add(two)); |
383 |
> |
assertTrue(q.remove(one)); |
384 |
> |
assertTrue(q.remove(two)); |
385 |
> |
assertTrue(q.add(three)); |
386 |
> |
assertSame(q.take(), three); |
387 |
|
} |
388 |
|
|
389 |
|
/** |
390 |
|
* contains(x) reports true when elements added but not yet removed |
391 |
|
*/ |
392 |
|
public void testContains() { |
393 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
393 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
394 |
|
for (int i = 0; i < SIZE; ++i) { |
395 |
< |
assertTrue(q.contains(new Integer(i))); |
396 |
< |
q.poll(); |
397 |
< |
assertFalse(q.contains(new Integer(i))); |
395 |
> |
assertTrue(q.contains(i)); |
396 |
> |
assertEquals(i, (int) q.poll()); |
397 |
> |
assertFalse(q.contains(i)); |
398 |
|
} |
399 |
|
} |
400 |
|
|
401 |
|
/** |
402 |
|
* clear removes all elements |
403 |
|
*/ |
404 |
< |
public void testClear() { |
404 |
> |
public void testClear() throws InterruptedException { |
405 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
503 |
– |
int remainingCapacity = q.remainingCapacity(); |
406 |
|
q.clear(); |
407 |
< |
assertTrue(q.isEmpty()); |
408 |
< |
assertEquals(0, q.size()); |
507 |
< |
assertEquals(remainingCapacity, q.remainingCapacity()); |
407 |
> |
checkEmpty(q); |
408 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
409 |
|
q.add(one); |
410 |
|
assertFalse(q.isEmpty()); |
411 |
+ |
assertEquals(1, q.size()); |
412 |
|
assertTrue(q.contains(one)); |
413 |
|
q.clear(); |
414 |
< |
assertTrue(q.isEmpty()); |
414 |
> |
checkEmpty(q); |
415 |
|
} |
416 |
|
|
417 |
|
/** |
418 |
|
* containsAll(c) is true when c contains a subset of elements |
419 |
|
*/ |
420 |
|
public void testContainsAll() { |
421 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
422 |
< |
LinkedTransferQueue p = new LinkedTransferQueue(); |
421 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
422 |
> |
LinkedTransferQueue<Integer> p = new LinkedTransferQueue<Integer>(); |
423 |
|
for (int i = 0; i < SIZE; ++i) { |
424 |
|
assertTrue(q.containsAll(p)); |
425 |
|
assertFalse(p.containsAll(q)); |
426 |
< |
p.add(new Integer(i)); |
426 |
> |
p.add(i); |
427 |
|
} |
428 |
|
assertTrue(p.containsAll(q)); |
429 |
|
} |
430 |
|
|
431 |
|
/** |
432 |
< |
* retainAll(c) retains only those elements of c and reports true if changed |
432 |
> |
* retainAll(c) retains only those elements of c and reports true |
433 |
> |
* if changed |
434 |
|
*/ |
435 |
|
public void testRetainAll() { |
436 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
449 |
|
} |
450 |
|
|
451 |
|
/** |
452 |
< |
* removeAll(c) removes only those elements of c and reports true if changed |
452 |
> |
* removeAll(c) removes only those elements of c and reports true |
453 |
> |
* if changed |
454 |
|
*/ |
455 |
|
public void testRemoveAll() { |
456 |
|
for (int i = 1; i < SIZE; ++i) { |
459 |
|
assertTrue(q.removeAll(p)); |
460 |
|
assertEquals(SIZE - i, q.size()); |
461 |
|
for (int j = 0; j < i; ++j) { |
462 |
< |
Integer I = (Integer) (p.remove()); |
559 |
< |
assertFalse(q.contains(I)); |
462 |
> |
assertFalse(q.contains(p.remove())); |
463 |
|
} |
464 |
|
} |
465 |
|
} |
466 |
|
|
467 |
|
/** |
468 |
< |
* toArray contains all elements |
468 |
> |
* toArray() contains all elements in FIFO order |
469 |
|
*/ |
470 |
|
public void testToArray() { |
471 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
472 |
|
Object[] o = q.toArray(); |
473 |
< |
try { |
474 |
< |
for (int i = 0; i < o.length; i++) { |
572 |
< |
assertEquals(o[i], q.take()); |
573 |
< |
} |
574 |
< |
} catch (InterruptedException e) { |
575 |
< |
unexpectedException(); |
473 |
> |
for (int i = 0; i < o.length; i++) { |
474 |
> |
assertSame(o[i], q.poll()); |
475 |
|
} |
476 |
|
} |
477 |
|
|
478 |
|
/** |
479 |
< |
* toArray(a) contains all elements |
479 |
> |
* toArray(a) contains all elements in FIFO order |
480 |
|
*/ |
481 |
|
public void testToArray2() { |
482 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
482 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
483 |
|
Integer[] ints = new Integer[SIZE]; |
484 |
< |
ints = (Integer[]) q.toArray(ints); |
485 |
< |
try { |
486 |
< |
for (int i = 0; i < ints.length; i++) { |
487 |
< |
assertEquals(ints[i], q.take()); |
589 |
< |
} |
590 |
< |
} catch (InterruptedException e) { |
591 |
< |
unexpectedException(); |
484 |
> |
Integer[] array = q.toArray(ints); |
485 |
> |
assertSame(ints, array); |
486 |
> |
for (int i = 0; i < ints.length; i++) { |
487 |
> |
assertSame(ints[i], q.poll()); |
488 |
|
} |
489 |
|
} |
490 |
|
|
491 |
|
/** |
492 |
< |
* toArray(null) throws NPE |
597 |
< |
*/ |
598 |
< |
public void testToArray_BadArg() { |
599 |
< |
try { |
600 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
601 |
< |
Object o[] = q.toArray(null); |
602 |
< |
shouldThrow(); |
603 |
< |
} catch (NullPointerException success) { |
604 |
< |
} |
605 |
< |
} |
606 |
< |
|
607 |
< |
/** |
608 |
< |
* toArray with incompatible array type throws CCE |
492 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
493 |
|
*/ |
494 |
|
public void testToArray1_BadArg() { |
495 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
496 |
|
try { |
497 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
613 |
< |
Object o[] = q.toArray(new String[10]); |
497 |
> |
q.toArray(new String[10]); |
498 |
|
shouldThrow(); |
499 |
< |
} catch (ArrayStoreException success) { |
616 |
< |
} |
499 |
> |
} catch (ArrayStoreException success) {} |
500 |
|
} |
501 |
|
|
502 |
|
/** |
503 |
|
* iterator iterates through all elements |
504 |
|
*/ |
505 |
< |
public void testIterator() { |
505 |
> |
public void testIterator() throws InterruptedException { |
506 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
507 |
|
Iterator it = q.iterator(); |
508 |
< |
try { |
509 |
< |
while (it.hasNext()) { |
510 |
< |
assertEquals(it.next(), q.take()); |
511 |
< |
} |
512 |
< |
} catch (InterruptedException e) { |
513 |
< |
unexpectedException(); |
514 |
< |
} |
508 |
> |
int i; |
509 |
> |
for (i = 0; it.hasNext(); i++) |
510 |
> |
assertTrue(q.contains(it.next())); |
511 |
> |
assertEquals(i, SIZE); |
512 |
> |
assertIteratorExhausted(it); |
513 |
> |
|
514 |
> |
it = q.iterator(); |
515 |
> |
for (i = 0; it.hasNext(); i++) |
516 |
> |
assertEquals(it.next(), q.take()); |
517 |
> |
assertEquals(i, SIZE); |
518 |
> |
assertIteratorExhausted(it); |
519 |
> |
} |
520 |
> |
|
521 |
> |
/** |
522 |
> |
* iterator of empty collection has no elements |
523 |
> |
*/ |
524 |
> |
public void testEmptyIterator() { |
525 |
> |
assertIteratorExhausted(new LinkedTransferQueue().iterator()); |
526 |
|
} |
527 |
|
|
528 |
|
/** |
529 |
< |
* iterator.remove removes current element |
529 |
> |
* iterator.remove() removes current element |
530 |
|
*/ |
531 |
|
public void testIteratorRemove() { |
532 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
539 |
|
it.remove(); |
540 |
|
|
541 |
|
it = q.iterator(); |
542 |
< |
assertEquals(it.next(), one); |
543 |
< |
assertEquals(it.next(), three); |
542 |
> |
assertSame(it.next(), one); |
543 |
> |
assertSame(it.next(), three); |
544 |
|
assertFalse(it.hasNext()); |
545 |
|
} |
546 |
|
|
548 |
|
* iterator ordering is FIFO |
549 |
|
*/ |
550 |
|
public void testIteratorOrdering() { |
551 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
552 |
< |
int remainingCapacity = q.remainingCapacity(); |
551 |
> |
final LinkedTransferQueue<Integer> q |
552 |
> |
= new LinkedTransferQueue<Integer>(); |
553 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
554 |
|
q.add(one); |
555 |
|
q.add(two); |
556 |
|
q.add(three); |
557 |
< |
assertEquals(remainingCapacity, q.remainingCapacity()); |
557 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
558 |
|
int k = 0; |
559 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
560 |
< |
int i = ((Integer) (it.next())).intValue(); |
666 |
< |
assertEquals(++k, i); |
559 |
> |
for (Integer n : q) { |
560 |
> |
assertEquals(++k, (int) n); |
561 |
|
} |
562 |
|
assertEquals(3, k); |
563 |
|
} |
570 |
|
q.add(one); |
571 |
|
q.add(two); |
572 |
|
q.add(three); |
573 |
< |
try { |
574 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
575 |
< |
q.remove(); |
682 |
< |
it.next(); |
683 |
< |
} |
684 |
< |
} catch (ConcurrentModificationException e) { |
685 |
< |
unexpectedException(); |
573 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
574 |
> |
q.remove(); |
575 |
> |
it.next(); |
576 |
|
} |
577 |
|
assertEquals(0, q.size()); |
578 |
|
} |
584 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
585 |
|
String s = q.toString(); |
586 |
|
for (int i = 0; i < SIZE; ++i) { |
587 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
587 |
> |
assertTrue(s.contains(String.valueOf(i))); |
588 |
|
} |
589 |
|
} |
590 |
|
|
593 |
|
*/ |
594 |
|
public void testOfferInExecutor() { |
595 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
596 |
< |
q.add(one); |
597 |
< |
q.add(two); |
598 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
709 |
< |
executor.execute(new Runnable() { |
710 |
< |
|
711 |
< |
public void run() { |
712 |
< |
try { |
713 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
714 |
< |
} catch (Exception e) { |
715 |
< |
threadUnexpectedException(); |
716 |
< |
} |
717 |
< |
} |
718 |
< |
}); |
596 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
597 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
598 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
599 |
|
|
600 |
< |
executor.execute(new Runnable() { |
600 |
> |
executor.execute(new CheckedRunnable() { |
601 |
> |
public void realRun() throws InterruptedException { |
602 |
> |
threadsStarted.await(); |
603 |
> |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
604 |
> |
}}); |
605 |
|
|
606 |
< |
public void run() { |
607 |
< |
try { |
608 |
< |
Thread.sleep(SMALL_DELAY_MS); |
609 |
< |
threadAssertEquals(one, q.take()); |
610 |
< |
} catch (InterruptedException e) { |
611 |
< |
threadUnexpectedException(); |
612 |
< |
} |
729 |
< |
} |
730 |
< |
}); |
731 |
< |
|
732 |
< |
joinPool(executor); |
606 |
> |
executor.execute(new CheckedRunnable() { |
607 |
> |
public void realRun() throws InterruptedException { |
608 |
> |
threadsStarted.await(); |
609 |
> |
assertSame(one, q.take()); |
610 |
> |
checkEmpty(q); |
611 |
> |
}}); |
612 |
> |
} |
613 |
|
} |
614 |
|
|
615 |
|
/** |
616 |
< |
* poll retrieves elements across Executor threads |
616 |
> |
* timed poll retrieves elements across Executor threads |
617 |
|
*/ |
618 |
|
public void testPollInExecutor() { |
619 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
620 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
621 |
< |
executor.execute(new Runnable() { |
622 |
< |
|
623 |
< |
public void run() { |
624 |
< |
threadAssertNull(q.poll()); |
625 |
< |
try { |
626 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
627 |
< |
threadAssertTrue(q.isEmpty()); |
628 |
< |
} catch (InterruptedException e) { |
629 |
< |
threadUnexpectedException(); |
630 |
< |
} |
631 |
< |
} |
632 |
< |
}); |
633 |
< |
|
634 |
< |
executor.execute(new Runnable() { |
755 |
< |
|
756 |
< |
public void run() { |
757 |
< |
try { |
758 |
< |
Thread.sleep(SMALL_DELAY_MS); |
620 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
621 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
622 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
623 |
> |
|
624 |
> |
executor.execute(new CheckedRunnable() { |
625 |
> |
public void realRun() throws InterruptedException { |
626 |
> |
assertNull(q.poll()); |
627 |
> |
threadsStarted.await(); |
628 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
629 |
> |
checkEmpty(q); |
630 |
> |
}}); |
631 |
> |
|
632 |
> |
executor.execute(new CheckedRunnable() { |
633 |
> |
public void realRun() throws InterruptedException { |
634 |
> |
threadsStarted.await(); |
635 |
|
q.put(one); |
636 |
< |
} catch (InterruptedException e) { |
761 |
< |
threadUnexpectedException(); |
762 |
< |
} |
763 |
< |
} |
764 |
< |
}); |
765 |
< |
|
766 |
< |
joinPool(executor); |
767 |
< |
} |
768 |
< |
|
769 |
< |
/** |
770 |
< |
* A deserialized serialized queue has same elements in same order |
771 |
< |
*/ |
772 |
< |
public void testSerialization() { |
773 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
774 |
< |
|
775 |
< |
try { |
776 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
777 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
778 |
< |
out.writeObject(q); |
779 |
< |
out.close(); |
780 |
< |
|
781 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
782 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
783 |
< |
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
784 |
< |
|
785 |
< |
assertEquals(q.size(), r.size()); |
786 |
< |
while (!q.isEmpty()) { |
787 |
< |
assertEquals(q.remove(), r.remove()); |
788 |
< |
} |
789 |
< |
} catch (Exception e) { |
790 |
< |
unexpectedException(); |
636 |
> |
}}); |
637 |
|
} |
638 |
|
} |
639 |
|
|
640 |
|
/** |
641 |
< |
* drainTo(null) throws NPE |
796 |
< |
*/ |
797 |
< |
public void testDrainToNull() { |
798 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
799 |
< |
try { |
800 |
< |
q.drainTo(null); |
801 |
< |
shouldThrow(); |
802 |
< |
} catch (NullPointerException success) { |
803 |
< |
} |
804 |
< |
} |
805 |
< |
|
806 |
< |
/** |
807 |
< |
* drainTo(this) throws IAE |
641 |
> |
* A deserialized serialized queue has same elements in same order |
642 |
|
*/ |
643 |
< |
public void testDrainToSelf() { |
644 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
645 |
< |
try { |
646 |
< |
q.drainTo(q); |
647 |
< |
shouldThrow(); |
648 |
< |
} catch (IllegalArgumentException success) { |
643 |
> |
public void testSerialization() throws Exception { |
644 |
> |
Queue x = populatedQueue(SIZE); |
645 |
> |
Queue y = serialClone(x); |
646 |
> |
|
647 |
> |
assertNotSame(y, x); |
648 |
> |
assertEquals(x.size(), y.size()); |
649 |
> |
assertEquals(x.toString(), y.toString()); |
650 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
651 |
> |
while (!x.isEmpty()) { |
652 |
> |
assertFalse(y.isEmpty()); |
653 |
> |
assertEquals(x.remove(), y.remove()); |
654 |
|
} |
655 |
+ |
assertTrue(y.isEmpty()); |
656 |
|
} |
657 |
|
|
658 |
|
/** |
662 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
663 |
|
ArrayList l = new ArrayList(); |
664 |
|
q.drainTo(l); |
665 |
< |
assertEquals(q.size(), 0); |
666 |
< |
assertEquals(l.size(), SIZE); |
665 |
> |
assertEquals(0, q.size()); |
666 |
> |
assertEquals(SIZE, l.size()); |
667 |
|
for (int i = 0; i < SIZE; ++i) { |
668 |
< |
assertEquals(l.get(i), new Integer(i)); |
668 |
> |
assertEquals(i, l.get(i)); |
669 |
|
} |
670 |
|
q.add(zero); |
671 |
|
q.add(one); |
674 |
|
assertTrue(q.contains(one)); |
675 |
|
l.clear(); |
676 |
|
q.drainTo(l); |
677 |
< |
assertEquals(q.size(), 0); |
678 |
< |
assertEquals(l.size(), 2); |
677 |
> |
assertEquals(0, q.size()); |
678 |
> |
assertEquals(2, l.size()); |
679 |
|
for (int i = 0; i < 2; ++i) { |
680 |
< |
assertEquals(l.get(i), new Integer(i)); |
680 |
> |
assertEquals(i, l.get(i)); |
681 |
|
} |
682 |
|
} |
683 |
|
|
684 |
|
/** |
685 |
< |
* drainTo empties full queue, unblocking a waiting put. |
685 |
> |
* drainTo(c) empties full queue, unblocking a waiting put. |
686 |
|
*/ |
687 |
< |
public void testDrainToWithActivePut() { |
687 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
688 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
689 |
< |
Thread t = new Thread(new Runnable() { |
690 |
< |
|
691 |
< |
public void run() { |
692 |
< |
try { |
693 |
< |
q.put(new Integer(SIZE + 1)); |
694 |
< |
} catch (Exception ie) { |
695 |
< |
threadUnexpectedException(); |
696 |
< |
} |
697 |
< |
} |
698 |
< |
}); |
699 |
< |
try { |
860 |
< |
t.start(); |
861 |
< |
ArrayList l = new ArrayList(); |
862 |
< |
q.drainTo(l); |
863 |
< |
assertTrue(l.size() >= SIZE); |
864 |
< |
for (int i = 0; i < SIZE; ++i) { |
865 |
< |
assertEquals(l.get(i), new Integer(i)); |
866 |
< |
} |
867 |
< |
t.join(); |
868 |
< |
assertTrue(q.size() + l.size() >= SIZE); |
869 |
< |
} catch (Exception e) { |
870 |
< |
unexpectedException(); |
871 |
< |
} |
872 |
< |
} |
873 |
< |
|
874 |
< |
/** |
875 |
< |
* drainTo(null, n) throws NPE |
876 |
< |
*/ |
877 |
< |
public void testDrainToNullN() { |
878 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
879 |
< |
try { |
880 |
< |
q.drainTo(null, 0); |
881 |
< |
shouldThrow(); |
882 |
< |
} catch (NullPointerException success) { |
883 |
< |
} |
884 |
< |
} |
885 |
< |
|
886 |
< |
/** |
887 |
< |
* drainTo(this, n) throws IAE |
888 |
< |
*/ |
889 |
< |
public void testDrainToSelfN() { |
890 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
891 |
< |
try { |
892 |
< |
q.drainTo(q, 0); |
893 |
< |
shouldThrow(); |
894 |
< |
} catch (IllegalArgumentException success) { |
895 |
< |
} |
689 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
690 |
> |
public void realRun() { |
691 |
> |
q.put(SIZE + 1); |
692 |
> |
}}); |
693 |
> |
ArrayList l = new ArrayList(); |
694 |
> |
q.drainTo(l); |
695 |
> |
assertTrue(l.size() >= SIZE); |
696 |
> |
for (int i = 0; i < SIZE; ++i) |
697 |
> |
assertEquals(i, l.get(i)); |
698 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
699 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
700 |
|
} |
701 |
|
|
702 |
|
/** |
703 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
703 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
704 |
|
*/ |
705 |
|
public void testDrainToN() { |
706 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
707 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
708 |
|
for (int j = 0; j < SIZE; j++) { |
709 |
< |
assertTrue(q.offer(new Integer(j))); |
709 |
> |
assertTrue(q.offer(j)); |
710 |
|
} |
711 |
|
ArrayList l = new ArrayList(); |
712 |
|
q.drainTo(l, i); |
713 |
|
int k = (i < SIZE) ? i : SIZE; |
714 |
< |
assertEquals(l.size(), k); |
715 |
< |
assertEquals(q.size(), SIZE - k); |
716 |
< |
for (int j = 0; j < k; ++j) { |
717 |
< |
assertEquals(l.get(j), new Integer(j)); |
718 |
< |
} |
915 |
< |
while (q.poll() != null); |
714 |
> |
assertEquals(k, l.size()); |
715 |
> |
assertEquals(SIZE - k, q.size()); |
716 |
> |
for (int j = 0; j < k; ++j) |
717 |
> |
assertEquals(j, l.get(j)); |
718 |
> |
do {} while (q.poll() != null); |
719 |
|
} |
720 |
|
} |
721 |
|
|
722 |
< |
/* |
723 |
< |
* poll and take should decrement the waiting consumer count |
722 |
> |
/** |
723 |
> |
* timed poll() or take() increments the waiting consumer count; |
724 |
> |
* offer(e) decrements the waiting consumer count |
725 |
|
*/ |
726 |
< |
public void testWaitingConsumer() { |
727 |
< |
try { |
728 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
729 |
< |
final ConsumerObserver waiting = new ConsumerObserver(); |
730 |
< |
new Thread(new Runnable() { |
927 |
< |
|
928 |
< |
public void run() { |
929 |
< |
try { |
930 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
931 |
< |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
932 |
< |
threadAssertTrue(q.offer(new Object())); |
933 |
< |
} catch (Exception ex) { |
934 |
< |
threadUnexpectedException(); |
935 |
< |
} |
726 |
> |
public void testWaitingConsumer() throws InterruptedException { |
727 |
> |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
728 |
> |
assertEquals(0, q.getWaitingConsumerCount()); |
729 |
> |
assertFalse(q.hasWaitingConsumer()); |
730 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
731 |
|
|
732 |
< |
} |
733 |
< |
}).start(); |
734 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null); |
735 |
< |
assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers()); |
736 |
< |
} catch (Exception ex) { |
737 |
< |
this.unexpectedException(); |
738 |
< |
} |
732 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
733 |
> |
public void realRun() throws InterruptedException { |
734 |
> |
threadStarted.countDown(); |
735 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
736 |
> |
assertEquals(0, q.getWaitingConsumerCount()); |
737 |
> |
assertFalse(q.hasWaitingConsumer()); |
738 |
> |
}}); |
739 |
> |
|
740 |
> |
threadStarted.await(); |
741 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
742 |
> |
assertEquals(1, q.getWaitingConsumerCount()); |
743 |
> |
assertTrue(q.hasWaitingConsumer()); |
744 |
> |
|
745 |
> |
assertTrue(q.offer(one)); |
746 |
> |
assertEquals(0, q.getWaitingConsumerCount()); |
747 |
> |
assertFalse(q.hasWaitingConsumer()); |
748 |
> |
|
749 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
750 |
|
} |
945 |
– |
/* |
946 |
– |
* Inserts null into transfer throws NPE |
947 |
– |
*/ |
751 |
|
|
752 |
< |
public void testTransfer1() { |
752 |
> |
/** |
753 |
> |
* transfer(null) throws NullPointerException |
754 |
> |
*/ |
755 |
> |
public void testTransfer1() throws InterruptedException { |
756 |
|
try { |
757 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
758 |
|
q.transfer(null); |
759 |
|
shouldThrow(); |
760 |
< |
} catch (NullPointerException ex) { |
761 |
< |
} catch (Exception ex) { |
762 |
< |
this.unexpectedException(); |
763 |
< |
} |
760 |
> |
} catch (NullPointerException success) {} |
761 |
> |
} |
762 |
> |
|
763 |
> |
/** |
764 |
> |
* transfer waits until a poll occurs. The transfered element |
765 |
> |
* is returned by this associated poll. |
766 |
> |
*/ |
767 |
> |
public void testTransfer2() throws InterruptedException { |
768 |
> |
final LinkedTransferQueue<Integer> q |
769 |
> |
= new LinkedTransferQueue<Integer>(); |
770 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
771 |
> |
|
772 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
773 |
> |
public void realRun() throws InterruptedException { |
774 |
> |
threadStarted.countDown(); |
775 |
> |
q.transfer(five); |
776 |
> |
checkEmpty(q); |
777 |
> |
}}); |
778 |
> |
|
779 |
> |
threadStarted.await(); |
780 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
781 |
> |
assertEquals(1, q.size()); |
782 |
> |
assertSame(five, q.poll()); |
783 |
> |
checkEmpty(q); |
784 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
785 |
|
} |
786 |
|
|
787 |
< |
/* |
788 |
< |
* transfer attempts to insert into the queue then wait until that |
962 |
< |
* object is removed via take or poll. |
787 |
> |
/** |
788 |
> |
* transfer waits until a poll occurs, and then transfers in fifo order |
789 |
|
*/ |
790 |
< |
public void testTransfer2() { |
791 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
792 |
< |
new Thread(new Runnable() { |
790 |
> |
public void testTransfer3() throws InterruptedException { |
791 |
> |
final LinkedTransferQueue<Integer> q |
792 |
> |
= new LinkedTransferQueue<Integer>(); |
793 |
|
|
794 |
< |
public void run() { |
795 |
< |
try { |
796 |
< |
q.transfer(new Integer(SIZE)); |
797 |
< |
threadAssertTrue(q.isEmpty()); |
798 |
< |
} catch (Exception ex) { |
799 |
< |
threadUnexpectedException(); |
974 |
< |
} |
975 |
< |
} |
976 |
< |
}).start(); |
794 |
> |
Thread first = newStartedThread(new CheckedRunnable() { |
795 |
> |
public void realRun() throws InterruptedException { |
796 |
> |
q.transfer(four); |
797 |
> |
assertTrue(!q.contains(four)); |
798 |
> |
assertEquals(1, q.size()); |
799 |
> |
}}); |
800 |
|
|
801 |
< |
try { |
802 |
< |
Thread.sleep(SHORT_DELAY_MS); |
803 |
< |
assertEquals(1, q.size()); |
804 |
< |
q.poll(); |
805 |
< |
assertTrue(q.isEmpty()); |
806 |
< |
} catch (Exception ex) { |
807 |
< |
this.unexpectedException(); |
808 |
< |
} |
809 |
< |
} |
810 |
< |
/* |
811 |
< |
* transfer will attempt to transfer in fifo order and continue waiting if |
812 |
< |
* the element being transfered is not polled or taken |
813 |
< |
*/ |
814 |
< |
|
815 |
< |
public void testTransfer3() { |
816 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
817 |
< |
new Thread(new Runnable() { |
995 |
< |
public void run() { |
996 |
< |
try { |
997 |
< |
Integer i; |
998 |
< |
q.transfer((i = new Integer(SIZE + 1))); |
999 |
< |
threadAssertTrue(!q.contains(i)); |
1000 |
< |
threadAssertEquals(1, q.size()); |
1001 |
< |
} catch (Exception ex) { |
1002 |
< |
threadUnexpectedException(); |
1003 |
< |
} |
1004 |
< |
} |
1005 |
< |
}).start(); |
1006 |
< |
Thread interruptedThread = |
1007 |
< |
new Thread(new Runnable() { |
1008 |
< |
public void run() { |
1009 |
< |
try { |
1010 |
< |
q.transfer(new Integer(SIZE)); |
1011 |
< |
threadShouldThrow(); |
1012 |
< |
} catch (InterruptedException ex) { |
1013 |
< |
} |
1014 |
< |
} |
1015 |
< |
}); |
1016 |
< |
interruptedThread.start(); |
1017 |
< |
try { |
1018 |
< |
Thread.sleep(LONG_DELAY_MS); |
1019 |
< |
assertEquals(2, q.size()); |
1020 |
< |
q.poll(); |
1021 |
< |
Thread.sleep(LONG_DELAY_MS); |
1022 |
< |
interruptedThread.interrupt(); |
1023 |
< |
assertEquals(1, q.size()); |
1024 |
< |
} catch (Exception ex) { |
1025 |
< |
this.unexpectedException(); |
1026 |
< |
} |
801 |
> |
Thread interruptedThread = newStartedThread( |
802 |
> |
new CheckedInterruptedRunnable() { |
803 |
> |
public void realRun() throws InterruptedException { |
804 |
> |
while (q.isEmpty()) |
805 |
> |
Thread.yield(); |
806 |
> |
q.transfer(five); |
807 |
> |
}}); |
808 |
> |
|
809 |
> |
while (q.size() < 2) |
810 |
> |
Thread.yield(); |
811 |
> |
assertEquals(2, q.size()); |
812 |
> |
assertSame(four, q.poll()); |
813 |
> |
first.join(); |
814 |
> |
assertEquals(1, q.size()); |
815 |
> |
interruptedThread.interrupt(); |
816 |
> |
interruptedThread.join(); |
817 |
> |
checkEmpty(q); |
818 |
|
} |
819 |
|
|
820 |
|
/** |
821 |
< |
* transfer will wait as long as a poll or take occurs if one does occur |
822 |
< |
* the waiting is finished and the thread that tries to poll/take |
1032 |
< |
* wins in retrieving the element |
821 |
> |
* transfer waits until a poll occurs, at which point the polling |
822 |
> |
* thread returns the element |
823 |
|
*/ |
824 |
< |
public void testTransfer4() { |
824 |
> |
public void testTransfer4() throws InterruptedException { |
825 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1036 |
– |
new Thread(new Runnable() { |
826 |
|
|
827 |
< |
public void run() { |
828 |
< |
try { |
829 |
< |
q.transfer(new Integer(four)); |
830 |
< |
threadAssertFalse(q.contains(new Integer(four))); |
831 |
< |
threadAssertEquals(new Integer(three), q.poll()); |
832 |
< |
} catch (Exception ex) { |
833 |
< |
threadUnexpectedException(); |
834 |
< |
} |
835 |
< |
} |
836 |
< |
}).start(); |
837 |
< |
try { |
838 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
839 |
< |
assertTrue(q.offer(three)); |
840 |
< |
assertEquals(new Integer(four), q.poll()); |
1052 |
< |
} catch (Exception ex) { |
1053 |
< |
this.unexpectedException(); |
1054 |
< |
} |
827 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
828 |
> |
public void realRun() throws InterruptedException { |
829 |
> |
q.transfer(four); |
830 |
> |
assertFalse(q.contains(four)); |
831 |
> |
assertSame(three, q.poll()); |
832 |
> |
}}); |
833 |
> |
|
834 |
> |
while (q.isEmpty()) |
835 |
> |
Thread.yield(); |
836 |
> |
assertFalse(q.isEmpty()); |
837 |
> |
assertEquals(1, q.size()); |
838 |
> |
assertTrue(q.offer(three)); |
839 |
> |
assertSame(four, q.poll()); |
840 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
841 |
|
} |
842 |
< |
/* |
843 |
< |
* Insert null into trTransfer throws NPE |
842 |
> |
|
843 |
> |
/** |
844 |
> |
* transfer waits until a take occurs. The transfered element |
845 |
> |
* is returned by this associated take. |
846 |
|
*/ |
847 |
+ |
public void testTransfer5() throws InterruptedException { |
848 |
+ |
final LinkedTransferQueue<Integer> q |
849 |
+ |
= new LinkedTransferQueue<Integer>(); |
850 |
|
|
851 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
852 |
+ |
public void realRun() throws InterruptedException { |
853 |
+ |
q.transfer(four); |
854 |
+ |
checkEmpty(q); |
855 |
+ |
}}); |
856 |
+ |
|
857 |
+ |
while (q.isEmpty()) |
858 |
+ |
Thread.yield(); |
859 |
+ |
assertFalse(q.isEmpty()); |
860 |
+ |
assertEquals(1, q.size()); |
861 |
+ |
assertSame(four, q.take()); |
862 |
+ |
checkEmpty(q); |
863 |
+ |
awaitTermination(t, MEDIUM_DELAY_MS); |
864 |
+ |
} |
865 |
+ |
|
866 |
+ |
/** |
867 |
+ |
* tryTransfer(null) throws NullPointerException |
868 |
+ |
*/ |
869 |
|
public void testTryTransfer1() { |
870 |
+ |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
871 |
|
try { |
1062 |
– |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
872 |
|
q.tryTransfer(null); |
873 |
< |
this.shouldThrow(); |
874 |
< |
} catch (NullPointerException ex) { |
1066 |
< |
} catch (Exception ex) { |
1067 |
< |
this.unexpectedException(); |
1068 |
< |
} |
873 |
> |
shouldThrow(); |
874 |
> |
} catch (NullPointerException success) {} |
875 |
|
} |
1070 |
– |
/* |
1071 |
– |
* tryTransfer returns false and does not enqueue if there are no consumers |
1072 |
– |
* waiting to poll or take. |
1073 |
– |
*/ |
876 |
|
|
877 |
< |
public void testTryTransfer2() { |
878 |
< |
try { |
879 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
880 |
< |
assertFalse(q.tryTransfer(new Object())); |
881 |
< |
assertEquals(0, q.size()); |
882 |
< |
} catch (Exception ex) { |
883 |
< |
this.unexpectedException(); |
884 |
< |
} |
877 |
> |
/** |
878 |
> |
* tryTransfer returns false and does not enqueue if there are no |
879 |
> |
* consumers waiting to poll or take. |
880 |
> |
*/ |
881 |
> |
public void testTryTransfer2() throws InterruptedException { |
882 |
> |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
883 |
> |
assertFalse(q.tryTransfer(new Object())); |
884 |
> |
assertFalse(q.hasWaitingConsumer()); |
885 |
> |
checkEmpty(q); |
886 |
|
} |
887 |
< |
/* |
888 |
< |
* if there is a consumer waiting poll or take tryTransfer returns |
889 |
< |
* true while enqueueing object |
887 |
> |
|
888 |
> |
/** |
889 |
> |
* If there is a consumer waiting in timed poll, tryTransfer |
890 |
> |
* returns true while successfully transfering object. |
891 |
|
*/ |
892 |
+ |
public void testTryTransfer3() throws InterruptedException { |
893 |
+ |
final Object hotPotato = new Object(); |
894 |
+ |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
895 |
|
|
896 |
< |
public void testTryTransfer3() { |
897 |
< |
try { |
898 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
899 |
< |
new Thread(new Runnable() { |
896 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
897 |
> |
public void realRun() { |
898 |
> |
while (! q.hasWaitingConsumer()) |
899 |
> |
Thread.yield(); |
900 |
> |
assertTrue(q.hasWaitingConsumer()); |
901 |
> |
checkEmpty(q); |
902 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
903 |
> |
}}); |
904 |
> |
|
905 |
> |
assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
906 |
> |
checkEmpty(q); |
907 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
908 |
> |
} |
909 |
> |
|
910 |
> |
/** |
911 |
> |
* If there is a consumer waiting in take, tryTransfer returns |
912 |
> |
* true while successfully transfering object. |
913 |
> |
*/ |
914 |
> |
public void testTryTransfer4() throws InterruptedException { |
915 |
> |
final Object hotPotato = new Object(); |
916 |
> |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
917 |
|
|
918 |
< |
public void run() { |
919 |
< |
try { |
920 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
921 |
< |
threadAssertTrue(q.tryTransfer(new Object())); |
922 |
< |
} catch (Exception ex) { |
923 |
< |
threadUnexpectedException(); |
924 |
< |
} |
918 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
919 |
> |
public void realRun() { |
920 |
> |
while (! q.hasWaitingConsumer()) |
921 |
> |
Thread.yield(); |
922 |
> |
assertTrue(q.hasWaitingConsumer()); |
923 |
> |
checkEmpty(q); |
924 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
925 |
> |
}}); |
926 |
|
|
927 |
< |
} |
928 |
< |
}).start(); |
929 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null); |
1105 |
< |
assertTrue(q.isEmpty()); |
1106 |
< |
} catch (Exception ex) { |
1107 |
< |
this.unexpectedException(); |
1108 |
< |
} |
927 |
> |
assertSame(q.take(), hotPotato); |
928 |
> |
checkEmpty(q); |
929 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
930 |
|
} |
931 |
|
|
932 |
< |
/* |
933 |
< |
* tryTransfer waits the amount given if interrupted, show an |
1113 |
< |
* interrupted exception |
932 |
> |
/** |
933 |
> |
* tryTransfer blocks interruptibly if no takers |
934 |
|
*/ |
935 |
< |
public void testTryTransfer4() { |
935 |
> |
public void testTryTransfer5() throws InterruptedException { |
936 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
937 |
< |
Thread toInterrupt = new Thread(new Runnable() { |
937 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
938 |
> |
assertTrue(q.isEmpty()); |
939 |
|
|
940 |
< |
public void run() { |
940 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
941 |
> |
public void realRun() throws InterruptedException { |
942 |
> |
Thread.currentThread().interrupt(); |
943 |
|
try { |
944 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
945 |
< |
threadShouldThrow(); |
946 |
< |
} catch (InterruptedException ex) { |
947 |
< |
} |
948 |
< |
} |
949 |
< |
}); |
950 |
< |
try { |
951 |
< |
toInterrupt.start(); |
952 |
< |
Thread.sleep(SMALL_DELAY_MS); |
953 |
< |
toInterrupt.interrupt(); |
954 |
< |
} catch (Exception ex) { |
955 |
< |
this.unexpectedException(); |
956 |
< |
} |
944 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
945 |
> |
shouldThrow(); |
946 |
> |
} catch (InterruptedException success) {} |
947 |
> |
assertFalse(Thread.interrupted()); |
948 |
> |
|
949 |
> |
pleaseInterrupt.countDown(); |
950 |
> |
try { |
951 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
952 |
> |
shouldThrow(); |
953 |
> |
} catch (InterruptedException success) {} |
954 |
> |
assertFalse(Thread.interrupted()); |
955 |
> |
}}); |
956 |
> |
|
957 |
> |
await(pleaseInterrupt); |
958 |
> |
assertThreadStaysAlive(t); |
959 |
> |
t.interrupt(); |
960 |
> |
awaitTermination(t); |
961 |
> |
checkEmpty(q); |
962 |
|
} |
963 |
|
|
964 |
< |
/* |
965 |
< |
* tryTransfer gives up after the timeout and return false |
964 |
> |
/** |
965 |
> |
* tryTransfer gives up after the timeout and returns false |
966 |
|
*/ |
967 |
< |
public void testTryTransfer5() { |
967 |
> |
public void testTryTransfer6() throws InterruptedException { |
968 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1141 |
– |
try { |
1142 |
– |
new Thread(new Runnable() { |
969 |
|
|
970 |
< |
public void run() { |
971 |
< |
try { |
972 |
< |
threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
973 |
< |
} catch (InterruptedException ex) { |
974 |
< |
threadUnexpectedException(); |
975 |
< |
} |
976 |
< |
} |
977 |
< |
}).start(); |
978 |
< |
Thread.sleep(LONG_DELAY_MS); |
979 |
< |
assertTrue(q.isEmpty()); |
980 |
< |
} catch (Exception ex) { |
1155 |
< |
this.unexpectedException(); |
1156 |
< |
} |
970 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
971 |
> |
public void realRun() throws InterruptedException { |
972 |
> |
long t0 = System.nanoTime(); |
973 |
> |
assertFalse(q.tryTransfer(new Object(), |
974 |
> |
timeoutMillis(), MILLISECONDS)); |
975 |
> |
assertTrue(millisElapsedSince(t0) >= timeoutMillis()); |
976 |
> |
checkEmpty(q); |
977 |
> |
}}); |
978 |
> |
|
979 |
> |
awaitTermination(t); |
980 |
> |
checkEmpty(q); |
981 |
|
} |
982 |
|
|
983 |
< |
/* |
983 |
> |
/** |
984 |
|
* tryTransfer waits for any elements previously in to be removed |
985 |
|
* before transfering to a poll or take |
986 |
|
*/ |
987 |
< |
public void testTryTransfer6() { |
987 |
> |
public void testTryTransfer7() throws InterruptedException { |
988 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
989 |
< |
q.offer(new Integer(four)); |
1166 |
< |
new Thread(new Runnable() { |
989 |
> |
assertTrue(q.offer(four)); |
990 |
|
|
991 |
< |
public void run() { |
992 |
< |
try { |
993 |
< |
threadAssertTrue(q.tryTransfer(new Integer(five), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
994 |
< |
threadAssertTrue(q.isEmpty()); |
995 |
< |
} catch (InterruptedException ex) { |
996 |
< |
threadUnexpectedException(); |
997 |
< |
} |
998 |
< |
} |
999 |
< |
}).start(); |
1000 |
< |
try { |
1001 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1002 |
< |
assertEquals(2, q.size()); |
1003 |
< |
assertEquals(new Integer(four), q.poll()); |
1181 |
< |
assertEquals(new Integer(five), q.poll()); |
1182 |
< |
assertTrue(q.isEmpty()); |
1183 |
< |
} catch (Exception ex) { |
1184 |
< |
this.unexpectedException(); |
1185 |
< |
} |
991 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
992 |
> |
public void realRun() throws InterruptedException { |
993 |
> |
assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); |
994 |
> |
checkEmpty(q); |
995 |
> |
}}); |
996 |
> |
|
997 |
> |
while (q.size() != 2) |
998 |
> |
Thread.yield(); |
999 |
> |
assertEquals(2, q.size()); |
1000 |
> |
assertSame(four, q.poll()); |
1001 |
> |
assertSame(five, q.poll()); |
1002 |
> |
checkEmpty(q); |
1003 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1004 |
|
} |
1005 |
|
|
1006 |
< |
/* |
1007 |
< |
* tryTransfer attempts to enqueue into the q and fails returning false not |
1008 |
< |
* enqueueing and the successing poll is null |
1006 |
> |
/** |
1007 |
> |
* tryTransfer attempts to enqueue into the queue and fails |
1008 |
> |
* returning false not enqueueing and the successive poll is null |
1009 |
|
*/ |
1010 |
< |
public void testTryTransfer7() { |
1010 |
> |
public void testTryTransfer8() throws InterruptedException { |
1011 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1012 |
< |
q.offer(new Integer(four)); |
1013 |
< |
new Thread(new Runnable() { |
1014 |
< |
|
1015 |
< |
public void run() { |
1016 |
< |
try { |
1017 |
< |
threadAssertFalse(q.tryTransfer(new Integer(five), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1018 |
< |
threadAssertTrue(q.isEmpty()); |
1019 |
< |
} catch (InterruptedException ex) { |
1020 |
< |
threadUnexpectedException(); |
1203 |
< |
} |
1204 |
< |
} |
1205 |
< |
}).start(); |
1206 |
< |
try { |
1207 |
< |
assertEquals(1, q.size()); |
1208 |
< |
assertEquals(new Integer(four), q.poll()); |
1209 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1210 |
< |
assertNull(q.poll()); |
1211 |
< |
} catch (Exception ex) { |
1212 |
< |
this.unexpectedException(); |
1213 |
< |
} |
1012 |
> |
assertTrue(q.offer(four)); |
1013 |
> |
assertEquals(1, q.size()); |
1014 |
> |
long t0 = System.nanoTime(); |
1015 |
> |
assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS)); |
1016 |
> |
assertTrue(millisElapsedSince(t0) >= timeoutMillis()); |
1017 |
> |
assertEquals(1, q.size()); |
1018 |
> |
assertSame(four, q.poll()); |
1019 |
> |
assertNull(q.poll()); |
1020 |
> |
checkEmpty(q); |
1021 |
|
} |
1022 |
|
|
1023 |
< |
private LinkedTransferQueue populatedQueue( |
1024 |
< |
int n) { |
1025 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
1026 |
< |
assertTrue(q.isEmpty()); |
1027 |
< |
int remainingCapacity = q.remainingCapacity(); |
1221 |
< |
for (int i = 0; i < |
1222 |
< |
n; i++) { |
1023 |
> |
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1024 |
> |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
1025 |
> |
checkEmpty(q); |
1026 |
> |
for (int i = 0; i < n; i++) { |
1027 |
> |
assertEquals(i, q.size()); |
1028 |
|
assertTrue(q.offer(i)); |
1029 |
+ |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
1030 |
|
} |
1225 |
– |
|
1031 |
|
assertFalse(q.isEmpty()); |
1227 |
– |
assertEquals(remainingCapacity, q.remainingCapacity()); |
1228 |
– |
assertEquals(n, q.size()); |
1032 |
|
return q; |
1033 |
|
} |
1034 |
|
|
1035 |
< |
private static class ConsumerObserver { |
1036 |
< |
|
1037 |
< |
private int waitingConsumers; |
1038 |
< |
|
1039 |
< |
private ConsumerObserver() { |
1040 |
< |
} |
1041 |
< |
|
1042 |
< |
private void setWaitingConsumer(int i) { |
1043 |
< |
this.waitingConsumers = i; |
1044 |
< |
} |
1045 |
< |
|
1046 |
< |
private int getWaitingConsumers() { |
1244 |
< |
return waitingConsumers; |
1035 |
> |
/** |
1036 |
> |
* remove(null), contains(null) always return false |
1037 |
> |
*/ |
1038 |
> |
public void testNeverContainsNull() { |
1039 |
> |
Collection<?>[] qs = { |
1040 |
> |
new LinkedTransferQueue<Object>(), |
1041 |
> |
populatedQueue(2), |
1042 |
> |
}; |
1043 |
> |
|
1044 |
> |
for (Collection<?> q : qs) { |
1045 |
> |
assertFalse(q.contains(null)); |
1046 |
> |
assertFalse(q.remove(null)); |
1047 |
|
} |
1048 |
|
} |
1049 |
|
} |