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