10 |
|
import java.util.concurrent.*; |
11 |
|
import java.io.*; |
12 |
|
|
13 |
< |
public class SynchronousQueueTest extends TestCase { |
14 |
< |
|
15 |
< |
private final static int N = 1; |
16 |
< |
private static long SHORT_DELAY_MS = 100; |
17 |
< |
private static long MEDIUM_DELAY_MS = 1000; |
18 |
< |
private static long LONG_DELAY_MS = 10000; |
13 |
> |
public class SynchronousQueueTest extends JSR166TestCase { |
14 |
|
|
15 |
|
public static void main(String[] args) { |
16 |
|
junit.textui.TestRunner.run (suite()); |
61 |
|
public void testAddAll2(){ |
62 |
|
try { |
63 |
|
SynchronousQueue q = new SynchronousQueue(); |
64 |
< |
Integer[] ints = new Integer[N]; |
64 |
> |
Integer[] ints = new Integer[1]; |
65 |
|
q.addAll(Arrays.asList(ints)); |
66 |
|
fail("Cannot add null elements"); |
67 |
|
} |
70 |
|
public void testAddAll4(){ |
71 |
|
try { |
72 |
|
SynchronousQueue q = new SynchronousQueue(); |
73 |
< |
Integer[] ints = new Integer[N]; |
74 |
< |
for (int i = 0; i < N; ++i) |
73 |
> |
Integer[] ints = new Integer[1]; |
74 |
> |
for (int i = 0; i < 1; ++i) |
75 |
|
ints[i] = new Integer(i); |
76 |
|
q.addAll(Arrays.asList(ints)); |
77 |
|
fail("Cannot add with insufficient capacity"); |
98 |
|
try { |
99 |
|
SynchronousQueue q = new SynchronousQueue(); |
100 |
|
q.put(new Integer(0)); |
101 |
< |
fail("put should block"); |
101 |
> |
threadFail("put should block"); |
102 |
|
} catch (InterruptedException ie){ |
103 |
|
} |
104 |
|
}}); |
127 |
|
++added; |
128 |
|
q.put(new Object()); |
129 |
|
++added; |
130 |
< |
fail("Should block"); |
130 |
> |
threadFail("Should block"); |
131 |
|
} catch (InterruptedException e){ |
132 |
|
assertTrue(added >= 1); |
133 |
|
} |
151 |
|
public void run(){ |
152 |
|
try { |
153 |
|
|
154 |
< |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); |
154 |
> |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
155 |
|
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
156 |
< |
fail("Should block"); |
156 |
> |
threadFail("Should block"); |
157 |
|
} catch (InterruptedException success){} |
158 |
|
} |
159 |
|
}); |
160 |
|
|
161 |
|
try { |
162 |
|
t.start(); |
163 |
< |
Thread.sleep(SHORT_DELAY_MS); |
163 |
> |
Thread.sleep(SMALL_DELAY_MS); |
164 |
|
t.interrupt(); |
165 |
|
t.join(); |
166 |
|
} catch (Exception e){ |
175 |
|
public void run(){ |
176 |
|
try { |
177 |
|
q.take(); |
178 |
< |
fail("Should block"); |
178 |
> |
threadFail("Should block"); |
179 |
|
} catch (InterruptedException success){ } |
180 |
|
} |
181 |
|
}); |
237 |
|
Thread t = new Thread(new Runnable() { |
238 |
|
public void run(){ |
239 |
|
try { |
240 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
240 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
241 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
242 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
243 |
< |
fail("Should block"); |
243 |
> |
threadFail("Should block"); |
244 |
|
} catch (InterruptedException success) { } |
245 |
|
} |
246 |
|
}); |
247 |
|
try { |
248 |
|
t.start(); |
249 |
< |
Thread.sleep(SHORT_DELAY_MS * 2); |
249 |
> |
Thread.sleep(SMALL_DELAY_MS); |
250 |
|
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
251 |
|
t.interrupt(); |
252 |
|
t.join(); |
281 |
|
|
282 |
|
public void testRemoveElement(){ |
283 |
|
SynchronousQueue q = new SynchronousQueue(); |
284 |
< |
for (int i = 1; i < N; i+=2) { |
290 |
< |
assertFalse(q.remove(new Integer(i))); |
291 |
< |
} |
284 |
> |
assertFalse(q.remove(new Integer(0))); |
285 |
|
assertTrue(q.isEmpty()); |
286 |
|
} |
287 |
|
|
288 |
|
public void testContains(){ |
289 |
|
SynchronousQueue q = new SynchronousQueue(); |
290 |
< |
for (int i = 0; i < N; ++i) { |
298 |
< |
assertFalse(q.contains(new Integer(i))); |
299 |
< |
} |
290 |
> |
assertFalse(q.contains(new Integer(0))); |
291 |
|
} |
292 |
|
|
293 |
|
public void testClear(){ |
370 |
|
|
371 |
|
executor.execute(new Runnable() { |
372 |
|
public void run() { |
373 |
< |
assertFalse(q.offer(one)); |
373 |
> |
threadAssertFalse(q.offer(one)); |
374 |
|
try { |
375 |
< |
assertTrue(q.offer(one, MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); |
376 |
< |
assertEquals(0, q.remainingCapacity()); |
375 |
> |
threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
376 |
> |
threadAssertEquals(0, q.remainingCapacity()); |
377 |
|
} |
378 |
|
catch (InterruptedException e) { |
379 |
< |
fail("should not be interrupted"); |
379 |
> |
threadFail("should not be interrupted"); |
380 |
|
} |
381 |
|
} |
382 |
|
}); |
384 |
|
executor.execute(new Runnable() { |
385 |
|
public void run() { |
386 |
|
try { |
387 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
388 |
< |
assertEquals(one, q.take()); |
387 |
> |
Thread.sleep(SMALL_DELAY_MS); |
388 |
> |
threadAssertEquals(one, q.take()); |
389 |
|
} |
390 |
|
catch (InterruptedException e) { |
391 |
|
fail("should not be interrupted"); |
393 |
|
} |
394 |
|
}); |
395 |
|
|
396 |
< |
executor.shutdown(); |
396 |
> |
joinPool(executor); |
397 |
|
|
398 |
|
} |
399 |
|
|
405 |
|
|
406 |
|
executor.execute(new Runnable() { |
407 |
|
public void run() { |
408 |
< |
assertNull(q.poll()); |
408 |
> |
threadAssertNull(q.poll()); |
409 |
|
try { |
410 |
< |
assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); |
411 |
< |
assertTrue(q.isEmpty()); |
410 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
411 |
> |
threadAssertTrue(q.isEmpty()); |
412 |
|
} |
413 |
|
catch (InterruptedException e) { |
414 |
< |
fail("should not be interrupted"); |
414 |
> |
threadFail("should not be interrupted"); |
415 |
|
} |
416 |
|
} |
417 |
|
}); |
419 |
|
executor.execute(new Runnable() { |
420 |
|
public void run() { |
421 |
|
try { |
422 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
422 |
> |
Thread.sleep(SMALL_DELAY_MS); |
423 |
|
q.put(new Integer(1)); |
424 |
|
} |
425 |
|
catch (InterruptedException e) { |
426 |
< |
fail("should not be interrupted"); |
426 |
> |
threadFail("should not be interrupted"); |
427 |
|
} |
428 |
|
} |
429 |
|
}); |
430 |
|
|
431 |
< |
executor.shutdown(); |
431 |
> |
joinPool(executor); |
432 |
|
|
433 |
|
} |
434 |
|
|