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.9 by jsr166, Sat Nov 21 02:33:20 2009 UTC vs.
Revision 1.15 by jsr166, Fri May 27 19:42:42 2011 UTC

# Line 1 | Line 1
1   /*
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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9
9   import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
# Line 17 | Line 16 | import java.security.*;
16  
17   public class ExecutorCompletionServiceTest extends JSR166TestCase {
18      public static void main(String[] args) {
19 <        junit.textui.TestRunner.run (suite());
19 >        junit.textui.TestRunner.run(suite());
20      }
21      public static Test suite() {
22          return new TestSuite(ExecutorCompletionServiceTest.class);
23      }
24  
26
25      /**
26       * Creating a new ECS with null Executor throw NPE
27       */
# Line 31 | Line 29 | public class ExecutorCompletionServiceTe
29          try {
30              ExecutorCompletionService ecs = new ExecutorCompletionService(null);
31              shouldThrow();
32 <        } catch (NullPointerException success) {
35 <        }
32 >        } catch (NullPointerException success) {}
33      }
34  
35      /**
# Line 43 | Line 40 | public class ExecutorCompletionServiceTe
40              ExecutorService e = Executors.newCachedThreadPool();
41              ExecutorCompletionService ecs = new ExecutorCompletionService(e, null);
42              shouldThrow();
43 <        } catch (NullPointerException success) {
47 <        }
43 >        } catch (NullPointerException success) {}
44      }
45  
46      /**
# Line 82 | Line 78 | public class ExecutorCompletionServiceTe
78      /**
79       * A taken submitted task is completed
80       */
81 <    public void testTake() {
81 >    public void testTake() throws InterruptedException {
82          ExecutorService e = Executors.newCachedThreadPool();
83          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
84          try {
# Line 90 | Line 86 | public class ExecutorCompletionServiceTe
86              ecs.submit(c);
87              Future f = ecs.take();
88              assertTrue(f.isDone());
93        } catch (Exception ex) {
94            unexpectedException();
89          } finally {
90              joinPool(e);
91          }
# Line 100 | Line 94 | public class ExecutorCompletionServiceTe
94      /**
95       * Take returns the same future object returned by submit
96       */
97 <    public void testTake2() {
97 >    public void testTake2() throws InterruptedException {
98          ExecutorService e = Executors.newCachedThreadPool();
99          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
100          try {
# Line 108 | Line 102 | public class ExecutorCompletionServiceTe
102              Future f1 = ecs.submit(c);
103              Future f2 = ecs.take();
104              assertSame(f1, f2);
111        } catch (Exception ex) {
112            unexpectedException();
105          } finally {
106              joinPool(e);
107          }
# Line 118 | Line 110 | public class ExecutorCompletionServiceTe
110      /**
111       * If poll returns non-null, the returned task is completed
112       */
113 <    public void testPoll1() {
113 >    public void testPoll1() throws InterruptedException {
114          ExecutorService e = Executors.newCachedThreadPool();
115          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
116          try {
117              assertNull(ecs.poll());
118              Callable c = new StringTask();
119              ecs.submit(c);
120 <            Thread.sleep(SHORT_DELAY_MS);
120 >            delay(SHORT_DELAY_MS);
121              for (;;) {
122                  Future f = ecs.poll();
123                  if (f != null) {
# Line 133 | Line 125 | public class ExecutorCompletionServiceTe
125                      break;
126                  }
127              }
136        } catch (Exception ex) {
137            unexpectedException();
128          } finally {
129              joinPool(e);
130          }
# Line 143 | Line 133 | public class ExecutorCompletionServiceTe
133      /**
134       * If timed poll returns non-null, the returned task is completed
135       */
136 <    public void testPoll2() {
136 >    public void testPoll2() throws InterruptedException {
137          ExecutorService e = Executors.newCachedThreadPool();
138          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
139          try {
# Line 153 | Line 143 | public class ExecutorCompletionServiceTe
143              Future f = ecs.poll(SHORT_DELAY_MS, MILLISECONDS);
144              if (f != null)
145                  assertTrue(f.isDone());
156        } catch (Exception ex) {
157            unexpectedException();
146          } finally {
147              joinPool(e);
148          }
149      }
150 <     /**
151 <      * Submitting to underlying AES that overrides newTaskFor(Callable)
152 <      * returns and eventually runs Future returned by newTaskFor.
153 <      */
154 <     public void testNewTaskForCallable() {
155 <         final AtomicBoolean done = new AtomicBoolean(false);
156 <         class MyCallableFuture<V> extends FutureTask<V> {
157 <             MyCallableFuture(Callable<V> c) { super(c); }
158 <             protected void done() { done.set(true); }
159 <         }
160 <         ExecutorService e = new ThreadPoolExecutor(
150 >
151 >    /**
152 >     * Submitting to underlying AES that overrides newTaskFor(Callable)
153 >     * returns and eventually runs Future returned by newTaskFor.
154 >     */
155 >    public void testNewTaskForCallable() throws InterruptedException {
156 >        final AtomicBoolean done = new AtomicBoolean(false);
157 >        class MyCallableFuture<V> extends FutureTask<V> {
158 >            MyCallableFuture(Callable<V> c) { super(c); }
159 >            protected void done() { done.set(true); }
160 >        }
161 >        ExecutorService e = new ThreadPoolExecutor(
162                                   1, 1, 30L, TimeUnit.SECONDS,
163                                   new ArrayBlockingQueue<Runnable>(1)) {
164 <             protected <T> RunnableFuture<T> newTaskFor(Callable<T> c) {
165 <                 return new MyCallableFuture<T>(c);
166 <             }
167 <         };
168 <         ExecutorCompletionService<String> ecs =
169 <             new ExecutorCompletionService<String>(e);
170 <         try {
171 <             assertNull(ecs.poll());
172 <             Callable<String> c = new StringTask();
173 <             Future f1 = ecs.submit(c);
174 <             assertTrue("submit must return MyCallableFuture",
175 <                        f1 instanceof MyCallableFuture);
176 <             Future f2 = ecs.take();
177 <             assertSame("submit and take must return same objects", f1, f2);
178 <             assertTrue("completed task must have set done", done.get());
179 <         } catch (Exception ex) {
180 <             unexpectedException();
181 <         } finally {
182 <             joinPool(e);
183 <         }
184 <     }
185 <
186 <     /**
187 <      * Submitting to underlying AES that overrides newTaskFor(Runnable,T)
188 <      * returns and eventually runs Future returned by newTaskFor.
189 <      */
190 <     public void testNewTaskForRunnable() {
191 <         final AtomicBoolean done = new AtomicBoolean(false);
192 <         class MyRunnableFuture<V> extends FutureTask<V> {
193 <             MyRunnableFuture(Runnable t, V r) { super(t, r); }
205 <             protected void done() { done.set(true); }
206 <         }
207 <         ExecutorService e = new ThreadPoolExecutor(
164 >            protected <T> RunnableFuture<T> newTaskFor(Callable<T> c) {
165 >                return new MyCallableFuture<T>(c);
166 >            }};
167 >        ExecutorCompletionService<String> ecs =
168 >            new ExecutorCompletionService<String>(e);
169 >        try {
170 >            assertNull(ecs.poll());
171 >            Callable<String> c = new StringTask();
172 >            Future f1 = ecs.submit(c);
173 >            assertTrue("submit must return MyCallableFuture",
174 >                       f1 instanceof MyCallableFuture);
175 >            Future f2 = ecs.take();
176 >            assertSame("submit and take must return same objects", f1, f2);
177 >            assertTrue("completed task must have set done", done.get());
178 >        } finally {
179 >            joinPool(e);
180 >        }
181 >    }
182 >
183 >    /**
184 >     * Submitting to underlying AES that overrides newTaskFor(Runnable,T)
185 >     * returns and eventually runs Future returned by newTaskFor.
186 >     */
187 >    public void testNewTaskForRunnable() throws InterruptedException {
188 >        final AtomicBoolean done = new AtomicBoolean(false);
189 >        class MyRunnableFuture<V> extends FutureTask<V> {
190 >            MyRunnableFuture(Runnable t, V r) { super(t, r); }
191 >            protected void done() { done.set(true); }
192 >        }
193 >        ExecutorService e = new ThreadPoolExecutor(
194                                   1, 1, 30L, TimeUnit.SECONDS,
195                                   new ArrayBlockingQueue<Runnable>(1)) {
196 <             protected <T> RunnableFuture<T> newTaskFor(Runnable t, T r) {
197 <                 return new MyRunnableFuture<T>(t, r);
198 <             }
199 <         };
200 <         ExecutorCompletionService<String> ecs =
201 <             new ExecutorCompletionService<String>(e);
202 <         try {
203 <             assertNull(ecs.poll());
204 <             Runnable r = new NoOpRunnable();
205 <             Future f1 = ecs.submit(r, null);
206 <             assertTrue("submit must return MyRunnableFuture",
207 <                        f1 instanceof MyRunnableFuture);
208 <             Future f2 = ecs.take();
209 <             assertSame("submit and take must return same objects", f1, f2);
210 <             assertTrue("completed task must have set done", done.get());
211 <         } catch (Exception ex) {
212 <             unexpectedException();
213 <         } finally {
228 <             joinPool(e);
229 <         }
230 <     }
231 <
232 <
196 >            protected <T> RunnableFuture<T> newTaskFor(Runnable t, T r) {
197 >                return new MyRunnableFuture<T>(t, r);
198 >            }};
199 >        ExecutorCompletionService<String> ecs =
200 >            new ExecutorCompletionService<String>(e);
201 >        try {
202 >            assertNull(ecs.poll());
203 >            Runnable r = new NoOpRunnable();
204 >            Future f1 = ecs.submit(r, null);
205 >            assertTrue("submit must return MyRunnableFuture",
206 >                       f1 instanceof MyRunnableFuture);
207 >            Future f2 = ecs.take();
208 >            assertSame("submit and take must return same objects", f1, f2);
209 >            assertTrue("completed task must have set done", done.get());
210 >        } finally {
211 >            joinPool(e);
212 >        }
213 >    }
214  
215   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines