52 |
|
* Override for collections with unusual element types. |
53 |
|
*/ |
54 |
|
protected Object makeElement(int i) { |
55 |
< |
return Integer.valueOf(i); |
55 |
> |
return JSR166TestCase.itemFor(i); |
56 |
|
} |
57 |
|
|
58 |
|
//---------------------------------------------------------------- |
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(); |
121 |
|
*/ |
122 |
|
public void testAddAllNullElements() { |
123 |
|
final Collection q = emptyCollection(); |
124 |
< |
final Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
124 |
> |
final Collection elements = Arrays.asList(new Item[SIZE]); |
125 |
|
try { |
126 |
|
q.addAll(elements); |
127 |
|
shouldThrow(); |
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 |
|
} |
189 |
|
public void testDrainToNonPositiveMaxElements() { |
190 |
|
final BlockingQueue q = emptyCollection(); |
191 |
|
final int[] ns = { 0, -1, -42, Integer.MIN_VALUE }; |
192 |
< |
for (int n : ns) |
193 |
< |
assertEquals(0, q.drainTo(new ArrayList(), n)); |
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 |
< |
ArrayList c = new ArrayList(); |
202 |
< |
for (int n : ns) |
203 |
< |
assertEquals(0, q.drainTo(new ArrayList(), n)); |
204 |
< |
assertEquals(1, q.size()); |
205 |
< |
assertSame(one, q.poll()); |
206 |
< |
assertTrue(c.isEmpty()); |
199 |
> |
Object e = makeElement(1); |
200 |
> |
if (q.add(e)) { |
201 |
> |
for (int n : ns) |
202 |
> |
assertEquals(0, q.drainTo(sink, n)); |
203 |
> |
assertEquals(1, q.size()); |
204 |
> |
assertEquals(e, q.poll()); |
205 |
> |
assertTrue(sink.isEmpty()); |
206 |
> |
} |
207 |
|
} |
208 |
|
} |
209 |
|
|
227 |
|
|
228 |
|
Thread.currentThread().interrupt(); |
229 |
|
try { |
230 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
230 |
> |
q.poll(randomTimeout(), randomTimeUnit()); |
231 |
|
shouldThrow(); |
232 |
|
} catch (InterruptedException success) {} |
233 |
|
assertFalse(Thread.interrupted()); |
248 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
249 |
|
|
250 |
|
barrier.await(); |
251 |
< |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
251 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
252 |
|
t.interrupt(); |
253 |
|
awaitTermination(t); |
254 |
|
} |
270 |
|
}}); |
271 |
|
|
272 |
|
await(threadStarted); |
273 |
< |
assertThreadBlocks(t, Thread.State.WAITING); |
273 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
274 |
|
t.interrupt(); |
275 |
|
awaitTermination(t); |
276 |
|
} |
299 |
|
*/ |
300 |
|
public void testTimedPollFromEmptyBlocksInterruptibly() { |
301 |
|
final BlockingQueue q = emptyCollection(); |
302 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
302 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
303 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
304 |
|
public void realRun() { |
305 |
< |
threadStarted.countDown(); |
305 |
> |
pleaseInterrupt.countDown(); |
306 |
|
try { |
307 |
< |
q.poll(2 * LONG_DELAY_MS, MILLISECONDS); |
307 |
> |
q.poll(LONGER_DELAY_MS, MILLISECONDS); |
308 |
|
shouldThrow(); |
309 |
|
} catch (InterruptedException success) {} |
310 |
|
assertFalse(Thread.interrupted()); |
311 |
|
}}); |
312 |
|
|
313 |
< |
await(threadStarted); |
314 |
< |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
313 |
> |
await(pleaseInterrupt); |
314 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
315 |
|
t.interrupt(); |
316 |
|
awaitTermination(t); |
317 |
|
} |
326 |
|
public void realRun() { |
327 |
|
Thread.currentThread().interrupt(); |
328 |
|
try { |
329 |
< |
q.poll(2 * LONG_DELAY_MS, MILLISECONDS); |
329 |
> |
q.poll(LONGER_DELAY_MS, MILLISECONDS); |
330 |
|
shouldThrow(); |
331 |
|
} catch (InterruptedException success) {} |
332 |
|
assertFalse(Thread.interrupted()); |
350 |
|
q.add(elts[i] = makeElement(i)); |
351 |
|
for (int i = 1; i < size; i += 2) { |
352 |
|
for (int pass = 0; pass < 2; pass++) { |
353 |
< |
assertEquals((pass == 0), q.contains(elts[i])); |
354 |
< |
assertEquals((pass == 0), q.remove(elts[i])); |
353 |
> |
mustEqual((pass == 0), q.contains(elts[i])); |
354 |
> |
mustEqual((pass == 0), q.remove(elts[i])); |
355 |
|
assertFalse(q.contains(elts[i])); |
356 |
|
assertTrue(q.contains(elts[i - 1])); |
357 |
|
if (i < size - 1) |