1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
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 static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
|
11 |
< |
import junit.framework.*; |
12 |
< |
import java.util.*; |
13 |
< |
import java.util.concurrent.*; |
14 |
< |
import java.math.BigInteger; |
11 |
> |
import java.security.AccessControlContext; |
12 |
> |
import java.security.AccessControlException; |
13 |
> |
import java.security.AccessController; |
14 |
> |
import java.security.PrivilegedAction; |
15 |
> |
import java.security.PrivilegedExceptionAction; |
16 |
> |
import java.util.ArrayList; |
17 |
> |
import java.util.List; |
18 |
> |
import java.util.concurrent.Callable; |
19 |
> |
import java.util.concurrent.CountDownLatch; |
20 |
> |
import java.util.concurrent.Executors; |
21 |
> |
import java.util.concurrent.ExecutorService; |
22 |
> |
import java.util.concurrent.Future; |
23 |
> |
import java.util.concurrent.ScheduledExecutorService; |
24 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
25 |
|
|
26 |
< |
public class ExecutorsTest extends TestCase{ |
27 |
< |
|
26 |
> |
import junit.framework.Test; |
27 |
> |
import junit.framework.TestSuite; |
28 |
> |
|
29 |
> |
public class ExecutorsTest extends JSR166TestCase { |
30 |
|
public static void main(String[] args) { |
31 |
< |
junit.textui.TestRunner.run (suite()); |
31 |
> |
junit.textui.TestRunner.run(suite()); |
32 |
|
} |
19 |
– |
|
20 |
– |
|
33 |
|
public static Test suite() { |
34 |
< |
return new TestSuite(ExecutorsTest.class); |
34 |
> |
return new TestSuite(ExecutorsTest.class); |
35 |
|
} |
36 |
|
|
37 |
< |
private static long SHORT_DELAY_MS = 100; |
38 |
< |
private static long MEDIUM_DELAY_MS = 1000; |
39 |
< |
private static long LONG_DELAY_MS = 10000; |
40 |
< |
|
41 |
< |
class SleepRun implements Runnable { |
42 |
< |
public void run() { |
43 |
< |
try{ |
44 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
45 |
< |
} catch(InterruptedException e){ |
34 |
< |
fail("unexpected exception"); |
35 |
< |
} |
36 |
< |
} |
37 |
> |
/** |
38 |
> |
* A newCachedThreadPool can execute runnables |
39 |
> |
*/ |
40 |
> |
public void testNewCachedThreadPool1() { |
41 |
> |
ExecutorService e = Executors.newCachedThreadPool(); |
42 |
> |
e.execute(new NoOpRunnable()); |
43 |
> |
e.execute(new NoOpRunnable()); |
44 |
> |
e.execute(new NoOpRunnable()); |
45 |
> |
joinPool(e); |
46 |
|
} |
38 |
– |
|
47 |
|
|
48 |
< |
class SleepCall implements Callable { |
49 |
< |
public Object call(){ |
50 |
< |
try{ |
51 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
52 |
< |
}catch(InterruptedException e){ |
53 |
< |
fail("unexpected exception"); |
54 |
< |
} |
55 |
< |
return Boolean.TRUE; |
56 |
< |
} |
48 |
> |
/** |
49 |
> |
* A newCachedThreadPool with given ThreadFactory can execute runnables |
50 |
> |
*/ |
51 |
> |
public void testNewCachedThreadPool2() { |
52 |
> |
ExecutorService e = Executors.newCachedThreadPool(new SimpleThreadFactory()); |
53 |
> |
e.execute(new NoOpRunnable()); |
54 |
> |
e.execute(new NoOpRunnable()); |
55 |
> |
e.execute(new NoOpRunnable()); |
56 |
> |
joinPool(e); |
57 |
|
} |
58 |
|
|
51 |
– |
|
52 |
– |
|
59 |
|
/** |
60 |
< |
* Test to verify execute(Executor, Runnable) will throw |
61 |
< |
* RejectedExecutionException Attempting to execute a runnable on |
62 |
< |
* a full ThreadPool will cause such an exception here, up to 5 |
63 |
< |
* runnables are attempted on a pool capable on handling one |
64 |
< |
* until it throws an exception |
65 |
< |
*/ |
66 |
< |
public void testExecute1(){ |
61 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
62 |
< |
try{ |
63 |
< |
|
64 |
< |
for(int i = 0; i < 5; ++i){ |
65 |
< |
Executors.execute(p, new SleepRun(), Boolean.TRUE); |
66 |
< |
} |
67 |
< |
fail("should throw"); |
68 |
< |
} catch(RejectedExecutionException success){} |
69 |
< |
p.shutdownNow(); |
60 |
> |
* A newCachedThreadPool with null ThreadFactory throws NPE |
61 |
> |
*/ |
62 |
> |
public void testNewCachedThreadPool3() { |
63 |
> |
try { |
64 |
> |
ExecutorService e = Executors.newCachedThreadPool(null); |
65 |
> |
shouldThrow(); |
66 |
> |
} catch (NullPointerException success) {} |
67 |
|
} |
68 |
|
|
69 |
|
/** |
70 |
< |
* Test to verify execute(Executor, Callable) will throw |
71 |
< |
* RejectedExecutionException Attempting to execute a callable on |
72 |
< |
* a full ThreadPool will cause such an exception here, up to 5 |
73 |
< |
* runnables are attempted on a pool capable on handling one |
74 |
< |
* until it throws an exception |
75 |
< |
*/ |
76 |
< |
public void testExecute2(){ |
77 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
81 |
< |
try{ |
82 |
< |
for(int i = 0; i < 5; ++i) { |
83 |
< |
Executors.execute(p, new SleepCall()); |
84 |
< |
} |
85 |
< |
fail("should throw"); |
86 |
< |
}catch(RejectedExecutionException e){} |
87 |
< |
p.shutdownNow(); |
70 |
> |
* A new SingleThreadExecutor can execute runnables |
71 |
> |
*/ |
72 |
> |
public void testNewSingleThreadExecutor1() { |
73 |
> |
ExecutorService e = Executors.newSingleThreadExecutor(); |
74 |
> |
e.execute(new NoOpRunnable()); |
75 |
> |
e.execute(new NoOpRunnable()); |
76 |
> |
e.execute(new NoOpRunnable()); |
77 |
> |
joinPool(e); |
78 |
|
} |
79 |
|
|
80 |
+ |
/** |
81 |
+ |
* A new SingleThreadExecutor with given ThreadFactory can execute runnables |
82 |
+ |
*/ |
83 |
+ |
public void testNewSingleThreadExecutor2() { |
84 |
+ |
ExecutorService e = Executors.newSingleThreadExecutor(new SimpleThreadFactory()); |
85 |
+ |
e.execute(new NoOpRunnable()); |
86 |
+ |
e.execute(new NoOpRunnable()); |
87 |
+ |
e.execute(new NoOpRunnable()); |
88 |
+ |
joinPool(e); |
89 |
+ |
} |
90 |
|
|
91 |
|
/** |
92 |
< |
* Test to verify invoke(Executor, Runnable) throws InterruptedException |
93 |
< |
* A single use of invoke starts that will wait long enough |
94 |
< |
* for the invoking thread to be interrupted |
95 |
< |
*/ |
96 |
< |
public void testInvoke2(){ |
97 |
< |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
98 |
< |
Thread t = new Thread(new Runnable() { |
99 |
< |
public void run(){ |
100 |
< |
try{ |
101 |
< |
Executors.invoke(p,new Runnable(){ |
102 |
< |
public void run(){ |
103 |
< |
try{ |
104 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
105 |
< |
fail("should throw"); |
106 |
< |
}catch(InterruptedException e){ |
107 |
< |
} |
108 |
< |
} |
109 |
< |
}); |
110 |
< |
} catch(InterruptedException success){ |
111 |
< |
} catch(Exception e) { |
112 |
< |
fail("unexpected exception"); |
113 |
< |
} |
114 |
< |
|
115 |
< |
} |
116 |
< |
}); |
117 |
< |
try{ |
118 |
< |
t.start(); |
119 |
< |
Thread.sleep(SHORT_DELAY_MS); |
120 |
< |
t.interrupt(); |
121 |
< |
}catch(Exception e){ |
122 |
< |
fail("unexpected exception"); |
123 |
< |
} |
124 |
< |
p.shutdownNow(); |
92 |
> |
* A new SingleThreadExecutor with null ThreadFactory throws NPE |
93 |
> |
*/ |
94 |
> |
public void testNewSingleThreadExecutor3() { |
95 |
> |
try { |
96 |
> |
ExecutorService e = Executors.newSingleThreadExecutor(null); |
97 |
> |
shouldThrow(); |
98 |
> |
} catch (NullPointerException success) {} |
99 |
|
} |
100 |
|
|
101 |
|
/** |
102 |
< |
* Test to verify invoke(Executor, Runnable) will throw |
103 |
< |
* ExecutionException An ExecutionException occurs when the |
104 |
< |
* underlying Runnable throws an exception, here the |
105 |
< |
* DivideByZeroException will cause an ExecutionException |
106 |
< |
*/ |
107 |
< |
public void testInvoke3(){ |
108 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
109 |
< |
try{ |
110 |
< |
Runnable r = new Runnable(){ |
111 |
< |
public void run(){ |
138 |
< |
int i = 5/0; |
139 |
< |
} |
140 |
< |
}; |
141 |
< |
|
142 |
< |
for(int i =0; i < 5; i++){ |
143 |
< |
Executors.invoke(p,r); |
144 |
< |
} |
145 |
< |
|
146 |
< |
fail("should throw"); |
147 |
< |
} catch(ExecutionException success){ |
148 |
< |
} catch(Exception e){ |
149 |
< |
fail("should throw EE"); |
102 |
> |
* A new SingleThreadExecutor cannot be casted to concrete implementation |
103 |
> |
*/ |
104 |
> |
public void testCastNewSingleThreadExecutor() { |
105 |
> |
ExecutorService e = Executors.newSingleThreadExecutor(); |
106 |
> |
try { |
107 |
> |
ThreadPoolExecutor tpe = (ThreadPoolExecutor)e; |
108 |
> |
shouldThrow(); |
109 |
> |
} catch (ClassCastException success) { |
110 |
> |
} finally { |
111 |
> |
joinPool(e); |
112 |
|
} |
151 |
– |
p.shutdownNow(); |
113 |
|
} |
114 |
|
|
115 |
< |
|
115 |
> |
/** |
116 |
> |
* A new newFixedThreadPool can execute runnables |
117 |
> |
*/ |
118 |
> |
public void testNewFixedThreadPool1() { |
119 |
> |
ExecutorService e = Executors.newFixedThreadPool(2); |
120 |
> |
e.execute(new NoOpRunnable()); |
121 |
> |
e.execute(new NoOpRunnable()); |
122 |
> |
e.execute(new NoOpRunnable()); |
123 |
> |
joinPool(e); |
124 |
> |
} |
125 |
|
|
126 |
|
/** |
127 |
< |
* Test to verify invoke(Executor, Callable) throws |
158 |
< |
* InterruptedException A single use of invoke starts that will |
159 |
< |
* wait long enough for the invoking thread to be interrupted |
127 |
> |
* A new newFixedThreadPool with given ThreadFactory can execute runnables |
128 |
|
*/ |
129 |
< |
public void testInvoke5(){ |
130 |
< |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
131 |
< |
|
132 |
< |
final Callable c = new Callable(){ |
133 |
< |
public Object call(){ |
134 |
< |
try{ |
135 |
< |
Executors.invoke(p, new SleepCall()); |
168 |
< |
fail("should throw"); |
169 |
< |
}catch(InterruptedException e){} |
170 |
< |
catch(RejectedExecutionException e2){} |
171 |
< |
catch(ExecutionException e3){} |
172 |
< |
return Boolean.TRUE; |
173 |
< |
} |
174 |
< |
}; |
129 |
> |
public void testNewFixedThreadPool2() { |
130 |
> |
ExecutorService e = Executors.newFixedThreadPool(2, new SimpleThreadFactory()); |
131 |
> |
e.execute(new NoOpRunnable()); |
132 |
> |
e.execute(new NoOpRunnable()); |
133 |
> |
e.execute(new NoOpRunnable()); |
134 |
> |
joinPool(e); |
135 |
> |
} |
136 |
|
|
137 |
+ |
/** |
138 |
+ |
* A new newFixedThreadPool with null ThreadFactory throws NPE |
139 |
+ |
*/ |
140 |
+ |
public void testNewFixedThreadPool3() { |
141 |
+ |
try { |
142 |
+ |
ExecutorService e = Executors.newFixedThreadPool(2, null); |
143 |
+ |
shouldThrow(); |
144 |
+ |
} catch (NullPointerException success) {} |
145 |
+ |
} |
146 |
|
|
147 |
< |
|
148 |
< |
Thread t = new Thread(new Runnable(){ |
149 |
< |
public void run(){ |
150 |
< |
try{ |
151 |
< |
c.call(); |
152 |
< |
}catch(Exception e){} |
153 |
< |
} |
154 |
< |
}); |
185 |
< |
try{ |
186 |
< |
t.start(); |
187 |
< |
Thread.sleep(SHORT_DELAY_MS); |
188 |
< |
t.interrupt(); |
189 |
< |
t.join(); |
190 |
< |
}catch(InterruptedException e){ |
191 |
< |
fail("unexpected exception"); |
192 |
< |
} |
193 |
< |
|
194 |
< |
p.shutdownNow(); |
147 |
> |
/** |
148 |
> |
* A new newFixedThreadPool with 0 threads throws IAE |
149 |
> |
*/ |
150 |
> |
public void testNewFixedThreadPool4() { |
151 |
> |
try { |
152 |
> |
ExecutorService e = Executors.newFixedThreadPool(0); |
153 |
> |
shouldThrow(); |
154 |
> |
} catch (IllegalArgumentException success) {} |
155 |
|
} |
156 |
|
|
157 |
|
/** |
158 |
< |
* Test to verify invoke(Executor, Callable) will throw ExecutionException |
159 |
< |
* An ExecutionException occurs when the underlying Runnable throws |
160 |
< |
* an exception, here the DivideByZeroException will cause an ExecutionException |
161 |
< |
*/ |
162 |
< |
public void testInvoke6(){ |
163 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
164 |
< |
|
165 |
< |
try{ |
206 |
< |
Callable c = new Callable(){ |
207 |
< |
public Object call(){ |
208 |
< |
int i = 5/0; |
209 |
< |
return Boolean.TRUE; |
210 |
< |
} |
211 |
< |
}; |
212 |
< |
|
213 |
< |
for(int i =0; i < 5; i++){ |
214 |
< |
Executors.invoke(p,c); |
215 |
< |
} |
216 |
< |
|
217 |
< |
fail("should throw"); |
218 |
< |
}catch(RejectedExecutionException e){} |
219 |
< |
catch(InterruptedException e2){} |
220 |
< |
catch(ExecutionException e3){} |
221 |
< |
p.shutdownNow(); |
158 |
> |
* An unconfigurable newFixedThreadPool can execute runnables |
159 |
> |
*/ |
160 |
> |
public void testUnconfigurableExecutorService() { |
161 |
> |
ExecutorService e = Executors.unconfigurableExecutorService(Executors.newFixedThreadPool(2)); |
162 |
> |
e.execute(new NoOpRunnable()); |
163 |
> |
e.execute(new NoOpRunnable()); |
164 |
> |
e.execute(new NoOpRunnable()); |
165 |
> |
joinPool(e); |
166 |
|
} |
167 |
|
|
168 |
< |
public void testExecuteRunnable () { |
168 |
> |
/** |
169 |
> |
* unconfigurableExecutorService(null) throws NPE |
170 |
> |
*/ |
171 |
> |
public void testUnconfigurableExecutorServiceNPE() { |
172 |
|
try { |
173 |
< |
Executor e = new DirectExecutor(); |
174 |
< |
Task task = new Task(); |
175 |
< |
|
176 |
< |
assertFalse("task should not be complete", task.isCompleted()); |
173 |
> |
ExecutorService e = Executors.unconfigurableExecutorService(null); |
174 |
> |
shouldThrow(); |
175 |
> |
} catch (NullPointerException success) {} |
176 |
> |
} |
177 |
|
|
178 |
< |
Future<String> future = Executors.execute(e, task, TEST_STRING); |
179 |
< |
String result = future.get(); |
178 |
> |
/** |
179 |
> |
* unconfigurableScheduledExecutorService(null) throws NPE |
180 |
> |
*/ |
181 |
> |
public void testUnconfigurableScheduledExecutorServiceNPE() { |
182 |
> |
try { |
183 |
> |
ExecutorService e = Executors.unconfigurableScheduledExecutorService(null); |
184 |
> |
shouldThrow(); |
185 |
> |
} catch (NullPointerException success) {} |
186 |
> |
} |
187 |
|
|
188 |
< |
assertTrue("task should be complete", task.isCompleted()); |
189 |
< |
assertSame("should return test string", TEST_STRING, result); |
190 |
< |
} |
191 |
< |
catch (ExecutionException ex) { |
192 |
< |
fail("Unexpected exception"); |
188 |
> |
/** |
189 |
> |
* a newSingleThreadScheduledExecutor successfully runs delayed task |
190 |
> |
*/ |
191 |
> |
public void testNewSingleThreadScheduledExecutor() throws Exception { |
192 |
> |
ScheduledExecutorService p = Executors.newSingleThreadScheduledExecutor(); |
193 |
> |
try { |
194 |
> |
final CountDownLatch proceed = new CountDownLatch(1); |
195 |
> |
final Runnable task = new CheckedRunnable() { |
196 |
> |
public void realRun() { |
197 |
> |
await(proceed); |
198 |
> |
}}; |
199 |
> |
long startTime = System.nanoTime(); |
200 |
> |
Future f = p.schedule(Executors.callable(task, Boolean.TRUE), |
201 |
> |
timeoutMillis(), MILLISECONDS); |
202 |
> |
assertFalse(f.isDone()); |
203 |
> |
proceed.countDown(); |
204 |
> |
assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS)); |
205 |
> |
assertSame(Boolean.TRUE, f.get()); |
206 |
> |
assertTrue(f.isDone()); |
207 |
> |
assertFalse(f.isCancelled()); |
208 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
209 |
> |
} finally { |
210 |
> |
joinPool(p); |
211 |
|
} |
212 |
< |
catch (InterruptedException ex) { |
213 |
< |
fail("Unexpected exception"); |
212 |
> |
} |
213 |
> |
|
214 |
> |
/** |
215 |
> |
* a newScheduledThreadPool successfully runs delayed task |
216 |
> |
*/ |
217 |
> |
public void testNewScheduledThreadPool() throws Exception { |
218 |
> |
ScheduledExecutorService p = Executors.newScheduledThreadPool(2); |
219 |
> |
try { |
220 |
> |
final CountDownLatch proceed = new CountDownLatch(1); |
221 |
> |
final Runnable task = new CheckedRunnable() { |
222 |
> |
public void realRun() { |
223 |
> |
await(proceed); |
224 |
> |
}}; |
225 |
> |
long startTime = System.nanoTime(); |
226 |
> |
Future f = p.schedule(Executors.callable(task, Boolean.TRUE), |
227 |
> |
timeoutMillis(), MILLISECONDS); |
228 |
> |
assertFalse(f.isDone()); |
229 |
> |
proceed.countDown(); |
230 |
> |
assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS)); |
231 |
> |
assertSame(Boolean.TRUE, f.get()); |
232 |
> |
assertTrue(f.isDone()); |
233 |
> |
assertFalse(f.isCancelled()); |
234 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
235 |
> |
} finally { |
236 |
> |
joinPool(p); |
237 |
|
} |
238 |
|
} |
239 |
|
|
240 |
< |
public void testInvokeRunnable () { |
240 |
> |
/** |
241 |
> |
* an unconfigurable newScheduledThreadPool successfully runs delayed task |
242 |
> |
*/ |
243 |
> |
public void testUnconfigurableScheduledExecutorService() throws Exception { |
244 |
> |
ScheduledExecutorService p = |
245 |
> |
Executors.unconfigurableScheduledExecutorService |
246 |
> |
(Executors.newScheduledThreadPool(2)); |
247 |
|
try { |
248 |
< |
Executor e = new DirectExecutor(); |
249 |
< |
Task task = new Task(); |
248 |
> |
final CountDownLatch proceed = new CountDownLatch(1); |
249 |
> |
final Runnable task = new CheckedRunnable() { |
250 |
> |
public void realRun() { |
251 |
> |
await(proceed); |
252 |
> |
}}; |
253 |
> |
long startTime = System.nanoTime(); |
254 |
> |
Future f = p.schedule(Executors.callable(task, Boolean.TRUE), |
255 |
> |
timeoutMillis(), MILLISECONDS); |
256 |
> |
assertFalse(f.isDone()); |
257 |
> |
proceed.countDown(); |
258 |
> |
assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS)); |
259 |
> |
assertSame(Boolean.TRUE, f.get()); |
260 |
> |
assertTrue(f.isDone()); |
261 |
> |
assertFalse(f.isCancelled()); |
262 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
263 |
> |
} finally { |
264 |
> |
joinPool(p); |
265 |
> |
} |
266 |
> |
} |
267 |
|
|
268 |
< |
assertFalse("task should not be complete", task.isCompleted()); |
268 |
> |
/** |
269 |
> |
* Future.get on submitted tasks will time out if they compute too long. |
270 |
> |
*/ |
271 |
> |
public void testTimedCallable() throws Exception { |
272 |
> |
final ExecutorService[] executors = { |
273 |
> |
Executors.newSingleThreadExecutor(), |
274 |
> |
Executors.newCachedThreadPool(), |
275 |
> |
Executors.newFixedThreadPool(2), |
276 |
> |
Executors.newScheduledThreadPool(2), |
277 |
> |
}; |
278 |
> |
|
279 |
> |
final Runnable sleeper = new CheckedInterruptedRunnable() { |
280 |
> |
public void realRun() throws InterruptedException { |
281 |
> |
delay(LONG_DELAY_MS); |
282 |
> |
}}; |
283 |
> |
|
284 |
> |
List<Thread> threads = new ArrayList<Thread>(); |
285 |
> |
for (final ExecutorService executor : executors) { |
286 |
> |
threads.add(newStartedThread(new CheckedRunnable() { |
287 |
> |
public void realRun() { |
288 |
> |
Future future = executor.submit(sleeper); |
289 |
> |
assertFutureTimesOut(future); |
290 |
> |
}})); |
291 |
> |
} |
292 |
> |
for (Thread thread : threads) |
293 |
> |
awaitTermination(thread); |
294 |
> |
for (ExecutorService executor : executors) |
295 |
> |
joinPool(executor); |
296 |
> |
} |
297 |
> |
|
298 |
> |
/** |
299 |
> |
* ThreadPoolExecutor using defaultThreadFactory has |
300 |
> |
* specified group, priority, daemon status, and name |
301 |
> |
*/ |
302 |
> |
public void testDefaultThreadFactory() throws Exception { |
303 |
> |
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
304 |
> |
final CountDownLatch done = new CountDownLatch(1); |
305 |
> |
Runnable r = new CheckedRunnable() { |
306 |
> |
public void realRun() { |
307 |
> |
try { |
308 |
> |
Thread current = Thread.currentThread(); |
309 |
> |
assertTrue(!current.isDaemon()); |
310 |
> |
assertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
311 |
> |
ThreadGroup g = current.getThreadGroup(); |
312 |
> |
SecurityManager s = System.getSecurityManager(); |
313 |
> |
if (s != null) |
314 |
> |
assertTrue(g == s.getThreadGroup()); |
315 |
> |
else |
316 |
> |
assertTrue(g == egroup); |
317 |
> |
String name = current.getName(); |
318 |
> |
assertTrue(name.endsWith("thread-1")); |
319 |
> |
} catch (SecurityException ok) { |
320 |
> |
// Also pass if not allowed to change setting |
321 |
> |
} |
322 |
> |
done.countDown(); |
323 |
> |
}}; |
324 |
> |
ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory()); |
325 |
|
|
326 |
< |
Executors.invoke(e, task); |
326 |
> |
e.execute(r); |
327 |
> |
await(done); |
328 |
|
|
329 |
< |
assertTrue("task should be complete", task.isCompleted()); |
330 |
< |
} |
331 |
< |
catch (ExecutionException ex) { |
257 |
< |
fail("Unexpected exception"); |
258 |
< |
} |
259 |
< |
catch (InterruptedException ex) { |
260 |
< |
fail("Unexpected exception"); |
329 |
> |
try { |
330 |
> |
e.shutdown(); |
331 |
> |
} catch (SecurityException ok) { |
332 |
|
} |
262 |
– |
} |
333 |
|
|
334 |
< |
public void testExecuteCallable () { |
335 |
< |
try { |
266 |
< |
Executor e = new DirectExecutor(); |
267 |
< |
Future<String> future = Executors.execute(e, new StringTask()); |
268 |
< |
String result = future.get(); |
334 |
> |
joinPool(e); |
335 |
> |
} |
336 |
|
|
337 |
< |
assertSame("should return test string", TEST_STRING, result); |
337 |
> |
/** |
338 |
> |
* ThreadPoolExecutor using privilegedThreadFactory has |
339 |
> |
* specified group, priority, daemon status, name, |
340 |
> |
* access control context and context class loader |
341 |
> |
*/ |
342 |
> |
public void testPrivilegedThreadFactory() throws Exception { |
343 |
> |
final CountDownLatch done = new CountDownLatch(1); |
344 |
> |
Runnable r = new CheckedRunnable() { |
345 |
> |
public void realRun() throws Exception { |
346 |
> |
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
347 |
> |
final ClassLoader thisccl = Thread.currentThread().getContextClassLoader(); |
348 |
> |
final AccessControlContext thisacc = AccessController.getContext(); |
349 |
> |
Runnable r = new CheckedRunnable() { |
350 |
> |
public void realRun() { |
351 |
> |
Thread current = Thread.currentThread(); |
352 |
> |
assertTrue(!current.isDaemon()); |
353 |
> |
assertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
354 |
> |
ThreadGroup g = current.getThreadGroup(); |
355 |
> |
SecurityManager s = System.getSecurityManager(); |
356 |
> |
if (s != null) |
357 |
> |
assertTrue(g == s.getThreadGroup()); |
358 |
> |
else |
359 |
> |
assertTrue(g == egroup); |
360 |
> |
String name = current.getName(); |
361 |
> |
assertTrue(name.endsWith("thread-1")); |
362 |
> |
assertSame(thisccl, current.getContextClassLoader()); |
363 |
> |
assertEquals(thisacc, AccessController.getContext()); |
364 |
> |
done.countDown(); |
365 |
> |
}}; |
366 |
> |
ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); |
367 |
> |
e.execute(r); |
368 |
> |
await(done); |
369 |
> |
e.shutdown(); |
370 |
> |
joinPool(e); |
371 |
> |
}}; |
372 |
> |
|
373 |
> |
runWithPermissions(r, |
374 |
> |
new RuntimePermission("getClassLoader"), |
375 |
> |
new RuntimePermission("setContextClassLoader"), |
376 |
> |
new RuntimePermission("modifyThread")); |
377 |
> |
} |
378 |
> |
|
379 |
> |
boolean haveCCLPermissions() { |
380 |
> |
SecurityManager sm = System.getSecurityManager(); |
381 |
> |
if (sm != null) { |
382 |
> |
try { |
383 |
> |
sm.checkPermission(new RuntimePermission("setContextClassLoader")); |
384 |
> |
sm.checkPermission(new RuntimePermission("getClassLoader")); |
385 |
> |
} catch (AccessControlException e) { |
386 |
> |
return false; |
387 |
> |
} |
388 |
|
} |
389 |
< |
catch (ExecutionException ex) { |
390 |
< |
fail("Unexpected exception"); |
389 |
> |
return true; |
390 |
> |
} |
391 |
> |
|
392 |
> |
void checkCCL() { |
393 |
> |
SecurityManager sm = System.getSecurityManager(); |
394 |
> |
if (sm != null) { |
395 |
> |
sm.checkPermission(new RuntimePermission("setContextClassLoader")); |
396 |
> |
sm.checkPermission(new RuntimePermission("getClassLoader")); |
397 |
|
} |
398 |
< |
catch (InterruptedException ex) { |
399 |
< |
fail("Unexpected exception"); |
398 |
> |
} |
399 |
> |
|
400 |
> |
class CheckCCL implements Callable<Object> { |
401 |
> |
public Object call() { |
402 |
> |
checkCCL(); |
403 |
> |
return null; |
404 |
|
} |
405 |
|
} |
406 |
|
|
407 |
< |
public void testInvokeCallable () { |
408 |
< |
try { |
409 |
< |
Executor e = new DirectExecutor(); |
410 |
< |
String result = Executors.invoke(e, new StringTask()); |
407 |
> |
/** |
408 |
> |
* Without class loader permissions, creating |
409 |
> |
* privilegedCallableUsingCurrentClassLoader throws ACE |
410 |
> |
*/ |
411 |
> |
public void testCreatePrivilegedCallableUsingCCLWithNoPrivs() { |
412 |
> |
Runnable r = new CheckedRunnable() { |
413 |
> |
public void realRun() throws Exception { |
414 |
> |
if (System.getSecurityManager() == null) |
415 |
> |
return; |
416 |
> |
try { |
417 |
> |
Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); |
418 |
> |
shouldThrow(); |
419 |
> |
} catch (AccessControlException success) {} |
420 |
> |
}}; |
421 |
|
|
422 |
< |
assertSame("should return test string", TEST_STRING, result); |
286 |
< |
} |
287 |
< |
catch (ExecutionException ex) { |
288 |
< |
fail("Unexpected exception" ); |
289 |
< |
} |
290 |
< |
catch (InterruptedException ex) { |
291 |
< |
fail("Unexpected exception"); |
292 |
< |
} |
422 |
> |
runWithoutPermissions(r); |
423 |
|
} |
424 |
|
|
425 |
< |
private static final String TEST_STRING = "a test string"; |
425 |
> |
/** |
426 |
> |
* With class loader permissions, calling |
427 |
> |
* privilegedCallableUsingCurrentClassLoader does not throw ACE |
428 |
> |
*/ |
429 |
> |
public void testPrivilegedCallableUsingCCLWithPrivs() throws Exception { |
430 |
> |
Runnable r = new CheckedRunnable() { |
431 |
> |
public void realRun() throws Exception { |
432 |
> |
Executors.privilegedCallableUsingCurrentClassLoader |
433 |
> |
(new NoOpCallable()) |
434 |
> |
.call(); |
435 |
> |
}}; |
436 |
> |
|
437 |
> |
runWithPermissions(r, |
438 |
> |
new RuntimePermission("getClassLoader"), |
439 |
> |
new RuntimePermission("setContextClassLoader")); |
440 |
> |
} |
441 |
|
|
442 |
< |
private static class Task implements Runnable { |
443 |
< |
public void run() { completed = true; } |
444 |
< |
public boolean isCompleted() { return completed; } |
445 |
< |
public void reset() { completed = false; } |
446 |
< |
private boolean completed = false; |
442 |
> |
/** |
443 |
> |
* Without permissions, calling privilegedCallable throws ACE |
444 |
> |
*/ |
445 |
> |
public void testPrivilegedCallableWithNoPrivs() throws Exception { |
446 |
> |
// Avoid classloader-related SecurityExceptions in swingui.TestRunner |
447 |
> |
Executors.privilegedCallable(new CheckCCL()); |
448 |
> |
|
449 |
> |
Runnable r = new CheckedRunnable() { |
450 |
> |
public void realRun() throws Exception { |
451 |
> |
if (System.getSecurityManager() == null) |
452 |
> |
return; |
453 |
> |
Callable task = Executors.privilegedCallable(new CheckCCL()); |
454 |
> |
try { |
455 |
> |
task.call(); |
456 |
> |
shouldThrow(); |
457 |
> |
} catch (AccessControlException success) {} |
458 |
> |
}}; |
459 |
> |
|
460 |
> |
runWithoutPermissions(r); |
461 |
> |
|
462 |
> |
// It seems rather difficult to test that the |
463 |
> |
// AccessControlContext of the privilegedCallable is used |
464 |
> |
// instead of its caller. Below is a failed attempt to do |
465 |
> |
// that, which does not work because the AccessController |
466 |
> |
// cannot capture the internal state of the current Policy. |
467 |
> |
// It would be much more work to differentiate based on, |
468 |
> |
// e.g. CodeSource. |
469 |
> |
|
470 |
> |
// final AccessControlContext[] noprivAcc = new AccessControlContext[1]; |
471 |
> |
// final Callable[] task = new Callable[1]; |
472 |
> |
|
473 |
> |
// runWithPermissions |
474 |
> |
// (new CheckedRunnable() { |
475 |
> |
// public void realRun() { |
476 |
> |
// if (System.getSecurityManager() == null) |
477 |
> |
// return; |
478 |
> |
// noprivAcc[0] = AccessController.getContext(); |
479 |
> |
// task[0] = Executors.privilegedCallable(new CheckCCL()); |
480 |
> |
// try { |
481 |
> |
// AccessController.doPrivileged(new PrivilegedAction<Void>() { |
482 |
> |
// public Void run() { |
483 |
> |
// checkCCL(); |
484 |
> |
// return null; |
485 |
> |
// }}, noprivAcc[0]); |
486 |
> |
// shouldThrow(); |
487 |
> |
// } catch (AccessControlException success) {} |
488 |
> |
// }}); |
489 |
> |
|
490 |
> |
// runWithPermissions |
491 |
> |
// (new CheckedRunnable() { |
492 |
> |
// public void realRun() throws Exception { |
493 |
> |
// if (System.getSecurityManager() == null) |
494 |
> |
// return; |
495 |
> |
// // Verify that we have an underprivileged ACC |
496 |
> |
// try { |
497 |
> |
// AccessController.doPrivileged(new PrivilegedAction<Void>() { |
498 |
> |
// public Void run() { |
499 |
> |
// checkCCL(); |
500 |
> |
// return null; |
501 |
> |
// }}, noprivAcc[0]); |
502 |
> |
// shouldThrow(); |
503 |
> |
// } catch (AccessControlException success) {} |
504 |
> |
|
505 |
> |
// try { |
506 |
> |
// task[0].call(); |
507 |
> |
// shouldThrow(); |
508 |
> |
// } catch (AccessControlException success) {} |
509 |
> |
// }}, |
510 |
> |
// new RuntimePermission("getClassLoader"), |
511 |
> |
// new RuntimePermission("setContextClassLoader")); |
512 |
|
} |
513 |
|
|
514 |
< |
private static class StringTask implements Callable<String> { |
515 |
< |
public String call() { return TEST_STRING; } |
514 |
> |
/** |
515 |
> |
* With permissions, calling privilegedCallable succeeds |
516 |
> |
*/ |
517 |
> |
public void testPrivilegedCallableWithPrivs() throws Exception { |
518 |
> |
Runnable r = new CheckedRunnable() { |
519 |
> |
public void realRun() throws Exception { |
520 |
> |
Executors.privilegedCallable(new CheckCCL()).call(); |
521 |
> |
}}; |
522 |
> |
|
523 |
> |
runWithPermissions(r, |
524 |
> |
new RuntimePermission("getClassLoader"), |
525 |
> |
new RuntimePermission("setContextClassLoader")); |
526 |
|
} |
527 |
|
|
528 |
< |
static class DirectExecutor implements Executor { |
529 |
< |
public void execute(Runnable r) { |
530 |
< |
r.run(); |
531 |
< |
} |
528 |
> |
/** |
529 |
> |
* callable(Runnable) returns null when called |
530 |
> |
*/ |
531 |
> |
public void testCallable1() throws Exception { |
532 |
> |
Callable c = Executors.callable(new NoOpRunnable()); |
533 |
> |
assertNull(c.call()); |
534 |
|
} |
535 |
|
|
536 |
|
/** |
537 |
< |
* Check that timeouts from execute will time out if they compute |
316 |
< |
* too long. |
537 |
> |
* callable(Runnable, result) returns result when called |
538 |
|
*/ |
539 |
+ |
public void testCallable2() throws Exception { |
540 |
+ |
Callable c = Executors.callable(new NoOpRunnable(), one); |
541 |
+ |
assertSame(one, c.call()); |
542 |
+ |
} |
543 |
|
|
544 |
< |
public void testTimedCallable() { |
545 |
< |
int N = 10000; |
546 |
< |
ExecutorService executor = Executors.newSingleThreadExecutor(); |
547 |
< |
List<Callable<BigInteger>> tasks = new ArrayList<Callable<BigInteger>>(N); |
548 |
< |
try { |
549 |
< |
long startTime = System.currentTimeMillis(); |
550 |
< |
|
326 |
< |
long i = 0; |
327 |
< |
while (tasks.size() < N) { |
328 |
< |
tasks.add(new TimedCallable<BigInteger>(executor, new Fib(i), 1)); |
329 |
< |
i += 10; |
330 |
< |
} |
331 |
< |
|
332 |
< |
int iters = 0; |
333 |
< |
BigInteger sum = BigInteger.ZERO; |
334 |
< |
for (Iterator<Callable<BigInteger>> it = tasks.iterator(); it.hasNext();) { |
335 |
< |
try { |
336 |
< |
++iters; |
337 |
< |
sum = sum.add(it.next().call()); |
338 |
< |
} |
339 |
< |
catch (TimeoutException success) { |
340 |
< |
assertTrue(iters > 0); |
341 |
< |
return; |
342 |
< |
} |
343 |
< |
catch (Exception e) { |
344 |
< |
fail("unexpected exception: " + e); |
345 |
< |
} |
346 |
< |
} |
347 |
< |
// if by chance we didn't ever time out, total time must be small |
348 |
< |
long elapsed = System.currentTimeMillis() - startTime; |
349 |
< |
assertTrue(elapsed < N); |
350 |
< |
} |
351 |
< |
finally { |
352 |
< |
executor.shutdownNow(); |
353 |
< |
} |
544 |
> |
/** |
545 |
> |
* callable(PrivilegedAction) returns its result when called |
546 |
> |
*/ |
547 |
> |
public void testCallable3() throws Exception { |
548 |
> |
Callable c = Executors.callable(new PrivilegedAction() { |
549 |
> |
public Object run() { return one; }}); |
550 |
> |
assertSame(one, c.call()); |
551 |
|
} |
552 |
|
|
553 |
< |
|
554 |
< |
static class TimedCallable<T> implements Callable<T> { |
555 |
< |
private final Executor exec; |
556 |
< |
private final Callable<T> func; |
557 |
< |
private final long msecs; |
558 |
< |
|
559 |
< |
TimedCallable(Executor exec, Callable<T> func, long msecs) { |
363 |
< |
this.exec = exec; |
364 |
< |
this.func = func; |
365 |
< |
this.msecs = msecs; |
366 |
< |
} |
367 |
< |
|
368 |
< |
public T call() throws Exception { |
369 |
< |
Future<T> ftask = Executors.execute(exec, func); |
370 |
< |
try { |
371 |
< |
return ftask.get(msecs, TimeUnit.MILLISECONDS); |
372 |
< |
} finally { |
373 |
< |
ftask.cancel(true); |
374 |
< |
} |
375 |
< |
} |
553 |
> |
/** |
554 |
> |
* callable(PrivilegedExceptionAction) returns its result when called |
555 |
> |
*/ |
556 |
> |
public void testCallable4() throws Exception { |
557 |
> |
Callable c = Executors.callable(new PrivilegedExceptionAction() { |
558 |
> |
public Object run() { return one; }}); |
559 |
> |
assertSame(one, c.call()); |
560 |
|
} |
561 |
|
|
562 |
+ |
/** |
563 |
+ |
* callable(null Runnable) throws NPE |
564 |
+ |
*/ |
565 |
+ |
public void testCallableNPE1() { |
566 |
+ |
try { |
567 |
+ |
Callable c = Executors.callable((Runnable) null); |
568 |
+ |
shouldThrow(); |
569 |
+ |
} catch (NullPointerException success) {} |
570 |
+ |
} |
571 |
|
|
572 |
< |
private static class Fib implements Callable<BigInteger> { |
573 |
< |
private final BigInteger n; |
574 |
< |
Fib(long n) { |
575 |
< |
if (n < 0) throw new IllegalArgumentException("need non-negative arg, but got " + n); |
576 |
< |
this.n = BigInteger.valueOf(n); |
577 |
< |
} |
578 |
< |
public BigInteger call() { |
579 |
< |
BigInteger f1 = BigInteger.ONE; |
580 |
< |
BigInteger f2 = f1; |
388 |
< |
for (BigInteger i = BigInteger.ZERO; i.compareTo(n) < 0; i = i.add(BigInteger.ONE)) { |
389 |
< |
BigInteger t = f1.add(f2); |
390 |
< |
f1 = f2; |
391 |
< |
f2 = t; |
392 |
< |
} |
393 |
< |
return f1; |
394 |
< |
} |
395 |
< |
}; |
572 |
> |
/** |
573 |
> |
* callable(null, result) throws NPE |
574 |
> |
*/ |
575 |
> |
public void testCallableNPE2() { |
576 |
> |
try { |
577 |
> |
Callable c = Executors.callable((Runnable) null, one); |
578 |
> |
shouldThrow(); |
579 |
> |
} catch (NullPointerException success) {} |
580 |
> |
} |
581 |
|
|
582 |
+ |
/** |
583 |
+ |
* callable(null PrivilegedAction) throws NPE |
584 |
+ |
*/ |
585 |
+ |
public void testCallableNPE3() { |
586 |
+ |
try { |
587 |
+ |
Callable c = Executors.callable((PrivilegedAction) null); |
588 |
+ |
shouldThrow(); |
589 |
+ |
} catch (NullPointerException success) {} |
590 |
+ |
} |
591 |
|
|
592 |
+ |
/** |
593 |
+ |
* callable(null PrivilegedExceptionAction) throws NPE |
594 |
+ |
*/ |
595 |
+ |
public void testCallableNPE4() { |
596 |
+ |
try { |
597 |
+ |
Callable c = Executors.callable((PrivilegedExceptionAction) null); |
598 |
+ |
shouldThrow(); |
599 |
+ |
} catch (NullPointerException success) {} |
600 |
+ |
} |
601 |
|
|
602 |
|
} |