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.3 by dl, Tue May 3 16:02:00 2005 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
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
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 java.math.BigInteger;
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.AtomicBoolean;
23   import java.security.*;
24  
25 < public class ExecutorCompletionServiceTest extends JSR166TestCase{
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  
24
33      /**
34       * Creating a new ECS with null Executor throw NPE
35 <     */
35 >     */
36      public void testConstructorNPE() {
37          try {
38              ExecutorCompletionService ecs = new ExecutorCompletionService(null);
39              shouldThrow();
40 <        } catch (NullPointerException success) {
33 <        }
40 >        } catch (NullPointerException success) {}
41      }
42  
43      /**
44       * Creating a new ECS with null queue throw NPE
45 <     */
45 >     */
46      public void testConstructorNPE2() {
47          try {
48              ExecutorService e = Executors.newCachedThreadPool();
49              ExecutorCompletionService ecs = new ExecutorCompletionService(e, null);
50              shouldThrow();
51 <        } catch (NullPointerException success) {
45 <        }
51 >        } catch (NullPointerException success) {}
52      }
53  
54      /**
55       * Submitting a null callable throws NPE
56 <     */
56 >     */
57      public void testSubmitNPE() {
58          ExecutorService e = Executors.newCachedThreadPool();
59          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
# Line 63 | Line 69 | public class ExecutorCompletionServiceTe
69  
70      /**
71       * Submitting a null runnable throws NPE
72 <     */
72 >     */
73      public void testSubmitNPE2() {
74          ExecutorService e = Executors.newCachedThreadPool();
75          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
# Line 79 | Line 85 | public class ExecutorCompletionServiceTe
85  
86      /**
87       * A taken submitted task is completed
88 <     */
89 <    public void testTake() {
88 >     */
89 >    public void testTake() throws InterruptedException {
90          ExecutorService e = Executors.newCachedThreadPool();
91          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
92          try {
# Line 88 | Line 94 | public class ExecutorCompletionServiceTe
94              ecs.submit(c);
95              Future f = ecs.take();
96              assertTrue(f.isDone());
91        } catch (Exception ex) {
92            unexpectedException();
97          } finally {
98              joinPool(e);
99          }
# Line 97 | Line 101 | public class ExecutorCompletionServiceTe
101  
102      /**
103       * Take returns the same future object returned by submit
104 <     */
105 <    public void testTake2() {
104 >     */
105 >    public void testTake2() throws InterruptedException {
106          ExecutorService e = Executors.newCachedThreadPool();
107          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
108          try {
# Line 106 | Line 110 | public class ExecutorCompletionServiceTe
110              Future f1 = ecs.submit(c);
111              Future f2 = ecs.take();
112              assertSame(f1, f2);
109        } catch (Exception ex) {
110            unexpectedException();
113          } finally {
114              joinPool(e);
115          }
# Line 115 | Line 117 | public class ExecutorCompletionServiceTe
117  
118      /**
119       * If poll returns non-null, the returned task is completed
120 <     */
121 <    public void testPoll1() {
120 >     */
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 140 | Line 142 | public class ExecutorCompletionServiceTe
142  
143      /**
144       * If timed poll returns non-null, the returned task is completed
145 <     */
146 <    public void testPoll2() {
145 >     */
146 >    public void testPoll2() throws InterruptedException {
147          ExecutorService e = Executors.newCachedThreadPool();
148          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
149          try {
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)
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 >    /**
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 >        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 >        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          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines