ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ExecutorsTest.java
Revision: 1.1
Committed: Sun Aug 31 19:24:55 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Log Message:
First check-in of tests to be in tck

File Contents

# Content
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.
6 */
7
8
9 import junit.framework.*;
10 import java.util.*;
11 import java.util.concurrent.*;
12
13 public class ExecutorsTest extends TestCase{
14
15 public static void main(String[] args) {
16 junit.textui.TestRunner.run (suite());
17 }
18
19
20 public static Test suite() {
21 return new TestSuite(ExecutorsTest.class);
22 }
23
24 private static long SHORT_DELAY_MS = 100;
25 private static long MEDIUM_DELAY_MS = 1000;
26 private static long LONG_DELAY_MS = 10000;
27
28 class SleepRun implements Runnable {
29 public void run() {
30 try{
31 Thread.sleep(MEDIUM_DELAY_MS);
32 } catch(InterruptedException e){
33 fail("unexpected exception");
34 }
35 }
36 }
37
38
39 class SleepCall implements Callable {
40 public Object call(){
41 try{
42 Thread.sleep(MEDIUM_DELAY_MS);
43 }catch(InterruptedException e){
44 fail("unexpected exception");
45 }
46 return Boolean.TRUE;
47 }
48 }
49
50
51
52 /**
53 * Test to verify execute(Executor, Runnable) will throw
54 * RejectedExecutionException Attempting to execute a runnable on
55 * a full ThreadPool will cause such an exception here, up to 5
56 * runnables are attempted on a pool capable on handling one
57 * until it throws an exception
58 */
59 public void testExecute1(){
60 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
61 try{
62
63 for(int i = 0; i < 5; ++i){
64 Executors.execute(p, new SleepRun(), Boolean.TRUE);
65 }
66 fail("should throw");
67 } catch(RejectedExecutionException success){}
68 p.shutdownNow();
69 }
70
71 /**
72 * Test to verify execute(Executor, Callable) will throw
73 * RejectedExecutionException Attempting to execute a callable on
74 * a full ThreadPool will cause such an exception here, up to 5
75 * runnables are attempted on a pool capable on handling one
76 * until it throws an exception
77 */
78 public void testExecute2(){
79 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
80 try{
81 for(int i = 0; i < 5; ++i) {
82 Executors.execute(p, new SleepCall());
83 }
84 fail("should throw");
85 }catch(RejectedExecutionException e){}
86 p.shutdownNow();
87 }
88
89
90 /**
91 * Test to verify invoke(Executor, Runnable) throws InterruptedException
92 * A single use of invoke starts that will wait long enough
93 * for the invoking thread to be interrupted
94 */
95 public void testInvoke2(){
96 final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
97 Thread t = new Thread(new Runnable() {
98 public void run(){
99 try{
100 Executors.invoke(p,new Runnable(){
101 public void run(){
102 try{
103 Thread.sleep(MEDIUM_DELAY_MS);
104 fail("should throw");
105 }catch(InterruptedException e){
106 }
107 }
108 });
109 } catch(InterruptedException success){
110 } catch(Exception e) {
111 fail("unexpected exception");
112 }
113
114 }
115 });
116 try{
117 t.start();
118 Thread.sleep(SHORT_DELAY_MS);
119 t.interrupt();
120 }catch(Exception e){
121 fail("unexpected exception");
122 }
123 p.shutdownNow();
124 }
125
126 /**
127 * Test to verify invoke(Executor, Runnable) will throw
128 * ExecutionException An ExecutionException occurs when the
129 * underlying Runnable throws an exception, here the
130 * DivideByZeroException will cause an ExecutionException
131 */
132 public void testInvoke3(){
133 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
134 try{
135 Runnable r = new Runnable(){
136 public void run(){
137 int i = 5/0;
138 }
139 };
140
141 for(int i =0; i < 5; i++){
142 Executors.invoke(p,r);
143 }
144
145 fail("should throw");
146 } catch(ExecutionException success){
147 } catch(Exception e){
148 fail("should throw EE");
149 }
150 p.shutdownNow();
151 }
152
153
154
155 /**
156 * Test to verify invoke(Executor, Callable) throws
157 * InterruptedException A single use of invoke starts that will
158 * wait long enough for the invoking thread to be interrupted
159 */
160 public void testInvoke5(){
161 final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
162
163 final Callable c = new Callable(){
164 public Object call(){
165 try{
166 Executors.invoke(p, new SleepCall());
167 fail("should throw");
168 }catch(InterruptedException e){}
169 catch(RejectedExecutionException e2){}
170 catch(ExecutionException e3){}
171 return Boolean.TRUE;
172 }
173 };
174
175
176
177 Thread t = new Thread(new Runnable(){
178 public void run(){
179 try{
180 c.call();
181 }catch(Exception e){}
182 }
183 });
184 try{
185 t.start();
186 Thread.sleep(SHORT_DELAY_MS);
187 t.interrupt();
188 t.join();
189 }catch(InterruptedException e){
190 fail("unexpected exception");
191 }
192
193 p.shutdownNow();
194 }
195
196 /**
197 * Test to verify invoke(Executor, Callable) will throw ExecutionException
198 * An ExecutionException occurs when the underlying Runnable throws
199 * an exception, here the DivideByZeroException will cause an ExecutionException
200 */
201 public void testInvoke6(){
202 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
203
204 try{
205 Callable c = new Callable(){
206 public Object call(){
207 int i = 5/0;
208 return Boolean.TRUE;
209 }
210 };
211
212 for(int i =0; i < 5; i++){
213 Executors.invoke(p,c);
214 }
215
216 fail("should throw");
217 }catch(RejectedExecutionException e){}
218 catch(InterruptedException e2){}
219 catch(ExecutionException e3){}
220 p.shutdownNow();
221 }
222
223 public void testExecuteRunnable () {
224 try {
225 Executor e = new DirectExecutor();
226 Task task = new Task();
227
228 assertFalse("task should not be complete", task.isCompleted());
229
230 Future<String> future = Executors.execute(e, task, TEST_STRING);
231 String result = future.get();
232
233 assertTrue("task should be complete", task.isCompleted());
234 assertSame("should return test string", TEST_STRING, result);
235 }
236 catch (ExecutionException ex) {
237 fail("Unexpected exception");
238 }
239 catch (InterruptedException ex) {
240 fail("Unexpected exception");
241 }
242 }
243
244 public void testInvokeRunnable () {
245 try {
246 Executor e = new DirectExecutor();
247 Task task = new Task();
248
249 assertFalse("task should not be complete", task.isCompleted());
250
251 Executors.invoke(e, task);
252
253 assertTrue("task should be complete", task.isCompleted());
254 }
255 catch (ExecutionException ex) {
256 fail("Unexpected exception");
257 }
258 catch (InterruptedException ex) {
259 fail("Unexpected exception");
260 }
261 }
262
263 public void testExecuteCallable () {
264 try {
265 Executor e = new DirectExecutor();
266 Future<String> future = Executors.execute(e, new StringTask());
267 String result = future.get();
268
269 assertSame("should return test string", TEST_STRING, result);
270 }
271 catch (ExecutionException ex) {
272 fail("Unexpected exception");
273 }
274 catch (InterruptedException ex) {
275 fail("Unexpected exception");
276 }
277 }
278
279 public void testInvokeCallable () {
280 try {
281 Executor e = new DirectExecutor();
282 String result = Executors.invoke(e, new StringTask());
283
284 assertSame("should return test string", TEST_STRING, result);
285 }
286 catch (ExecutionException ex) {
287 fail("Unexpected exception" );
288 }
289 catch (InterruptedException ex) {
290 fail("Unexpected exception");
291 }
292 }
293
294 private static final String TEST_STRING = "a test string";
295
296 private static class Task implements Runnable {
297 public void run() { completed = true; }
298 public boolean isCompleted() { return completed; }
299 public void reset() { completed = false; }
300 private boolean completed = false;
301 }
302
303 private static class StringTask implements Callable<String> {
304 public String call() { return TEST_STRING; }
305 }
306
307 static class DirectExecutor implements Executor {
308 public void execute(Runnable r) {
309 r.run();
310 }
311 }
312
313
314 }