@Override public void runTask(Engine engine, Task<?> t) { engine.blockingRun(t); }
/** * Runs the given task. Task passed in as a parameter becomes a root on a new Plan. * All tasks created and started as a consequence of a root task will belong to that plan and will share a Trace. * <p> * This method blocks until Engine has a capacity to run the task. Engine's capacity is * specified by a {@value #MAX_CONCURRENT_PLANS} configuration property. Use * {@link EngineBuilder#setEngineProperty(String, Object)} to set this property. * For the sake of backwards compatibility default value for a {@value #MAX_CONCURRENT_PLANS} is * {@value #DEFUALT_MAX_CONCURRENT_PLANS} which essentially means "unbounded capacity". * * @param task the task to run */ public void blockingRun(final Task<?> task) { blockingRun(task, defaultPlanClass(task)); }
@Test public void testBlockingRunWithinCapacity() throws InterruptedException { final AtomicInteger counter = new AtomicInteger(0); Task<?>[] tasks = new Task<?>[10]; for (int i = 0; i < 10; i++) { tasks[i] = Task.action(counter::incrementAndGet); } for (int i = 0; i < 10; i++) { _engine.blockingRun(tasks[i], (i % 2 == 0) ? "evenPlan" : "oddPlan"); } assertTrue(awaitAll(tasks)); assertEquals(10, counter.get()); }
@Test public void testBlockingRunOverCapacity() throws InterruptedException { final CountDownLatch latch = new CountDownLatch(1); Task<?>[] tasks = new Task<?>[10]; for (int i = 0; i < 10; i++) { tasks[i] = Task.action(latch::await); } //within capacity for (int i = 0; i < 10; i++) { _engine.blockingRun(tasks[i], (i % 2 == 0) ? "evenPlan" : "oddPlan"); } final CountDownLatch blockedTaskLatch = new CountDownLatch(1); final CountDownLatch blockedThreadLatch = new CountDownLatch(1); new Thread(() -> { blockedThreadLatch.countDown(); _engine.blockingRun(Task.action(() -> { blockedTaskLatch.countDown(); }), "oddPlan"); }).start(); //first wait for a background thread to reach _engine.run() assertTrue(blockedThreadLatch.await(5, TimeUnit.SECONDS)); //sleep for 200ms to make sure background thread executed _engine.run() Thread.sleep(200); //verify background tasks didn't run assertEquals(1L, blockedTaskLatch.getCount()); //release tasks latch.countDown(); //background thread should be unblocked and background task should eventually run assertTrue(blockedTaskLatch.await(5, TimeUnit.SECONDS)); }
@Test public void testBlockingRunOverCapacity() throws InterruptedException { final CountDownLatch latch = new CountDownLatch(1); Task<?>[] tasks = new Task<?>[10]; for (int i = 0; i < 10; i++) { tasks[i] = Task.action(latch::await); } //within capacity for (int i = 0; i < 10; i++) { _engine.blockingRun(tasks[i]); } final CountDownLatch blockedTaskLatch = new CountDownLatch(1); final CountDownLatch blockedThreadLatch = new CountDownLatch(1); new Thread(() -> { blockedThreadLatch.countDown(); _engine.blockingRun(Task.action(() -> { blockedTaskLatch.countDown(); })); }).start(); //first wait for a background thread to reach _engine.run() assertTrue(blockedThreadLatch.await(5, TimeUnit.SECONDS)); //sleep for 200ms to make sure background thread executed _engine.run() Thread.sleep(200); //verify background tasks didn't run assertEquals(1L, blockedTaskLatch.getCount()); //release tasks latch.countDown(); //background thread should be unblocked and background task should eventually run assertTrue(blockedTaskLatch.await(5, TimeUnit.SECONDS)); }
@Test public void testBlockingRunWithinCapacity() throws InterruptedException { final AtomicInteger counter = new AtomicInteger(0); Task<?>[] tasks = new Task<?>[10]; for (int i = 0; i < 10; i++) { tasks[i] = Task.action(counter::incrementAndGet); } for (int i = 0; i < 10; i++) { _engine.blockingRun(tasks[i]); } assertTrue(awaitAll(tasks)); assertEquals(10, counter.get()); }
/** * Runs the given task. Task passed in as a parameter becomes a root on a new Plan. * All tasks created and started as a consequence of a root task will belong to that plan and will share a Trace. * <p> * This method blocks until Engine has a capacity to run the task. Engine's capacity is * specified by a {@value #MAX_CONCURRENT_PLANS} configuration property. Use * {@link EngineBuilder#setEngineProperty(String, Object)} to set this property. * For the sake of backwards compatibility default value for a {@value #MAX_CONCURRENT_PLANS} is * {@value #DEFUALT_MAX_CONCURRENT_PLANS} which essentially means "unbounded capacity". * * @param task the task to run */ public void blockingRun(final Task<?> task) { blockingRun(task, defaultPlanClass(task)); }