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 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; }
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"); }
private CallInfo myUniquelyNamedMethod() { return Tasks.resolving(supplier).as(CallInfo.class).immediately(true).get(); } });
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 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 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 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 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); } }
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 testTimeoutZero() { Maybe<String> result = Tasks.resolving(newSleepTask(Duration.TEN_SECONDS, "foo")).as(String.class).context(app).timeout(Duration.ZERO).getMaybe(); Assert.assertFalse(result.isPresent()); }
@Override public Maybe<?> call() throws Exception { return Tasks.resolving(dsl).as(type) .context(context) .description("Computing "+dsl) .immediately(true) .getMaybe(); } };
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 testGetImmediately() { MyImmediateAndDeferredSupplier supplier = new MyImmediateAndDeferredSupplier(); CallInfo callInfo = Tasks.resolving(supplier).as(CallInfo.class).context(app).immediately(true).get(); assertImmediateFakeTaskFromMethod(callInfo, "testGetImmediately"); }
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<String> call() throws Exception { return Tasks.resolving(t).as(String.class).timeout(Duration.ZERO).getMaybe(); }}) .getUnchecked();
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 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 testDefaultWhenSwallowError() { ValueResolver<String> result = Tasks.resolving(newThrowTask(Duration.ZERO)).as(String.class).context(app).swallowExceptions().defaultValue("foo"); assertMaybeIsAbsent(result); Assert.assertEquals(result.get(), "foo"); }