1 |
dl |
1.1 |
/* |
2 |
dl |
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 |
jsr166 |
1.7 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
|
10 |
|
|
import junit.framework.*; |
11 |
|
|
import java.util.*; |
12 |
|
|
import java.util.concurrent.*; |
13 |
jsr166 |
1.9 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
dl |
1.5 |
import java.util.concurrent.atomic.*; |
15 |
dl |
1.1 |
import java.math.BigInteger; |
16 |
|
|
import java.security.*; |
17 |
|
|
|
18 |
jsr166 |
1.8 |
public class ExecutorCompletionServiceTest extends JSR166TestCase { |
19 |
dl |
1.1 |
public static void main(String[] args) { |
20 |
jsr166 |
1.11 |
junit.textui.TestRunner.run(suite()); |
21 |
dl |
1.1 |
} |
22 |
|
|
public static Test suite() { |
23 |
|
|
return new TestSuite(ExecutorCompletionServiceTest.class); |
24 |
|
|
} |
25 |
|
|
|
26 |
|
|
|
27 |
|
|
/** |
28 |
|
|
* Creating a new ECS with null Executor throw NPE |
29 |
jsr166 |
1.7 |
*/ |
30 |
dl |
1.1 |
public void testConstructorNPE() { |
31 |
|
|
try { |
32 |
|
|
ExecutorCompletionService ecs = new ExecutorCompletionService(null); |
33 |
|
|
shouldThrow(); |
34 |
jsr166 |
1.10 |
} catch (NullPointerException success) {} |
35 |
dl |
1.1 |
} |
36 |
|
|
|
37 |
|
|
/** |
38 |
|
|
* Creating a new ECS with null queue throw NPE |
39 |
jsr166 |
1.7 |
*/ |
40 |
dl |
1.1 |
public void testConstructorNPE2() { |
41 |
|
|
try { |
42 |
|
|
ExecutorService e = Executors.newCachedThreadPool(); |
43 |
|
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e, null); |
44 |
|
|
shouldThrow(); |
45 |
jsr166 |
1.10 |
} catch (NullPointerException success) {} |
46 |
dl |
1.1 |
} |
47 |
|
|
|
48 |
|
|
/** |
49 |
|
|
* Submitting a null callable throws NPE |
50 |
jsr166 |
1.7 |
*/ |
51 |
dl |
1.1 |
public void testSubmitNPE() { |
52 |
|
|
ExecutorService e = Executors.newCachedThreadPool(); |
53 |
|
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
54 |
|
|
try { |
55 |
|
|
Callable c = null; |
56 |
|
|
ecs.submit(c); |
57 |
|
|
shouldThrow(); |
58 |
|
|
} catch (NullPointerException success) { |
59 |
|
|
} finally { |
60 |
|
|
joinPool(e); |
61 |
|
|
} |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
/** |
65 |
|
|
* Submitting a null runnable throws NPE |
66 |
jsr166 |
1.7 |
*/ |
67 |
dl |
1.1 |
public void testSubmitNPE2() { |
68 |
|
|
ExecutorService e = Executors.newCachedThreadPool(); |
69 |
|
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
70 |
|
|
try { |
71 |
|
|
Runnable r = null; |
72 |
|
|
ecs.submit(r, Boolean.TRUE); |
73 |
|
|
shouldThrow(); |
74 |
|
|
} catch (NullPointerException success) { |
75 |
|
|
} finally { |
76 |
|
|
joinPool(e); |
77 |
|
|
} |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
/** |
81 |
|
|
* A taken submitted task is completed |
82 |
jsr166 |
1.7 |
*/ |
83 |
jsr166 |
1.10 |
public void testTake() throws InterruptedException { |
84 |
dl |
1.1 |
ExecutorService e = Executors.newCachedThreadPool(); |
85 |
|
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
86 |
|
|
try { |
87 |
|
|
Callable c = new StringTask(); |
88 |
|
|
ecs.submit(c); |
89 |
|
|
Future f = ecs.take(); |
90 |
dl |
1.3 |
assertTrue(f.isDone()); |
91 |
dl |
1.1 |
} finally { |
92 |
|
|
joinPool(e); |
93 |
|
|
} |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
/** |
97 |
|
|
* Take returns the same future object returned by submit |
98 |
jsr166 |
1.7 |
*/ |
99 |
jsr166 |
1.10 |
public void testTake2() throws InterruptedException { |
100 |
dl |
1.1 |
ExecutorService e = Executors.newCachedThreadPool(); |
101 |
|
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
102 |
|
|
try { |
103 |
|
|
Callable c = new StringTask(); |
104 |
|
|
Future f1 = ecs.submit(c); |
105 |
|
|
Future f2 = ecs.take(); |
106 |
|
|
assertSame(f1, f2); |
107 |
|
|
} finally { |
108 |
|
|
joinPool(e); |
109 |
|
|
} |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
/** |
113 |
|
|
* If poll returns non-null, the returned task is completed |
114 |
jsr166 |
1.7 |
*/ |
115 |
jsr166 |
1.10 |
public void testPoll1() throws InterruptedException { |
116 |
dl |
1.1 |
ExecutorService e = Executors.newCachedThreadPool(); |
117 |
|
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
118 |
|
|
try { |
119 |
|
|
assertNull(ecs.poll()); |
120 |
|
|
Callable c = new StringTask(); |
121 |
|
|
ecs.submit(c); |
122 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
123 |
|
|
for (;;) { |
124 |
|
|
Future f = ecs.poll(); |
125 |
|
|
if (f != null) { |
126 |
dl |
1.3 |
assertTrue(f.isDone()); |
127 |
dl |
1.1 |
break; |
128 |
|
|
} |
129 |
|
|
} |
130 |
|
|
} finally { |
131 |
|
|
joinPool(e); |
132 |
|
|
} |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
/** |
136 |
|
|
* If timed poll returns non-null, the returned task is completed |
137 |
jsr166 |
1.7 |
*/ |
138 |
jsr166 |
1.10 |
public void testPoll2() throws InterruptedException { |
139 |
dl |
1.1 |
ExecutorService e = Executors.newCachedThreadPool(); |
140 |
|
|
ExecutorCompletionService ecs = new ExecutorCompletionService(e); |
141 |
|
|
try { |
142 |
|
|
assertNull(ecs.poll()); |
143 |
|
|
Callable c = new StringTask(); |
144 |
|
|
ecs.submit(c); |
145 |
jsr166 |
1.9 |
Future f = ecs.poll(SHORT_DELAY_MS, MILLISECONDS); |
146 |
jsr166 |
1.7 |
if (f != null) |
147 |
dl |
1.3 |
assertTrue(f.isDone()); |
148 |
dl |
1.1 |
} finally { |
149 |
|
|
joinPool(e); |
150 |
|
|
} |
151 |
|
|
} |
152 |
dl |
1.4 |
/** |
153 |
|
|
* Submitting to underlying AES that overrides newTaskFor(Callable) |
154 |
|
|
* returns and eventually runs Future returned by newTaskFor. |
155 |
|
|
*/ |
156 |
jsr166 |
1.10 |
public void testNewTaskForCallable() throws InterruptedException { |
157 |
dl |
1.4 |
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 |
|
|
ExecutorService e = new ThreadPoolExecutor( |
163 |
|
|
1, 1, 30L, TimeUnit.SECONDS, |
164 |
|
|
new ArrayBlockingQueue<Runnable>(1)) { |
165 |
|
|
protected <T> RunnableFuture<T> newTaskFor(Callable<T> c) { |
166 |
|
|
return new MyCallableFuture<T>(c); |
167 |
|
|
} |
168 |
|
|
}; |
169 |
|
|
ExecutorCompletionService<String> ecs = |
170 |
|
|
new ExecutorCompletionService<String>(e); |
171 |
|
|
try { |
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 |
|
|
} finally { |
181 |
|
|
joinPool(e); |
182 |
|
|
} |
183 |
|
|
} |
184 |
|
|
|
185 |
|
|
/** |
186 |
|
|
* Submitting to underlying AES that overrides newTaskFor(Runnable,T) |
187 |
|
|
* returns and eventually runs Future returned by newTaskFor. |
188 |
|
|
*/ |
189 |
jsr166 |
1.10 |
public void testNewTaskForRunnable() throws InterruptedException { |
190 |
dl |
1.4 |
final AtomicBoolean done = new AtomicBoolean(false); |
191 |
|
|
class MyRunnableFuture<V> extends FutureTask<V> { |
192 |
|
|
MyRunnableFuture(Runnable t, V r) { super(t, r); } |
193 |
|
|
protected void done() { done.set(true); } |
194 |
|
|
} |
195 |
|
|
ExecutorService e = new ThreadPoolExecutor( |
196 |
|
|
1, 1, 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 |
|
|
}; |
202 |
|
|
ExecutorCompletionService<String> ecs = |
203 |
|
|
new ExecutorCompletionService<String>(e); |
204 |
|
|
try { |
205 |
|
|
assertNull(ecs.poll()); |
206 |
dl |
1.6 |
Runnable r = new NoOpRunnable(); |
207 |
|
|
Future f1 = ecs.submit(r, null); |
208 |
dl |
1.4 |
assertTrue("submit must return MyRunnableFuture", |
209 |
|
|
f1 instanceof MyRunnableFuture); |
210 |
|
|
Future f2 = ecs.take(); |
211 |
|
|
assertSame("submit and take must return same objects", f1, f2); |
212 |
|
|
assertTrue("completed task must have set done", done.get()); |
213 |
|
|
} finally { |
214 |
|
|
joinPool(e); |
215 |
|
|
} |
216 |
|
|
} |
217 |
|
|
|
218 |
dl |
1.1 |
} |