13 |
|
import java.util.concurrent.CompletionService; |
14 |
|
import java.util.concurrent.CountDownLatch; |
15 |
|
import java.util.concurrent.ExecutionException; |
16 |
– |
import java.util.concurrent.ExecutorCompletionService; |
16 |
|
import java.util.concurrent.Executor; |
17 |
< |
import java.util.concurrent.Executors; |
17 |
> |
import java.util.concurrent.ExecutorCompletionService; |
18 |
|
import java.util.concurrent.ExecutorService; |
20 |
– |
import java.util.concurrent.ForkJoinPool; |
19 |
|
import java.util.concurrent.Future; |
20 |
|
import java.util.concurrent.FutureTask; |
21 |
|
import java.util.concurrent.RunnableFuture; |
48 |
|
* new ExecutorCompletionService(e, null) throws NullPointerException |
49 |
|
*/ |
50 |
|
public void testConstructorNPE2() { |
53 |
– |
final Executor e = ForkJoinPool.commonPool(); |
51 |
|
try { |
52 |
< |
new ExecutorCompletionService(e, null); |
52 |
> |
new ExecutorCompletionService(cachedThreadPool, null); |
53 |
|
shouldThrow(); |
54 |
|
} catch (NullPointerException success) {} |
55 |
|
} |
58 |
|
* ecs.submit(null) throws NullPointerException |
59 |
|
*/ |
60 |
|
public void testSubmitNullCallable() { |
61 |
< |
final ExecutorCompletionService ecs = |
65 |
< |
new ExecutorCompletionService(ForkJoinPool.commonPool()); |
61 |
> |
CompletionService cs = new ExecutorCompletionService(cachedThreadPool); |
62 |
|
try { |
63 |
< |
ecs.submit((Callable) null); |
63 |
> |
cs.submit((Callable) null); |
64 |
|
shouldThrow(); |
65 |
|
} catch (NullPointerException success) {} |
66 |
|
} |
69 |
|
* ecs.submit(null, val) throws NullPointerException |
70 |
|
*/ |
71 |
|
public void testSubmitNullRunnable() { |
72 |
< |
final ExecutorCompletionService ecs = |
77 |
< |
new ExecutorCompletionService(ForkJoinPool.commonPool()); |
72 |
> |
CompletionService cs = new ExecutorCompletionService(cachedThreadPool); |
73 |
|
try { |
74 |
< |
ecs.submit((Runnable) null, Boolean.TRUE); |
74 |
> |
cs.submit((Runnable) null, Boolean.TRUE); |
75 |
|
shouldThrow(); |
76 |
|
} catch (NullPointerException success) {} |
77 |
|
} |
81 |
|
*/ |
82 |
|
public void testTake() |
83 |
|
throws InterruptedException, ExecutionException { |
84 |
< |
final ExecutorCompletionService ecs = |
85 |
< |
new ExecutorCompletionService(ForkJoinPool.commonPool()); |
86 |
< |
ecs.submit(new StringTask()); |
92 |
< |
Future f = ecs.take(); |
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 |
|
} |
92 |
|
* Take returns the same future object returned by submit |
93 |
|
*/ |
94 |
|
public void testTake2() throws InterruptedException { |
95 |
< |
final ExecutorCompletionService ecs = |
96 |
< |
new ExecutorCompletionService(ForkJoinPool.commonPool()); |
97 |
< |
Future f1 = ecs.submit(new StringTask()); |
104 |
< |
Future f2 = ecs.take(); |
95 |
> |
CompletionService cs = new ExecutorCompletionService(cachedThreadPool); |
96 |
> |
Future f1 = cs.submit(new StringTask()); |
97 |
> |
Future f2 = cs.take(); |
98 |
|
assertSame(f1, f2); |
99 |
|
} |
100 |
|
|
103 |
|
*/ |
104 |
|
public void testPoll1() |
105 |
|
throws InterruptedException, ExecutionException { |
106 |
< |
final ExecutorCompletionService ecs = |
107 |
< |
new ExecutorCompletionService(ForkJoinPool.commonPool()); |
108 |
< |
assertNull(ecs.poll()); |
116 |
< |
ecs.submit(new StringTask()); |
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 = ecs.poll()) == null) { |
112 |
> |
while ((f = cs.poll()) == null) { |
113 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
114 |
|
fail("timed out"); |
115 |
|
Thread.yield(); |
123 |
|
*/ |
124 |
|
public void testPoll2() |
125 |
|
throws InterruptedException, ExecutionException { |
126 |
< |
final ExecutorCompletionService ecs = |
127 |
< |
new ExecutorCompletionService(ForkJoinPool.commonPool()); |
128 |
< |
assertNull(ecs.poll()); |
137 |
< |
ecs.submit(new StringTask()); |
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 = ecs.poll(SHORT_DELAY_MS, MILLISECONDS)) == null) { |
132 |
> |
while ((f = cs.poll(SHORT_DELAY_MS, MILLISECONDS)) == null) { |
133 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
134 |
|
fail("timed out"); |
135 |
|
Thread.yield(); |
143 |
|
*/ |
144 |
|
public void testPollReturnsNull() |
145 |
|
throws InterruptedException, ExecutionException { |
146 |
< |
final ExecutorCompletionService ecs = |
147 |
< |
new ExecutorCompletionService(ForkJoinPool.commonPool()); |
148 |
< |
final CountDownLatch proceed = new CountDownLatch(1); |
158 |
< |
ecs.submit(new Callable() { public String call() throws Exception { |
146 |
> |
CompletionService cs = new ExecutorCompletionService(cachedThreadPool); |
147 |
> |
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(ecs.poll()); |
153 |
< |
assertNull(ecs.poll(0L, MILLISECONDS)); |
154 |
< |
assertNull(ecs.poll(Long.MIN_VALUE, MILLISECONDS)); |
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(ecs.poll(timeoutMillis(), MILLISECONDS)); |
156 |
> |
assertNull(cs.poll(timeoutMillis(), MILLISECONDS)); |
157 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
158 |
|
proceed.countDown(); |
159 |
< |
assertSame(TEST_STRING, ecs.take().get()); |
159 |
> |
assertSame(TEST_STRING, cs.take().get()); |
160 |
|
} |
161 |
|
|
162 |
|
/** |
164 |
|
*/ |
165 |
|
public void testTaskAssortment() |
166 |
|
throws InterruptedException, ExecutionException { |
167 |
< |
final ExecutorService e = Executors.newCachedThreadPool(); |
168 |
< |
final CompletionService cs = new ExecutorCompletionService(e); |
169 |
< |
final ArithmeticException ex = new ArithmeticException(); |
170 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
171 |
< |
for (int i = 0; i < 2; i++) { |
172 |
< |
cs.submit(new StringTask()); |
173 |
< |
cs.submit(callableThrowing(ex)); |
174 |
< |
cs.submit(runnableThrowing(ex), null); |
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 |
< |
int normalCompletions = 0; |
182 |
< |
int exceptionalCompletions = 0; |
183 |
< |
for (int i = 0; i < 3 * 2; i++) { |
189 |
< |
try { |
190 |
< |
if (cs.take().get() == TEST_STRING) |
191 |
< |
normalCompletions++; |
192 |
< |
} |
193 |
< |
catch (ExecutionException expected) { |
194 |
< |
assertTrue(expected.getCause() instanceof ArithmeticException); |
195 |
< |
exceptionalCompletions++; |
196 |
< |
} |
181 |
> |
catch (ExecutionException expected) { |
182 |
> |
assertTrue(expected.getCause() instanceof ArithmeticException); |
183 |
> |
exceptionalCompletions++; |
184 |
|
} |
198 |
– |
assertEquals(2 * 1, normalCompletions); |
199 |
– |
assertEquals(2 * 2, exceptionalCompletions); |
200 |
– |
assertNull(cs.poll()); |
185 |
|
} |
186 |
+ |
assertEquals(2 * 1, normalCompletions); |
187 |
+ |
assertEquals(2 * 2, exceptionalCompletions); |
188 |
+ |
assertNull(cs.poll()); |
189 |
|
} |
190 |
|
|
191 |
|
/** |