13 |
|
import java.io.ObjectOutputStream; |
14 |
|
import java.util.ArrayList; |
15 |
|
import java.util.Arrays; |
16 |
– |
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 |
+ |
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 being true |
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()); |
65 |
|
} |
66 |
|
|
67 |
|
/** |
68 |
< |
* Initizialing constructor with null collection throws NPE |
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) { |
49 |
< |
} |
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) { |
61 |
< |
} |
87 |
> |
} catch (NullPointerException success) {} |
88 |
|
} |
89 |
|
|
90 |
|
/** |
91 |
|
* Initializing constructor with a collection containing some null elements |
92 |
< |
* throws NPE |
92 |
> |
* throws NullPointerException |
93 |
|
*/ |
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) { |
77 |
< |
} |
102 |
> |
} catch (NullPointerException success) {} |
103 |
|
} |
104 |
|
|
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); |
102 |
< |
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) { |
124 |
< |
} |
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) { |
136 |
< |
} |
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) { |
148 |
< |
} |
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) { |
160 |
< |
} |
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) { |
173 |
< |
} |
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) { |
190 |
< |
} |
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) { |
207 |
< |
assertEquals(ints[i], q.poll()); |
208 |
< |
} |
209 |
< |
} 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) { |
222 |
< |
} catch (Exception ie) { |
223 |
< |
unexpectedException(); |
224 |
< |
} |
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); |
236 |
< |
assertTrue(q.contains(I)); |
237 |
< |
} |
238 |
< |
} catch (Exception ie) { |
239 |
< |
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) { |
250 |
< |
assertEquals(i, ((Integer) q.take()).intValue()); |
251 |
< |
} |
252 |
< |
} catch (InterruptedException e) { |
253 |
< |
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) { |
269 |
< |
} |
270 |
< |
} |
271 |
< |
}); |
272 |
< |
try { |
273 |
< |
t.start(); |
274 |
< |
Thread.sleep(SHORT_DELAY_MS); |
275 |
< |
t.interrupt(); |
276 |
< |
t.join(); |
277 |
< |
} catch (Exception e) { |
278 |
< |
unexpectedException(); |
279 |
< |
} |
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 |
|
|
282 |
|
/** |
283 |
|
* Take removes existing elements until empty, then blocks interruptibly |
284 |
|
*/ |
285 |
< |
public void testBlockingTake() { |
286 |
< |
Thread t = new Thread(new Runnable() { |
287 |
< |
|
288 |
< |
public void run() { |
289 |
< |
try { |
290 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
291 |
< |
for (int i = 0; i < SIZE; ++i) { |
292 |
< |
assertEquals(i, ((Integer) q.take()).intValue()); |
293 |
< |
} |
294 |
< |
q.take(); |
295 |
< |
threadShouldThrow(); |
296 |
< |
} 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(); |
304 |
< |
t.join(); |
305 |
< |
} catch (InterruptedException ie) { |
306 |
< |
unexpectedException(); |
307 |
< |
} |
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) { |
328 |
< |
assertEquals(i, ((Integer) q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
329 |
< |
} |
330 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
331 |
< |
} catch (InterruptedException e) { |
332 |
< |
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)); |
346 |
< |
} catch (InterruptedException e) { |
347 |
< |
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)); |
365 |
< |
} 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 |
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() { |
372 |
> |
Thread t = new Thread(new CheckedRunnable() { |
373 |
> |
void realRun() throws InterruptedException { |
374 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
375 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
376 |
|
try { |
377 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
378 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
379 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
380 |
< |
threadShouldThrow(); |
381 |
< |
} catch (InterruptedException success) { |
382 |
< |
} |
383 |
< |
} |
384 |
< |
}); |
385 |
< |
try { |
398 |
< |
t.start(); |
399 |
< |
Thread.sleep(SMALL_DELAY_MS); |
400 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
401 |
< |
t.interrupt(); |
402 |
< |
t.join(); |
403 |
< |
} catch (Exception e) { |
404 |
< |
unexpectedException(); |
405 |
< |
} |
377 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
378 |
> |
shouldThrow(); |
379 |
> |
} catch (InterruptedException success) {} |
380 |
> |
}}); |
381 |
> |
|
382 |
> |
Thread.sleep(SMALL_DELAY_MS); |
383 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
384 |
> |
t.interrupt(); |
385 |
> |
t.join(); |
386 |
|
} |
387 |
|
|
388 |
|
/** |
389 |
|
* peek returns next element, or null if empty |
390 |
|
*/ |
391 |
< |
public void testPeek() { |
392 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
391 |
> |
public void testPeek() throws InterruptedException { |
392 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
393 |
|
for (int i = 0; i < SIZE; ++i) { |
394 |
< |
assertEquals(i, ((Integer) q.peek()).intValue()); |
395 |
< |
q.poll(); |
394 |
> |
assertEquals(i, (int) q.peek()); |
395 |
> |
assertEquals(i, (int) q.poll()); |
396 |
|
assertTrue(q.peek() == null || |
397 |
< |
i != ((Integer) q.peek()).intValue()); |
397 |
> |
i != (int) q.peek()); |
398 |
|
} |
399 |
|
assertNull(q.peek()); |
400 |
+ |
checkEmpty(q); |
401 |
|
} |
402 |
|
|
403 |
|
/** |
404 |
< |
* element returns next element, or throws NSEE if empty |
404 |
> |
* element returns next element, or throws NoSuchElementException if empty |
405 |
|
*/ |
406 |
< |
public void testElement() { |
407 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
406 |
> |
public void testElement() throws InterruptedException { |
407 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
408 |
|
for (int i = 0; i < SIZE; ++i) { |
409 |
< |
assertEquals(i, ((Integer) q.element()).intValue()); |
410 |
< |
q.poll(); |
409 |
> |
assertEquals(i, (int) q.element()); |
410 |
> |
assertEquals(i, (int) q.poll()); |
411 |
|
} |
412 |
|
try { |
413 |
|
q.element(); |
414 |
|
shouldThrow(); |
415 |
< |
} catch (NoSuchElementException success) { |
416 |
< |
} |
415 |
> |
} catch (NoSuchElementException success) {} |
416 |
> |
checkEmpty(q); |
417 |
|
} |
418 |
|
|
419 |
|
/** |
420 |
< |
* remove removes next element, or throws NSEE if empty |
420 |
> |
* remove removes next element, or throws NoSuchElementException if empty |
421 |
|
*/ |
422 |
< |
public void testRemove() { |
423 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
422 |
> |
public void testRemove() throws InterruptedException { |
423 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
424 |
|
for (int i = 0; i < SIZE; ++i) { |
425 |
< |
assertEquals(i, ((Integer) q.remove()).intValue()); |
425 |
> |
assertEquals(i, (int) q.remove()); |
426 |
|
} |
427 |
|
try { |
428 |
|
q.remove(); |
429 |
|
shouldThrow(); |
430 |
< |
} catch (NoSuchElementException success) { |
431 |
< |
} |
430 |
> |
} catch (NoSuchElementException success) {} |
431 |
> |
checkEmpty(q); |
432 |
|
} |
433 |
|
|
434 |
|
/** |
435 |
|
* remove(x) removes x and returns true if present |
436 |
|
*/ |
437 |
< |
public void testRemoveElement() { |
437 |
> |
public void testRemoveElement() throws InterruptedException { |
438 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
439 |
|
for (int i = 1; i < SIZE; i += 2) { |
440 |
< |
assertTrue(q.remove(new Integer(i))); |
440 |
> |
assertTrue(q.remove(i)); |
441 |
|
} |
442 |
|
for (int i = 0; i < SIZE; i += 2) { |
443 |
< |
assertTrue(q.remove(new Integer(i))); |
444 |
< |
assertFalse(q.remove(new Integer(i + 1))); |
443 |
> |
assertTrue(q.remove(i)); |
444 |
> |
assertFalse(q.remove(i + 1)); |
445 |
|
} |
446 |
< |
assertTrue(q.isEmpty()); |
446 |
> |
checkEmpty(q); |
447 |
|
} |
448 |
|
|
449 |
|
/** |
450 |
|
* An add following remove(x) succeeds |
451 |
|
*/ |
452 |
< |
public void testRemoveElementAndAdd() { |
453 |
< |
try { |
454 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
455 |
< |
assertTrue(q.add(new Integer(1))); |
456 |
< |
assertTrue(q.add(new Integer(2))); |
457 |
< |
assertTrue(q.remove(new Integer(1))); |
458 |
< |
assertTrue(q.remove(new Integer(2))); |
459 |
< |
assertTrue(q.add(new Integer(3))); |
479 |
< |
assertTrue(q.take() != null); |
480 |
< |
} catch (Exception e) { |
481 |
< |
unexpectedException(); |
482 |
< |
} |
452 |
> |
public void testRemoveElementAndAdd() throws InterruptedException { |
453 |
> |
LinkedTransferQueue q = new LinkedTransferQueue(); |
454 |
> |
assertTrue(q.add(one)); |
455 |
> |
assertTrue(q.add(two)); |
456 |
> |
assertTrue(q.remove(one)); |
457 |
> |
assertTrue(q.remove(two)); |
458 |
> |
assertTrue(q.add(three)); |
459 |
> |
assertTrue(q.take() == three); |
460 |
|
} |
461 |
|
|
462 |
|
/** |
463 |
|
* contains(x) reports true when elements added but not yet removed |
464 |
|
*/ |
465 |
|
public void testContains() { |
466 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
466 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
467 |
|
for (int i = 0; i < SIZE; ++i) { |
468 |
< |
assertTrue(q.contains(new Integer(i))); |
469 |
< |
q.poll(); |
470 |
< |
assertFalse(q.contains(new Integer(i))); |
468 |
> |
assertTrue(q.contains(i)); |
469 |
> |
assertEquals(i, (int) q.poll()); |
470 |
> |
assertFalse(q.contains(i)); |
471 |
|
} |
472 |
|
} |
473 |
|
|
474 |
|
/** |
475 |
|
* clear removes all elements |
476 |
|
*/ |
477 |
< |
public void testClear() { |
477 |
> |
public void testClear() throws InterruptedException { |
478 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
502 |
– |
int remainingCapacity = q.remainingCapacity(); |
479 |
|
q.clear(); |
480 |
< |
assertTrue(q.isEmpty()); |
481 |
< |
assertEquals(0, q.size()); |
506 |
< |
assertEquals(remainingCapacity, q.remainingCapacity()); |
480 |
> |
checkEmpty(q); |
481 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
482 |
|
q.add(one); |
483 |
|
assertFalse(q.isEmpty()); |
484 |
+ |
assertEquals(1, q.size()); |
485 |
|
assertTrue(q.contains(one)); |
486 |
|
q.clear(); |
487 |
< |
assertTrue(q.isEmpty()); |
487 |
> |
checkEmpty(q); |
488 |
|
} |
489 |
|
|
490 |
|
/** |
491 |
|
* containsAll(c) is true when c contains a subset of elements |
492 |
|
*/ |
493 |
|
public void testContainsAll() { |
494 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
495 |
< |
LinkedTransferQueue p = new LinkedTransferQueue(); |
494 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
495 |
> |
LinkedTransferQueue<Integer> p = new LinkedTransferQueue<Integer>(); |
496 |
|
for (int i = 0; i < SIZE; ++i) { |
497 |
|
assertTrue(q.containsAll(p)); |
498 |
|
assertFalse(p.containsAll(q)); |
499 |
< |
p.add(new Integer(i)); |
499 |
> |
p.add(i); |
500 |
|
} |
501 |
|
assertTrue(p.containsAll(q)); |
502 |
|
} |
503 |
|
|
504 |
|
/** |
505 |
< |
* retainAll(c) retains only those elements of c and reports true if changed |
505 |
> |
* retainAll(c) retains only those elements of c and reports true |
506 |
> |
* if changed |
507 |
|
*/ |
508 |
|
public void testRetainAll() { |
509 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
522 |
|
} |
523 |
|
|
524 |
|
/** |
525 |
< |
* removeAll(c) removes only those elements of c and reports true if changed |
525 |
> |
* removeAll(c) removes only those elements of c and reports true |
526 |
> |
* if changed |
527 |
|
*/ |
528 |
|
public void testRemoveAll() { |
529 |
|
for (int i = 1; i < SIZE; ++i) { |
532 |
|
assertTrue(q.removeAll(p)); |
533 |
|
assertEquals(SIZE - i, q.size()); |
534 |
|
for (int j = 0; j < i; ++j) { |
535 |
< |
Integer I = (Integer) (p.remove()); |
558 |
< |
assertFalse(q.contains(I)); |
535 |
> |
assertFalse(q.contains(p.remove())); |
536 |
|
} |
537 |
|
} |
538 |
|
} |
539 |
|
|
540 |
|
/** |
541 |
< |
* toArray contains all elements |
541 |
> |
* toArray() contains all elements |
542 |
|
*/ |
543 |
< |
public void testToArray() { |
543 |
> |
public void testToArray() throws InterruptedException { |
544 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
545 |
|
Object[] o = q.toArray(); |
546 |
< |
try { |
547 |
< |
for (int i = 0; i < o.length; i++) { |
571 |
< |
assertEquals(o[i], q.take()); |
572 |
< |
} |
573 |
< |
} catch (InterruptedException e) { |
574 |
< |
unexpectedException(); |
546 |
> |
for (int i = 0; i < o.length; i++) { |
547 |
> |
assertEquals(o[i], q.take()); |
548 |
|
} |
549 |
|
} |
550 |
|
|
551 |
|
/** |
552 |
|
* toArray(a) contains all elements |
553 |
|
*/ |
554 |
< |
public void testToArray2() { |
555 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
554 |
> |
public void testToArray2() throws InterruptedException { |
555 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
556 |
|
Integer[] ints = new Integer[SIZE]; |
557 |
< |
ints = (Integer[]) q.toArray(ints); |
558 |
< |
try { |
559 |
< |
for (int i = 0; i < ints.length; i++) { |
587 |
< |
assertEquals(ints[i], q.take()); |
588 |
< |
} |
589 |
< |
} catch (InterruptedException e) { |
590 |
< |
unexpectedException(); |
557 |
> |
ints = q.toArray(ints); |
558 |
> |
for (int i = 0; i < ints.length; i++) { |
559 |
> |
assertEquals(ints[i], q.take()); |
560 |
|
} |
561 |
|
} |
562 |
|
|
563 |
|
/** |
564 |
< |
* toArray(null) throws NPE |
564 |
> |
* toArray(null) throws NullPointerException |
565 |
|
*/ |
566 |
|
public void testToArray_BadArg() { |
567 |
|
try { |
568 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
569 |
|
Object o[] = q.toArray(null); |
570 |
|
shouldThrow(); |
571 |
< |
} catch (NullPointerException success) { |
603 |
< |
} |
571 |
> |
} catch (NullPointerException success) {} |
572 |
|
} |
573 |
|
|
574 |
|
/** |
575 |
< |
* toArray with incompatible array type throws CCE |
575 |
> |
* toArray(incompatible array type) throws CCE |
576 |
|
*/ |
577 |
|
public void testToArray1_BadArg() { |
578 |
|
try { |
579 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
580 |
|
Object o[] = q.toArray(new String[10]); |
581 |
|
shouldThrow(); |
582 |
< |
} catch (ArrayStoreException success) { |
615 |
< |
} |
582 |
> |
} catch (ArrayStoreException success) {} |
583 |
|
} |
584 |
|
|
585 |
|
/** |
586 |
|
* iterator iterates through all elements |
587 |
|
*/ |
588 |
< |
public void testIterator() { |
588 |
> |
public void testIterator() throws InterruptedException { |
589 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
590 |
|
Iterator it = q.iterator(); |
591 |
< |
try { |
592 |
< |
while (it.hasNext()) { |
593 |
< |
assertEquals(it.next(), q.take()); |
627 |
< |
} |
628 |
< |
} catch (InterruptedException e) { |
629 |
< |
unexpectedException(); |
591 |
> |
int i = 0; |
592 |
> |
while (it.hasNext()) { |
593 |
> |
assertEquals(it.next(), i++); |
594 |
|
} |
595 |
+ |
assertEquals(i, SIZE); |
596 |
|
} |
597 |
|
|
598 |
|
/** |
599 |
< |
* iterator.remove removes current element |
599 |
> |
* iterator.remove() removes current element |
600 |
|
*/ |
601 |
|
public void testIteratorRemove() { |
602 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
618 |
|
* iterator ordering is FIFO |
619 |
|
*/ |
620 |
|
public void testIteratorOrdering() { |
621 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
622 |
< |
int remainingCapacity = q.remainingCapacity(); |
621 |
> |
final LinkedTransferQueue<Integer> q |
622 |
> |
= new LinkedTransferQueue<Integer>(); |
623 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
624 |
|
q.add(one); |
625 |
|
q.add(two); |
626 |
|
q.add(three); |
627 |
< |
assertEquals(remainingCapacity, q.remainingCapacity()); |
627 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
628 |
|
int k = 0; |
629 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
630 |
< |
int i = ((Integer) (it.next())).intValue(); |
665 |
< |
assertEquals(++k, i); |
629 |
> |
for (Integer n : q) { |
630 |
> |
assertEquals(++k, (int) n); |
631 |
|
} |
632 |
|
assertEquals(3, k); |
633 |
|
} |
640 |
|
q.add(one); |
641 |
|
q.add(two); |
642 |
|
q.add(three); |
643 |
< |
try { |
644 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
645 |
< |
q.remove(); |
681 |
< |
it.next(); |
682 |
< |
} |
683 |
< |
} catch (ConcurrentModificationException e) { |
684 |
< |
unexpectedException(); |
643 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
644 |
> |
q.remove(); |
645 |
> |
it.next(); |
646 |
|
} |
647 |
|
assertEquals(0, q.size()); |
648 |
|
} |
666 |
|
q.add(one); |
667 |
|
q.add(two); |
668 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
708 |
– |
executor.execute(new Runnable() { |
669 |
|
|
670 |
< |
public void run() { |
671 |
< |
try { |
672 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
673 |
< |
} catch (Exception e) { |
674 |
< |
threadUnexpectedException(); |
675 |
< |
} |
676 |
< |
} |
677 |
< |
}); |
678 |
< |
|
679 |
< |
executor.execute(new Runnable() { |
680 |
< |
|
721 |
< |
public void run() { |
722 |
< |
try { |
723 |
< |
Thread.sleep(SMALL_DELAY_MS); |
724 |
< |
threadAssertEquals(one, q.take()); |
725 |
< |
} catch (InterruptedException e) { |
726 |
< |
threadUnexpectedException(); |
727 |
< |
} |
728 |
< |
} |
729 |
< |
}); |
670 |
> |
executor.execute(new CheckedRunnable() { |
671 |
> |
void realRun() { |
672 |
> |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, |
673 |
> |
MILLISECONDS)); |
674 |
> |
}}); |
675 |
> |
|
676 |
> |
executor.execute(new CheckedRunnable() { |
677 |
> |
void realRun() throws InterruptedException { |
678 |
> |
Thread.sleep(SMALL_DELAY_MS); |
679 |
> |
threadAssertEquals(one, q.take()); |
680 |
> |
}}); |
681 |
|
|
682 |
|
joinPool(executor); |
683 |
|
} |
684 |
|
|
685 |
|
/** |
686 |
< |
* poll retrieves elements across Executor threads |
686 |
> |
* timed poll retrieves elements across Executor threads |
687 |
|
*/ |
688 |
|
public void testPollInExecutor() { |
689 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
690 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
740 |
– |
executor.execute(new Runnable() { |
691 |
|
|
692 |
< |
public void run() { |
692 |
> |
executor.execute(new CheckedRunnable() { |
693 |
> |
void realRun() throws InterruptedException { |
694 |
|
threadAssertNull(q.poll()); |
695 |
< |
try { |
696 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
697 |
< |
threadAssertTrue(q.isEmpty()); |
698 |
< |
} catch (InterruptedException e) { |
699 |
< |
threadUnexpectedException(); |
700 |
< |
} |
701 |
< |
} |
702 |
< |
}); |
703 |
< |
|
704 |
< |
executor.execute(new Runnable() { |
754 |
< |
|
755 |
< |
public void run() { |
756 |
< |
try { |
757 |
< |
Thread.sleep(SMALL_DELAY_MS); |
758 |
< |
q.put(one); |
759 |
< |
} catch (InterruptedException e) { |
760 |
< |
threadUnexpectedException(); |
761 |
< |
} |
762 |
< |
} |
763 |
< |
}); |
695 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, |
696 |
> |
MILLISECONDS)); |
697 |
> |
threadAssertTrue(q.isEmpty()); |
698 |
> |
}}); |
699 |
> |
|
700 |
> |
executor.execute(new CheckedRunnable() { |
701 |
> |
void realRun() throws InterruptedException { |
702 |
> |
Thread.sleep(SMALL_DELAY_MS); |
703 |
> |
q.put(one); |
704 |
> |
}}); |
705 |
|
|
706 |
|
joinPool(executor); |
707 |
|
} |
709 |
|
/** |
710 |
|
* A deserialized serialized queue has same elements in same order |
711 |
|
*/ |
712 |
< |
public void testSerialization() { |
712 |
> |
public void testSerialization() throws Exception { |
713 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
714 |
|
|
715 |
< |
try { |
716 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
717 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
718 |
< |
out.writeObject(q); |
719 |
< |
out.close(); |
720 |
< |
|
721 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
722 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
723 |
< |
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
724 |
< |
|
725 |
< |
assertEquals(q.size(), r.size()); |
726 |
< |
while (!q.isEmpty()) { |
727 |
< |
assertEquals(q.remove(), r.remove()); |
728 |
< |
} |
729 |
< |
} catch (Exception e) { |
789 |
< |
unexpectedException(); |
715 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
716 |
> |
ObjectOutputStream out |
717 |
> |
= new ObjectOutputStream(new BufferedOutputStream(bout)); |
718 |
> |
out.writeObject(q); |
719 |
> |
out.close(); |
720 |
> |
|
721 |
> |
ByteArrayInputStream bin |
722 |
> |
= new ByteArrayInputStream(bout.toByteArray()); |
723 |
> |
ObjectInputStream in |
724 |
> |
= new ObjectInputStream(new BufferedInputStream(bin)); |
725 |
> |
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
726 |
> |
|
727 |
> |
assertEquals(q.size(), r.size()); |
728 |
> |
while (!q.isEmpty()) { |
729 |
> |
assertEquals(q.remove(), r.remove()); |
730 |
|
} |
731 |
|
} |
732 |
|
|
733 |
|
/** |
734 |
< |
* drainTo(null) throws NPE |
734 |
> |
* drainTo(null) throws NullPointerException |
735 |
|
*/ |
736 |
|
public void testDrainToNull() { |
737 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
738 |
|
try { |
739 |
|
q.drainTo(null); |
740 |
|
shouldThrow(); |
741 |
< |
} catch (NullPointerException success) { |
802 |
< |
} |
741 |
> |
} catch (NullPointerException success) {} |
742 |
|
} |
743 |
|
|
744 |
|
/** |
745 |
< |
* drainTo(this) throws IAE |
745 |
> |
* drainTo(this) throws IllegalArgumentException |
746 |
|
*/ |
747 |
|
public void testDrainToSelf() { |
748 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
749 |
|
try { |
750 |
|
q.drainTo(q); |
751 |
|
shouldThrow(); |
752 |
< |
} catch (IllegalArgumentException success) { |
814 |
< |
} |
752 |
> |
} catch (IllegalArgumentException success) {} |
753 |
|
} |
754 |
|
|
755 |
|
/** |
762 |
|
assertEquals(q.size(), 0); |
763 |
|
assertEquals(l.size(), SIZE); |
764 |
|
for (int i = 0; i < SIZE; ++i) { |
765 |
< |
assertEquals(l.get(i), new Integer(i)); |
765 |
> |
assertEquals(l.get(i), i); |
766 |
|
} |
767 |
|
q.add(zero); |
768 |
|
q.add(one); |
774 |
|
assertEquals(q.size(), 0); |
775 |
|
assertEquals(l.size(), 2); |
776 |
|
for (int i = 0; i < 2; ++i) { |
777 |
< |
assertEquals(l.get(i), new Integer(i)); |
777 |
> |
assertEquals(l.get(i), i); |
778 |
|
} |
779 |
|
} |
780 |
|
|
781 |
|
/** |
782 |
< |
* drainTo empties full queue, unblocking a waiting put. |
782 |
> |
* drainTo(c) empties full queue, unblocking a waiting put. |
783 |
|
*/ |
784 |
< |
public void testDrainToWithActivePut() { |
784 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
785 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
786 |
< |
Thread t = new Thread(new Runnable() { |
787 |
< |
|
788 |
< |
public void run() { |
789 |
< |
try { |
790 |
< |
q.put(new Integer(SIZE + 1)); |
791 |
< |
} catch (Exception ie) { |
792 |
< |
threadUnexpectedException(); |
793 |
< |
} |
794 |
< |
} |
857 |
< |
}); |
858 |
< |
try { |
859 |
< |
t.start(); |
860 |
< |
ArrayList l = new ArrayList(); |
861 |
< |
q.drainTo(l); |
862 |
< |
assertTrue(l.size() >= SIZE); |
863 |
< |
for (int i = 0; i < SIZE; ++i) { |
864 |
< |
assertEquals(l.get(i), new Integer(i)); |
865 |
< |
} |
866 |
< |
t.join(); |
867 |
< |
assertTrue(q.size() + l.size() >= SIZE); |
868 |
< |
} catch (Exception e) { |
869 |
< |
unexpectedException(); |
786 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
787 |
> |
void realRun() { |
788 |
> |
q.put(SIZE + 1); |
789 |
> |
}}); |
790 |
> |
ArrayList l = new ArrayList(); |
791 |
> |
q.drainTo(l); |
792 |
> |
assertTrue(l.size() >= SIZE); |
793 |
> |
for (int i = 0; i < SIZE; ++i) { |
794 |
> |
assertEquals(l.get(i), i); |
795 |
|
} |
796 |
+ |
t.join(); |
797 |
+ |
assertTrue(q.size() + l.size() >= SIZE); |
798 |
|
} |
799 |
|
|
800 |
|
/** |
801 |
< |
* drainTo(null, n) throws NPE |
801 |
> |
* drainTo(null, n) throws NullPointerException |
802 |
|
*/ |
803 |
|
public void testDrainToNullN() { |
804 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
805 |
|
try { |
806 |
< |
q.drainTo(null, 0); |
806 |
> |
q.drainTo(null, SIZE); |
807 |
|
shouldThrow(); |
808 |
< |
} catch (NullPointerException success) { |
882 |
< |
} |
808 |
> |
} catch (NullPointerException success) {} |
809 |
|
} |
810 |
|
|
811 |
|
/** |
812 |
< |
* drainTo(this, n) throws IAE |
812 |
> |
* drainTo(this, n) throws IllegalArgumentException |
813 |
|
*/ |
814 |
|
public void testDrainToSelfN() { |
815 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
816 |
|
try { |
817 |
< |
q.drainTo(q, 0); |
817 |
> |
q.drainTo(q, SIZE); |
818 |
|
shouldThrow(); |
819 |
< |
} catch (IllegalArgumentException success) { |
894 |
< |
} |
819 |
> |
} catch (IllegalArgumentException success) {} |
820 |
|
} |
821 |
|
|
822 |
|
/** |
826 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
827 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
828 |
|
for (int j = 0; j < SIZE; j++) { |
829 |
< |
assertTrue(q.offer(new Integer(j))); |
829 |
> |
assertTrue(q.offer(j)); |
830 |
|
} |
831 |
|
ArrayList l = new ArrayList(); |
832 |
|
q.drainTo(l, i); |
834 |
|
assertEquals(l.size(), k); |
835 |
|
assertEquals(q.size(), SIZE - k); |
836 |
|
for (int j = 0; j < k; ++j) { |
837 |
< |
assertEquals(l.get(j), new Integer(j)); |
837 |
> |
assertEquals(l.get(j), j); |
838 |
|
} |
839 |
< |
while (q.poll() != null); |
839 |
> |
while (q.poll() != null) |
840 |
> |
; |
841 |
|
} |
842 |
|
} |
843 |
|
|
844 |
|
/** |
845 |
< |
* poll and take should decrement the waiting consumer count |
845 |
> |
* timed poll() or take() increments the waiting consumer count; |
846 |
> |
* offer(e) decrements the waiting consumer count |
847 |
|
*/ |
848 |
< |
public void testWaitingConsumer() { |
849 |
< |
try { |
850 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
851 |
< |
final ConsumerObserver waiting = new ConsumerObserver(); |
925 |
< |
new Thread(new Runnable() { |
848 |
> |
public void testWaitingConsumer() throws InterruptedException { |
849 |
> |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
850 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
851 |
> |
assertFalse(q.hasWaitingConsumer()); |
852 |
|
|
853 |
< |
public void run() { |
854 |
< |
try { |
855 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
856 |
< |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
857 |
< |
threadAssertTrue(q.offer(new Object())); |
858 |
< |
} catch (Exception ex) { |
859 |
< |
threadUnexpectedException(); |
860 |
< |
} |
853 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
854 |
> |
void realRun() throws InterruptedException { |
855 |
> |
Thread.sleep(SMALL_DELAY_MS); |
856 |
> |
threadAssertTrue(q.hasWaitingConsumer()); |
857 |
> |
threadAssertEquals(q.getWaitingConsumerCount(), 1); |
858 |
> |
threadAssertTrue(q.offer(new Object())); |
859 |
> |
threadAssertFalse(q.hasWaitingConsumer()); |
860 |
> |
threadAssertEquals(q.getWaitingConsumerCount(), 0); |
861 |
> |
}}); |
862 |
|
|
863 |
< |
} |
864 |
< |
}).start(); |
865 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null); |
866 |
< |
assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers()); |
940 |
< |
} catch (Exception ex) { |
941 |
< |
this.unexpectedException(); |
942 |
< |
} |
863 |
> |
assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); |
864 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
865 |
> |
assertFalse(q.hasWaitingConsumer()); |
866 |
> |
t.join(); |
867 |
|
} |
868 |
|
|
869 |
|
/** |
870 |
< |
* Inserts null into transfer throws NPE |
870 |
> |
* transfer(null) throws NullPointerException |
871 |
|
*/ |
872 |
< |
public void testTransfer1() { |
872 |
> |
public void testTransfer1() throws InterruptedException { |
873 |
|
try { |
874 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
875 |
|
q.transfer(null); |
876 |
|
shouldThrow(); |
877 |
< |
} catch (NullPointerException ex) { |
954 |
< |
} catch (Exception ex) { |
955 |
< |
this.unexpectedException(); |
956 |
< |
} |
877 |
> |
} catch (NullPointerException success) {} |
878 |
|
} |
879 |
|
|
880 |
|
/** |
881 |
< |
* transfer attempts to insert into the queue then wait until that |
882 |
< |
* object is removed via take or poll. |
881 |
> |
* transfer waits until a poll occurs. The transfered element |
882 |
> |
* is returned by this associated poll. |
883 |
|
*/ |
884 |
< |
public void testTransfer2() { |
885 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
886 |
< |
new Thread(new Runnable() { |
884 |
> |
public void testTransfer2() throws InterruptedException { |
885 |
> |
final LinkedTransferQueue<Integer> q |
886 |
> |
= new LinkedTransferQueue<Integer>(); |
887 |
|
|
888 |
< |
public void run() { |
889 |
< |
try { |
890 |
< |
q.transfer(new Integer(SIZE)); |
891 |
< |
threadAssertTrue(q.isEmpty()); |
892 |
< |
} catch (Exception ex) { |
972 |
< |
threadUnexpectedException(); |
973 |
< |
} |
974 |
< |
} |
975 |
< |
}).start(); |
888 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
889 |
> |
void realRun() throws InterruptedException { |
890 |
> |
q.transfer(SIZE); |
891 |
> |
threadAssertTrue(q.isEmpty()); |
892 |
> |
}}); |
893 |
|
|
894 |
< |
try { |
895 |
< |
Thread.sleep(SHORT_DELAY_MS); |
896 |
< |
assertEquals(1, q.size()); |
897 |
< |
q.poll(); |
898 |
< |
assertTrue(q.isEmpty()); |
982 |
< |
} catch (Exception ex) { |
983 |
< |
this.unexpectedException(); |
984 |
< |
} |
894 |
> |
Thread.sleep(SHORT_DELAY_MS); |
895 |
> |
assertEquals(1, q.size()); |
896 |
> |
assertEquals(SIZE, (int) q.poll()); |
897 |
> |
assertTrue(q.isEmpty()); |
898 |
> |
t.join(); |
899 |
|
} |
900 |
|
|
901 |
|
/** |
902 |
< |
* transfer will attempt to transfer in fifo order and continue |
903 |
< |
* waiting if the element being transfered is not polled or taken |
904 |
< |
*/ |
905 |
< |
public void testTransfer3() { |
906 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
907 |
< |
new Thread(new Runnable() { |
908 |
< |
public void run() { |
909 |
< |
try { |
910 |
< |
Integer i; |
911 |
< |
q.transfer((i = new Integer(SIZE + 1))); |
912 |
< |
threadAssertTrue(!q.contains(i)); |
913 |
< |
threadAssertEquals(1, q.size()); |
914 |
< |
} catch (Exception ex) { |
915 |
< |
threadUnexpectedException(); |
916 |
< |
} |
917 |
< |
} |
918 |
< |
}).start(); |
919 |
< |
Thread interruptedThread = |
920 |
< |
new Thread(new Runnable() { |
921 |
< |
public void run() { |
922 |
< |
try { |
923 |
< |
q.transfer(new Integer(SIZE)); |
924 |
< |
threadShouldThrow(); |
925 |
< |
} catch (InterruptedException ex) { |
926 |
< |
} |
927 |
< |
} |
928 |
< |
}); |
929 |
< |
interruptedThread.start(); |
930 |
< |
try { |
931 |
< |
Thread.sleep(LONG_DELAY_MS); |
932 |
< |
assertEquals(2, q.size()); |
933 |
< |
q.poll(); |
1020 |
< |
Thread.sleep(LONG_DELAY_MS); |
1021 |
< |
interruptedThread.interrupt(); |
1022 |
< |
assertEquals(1, q.size()); |
1023 |
< |
} catch (Exception ex) { |
1024 |
< |
this.unexpectedException(); |
1025 |
< |
} |
902 |
> |
* transfer waits until a poll occurs, and then transfers in fifo order |
903 |
> |
*/ |
904 |
> |
public void testTransfer3() throws InterruptedException { |
905 |
> |
final LinkedTransferQueue<Integer> q |
906 |
> |
= new LinkedTransferQueue<Integer>(); |
907 |
> |
|
908 |
> |
Thread first = newStartedThread(new CheckedRunnable() { |
909 |
> |
void realRun() throws InterruptedException { |
910 |
> |
Integer i = SIZE + 1; |
911 |
> |
q.transfer(i); |
912 |
> |
threadAssertTrue(!q.contains(i)); |
913 |
> |
threadAssertEquals(1, q.size()); |
914 |
> |
}}); |
915 |
> |
|
916 |
> |
Thread interruptedThread = newStartedThread( |
917 |
> |
new CheckedInterruptedRunnable() { |
918 |
> |
void realRun() throws InterruptedException { |
919 |
> |
while (q.size() == 0) |
920 |
> |
Thread.yield(); |
921 |
> |
q.transfer(SIZE); |
922 |
> |
}}); |
923 |
> |
|
924 |
> |
while (q.size() < 2) |
925 |
> |
Thread.yield(); |
926 |
> |
assertEquals(2, q.size()); |
927 |
> |
assertEquals(SIZE + 1, (int) q.poll()); |
928 |
> |
first.join(); |
929 |
> |
assertEquals(1, q.size()); |
930 |
> |
interruptedThread.interrupt(); |
931 |
> |
interruptedThread.join(); |
932 |
> |
assertEquals(0, q.size()); |
933 |
> |
assertTrue(q.isEmpty()); |
934 |
|
} |
935 |
|
|
936 |
|
/** |
937 |
< |
* transfer will wait as long as a poll or take occurs if one does occur |
938 |
< |
* the waiting is finished and the thread that tries to poll/take |
1031 |
< |
* wins in retrieving the element |
937 |
> |
* transfer waits until a poll occurs, at which point the polling |
938 |
> |
* thread returns the element |
939 |
|
*/ |
940 |
< |
public void testTransfer4() { |
940 |
> |
public void testTransfer4() throws InterruptedException { |
941 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1035 |
– |
new Thread(new Runnable() { |
942 |
|
|
943 |
< |
public void run() { |
944 |
< |
try { |
945 |
< |
q.transfer(new Integer(four)); |
946 |
< |
threadAssertFalse(q.contains(new Integer(four))); |
947 |
< |
threadAssertEquals(new Integer(three), q.poll()); |
948 |
< |
} catch (Exception ex) { |
949 |
< |
threadUnexpectedException(); |
950 |
< |
} |
951 |
< |
} |
952 |
< |
}).start(); |
953 |
< |
try { |
954 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
955 |
< |
assertTrue(q.offer(three)); |
956 |
< |
assertEquals(new Integer(four), q.poll()); |
957 |
< |
} catch (Exception ex) { |
958 |
< |
this.unexpectedException(); |
959 |
< |
} |
943 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
944 |
> |
void realRun() throws InterruptedException { |
945 |
> |
q.transfer(four); |
946 |
> |
threadAssertFalse(q.contains(four)); |
947 |
> |
threadAssertEquals(three, q.poll()); |
948 |
> |
}}); |
949 |
> |
|
950 |
> |
Thread.sleep(SHORT_DELAY_MS); |
951 |
> |
assertTrue(q.offer(three)); |
952 |
> |
assertEquals(four, q.poll()); |
953 |
> |
t.join(); |
954 |
> |
} |
955 |
> |
|
956 |
> |
/** |
957 |
> |
* transfer waits until a take occurs. The transfered element |
958 |
> |
* is returned by this associated take. |
959 |
> |
*/ |
960 |
> |
public void testTransfer5() throws InterruptedException { |
961 |
> |
final LinkedTransferQueue<Integer> q |
962 |
> |
= new LinkedTransferQueue<Integer>(); |
963 |
> |
|
964 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
965 |
> |
void realRun() throws InterruptedException { |
966 |
> |
q.transfer(SIZE); |
967 |
> |
checkEmpty(q); |
968 |
> |
}}); |
969 |
> |
|
970 |
> |
Thread.sleep(SHORT_DELAY_MS); |
971 |
> |
assertEquals(SIZE, (int) q.take()); |
972 |
> |
checkEmpty(q); |
973 |
> |
t.join(); |
974 |
|
} |
975 |
|
|
976 |
|
/** |
977 |
< |
* Insert null into tryTransfer throws NPE |
977 |
> |
* tryTransfer(null) throws NullPointerException |
978 |
|
*/ |
979 |
|
public void testTryTransfer1() { |
980 |
|
try { |
981 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
982 |
|
q.tryTransfer(null); |
983 |
< |
this.shouldThrow(); |
984 |
< |
} catch (NullPointerException ex) { |
1065 |
< |
} catch (Exception ex) { |
1066 |
< |
this.unexpectedException(); |
1067 |
< |
} |
983 |
> |
shouldThrow(); |
984 |
> |
} catch (NullPointerException success) {} |
985 |
|
} |
986 |
|
|
987 |
|
/** |
988 |
< |
* tryTransfer returns false and does not enqueue if there are no consumers |
989 |
< |
* waiting to poll or take. |
988 |
> |
* tryTransfer returns false and does not enqueue if there are no |
989 |
> |
* consumers waiting to poll or take. |
990 |
|
*/ |
991 |
< |
public void testTryTransfer2() { |
992 |
< |
try { |
993 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
994 |
< |
assertFalse(q.tryTransfer(new Object())); |
995 |
< |
assertEquals(0, q.size()); |
1079 |
< |
} catch (Exception ex) { |
1080 |
< |
this.unexpectedException(); |
1081 |
< |
} |
991 |
> |
public void testTryTransfer2() throws InterruptedException { |
992 |
> |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
993 |
> |
assertFalse(q.tryTransfer(new Object())); |
994 |
> |
assertFalse(q.hasWaitingConsumer()); |
995 |
> |
checkEmpty(q); |
996 |
|
} |
997 |
|
|
998 |
|
/** |
999 |
< |
* if there is a consumer waiting poll or take tryTransfer returns |
1000 |
< |
* true while enqueueing object |
999 |
> |
* If there is a consumer waiting in timed poll, tryTransfer |
1000 |
> |
* returns true while successfully transfering object. |
1001 |
|
*/ |
1002 |
< |
public void testTryTransfer3() { |
1003 |
< |
try { |
1004 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1091 |
< |
new Thread(new Runnable() { |
1002 |
> |
public void testTryTransfer3() throws InterruptedException { |
1003 |
> |
final Object hotPotato = new Object(); |
1004 |
> |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1005 |
|
|
1006 |
< |
public void run() { |
1007 |
< |
try { |
1008 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
1009 |
< |
threadAssertTrue(q.tryTransfer(new Object())); |
1010 |
< |
} catch (Exception ex) { |
1011 |
< |
threadUnexpectedException(); |
1012 |
< |
} |
1006 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1007 |
> |
void realRun() { |
1008 |
> |
while (! q.hasWaitingConsumer()) |
1009 |
> |
Thread.yield(); |
1010 |
> |
threadAssertTrue(q.hasWaitingConsumer()); |
1011 |
> |
threadAssertTrue(q.isEmpty()); |
1012 |
> |
threadAssertTrue(q.size() == 0); |
1013 |
> |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1014 |
> |
}}); |
1015 |
> |
|
1016 |
> |
assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato); |
1017 |
> |
checkEmpty(q); |
1018 |
> |
t.join(); |
1019 |
> |
} |
1020 |
|
|
1021 |
< |
} |
1022 |
< |
}).start(); |
1023 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null); |
1024 |
< |
assertTrue(q.isEmpty()); |
1025 |
< |
} catch (Exception ex) { |
1026 |
< |
this.unexpectedException(); |
1027 |
< |
} |
1021 |
> |
/** |
1022 |
> |
* If there is a consumer waiting in take, tryTransfer returns |
1023 |
> |
* true while successfully transfering object. |
1024 |
> |
*/ |
1025 |
> |
public void testTryTransfer4() throws InterruptedException { |
1026 |
> |
final Object hotPotato = new Object(); |
1027 |
> |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1028 |
> |
|
1029 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1030 |
> |
void realRun() { |
1031 |
> |
while (! q.hasWaitingConsumer()) |
1032 |
> |
Thread.yield(); |
1033 |
> |
threadAssertTrue(q.hasWaitingConsumer()); |
1034 |
> |
threadAssertTrue(q.isEmpty()); |
1035 |
> |
threadAssertTrue(q.size() == 0); |
1036 |
> |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1037 |
> |
}}); |
1038 |
> |
|
1039 |
> |
assertTrue(q.take() == hotPotato); |
1040 |
> |
checkEmpty(q); |
1041 |
> |
t.join(); |
1042 |
|
} |
1043 |
|
|
1044 |
|
/** |
1045 |
< |
* tryTransfer waits the amount given if interrupted, show an |
1046 |
< |
* interrupted exception |
1045 |
> |
* tryTransfer waits the amount given if interrupted, and |
1046 |
> |
* throws interrupted exception |
1047 |
|
*/ |
1048 |
< |
public void testTryTransfer4() { |
1048 |
> |
public void testTryTransfer5() throws InterruptedException { |
1049 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1116 |
– |
Thread toInterrupt = new Thread(new Runnable() { |
1050 |
|
|
1051 |
< |
public void run() { |
1052 |
< |
try { |
1053 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1054 |
< |
threadShouldThrow(); |
1055 |
< |
} catch (InterruptedException ex) { |
1056 |
< |
} |
1057 |
< |
} |
1058 |
< |
}); |
1126 |
< |
try { |
1127 |
< |
toInterrupt.start(); |
1128 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1129 |
< |
toInterrupt.interrupt(); |
1130 |
< |
} catch (Exception ex) { |
1131 |
< |
this.unexpectedException(); |
1132 |
< |
} |
1051 |
> |
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
1052 |
> |
void realRun() throws InterruptedException { |
1053 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1054 |
> |
}}); |
1055 |
> |
|
1056 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1057 |
> |
toInterrupt.interrupt(); |
1058 |
> |
toInterrupt.join(); |
1059 |
|
} |
1060 |
|
|
1061 |
|
/** |
1062 |
|
* tryTransfer gives up after the timeout and return false |
1063 |
|
*/ |
1064 |
< |
public void testTryTransfer5() { |
1064 |
> |
public void testTryTransfer6() throws InterruptedException { |
1065 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1140 |
– |
try { |
1141 |
– |
new Thread(new Runnable() { |
1066 |
|
|
1067 |
< |
public void run() { |
1068 |
< |
try { |
1069 |
< |
threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1070 |
< |
} catch (InterruptedException ex) { |
1071 |
< |
threadUnexpectedException(); |
1072 |
< |
} |
1073 |
< |
} |
1074 |
< |
}).start(); |
1075 |
< |
Thread.sleep(LONG_DELAY_MS); |
1076 |
< |
assertTrue(q.isEmpty()); |
1153 |
< |
} catch (Exception ex) { |
1154 |
< |
this.unexpectedException(); |
1155 |
< |
} |
1067 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1068 |
> |
void realRun() throws InterruptedException { |
1069 |
> |
threadAssertFalse |
1070 |
> |
(q.tryTransfer(new Object(), |
1071 |
> |
SHORT_DELAY_MS, MILLISECONDS)); |
1072 |
> |
}}); |
1073 |
> |
|
1074 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1075 |
> |
checkEmpty(q); |
1076 |
> |
t.join(); |
1077 |
|
} |
1078 |
|
|
1079 |
|
/** |
1080 |
|
* tryTransfer waits for any elements previously in to be removed |
1081 |
|
* before transfering to a poll or take |
1082 |
|
*/ |
1083 |
< |
public void testTryTransfer6() { |
1083 |
> |
public void testTryTransfer7() throws InterruptedException { |
1084 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1085 |
< |
q.offer(new Integer(four)); |
1165 |
< |
new Thread(new Runnable() { |
1085 |
> |
assertTrue(q.offer(four)); |
1086 |
|
|
1087 |
< |
public void run() { |
1088 |
< |
try { |
1089 |
< |
threadAssertTrue(q.tryTransfer(new Integer(five), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
1090 |
< |
threadAssertTrue(q.isEmpty()); |
1091 |
< |
} catch (InterruptedException ex) { |
1092 |
< |
threadUnexpectedException(); |
1093 |
< |
} |
1094 |
< |
} |
1095 |
< |
}).start(); |
1096 |
< |
try { |
1097 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1098 |
< |
assertEquals(2, q.size()); |
1099 |
< |
assertEquals(new Integer(four), q.poll()); |
1180 |
< |
assertEquals(new Integer(five), q.poll()); |
1181 |
< |
assertTrue(q.isEmpty()); |
1182 |
< |
} catch (Exception ex) { |
1183 |
< |
this.unexpectedException(); |
1184 |
< |
} |
1087 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1088 |
> |
void realRun() throws InterruptedException { |
1089 |
> |
threadAssertTrue(q.tryTransfer(five, |
1090 |
> |
MEDIUM_DELAY_MS, MILLISECONDS)); |
1091 |
> |
threadAssertTrue(q.isEmpty()); |
1092 |
> |
}}); |
1093 |
> |
|
1094 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1095 |
> |
assertEquals(2, q.size()); |
1096 |
> |
assertEquals(four, q.poll()); |
1097 |
> |
assertEquals(five, q.poll()); |
1098 |
> |
checkEmpty(q); |
1099 |
> |
t.join(); |
1100 |
|
} |
1101 |
|
|
1102 |
|
/** |
1103 |
< |
* tryTransfer attempts to enqueue into the q and fails returning false not |
1104 |
< |
* enqueueing and the successing poll is null |
1103 |
> |
* tryTransfer attempts to enqueue into the q and fails returning |
1104 |
> |
* false not enqueueing and the successive poll is null |
1105 |
|
*/ |
1106 |
< |
public void testTryTransfer7() { |
1106 |
> |
public void testTryTransfer8() throws InterruptedException { |
1107 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1108 |
< |
q.offer(new Integer(four)); |
1109 |
< |
new Thread(new Runnable() { |
1110 |
< |
|
1111 |
< |
public void run() { |
1112 |
< |
try { |
1113 |
< |
threadAssertFalse(q.tryTransfer(new Integer(five), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1114 |
< |
threadAssertTrue(q.isEmpty()); |
1200 |
< |
} catch (InterruptedException ex) { |
1201 |
< |
threadUnexpectedException(); |
1202 |
< |
} |
1203 |
< |
} |
1204 |
< |
}).start(); |
1205 |
< |
try { |
1206 |
< |
assertEquals(1, q.size()); |
1207 |
< |
assertEquals(new Integer(four), q.poll()); |
1208 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1209 |
< |
assertNull(q.poll()); |
1210 |
< |
} catch (Exception ex) { |
1211 |
< |
this.unexpectedException(); |
1212 |
< |
} |
1108 |
> |
assertTrue(q.offer(four)); |
1109 |
> |
assertEquals(1, q.size()); |
1110 |
> |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1111 |
> |
assertEquals(1, q.size()); |
1112 |
> |
assertEquals(four, q.poll()); |
1113 |
> |
assertNull(q.poll()); |
1114 |
> |
checkEmpty(q); |
1115 |
|
} |
1116 |
|
|
1117 |
< |
private LinkedTransferQueue populatedQueue( |
1118 |
< |
int n) { |
1217 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(); |
1117 |
> |
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1118 |
> |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
1119 |
|
assertTrue(q.isEmpty()); |
1120 |
< |
int remainingCapacity = q.remainingCapacity(); |
1121 |
< |
for (int i = 0; i < |
1221 |
< |
n; i++) { |
1120 |
> |
for (int i = 0; i < n; i++) { |
1121 |
> |
assertEquals(i, q.size()); |
1122 |
|
assertTrue(q.offer(i)); |
1123 |
+ |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
1124 |
|
} |
1224 |
– |
|
1125 |
|
assertFalse(q.isEmpty()); |
1226 |
– |
assertEquals(remainingCapacity, q.remainingCapacity()); |
1227 |
– |
assertEquals(n, q.size()); |
1126 |
|
return q; |
1127 |
|
} |
1230 |
– |
|
1231 |
– |
private static class ConsumerObserver { |
1232 |
– |
|
1233 |
– |
private int waitingConsumers; |
1234 |
– |
|
1235 |
– |
private ConsumerObserver() { |
1236 |
– |
} |
1237 |
– |
|
1238 |
– |
private void setWaitingConsumer(int i) { |
1239 |
– |
this.waitingConsumers = i; |
1240 |
– |
} |
1241 |
– |
|
1242 |
– |
private int getWaitingConsumers() { |
1243 |
– |
return waitingConsumers; |
1244 |
– |
} |
1245 |
– |
} |
1128 |
|
} |