ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ExecutorCompletionServiceTest.java
Revision: 1.1
Committed: Tue Dec 23 19:40:24 2003 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Log Message:
Add and adapt tests to refactored Executor API

File Contents

# User Rev Content
1 dl 1.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     import java.math.BigInteger;
13     import java.security.*;
14    
15     public class ExecutorCompletionServiceTest extends JSR166TestCase{
16     public static void main(String[] args) {
17     junit.textui.TestRunner.run (suite());
18     }
19     public static Test suite() {
20     return new TestSuite(ExecutorCompletionServiceTest.class);
21     }
22    
23    
24     /**
25     * Creating a new ECS with null Executor throw NPE
26     */
27     public void testConstructorNPE() {
28     try {
29     ExecutorCompletionService ecs = new ExecutorCompletionService(null);
30     shouldThrow();
31     } catch (NullPointerException success) {
32     }
33     }
34    
35     /**
36     * Creating a new ECS with null queue throw NPE
37     */
38     public void testConstructorNPE2() {
39     try {
40     ExecutorService e = Executors.newCachedThreadPool();
41     ExecutorCompletionService ecs = new ExecutorCompletionService(e, null);
42     shouldThrow();
43     } catch (NullPointerException success) {
44     }
45     }
46    
47     /**
48     * Submitting a null callable throws NPE
49     */
50     public void testSubmitNPE() {
51     ExecutorService e = Executors.newCachedThreadPool();
52     ExecutorCompletionService ecs = new ExecutorCompletionService(e);
53     try {
54     Callable c = null;
55     ecs.submit(c);
56     shouldThrow();
57     } catch (NullPointerException success) {
58     } finally {
59     joinPool(e);
60     }
61     }
62    
63     /**
64     * Submitting a null runnable throws NPE
65     */
66     public void testSubmitNPE2() {
67     ExecutorService e = Executors.newCachedThreadPool();
68     ExecutorCompletionService ecs = new ExecutorCompletionService(e);
69     try {
70     Runnable r = null;
71     ecs.submit(r, Boolean.TRUE);
72     shouldThrow();
73     } catch (NullPointerException success) {
74     } finally {
75     joinPool(e);
76     }
77     }
78    
79     /**
80     * A taken submitted task is completed
81     */
82     public void testTake() {
83     ExecutorService e = Executors.newCachedThreadPool();
84     ExecutorCompletionService ecs = new ExecutorCompletionService(e);
85     try {
86     Callable c = new StringTask();
87     ecs.submit(c);
88     Future f = ecs.take();
89     assert(f.isDone());
90     } catch (Exception ex) {
91     unexpectedException();
92     } finally {
93     joinPool(e);
94     }
95     }
96    
97     /**
98     * Take returns the same future object returned by submit
99     */
100     public void testTake2() {
101     ExecutorService e = Executors.newCachedThreadPool();
102     ExecutorCompletionService ecs = new ExecutorCompletionService(e);
103     try {
104     Callable c = new StringTask();
105     Future f1 = ecs.submit(c);
106     Future f2 = ecs.take();
107     assertSame(f1, f2);
108     } catch (Exception ex) {
109     unexpectedException();
110     } finally {
111     joinPool(e);
112     }
113     }
114    
115     /**
116     * If poll returns non-null, the returned task is completed
117     */
118     public void testPoll1() {
119     ExecutorService e = Executors.newCachedThreadPool();
120     ExecutorCompletionService ecs = new ExecutorCompletionService(e);
121     try {
122     assertNull(ecs.poll());
123     Callable c = new StringTask();
124     ecs.submit(c);
125     Thread.sleep(SHORT_DELAY_MS);
126     for (;;) {
127     Future f = ecs.poll();
128     if (f != null) {
129     assert(f.isDone());
130     break;
131     }
132     }
133     } catch (Exception ex) {
134     unexpectedException();
135     } finally {
136     joinPool(e);
137     }
138     }
139    
140     /**
141     * If timed poll returns non-null, the returned task is completed
142     */
143     public void testPoll2() {
144     ExecutorService e = Executors.newCachedThreadPool();
145     ExecutorCompletionService ecs = new ExecutorCompletionService(e);
146     try {
147     assertNull(ecs.poll());
148     Callable c = new StringTask();
149     ecs.submit(c);
150     Future f = ecs.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
151     if (f != null)
152     assert(f.isDone());
153     } catch (Exception ex) {
154     unexpectedException();
155     } finally {
156     joinPool(e);
157     }
158     }
159    
160     }