78 |
|
/** |
79 |
|
* A taken submitted task is completed |
80 |
|
*/ |
81 |
< |
public void testTake() |
82 |
< |
throws InterruptedException, ExecutionException { |
81 |
> |
public void testTake() throws Exception { |
82 |
|
CompletionService cs = new ExecutorCompletionService(cachedThreadPool); |
83 |
|
cs.submit(new StringTask()); |
84 |
|
Future f = cs.take(); |
99 |
|
/** |
100 |
|
* poll returns non-null when the returned task is completed |
101 |
|
*/ |
102 |
< |
public void testPoll1() |
104 |
< |
throws InterruptedException, ExecutionException { |
102 |
> |
public void testPoll1() throws Exception { |
103 |
|
CompletionService cs = new ExecutorCompletionService(cachedThreadPool); |
104 |
|
assertNull(cs.poll()); |
105 |
|
cs.submit(new StringTask()); |
118 |
|
/** |
119 |
|
* timed poll returns non-null when the returned task is completed |
120 |
|
*/ |
121 |
< |
public void testPoll2() |
124 |
< |
throws InterruptedException, ExecutionException { |
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 = cs.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() |
144 |
< |
throws InterruptedException, ExecutionException { |
141 |
> |
public void testPollReturnsNullBeforeCompletion() throws Exception { |
142 |
|
CompletionService cs = new ExecutorCompletionService(cachedThreadPool); |
143 |
|
final CountDownLatch proceed = new CountDownLatch(1); |
144 |
|
cs.submit(new Callable() { public String call() throws Exception { |
158 |
|
/** |
159 |
|
* successful and failed tasks are both returned |
160 |
|
*/ |
161 |
< |
public void testTaskAssortment() |
165 |
< |
throws InterruptedException, ExecutionException { |
161 |
> |
public void testTaskAssortment() throws Exception { |
162 |
|
CompletionService cs = new ExecutorCompletionService(cachedThreadPool); |
163 |
|
ArithmeticException ex = new ArithmeticException(); |
164 |
< |
for (int i = 0; i < 2; i++) { |
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 = 0; i < 3 * 2; i++) { |
172 |
> |
for (int i = 3 * rounds; i--> 0; ) { |
173 |
|
try { |
174 |
< |
if (cs.take().get() == TEST_STRING) |
175 |
< |
normalCompletions++; |
176 |
< |
} |
177 |
< |
catch (ExecutionException expected) { |
181 |
< |
assertTrue(expected.getCause() instanceof ArithmeticException); |
174 |
> |
assertSame(TEST_STRING, cs.take().get()); |
175 |
> |
normalCompletions++; |
176 |
> |
} catch (ExecutionException expected) { |
177 |
> |
assertSame(ex, expected.getCause()); |
178 |
|
exceptionalCompletions++; |
179 |
|
} |
180 |
|
} |
181 |
< |
assertEquals(2 * 1, normalCompletions); |
182 |
< |
assertEquals(2 * 2, exceptionalCompletions); |
181 |
> |
assertEquals(1 * rounds, normalCompletions); |
182 |
> |
assertEquals(2 * rounds, exceptionalCompletions); |
183 |
|
assertNull(cs.poll()); |
184 |
|
} |
185 |
|
|