11 |
|
import java.util.concurrent.*; |
12 |
|
import java.math.BigInteger; |
13 |
|
|
14 |
< |
public class ExecutorsTest extends TestCase{ |
14 |
> |
public class ExecutorsTest extends JSR166TestCase{ |
15 |
|
|
16 |
|
public static void main(String[] args) { |
17 |
|
junit.textui.TestRunner.run (suite()); |
22 |
|
return new TestSuite(ExecutorsTest.class); |
23 |
|
} |
24 |
|
|
25 |
– |
private static long SHORT_DELAY_MS = 100; |
26 |
– |
private static long MEDIUM_DELAY_MS = 1000; |
27 |
– |
private static long LONG_DELAY_MS = 10000; |
28 |
– |
|
29 |
– |
class SleepRun implements Runnable { |
30 |
– |
public void run() { |
31 |
– |
try{ |
32 |
– |
Thread.sleep(MEDIUM_DELAY_MS); |
33 |
– |
} catch(InterruptedException e){ |
34 |
– |
fail("unexpected exception"); |
35 |
– |
} |
36 |
– |
} |
37 |
– |
} |
38 |
– |
|
39 |
– |
|
40 |
– |
class SleepCall implements Callable { |
41 |
– |
public Object call(){ |
42 |
– |
try{ |
43 |
– |
Thread.sleep(MEDIUM_DELAY_MS); |
44 |
– |
}catch(InterruptedException e){ |
45 |
– |
fail("unexpected exception"); |
46 |
– |
} |
47 |
– |
return Boolean.TRUE; |
48 |
– |
} |
49 |
– |
} |
50 |
– |
|
51 |
– |
|
52 |
– |
|
25 |
|
/** |
26 |
< |
* Test to verify execute(Executor, Runnable) will throw |
26 |
> |
* execute(Executor, Runnable) will throw |
27 |
|
* RejectedExecutionException Attempting to execute a runnable on |
28 |
|
* a full ThreadPool will cause such an exception here, up to 5 |
29 |
|
* runnables are attempted on a pool capable on handling one |
30 |
|
* until it throws an exception |
31 |
|
*/ |
32 |
|
public void testExecute1(){ |
33 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
33 |
> |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
34 |
|
try{ |
35 |
|
|
36 |
|
for(int i = 0; i < 5; ++i){ |
37 |
< |
Executors.execute(p, new SleepRun(), Boolean.TRUE); |
37 |
> |
Executors.execute(p, new MediumRunnable(), Boolean.TRUE); |
38 |
|
} |
39 |
|
fail("should throw"); |
40 |
|
} catch(RejectedExecutionException success){} |
41 |
< |
p.shutdownNow(); |
41 |
> |
joinPool(p); |
42 |
|
} |
43 |
|
|
44 |
|
/** |
45 |
< |
* Test to verify execute(Executor, Callable) will throw |
45 |
> |
* execute(Executor, Callable) will throw |
46 |
|
* RejectedExecutionException Attempting to execute a callable on |
47 |
|
* a full ThreadPool will cause such an exception here, up to 5 |
48 |
|
* runnables are attempted on a pool capable on handling one |
49 |
|
* until it throws an exception |
50 |
|
*/ |
51 |
|
public void testExecute2(){ |
52 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
52 |
> |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
53 |
|
try{ |
54 |
|
for(int i = 0; i < 5; ++i) { |
55 |
< |
Executors.execute(p, new SleepCall()); |
55 |
> |
Executors.execute(p, new SmallCallable()); |
56 |
|
} |
57 |
|
fail("should throw"); |
58 |
|
}catch(RejectedExecutionException e){} |
59 |
< |
p.shutdownNow(); |
59 |
> |
joinPool(p); |
60 |
|
} |
61 |
|
|
62 |
|
|
63 |
|
/** |
64 |
< |
* Test to verify invoke(Executor, Runnable) throws InterruptedException |
64 |
> |
* invoke(Executor, Runnable) throws InterruptedException |
65 |
|
* A single use of invoke starts that will wait long enough |
66 |
|
* for the invoking thread to be interrupted |
67 |
|
*/ |
68 |
|
public void testInvoke2(){ |
69 |
< |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
69 |
> |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
70 |
|
Thread t = new Thread(new Runnable() { |
71 |
|
public void run(){ |
72 |
|
try{ |
93 |
|
}catch(Exception e){ |
94 |
|
fail("unexpected exception"); |
95 |
|
} |
96 |
< |
p.shutdownNow(); |
96 |
> |
joinPool(p); |
97 |
|
} |
98 |
|
|
99 |
|
/** |
100 |
< |
* Test to verify invoke(Executor, Runnable) will throw |
100 |
> |
* invoke(Executor, Runnable) will throw |
101 |
|
* ExecutionException An ExecutionException occurs when the |
102 |
|
* underlying Runnable throws an exception, here the |
103 |
|
* DivideByZeroException will cause an ExecutionException |
104 |
|
*/ |
105 |
|
public void testInvoke3(){ |
106 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
106 |
> |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
107 |
|
try{ |
108 |
|
Runnable r = new Runnable(){ |
109 |
|
public void run(){ |
120 |
|
} catch(Exception e){ |
121 |
|
fail("should throw EE"); |
122 |
|
} |
123 |
< |
p.shutdownNow(); |
123 |
> |
joinPool(p); |
124 |
|
} |
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
/** |
129 |
< |
* Test to verify invoke(Executor, Callable) throws |
129 |
> |
* invoke(Executor, Callable) throws |
130 |
|
* InterruptedException A single use of invoke starts that will |
131 |
|
* wait long enough for the invoking thread to be interrupted |
132 |
|
*/ |
133 |
|
public void testInvoke5(){ |
134 |
< |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
134 |
> |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
135 |
|
|
136 |
|
final Callable c = new Callable(){ |
137 |
|
public Object call(){ |
138 |
|
try{ |
139 |
< |
Executors.invoke(p, new SleepCall()); |
139 |
> |
Executors.invoke(p, new SmallCallable()); |
140 |
|
fail("should throw"); |
141 |
|
}catch(InterruptedException e){} |
142 |
|
catch(RejectedExecutionException e2){} |
163 |
|
fail("unexpected exception"); |
164 |
|
} |
165 |
|
|
166 |
< |
p.shutdownNow(); |
166 |
> |
joinPool(p); |
167 |
|
} |
168 |
|
|
169 |
|
/** |
170 |
< |
* Test to verify invoke(Executor, Callable) will throw ExecutionException |
170 |
> |
* invoke(Executor, Callable) will throw ExecutionException |
171 |
|
* An ExecutionException occurs when the underlying Runnable throws |
172 |
|
* an exception, here the DivideByZeroException will cause an ExecutionException |
173 |
|
*/ |
174 |
|
public void testInvoke6(){ |
175 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
175 |
> |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
176 |
|
|
177 |
|
try{ |
178 |
|
Callable c = new Callable(){ |
190 |
|
}catch(RejectedExecutionException e){} |
191 |
|
catch(InterruptedException e2){} |
192 |
|
catch(ExecutionException e3){} |
193 |
< |
p.shutdownNow(); |
193 |
> |
joinPool(p); |
194 |
|
} |
195 |
|
|
196 |
|
public void testExecuteRunnable () { |
321 |
|
assertTrue(elapsed < N); |
322 |
|
} |
323 |
|
finally { |
324 |
< |
executor.shutdownNow(); |
324 |
> |
joinPool(executor); |
325 |
|
} |
326 |
|
} |
327 |
|
|