ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ExecutorCompletionServiceTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ExecutorCompletionServiceTest.java (file contents):
Revision 1.1 by dl, Tue Dec 23 19:40:24 2003 UTC vs.
Revision 1.9 by jsr166, Sat Nov 21 02:33:20 2009 UTC

# Line 1 | Line 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.
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 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9  
10   import junit.framework.*;
11   import java.util.*;
12   import java.util.concurrent.*;
13 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
14 + import java.util.concurrent.atomic.*;
15   import java.math.BigInteger;
16   import java.security.*;
17  
18 < public class ExecutorCompletionServiceTest extends JSR166TestCase{
18 > public class ExecutorCompletionServiceTest extends JSR166TestCase {
19      public static void main(String[] args) {
20 <        junit.textui.TestRunner.run (suite());  
20 >        junit.textui.TestRunner.run (suite());
21      }
22      public static Test suite() {
23          return new TestSuite(ExecutorCompletionServiceTest.class);
# Line 23 | Line 26 | public class ExecutorCompletionServiceTe
26  
27      /**
28       * Creating a new ECS with null Executor throw NPE
29 <     */
29 >     */
30      public void testConstructorNPE() {
31          try {
32              ExecutorCompletionService ecs = new ExecutorCompletionService(null);
# Line 34 | Line 37 | public class ExecutorCompletionServiceTe
37  
38      /**
39       * Creating a new ECS with null queue throw NPE
40 <     */
40 >     */
41      public void testConstructorNPE2() {
42          try {
43              ExecutorService e = Executors.newCachedThreadPool();
# Line 46 | Line 49 | public class ExecutorCompletionServiceTe
49  
50      /**
51       * Submitting a null callable throws NPE
52 <     */
52 >     */
53      public void testSubmitNPE() {
54          ExecutorService e = Executors.newCachedThreadPool();
55          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
# Line 62 | Line 65 | public class ExecutorCompletionServiceTe
65  
66      /**
67       * Submitting a null runnable throws NPE
68 <     */
68 >     */
69      public void testSubmitNPE2() {
70          ExecutorService e = Executors.newCachedThreadPool();
71          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
# Line 78 | Line 81 | public class ExecutorCompletionServiceTe
81  
82      /**
83       * A taken submitted task is completed
84 <     */
84 >     */
85      public void testTake() {
86          ExecutorService e = Executors.newCachedThreadPool();
87          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
# Line 86 | Line 89 | public class ExecutorCompletionServiceTe
89              Callable c = new StringTask();
90              ecs.submit(c);
91              Future f = ecs.take();
92 <            assert(f.isDone());
92 >            assertTrue(f.isDone());
93          } catch (Exception ex) {
94              unexpectedException();
95          } finally {
# Line 96 | Line 99 | public class ExecutorCompletionServiceTe
99  
100      /**
101       * Take returns the same future object returned by submit
102 <     */
102 >     */
103      public void testTake2() {
104          ExecutorService e = Executors.newCachedThreadPool();
105          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
# Line 114 | Line 117 | public class ExecutorCompletionServiceTe
117  
118      /**
119       * If poll returns non-null, the returned task is completed
120 <     */
120 >     */
121      public void testPoll1() {
122          ExecutorService e = Executors.newCachedThreadPool();
123          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
# Line 126 | Line 129 | public class ExecutorCompletionServiceTe
129              for (;;) {
130                  Future f = ecs.poll();
131                  if (f != null) {
132 <                    assert(f.isDone());
132 >                    assertTrue(f.isDone());
133                      break;
134                  }
135              }
# Line 139 | Line 142 | public class ExecutorCompletionServiceTe
142  
143      /**
144       * If timed poll returns non-null, the returned task is completed
145 <     */
145 >     */
146      public void testPoll2() {
147          ExecutorService e = Executors.newCachedThreadPool();
148          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
# Line 147 | Line 150 | public class ExecutorCompletionServiceTe
150              assertNull(ecs.poll());
151              Callable c = new StringTask();
152              ecs.submit(c);
153 <            Future f = ecs.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
154 <            if (f != null)
155 <                assert(f.isDone());
153 >            Future f = ecs.poll(SHORT_DELAY_MS, MILLISECONDS);
154 >            if (f != null)
155 >                assertTrue(f.isDone());
156          } catch (Exception ex) {
157              unexpectedException();
158          } finally {
159              joinPool(e);
160          }
161      }
162 +     /**
163 +      * Submitting to underlying AES that overrides newTaskFor(Callable)
164 +      * returns and eventually runs Future returned by newTaskFor.
165 +      */
166 +     public void testNewTaskForCallable() {
167 +         final AtomicBoolean done = new AtomicBoolean(false);
168 +         class MyCallableFuture<V> extends FutureTask<V> {
169 +             MyCallableFuture(Callable<V> c) { super(c); }
170 +             protected void done() { done.set(true); }
171 +         }
172 +         ExecutorService e = new ThreadPoolExecutor(
173 +                                 1, 1, 30L, TimeUnit.SECONDS,
174 +                                 new ArrayBlockingQueue<Runnable>(1)) {
175 +             protected <T> RunnableFuture<T> newTaskFor(Callable<T> c) {
176 +                 return new MyCallableFuture<T>(c);
177 +             }
178 +         };
179 +         ExecutorCompletionService<String> ecs =
180 +             new ExecutorCompletionService<String>(e);
181 +         try {
182 +             assertNull(ecs.poll());
183 +             Callable<String> c = new StringTask();
184 +             Future f1 = ecs.submit(c);
185 +             assertTrue("submit must return MyCallableFuture",
186 +                        f1 instanceof MyCallableFuture);
187 +             Future f2 = ecs.take();
188 +             assertSame("submit and take must return same objects", f1, f2);
189 +             assertTrue("completed task must have set done", done.get());
190 +         } catch (Exception ex) {
191 +             unexpectedException();
192 +         } finally {
193 +             joinPool(e);
194 +         }
195 +     }
196 +
197 +     /**
198 +      * Submitting to underlying AES that overrides newTaskFor(Runnable,T)
199 +      * returns and eventually runs Future returned by newTaskFor.
200 +      */
201 +     public void testNewTaskForRunnable() {
202 +         final AtomicBoolean done = new AtomicBoolean(false);
203 +         class MyRunnableFuture<V> extends FutureTask<V> {
204 +             MyRunnableFuture(Runnable t, V r) { super(t, r); }
205 +             protected void done() { done.set(true); }
206 +         }
207 +         ExecutorService e = new ThreadPoolExecutor(
208 +                                 1, 1, 30L, TimeUnit.SECONDS,
209 +                                 new ArrayBlockingQueue<Runnable>(1)) {
210 +             protected <T> RunnableFuture<T> newTaskFor(Runnable t, T r) {
211 +                 return new MyRunnableFuture<T>(t, r);
212 +             }
213 +         };
214 +         ExecutorCompletionService<String> ecs =
215 +             new ExecutorCompletionService<String>(e);
216 +         try {
217 +             assertNull(ecs.poll());
218 +             Runnable r = new NoOpRunnable();
219 +             Future f1 = ecs.submit(r, null);
220 +             assertTrue("submit must return MyRunnableFuture",
221 +                        f1 instanceof MyRunnableFuture);
222 +             Future f2 = ecs.take();
223 +             assertSame("submit and take must return same objects", f1, f2);
224 +             assertTrue("completed task must have set done", done.get());
225 +         } catch (Exception ex) {
226 +             unexpectedException();
227 +         } finally {
228 +             joinPool(e);
229 +         }
230 +     }
231 +
232 +
233  
234   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines