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.19 by jsr166, Wed Dec 31 19:05:42 2014 UTC vs.
Revision 1.26 by jsr166, Mon May 23 18:29:31 2016 UTC

# Line 10 | Line 10 | import static java.util.concurrent.TimeU
10  
11   import java.util.concurrent.ArrayBlockingQueue;
12   import java.util.concurrent.Callable;
13 + import java.util.concurrent.CompletionService;
14 + import java.util.concurrent.CountDownLatch;
15 + import java.util.concurrent.ExecutionException;
16 + import java.util.concurrent.Executor;
17   import java.util.concurrent.ExecutorCompletionService;
14 import java.util.concurrent.Executors;
18   import java.util.concurrent.ExecutorService;
19   import java.util.concurrent.Future;
20   import java.util.concurrent.FutureTask;
# Line 25 | Line 28 | import junit.framework.TestSuite;
28  
29   public class ExecutorCompletionServiceTest extends JSR166TestCase {
30      public static void main(String[] args) {
31 <        junit.textui.TestRunner.run(suite());
31 >        main(suite(), args);
32      }
33      public static Test suite() {
34          return new TestSuite(ExecutorCompletionServiceTest.class);
35      }
36  
37      /**
38 <     * Creating a new ECS with null Executor throw NPE
38 >     * new ExecutorCompletionService(null) throws NullPointerException
39       */
40      public void testConstructorNPE() {
41          try {
42 <            ExecutorCompletionService ecs = new ExecutorCompletionService(null);
42 >            new ExecutorCompletionService(null);
43              shouldThrow();
44          } catch (NullPointerException success) {}
45      }
46  
47      /**
48 <     * Creating a new ECS with null queue throw NPE
48 >     * new ExecutorCompletionService(e, null) throws NullPointerException
49       */
50      public void testConstructorNPE2() {
51          try {
52 <            ExecutorService e = Executors.newCachedThreadPool();
50 <            ExecutorCompletionService ecs = new ExecutorCompletionService(e, null);
52 >            new ExecutorCompletionService(cachedThreadPool, null);
53              shouldThrow();
54          } catch (NullPointerException success) {}
55      }
56  
57      /**
58 <     * Submitting a null callable throws NPE
58 >     * ecs.submit(null) throws NullPointerException
59       */
60 <    public void testSubmitNPE() {
61 <        ExecutorService e = Executors.newCachedThreadPool();
60 <        ExecutorCompletionService ecs = new ExecutorCompletionService(e);
60 >    public void testSubmitNullCallable() {
61 >        CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
62          try {
63 <            Callable c = null;
63 <            ecs.submit(c);
63 >            cs.submit((Callable) null);
64              shouldThrow();
65 <        } catch (NullPointerException success) {
66 <        } finally {
67 <            joinPool(e);
68 <        }
65 >        } catch (NullPointerException success) {}
66      }
67  
68      /**
69 <     * Submitting a null runnable throws NPE
69 >     * ecs.submit(null, val) throws NullPointerException
70       */
71 <    public void testSubmitNPE2() {
72 <        ExecutorService e = Executors.newCachedThreadPool();
76 <        ExecutorCompletionService ecs = new ExecutorCompletionService(e);
71 >    public void testSubmitNullRunnable() {
72 >        CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
73          try {
74 <            Runnable r = null;
79 <            ecs.submit(r, Boolean.TRUE);
74 >            cs.submit((Runnable) null, Boolean.TRUE);
75              shouldThrow();
76 <        } catch (NullPointerException success) {
82 <        } finally {
83 <            joinPool(e);
84 <        }
76 >        } catch (NullPointerException success) {}
77      }
78  
79      /**
80       * A taken submitted task is completed
81       */
82 <    public void testTake() throws InterruptedException {
83 <        ExecutorService e = Executors.newCachedThreadPool();
84 <        ExecutorCompletionService ecs = new ExecutorCompletionService(e);
85 <        try {
86 <            Callable c = new StringTask();
87 <            ecs.submit(c);
88 <            Future f = ecs.take();
97 <            assertTrue(f.isDone());
98 <        } finally {
99 <            joinPool(e);
100 <        }
82 >    public void testTake()
83 >        throws InterruptedException, ExecutionException {
84 >        CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
85 >        cs.submit(new StringTask());
86 >        Future f = cs.take();
87 >        assertTrue(f.isDone());
88 >        assertSame(TEST_STRING, f.get());
89      }
90  
91      /**
92       * Take returns the same future object returned by submit
93       */
94      public void testTake2() throws InterruptedException {
95 <        ExecutorService e = Executors.newCachedThreadPool();
96 <        ExecutorCompletionService ecs = new ExecutorCompletionService(e);
97 <        try {
98 <            Callable c = new StringTask();
99 <            Future f1 = ecs.submit(c);
100 <            Future f2 = ecs.take();
101 <            assertSame(f1, f2);
102 <        } finally {
103 <            joinPool(e);
104 <        }
105 <    }
106 <
107 <    /**
108 <     * If poll returns non-null, the returned task is completed
109 <     */
110 <    public void testPoll1() throws Exception {
111 <        ExecutorService e = Executors.newCachedThreadPool();
112 <        ExecutorCompletionService ecs = new ExecutorCompletionService(e);
113 <        try {
114 <            assertNull(ecs.poll());
115 <            Callable c = new StringTask();
116 <            ecs.submit(c);
117 <
118 <            long startTime = System.nanoTime();
119 <            Future f;
120 <            while ((f = ecs.poll()) == null) {
121 <                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
122 <                    fail("timed out");
123 <                Thread.yield();
95 >        CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
96 >        Future f1 = cs.submit(new StringTask());
97 >        Future f2 = cs.take();
98 >        assertSame(f1, f2);
99 >    }
100 >
101 >    /**
102 >     * poll returns non-null when the returned task is completed
103 >     */
104 >    public void testPoll1()
105 >        throws InterruptedException, ExecutionException {
106 >        CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
107 >        assertNull(cs.poll());
108 >        cs.submit(new StringTask());
109 >
110 >        long startTime = System.nanoTime();
111 >        Future f;
112 >        while ((f = cs.poll()) == null) {
113 >            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
114 >                fail("timed out");
115 >            Thread.yield();
116 >        }
117 >        assertTrue(f.isDone());
118 >        assertSame(TEST_STRING, f.get());
119 >    }
120 >
121 >    /**
122 >     * timed poll returns non-null when the returned task is completed
123 >     */
124 >    public void testPoll2()
125 >        throws InterruptedException, ExecutionException {
126 >        CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
127 >        assertNull(cs.poll());
128 >        cs.submit(new StringTask());
129 >
130 >        long startTime = System.nanoTime();
131 >        Future f;
132 >        while ((f = cs.poll(SHORT_DELAY_MS, MILLISECONDS)) == null) {
133 >            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
134 >                fail("timed out");
135 >            Thread.yield();
136 >        }
137 >        assertTrue(f.isDone());
138 >        assertSame(TEST_STRING, f.get());
139 >    }
140 >
141 >    /**
142 >     * poll returns null before the returned task is completed
143 >     */
144 >    public void testPollReturnsNull()
145 >        throws InterruptedException, ExecutionException {
146 >        CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
147 >        final CountDownLatch proceed = new CountDownLatch(1);
148 >        cs.submit(new Callable() { public String call() throws Exception {
149 >            proceed.await();
150 >            return TEST_STRING;
151 >        }});
152 >        assertNull(cs.poll());
153 >        assertNull(cs.poll(0L, MILLISECONDS));
154 >        assertNull(cs.poll(Long.MIN_VALUE, MILLISECONDS));
155 >        long startTime = System.nanoTime();
156 >        assertNull(cs.poll(timeoutMillis(), MILLISECONDS));
157 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
158 >        proceed.countDown();
159 >        assertSame(TEST_STRING, cs.take().get());
160 >    }
161 >
162 >    /**
163 >     * successful and failed tasks are both returned
164 >     */
165 >    public void testTaskAssortment()
166 >        throws InterruptedException, ExecutionException {
167 >        CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
168 >        ArithmeticException ex = new ArithmeticException();
169 >        for (int i = 0; i < 2; i++) {
170 >            cs.submit(new StringTask());
171 >            cs.submit(callableThrowing(ex));
172 >            cs.submit(runnableThrowing(ex), null);
173 >        }
174 >        int normalCompletions = 0;
175 >        int exceptionalCompletions = 0;
176 >        for (int i = 0; i < 3 * 2; i++) {
177 >            try {
178 >                if (cs.take().get() == TEST_STRING)
179 >                    normalCompletions++;
180 >            }
181 >            catch (ExecutionException expected) {
182 >                assertTrue(expected.getCause() instanceof ArithmeticException);
183 >                exceptionalCompletions++;
184              }
137            assertTrue(f.isDone());
138            assertSame(TEST_STRING, f.get());
139        } finally {
140            joinPool(e);
141        }
142    }
143
144    /**
145     * If timed poll returns non-null, the returned task is completed
146     */
147    public void testPoll2() throws InterruptedException {
148        ExecutorService e = Executors.newCachedThreadPool();
149        ExecutorCompletionService ecs = new ExecutorCompletionService(e);
150        try {
151            assertNull(ecs.poll());
152            Callable c = new StringTask();
153            ecs.submit(c);
154            Future f = ecs.poll(SHORT_DELAY_MS, MILLISECONDS);
155            if (f != null)
156                assertTrue(f.isDone());
157        } finally {
158            joinPool(e);
185          }
186 +        assertEquals(2 * 1, normalCompletions);
187 +        assertEquals(2 * 2, exceptionalCompletions);
188 +        assertNull(cs.poll());
189      }
190  
191      /**
# Line 167 | Line 196 | public class ExecutorCompletionServiceTe
196          final AtomicBoolean done = new AtomicBoolean(false);
197          class MyCallableFuture<V> extends FutureTask<V> {
198              MyCallableFuture(Callable<V> c) { super(c); }
199 <            protected void done() { done.set(true); }
199 >            @Override protected void done() { done.set(true); }
200          }
201 <        ExecutorService e = new ThreadPoolExecutor(
202 <                                 1, 1, 30L, TimeUnit.SECONDS,
203 <                                 new ArrayBlockingQueue<Runnable>(1)) {
204 <            protected <T> RunnableFuture<T> newTaskFor(Callable<T> c) {
205 <                return new MyCallableFuture<T>(c);
206 <            }};
207 <        ExecutorCompletionService<String> ecs =
208 <            new ExecutorCompletionService<String>(e);
209 <        try {
210 <            assertNull(ecs.poll());
201 >        final ExecutorService e =
202 >            new ThreadPoolExecutor(1, 1,
203 >                                   30L, TimeUnit.SECONDS,
204 >                                   new ArrayBlockingQueue<Runnable>(1)) {
205 >                protected <T> RunnableFuture<T> newTaskFor(Callable<T> c) {
206 >                    return new MyCallableFuture<T>(c);
207 >                }};
208 >        CompletionService<String> cs = new ExecutorCompletionService<>(e);
209 >        try (PoolCleaner cleaner = cleaner(e)) {
210 >            assertNull(cs.poll());
211              Callable<String> c = new StringTask();
212 <            Future f1 = ecs.submit(c);
212 >            Future f1 = cs.submit(c);
213              assertTrue("submit must return MyCallableFuture",
214                         f1 instanceof MyCallableFuture);
215 <            Future f2 = ecs.take();
215 >            Future f2 = cs.take();
216              assertSame("submit and take must return same objects", f1, f2);
217              assertTrue("completed task must have set done", done.get());
189        } finally {
190            joinPool(e);
218          }
219      }
220  
# Line 199 | Line 226 | public class ExecutorCompletionServiceTe
226          final AtomicBoolean done = new AtomicBoolean(false);
227          class MyRunnableFuture<V> extends FutureTask<V> {
228              MyRunnableFuture(Runnable t, V r) { super(t, r); }
229 <            protected void done() { done.set(true); }
229 >            @Override protected void done() { done.set(true); }
230          }
231 <        ExecutorService e = new ThreadPoolExecutor(
232 <                                 1, 1, 30L, TimeUnit.SECONDS,
233 <                                 new ArrayBlockingQueue<Runnable>(1)) {
234 <            protected <T> RunnableFuture<T> newTaskFor(Runnable t, T r) {
235 <                return new MyRunnableFuture<T>(t, r);
236 <            }};
237 <        ExecutorCompletionService<String> ecs =
238 <            new ExecutorCompletionService<String>(e);
239 <        try {
240 <            assertNull(ecs.poll());
231 >        final ExecutorService e =
232 >            new ThreadPoolExecutor(1, 1,
233 >                                   30L, TimeUnit.SECONDS,
234 >                                   new ArrayBlockingQueue<Runnable>(1)) {
235 >                protected <T> RunnableFuture<T> newTaskFor(Runnable t, T r) {
236 >                    return new MyRunnableFuture<T>(t, r);
237 >                }};
238 >        CompletionService<String> cs = new ExecutorCompletionService<>(e);
239 >        try (PoolCleaner cleaner = cleaner(e)) {
240 >            assertNull(cs.poll());
241              Runnable r = new NoOpRunnable();
242 <            Future f1 = ecs.submit(r, null);
242 >            Future f1 = cs.submit(r, null);
243              assertTrue("submit must return MyRunnableFuture",
244                         f1 instanceof MyRunnableFuture);
245 <            Future f2 = ecs.take();
245 >            Future f2 = cs.take();
246              assertSame("submit and take must return same objects", f1, f2);
247              assertTrue("completed task must have set done", done.get());
221        } finally {
222            joinPool(e);
248          }
249      }
250  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines