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 |
> |
main(suite(), args); |
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); |
61 |
< |
try { |
59 |
> |
final ExecutorService e = Executors.newCachedThreadPool(); |
60 |
> |
final ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
61 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
62 |
|
Callable c = null; |
63 |
< |
ecs.submit(c); |
64 |
< |
shouldThrow(); |
65 |
< |
} catch (NullPointerException success) { |
66 |
< |
} finally { |
61 |
< |
joinPool(e); |
63 |
> |
try { |
64 |
> |
ecs.submit(c); |
65 |
> |
shouldThrow(); |
66 |
> |
} catch (NullPointerException success) {} |
67 |
|
} |
68 |
|
} |
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); |
76 |
< |
try { |
74 |
> |
final ExecutorService e = Executors.newCachedThreadPool(); |
75 |
> |
final ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
76 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
77 |
|
Runnable r = null; |
78 |
< |
ecs.submit(r, Boolean.TRUE); |
79 |
< |
shouldThrow(); |
80 |
< |
} catch (NullPointerException success) { |
81 |
< |
} finally { |
77 |
< |
joinPool(e); |
78 |
> |
try { |
79 |
> |
ecs.submit(r, Boolean.TRUE); |
80 |
> |
shouldThrow(); |
81 |
> |
} catch (NullPointerException success) {} |
82 |
|
} |
83 |
|
} |
84 |
|
|
85 |
|
/** |
86 |
|
* A taken submitted task is completed |
87 |
< |
*/ |
88 |
< |
public void testTake() { |
89 |
< |
ExecutorService e = Executors.newCachedThreadPool(); |
90 |
< |
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
91 |
< |
try { |
87 |
> |
*/ |
88 |
> |
public void testTake() throws InterruptedException { |
89 |
> |
final ExecutorService e = Executors.newCachedThreadPool(); |
90 |
> |
final ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
91 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
92 |
|
Callable c = new StringTask(); |
93 |
|
ecs.submit(c); |
94 |
|
Future f = ecs.take(); |
95 |
|
assertTrue(f.isDone()); |
92 |
– |
} 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 |
< |
*/ |
102 |
< |
public void testTake2() { |
103 |
< |
ExecutorService e = Executors.newCachedThreadPool(); |
104 |
< |
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
105 |
< |
try { |
101 |
> |
*/ |
102 |
> |
public void testTake2() throws InterruptedException { |
103 |
> |
final ExecutorService e = Executors.newCachedThreadPool(); |
104 |
> |
final ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
105 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
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); |
110 |
|
} |
111 |
|
} |
112 |
|
|
113 |
|
/** |
114 |
|
* If poll returns non-null, the returned task is completed |
115 |
< |
*/ |
116 |
< |
public void testPoll1() { |
117 |
< |
ExecutorService e = Executors.newCachedThreadPool(); |
118 |
< |
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
119 |
< |
try { |
115 |
> |
*/ |
116 |
> |
public void testPoll1() throws Exception { |
117 |
> |
final ExecutorService e = Executors.newCachedThreadPool(); |
118 |
> |
final ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
119 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
120 |
|
assertNull(ecs.poll()); |
121 |
|
Callable c = new StringTask(); |
122 |
|
ecs.submit(c); |
123 |
< |
Thread.sleep(SHORT_DELAY_MS); |
124 |
< |
for (;;) { |
125 |
< |
Future f = ecs.poll(); |
126 |
< |
if (f != null) { |
127 |
< |
assertTrue(f.isDone()); |
128 |
< |
break; |
129 |
< |
} |
123 |
> |
|
124 |
> |
long startTime = System.nanoTime(); |
125 |
> |
Future f; |
126 |
> |
while ((f = ecs.poll()) == null) { |
127 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
128 |
> |
fail("timed out"); |
129 |
> |
Thread.yield(); |
130 |
|
} |
131 |
< |
} catch (Exception ex) { |
132 |
< |
unexpectedException(); |
137 |
< |
} finally { |
138 |
< |
joinPool(e); |
131 |
> |
assertTrue(f.isDone()); |
132 |
> |
assertSame(TEST_STRING, f.get()); |
133 |
|
} |
134 |
|
} |
135 |
|
|
136 |
|
/** |
137 |
|
* If timed poll returns non-null, the returned task is completed |
138 |
< |
*/ |
139 |
< |
public void testPoll2() { |
140 |
< |
ExecutorService e = Executors.newCachedThreadPool(); |
141 |
< |
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
142 |
< |
try { |
138 |
> |
*/ |
139 |
> |
public void testPoll2() throws InterruptedException { |
140 |
> |
final ExecutorService e = Executors.newCachedThreadPool(); |
141 |
> |
final ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
142 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
143 |
|
assertNull(ecs.poll()); |
144 |
|
Callable c = new StringTask(); |
145 |
|
ecs.submit(c); |
146 |
< |
Future f = ecs.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
147 |
< |
if (f != null) |
146 |
> |
Future f = ecs.poll(SHORT_DELAY_MS, MILLISECONDS); |
147 |
> |
if (f != null) |
148 |
|
assertTrue(f.isDone()); |
149 |
< |
} catch (Exception ex) { |
150 |
< |
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 |
< |
} |
149 |
> |
} |
150 |
> |
} |
151 |
|
|
152 |
+ |
/** |
153 |
+ |
* Submitting to underlying AES that overrides newTaskFor(Callable) |
154 |
+ |
* returns and eventually runs Future returned by newTaskFor. |
155 |
+ |
*/ |
156 |
+ |
public void testNewTaskForCallable() throws InterruptedException { |
157 |
+ |
final AtomicBoolean done = new AtomicBoolean(false); |
158 |
+ |
class MyCallableFuture<V> extends FutureTask<V> { |
159 |
+ |
MyCallableFuture(Callable<V> c) { super(c); } |
160 |
+ |
protected void done() { done.set(true); } |
161 |
+ |
} |
162 |
+ |
final ExecutorService e = |
163 |
+ |
new ThreadPoolExecutor(1, 1, |
164 |
+ |
30L, TimeUnit.SECONDS, |
165 |
+ |
new ArrayBlockingQueue<Runnable>(1)) { |
166 |
+ |
protected <T> RunnableFuture<T> newTaskFor(Callable<T> c) { |
167 |
+ |
return new MyCallableFuture<T>(c); |
168 |
+ |
}}; |
169 |
+ |
ExecutorCompletionService<String> ecs = |
170 |
+ |
new ExecutorCompletionService<String>(e); |
171 |
+ |
try (PoolCleaner cleaner = cleaner(e)) { |
172 |
+ |
assertNull(ecs.poll()); |
173 |
+ |
Callable<String> c = new StringTask(); |
174 |
+ |
Future f1 = ecs.submit(c); |
175 |
+ |
assertTrue("submit must return MyCallableFuture", |
176 |
+ |
f1 instanceof MyCallableFuture); |
177 |
+ |
Future f2 = ecs.take(); |
178 |
+ |
assertSame("submit and take must return same objects", f1, f2); |
179 |
+ |
assertTrue("completed task must have set done", done.get()); |
180 |
+ |
} |
181 |
+ |
} |
182 |
|
|
183 |
+ |
/** |
184 |
+ |
* Submitting to underlying AES that overrides newTaskFor(Runnable,T) |
185 |
+ |
* returns and eventually runs Future returned by newTaskFor. |
186 |
+ |
*/ |
187 |
+ |
public void testNewTaskForRunnable() throws InterruptedException { |
188 |
+ |
final AtomicBoolean done = new AtomicBoolean(false); |
189 |
+ |
class MyRunnableFuture<V> extends FutureTask<V> { |
190 |
+ |
MyRunnableFuture(Runnable t, V r) { super(t, r); } |
191 |
+ |
protected void done() { done.set(true); } |
192 |
+ |
} |
193 |
+ |
final ExecutorService e = |
194 |
+ |
new ThreadPoolExecutor(1, 1, |
195 |
+ |
30L, TimeUnit.SECONDS, |
196 |
+ |
new ArrayBlockingQueue<Runnable>(1)) { |
197 |
+ |
protected <T> RunnableFuture<T> newTaskFor(Runnable t, T r) { |
198 |
+ |
return new MyRunnableFuture<T>(t, r); |
199 |
+ |
}}; |
200 |
+ |
final ExecutorCompletionService<String> ecs = |
201 |
+ |
new ExecutorCompletionService<String>(e); |
202 |
+ |
try (PoolCleaner cleaner = cleaner(e)) { |
203 |
+ |
assertNull(ecs.poll()); |
204 |
+ |
Runnable r = new NoOpRunnable(); |
205 |
+ |
Future f1 = ecs.submit(r, null); |
206 |
+ |
assertTrue("submit must return MyRunnableFuture", |
207 |
+ |
f1 instanceof MyRunnableFuture); |
208 |
+ |
Future f2 = ecs.take(); |
209 |
+ |
assertSame("submit and take must return same objects", f1, f2); |
210 |
+ |
assertTrue("completed task must have set done", done.get()); |
211 |
+ |
} |
212 |
+ |
} |
213 |
|
|
214 |
|
} |