7 |
|
* Pat Fisher, Mike Judd. |
8 |
|
*/ |
9 |
|
|
10 |
< |
import junit.framework.*; |
10 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
11 |
> |
|
12 |
> |
import java.util.ArrayList; |
13 |
|
import java.util.Arrays; |
14 |
|
import java.util.Collection; |
15 |
|
import java.util.Queue; |
16 |
|
import java.util.concurrent.BlockingQueue; |
17 |
|
import java.util.concurrent.CountDownLatch; |
18 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
18 |
> |
|
19 |
> |
import junit.framework.Test; |
20 |
> |
import junit.framework.TestSuite; |
21 |
|
|
22 |
|
/** |
23 |
|
* Contains "contract" tests applicable to all BlockingQueue implementations. |
43 |
|
//---------------------------------------------------------------- |
44 |
|
// Configuration methods |
45 |
|
//---------------------------------------------------------------- |
46 |
< |
|
46 |
> |
|
47 |
|
/** Returns an empty instance of the implementation class. */ |
48 |
|
protected abstract BlockingQueue emptyCollection(); |
49 |
|
|
58 |
|
//---------------------------------------------------------------- |
59 |
|
// Tests |
60 |
|
//---------------------------------------------------------------- |
61 |
< |
|
61 |
> |
|
62 |
|
/** |
63 |
|
* offer(null) throws NullPointerException |
64 |
|
*/ |
106 |
|
} |
107 |
|
|
108 |
|
/** |
109 |
< |
* put(null) throws NullPointerException |
109 |
> |
* addAll(null) throws NullPointerException |
110 |
|
*/ |
111 |
|
public void testAddAllNull() throws InterruptedException { |
112 |
|
final Collection q = emptyCollection(); |
134 |
|
public void testToArray_NullArray() { |
135 |
|
final Collection q = emptyCollection(); |
136 |
|
try { |
137 |
< |
q.toArray(null); |
137 |
> |
q.toArray((Object[])null); |
138 |
|
shouldThrow(); |
139 |
|
} catch (NullPointerException success) {} |
140 |
|
} |
184 |
|
} |
185 |
|
|
186 |
|
/** |
187 |
+ |
* drainTo(c, n) returns 0 and does nothing when n <= 0 |
188 |
+ |
*/ |
189 |
+ |
public void testDrainToNonPositiveMaxElements() { |
190 |
+ |
final BlockingQueue q = emptyCollection(); |
191 |
+ |
final int[] ns = { 0, -1, -42, Integer.MIN_VALUE }; |
192 |
+ |
final ArrayList sink = new ArrayList(); |
193 |
+ |
for (int n : ns) { |
194 |
+ |
assertEquals(0, q.drainTo(sink, n)); |
195 |
+ |
assertTrue(sink.isEmpty()); |
196 |
+ |
} |
197 |
+ |
if (q.remainingCapacity() > 0) { |
198 |
+ |
// Not SynchronousQueue, that is |
199 |
+ |
Object one = makeElement(1); |
200 |
+ |
q.add(one); |
201 |
+ |
for (int n : ns) |
202 |
+ |
assertEquals(0, q.drainTo(sink, n)); |
203 |
+ |
assertEquals(1, q.size()); |
204 |
+ |
assertSame(one, q.poll()); |
205 |
+ |
assertTrue(sink.isEmpty()); |
206 |
+ |
} |
207 |
+ |
} |
208 |
+ |
|
209 |
+ |
/** |
210 |
|
* timed poll before a delayed offer times out; after offer succeeds; |
211 |
|
* on interruption throws |
212 |
|
*/ |
237 |
|
shouldThrow(); |
238 |
|
} catch (InterruptedException success) {} |
239 |
|
assertFalse(Thread.interrupted()); |
240 |
+ |
|
241 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
242 |
|
}}); |
243 |
|
|
244 |
|
barrier.await(); |
247 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
248 |
|
|
249 |
|
barrier.await(); |
250 |
< |
assertThreadStaysAlive(t); |
250 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
251 |
|
t.interrupt(); |
252 |
|
awaitTermination(t); |
253 |
|
} |
269 |
|
}}); |
270 |
|
|
271 |
|
await(threadStarted); |
272 |
< |
assertThreadStaysAlive(t); |
272 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
273 |
|
t.interrupt(); |
274 |
|
awaitTermination(t); |
275 |
|
} |
310 |
|
}}); |
311 |
|
|
312 |
|
await(threadStarted); |
313 |
< |
assertThreadStaysAlive(t); |
313 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
314 |
|
t.interrupt(); |
315 |
|
awaitTermination(t); |
316 |
|
} |
334 |
|
awaitTermination(t); |
335 |
|
} |
336 |
|
|
337 |
+ |
/** |
338 |
+ |
* remove(x) removes x and returns true if present |
339 |
+ |
* TODO: move to superclass CollectionTest.java |
340 |
+ |
*/ |
341 |
+ |
public void testRemoveElement() { |
342 |
+ |
final BlockingQueue q = emptyCollection(); |
343 |
+ |
final int size = Math.min(q.remainingCapacity(), SIZE); |
344 |
+ |
final Object[] elts = new Object[size]; |
345 |
+ |
assertFalse(q.contains(makeElement(99))); |
346 |
+ |
assertFalse(q.remove(makeElement(99))); |
347 |
+ |
checkEmpty(q); |
348 |
+ |
for (int i = 0; i < size; i++) |
349 |
+ |
q.add(elts[i] = makeElement(i)); |
350 |
+ |
for (int i = 1; i < size; i += 2) { |
351 |
+ |
for (int pass = 0; pass < 2; pass++) { |
352 |
+ |
assertEquals((pass == 0), q.contains(elts[i])); |
353 |
+ |
assertEquals((pass == 0), q.remove(elts[i])); |
354 |
+ |
assertFalse(q.contains(elts[i])); |
355 |
+ |
assertTrue(q.contains(elts[i - 1])); |
356 |
+ |
if (i < size - 1) |
357 |
+ |
assertTrue(q.contains(elts[i + 1])); |
358 |
+ |
} |
359 |
+ |
} |
360 |
+ |
if (size > 0) |
361 |
+ |
assertTrue(q.contains(elts[0])); |
362 |
+ |
for (int i = size - 2; i >= 0; i -= 2) { |
363 |
+ |
assertTrue(q.contains(elts[i])); |
364 |
+ |
assertFalse(q.contains(elts[i + 1])); |
365 |
+ |
assertTrue(q.remove(elts[i])); |
366 |
+ |
assertFalse(q.contains(elts[i])); |
367 |
+ |
assertFalse(q.remove(elts[i + 1])); |
368 |
+ |
assertFalse(q.contains(elts[i + 1])); |
369 |
+ |
} |
370 |
+ |
checkEmpty(q); |
371 |
+ |
} |
372 |
+ |
|
373 |
|
/** For debugging. */ |
374 |
|
public void XXXXtestFails() { |
375 |
|
fail(emptyCollection().getClass().toString()); |