public void testUnsubmittedTaskWithExecutionContextExecutesAndTimesOut() { final Task<String> t = newSleepTask(Duration.ONE_MINUTE, "foo"); // Below, we call ValueResolver.getMaybe() in app's execution context. Therefore it will execute the task. // However, it will quickly timeout as the task will not have completed. Maybe<String> result = app.getExecutionContext() .submit(new Callable<Maybe<String> >() { @Override public Maybe<String> call() throws Exception { return Tasks.resolving(t).as(String.class).timeout(Duration.ZERO).getMaybe(); }}) .getUnchecked(); Assert.assertTrue(result.isAbsent(), "result="+result); Exception exception = Maybe.getException(result); Assert.assertTrue(exception.toString().contains("not completed when immediate completion requested"), "exception="+exception); }
public void testUnsubmittedTaskWhenNoExecutionContextFails() { Task<String> t = newSleepTask(Duration.ZERO, "foo"); // Below, we call ValueResolver.getMaybe() with no execution context. Therefore it will not execute the task. Maybe<String> result = Tasks.resolving(t).as(String.class).timeout(Duration.ZERO).getMaybe(); Assert.assertTrue(result.isAbsent(), "result="+result); Exception exception = Maybe.getException(result); Assert.assertTrue(exception.toString().contains("no execution context available"), "exception="+exception); }
public void testUnsubmittedTaskWithExecutionContextExecutesAndReturns() { final Task<String> t = newSleepTask(Duration.ZERO, "foo"); // Below, we call ValueResolver.getMaybe() in app's execution context. Therefore it will execute the task. Maybe<String> result = app.getExecutionContext() .submit(new Callable<Maybe<String> >() { @Override public Maybe<String> call() throws Exception { return Tasks.resolving(t).as(String.class).timeout(Asserts.DEFAULT_LONG_TIMEOUT).getMaybe(); }}) .getUnchecked(); Assert.assertEquals(result.get(), "foo"); }
public void testTimeoutZero() { Maybe<String> result = Tasks.resolving(newSleepTask(Duration.TEN_SECONDS, "foo")).as(String.class).context(app).timeout(Duration.ZERO).getMaybe(); Assert.assertFalse(result.isPresent()); }
public void testTimeoutBig() { Maybe<String> result = Tasks.resolving(newSleepTask(Duration.ZERO, "foo")).as(String.class).context(app).timeout(Duration.TEN_SECONDS).getMaybe(); Assert.assertEquals(result.get(), "foo"); }
public void testCompletedTaskReturnsResultImmediately() { Task<String> t = newSleepTask(Duration.ZERO, "foo"); app.getExecutionContext().submit(t).getUnchecked(); // Below, we call ValueResolver.getMaybe() from this thread, which has no execution context. // However, the task has already been submitted and we have waited for it to complete. // Therefore the ValueResolver can simply check for task.isDone() and return its result immediately. Maybe<String> result = Tasks.resolving(t).as(String.class).timeout(Duration.ZERO).getMaybe(); Assert.assertEquals(result.get(), "foo"); }