Source:
<code>public void doTestCancellationModeAndSubmitted( boolean isSubtaskTransient, Object cancellationMode, boolean expectedTaskInterrupted, boolean expectedSubtaskCancelled ) throws Exception { tearDown(); setUp(); final Task<String> t1 = sayTask("1-wait", Duration.minutes(10), "1-done"); if (isSubtaskTransient) { BrooklynTaskTags.addTagDynamically(t1, BrooklynTaskTags.TRANSIENT_TASK_TAG); } final Task<List<?>> t = Tasks.parallel( submitting(t1), sayTask("2-wait", Duration.minutes(10), "2-done")); ec.submit(t); waitForMessages(Predicates.compose(MathPredicates.greaterThanOrEqual(2), CollectionFunctionals.sizeFunction()), TIMEOUT); Asserts.assertEquals(MutableSet.copyOf(messages), MutableSet.of("1-wait", "2-wait")); <START> Time.sleep(Duration.millis(400)); <END> if (cancellationMode==null) { ((TaskInternal<?>)t).cancel(); } else if (cancellationMode instanceof Boolean) { t.cancel((Boolean)cancellationMode); } else if (cancellationMode instanceof TaskCancellationMode) { ((TaskInternal<?>)t).cancel((TaskCancellationMode)cancellationMode); } Assert.assertEquals(t.isDone(), true); Assert.assertEquals(t.isCancelled(), true); if (expectedTaskInterrupted) { Asserts.eventually(new Supplier<Number>() { @Override public Number get() { return t.getEndTimeUtc(); }}, MathPredicates.<Number>greaterThanOrEqual(0)); } else { Assert.assertTrue(t.getEndTimeUtc() < 0, "Wrong end time: "+t.getEndTimeUtc()); } if (expectedSubtaskCancelled) { Asserts.eventually(Suppliers.ofInstance(t1), TaskPredicates.isDone()); Assert.assertTrue(t1.isCancelled()); Asserts.eventually(new Supplier<Number>() { @Override public Number get() { return t1.getEndTimeUtc(); }}, MathPredicates.<Number>greaterThanOrEqual(0)); } else { Time.sleep(Duration.millis(5)); Assert.assertFalse(t1.isCancelled()); Assert.assertFalse(t1.isDone()); } }</code><technical_language>do need sleep? I thought assert for messages for (that task executing)</technical_language>

Target:
public void doTestCancellationModeAndSubmitted( boolean isSubtaskTransient, Object cancellationMode, boolean expectedTaskInterrupted, boolean expectedSubtaskCancelled ) throws Exception { tearDown(); setUp(); final Task<String> t1 = sayTask("1-wait", Duration.minutes(10), "1-done"); if (isSubtaskTransient) { BrooklynTaskTags.addTagDynamically(t1, BrooklynTaskTags.TRANSIENT_TASK_TAG); } final Task<List<?>> t = Tasks.parallel( submitting(t1), sayTask("2-wait", Duration.minutes(10), "2-done")); ec.submit(t); waitForMessages(Predicates.compose(MathPredicates.greaterThanOrEqual(2), CollectionFunctionals.sizeFunction()), TIMEOUT); Asserts.assertEquals(MutableSet.copyOf(messages), MutableSet.of("1-wait", "2-wait")); if (cancellationMode==null) { ((TaskInternal<?>)t).cancel(); } else if (cancellationMode instanceof Boolean) { t.cancel((Boolean)cancellationMode); } else if (cancellationMode instanceof TaskCancellationMode) { ((TaskInternal<?>)t).cancel((TaskCancellationMode)cancellationMode); } else { throw new IllegalStateException("Invalid cancellationMode: "+cancellationMode); } Assert.assertEquals(t.isDone(), true); Assert.assertEquals(t.isCancelled(), true); if (expectedTaskInterrupted) { Asserts.eventually(new Supplier<Number>() { @Override public Number get() { return t.getEndTimeUtc(); }}, MathPredicates.<Number>greaterThanOrEqual(0)); } else { Assert.assertTrue(t.getEndTimeUtc() < 0, "Wrong end time: "+t.getEndTimeUtc()); } if (expectedSubtaskCancelled) { Asserts.eventually(Suppliers.ofInstance(t1), TaskPredicates.isDone()); Assert.assertTrue(t1.isCancelled()); Asserts.eventually(new Supplier<Number>() { @Override public Number get() { return t1.getEndTimeUtc(); }}, MathPredicates.<Number>greaterThanOrEqual(0)); } else { Time.sleep(Duration.millis(5)); Assert.assertFalse(t1.isCancelled()); Assert.assertFalse(t1.isDone()); } }