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. |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
< |
import java.util.concurrent.*; |
12 |
< |
import java.util.concurrent.atomic.*; |
13 |
< |
import java.math.BigInteger; |
11 |
> |
import java.util.concurrent.ArrayBlockingQueue; |
12 |
> |
import java.util.concurrent.Callable; |
13 |
> |
import java.util.concurrent.ExecutorCompletionService; |
14 |
> |
import java.util.concurrent.ExecutorService; |
15 |
> |
import java.util.concurrent.Executors; |
16 |
> |
import java.util.concurrent.Future; |
17 |
> |
import java.util.concurrent.FutureTask; |
18 |
> |
import java.util.concurrent.RunnableFuture; |
19 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
20 |
> |
import java.util.concurrent.TimeUnit; |
21 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
22 |
> |
import java.util.concurrent.atomic.AtomicBoolean; |
23 |
|
import java.security.*; |
24 |
|
|
25 |
< |
public class ExecutorCompletionServiceTest extends JSR166TestCase{ |
25 |
> |
public class ExecutorCompletionServiceTest extends JSR166TestCase { |
26 |
|
public static void main(String[] args) { |
27 |
< |
junit.textui.TestRunner.run (suite()); |
27 |
> |
junit.textui.TestRunner.run(suite()); |
28 |
|
} |
29 |
|
public static Test suite() { |
30 |
|
return new TestSuite(ExecutorCompletionServiceTest.class); |
31 |
|
} |
32 |
|
|
25 |
– |
|
33 |
|
/** |
34 |
|
* Creating a new ECS with null Executor throw NPE |
35 |
< |
*/ |
35 |
> |
*/ |
36 |
|
public void testConstructorNPE() { |
37 |
|
try { |
38 |
|
ExecutorCompletionService ecs = new ExecutorCompletionService(null); |
39 |
|
shouldThrow(); |
40 |
< |
} catch (NullPointerException success) { |
34 |
< |
} |
40 |
> |
} catch (NullPointerException success) {} |
41 |
|
} |
42 |
|
|
43 |
|
/** |
44 |
|
* Creating a new ECS with null queue throw NPE |
45 |
< |
*/ |
45 |
> |
*/ |
46 |
|
public void testConstructorNPE2() { |
47 |
|
try { |
48 |
|
ExecutorService e = Executors.newCachedThreadPool(); |
49 |
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e, null); |
50 |
|
shouldThrow(); |
51 |
< |
} catch (NullPointerException success) { |
46 |
< |
} |
51 |
> |
} catch (NullPointerException success) {} |
52 |
|
} |
53 |
|
|
54 |
|
/** |
55 |
|
* Submitting a null callable throws NPE |
56 |
< |
*/ |
56 |
> |
*/ |
57 |
|
public void testSubmitNPE() { |
58 |
|
ExecutorService e = Executors.newCachedThreadPool(); |
59 |
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
69 |
|
|
70 |
|
/** |
71 |
|
* Submitting a null runnable throws NPE |
72 |
< |
*/ |
72 |
> |
*/ |
73 |
|
public void testSubmitNPE2() { |
74 |
|
ExecutorService e = Executors.newCachedThreadPool(); |
75 |
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
85 |
|
|
86 |
|
/** |
87 |
|
* A taken submitted task is completed |
88 |
< |
*/ |
89 |
< |
public void testTake() { |
88 |
> |
*/ |
89 |
> |
public void testTake() throws InterruptedException { |
90 |
|
ExecutorService e = Executors.newCachedThreadPool(); |
91 |
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
92 |
|
try { |
94 |
|
ecs.submit(c); |
95 |
|
Future f = ecs.take(); |
96 |
|
assertTrue(f.isDone()); |
92 |
– |
} catch (Exception ex) { |
93 |
– |
unexpectedException(); |
97 |
|
} finally { |
98 |
|
joinPool(e); |
99 |
|
} |
101 |
|
|
102 |
|
/** |
103 |
|
* Take returns the same future object returned by submit |
104 |
< |
*/ |
105 |
< |
public void testTake2() { |
104 |
> |
*/ |
105 |
> |
public void testTake2() throws InterruptedException { |
106 |
|
ExecutorService e = Executors.newCachedThreadPool(); |
107 |
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
108 |
|
try { |
110 |
|
Future f1 = ecs.submit(c); |
111 |
|
Future f2 = ecs.take(); |
112 |
|
assertSame(f1, f2); |
110 |
– |
} catch (Exception ex) { |
111 |
– |
unexpectedException(); |
113 |
|
} finally { |
114 |
|
joinPool(e); |
115 |
|
} |
117 |
|
|
118 |
|
/** |
119 |
|
* If poll returns non-null, the returned task is completed |
120 |
< |
*/ |
121 |
< |
public void testPoll1() { |
120 |
> |
*/ |
121 |
> |
public void testPoll1() throws Exception { |
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 |
< |
} |
128 |
> |
|
129 |
> |
long startTime = System.nanoTime(); |
130 |
> |
Future f; |
131 |
> |
while ((f = ecs.poll()) == null) { |
132 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
133 |
> |
fail("timed out"); |
134 |
> |
Thread.yield(); |
135 |
|
} |
136 |
< |
} catch (Exception ex) { |
137 |
< |
unexpectedException(); |
136 |
> |
assertTrue(f.isDone()); |
137 |
> |
assertSame(TEST_STRING, f.get()); |
138 |
|
} finally { |
139 |
|
joinPool(e); |
140 |
|
} |
142 |
|
|
143 |
|
/** |
144 |
|
* If timed poll returns non-null, the returned task is completed |
145 |
< |
*/ |
146 |
< |
public void testPoll2() { |
145 |
> |
*/ |
146 |
> |
public void testPoll2() throws InterruptedException { |
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, TimeUnit.MILLISECONDS); |
154 |
< |
if (f != null) |
153 |
> |
Future f = ecs.poll(SHORT_DELAY_MS, MILLISECONDS); |
154 |
> |
if (f != null) |
155 |
|
assertTrue(f.isDone()); |
155 |
– |
} catch (Exception ex) { |
156 |
– |
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( |
160 |
> |
|
161 |
> |
/** |
162 |
> |
* Submitting to underlying AES that overrides newTaskFor(Callable) |
163 |
> |
* returns and eventually runs Future returned by newTaskFor. |
164 |
> |
*/ |
165 |
> |
public void testNewTaskForCallable() throws InterruptedException { |
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( |
174 |
> |
protected <T> RunnableFuture<T> newTaskFor(Callable<T> c) { |
175 |
> |
return new MyCallableFuture<T>(c); |
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 |
> |
} finally { |
189 |
> |
joinPool(e); |
190 |
> |
} |
191 |
> |
} |
192 |
> |
|
193 |
> |
/** |
194 |
> |
* Submitting to underlying AES that overrides newTaskFor(Runnable,T) |
195 |
> |
* returns and eventually runs Future returned by newTaskFor. |
196 |
> |
*/ |
197 |
> |
public void testNewTaskForRunnable() throws InterruptedException { |
198 |
> |
final AtomicBoolean done = new AtomicBoolean(false); |
199 |
> |
class MyRunnableFuture<V> extends FutureTask<V> { |
200 |
> |
MyRunnableFuture(Runnable t, V r) { super(t, r); } |
201 |
> |
protected void done() { done.set(true); } |
202 |
> |
} |
203 |
> |
ExecutorService e = new ThreadPoolExecutor( |
204 |
|
1, 1, 30L, TimeUnit.SECONDS, |
205 |
|
new ArrayBlockingQueue<Runnable>(1)) { |
206 |
< |
protected <T> RunnableFuture<T> newTaskFor(Runnable t, T r) { |
207 |
< |
return new MyRunnableFuture<T>(t, r); |
208 |
< |
} |
209 |
< |
}; |
210 |
< |
ExecutorCompletionService<String> ecs = |
211 |
< |
new ExecutorCompletionService<String>(e); |
212 |
< |
try { |
213 |
< |
assertNull(ecs.poll()); |
214 |
< |
Runnable r = new NoOpRunnable(); |
215 |
< |
Future f1 = ecs.submit(r, null); |
216 |
< |
assertTrue("submit must return MyRunnableFuture", |
217 |
< |
f1 instanceof MyRunnableFuture); |
218 |
< |
Future f2 = ecs.take(); |
219 |
< |
assertSame("submit and take must return same objects", f1, f2); |
220 |
< |
assertTrue("completed task must have set done", done.get()); |
221 |
< |
} catch (Exception ex) { |
222 |
< |
unexpectedException(); |
223 |
< |
} finally { |
227 |
< |
joinPool(e); |
228 |
< |
} |
229 |
< |
} |
230 |
< |
|
231 |
< |
|
206 |
> |
protected <T> RunnableFuture<T> newTaskFor(Runnable t, T r) { |
207 |
> |
return new MyRunnableFuture<T>(t, r); |
208 |
> |
}}; |
209 |
> |
ExecutorCompletionService<String> ecs = |
210 |
> |
new ExecutorCompletionService<String>(e); |
211 |
> |
try { |
212 |
> |
assertNull(ecs.poll()); |
213 |
> |
Runnable r = new NoOpRunnable(); |
214 |
> |
Future f1 = ecs.submit(r, null); |
215 |
> |
assertTrue("submit must return MyRunnableFuture", |
216 |
> |
f1 instanceof MyRunnableFuture); |
217 |
> |
Future f2 = ecs.take(); |
218 |
> |
assertSame("submit and take must return same objects", f1, f2); |
219 |
> |
assertTrue("completed task must have set done", done.get()); |
220 |
> |
} finally { |
221 |
> |
joinPool(e); |
222 |
> |
} |
223 |
> |
} |
224 |
|
|
225 |
|
} |