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

Side by Side 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: Code review changes 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library task; 5 library task;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:collection';
8 9
10 import 'future_group.dart';
9 import 'schedule.dart'; 11 import 'schedule.dart';
10 import 'utils.dart'; 12 import 'utils.dart';
11 13
12 typedef Future TaskBody(); 14 typedef Future TaskBody();
13 15
14 /// A single task to be run as part of a [TaskQueue]. 16 /// A single task to be run as part of a [TaskQueue].
17 ///
18 /// There are two levels of tasks. **Top-level tasks** are created by calling
19 /// [TaskQueue.schedule] before the queue in question is running. They're run in
20 /// sequence as part of that [TaskQueue]. **Nested tasks** are created by
21 /// calling [TaskQueue.schedule] once the queue is already running, and are run
22 /// in parallel as part of a top-level task.
15 class Task { 23 class Task {
16 /// The queue to which this [Task] belongs. 24 /// The queue to which this [Task] belongs.
17 final TaskQueue queue; 25 final TaskQueue queue;
18 26
27 // TODO(nweiz): make this a read-only view when issue 8321 is fixed.
28 /// Child tasks that have been spawned while running this task. This will be
29 /// empty if this task is a nested task.
30 final children = new Queue<Task>();
31
32 /// A [FutureGroup] that will complete once all current child tasks are
33 /// finished running. This will be null if no child tasks are currently
34 /// running.
35 FutureGroup _childGroup;
36
19 /// A description of this task. Used for debugging. May be `null`. 37 /// A description of this task. Used for debugging. May be `null`.
20 final String description; 38 final String description;
21 39
40 /// The parent task, if this is a nested task that was started while another
41 /// task was running. This will be `null` for top-level tasks.
42 final Task parent;
43
22 /// The body of the task. 44 /// The body of the task.
23 TaskBody fn; 45 TaskBody fn;
24 46
25 /// The identifier of the task. This is unique within [queue]. It's used for 47 /// The identifier of the task. For top-level tasks, this is the index of the
26 /// debugging when [description] isn't provided. 48 /// task within [queue]; for nested tasks, this is the index within
49 /// [parent.children]. It's used for debugging when [description] isn't
50 /// provided.
27 int _id; 51 int _id;
28 52
29 /// A Future that will complete to the return value of [fn] once this task 53 /// A Future that will complete to the return value of [fn] once this task
30 /// finishes running. 54 /// finishes running.
31 Future get result => _resultCompleter.future; 55 Future get result => _resultCompleter.future;
32 final _resultCompleter = new Completer(); 56 final _resultCompleter = new Completer();
33 57
34 Task(fn(), this.queue, this.description) { 58 Task(fn(), String description, TaskQueue queue)
35 _id = this.queue.contents.length; 59 : this._(fn, description, queue, null, queue.contents.length);
60
61 Task._child(fn(), String description, Task parent)
62 : this._(fn, description, parent.queue, parent, parent.children.length);
63
64 Task._(fn(), this.description, this.queue, this.parent, this._id) {
36 this.fn = () { 65 this.fn = () {
37 var future = new Future.immediate(null).then((_) => fn()); 66 var future = new Future.immediate(null).then((_) => fn())
67 .whenComplete(() {
68 if (_childGroup == null || _childGroup.completed) return;
69 return _childGroup.future;
70 });
38 chainToCompleter(future, _resultCompleter); 71 chainToCompleter(future, _resultCompleter);
39 return future; 72 return future;
40 }; 73 };
41 74
42 // Make sure any error thrown by fn isn't top-leveled by virtue of being 75 // Make sure any error thrown by fn isn't top-leveled by virtue of being
43 // passed to the result future. 76 // passed to the result future.
44 result.catchError((_) {}); 77 result.catchError((_) {});
45 } 78 }
46 79
80 /// Run [fn] as a child of this task. Returns a Future that will complete with
81 /// the result of the child task. This task will not complete until [fn] has
82 /// finished.
83 Future runChild(fn(), String description) {
84 var task = new Task._child(fn, description, this);
85 children.add(task);
86 if (_childGroup == null || _childGroup.completed) {
87 _childGroup = new FutureGroup();
88 }
89 // Ignore errors in the FutureGroup; they'll get picked up via wrapFuture,
90 // and we don't want them to short-circuit the other Futures.
91 _childGroup.add(task.result.catchError((_) {}));
92 task.fn();
93 return task.result;
94 }
95
47 String toString() => description == null ? "#$_id" : description; 96 String toString() => description == null ? "#$_id" : description;
48 97
49 /// Returns a detailed representation of [queue] with this task highlighted. 98 /// Returns a detailed representation of [queue] with this task highlighted.
50 String generateTree() => queue.generateTree(this); 99 String generateTree() => queue.generateTree(this);
51 } 100 }
OLDNEW
« no previous file with comments | « pkg/scheduled_test/lib/src/schedule_error.dart ('k') | pkg/scheduled_test/test/scheduled_test_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698