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.7 by jsr166, Mon Nov 2 20:28:31 2009 UTC vs.
Revision 1.17 by jsr166, Sun May 29 06:54: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.*;
12 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
13   import java.util.concurrent.atomic.*;
14 import java.math.BigInteger;
14   import java.security.*;
15  
16 < public class ExecutorCompletionServiceTest extends JSR166TestCase{
16 > public class ExecutorCompletionServiceTest extends JSR166TestCase {
17      public static void main(String[] args) {
18 <        junit.textui.TestRunner.run (suite());
18 >        junit.textui.TestRunner.run(suite());
19      }
20      public static Test suite() {
21          return new TestSuite(ExecutorCompletionServiceTest.class);
22      }
23  
25
24      /**
25       * Creating a new ECS with null Executor throw NPE
26       */
# Line 30 | Line 28 | public class ExecutorCompletionServiceTe
28          try {
29              ExecutorCompletionService ecs = new ExecutorCompletionService(null);
30              shouldThrow();
31 <        } catch (NullPointerException success) {
34 <        }
31 >        } catch (NullPointerException success) {}
32      }
33  
34      /**
# Line 42 | Line 39 | public class ExecutorCompletionServiceTe
39              ExecutorService e = Executors.newCachedThreadPool();
40              ExecutorCompletionService ecs = new ExecutorCompletionService(e, null);
41              shouldThrow();
42 <        } catch (NullPointerException success) {
46 <        }
42 >        } catch (NullPointerException success) {}
43      }
44  
45      /**
# Line 81 | Line 77 | public class ExecutorCompletionServiceTe
77      /**
78       * A taken submitted task is completed
79       */
80 <    public void testTake() {
80 >    public void testTake() throws InterruptedException {
81          ExecutorService e = Executors.newCachedThreadPool();
82          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
83          try {
# Line 89 | Line 85 | public class ExecutorCompletionServiceTe
85              ecs.submit(c);
86              Future f = ecs.take();
87              assertTrue(f.isDone());
92        } catch (Exception ex) {
93            unexpectedException();
88          } finally {
89              joinPool(e);
90          }
# Line 99 | Line 93 | public class ExecutorCompletionServiceTe
93      /**
94       * Take returns the same future object returned by submit
95       */
96 <    public void testTake2() {
96 >    public void testTake2() throws InterruptedException {
97          ExecutorService e = Executors.newCachedThreadPool();
98          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
99          try {
# Line 107 | Line 101 | public class ExecutorCompletionServiceTe
101              Future f1 = ecs.submit(c);
102              Future f2 = ecs.take();
103              assertSame(f1, f2);
110        } catch (Exception ex) {
111            unexpectedException();
104          } finally {
105              joinPool(e);
106          }
# Line 117 | Line 109 | public class ExecutorCompletionServiceTe
109      /**
110       * If poll returns non-null, the returned task is completed
111       */
112 <    public void testPoll1() {
112 >    public void testPoll1() throws Exception {
113          ExecutorService e = Executors.newCachedThreadPool();
114          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
115          try {
116              assertNull(ecs.poll());
117              Callable c = new StringTask();
118              ecs.submit(c);
119 <            Thread.sleep(SHORT_DELAY_MS);
120 <            for (;;) {
121 <                Future f = ecs.poll();
122 <                if (f != null) {
123 <                    assertTrue(f.isDone());
124 <                    break;
125 <                }
119 >
120 >            long startTime = System.nanoTime();
121 >            Future f;
122 >            while ((f = ecs.poll()) == null) {
123 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
124 >                    fail("timed out");
125 >                Thread.yield();
126              }
127 <        } catch (Exception ex) {
128 <            unexpectedException();
127 >            assertTrue(f.isDone());
128 >            assertSame(TEST_STRING, f.get());
129          } finally {
130              joinPool(e);
131          }
# Line 142 | Line 134 | public class ExecutorCompletionServiceTe
134      /**
135       * If timed poll returns non-null, the returned task is completed
136       */
137 <    public void testPoll2() {
137 >    public void testPoll2() throws InterruptedException {
138          ExecutorService e = Executors.newCachedThreadPool();
139          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
140          try {
141              assertNull(ecs.poll());
142              Callable c = new StringTask();
143              ecs.submit(c);
144 <            Future f = ecs.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
144 >            Future f = ecs.poll(SHORT_DELAY_MS, MILLISECONDS);
145              if (f != null)
146                  assertTrue(f.isDone());
155        } catch (Exception ex) {
156            unexpectedException();
147          } finally {
148              joinPool(e);
149          }
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() {
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(
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() throws InterruptedException {
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(
163                                   1, 1, 30L, TimeUnit.SECONDS,
164                                   new ArrayBlockingQueue<Runnable>(1)) {
165 <             protected <T> RunnableFuture<T> newTaskFor(Callable<T> c) {
166 <                 return new MyCallableFuture<T>(c);
167 <             }
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 <         } catch (Exception ex) {
181 <             unexpectedException();
182 <         } finally {
183 <             joinPool(e);
184 <         }
185 <     }
186 <
187 <     /**
188 <      * Submitting to underlying AES that overrides newTaskFor(Runnable,T)
189 <      * returns and eventually runs Future returned by newTaskFor.
190 <      */
191 <     public void testNewTaskForRunnable() {
192 <         final AtomicBoolean done = new AtomicBoolean(false);
193 <         class MyRunnableFuture<V> extends FutureTask<V> {
194 <             MyRunnableFuture(Runnable t, V r) { super(t, r); }
204 <             protected void done() { done.set(true); }
205 <         }
206 <         ExecutorService e = new ThreadPoolExecutor(
165 >            protected <T> RunnableFuture<T> newTaskFor(Callable<T> c) {
166 >                return new MyCallableFuture<T>(c);
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 >        } finally {
180 >            joinPool(e);
181 >        }
182 >    }
183 >
184 >    /**
185 >     * Submitting to underlying AES that overrides newTaskFor(Runnable,T)
186 >     * returns and eventually runs Future returned by newTaskFor.
187 >     */
188 >    public void testNewTaskForRunnable() throws InterruptedException {
189 >        final AtomicBoolean done = new AtomicBoolean(false);
190 >        class MyRunnableFuture<V> extends FutureTask<V> {
191 >            MyRunnableFuture(Runnable t, V r) { super(t, r); }
192 >            protected void done() { done.set(true); }
193 >        }
194 >        ExecutorService e = new ThreadPoolExecutor(
195                                   1, 1, 30L, TimeUnit.SECONDS,
196                                   new ArrayBlockingQueue<Runnable>(1)) {
197 <             protected <T> RunnableFuture<T> newTaskFor(Runnable t, T r) {
198 <                 return new MyRunnableFuture<T>(t, r);
199 <             }
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 <         } catch (Exception ex) {
213 <             unexpectedException();
214 <         } finally {
227 <             joinPool(e);
228 <         }
229 <     }
230 <
231 <
197 >            protected <T> RunnableFuture<T> newTaskFor(Runnable t, T r) {
198 >                return new MyRunnableFuture<T>(t, r);
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 >        } finally {
212 >            joinPool(e);
213 >        }
214 >    }
215  
216   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines