/** as {@link #context(ExecutionContext)} for use from an entity */ public ValueResolver<T> context(Entity entity) { return context(entity!=null ? ((EntityInternal)entity).getExecutionContext() : null); }
/** attempt to resolve the given value as the given type, waiting on futures, submitting if necessary, * and coercing as allowed by TypeCoercions; * contextMessage (optional) will be displayed in status reports while it waits (e.g. the name of the config key being looked up). * if no execution context supplied (null) this method will throw an exception if the object is an unsubmitted task */ public static <T> T resolveValue(Object v, Class<T> type, @Nullable ExecutionContext exec, String contextMessage) throws ExecutionException, InterruptedException { return new ValueResolver<T>(v, type).context(exec).description(contextMessage).get(); }
private Maybe<Entity> resolveTarget() { return Tasks.resolving(target, Entity.class) .context(entity()) .getMaybe(); }
private List<Sensor<?>> resolveSensorCollection(Iterable<?> sensors) { if (sensors == null) { return MutableList.of(); } List<Sensor<?>> result = MutableList.of(); for (Object sensorO : sensors) { Sensor<?> sensor = Tasks.resolving(sensorO).as(Sensor.class).immediately(true).context(producer).get(); result.add(sensor); } return result; }
private static ValueResolverIterator<Boolean> resolveLatchIterator(EntityInternal entity, Object val, ConfigKey<Boolean> key) { return Tasks.resolving(val, Boolean.class) .context(entity.getExecutionContext()) .description("config " + key.getName()) .iterator(); }
public void testTaskFactoryGet() { TaskFactory<TaskAdaptable<String>> taskFactory = new TaskFactory<TaskAdaptable<String>>() { @Override public TaskAdaptable<String> newTask() { return new BasicTask<>(Callables.returning("myval")); } }; String result = Tasks.resolving(taskFactory).as(String.class).context(app).get(); assertEquals(result, "myval"); }
public void testTaskFactoryGetImmediately() { TaskFactory<TaskAdaptable<String>> taskFactory = new TaskFactory<TaskAdaptable<String>>() { @Override public TaskAdaptable<String> newTask() { return new BasicTask<>(Callables.returning("myval")); } }; String result = Tasks.resolving(taskFactory).as(String.class).context(app).immediately(true).get(); assertEquals(result, "myval"); }
public void testNonRecursiveBlocking() throws Exception { Object result = Tasks.resolving(new WrappingImmediateAndDeferredSupplier(new FailingImmediateAndDeferredSupplier())) .as(Object.class) .context(app) .immediately(false) .recursive(false) .get(); assertEquals(result.getClass(), FailingImmediateAndDeferredSupplier.class); }
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 testNonRecursiveImmediately() throws Exception { Object result = Tasks.resolving(new WrappingImmediateAndDeferredSupplier(new FailingImmediateAndDeferredSupplier())) .as(Object.class) .context(app) .immediately(true) .recursive(false) .get(); assertEquals(result.getClass(), FailingImmediateAndDeferredSupplier.class); }
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 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"); }
@Override public Maybe<?> call() throws Exception { return Tasks.resolving(dsl).as(type) .context(context) .description("Computing "+dsl) .immediately(true) .getMaybe(); } };
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 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 testDontSwallowError() { ValueResolver<String> result = Tasks.resolving(newThrowTask(Duration.ZERO)).as(String.class).context(app); assertThrowsOnGetMaybe(result); assertThrowsOnGet(result); }
public void testGetImmediately() { MyImmediateAndDeferredSupplier supplier = new MyImmediateAndDeferredSupplier(); CallInfo callInfo = Tasks.resolving(supplier).as(CallInfo.class).context(app).immediately(true).get(); assertImmediateFakeTaskFromMethod(callInfo, "testGetImmediately"); }
private Maybe<Entity> getResolvedConfigInTask(final Entity entity, final ConfigKey<Entity> key) { return Tasks.resolving(Tasks.<Entity>builder().body( Functionals.callable(Suppliers.compose(EntityFunctions.config(key), Suppliers.ofInstance(entity))) ).build()) .as(Entity.class) .context(entity).embedResolutionInTask(true) .getMaybe(); }
public void testGetImmediatelyDoesntFallBackToDeferredCallOnNotAvailable() throws Exception { final MyImmediateAndDeferredSupplier supplier = new MyImmediateAndDeferredSupplier(new ImmediateSupplier.ImmediateValueNotAvailableException()); Maybe<CallInfo> callInfo = Tasks.resolving(supplier).as(CallInfo.class).context(app).immediately(true).getMaybe(); Asserts.assertNotPresent(callInfo); try { callInfo.get(); Asserts.shouldHaveFailedPreviously("resolution should have failed now the ImmediateSupplier is not expected to fallback to other evaluation forms; instead got "+callInfo); } catch (Exception e) { Asserts.expectedFailureOfType(e, ImmediateValueNotAvailableException.class); } }