private static Object getImmediateValue(Object value, @Nullable Entity context, @Nullable Boolean immediately, @Nullable Duration timeout) { return Tasks.resolving(value) .as(Object.class) .defaultValue(UNRESOLVED) .timeout(timeout) .immediately(immediately == null ? false : immediately.booleanValue()) .context(context) .swallowExceptions() .get(); }
<S> ValueResolver<S> cloneReplacingValueAndType(Object newValue, Class<S> superType) { // superType expected to be either type or Object.class if (!superType.isAssignableFrom(type)) { throw new IllegalStateException("superType must be assignable from " + type); } ValueResolver<S> result = new ValueResolver<S>(newValue, superType) .context(exec).description(description) .embedResolutionInTask(embedResolutionInTask) .deep(forceDeep) .timeout(timeout) .immediately(immediately) .recursive(recursive); if (returnDefaultOnGet) { if (!superType.isInstance(defaultValue)) { throw new IllegalStateException("Existing default value " + defaultValue + " not compatible with new type " + superType); } @SuppressWarnings("unchecked") S typedDefaultValue = (S)defaultValue; result.defaultValue(typedDefaultValue); } if (swallowExceptions) result.swallowExceptions(); return result; }
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); }
@Override public Maybe<String> call() throws Exception { return Tasks.resolving(t).as(String.class).timeout(Duration.ZERO).getMaybe(); }}) .getUnchecked();
@Override public Maybe<String> call() throws Exception { return Tasks.resolving(t).as(String.class).timeout(Asserts.DEFAULT_LONG_TIMEOUT).getMaybe(); }}) .getUnchecked();
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"); }
static Maybe<?> execDslEventually(BrooklynDslDeferredSupplier<?> dsl, Class<?> type, Entity context, Duration timeout) { return Tasks.resolving(dsl).as(type) .context(context) .description("Computing "+dsl) .timeout(timeout) .getMaybe(); } }
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 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"); }
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"); }
.as(Object.class) .defaultValue("UNRESOLVED") .timeout(Duration.millis(100)) .context(entity) .swallowExceptions()