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 |
|
} |
78 |
|
/** |
79 |
|
* A taken submitted task is completed |
80 |
|
*/ |
81 |
< |
public void testTake() |
82 |
< |
throws InterruptedException, ExecutionException { |
83 |
< |
final ExecutorCompletionService ecs = |
84 |
< |
new ExecutorCompletionService(ForkJoinPool.commonPool()); |
91 |
< |
ecs.submit(new StringTask()); |
92 |
< |
Future f = ecs.take(); |
81 |
> |
public void testTake() throws Exception { |
82 |
> |
CompletionService cs = new ExecutorCompletionService(cachedThreadPool); |
83 |
> |
cs.submit(new StringTask()); |
84 |
> |
Future f = cs.take(); |
85 |
|
assertTrue(f.isDone()); |
86 |
|
assertSame(TEST_STRING, f.get()); |
87 |
|
} |
90 |
|
* Take returns the same future object returned by submit |
91 |
|
*/ |
92 |
|
public void testTake2() throws InterruptedException { |
93 |
< |
final ExecutorCompletionService ecs = |
94 |
< |
new ExecutorCompletionService(ForkJoinPool.commonPool()); |
95 |
< |
Future f1 = ecs.submit(new StringTask()); |
104 |
< |
Future f2 = ecs.take(); |
93 |
> |
CompletionService cs = new ExecutorCompletionService(cachedThreadPool); |
94 |
> |
Future f1 = cs.submit(new StringTask()); |
95 |
> |
Future f2 = cs.take(); |
96 |
|
assertSame(f1, f2); |
97 |
|
} |
98 |
|
|
99 |
|
/** |
100 |
|
* poll returns non-null when the returned task is completed |
101 |
|
*/ |
102 |
< |
public void testPoll1() |
103 |
< |
throws InterruptedException, ExecutionException { |
104 |
< |
final ExecutorCompletionService ecs = |
105 |
< |
new ExecutorCompletionService(ForkJoinPool.commonPool()); |
115 |
< |
assertNull(ecs.poll()); |
116 |
< |
ecs.submit(new StringTask()); |
102 |
> |
public void testPoll1() throws Exception { |
103 |
> |
CompletionService cs = new ExecutorCompletionService(cachedThreadPool); |
104 |
> |
assertNull(cs.poll()); |
105 |
> |
cs.submit(new StringTask()); |
106 |
|
|
107 |
|
long startTime = System.nanoTime(); |
108 |
|
Future f; |
109 |
< |
while ((f = ecs.poll()) == null) { |
109 |
> |
while ((f = cs.poll()) == null) { |
110 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
111 |
|
fail("timed out"); |
112 |
|
Thread.yield(); |
118 |
|
/** |
119 |
|
* timed poll returns non-null when the returned task is completed |
120 |
|
*/ |
121 |
< |
public void testPoll2() |
122 |
< |
throws InterruptedException, ExecutionException { |
123 |
< |
final ExecutorCompletionService ecs = |
124 |
< |
new ExecutorCompletionService(ForkJoinPool.commonPool()); |
136 |
< |
assertNull(ecs.poll()); |
137 |
< |
ecs.submit(new StringTask()); |
121 |
> |
public void testPoll2() throws Exception { |
122 |
> |
CompletionService cs = new ExecutorCompletionService(cachedThreadPool); |
123 |
> |
assertNull(cs.poll()); |
124 |
> |
cs.submit(new StringTask()); |
125 |
|
|
126 |
|
long startTime = System.nanoTime(); |
127 |
|
Future f; |
128 |
< |
while ((f = ecs.poll(SHORT_DELAY_MS, MILLISECONDS)) == null) { |
128 |
> |
while ((f = cs.poll(timeoutMillis(), MILLISECONDS)) == null) { |
129 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
130 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
131 |
|
fail("timed out"); |
132 |
|
Thread.yield(); |
138 |
|
/** |
139 |
|
* poll returns null before the returned task is completed |
140 |
|
*/ |
141 |
< |
public void testPollReturnsNull() |
142 |
< |
throws InterruptedException, ExecutionException { |
155 |
< |
final ExecutorCompletionService ecs = |
156 |
< |
new ExecutorCompletionService(ForkJoinPool.commonPool()); |
141 |
> |
public void testPollReturnsNullBeforeCompletion() throws Exception { |
142 |
> |
CompletionService cs = new ExecutorCompletionService(cachedThreadPool); |
143 |
|
final CountDownLatch proceed = new CountDownLatch(1); |
144 |
< |
ecs.submit(new Callable() { public String call() throws Exception { |
145 |
< |
proceed.await(); |
144 |
> |
cs.submit(new Callable() { public String call() throws Exception { |
145 |
> |
await(proceed); |
146 |
|
return TEST_STRING; |
147 |
|
}}); |
148 |
< |
assertNull(ecs.poll()); |
149 |
< |
assertNull(ecs.poll(0L, MILLISECONDS)); |
150 |
< |
assertNull(ecs.poll(Long.MIN_VALUE, MILLISECONDS)); |
148 |
> |
assertNull(cs.poll()); |
149 |
> |
assertNull(cs.poll(0L, MILLISECONDS)); |
150 |
> |
assertNull(cs.poll(Long.MIN_VALUE, MILLISECONDS)); |
151 |
|
long startTime = System.nanoTime(); |
152 |
< |
assertNull(ecs.poll(timeoutMillis(), MILLISECONDS)); |
152 |
> |
assertNull(cs.poll(timeoutMillis(), MILLISECONDS)); |
153 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
154 |
|
proceed.countDown(); |
155 |
< |
assertSame(TEST_STRING, ecs.take().get()); |
155 |
> |
assertSame(TEST_STRING, cs.take().get()); |
156 |
|
} |
157 |
|
|
158 |
|
/** |
159 |
|
* successful and failed tasks are both returned |
160 |
|
*/ |
161 |
< |
public void testTaskAssortment() |
162 |
< |
throws InterruptedException, ExecutionException { |
163 |
< |
final ExecutorService e = Executors.newCachedThreadPool(); |
164 |
< |
final CompletionService cs = new ExecutorCompletionService(e); |
165 |
< |
final ArithmeticException ex = new ArithmeticException(); |
166 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
167 |
< |
for (int i = 0; i < 2; i++) { |
168 |
< |
cs.submit(new StringTask()); |
169 |
< |
cs.submit(callableThrowing(ex)); |
170 |
< |
cs.submit(runnableThrowing(ex), null); |
171 |
< |
} |
172 |
< |
int normalCompletions = 0; |
173 |
< |
int exceptionalCompletions = 0; |
174 |
< |
for (int i = 0; i < 3 * 2; i++) { |
175 |
< |
try { |
176 |
< |
if (cs.take().get() == TEST_STRING) |
177 |
< |
normalCompletions++; |
178 |
< |
} |
193 |
< |
catch (ExecutionException expected) { |
194 |
< |
assertTrue(expected.getCause() instanceof ArithmeticException); |
195 |
< |
exceptionalCompletions++; |
196 |
< |
} |
161 |
> |
public void testTaskAssortment() throws Exception { |
162 |
> |
CompletionService cs = new ExecutorCompletionService(cachedThreadPool); |
163 |
> |
ArithmeticException ex = new ArithmeticException(); |
164 |
> |
final int rounds = 2; |
165 |
> |
for (int i = rounds; i--> 0; ) { |
166 |
> |
cs.submit(new StringTask()); |
167 |
> |
cs.submit(callableThrowing(ex)); |
168 |
> |
cs.submit(runnableThrowing(ex), null); |
169 |
> |
} |
170 |
> |
int normalCompletions = 0; |
171 |
> |
int exceptionalCompletions = 0; |
172 |
> |
for (int i = 3 * rounds; i--> 0; ) { |
173 |
> |
try { |
174 |
> |
assertSame(TEST_STRING, cs.take().get()); |
175 |
> |
normalCompletions++; |
176 |
> |
} catch (ExecutionException expected) { |
177 |
> |
assertSame(ex, expected.getCause()); |
178 |
> |
exceptionalCompletions++; |
179 |
|
} |
198 |
– |
assertEquals(2 * 1, normalCompletions); |
199 |
– |
assertEquals(2 * 2, exceptionalCompletions); |
200 |
– |
assertNull(cs.poll()); |
180 |
|
} |
181 |
+ |
assertEquals(1 * rounds, normalCompletions); |
182 |
+ |
assertEquals(2 * rounds, exceptionalCompletions); |
183 |
+ |
assertNull(cs.poll()); |
184 |
|
} |
185 |
|
|
186 |
|
/** |