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/publicdomain/zero/1.0/ |
5 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
* Pat Fisher, Mike Judd. |
7 |
*/ |
8 |
|
9 |
import junit.framework.*; |
10 |
import java.util.*; |
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 { |
26 |
public static void main(String[] args) { |
27 |
junit.textui.TestRunner.run(suite()); |
28 |
} |
29 |
public static Test suite() { |
30 |
return new TestSuite(ExecutorCompletionServiceTest.class); |
31 |
} |
32 |
|
33 |
/** |
34 |
* Creating a new ECS with null Executor throw NPE |
35 |
*/ |
36 |
public void testConstructorNPE() { |
37 |
try { |
38 |
ExecutorCompletionService ecs = new ExecutorCompletionService(null); |
39 |
shouldThrow(); |
40 |
} catch (NullPointerException success) {} |
41 |
} |
42 |
|
43 |
/** |
44 |
* Creating a new ECS with null queue throw NPE |
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) {} |
52 |
} |
53 |
|
54 |
/** |
55 |
* Submitting a null callable throws NPE |
56 |
*/ |
57 |
public void testSubmitNPE() { |
58 |
ExecutorService e = Executors.newCachedThreadPool(); |
59 |
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
60 |
try { |
61 |
Callable c = null; |
62 |
ecs.submit(c); |
63 |
shouldThrow(); |
64 |
} catch (NullPointerException success) { |
65 |
} finally { |
66 |
joinPool(e); |
67 |
} |
68 |
} |
69 |
|
70 |
/** |
71 |
* Submitting a null runnable throws NPE |
72 |
*/ |
73 |
public void testSubmitNPE2() { |
74 |
ExecutorService e = Executors.newCachedThreadPool(); |
75 |
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
76 |
try { |
77 |
Runnable r = null; |
78 |
ecs.submit(r, Boolean.TRUE); |
79 |
shouldThrow(); |
80 |
} catch (NullPointerException success) { |
81 |
} finally { |
82 |
joinPool(e); |
83 |
} |
84 |
} |
85 |
|
86 |
/** |
87 |
* A taken submitted task is completed |
88 |
*/ |
89 |
public void testTake() throws InterruptedException { |
90 |
ExecutorService e = Executors.newCachedThreadPool(); |
91 |
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
92 |
try { |
93 |
Callable c = new StringTask(); |
94 |
ecs.submit(c); |
95 |
Future f = ecs.take(); |
96 |
assertTrue(f.isDone()); |
97 |
} finally { |
98 |
joinPool(e); |
99 |
} |
100 |
} |
101 |
|
102 |
/** |
103 |
* Take returns the same future object returned by submit |
104 |
*/ |
105 |
public void testTake2() throws InterruptedException { |
106 |
ExecutorService e = Executors.newCachedThreadPool(); |
107 |
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
108 |
try { |
109 |
Callable c = new StringTask(); |
110 |
Future f1 = ecs.submit(c); |
111 |
Future f2 = ecs.take(); |
112 |
assertSame(f1, f2); |
113 |
} finally { |
114 |
joinPool(e); |
115 |
} |
116 |
} |
117 |
|
118 |
/** |
119 |
* If poll returns non-null, the returned task is completed |
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 |
|
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 |
assertTrue(f.isDone()); |
137 |
assertSame(TEST_STRING, f.get()); |
138 |
} finally { |
139 |
joinPool(e); |
140 |
} |
141 |
} |
142 |
|
143 |
/** |
144 |
* If timed poll returns non-null, the returned task is completed |
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, MILLISECONDS); |
154 |
if (f != null) |
155 |
assertTrue(f.isDone()); |
156 |
} finally { |
157 |
joinPool(e); |
158 |
} |
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() 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 |
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 |
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 |
} |