|
16 | 16 |
|
17 | 17 | package com.linkedin.parseq;
|
18 | 18 |
|
19 |
| -import com.linkedin.parseq.promise.Promise; |
20 |
| -import com.linkedin.parseq.promise.PromiseListener; |
21 |
| -import com.linkedin.parseq.promise.Promises; |
22 |
| -import org.testng.annotations.Test; |
| 19 | +import static com.linkedin.parseq.TestUtil.value; |
| 20 | +import static org.testng.AssertJUnit.assertEquals; |
| 21 | +import static org.testng.AssertJUnit.assertFalse; |
| 22 | +import static org.testng.AssertJUnit.assertTrue; |
| 23 | +import static org.testng.AssertJUnit.fail; |
23 | 24 |
|
| 25 | +import java.io.IOException; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
24 | 28 | import java.util.concurrent.Callable;
|
| 29 | +import java.util.concurrent.Executors; |
| 30 | +import java.util.concurrent.ScheduledExecutorService; |
25 | 31 | import java.util.concurrent.TimeUnit;
|
26 | 32 | import java.util.concurrent.TimeoutException;
|
27 | 33 | import java.util.concurrent.atomic.AtomicReference;
|
28 | 34 |
|
29 |
| -import static com.linkedin.parseq.TestUtil.value; |
30 |
| -import static org.testng.AssertJUnit.assertEquals; |
31 |
| -import static org.testng.AssertJUnit.assertFalse; |
32 |
| -import static org.testng.AssertJUnit.assertTrue; |
33 |
| -import static org.testng.AssertJUnit.fail; |
| 35 | +import org.testng.annotations.Test; |
| 36 | + |
| 37 | +import com.linkedin.parseq.promise.Promise; |
| 38 | +import com.linkedin.parseq.promise.PromiseListener; |
| 39 | +import com.linkedin.parseq.promise.Promises; |
| 40 | +import com.linkedin.parseq.promise.SettablePromise; |
34 | 41 |
|
35 | 42 | /**
|
36 | 43 | * @author Chris Pettitt ([email protected])
|
@@ -171,6 +178,52 @@ public String call() throws Exception
|
171 | 178 | assertEquals(error, timeoutTask.getError());
|
172 | 179 | }
|
173 | 180 |
|
| 181 | + /** |
| 182 | + * Test scenario in which there are many TimeoutWithErrorTasks scheduled for execution |
| 183 | + * e.g. by using Tasks.par(). |
| 184 | + */ |
| 185 | + @Test |
| 186 | + public void testManyTimeoutTaskWithoutTimeoutOnAQueeu() throws InterruptedException, IOException |
| 187 | + { |
| 188 | + final String value = "value"; |
| 189 | + final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); |
| 190 | + |
| 191 | + List<Task<String>> tasks = new ArrayList<Task<String>>(); |
| 192 | + for (int i = 0; i < 50; i++) { |
| 193 | + |
| 194 | + // Task which simulates doing something for 0.5ms and setting response |
| 195 | + // asynchronously after 5ms. |
| 196 | + Task<String> t = new BaseTask<String>("test") { |
| 197 | + @Override |
| 198 | + protected Promise<? extends String> run(Context context) throws Throwable { |
| 199 | + final SettablePromise<String> result = Promises.settable(); |
| 200 | + Thread.sleep(0, 500000); |
| 201 | + scheduler.schedule(new Runnable() { |
| 202 | + @Override |
| 203 | + public void run() { |
| 204 | + result.done(value); |
| 205 | + } |
| 206 | + }, 5, TimeUnit.MILLISECONDS); |
| 207 | + return result; |
| 208 | + } |
| 209 | + }; |
| 210 | + // add 50ms timeout for the task |
| 211 | + tasks.add(Tasks.timeoutWithError(50, TimeUnit.MILLISECONDS, t)); |
| 212 | + } |
| 213 | + |
| 214 | + // final task runs all the tasks in parallel |
| 215 | + final Task<?> timeoutTask = Tasks.par(tasks); |
| 216 | + |
| 217 | + getEngine().run(timeoutTask); |
| 218 | + |
| 219 | + assertTrue(timeoutTask.await(5, TimeUnit.SECONDS)); |
| 220 | + |
| 221 | + scheduler.shutdown(); |
| 222 | + |
| 223 | + //tasks should not time out |
| 224 | + assertEquals(false, timeoutTask.isFailed()); |
| 225 | + } |
| 226 | + |
174 | 227 | @Test
|
175 | 228 | public void testSetPriorityBelowMinValue()
|
176 | 229 | {
|
|
0 commit comments