ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/BlockingQueueTest.java
Revision: 1.2
Committed: Thu Oct 28 17:57:26 2010 UTC (13 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.1: +25 -0 lines
Log Message:
refactor testTakeFromEmpty into BlockingQueueTest

File Contents

# Content
1 /*
2 * Written by Doug Lea and Martin Buchholz with assistance from members
3 * of JCP JSR-166 Expert Group and released to the public domain, as
4 * explained at http://creativecommons.org/licenses/publicdomain
5 *
6 * Other contributors include Andrew Wright, Jeffrey Hayes,
7 * Pat Fisher, Mike Judd.
8 */
9
10 import junit.framework.*;
11 import java.util.*;
12 import java.util.concurrent.*;
13 import static java.util.concurrent.TimeUnit.MILLISECONDS;
14
15 /**
16 * Contains tests generally applicable to BlockingQueue implementations.
17 */
18 public abstract class BlockingQueueTest extends JSR166TestCase {
19 /*
20 * This is the start of an attempt to refactor the tests for the
21 * various related implementations of related interfaces without
22 * too much duplicated code. junit does not really support such
23 * testing. Here subclasses of TestCase not only contain tests,
24 * but also configuration information that describes the
25 * implementation class, most importantly how to instantiate
26 * instances.
27 */
28
29 /** Like suite(), but non-static */
30 public Test testSuite() {
31 // TODO: filter the returned tests using the configuration
32 // information provided by the subclass via protected methods.
33 return new TestSuite(this.getClass());
34 }
35
36 /** Returns an empty instance of the implementation class. */
37 protected abstract BlockingQueue emptyCollection();
38
39 /**
40 * timed poll before a delayed offer fails; after offer succeeds;
41 * on interruption throws
42 */
43 public void testTimedPollWithOffer() throws InterruptedException {
44 final BlockingQueue q = emptyCollection();
45 final CheckedBarrier barrier = new CheckedBarrier(2);
46 Thread t = newStartedThread(new CheckedRunnable() {
47 public void realRun() throws InterruptedException {
48 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
49
50 barrier.await();
51 assertSame(zero, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
52
53 Thread.currentThread().interrupt();
54 try {
55 q.poll(SHORT_DELAY_MS, MILLISECONDS);
56 shouldThrow();
57 } catch (InterruptedException success) {}
58
59 barrier.await();
60 try {
61 q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
62 shouldThrow();
63 } catch (InterruptedException success) {}
64 }});
65
66 barrier.await();
67 assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
68 barrier.await();
69 sleep(SHORT_DELAY_MS);
70 t.interrupt();
71 awaitTermination(t, MEDIUM_DELAY_MS);
72 }
73
74 /**
75 * take blocks interruptibly when empty
76 */
77 public void testTakeFromEmptyBlocksInterruptibly()
78 throws InterruptedException {
79 final BlockingQueue q = emptyCollection();
80 final CountDownLatch threadStarted = new CountDownLatch(1);
81 Thread t = newStartedThread(new CheckedRunnable() {
82 public void realRun() {
83 long t0 = System.nanoTime();
84 threadStarted.countDown();
85 try {
86 q.take();
87 shouldThrow();
88 } catch (InterruptedException success) {}
89 assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
90 }});
91 threadStarted.await();
92 Thread.sleep(SHORT_DELAY_MS);
93 assertTrue(t.isAlive());
94 t.interrupt();
95 awaitTermination(t, MEDIUM_DELAY_MS);
96 assertFalse(t.isAlive());
97 }
98
99 /** For debugging. */
100 public void XXXXtestFails() {
101 fail(emptyCollection().getClass().toString());
102 }
103
104 }