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