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.8 by jsr166, Mon Nov 16 05:30:07 2009 UTC vs.
Revision 1.12 by jsr166, Wed Aug 25 01:44:48 2010 UTC

# Line 10 | Line 10
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 {
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 30 | Line 31 | public class ExecutorCompletionServiceTe
31          try {
32              ExecutorCompletionService ecs = new ExecutorCompletionService(null);
33              shouldThrow();
34 <        } catch (NullPointerException success) {
34 <        }
34 >        } catch (NullPointerException success) {}
35      }
36  
37      /**
# Line 42 | Line 42 | public class ExecutorCompletionServiceTe
42              ExecutorService e = Executors.newCachedThreadPool();
43              ExecutorCompletionService ecs = new ExecutorCompletionService(e, null);
44              shouldThrow();
45 <        } catch (NullPointerException success) {
46 <        }
45 >        } catch (NullPointerException success) {}
46      }
47  
48      /**
# Line 81 | Line 80 | public class ExecutorCompletionServiceTe
80      /**
81       * A taken submitted task is completed
82       */
83 <    public void testTake() {
83 >    public void testTake() throws InterruptedException {
84          ExecutorService e = Executors.newCachedThreadPool();
85          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
86          try {
# Line 89 | Line 88 | public class ExecutorCompletionServiceTe
88              ecs.submit(c);
89              Future f = ecs.take();
90              assertTrue(f.isDone());
92        } catch (Exception ex) {
93            unexpectedException();
91          } finally {
92              joinPool(e);
93          }
# Line 99 | Line 96 | public class ExecutorCompletionServiceTe
96      /**
97       * Take returns the same future object returned by submit
98       */
99 <    public void testTake2() {
99 >    public void testTake2() throws InterruptedException {
100          ExecutorService e = Executors.newCachedThreadPool();
101          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
102          try {
# Line 107 | Line 104 | public class ExecutorCompletionServiceTe
104              Future f1 = ecs.submit(c);
105              Future f2 = ecs.take();
106              assertSame(f1, f2);
110        } catch (Exception ex) {
111            unexpectedException();
107          } finally {
108              joinPool(e);
109          }
# Line 117 | Line 112 | public class ExecutorCompletionServiceTe
112      /**
113       * If poll returns non-null, the returned task is completed
114       */
115 <    public void testPoll1() {
115 >    public void testPoll1() throws InterruptedException {
116          ExecutorService e = Executors.newCachedThreadPool();
117          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
118          try {
# Line 132 | Line 127 | public class ExecutorCompletionServiceTe
127                      break;
128                  }
129              }
135        } catch (Exception ex) {
136            unexpectedException();
130          } finally {
131              joinPool(e);
132          }
# Line 142 | Line 135 | public class ExecutorCompletionServiceTe
135      /**
136       * If timed poll returns non-null, the returned task is completed
137       */
138 <    public void testPoll2() {
138 >    public void testPoll2() throws InterruptedException {
139          ExecutorService e = Executors.newCachedThreadPool();
140          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
141          try {
142              assertNull(ecs.poll());
143              Callable c = new StringTask();
144              ecs.submit(c);
145 <            Future f = ecs.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
145 >            Future f = ecs.poll(SHORT_DELAY_MS, MILLISECONDS);
146              if (f != null)
147                  assertTrue(f.isDone());
155        } catch (Exception ex) {
156            unexpectedException();
148          } finally {
149              joinPool(e);
150          }
151      }
152 <     /**
153 <      * Submitting to underlying AES that overrides newTaskFor(Callable)
154 <      * returns and eventually runs Future returned by newTaskFor.
155 <      */
156 <     public void testNewTaskForCallable() {
157 <         final AtomicBoolean done = new AtomicBoolean(false);
158 <         class MyCallableFuture<V> extends FutureTask<V> {
159 <             MyCallableFuture(Callable<V> c) { super(c); }
160 <             protected void done() { done.set(true); }
161 <         }
162 <         ExecutorService e = new ThreadPoolExecutor(
152 >
153 >    /**
154 >     * Submitting to underlying AES that overrides newTaskFor(Callable)
155 >     * returns and eventually runs Future returned by newTaskFor.
156 >     */
157 >    public void testNewTaskForCallable() throws InterruptedException {
158 >        final AtomicBoolean done = new AtomicBoolean(false);
159 >        class MyCallableFuture<V> extends FutureTask<V> {
160 >            MyCallableFuture(Callable<V> c) { super(c); }
161 >            protected void done() { done.set(true); }
162 >        }
163 >        ExecutorService e = new ThreadPoolExecutor(
164                                   1, 1, 30L, TimeUnit.SECONDS,
165                                   new ArrayBlockingQueue<Runnable>(1)) {
166 <             protected <T> RunnableFuture<T> newTaskFor(Callable<T> c) {
167 <                 return new MyCallableFuture<T>(c);
168 <             }
169 <         };
170 <         ExecutorCompletionService<String> ecs =
171 <             new ExecutorCompletionService<String>(e);
172 <         try {
173 <             assertNull(ecs.poll());
174 <             Callable<String> c = new StringTask();
175 <             Future f1 = ecs.submit(c);
176 <             assertTrue("submit must return MyCallableFuture",
177 <                        f1 instanceof MyCallableFuture);
178 <             Future f2 = ecs.take();
179 <             assertSame("submit and take must return same objects", f1, f2);
180 <             assertTrue("completed task must have set done", done.get());
181 <         } catch (Exception ex) {
182 <             unexpectedException();
183 <         } finally {
184 <             joinPool(e);
185 <         }
186 <     }
187 <
188 <     /**
189 <      * Submitting to underlying AES that overrides newTaskFor(Runnable,T)
190 <      * returns and eventually runs Future returned by newTaskFor.
191 <      */
192 <     public void testNewTaskForRunnable() {
193 <         final AtomicBoolean done = new AtomicBoolean(false);
194 <         class MyRunnableFuture<V> extends FutureTask<V> {
195 <             MyRunnableFuture(Runnable t, V r) { super(t, r); }
204 <             protected void done() { done.set(true); }
205 <         }
206 <         ExecutorService e = new ThreadPoolExecutor(
166 >            protected <T> RunnableFuture<T> newTaskFor(Callable<T> c) {
167 >                return new MyCallableFuture<T>(c);
168 >            }};
169 >        ExecutorCompletionService<String> ecs =
170 >            new ExecutorCompletionService<String>(e);
171 >        try {
172 >            assertNull(ecs.poll());
173 >            Callable<String> c = new StringTask();
174 >            Future f1 = ecs.submit(c);
175 >            assertTrue("submit must return MyCallableFuture",
176 >                       f1 instanceof MyCallableFuture);
177 >            Future f2 = ecs.take();
178 >            assertSame("submit and take must return same objects", f1, f2);
179 >            assertTrue("completed task must have set done", done.get());
180 >        } finally {
181 >            joinPool(e);
182 >        }
183 >    }
184 >
185 >    /**
186 >     * Submitting to underlying AES that overrides newTaskFor(Runnable,T)
187 >     * returns and eventually runs Future returned by newTaskFor.
188 >     */
189 >    public void testNewTaskForRunnable() throws InterruptedException {
190 >        final AtomicBoolean done = new AtomicBoolean(false);
191 >        class MyRunnableFuture<V> extends FutureTask<V> {
192 >            MyRunnableFuture(Runnable t, V r) { super(t, r); }
193 >            protected void done() { done.set(true); }
194 >        }
195 >        ExecutorService e = new ThreadPoolExecutor(
196                                   1, 1, 30L, TimeUnit.SECONDS,
197                                   new ArrayBlockingQueue<Runnable>(1)) {
198 <             protected <T> RunnableFuture<T> newTaskFor(Runnable t, T r) {
199 <                 return new MyRunnableFuture<T>(t, r);
200 <             }
201 <         };
202 <         ExecutorCompletionService<String> ecs =
203 <             new ExecutorCompletionService<String>(e);
204 <         try {
205 <             assertNull(ecs.poll());
206 <             Runnable r = new NoOpRunnable();
207 <             Future f1 = ecs.submit(r, null);
208 <             assertTrue("submit must return MyRunnableFuture",
209 <                        f1 instanceof MyRunnableFuture);
210 <             Future f2 = ecs.take();
211 <             assertSame("submit and take must return same objects", f1, f2);
212 <             assertTrue("completed task must have set done", done.get());
213 <         } catch (Exception ex) {
214 <             unexpectedException();
215 <         } finally {
227 <             joinPool(e);
228 <         }
229 <     }
230 <
231 <
198 >            protected <T> RunnableFuture<T> newTaskFor(Runnable t, T r) {
199 >                return new MyRunnableFuture<T>(t, r);
200 >            }};
201 >        ExecutorCompletionService<String> ecs =
202 >            new ExecutorCompletionService<String>(e);
203 >        try {
204 >            assertNull(ecs.poll());
205 >            Runnable r = new NoOpRunnable();
206 >            Future f1 = ecs.submit(r, null);
207 >            assertTrue("submit must return MyRunnableFuture",
208 >                       f1 instanceof MyRunnableFuture);
209 >            Future f2 = ecs.take();
210 >            assertSame("submit and take must return same objects", f1, f2);
211 >            assertTrue("completed task must have set done", done.get());
212 >        } finally {
213 >            joinPool(e);
214 >        }
215 >    }
216  
217   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines