ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ExecutorCompletionServiceTest.java
Revision: 1.4
Committed: Fri Jul 8 20:00:10 2005 UTC (18 years, 10 months ago) by dl
Branch: MAIN
Changes since 1.3: +71 -0 lines
Log Message:
Add tests for newTaskFor

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/licenses/publicdomain
5 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
7 */
8
9
10 import junit.framework.*;
11 import java.util.*;
12 import java.util.concurrent.*;
13 import java.math.BigInteger;
14 import java.security.*;
15
16 public class ExecutorCompletionServiceTest extends JSR166TestCase{
17 public static void main(String[] args) {
18 junit.textui.TestRunner.run (suite());
19 }
20 public static Test suite() {
21 return new TestSuite(ExecutorCompletionServiceTest.class);
22 }
23
24
25 /**
26 * Creating a new ECS with null Executor throw NPE
27 */
28 public void testConstructorNPE() {
29 try {
30 ExecutorCompletionService ecs = new ExecutorCompletionService(null);
31 shouldThrow();
32 } catch (NullPointerException success) {
33 }
34 }
35
36 /**
37 * Creating a new ECS with null queue throw NPE
38 */
39 public void testConstructorNPE2() {
40 try {
41 ExecutorService e = Executors.newCachedThreadPool();
42 ExecutorCompletionService ecs = new ExecutorCompletionService(e, null);
43 shouldThrow();
44 } catch (NullPointerException success) {
45 }
46 }
47
48 /**
49 * Submitting a null callable throws NPE
50 */
51 public void testSubmitNPE() {
52 ExecutorService e = Executors.newCachedThreadPool();
53 ExecutorCompletionService ecs = new ExecutorCompletionService(e);
54 try {
55 Callable c = null;
56 ecs.submit(c);
57 shouldThrow();
58 } catch (NullPointerException success) {
59 } finally {
60 joinPool(e);
61 }
62 }
63
64 /**
65 * Submitting a null runnable throws NPE
66 */
67 public void testSubmitNPE2() {
68 ExecutorService e = Executors.newCachedThreadPool();
69 ExecutorCompletionService ecs = new ExecutorCompletionService(e);
70 try {
71 Runnable r = null;
72 ecs.submit(r, Boolean.TRUE);
73 shouldThrow();
74 } catch (NullPointerException success) {
75 } finally {
76 joinPool(e);
77 }
78 }
79
80 /**
81 * A taken submitted task is completed
82 */
83 public void testTake() {
84 ExecutorService e = Executors.newCachedThreadPool();
85 ExecutorCompletionService ecs = new ExecutorCompletionService(e);
86 try {
87 Callable c = new StringTask();
88 ecs.submit(c);
89 Future f = ecs.take();
90 assertTrue(f.isDone());
91 } catch (Exception ex) {
92 unexpectedException();
93 } finally {
94 joinPool(e);
95 }
96 }
97
98 /**
99 * Take returns the same future object returned by submit
100 */
101 public void testTake2() {
102 ExecutorService e = Executors.newCachedThreadPool();
103 ExecutorCompletionService ecs = new ExecutorCompletionService(e);
104 try {
105 Callable c = new StringTask();
106 Future f1 = ecs.submit(c);
107 Future f2 = ecs.take();
108 assertSame(f1, f2);
109 } catch (Exception ex) {
110 unexpectedException();
111 } finally {
112 joinPool(e);
113 }
114 }
115
116 /**
117 * If poll returns non-null, the returned task is completed
118 */
119 public void testPoll1() {
120 ExecutorService e = Executors.newCachedThreadPool();
121 ExecutorCompletionService ecs = new ExecutorCompletionService(e);
122 try {
123 assertNull(ecs.poll());
124 Callable c = new StringTask();
125 ecs.submit(c);
126 Thread.sleep(SHORT_DELAY_MS);
127 for (;;) {
128 Future f = ecs.poll();
129 if (f != null) {
130 assertTrue(f.isDone());
131 break;
132 }
133 }
134 } catch (Exception ex) {
135 unexpectedException();
136 } finally {
137 joinPool(e);
138 }
139 }
140
141 /**
142 * If timed poll returns non-null, the returned task is completed
143 */
144 public void testPoll2() {
145 ExecutorService e = Executors.newCachedThreadPool();
146 ExecutorCompletionService ecs = new ExecutorCompletionService(e);
147 try {
148 assertNull(ecs.poll());
149 Callable c = new StringTask();
150 ecs.submit(c);
151 Future f = ecs.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
152 if (f != null)
153 assertTrue(f.isDone());
154 } catch (Exception ex) {
155 unexpectedException();
156 } finally {
157 joinPool(e);
158 }
159 }
160 /**
161 * Submitting to underlying AES that overrides newTaskFor(Callable)
162 * returns and eventually runs Future returned by newTaskFor.
163 */
164 public void testNewTaskForCallable() {
165 final AtomicBoolean done = new AtomicBoolean(false);
166 class MyCallableFuture<V> extends FutureTask<V> {
167 MyCallableFuture(Callable<V> c) { super(c); }
168 protected void done() { done.set(true); }
169 }
170 ExecutorService e = new ThreadPoolExecutor(
171 1, 1, 30L, TimeUnit.SECONDS,
172 new ArrayBlockingQueue<Runnable>(1)) {
173 protected <T> RunnableFuture<T> newTaskFor(Callable<T> c) {
174 return new MyCallableFuture<T>(c);
175 }
176 };
177 ExecutorCompletionService<String> ecs =
178 new ExecutorCompletionService<String>(e);
179 try {
180 assertNull(ecs.poll());
181 Callable<String> c = new StringTask();
182 Future f1 = ecs.submit(c);
183 assertTrue("submit must return MyCallableFuture",
184 f1 instanceof MyCallableFuture);
185 Future f2 = ecs.take();
186 assertSame("submit and take must return same objects", f1, f2);
187 assertTrue("completed task must have set done", done.get());
188 } catch (Exception ex) {
189 unexpectedException();
190 } finally {
191 joinPool(e);
192 }
193 }
194
195 /**
196 * Submitting to underlying AES that overrides newTaskFor(Runnable,T)
197 * returns and eventually runs Future returned by newTaskFor.
198 */
199 public void testNewTaskForRunnable() {
200 final AtomicBoolean done = new AtomicBoolean(false);
201 class MyRunnableFuture<V> extends FutureTask<V> {
202 MyRunnableFuture(Runnable t, V r) { super(t, r); }
203 protected void done() { done.set(true); }
204 }
205 ExecutorService e = new ThreadPoolExecutor(
206 1, 1, 30L, TimeUnit.SECONDS,
207 new ArrayBlockingQueue<Runnable>(1)) {
208 protected <T> RunnableFuture<T> newTaskFor(Runnable t, T r) {
209 return new MyRunnableFuture<T>(t, r);
210 }
211 };
212 ExecutorCompletionService<String> ecs =
213 new ExecutorCompletionService<String>(e);
214 try {
215 assertNull(ecs.poll());
216 Callable<String> c = new StringTask();
217 Future f1 = ecs.submit(c);
218 assertTrue("submit must return MyRunnableFuture",
219 f1 instanceof MyRunnableFuture);
220 Future f2 = ecs.take();
221 assertSame("submit and take must return same objects", f1, f2);
222 assertTrue("completed task must have set done", done.get());
223 } catch (Exception ex) {
224 unexpectedException();
225 } finally {
226 joinPool(e);
227 }
228 }
229
230
231
232 }