private void assertImmediateFakeTaskFromMethod(CallInfo callInfo, String method) { // previously task was null, but now there is a "fake task" assertNotNull(callInfo.task); Assert.assertFalse(callInfo.task.isSubmitted()); assertContainsCallingMethod(callInfo.stackTrace, method); }
@Test public void testErrorsResolvingPropagatesOrSwallowedAllCorrectly() throws Exception { app.config().set(TestEntity.CONF_OBJECT, ValueResolverTest.newThrowTask(Duration.ZERO)); Task<Object> t = Tasks.builder().body(Functionals.callable(EntityFunctions.config(TestEntity.CONF_OBJECT), app)).build(); ValueResolver<Object> v = Tasks.resolving(t).as(Object.class).context(app); ValueResolverTest.assertThrowsOnGetMaybe(v); ValueResolverTest.assertThrowsOnGet(v); v.swallowExceptions(); ValueResolverTest.assertMaybeIsAbsent(v); ValueResolverTest.assertThrowsOnGet(v); v.defaultValue("foo"); ValueResolverTest.assertMaybeIsAbsent(v); assertEquals(v.clone().get(), "foo"); assertResolvesValue(v, Object.class, "foo"); }
private void assertRealTaskNotFromMethod(CallInfo callInfo, String method) { assertNotNull(callInfo.task); Assert.assertTrue(callInfo.task.isSubmitted()); assertNotContainsCallingMethod(callInfo.stackTrace, method); }
public void testDontSwallowError() { ValueResolver<String> result = Tasks.resolving(newThrowTask(Duration.ZERO)).as(String.class).context(app); assertThrowsOnGetMaybe(result); assertThrowsOnGet(result); }
public void testSwallowError() { ValueResolver<String> result = Tasks.resolving(newThrowTask(Duration.ZERO)).as(String.class).context(app).swallowExceptions(); assertMaybeIsAbsent(result); assertThrowsOnGet(result); }
public void testDefaultBeforeDelayAndError() { ValueResolver<String> result = Tasks.resolving(newThrowTask(Duration.TEN_SECONDS)).as(String.class).context(app).timeout(Duration.ZERO).defaultValue("foo"); assertMaybeIsAbsent(result); Assert.assertEquals(result.get(), "foo"); }
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 testGetImmediatelyInTask() throws Exception { final MyImmediateAndDeferredSupplier supplier = new MyImmediateAndDeferredSupplier(); Task<CallInfo> task = app.getExecutionContext().submit(new Callable<CallInfo>() { @Override public CallInfo call() { return myUniquelyNamedMethod(); } private CallInfo myUniquelyNamedMethod() { return Tasks.resolving(supplier).as(CallInfo.class).immediately(true).get(); } }); CallInfo callInfo = task.get(); assertImmediateFakeTaskFromMethod(callInfo, "myUniquelyNamedMethod"); }
public void testImmediateSupplierWithTimeoutUsesBlocking() { MyImmediateAndDeferredSupplier supplier = new MyImmediateAndDeferredSupplier(); CallInfo callInfo = Tasks.resolving(supplier).as(CallInfo.class).context(app).timeout(Asserts.DEFAULT_LONG_TIMEOUT).get(); assertRealTaskNotFromMethod(callInfo, "testImmediateSupplierWithTimeoutUsesBlocking"); }
public void testDefaultWhenSwallowError() { ValueResolver<String> result = Tasks.resolving(newThrowTask(Duration.ZERO)).as(String.class).context(app).swallowExceptions().defaultValue("foo"); assertMaybeIsAbsent(result); Assert.assertEquals(result.get(), "foo"); }
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 testGetImmediately() { MyImmediateAndDeferredSupplier supplier = new MyImmediateAndDeferredSupplier(); CallInfo callInfo = Tasks.resolving(supplier).as(CallInfo.class).context(app).immediately(true).get(); assertImmediateFakeTaskFromMethod(callInfo, "testGetImmediately"); }
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 testGetImmediatelyFallsBackToDeferredCallInTaskOnUnsupported() throws Exception { final MyImmediateAndDeferredSupplier supplier = new MyImmediateAndDeferredSupplier(new ImmediateSupplier.ImmediateUnsupportedException("Simulate immediate unsupported")); CallInfo callInfo = Tasks.resolving(supplier).as(CallInfo.class).context(app).immediately(true).get(); assertNotNull(callInfo.task); assertEquals(BrooklynTaskTags.getContextEntity(callInfo.task), app); assertNotContainsCallingMethod(callInfo.stackTrace, "testGetImmediatelyFallsBackToDeferredCallInTask"); }
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"); }