[cvs] / jsr166 / src / test / tck / BlockingQueueTest.java Repository:
ViewVC logotype

Annotation of /jsr166/src/test/tck/BlockingQueueTest.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (view) (download)

1 : jsr166 1.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 : jsr166 1.5 * explained at http://creativecommons.org/publicdomain/zero/1.0/
5 : jsr166 1.1 *
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 : jsr166 1.7 * timed poll before a delayed offer times out; after offer succeeds;
41 : jsr166 1.1 * 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 : jsr166 1.7 long startTime = System.nanoTime();
49 :     assertNull(q.poll(timeoutMillis(), MILLISECONDS));
50 :     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
51 : jsr166 1.1
52 :     barrier.await();
53 : jsr166 1.7
54 :     assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
55 : jsr166 1.1
56 :     Thread.currentThread().interrupt();
57 :     try {
58 : jsr166 1.7 q.poll(LONG_DELAY_MS, MILLISECONDS);
59 : jsr166 1.1 shouldThrow();
60 :     } catch (InterruptedException success) {}
61 : jsr166 1.7 assertFalse(Thread.interrupted());
62 : jsr166 1.1
63 :     barrier.await();
64 :     try {
65 : jsr166 1.7 q.poll(LONG_DELAY_MS, MILLISECONDS);
66 : jsr166 1.1 shouldThrow();
67 :     } catch (InterruptedException success) {}
68 : jsr166 1.7 assertFalse(Thread.interrupted());
69 : jsr166 1.1 }});
70 :    
71 :     barrier.await();
72 : jsr166 1.7 long startTime = System.nanoTime();
73 :     assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS));
74 :     assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
75 :    
76 : jsr166 1.1 barrier.await();
77 : jsr166 1.7 assertThreadStaysAlive(t);
78 : jsr166 1.1 t.interrupt();
79 : jsr166 1.7 awaitTermination(t);
80 : jsr166 1.1 }
81 :    
82 : jsr166 1.2 /**
83 : jsr166 1.3 * take() blocks interruptibly when empty
84 : jsr166 1.2 */
85 : jsr166 1.7 public void testTakeFromEmptyBlocksInterruptibly() {
86 : jsr166 1.2 final BlockingQueue q = emptyCollection();
87 :     final CountDownLatch threadStarted = new CountDownLatch(1);
88 :     Thread t = newStartedThread(new CheckedRunnable() {
89 :     public void realRun() {
90 :     threadStarted.countDown();
91 :     try {
92 :     q.take();
93 :     shouldThrow();
94 :     } catch (InterruptedException success) {}
95 : jsr166 1.7 assertFalse(Thread.interrupted());
96 : jsr166 1.2 }});
97 : jsr166 1.3
98 : jsr166 1.7 await(threadStarted);
99 :     assertThreadStaysAlive(t);
100 : jsr166 1.2 t.interrupt();
101 : jsr166 1.7 awaitTermination(t);
102 : jsr166 1.3 }
103 :    
104 :     /**
105 :     * take() throws InterruptedException immediately if interrupted
106 :     * before waiting
107 :     */
108 : jsr166 1.7 public void testTakeFromEmptyAfterInterrupt() {
109 : jsr166 1.3 final BlockingQueue q = emptyCollection();
110 :     Thread t = newStartedThread(new CheckedRunnable() {
111 :     public void realRun() {
112 :     Thread.currentThread().interrupt();
113 :     try {
114 :     q.take();
115 :     shouldThrow();
116 :     } catch (InterruptedException success) {}
117 : jsr166 1.7 assertFalse(Thread.interrupted());
118 :     }});
119 :    
120 :     awaitTermination(t);
121 :     }
122 :    
123 :     /**
124 :     * timed poll() blocks interruptibly when empty
125 :     */
126 :     public void testTimedPollFromEmptyBlocksInterruptibly() {
127 :     final BlockingQueue q = emptyCollection();
128 :     final CountDownLatch threadStarted = new CountDownLatch(1);
129 :     Thread t = newStartedThread(new CheckedRunnable() {
130 :     public void realRun() {
131 :     threadStarted.countDown();
132 :     try {
133 :     q.poll(2 * LONG_DELAY_MS, MILLISECONDS);
134 :     shouldThrow();
135 :     } catch (InterruptedException success) {}
136 :     assertFalse(Thread.interrupted());
137 :     }});
138 :    
139 :     await(threadStarted);
140 :     assertThreadStaysAlive(t);
141 :     t.interrupt();
142 :     awaitTermination(t);
143 :     }
144 :    
145 :     /**
146 :     * timed poll() throws InterruptedException immediately if
147 :     * interrupted before waiting
148 :     */
149 :     public void testTimedPollFromEmptyAfterInterrupt() {
150 :     final BlockingQueue q = emptyCollection();
151 :     Thread t = newStartedThread(new CheckedRunnable() {
152 :     public void realRun() {
153 :     Thread.currentThread().interrupt();
154 :     try {
155 :     q.poll(2 * LONG_DELAY_MS, MILLISECONDS);
156 :     shouldThrow();
157 :     } catch (InterruptedException success) {}
158 :     assertFalse(Thread.interrupted());
159 : jsr166 1.3 }});
160 :    
161 : jsr166 1.7 awaitTermination(t);
162 : jsr166 1.2 }
163 :    
164 : jsr166 1.1 /** For debugging. */
165 :     public void XXXXtestFails() {
166 :     fail(emptyCollection().getClass().toString());
167 :     }
168 :    
169 :     }

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8