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.18 by jsr166, Tue May 31 16:16:23 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.*;
11 > import java.util.concurrent.ArrayBlockingQueue;
12 > import java.util.concurrent.Callable;
13 > import java.util.concurrent.ExecutorCompletionService;
14 > import java.util.concurrent.ExecutorService;
15 > import java.util.concurrent.Executors;
16 > import java.util.concurrent.Future;
17 > import java.util.concurrent.FutureTask;
18 > import java.util.concurrent.RunnableFuture;
19 > import java.util.concurrent.ThreadPoolExecutor;
20 > import java.util.concurrent.TimeUnit;
21   import static java.util.concurrent.TimeUnit.MILLISECONDS;
22 < import java.util.concurrent.atomic.*;
15 < import java.math.BigInteger;
22 > import java.util.concurrent.atomic.AtomicBoolean;
23   import java.security.*;
24  
25   public class ExecutorCompletionServiceTest extends JSR166TestCase {
26      public static void main(String[] args) {
27 <        junit.textui.TestRunner.run (suite());
27 >        junit.textui.TestRunner.run(suite());
28      }
29      public static Test suite() {
30          return new TestSuite(ExecutorCompletionServiceTest.class);
31      }
32  
26
33      /**
34       * Creating a new ECS with null Executor throw NPE
35       */
# Line 31 | Line 37 | public class ExecutorCompletionServiceTe
37          try {
38              ExecutorCompletionService ecs = new ExecutorCompletionService(null);
39              shouldThrow();
40 <        } catch (NullPointerException success) {
35 <        }
40 >        } catch (NullPointerException success) {}
41      }
42  
43      /**
# Line 43 | Line 48 | public class ExecutorCompletionServiceTe
48              ExecutorService e = Executors.newCachedThreadPool();
49              ExecutorCompletionService ecs = new ExecutorCompletionService(e, null);
50              shouldThrow();
51 <        } catch (NullPointerException success) {
47 <        }
51 >        } catch (NullPointerException success) {}
52      }
53  
54      /**
# Line 82 | Line 86 | public class ExecutorCompletionServiceTe
86      /**
87       * A taken submitted task is completed
88       */
89 <    public void testTake() {
89 >    public void testTake() throws InterruptedException {
90          ExecutorService e = Executors.newCachedThreadPool();
91          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
92          try {
# Line 90 | Line 94 | public class ExecutorCompletionServiceTe
94              ecs.submit(c);
95              Future f = ecs.take();
96              assertTrue(f.isDone());
93        } catch (Exception ex) {
94            unexpectedException();
97          } finally {
98              joinPool(e);
99          }
# Line 100 | Line 102 | public class ExecutorCompletionServiceTe
102      /**
103       * Take returns the same future object returned by submit
104       */
105 <    public void testTake2() {
105 >    public void testTake2() throws InterruptedException {
106          ExecutorService e = Executors.newCachedThreadPool();
107          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
108          try {
# Line 108 | Line 110 | public class ExecutorCompletionServiceTe
110              Future f1 = ecs.submit(c);
111              Future f2 = ecs.take();
112              assertSame(f1, f2);
111        } catch (Exception ex) {
112            unexpectedException();
113          } finally {
114              joinPool(e);
115          }
# Line 118 | Line 118 | public class ExecutorCompletionServiceTe
118      /**
119       * If poll returns non-null, the returned task is completed
120       */
121 <    public void testPoll1() {
121 >    public void testPoll1() throws Exception {
122          ExecutorService e = Executors.newCachedThreadPool();
123          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
124          try {
125              assertNull(ecs.poll());
126              Callable c = new StringTask();
127              ecs.submit(c);
128 <            Thread.sleep(SHORT_DELAY_MS);
129 <            for (;;) {
130 <                Future f = ecs.poll();
131 <                if (f != null) {
132 <                    assertTrue(f.isDone());
133 <                    break;
134 <                }
128 >
129 >            long startTime = System.nanoTime();
130 >            Future f;
131 >            while ((f = ecs.poll()) == null) {
132 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
133 >                    fail("timed out");
134 >                Thread.yield();
135              }
136 <        } catch (Exception ex) {
137 <            unexpectedException();
136 >            assertTrue(f.isDone());
137 >            assertSame(TEST_STRING, f.get());
138          } finally {
139              joinPool(e);
140          }
# Line 143 | Line 143 | public class ExecutorCompletionServiceTe
143      /**
144       * If timed poll returns non-null, the returned task is completed
145       */
146 <    public void testPoll2() {
146 >    public void testPoll2() throws InterruptedException {
147          ExecutorService e = Executors.newCachedThreadPool();
148          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
149          try {
# Line 153 | Line 153 | public class ExecutorCompletionServiceTe
153              Future f = ecs.poll(SHORT_DELAY_MS, MILLISECONDS);
154              if (f != null)
155                  assertTrue(f.isDone());
156        } catch (Exception ex) {
157            unexpectedException();
156          } finally {
157              joinPool(e);
158          }
159      }
160 <     /**
161 <      * Submitting to underlying AES that overrides newTaskFor(Callable)
162 <      * returns and eventually runs Future returned by newTaskFor.
163 <      */
164 <     public void testNewTaskForCallable() {
165 <         final AtomicBoolean done = new AtomicBoolean(false);
166 <         class MyCallableFuture<V> extends FutureTask<V> {
167 <             MyCallableFuture(Callable<V> c) { super(c); }
168 <             protected void done() { done.set(true); }
169 <         }
170 <         ExecutorService e = new ThreadPoolExecutor(
160 >
161 >    /**
162 >     * Submitting to underlying AES that overrides newTaskFor(Callable)
163 >     * returns and eventually runs Future returned by newTaskFor.
164 >     */
165 >    public void testNewTaskForCallable() throws InterruptedException {
166 >        final AtomicBoolean done = new AtomicBoolean(false);
167 >        class MyCallableFuture<V> extends FutureTask<V> {
168 >            MyCallableFuture(Callable<V> c) { super(c); }
169 >            protected void done() { done.set(true); }
170 >        }
171 >        ExecutorService e = new ThreadPoolExecutor(
172                                   1, 1, 30L, TimeUnit.SECONDS,
173                                   new ArrayBlockingQueue<Runnable>(1)) {
174 <             protected <T> RunnableFuture<T> newTaskFor(Callable<T> c) {
175 <                 return new MyCallableFuture<T>(c);
176 <             }
177 <         };
178 <         ExecutorCompletionService<String> ecs =
179 <             new ExecutorCompletionService<String>(e);
180 <         try {
181 <             assertNull(ecs.poll());
182 <             Callable<String> c = new StringTask();
183 <             Future f1 = ecs.submit(c);
184 <             assertTrue("submit must return MyCallableFuture",
185 <                        f1 instanceof MyCallableFuture);
186 <             Future f2 = ecs.take();
187 <             assertSame("submit and take must return same objects", f1, f2);
188 <             assertTrue("completed task must have set done", done.get());
189 <         } catch (Exception ex) {
190 <             unexpectedException();
191 <         } finally {
192 <             joinPool(e);
193 <         }
194 <     }
195 <
196 <     /**
197 <      * Submitting to underlying AES that overrides newTaskFor(Runnable,T)
198 <      * returns and eventually runs Future returned by newTaskFor.
199 <      */
200 <     public void testNewTaskForRunnable() {
201 <         final AtomicBoolean done = new AtomicBoolean(false);
202 <         class MyRunnableFuture<V> extends FutureTask<V> {
203 <             MyRunnableFuture(Runnable t, V r) { super(t, r); }
205 <             protected void done() { done.set(true); }
206 <         }
207 <         ExecutorService e = new ThreadPoolExecutor(
174 >            protected <T> RunnableFuture<T> newTaskFor(Callable<T> c) {
175 >                return new MyCallableFuture<T>(c);
176 >            }};
177 >        ExecutorCompletionService<String> ecs =
178 >            new ExecutorCompletionService<String>(e);
179 >        try {
180 >            assertNull(ecs.poll());
181 >            Callable<String> c = new StringTask();
182 >            Future f1 = ecs.submit(c);
183 >            assertTrue("submit must return MyCallableFuture",
184 >                       f1 instanceof MyCallableFuture);
185 >            Future f2 = ecs.take();
186 >            assertSame("submit and take must return same objects", f1, f2);
187 >            assertTrue("completed task must have set done", done.get());
188 >        } finally {
189 >            joinPool(e);
190 >        }
191 >    }
192 >
193 >    /**
194 >     * Submitting to underlying AES that overrides newTaskFor(Runnable,T)
195 >     * returns and eventually runs Future returned by newTaskFor.
196 >     */
197 >    public void testNewTaskForRunnable() throws InterruptedException {
198 >        final AtomicBoolean done = new AtomicBoolean(false);
199 >        class MyRunnableFuture<V> extends FutureTask<V> {
200 >            MyRunnableFuture(Runnable t, V r) { super(t, r); }
201 >            protected void done() { done.set(true); }
202 >        }
203 >        ExecutorService e = new ThreadPoolExecutor(
204                                   1, 1, 30L, TimeUnit.SECONDS,
205                                   new ArrayBlockingQueue<Runnable>(1)) {
206 <             protected <T> RunnableFuture<T> newTaskFor(Runnable t, T r) {
207 <                 return new MyRunnableFuture<T>(t, r);
208 <             }
209 <         };
210 <         ExecutorCompletionService<String> ecs =
211 <             new ExecutorCompletionService<String>(e);
212 <         try {
213 <             assertNull(ecs.poll());
214 <             Runnable r = new NoOpRunnable();
215 <             Future f1 = ecs.submit(r, null);
216 <             assertTrue("submit must return MyRunnableFuture",
217 <                        f1 instanceof MyRunnableFuture);
218 <             Future f2 = ecs.take();
219 <             assertSame("submit and take must return same objects", f1, f2);
220 <             assertTrue("completed task must have set done", done.get());
221 <         } catch (Exception ex) {
222 <             unexpectedException();
223 <         } finally {
228 <             joinPool(e);
229 <         }
230 <     }
231 <
232 <
206 >            protected <T> RunnableFuture<T> newTaskFor(Runnable t, T r) {
207 >                return new MyRunnableFuture<T>(t, r);
208 >            }};
209 >        ExecutorCompletionService<String> ecs =
210 >            new ExecutorCompletionService<String>(e);
211 >        try {
212 >            assertNull(ecs.poll());
213 >            Runnable r = new NoOpRunnable();
214 >            Future f1 = ecs.submit(r, null);
215 >            assertTrue("submit must return MyRunnableFuture",
216 >                       f1 instanceof MyRunnableFuture);
217 >            Future f2 = ecs.take();
218 >            assertSame("submit and take must return same objects", f1, f2);
219 >            assertTrue("completed task must have set done", done.get());
220 >        } finally {
221 >            joinPool(e);
222 >        }
223 >    }
224  
225   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines