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.23 by jsr166, Tue May 3 23:06:12 2016 UTC vs.
Revision 1.26 by jsr166, Mon May 23 18:29:31 2016 UTC

# Line 11 | Line 11 | import static java.util.concurrent.TimeU
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;
15 import java.util.concurrent.Executors;
18   import java.util.concurrent.ExecutorService;
19   import java.util.concurrent.Future;
20   import java.util.concurrent.FutureTask;
# Line 33 | Line 35 | public class ExecutorCompletionServiceTe
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 {
# Line 43 | Line 45 | public class ExecutorCompletionServiceTe
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();
51 <            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 <        final ExecutorService e = Executors.newCachedThreadPool();
62 <        final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
63 <        try (PoolCleaner cleaner = cleaner(e)) {
64 <            Callable c = null;
65 <            try {
65 <                ecs.submit(c);
66 <                shouldThrow();
67 <            } catch (NullPointerException success) {}
68 <        }
60 >    public void testSubmitNullCallable() {
61 >        CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
62 >        try {
63 >            cs.submit((Callable) null);
64 >            shouldThrow();
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 <        final ExecutorService e = Executors.newCachedThreadPool();
73 <        final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
74 <        try (PoolCleaner cleaner = cleaner(e)) {
75 <            Runnable r = null;
76 <            try {
80 <                ecs.submit(r, Boolean.TRUE);
81 <                shouldThrow();
82 <            } catch (NullPointerException success) {}
83 <        }
71 >    public void testSubmitNullRunnable() {
72 >        CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
73 >        try {
74 >            cs.submit((Runnable) null, Boolean.TRUE);
75 >            shouldThrow();
76 >        } catch (NullPointerException success) {}
77      }
78  
79      /**
80       * A taken submitted task is completed
81       */
82 <    public void testTake() throws InterruptedException {
83 <        final ExecutorService e = Executors.newCachedThreadPool();
84 <        final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
85 <        try (PoolCleaner cleaner = cleaner(e)) {
86 <            Callable c = new StringTask();
87 <            ecs.submit(c);
88 <            Future f = ecs.take();
96 <            assertTrue(f.isDone());
97 <        }
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 <        final ExecutorService e = Executors.newCachedThreadPool();
96 <        final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
97 <        try (PoolCleaner cleaner = cleaner(e)) {
98 <            Callable c = new StringTask();
99 <            Future f1 = ecs.submit(c);
100 <            Future f2 = ecs.take();
101 <            assertSame(f1, f2);
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 <     * If poll returns non-null, the returned task is completed
122 >     * timed poll returns non-null when the returned task is completed
123       */
124 <    public void testPoll1() throws Exception {
125 <        final ExecutorService e = Executors.newCachedThreadPool();
126 <        final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
127 <        try (PoolCleaner cleaner = cleaner(e)) {
128 <            assertNull(ecs.poll());
129 <            Callable c = new StringTask();
130 <            ecs.submit(c);
131 <
132 <            long startTime = System.nanoTime();
133 <            Future f;
134 <            while ((f = ecs.poll()) == null) {
135 <                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
129 <                    fail("timed out");
130 <                Thread.yield();
131 <            }
132 <            assertTrue(f.isDone());
133 <            assertSame(TEST_STRING, f.get());
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 <     * If timed poll returns non-null, the returned task is completed
142 >     * poll returns null before the returned task is completed
143       */
144 <    public void testPoll2() throws InterruptedException {
145 <        final ExecutorService e = Executors.newCachedThreadPool();
146 <        final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
147 <        try (PoolCleaner cleaner = cleaner(e)) {
148 <            assertNull(ecs.poll());
149 <            Callable c = new StringTask();
150 <            ecs.submit(c);
151 <            Future f = ecs.poll(SHORT_DELAY_MS, MILLISECONDS);
152 <            if (f != null)
153 <                assertTrue(f.isDone());
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 >            }
185          }
186 +        assertEquals(2 * 1, normalCompletions);
187 +        assertEquals(2 * 2, exceptionalCompletions);
188 +        assertNull(cs.poll());
189      }
190  
191      /**
# Line 158 | 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          final ExecutorService e =
202              new ThreadPoolExecutor(1, 1,
# Line 167 | Line 205 | public class ExecutorCompletionServiceTe
205                  protected <T> RunnableFuture<T> newTaskFor(Callable<T> c) {
206                      return new MyCallableFuture<T>(c);
207                  }};
208 <        CompletionService<String> ecs =
171 <            new ExecutorCompletionService<>(e);
208 >        CompletionService<String> cs = new ExecutorCompletionService<>(e);
209          try (PoolCleaner cleaner = cleaner(e)) {
210 <            assertNull(ecs.poll());
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());
218          }
# Line 189 | 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          final ExecutorService e =
232              new ThreadPoolExecutor(1, 1,
# Line 198 | Line 235 | public class ExecutorCompletionServiceTe
235                  protected <T> RunnableFuture<T> newTaskFor(Runnable t, T r) {
236                      return new MyRunnableFuture<T>(t, r);
237                  }};
238 <        final CompletionService<String> ecs =
202 <            new ExecutorCompletionService<>(e);
238 >        CompletionService<String> cs = new ExecutorCompletionService<>(e);
239          try (PoolCleaner cleaner = cleaner(e)) {
240 <            assertNull(ecs.poll());
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());
248          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines