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 |
> |
public 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() { |
285 |
> |
public void testBlockingTake() throws InterruptedException { |
286 |
> |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
287 |
> |
Thread t = new Thread(new CheckedRunnable() { |
288 |
> |
public void realRun() throws InterruptedException { |
289 |
> |
for (int i = 0; i < SIZE; ++i) { |
290 |
> |
assertEquals(i, (int) q.take()); |
291 |
> |
} |
292 |
|
try { |
291 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
292 |
– |
for (int i = 0; i < SIZE; ++i) { |
293 |
– |
assertEquals(i, ((Integer) q.take()).intValue()); |
294 |
– |
} |
293 |
|
q.take(); |
294 |
< |
threadShouldThrow(); |
295 |
< |
} catch (InterruptedException success) { |
296 |
< |
} |
297 |
< |
} |
300 |
< |
}); |
294 |
> |
shouldThrow(); |
295 |
> |
} catch (InterruptedException success) {} |
296 |
> |
}}); |
297 |
> |
|
298 |
|
t.start(); |
299 |
< |
try { |
300 |
< |
Thread.sleep(SHORT_DELAY_MS); |
301 |
< |
t.interrupt(); |
302 |
< |
t.join(); |
306 |
< |
} catch (InterruptedException ie) { |
307 |
< |
unexpectedException(); |
308 |
< |
} |
299 |
> |
Thread.sleep(SHORT_DELAY_MS); |
300 |
> |
t.interrupt(); |
301 |
> |
t.join(); |
302 |
> |
checkEmpty(q); |
303 |
|
} |
304 |
|
|
305 |
|
/** |
306 |
|
* poll succeeds unless empty |
307 |
|
*/ |
308 |
< |
public void testPoll() { |
309 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
308 |
> |
public void testPoll() throws InterruptedException { |
309 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
310 |
|
for (int i = 0; i < SIZE; ++i) { |
311 |
< |
assertEquals(i, ((Integer) q.poll()).intValue()); |
311 |
> |
assertEquals(i, (int) q.poll()); |
312 |
|
} |
313 |
|
assertNull(q.poll()); |
314 |
+ |
checkEmpty(q); |
315 |
|
} |
316 |
|
|
317 |
|
/** |
318 |
|
* timed pool with zero timeout succeeds when non-empty, else times out |
319 |
|
*/ |
320 |
< |
public void testTimedPoll0() { |
321 |
< |
try { |
322 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
323 |
< |
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(); |
320 |
> |
public void testTimedPoll0() throws InterruptedException { |
321 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
322 |
> |
for (int i = 0; i < SIZE; ++i) { |
323 |
> |
assertEquals(i, (int) q.poll(0, MILLISECONDS)); |
324 |
|
} |
325 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
326 |
+ |
checkEmpty(q); |
327 |
|
} |
328 |
|
|
329 |
|
/** |
330 |
|
* timed pool with nonzero timeout succeeds when non-empty, else times out |
331 |
|
*/ |
332 |
< |
public void testTimedPoll() { |
333 |
< |
try { |
334 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
335 |
< |
for (int i = 0; i < SIZE; ++i) { |
336 |
< |
assertEquals(i, ((Integer) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
337 |
< |
} |
338 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
347 |
< |
} catch (InterruptedException e) { |
348 |
< |
unexpectedException(); |
332 |
> |
public void testTimedPoll() throws InterruptedException { |
333 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
334 |
> |
for (int i = 0; i < SIZE; ++i) { |
335 |
> |
long t0 = System.nanoTime(); |
336 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
337 |
> |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
338 |
> |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
339 |
|
} |
340 |
+ |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
341 |
+ |
checkEmpty(q); |
342 |
|
} |
343 |
|
|
344 |
|
/** |
345 |
|
* Interrupted timed poll throws InterruptedException instead of |
346 |
|
* returning timeout status |
347 |
|
*/ |
348 |
< |
public void testInterruptedTimedPoll() { |
349 |
< |
Thread t = new Thread(new Runnable() { |
350 |
< |
|
351 |
< |
public void run() { |
352 |
< |
try { |
353 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
354 |
< |
for (int i = 0; i < SIZE; ++i) { |
355 |
< |
threadAssertEquals(i, ((Integer) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
356 |
< |
} |
365 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
366 |
< |
} catch (InterruptedException success) { |
348 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
349 |
> |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
350 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
351 |
> |
public void realRun() throws InterruptedException { |
352 |
> |
for (int i = 0; i < SIZE; ++i) { |
353 |
> |
long t0 = System.nanoTime(); |
354 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
355 |
> |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
356 |
> |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
357 |
|
} |
358 |
< |
} |
359 |
< |
}); |
360 |
< |
t.start(); |
361 |
< |
try { |
362 |
< |
Thread.sleep(SHORT_DELAY_MS); |
363 |
< |
t.interrupt(); |
364 |
< |
t.join(); |
365 |
< |
} catch (InterruptedException ie) { |
366 |
< |
unexpectedException(); |
367 |
< |
} |
358 |
> |
try { |
359 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
360 |
> |
shouldThrow(); |
361 |
> |
} catch (InterruptedException success) {} |
362 |
> |
}}); |
363 |
> |
|
364 |
> |
Thread.sleep(SMALL_DELAY_MS); |
365 |
> |
t.interrupt(); |
366 |
> |
t.join(); |
367 |
> |
checkEmpty(q); |
368 |
|
} |
369 |
|
|
370 |
|
/** |
371 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
372 |
< |
* on interruption throws |
371 |
> |
* timed poll before a delayed offer fails; after offer succeeds; |
372 |
> |
* on interruption throws |
373 |
|
*/ |
374 |
< |
public void testTimedPollWithOffer() { |
374 |
> |
public void testTimedPollWithOffer() throws InterruptedException { |
375 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
376 |
< |
Thread t = new Thread(new Runnable() { |
377 |
< |
|
378 |
< |
public void run() { |
376 |
> |
Thread t = new Thread(new CheckedRunnable() { |
377 |
> |
public void realRun() throws InterruptedException { |
378 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
379 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
380 |
|
try { |
381 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
382 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
383 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
384 |
< |
threadShouldThrow(); |
385 |
< |
} catch (InterruptedException success) { |
386 |
< |
} |
387 |
< |
} |
388 |
< |
}); |
389 |
< |
try { |
390 |
< |
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 |
< |
} |
381 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
382 |
> |
shouldThrow(); |
383 |
> |
} catch (InterruptedException success) {} |
384 |
> |
}}); |
385 |
> |
|
386 |
> |
t.start(); |
387 |
> |
Thread.sleep(SMALL_DELAY_MS); |
388 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
389 |
> |
t.interrupt(); |
390 |
> |
t.join(); |
391 |
|
} |
392 |
|
|
393 |
|
/** |
394 |
|
* peek returns next element, or null if empty |
395 |
|
*/ |
396 |
< |
public void testPeek() { |
397 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
396 |
> |
public void testPeek() throws InterruptedException { |
397 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
398 |
|
for (int i = 0; i < SIZE; ++i) { |
399 |
< |
assertEquals(i, ((Integer) q.peek()).intValue()); |
400 |
< |
q.poll(); |
399 |
> |
assertEquals(i, (int) q.peek()); |
400 |
> |
assertEquals(i, (int) q.poll()); |
401 |
|
assertTrue(q.peek() == null || |
402 |
< |
i != ((Integer) q.peek()).intValue()); |
402 |
> |
i != (int) q.peek()); |
403 |
|
} |
404 |
|
assertNull(q.peek()); |
405 |
+ |
checkEmpty(q); |
406 |
|
} |
407 |
|
|
408 |
|
/** |
409 |
< |
* element returns next element, or throws NSEE if empty |
409 |
> |
* element returns next element, or throws NoSuchElementException if empty |
410 |
|
*/ |
411 |
< |
public void testElement() { |
412 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
411 |
> |
public void testElement() throws InterruptedException { |
412 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
413 |
|
for (int i = 0; i < SIZE; ++i) { |
414 |
< |
assertEquals(i, ((Integer) q.element()).intValue()); |
415 |
< |
q.poll(); |
414 |
> |
assertEquals(i, (int) q.element()); |
415 |
> |
assertEquals(i, (int) q.poll()); |
416 |
|
} |
417 |
|
try { |
418 |
|
q.element(); |
419 |
|
shouldThrow(); |
420 |
< |
} catch (NoSuchElementException success) { |
421 |
< |
} |
420 |
> |
} catch (NoSuchElementException success) {} |
421 |
> |
checkEmpty(q); |
422 |
|
} |
423 |
|
|
424 |
|
/** |
425 |
< |
* remove removes next element, or throws NSEE if empty |
425 |
> |
* remove removes next element, or throws NoSuchElementException if empty |
426 |
|
*/ |
427 |
< |
public void testRemove() { |
428 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
427 |
> |
public void testRemove() throws InterruptedException { |
428 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
429 |
|
for (int i = 0; i < SIZE; ++i) { |
430 |
< |
assertEquals(i, ((Integer) q.remove()).intValue()); |
430 |
> |
assertEquals(i, (int) q.remove()); |
431 |
|
} |
432 |
|
try { |
433 |
|
q.remove(); |
434 |
|
shouldThrow(); |
435 |
< |
} catch (NoSuchElementException success) { |
436 |
< |
} |
435 |
> |
} catch (NoSuchElementException success) {} |
436 |
> |
checkEmpty(q); |
437 |
|
} |
438 |
|
|
439 |
|
/** |
440 |
|
* remove(x) removes x and returns true if present |
441 |
|
*/ |
442 |
< |
public void testRemoveElement() { |
442 |
> |
public void testRemoveElement() throws InterruptedException { |
443 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
444 |
|
for (int i = 1; i < SIZE; i += 2) { |
445 |
< |
assertTrue(q.remove(new Integer(i))); |
445 |
> |
assertTrue(q.remove(i)); |
446 |
|
} |
447 |
|
for (int i = 0; i < SIZE; i += 2) { |
448 |
< |
assertTrue(q.remove(new Integer(i))); |
449 |
< |
assertFalse(q.remove(new Integer(i + 1))); |
448 |
> |
assertTrue(q.remove(i)); |
449 |
> |
assertFalse(q.remove(i + 1)); |
450 |
|
} |
451 |
< |
assertTrue(q.isEmpty()); |
451 |
> |
checkEmpty(q); |
452 |
|
} |
453 |
|
|
454 |
|
/** |
455 |
|
* An add following remove(x) succeeds |
456 |
|
*/ |
457 |
< |
public void testRemoveElementAndAdd() { |
458 |
< |
try { |
459 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
460 |
< |
assertTrue(q.add(new Integer(1))); |
461 |
< |
assertTrue(q.add(new Integer(2))); |
462 |
< |
assertTrue(q.remove(new Integer(1))); |
463 |
< |
assertTrue(q.remove(new Integer(2))); |
464 |
< |
assertTrue(q.add(new Integer(3))); |
480 |
< |
assertTrue(q.take() != null); |
481 |
< |
} catch (Exception e) { |
482 |
< |
unexpectedException(); |
483 |
< |
} |
457 |
> |
public void testRemoveElementAndAdd() throws InterruptedException { |
458 |
> |
LinkedTransferQueue q = new LinkedTransferQueue(); |
459 |
> |
assertTrue(q.add(one)); |
460 |
> |
assertTrue(q.add(two)); |
461 |
> |
assertTrue(q.remove(one)); |
462 |
> |
assertTrue(q.remove(two)); |
463 |
> |
assertTrue(q.add(three)); |
464 |
> |
assertSame(q.take(), three); |
465 |
|
} |
466 |
|
|
467 |
|
/** |
468 |
|
* contains(x) reports true when elements added but not yet removed |
469 |
|
*/ |
470 |
|
public void testContains() { |
471 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
471 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
472 |
|
for (int i = 0; i < SIZE; ++i) { |
473 |
< |
assertTrue(q.contains(new Integer(i))); |
474 |
< |
q.poll(); |
475 |
< |
assertFalse(q.contains(new Integer(i))); |
473 |
> |
assertTrue(q.contains(i)); |
474 |
> |
assertEquals(i, (int) q.poll()); |
475 |
> |
assertFalse(q.contains(i)); |
476 |
|
} |
477 |
|
} |
478 |
|
|
479 |
|
/** |
480 |
|
* clear removes all elements |
481 |
|
*/ |
482 |
< |
public void testClear() { |
482 |
> |
public void testClear() throws InterruptedException { |
483 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
503 |
– |
int remainingCapacity = q.remainingCapacity(); |
484 |
|
q.clear(); |
485 |
< |
assertTrue(q.isEmpty()); |
486 |
< |
assertEquals(0, q.size()); |
507 |
< |
assertEquals(remainingCapacity, q.remainingCapacity()); |
485 |
> |
checkEmpty(q); |
486 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
487 |
|
q.add(one); |
488 |
|
assertFalse(q.isEmpty()); |
489 |
+ |
assertEquals(1, q.size()); |
490 |
|
assertTrue(q.contains(one)); |
491 |
|
q.clear(); |
492 |
< |
assertTrue(q.isEmpty()); |
492 |
> |
checkEmpty(q); |
493 |
|
} |
494 |
|
|
495 |
|
/** |
496 |
|
* containsAll(c) is true when c contains a subset of elements |
497 |
|
*/ |
498 |
|
public void testContainsAll() { |
499 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
500 |
< |
LinkedTransferQueue p = new LinkedTransferQueue(); |
499 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
500 |
> |
LinkedTransferQueue<Integer> p = new LinkedTransferQueue<Integer>(); |
501 |
|
for (int i = 0; i < SIZE; ++i) { |
502 |
|
assertTrue(q.containsAll(p)); |
503 |
|
assertFalse(p.containsAll(q)); |
504 |
< |
p.add(new Integer(i)); |
504 |
> |
p.add(i); |
505 |
|
} |
506 |
|
assertTrue(p.containsAll(q)); |
507 |
|
} |
508 |
|
|
509 |
|
/** |
510 |
< |
* retainAll(c) retains only those elements of c and reports true if changed |
510 |
> |
* retainAll(c) retains only those elements of c and reports true |
511 |
> |
* if changed |
512 |
|
*/ |
513 |
|
public void testRetainAll() { |
514 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
527 |
|
} |
528 |
|
|
529 |
|
/** |
530 |
< |
* removeAll(c) removes only those elements of c and reports true if changed |
530 |
> |
* removeAll(c) removes only those elements of c and reports true |
531 |
> |
* if changed |
532 |
|
*/ |
533 |
|
public void testRemoveAll() { |
534 |
|
for (int i = 1; i < SIZE; ++i) { |
537 |
|
assertTrue(q.removeAll(p)); |
538 |
|
assertEquals(SIZE - i, q.size()); |
539 |
|
for (int j = 0; j < i; ++j) { |
540 |
< |
Integer I = (Integer) (p.remove()); |
559 |
< |
assertFalse(q.contains(I)); |
540 |
> |
assertFalse(q.contains(p.remove())); |
541 |
|
} |
542 |
|
} |
543 |
|
} |
544 |
|
|
545 |
|
/** |
546 |
< |
* toArray contains all elements |
546 |
> |
* toArray() contains all elements |
547 |
|
*/ |
548 |
< |
public void testToArray() { |
548 |
> |
public void testToArray() throws InterruptedException { |
549 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
550 |
|
Object[] o = q.toArray(); |
551 |
< |
try { |
552 |
< |
for (int i = 0; i < o.length; i++) { |
572 |
< |
assertEquals(o[i], q.take()); |
573 |
< |
} |
574 |
< |
} catch (InterruptedException e) { |
575 |
< |
unexpectedException(); |
551 |
> |
for (int i = 0; i < o.length; i++) { |
552 |
> |
assertEquals(o[i], q.take()); |
553 |
|
} |
554 |
|
} |
555 |
|
|
556 |
|
/** |
557 |
|
* toArray(a) contains all elements |
558 |
|
*/ |
559 |
< |
public void testToArray2() { |
560 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
559 |
> |
public void testToArray2() throws InterruptedException { |
560 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
561 |
|
Integer[] ints = new Integer[SIZE]; |
562 |
< |
ints = (Integer[]) q.toArray(ints); |
563 |
< |
try { |
564 |
< |
for (int i = 0; i < ints.length; i++) { |
588 |
< |
assertEquals(ints[i], q.take()); |
589 |
< |
} |
590 |
< |
} catch (InterruptedException e) { |
591 |
< |
unexpectedException(); |
562 |
> |
ints = q.toArray(ints); |
563 |
> |
for (int i = 0; i < ints.length; i++) { |
564 |
> |
assertEquals(ints[i], q.take()); |
565 |
|
} |
566 |
|
} |
567 |
|
|
568 |
|
/** |
569 |
< |
* toArray(null) throws NPE |
569 |
> |
* toArray(null) throws NullPointerException |
570 |
|
*/ |
571 |
|
public void testToArray_BadArg() { |
572 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
573 |
|
try { |
600 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
574 |
|
Object o[] = q.toArray(null); |
575 |
|
shouldThrow(); |
576 |
< |
} catch (NullPointerException success) { |
604 |
< |
} |
576 |
> |
} catch (NullPointerException success) {} |
577 |
|
} |
578 |
|
|
579 |
|
/** |
580 |
< |
* toArray with incompatible array type throws CCE |
580 |
> |
* toArray(incompatible array type) throws CCE |
581 |
|
*/ |
582 |
|
public void testToArray1_BadArg() { |
583 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
584 |
|
try { |
612 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
585 |
|
Object o[] = q.toArray(new String[10]); |
586 |
|
shouldThrow(); |
587 |
< |
} catch (ArrayStoreException success) { |
616 |
< |
} |
587 |
> |
} catch (ArrayStoreException success) {} |
588 |
|
} |
589 |
|
|
590 |
|
/** |
591 |
|
* iterator iterates through all elements |
592 |
|
*/ |
593 |
< |
public void testIterator() { |
593 |
> |
public void testIterator() throws InterruptedException { |
594 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
595 |
|
Iterator it = q.iterator(); |
596 |
< |
try { |
597 |
< |
while (it.hasNext()) { |
598 |
< |
assertEquals(it.next(), q.take()); |
628 |
< |
} |
629 |
< |
} catch (InterruptedException e) { |
630 |
< |
unexpectedException(); |
596 |
> |
int i = 0; |
597 |
> |
while (it.hasNext()) { |
598 |
> |
assertEquals(it.next(), i++); |
599 |
|
} |
600 |
+ |
assertEquals(i, SIZE); |
601 |
|
} |
602 |
|
|
603 |
|
/** |
604 |
< |
* iterator.remove removes current element |
604 |
> |
* iterator.remove() removes current element |
605 |
|
*/ |
606 |
|
public void testIteratorRemove() { |
607 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
623 |
|
* iterator ordering is FIFO |
624 |
|
*/ |
625 |
|
public void testIteratorOrdering() { |
626 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
627 |
< |
int remainingCapacity = q.remainingCapacity(); |
626 |
> |
final LinkedTransferQueue<Integer> q |
627 |
> |
= new LinkedTransferQueue<Integer>(); |
628 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
629 |
|
q.add(one); |
630 |
|
q.add(two); |
631 |
|
q.add(three); |
632 |
< |
assertEquals(remainingCapacity, q.remainingCapacity()); |
632 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
633 |
|
int k = 0; |
634 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
635 |
< |
int i = ((Integer) (it.next())).intValue(); |
666 |
< |
assertEquals(++k, i); |
634 |
> |
for (Integer n : q) { |
635 |
> |
assertEquals(++k, (int) n); |
636 |
|
} |
637 |
|
assertEquals(3, k); |
638 |
|
} |
645 |
|
q.add(one); |
646 |
|
q.add(two); |
647 |
|
q.add(three); |
648 |
< |
try { |
649 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
650 |
< |
q.remove(); |
682 |
< |
it.next(); |
683 |
< |
} |
684 |
< |
} catch (ConcurrentModificationException e) { |
685 |
< |
unexpectedException(); |
648 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
649 |
> |
q.remove(); |
650 |
> |
it.next(); |
651 |
|
} |
652 |
|
assertEquals(0, q.size()); |
653 |
|
} |
671 |
|
q.add(one); |
672 |
|
q.add(two); |
673 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
709 |
– |
executor.execute(new Runnable() { |
674 |
|
|
675 |
< |
public void run() { |
676 |
< |
try { |
677 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
678 |
< |
} catch (Exception e) { |
679 |
< |
threadUnexpectedException(); |
680 |
< |
} |
681 |
< |
} |
682 |
< |
}); |
683 |
< |
|
684 |
< |
executor.execute(new Runnable() { |
721 |
< |
|
722 |
< |
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 |
< |
}); |
675 |
> |
executor.execute(new CheckedRunnable() { |
676 |
> |
public void realRun() { |
677 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
678 |
> |
}}); |
679 |
> |
|
680 |
> |
executor.execute(new CheckedRunnable() { |
681 |
> |
public void realRun() throws InterruptedException { |
682 |
> |
Thread.sleep(SMALL_DELAY_MS); |
683 |
> |
assertEquals(one, q.take()); |
684 |
> |
}}); |
685 |
|
|
686 |
|
joinPool(executor); |
687 |
|
} |
688 |
|
|
689 |
|
/** |
690 |
< |
* poll retrieves elements across Executor threads |
690 |
> |
* timed poll retrieves elements across Executor threads |
691 |
|
*/ |
692 |
|
public void testPollInExecutor() { |
693 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
694 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
741 |
– |
executor.execute(new Runnable() { |
742 |
– |
|
743 |
– |
public void run() { |
744 |
– |
threadAssertNull(q.poll()); |
745 |
– |
try { |
746 |
– |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
747 |
– |
threadAssertTrue(q.isEmpty()); |
748 |
– |
} catch (InterruptedException e) { |
749 |
– |
threadUnexpectedException(); |
750 |
– |
} |
751 |
– |
} |
752 |
– |
}); |
695 |
|
|
696 |
< |
executor.execute(new Runnable() { |
697 |
< |
|
698 |
< |
public void run() { |
699 |
< |
try { |
700 |
< |
Thread.sleep(SMALL_DELAY_MS); |
701 |
< |
q.put(one); |
702 |
< |
} catch (InterruptedException e) { |
703 |
< |
threadUnexpectedException(); |
704 |
< |
} |
705 |
< |
} |
706 |
< |
}); |
696 |
> |
executor.execute(new CheckedRunnable() { |
697 |
> |
public void realRun() throws InterruptedException { |
698 |
> |
assertNull(q.poll()); |
699 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
700 |
> |
assertTrue(q.isEmpty()); |
701 |
> |
}}); |
702 |
> |
|
703 |
> |
executor.execute(new CheckedRunnable() { |
704 |
> |
public void realRun() throws InterruptedException { |
705 |
> |
Thread.sleep(SMALL_DELAY_MS); |
706 |
> |
q.put(one); |
707 |
> |
}}); |
708 |
|
|
709 |
|
joinPool(executor); |
710 |
|
} |
712 |
|
/** |
713 |
|
* A deserialized serialized queue has same elements in same order |
714 |
|
*/ |
715 |
< |
public void testSerialization() { |
715 |
> |
public void testSerialization() throws Exception { |
716 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
717 |
|
|
718 |
< |
try { |
719 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
720 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
721 |
< |
out.writeObject(q); |
722 |
< |
out.close(); |
723 |
< |
|
724 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
725 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
726 |
< |
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
727 |
< |
|
728 |
< |
assertEquals(q.size(), r.size()); |
729 |
< |
while (!q.isEmpty()) { |
730 |
< |
assertEquals(q.remove(), r.remove()); |
731 |
< |
} |
732 |
< |
} catch (Exception e) { |
790 |
< |
unexpectedException(); |
718 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
719 |
> |
ObjectOutputStream out |
720 |
> |
= new ObjectOutputStream(new BufferedOutputStream(bout)); |
721 |
> |
out.writeObject(q); |
722 |
> |
out.close(); |
723 |
> |
|
724 |
> |
ByteArrayInputStream bin |
725 |
> |
= new ByteArrayInputStream(bout.toByteArray()); |
726 |
> |
ObjectInputStream in |
727 |
> |
= new ObjectInputStream(new BufferedInputStream(bin)); |
728 |
> |
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
729 |
> |
|
730 |
> |
assertEquals(q.size(), r.size()); |
731 |
> |
while (!q.isEmpty()) { |
732 |
> |
assertEquals(q.remove(), r.remove()); |
733 |
|
} |
734 |
|
} |
735 |
|
|
736 |
|
/** |
737 |
< |
* drainTo(null) throws NPE |
737 |
> |
* drainTo(null) throws NullPointerException |
738 |
|
*/ |
739 |
|
public void testDrainToNull() { |
740 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
741 |
|
try { |
742 |
|
q.drainTo(null); |
743 |
|
shouldThrow(); |
744 |
< |
} catch (NullPointerException success) { |
803 |
< |
} |
744 |
> |
} catch (NullPointerException success) {} |
745 |
|
} |
746 |
|
|
747 |
|
/** |
748 |
< |
* drainTo(this) throws IAE |
748 |
> |
* drainTo(this) throws IllegalArgumentException |
749 |
|
*/ |
750 |
|
public void testDrainToSelf() { |
751 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
752 |
|
try { |
753 |
|
q.drainTo(q); |
754 |
|
shouldThrow(); |
755 |
< |
} catch (IllegalArgumentException success) { |
815 |
< |
} |
755 |
> |
} catch (IllegalArgumentException success) {} |
756 |
|
} |
757 |
|
|
758 |
|
/** |
765 |
|
assertEquals(q.size(), 0); |
766 |
|
assertEquals(l.size(), SIZE); |
767 |
|
for (int i = 0; i < SIZE; ++i) { |
768 |
< |
assertEquals(l.get(i), new Integer(i)); |
768 |
> |
assertEquals(l.get(i), i); |
769 |
|
} |
770 |
|
q.add(zero); |
771 |
|
q.add(one); |
777 |
|
assertEquals(q.size(), 0); |
778 |
|
assertEquals(l.size(), 2); |
779 |
|
for (int i = 0; i < 2; ++i) { |
780 |
< |
assertEquals(l.get(i), new Integer(i)); |
780 |
> |
assertEquals(l.get(i), i); |
781 |
|
} |
782 |
|
} |
783 |
|
|
784 |
|
/** |
785 |
< |
* drainTo empties full queue, unblocking a waiting put. |
785 |
> |
* drainTo(c) empties full queue, unblocking a waiting put. |
786 |
|
*/ |
787 |
< |
public void testDrainToWithActivePut() { |
787 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
788 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
789 |
< |
Thread t = new Thread(new Runnable() { |
790 |
< |
|
791 |
< |
public void run() { |
792 |
< |
try { |
793 |
< |
q.put(new Integer(SIZE + 1)); |
794 |
< |
} catch (Exception ie) { |
795 |
< |
threadUnexpectedException(); |
796 |
< |
} |
797 |
< |
} |
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(); |
789 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
790 |
> |
public void realRun() { |
791 |
> |
q.put(SIZE + 1); |
792 |
> |
}}); |
793 |
> |
ArrayList l = new ArrayList(); |
794 |
> |
q.drainTo(l); |
795 |
> |
assertTrue(l.size() >= SIZE); |
796 |
> |
for (int i = 0; i < SIZE; ++i) { |
797 |
> |
assertEquals(l.get(i), i); |
798 |
|
} |
799 |
+ |
t.join(); |
800 |
+ |
assertTrue(q.size() + l.size() >= SIZE); |
801 |
|
} |
802 |
|
|
803 |
|
/** |
804 |
< |
* drainTo(null, n) throws NPE |
804 |
> |
* drainTo(null, n) throws NullPointerException |
805 |
|
*/ |
806 |
|
public void testDrainToNullN() { |
807 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
808 |
|
try { |
809 |
< |
q.drainTo(null, 0); |
809 |
> |
q.drainTo(null, SIZE); |
810 |
|
shouldThrow(); |
811 |
< |
} catch (NullPointerException success) { |
883 |
< |
} |
811 |
> |
} catch (NullPointerException success) {} |
812 |
|
} |
813 |
|
|
814 |
|
/** |
815 |
< |
* drainTo(this, n) throws IAE |
815 |
> |
* drainTo(this, n) throws IllegalArgumentException |
816 |
|
*/ |
817 |
|
public void testDrainToSelfN() { |
818 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
819 |
|
try { |
820 |
< |
q.drainTo(q, 0); |
820 |
> |
q.drainTo(q, SIZE); |
821 |
|
shouldThrow(); |
822 |
< |
} catch (IllegalArgumentException success) { |
895 |
< |
} |
822 |
> |
} catch (IllegalArgumentException success) {} |
823 |
|
} |
824 |
|
|
825 |
|
/** |
829 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
830 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
831 |
|
for (int j = 0; j < SIZE; j++) { |
832 |
< |
assertTrue(q.offer(new Integer(j))); |
832 |
> |
assertTrue(q.offer(j)); |
833 |
|
} |
834 |
|
ArrayList l = new ArrayList(); |
835 |
|
q.drainTo(l, i); |
837 |
|
assertEquals(l.size(), k); |
838 |
|
assertEquals(q.size(), SIZE - k); |
839 |
|
for (int j = 0; j < k; ++j) { |
840 |
< |
assertEquals(l.get(j), new Integer(j)); |
840 |
> |
assertEquals(l.get(j), j); |
841 |
|
} |
842 |
< |
while (q.poll() != null); |
842 |
> |
while (q.poll() != null) |
843 |
> |
; |
844 |
|
} |
845 |
|
} |
846 |
|
|
847 |
< |
/* |
848 |
< |
* poll and take should decrement the waiting consumer count |
847 |
> |
/** |
848 |
> |
* timed poll() or take() increments the waiting consumer count; |
849 |
> |
* offer(e) decrements the waiting consumer count |
850 |
|
*/ |
851 |
< |
public void testWaitingConsumer() { |
852 |
< |
try { |
853 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
854 |
< |
final ConsumerObserver waiting = new ConsumerObserver(); |
926 |
< |
new Thread(new Runnable() { |
851 |
> |
public void testWaitingConsumer() throws InterruptedException { |
852 |
> |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
853 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
854 |
> |
assertFalse(q.hasWaitingConsumer()); |
855 |
|
|
856 |
< |
public void run() { |
857 |
< |
try { |
858 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
859 |
< |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
860 |
< |
threadAssertTrue(q.offer(new Object())); |
861 |
< |
} catch (Exception ex) { |
862 |
< |
threadUnexpectedException(); |
863 |
< |
} |
856 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
857 |
> |
public void realRun() throws InterruptedException { |
858 |
> |
Thread.sleep(SMALL_DELAY_MS); |
859 |
> |
assertTrue(q.hasWaitingConsumer()); |
860 |
> |
assertEquals(q.getWaitingConsumerCount(), 1); |
861 |
> |
assertTrue(q.offer(one)); |
862 |
> |
assertFalse(q.hasWaitingConsumer()); |
863 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
864 |
> |
}}); |
865 |
|
|
866 |
< |
} |
867 |
< |
}).start(); |
868 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null); |
869 |
< |
assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers()); |
941 |
< |
} catch (Exception ex) { |
942 |
< |
this.unexpectedException(); |
943 |
< |
} |
866 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
867 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
868 |
> |
assertFalse(q.hasWaitingConsumer()); |
869 |
> |
t.join(); |
870 |
|
} |
945 |
– |
/* |
946 |
– |
* Inserts null into transfer throws NPE |
947 |
– |
*/ |
871 |
|
|
872 |
< |
public void testTransfer1() { |
872 |
> |
/** |
873 |
> |
* transfer(null) throws NullPointerException |
874 |
> |
*/ |
875 |
> |
public void testTransfer1() throws InterruptedException { |
876 |
|
try { |
877 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
878 |
|
q.transfer(null); |
879 |
|
shouldThrow(); |
880 |
< |
} catch (NullPointerException ex) { |
955 |
< |
} catch (Exception ex) { |
956 |
< |
this.unexpectedException(); |
957 |
< |
} |
880 |
> |
} catch (NullPointerException success) {} |
881 |
|
} |
882 |
|
|
883 |
< |
/* |
884 |
< |
* transfer attempts to insert into the queue then wait until that |
885 |
< |
* object is removed via take or poll. |
883 |
> |
/** |
884 |
> |
* transfer waits until a poll occurs. The transfered element |
885 |
> |
* is returned by this associated poll. |
886 |
|
*/ |
887 |
< |
public void testTransfer2() { |
888 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
889 |
< |
new Thread(new Runnable() { |
887 |
> |
public void testTransfer2() throws InterruptedException { |
888 |
> |
final LinkedTransferQueue<Integer> q |
889 |
> |
= new LinkedTransferQueue<Integer>(); |
890 |
|
|
891 |
< |
public void run() { |
892 |
< |
try { |
893 |
< |
q.transfer(new Integer(SIZE)); |
894 |
< |
threadAssertTrue(q.isEmpty()); |
895 |
< |
} catch (Exception ex) { |
973 |
< |
threadUnexpectedException(); |
974 |
< |
} |
975 |
< |
} |
976 |
< |
}).start(); |
891 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
892 |
> |
public void realRun() throws InterruptedException { |
893 |
> |
q.transfer(SIZE); |
894 |
> |
threadAssertTrue(q.isEmpty()); |
895 |
> |
}}); |
896 |
|
|
897 |
< |
try { |
898 |
< |
Thread.sleep(SHORT_DELAY_MS); |
899 |
< |
assertEquals(1, q.size()); |
900 |
< |
q.poll(); |
901 |
< |
assertTrue(q.isEmpty()); |
902 |
< |
} catch (Exception ex) { |
903 |
< |
this.unexpectedException(); |
904 |
< |
} |
905 |
< |
} |
906 |
< |
/* |
907 |
< |
* transfer will attempt to transfer in fifo order and continue waiting if |
908 |
< |
* the element being transfered is not polled or taken |
909 |
< |
*/ |
910 |
< |
|
911 |
< |
public void testTransfer3() { |
912 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
913 |
< |
new Thread(new Runnable() { |
914 |
< |
public void run() { |
915 |
< |
try { |
916 |
< |
Integer i; |
917 |
< |
q.transfer((i = new Integer(SIZE + 1))); |
918 |
< |
threadAssertTrue(!q.contains(i)); |
919 |
< |
threadAssertEquals(1, q.size()); |
920 |
< |
} catch (Exception ex) { |
921 |
< |
threadUnexpectedException(); |
922 |
< |
} |
923 |
< |
} |
924 |
< |
}).start(); |
925 |
< |
Thread interruptedThread = |
926 |
< |
new Thread(new Runnable() { |
927 |
< |
public void run() { |
928 |
< |
try { |
929 |
< |
q.transfer(new Integer(SIZE)); |
930 |
< |
threadShouldThrow(); |
931 |
< |
} catch (InterruptedException ex) { |
932 |
< |
} |
933 |
< |
} |
934 |
< |
}); |
935 |
< |
interruptedThread.start(); |
936 |
< |
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 |
< |
} |
897 |
> |
Thread.sleep(SHORT_DELAY_MS); |
898 |
> |
assertEquals(1, q.size()); |
899 |
> |
assertEquals(SIZE, (int) q.poll()); |
900 |
> |
assertTrue(q.isEmpty()); |
901 |
> |
t.join(); |
902 |
> |
} |
903 |
> |
|
904 |
> |
/** |
905 |
> |
* transfer waits until a poll occurs, and then transfers in fifo order |
906 |
> |
*/ |
907 |
> |
public void testTransfer3() throws InterruptedException { |
908 |
> |
final LinkedTransferQueue<Integer> q |
909 |
> |
= new LinkedTransferQueue<Integer>(); |
910 |
> |
|
911 |
> |
Thread first = newStartedThread(new CheckedRunnable() { |
912 |
> |
public void realRun() throws InterruptedException { |
913 |
> |
Integer i = SIZE + 1; |
914 |
> |
q.transfer(i); |
915 |
> |
threadAssertTrue(!q.contains(i)); |
916 |
> |
threadAssertEquals(1, q.size()); |
917 |
> |
}}); |
918 |
> |
|
919 |
> |
Thread interruptedThread = newStartedThread( |
920 |
> |
new CheckedInterruptedRunnable() { |
921 |
> |
public void realRun() throws InterruptedException { |
922 |
> |
while (q.size() == 0) |
923 |
> |
Thread.yield(); |
924 |
> |
q.transfer(SIZE); |
925 |
> |
}}); |
926 |
> |
|
927 |
> |
while (q.size() < 2) |
928 |
> |
Thread.yield(); |
929 |
> |
assertEquals(2, q.size()); |
930 |
> |
assertEquals(SIZE + 1, (int) q.poll()); |
931 |
> |
first.join(); |
932 |
> |
assertEquals(1, q.size()); |
933 |
> |
interruptedThread.interrupt(); |
934 |
> |
interruptedThread.join(); |
935 |
> |
assertEquals(0, q.size()); |
936 |
> |
assertTrue(q.isEmpty()); |
937 |
|
} |
938 |
|
|
939 |
|
/** |
940 |
< |
* transfer will wait as long as a poll or take occurs if one does occur |
941 |
< |
* the waiting is finished and the thread that tries to poll/take |
1032 |
< |
* wins in retrieving the element |
940 |
> |
* transfer waits until a poll occurs, at which point the polling |
941 |
> |
* thread returns the element |
942 |
|
*/ |
943 |
< |
public void testTransfer4() { |
943 |
> |
public void testTransfer4() throws InterruptedException { |
944 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1036 |
– |
new Thread(new Runnable() { |
945 |
|
|
946 |
< |
public void run() { |
947 |
< |
try { |
948 |
< |
q.transfer(new Integer(four)); |
949 |
< |
threadAssertFalse(q.contains(new Integer(four))); |
950 |
< |
threadAssertEquals(new Integer(three), q.poll()); |
951 |
< |
} catch (Exception ex) { |
952 |
< |
threadUnexpectedException(); |
953 |
< |
} |
954 |
< |
} |
955 |
< |
}).start(); |
956 |
< |
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 |
< |
} |
946 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
947 |
> |
public void realRun() throws InterruptedException { |
948 |
> |
q.transfer(four); |
949 |
> |
threadAssertFalse(q.contains(four)); |
950 |
> |
threadAssertEquals(three, q.poll()); |
951 |
> |
}}); |
952 |
> |
|
953 |
> |
Thread.sleep(SHORT_DELAY_MS); |
954 |
> |
assertTrue(q.offer(three)); |
955 |
> |
assertEquals(four, q.poll()); |
956 |
> |
t.join(); |
957 |
|
} |
958 |
< |
/* |
959 |
< |
* Insert null into trTransfer throws NPE |
958 |
> |
|
959 |
> |
/** |
960 |
> |
* transfer waits until a take occurs. The transfered element |
961 |
> |
* is returned by this associated take. |
962 |
|
*/ |
963 |
+ |
public void testTransfer5() throws InterruptedException { |
964 |
+ |
final LinkedTransferQueue<Integer> q |
965 |
+ |
= new LinkedTransferQueue<Integer>(); |
966 |
|
|
967 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
968 |
+ |
public void realRun() throws InterruptedException { |
969 |
+ |
q.transfer(SIZE); |
970 |
+ |
checkEmpty(q); |
971 |
+ |
}}); |
972 |
+ |
|
973 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
974 |
+ |
assertEquals(SIZE, (int) q.take()); |
975 |
+ |
checkEmpty(q); |
976 |
+ |
t.join(); |
977 |
+ |
} |
978 |
+ |
|
979 |
+ |
/** |
980 |
+ |
* tryTransfer(null) throws NullPointerException |
981 |
+ |
*/ |
982 |
|
public void testTryTransfer1() { |
983 |
|
try { |
984 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
985 |
|
q.tryTransfer(null); |
986 |
< |
this.shouldThrow(); |
987 |
< |
} catch (NullPointerException ex) { |
1066 |
< |
} catch (Exception ex) { |
1067 |
< |
this.unexpectedException(); |
1068 |
< |
} |
986 |
> |
shouldThrow(); |
987 |
> |
} catch (NullPointerException success) {} |
988 |
|
} |
1070 |
– |
/* |
1071 |
– |
* tryTransfer returns false and does not enqueue if there are no consumers |
1072 |
– |
* waiting to poll or take. |
1073 |
– |
*/ |
989 |
|
|
990 |
< |
public void testTryTransfer2() { |
991 |
< |
try { |
992 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
993 |
< |
assertFalse(q.tryTransfer(new Object())); |
994 |
< |
assertEquals(0, q.size()); |
995 |
< |
} catch (Exception ex) { |
996 |
< |
this.unexpectedException(); |
997 |
< |
} |
990 |
> |
/** |
991 |
> |
* tryTransfer returns false and does not enqueue if there are no |
992 |
> |
* consumers waiting to poll or take. |
993 |
> |
*/ |
994 |
> |
public void testTryTransfer2() throws InterruptedException { |
995 |
> |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
996 |
> |
assertFalse(q.tryTransfer(new Object())); |
997 |
> |
assertFalse(q.hasWaitingConsumer()); |
998 |
> |
checkEmpty(q); |
999 |
|
} |
1000 |
< |
/* |
1001 |
< |
* if there is a consumer waiting poll or take tryTransfer returns |
1002 |
< |
* true while enqueueing object |
1000 |
> |
|
1001 |
> |
/** |
1002 |
> |
* If there is a consumer waiting in timed poll, tryTransfer |
1003 |
> |
* returns true while successfully transfering object. |
1004 |
|
*/ |
1005 |
+ |
public void testTryTransfer3() throws InterruptedException { |
1006 |
+ |
final Object hotPotato = new Object(); |
1007 |
+ |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1008 |
|
|
1009 |
< |
public void testTryTransfer3() { |
1010 |
< |
try { |
1011 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1012 |
< |
new Thread(new Runnable() { |
1009 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1010 |
> |
public void realRun() { |
1011 |
> |
while (! q.hasWaitingConsumer()) |
1012 |
> |
Thread.yield(); |
1013 |
> |
threadAssertTrue(q.hasWaitingConsumer()); |
1014 |
> |
threadAssertTrue(q.isEmpty()); |
1015 |
> |
threadAssertTrue(q.size() == 0); |
1016 |
> |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1017 |
> |
}}); |
1018 |
> |
|
1019 |
> |
assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1020 |
> |
checkEmpty(q); |
1021 |
> |
t.join(); |
1022 |
> |
} |
1023 |
|
|
1024 |
< |
public void run() { |
1025 |
< |
try { |
1026 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
1027 |
< |
threadAssertTrue(q.tryTransfer(new Object())); |
1028 |
< |
} catch (Exception ex) { |
1029 |
< |
threadUnexpectedException(); |
1030 |
< |
} |
1024 |
> |
/** |
1025 |
> |
* If there is a consumer waiting in take, tryTransfer returns |
1026 |
> |
* true while successfully transfering object. |
1027 |
> |
*/ |
1028 |
> |
public void testTryTransfer4() throws InterruptedException { |
1029 |
> |
final Object hotPotato = new Object(); |
1030 |
> |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1031 |
|
|
1032 |
< |
} |
1033 |
< |
}).start(); |
1034 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null); |
1035 |
< |
assertTrue(q.isEmpty()); |
1036 |
< |
} catch (Exception ex) { |
1037 |
< |
this.unexpectedException(); |
1038 |
< |
} |
1032 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1033 |
> |
public void realRun() { |
1034 |
> |
while (! q.hasWaitingConsumer()) |
1035 |
> |
Thread.yield(); |
1036 |
> |
threadAssertTrue(q.hasWaitingConsumer()); |
1037 |
> |
threadAssertTrue(q.isEmpty()); |
1038 |
> |
threadAssertTrue(q.size() == 0); |
1039 |
> |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1040 |
> |
}}); |
1041 |
> |
|
1042 |
> |
assertSame(q.take(), hotPotato); |
1043 |
> |
checkEmpty(q); |
1044 |
> |
t.join(); |
1045 |
|
} |
1046 |
|
|
1047 |
< |
/* |
1048 |
< |
* tryTransfer waits the amount given if interrupted, show an |
1049 |
< |
* interrupted exception |
1047 |
> |
/** |
1048 |
> |
* tryTransfer waits the amount given if interrupted, and |
1049 |
> |
* throws interrupted exception |
1050 |
|
*/ |
1051 |
< |
public void testTryTransfer4() { |
1051 |
> |
public void testTryTransfer5() throws InterruptedException { |
1052 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1117 |
– |
Thread toInterrupt = new Thread(new Runnable() { |
1053 |
|
|
1054 |
< |
public void run() { |
1055 |
< |
try { |
1056 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1057 |
< |
threadShouldThrow(); |
1058 |
< |
} catch (InterruptedException ex) { |
1059 |
< |
} |
1060 |
< |
} |
1061 |
< |
}); |
1127 |
< |
try { |
1128 |
< |
toInterrupt.start(); |
1129 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1130 |
< |
toInterrupt.interrupt(); |
1131 |
< |
} catch (Exception ex) { |
1132 |
< |
this.unexpectedException(); |
1133 |
< |
} |
1054 |
> |
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
1055 |
> |
public void realRun() throws InterruptedException { |
1056 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1057 |
> |
}}); |
1058 |
> |
|
1059 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1060 |
> |
toInterrupt.interrupt(); |
1061 |
> |
toInterrupt.join(); |
1062 |
|
} |
1063 |
|
|
1064 |
< |
/* |
1064 |
> |
/** |
1065 |
|
* tryTransfer gives up after the timeout and return false |
1066 |
|
*/ |
1067 |
< |
public void testTryTransfer5() { |
1067 |
> |
public void testTryTransfer6() throws InterruptedException { |
1068 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1141 |
– |
try { |
1142 |
– |
new Thread(new Runnable() { |
1069 |
|
|
1070 |
< |
public void run() { |
1071 |
< |
try { |
1072 |
< |
threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1073 |
< |
} catch (InterruptedException ex) { |
1074 |
< |
threadUnexpectedException(); |
1075 |
< |
} |
1076 |
< |
} |
1077 |
< |
}).start(); |
1078 |
< |
Thread.sleep(LONG_DELAY_MS); |
1153 |
< |
assertTrue(q.isEmpty()); |
1154 |
< |
} catch (Exception ex) { |
1155 |
< |
this.unexpectedException(); |
1156 |
< |
} |
1070 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1071 |
> |
public void realRun() throws InterruptedException { |
1072 |
> |
assertFalse(q.tryTransfer(new Object(), |
1073 |
> |
SHORT_DELAY_MS, MILLISECONDS)); |
1074 |
> |
}}); |
1075 |
> |
|
1076 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1077 |
> |
checkEmpty(q); |
1078 |
> |
t.join(); |
1079 |
|
} |
1080 |
|
|
1081 |
< |
/* |
1081 |
> |
/** |
1082 |
|
* tryTransfer waits for any elements previously in to be removed |
1083 |
|
* before transfering to a poll or take |
1084 |
|
*/ |
1085 |
< |
public void testTryTransfer6() { |
1085 |
> |
public void testTryTransfer7() throws InterruptedException { |
1086 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1087 |
< |
q.offer(new Integer(four)); |
1166 |
< |
new Thread(new Runnable() { |
1087 |
> |
assertTrue(q.offer(four)); |
1088 |
|
|
1089 |
< |
public void run() { |
1090 |
< |
try { |
1091 |
< |
threadAssertTrue(q.tryTransfer(new Integer(five), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
1092 |
< |
threadAssertTrue(q.isEmpty()); |
1093 |
< |
} catch (InterruptedException ex) { |
1094 |
< |
threadUnexpectedException(); |
1095 |
< |
} |
1096 |
< |
} |
1097 |
< |
}).start(); |
1098 |
< |
try { |
1099 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1100 |
< |
assertEquals(2, q.size()); |
1180 |
< |
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 |
< |
} |
1089 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1090 |
> |
public void realRun() throws InterruptedException { |
1091 |
> |
assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); |
1092 |
> |
assertTrue(q.isEmpty()); |
1093 |
> |
}}); |
1094 |
> |
|
1095 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1096 |
> |
assertEquals(2, q.size()); |
1097 |
> |
assertEquals(four, q.poll()); |
1098 |
> |
assertEquals(five, q.poll()); |
1099 |
> |
checkEmpty(q); |
1100 |
> |
t.join(); |
1101 |
|
} |
1102 |
|
|
1103 |
< |
/* |
1104 |
< |
* tryTransfer attempts to enqueue into the q and fails returning false not |
1105 |
< |
* enqueueing and the successing poll is null |
1103 |
> |
/** |
1104 |
> |
* tryTransfer attempts to enqueue into the q and fails returning |
1105 |
> |
* false not enqueueing and the successive poll is null |
1106 |
|
*/ |
1107 |
< |
public void testTryTransfer7() { |
1107 |
> |
public void testTryTransfer8() throws InterruptedException { |
1108 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1109 |
< |
q.offer(new Integer(four)); |
1110 |
< |
new Thread(new Runnable() { |
1111 |
< |
|
1112 |
< |
public void run() { |
1113 |
< |
try { |
1114 |
< |
threadAssertFalse(q.tryTransfer(new Integer(five), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1115 |
< |
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 |
< |
} |
1109 |
> |
assertTrue(q.offer(four)); |
1110 |
> |
assertEquals(1, q.size()); |
1111 |
> |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1112 |
> |
assertEquals(1, q.size()); |
1113 |
> |
assertEquals(four, q.poll()); |
1114 |
> |
assertNull(q.poll()); |
1115 |
> |
checkEmpty(q); |
1116 |
|
} |
1117 |
|
|
1118 |
< |
private LinkedTransferQueue populatedQueue( |
1119 |
< |
int n) { |
1218 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
1118 |
> |
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1119 |
> |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
1120 |
|
assertTrue(q.isEmpty()); |
1121 |
< |
int remainingCapacity = q.remainingCapacity(); |
1122 |
< |
for (int i = 0; i < |
1222 |
< |
n; i++) { |
1121 |
> |
for (int i = 0; i < n; i++) { |
1122 |
> |
assertEquals(i, q.size()); |
1123 |
|
assertTrue(q.offer(i)); |
1124 |
+ |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
1125 |
|
} |
1225 |
– |
|
1126 |
|
assertFalse(q.isEmpty()); |
1227 |
– |
assertEquals(remainingCapacity, q.remainingCapacity()); |
1228 |
– |
assertEquals(n, q.size()); |
1127 |
|
return q; |
1128 |
|
} |
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 |
– |
} |
1129 |
|
} |