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