Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Unified Diff: pkg/scheduled_test/lib/src/task.dart

Issue 12288061: Support nested tasks in scheduled_test. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/scheduled_test/lib/src/task.dart
diff --git a/pkg/scheduled_test/lib/src/task.dart b/pkg/scheduled_test/lib/src/task.dart
index 558a9cd6f065584177e820a9ab7bbc0dde29bfc8..c20f5e9f622ae03c5e4dc21b3fcd40d6890d8a23 100644
--- a/pkg/scheduled_test/lib/src/task.dart
+++ b/pkg/scheduled_test/lib/src/task.dart
@@ -5,7 +5,9 @@
library task;
import 'dart:async';
+import 'dart:collection';
+import 'future_group.dart';
import 'schedule.dart';
import 'utils.dart';
@@ -16,9 +18,23 @@ class Task {
/// The queue to which this [Task] belongs.
final TaskQueue queue;
+ // TODO(nweiz): make this a read-only view when issue 8321 is fixed.
+ /// Child tasks that have been spawned while running this task. This will be
+ /// empty if this task is itself a child task.
Bob Nystrom 2013/02/19 23:15:04 Does this mean child tasks cannot have their own c
nweiz 2013/02/20 00:23:12 Correct.
Bob Nystrom 2013/02/20 17:30:25 Let's make sure that's documented somewhere. When
nweiz 2013/02/20 23:54:58 Done.
+ final children = new Queue<Task>();
+
+ /// A [FutureGroup] that will complete once all current child tasks are
+ /// finished running. This will be null if no child tasks are currently
+ /// running.
+ FutureGroup _childGroup;
+
/// A description of this task. Used for debugging. May be `null`.
final String description;
+ /// The parent task, if this is a nested task that was started while another
+ /// task was running. This will be `null` for top-level tasks.
+ final Task parent;
+
/// The body of the task.
TaskBody fn;
@@ -31,10 +47,19 @@ class Task {
Future get result => _resultCompleter.future;
final _resultCompleter = new Completer();
- Task(fn(), this.queue, this.description) {
- _id = this.queue.contents.length;
+ Task(fn(), String description, TaskQueue queue)
+ : this._(fn, description, queue, null, queue.contents.length);
+
+ Task._child(fn(), String description, Task parent)
+ : this._(fn, description, parent.queue, parent, parent.children.length);
Bob Nystrom 2013/02/19 23:15:04 I think this means that ids are not unique. If top
nweiz 2013/02/20 00:23:12 Yes.
+
+ Task._(fn(), this.description, this.queue, this.parent, this._id) {
this.fn = () {
- var future = new Future.immediate(null).then((_) => fn());
+ var future = new Future.immediate(null).then((_) => fn())
+ .whenComplete(() {
+ if (_childGroup == null || _childGroup.completed) return;
+ return _childGroup.future;
+ });
chainToCompleter(future, _resultCompleter);
return future;
};
@@ -44,6 +69,22 @@ class Task {
result.catchError((_) {});
}
+ /// Run [fn] as a child of this task. Returns a Future that will complete with
+ /// the result of the child task. This task will not complete until [fn] has
+ /// finished.
+ Future runChild(fn(), String description) {
+ var task = new Task._child(fn, description, this);
+ children.add(task);
+ if (_childGroup == null || _childGroup.completed) {
+ _childGroup = new FutureGroup();
+ }
+ // Ignore errors in the FutureGroup; they'll get picked up via wrapFuture,
+ // and we don't want them to short-circuit the other Futures.
+ _childGroup.add(task.result.catchError((_) {}));
+ task.fn();
+ return task.result;
+ }
+
String toString() => description == null ? "#$_id" : description;
/// Returns a detailed representation of [queue] with this task highlighted.

Powered by Google App Engine
This is Rietveld 408576698