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