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