/** returns the task, its children, and all its children, and so on; * @param root task whose descendants should be iterated * @param parentFirst whether to put parents before children or after */ public static Iterable<Task<?>> descendants(Task<?> root, final boolean parentFirst) { Iterable<Task<?>> descs = Iterables.concat(Iterables.transform(Tasks.children(root), new Function<Task<?>,Iterable<Task<?>>>() { @Override public Iterable<Task<?>> apply(Task<?> input) { return descendants(input, parentFirst); } })); if (parentFirst) return Iterables.concat(Collections.singleton(root), descs); else return Iterables.concat(descs, Collections.singleton(root)); }
private Task<?> findSshLaunchChild(Task<?> t) { Iterable<Task<?>> children = Tasks.children(t); for (Task<?> c : children) { if (c.getDisplayName().startsWith(SSH_LAUNCH_TASK_PREFIX)) { return c; } } for (Task<?> c : children) { Task<?> launchTask = findSshLaunchChild(c); if (launchTask != null) { return launchTask; } } return null; } }
public void doTestReleaseEvenIfErrorDuringStop(final Class<? extends SimulatedDriver> driver) throws Exception { MyService entity = app.addChild(EntitySpec.create(MyServiceWithCustomDriver.class) .configure(MyServiceWithCustomDriver.DRIVER_CLASS, driver)); entity.start(ImmutableList.of(loc)); Task<Void> t = entity.invoke(Startable.STOP, ImmutableMap.<String, Object>of()); t.blockUntilEnded(); assertFalse(t.isError(), "Expected parent to succeed, not fail with " + Tasks.getError(t)); Iterator<Task<?>> failures; failures = Tasks.failed(Tasks.descendants(t, true)).iterator(); Assert.assertTrue(failures.hasNext(), "Expected error in descendants"); Optional<Task<?>> stopping = Iterables.tryFind(Tasks.children(t), TaskPredicates.displayNameEqualTo("stopping")); Assert.assertTrue(stopping.isPresent(), "Could not find stopping task"); failures = Tasks.failed(Tasks.children(stopping.get())).iterator(); Assert.assertTrue(failures.hasNext(), "Expected error in child"); Throwable e = Tasks.getError(failures.next()); if (e == null || !e.toString().contains("Simulating stop error")) Assert.fail("Wrong error", e); Assert.assertEquals(loc.getAvailable(), ImmutableSet.of(machine), "Expected location to be available again"); Entities.unmanage(entity); }
Iterable<Task<?>> failed = Tasks.failed(Tasks.children(Tasks.current())); boolean noFailed = Iterables.isEmpty(failed); boolean severalFailed = Iterables.size(failed) > 1;