ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ExecutorCompletionServiceTest.java
Revision: 1.9
Committed: Sat Nov 21 02:33:20 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.8: +2 -1 lines
Log Message:
import static TimeUnit.MILLISECONDS

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