/** @see #resolveValue(Object, Class, ExecutionContext, String) */ public static <T> T resolveValue(Object v, Class<T> type, @Nullable ExecutionContext exec) throws ExecutionException, InterruptedException { return new ValueResolver<T>(v, type).context(exec).get(); }
/** 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(); }
public static Exception assertThrowsOnGet(ValueResolver<?> result) { result = result.clone(); try { result.get(); Assert.fail("should have thrown"); return null; } catch (Exception e) { return e; } }
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(); }
private Map<Sensor<?>, Sensor<?>> resolveSensorMappings(Map<?,?> mapping) { if (mapping == null) { return MutableMap.of(); } Map<Sensor<?>, Sensor<?>> result = MutableMap.of(); for (Map.Entry<?,?> entry : mapping.entrySet()) { Object keyO = entry.getKey(); Object valueO = entry.getValue(); Sensor<?> key = Tasks.resolving(keyO).as(Sensor.class).immediately(true).context(producer).get(); Sensor<?> value = Tasks.resolving(valueO).as(Sensor.class).immediately(true).context(producer).get(); result.put(key, value); } return result; }
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; }
/** * Resolves the given object, blocking on futures and coercing it to the given type. If the object is a * map or iterable (or a list of map of maps, etc, etc) then walks these maps/iterables to convert all of * their values to the given type. For example, the following will return a list containing a map with "1"="true": * * {@code Object result = resolveDeepValue(ImmutableList.of(ImmutableMap.of(1, true)), String.class, exec)} * * To perform a deep conversion of futures contained within Iterables or Maps without coercion of each element, * the type should normally be Object, not the type of the collection. This differs from * {@link #resolveValue(Object, Class, ExecutionContext, String)} which will accept Map and Iterable * as the required type. */ public static <T> T resolveDeepValue(Object v, Class<T> type, ExecutionContext exec, String contextMessage) throws ExecutionException, InterruptedException { return new ValueResolver<T>(v, type).context(exec).deep(true).description(contextMessage).get(); }
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"); }
private CallInfo myUniquelyNamedMethod() { return Tasks.resolving(supplier).as(CallInfo.class).immediately(true).get(); } });
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 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 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 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 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 testGetImmediately() { MyImmediateAndDeferredSupplier supplier = new MyImmediateAndDeferredSupplier(); CallInfo callInfo = Tasks.resolving(supplier).as(CallInfo.class).context(app).immediately(true).get(); assertImmediateFakeTaskFromMethod(callInfo, "testGetImmediately"); }
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 testNonRecursiveImmediateFailsOnNonObjectType() throws Exception { try { Tasks.resolving(new WrappingImmediateAndDeferredSupplier(new FailingImmediateAndDeferredSupplier())) .as(FailingImmediateAndDeferredSupplier.class) .context(app) .immediately(true) .recursive(false) .get(); Asserts.shouldHaveFailedPreviously("recursive(true) accepts only as(Object.class)"); } catch (IllegalStateException e) { Asserts.expectedFailureContains(e, "must be Object"); } }
public void testNonRecursiveBlockingFailsOnNonObjectType() throws Exception { try { Tasks.resolving(new WrappingImmediateAndDeferredSupplier(new FailingImmediateAndDeferredSupplier())) .as(FailingImmediateAndDeferredSupplier.class) .context(app) .immediately(false) .recursive(false) .get(); Asserts.shouldHaveFailedPreviously("recursive(true) accepts only as(Object.class)"); } catch (IllegalStateException e) { Asserts.expectedFailureContains(e, "must be Object"); } }
@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"); }