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