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

Side by Side Diff: pkg/scheduled_test/lib/src/task.dart

Issue 12377093: Add a ScheduledProcess class to pkg/scheduled_test. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes. Created 7 years, 9 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 import 'dart:collection';
9 9
10 import 'future_group.dart'; 10 import 'future_group.dart';
(...skipping 26 matching lines...) Expand all
37 /// A description of this task. Used for debugging. May be `null`. 37 /// A description of this task. Used for debugging. May be `null`.
38 final String description; 38 final String description;
39 39
40 /// The parent task, if this is a nested task that was started while another 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. 41 /// task was running. This will be `null` for top-level tasks.
42 final Task parent; 42 final Task parent;
43 43
44 /// The body of the task. 44 /// The body of the task.
45 TaskBody fn; 45 TaskBody fn;
46 46
47 /// The current state of [this].
48 TaskState get state => _state;
49 var _state = TaskState.WAITING;
50
47 /// The identifier of the task. For top-level tasks, this is the index of the 51 /// The identifier of the task. For top-level tasks, this is the index of the
48 /// task within [queue]; for nested tasks, this is the index within 52 /// task within [queue]; for nested tasks, this is the index within
49 /// [parent.children]. It's used for debugging when [description] isn't 53 /// [parent.children]. It's used for debugging when [description] isn't
50 /// provided. 54 /// provided.
51 int _id; 55 int _id;
52 56
53 /// A Future that will complete to the return value of [fn] once this task 57 /// A Future that will complete to the return value of [fn] once this task
54 /// finishes running. 58 /// finishes running.
55 Future get result => _resultCompleter.future; 59 Future get result => _resultCompleter.future;
56 final _resultCompleter = new Completer(); 60 final _resultCompleter = new Completer();
57 61
58 Task(fn(), String description, TaskQueue queue) 62 Task(fn(), String description, TaskQueue queue)
59 : this._(fn, description, queue, null, queue.contents.length); 63 : this._(fn, description, queue, null, queue.contents.length);
60 64
61 Task._child(fn(), String description, Task parent) 65 Task._child(fn(), String description, Task parent)
62 : this._(fn, description, parent.queue, parent, parent.children.length); 66 : this._(fn, description, parent.queue, parent, parent.children.length);
63 67
64 Task._(fn(), this.description, this.queue, this.parent, this._id) { 68 Task._(fn(), this.description, this.queue, this.parent, this._id) {
65 this.fn = () { 69 this.fn = () {
70 if (state != TaskState.WAITING) {
71 throw new StateError("Can't run $state task '$this'.");
72 }
73
74 _state = TaskState.RUNNING;
66 var future = new Future.immediate(null).then((_) => fn()) 75 var future = new Future.immediate(null).then((_) => fn())
67 .whenComplete(() { 76 .whenComplete(() {
68 if (_childGroup == null || _childGroup.completed) return; 77 if (_childGroup == null || _childGroup.completed) return;
69 return _childGroup.future; 78 return _childGroup.future;
70 }); 79 });
71 chainToCompleter(future, _resultCompleter); 80 chainToCompleter(future, _resultCompleter);
72 return future; 81 return future;
73 }; 82 };
74 83
75 // Make sure any error thrown by fn isn't top-leveled by virtue of being 84 // If the parent queue experiences an error before this task has started
76 // passed to the result future. 85 // running, pipe that error out through [result]. This ensures that we don't
77 result.catchError((_) {}); 86 // get deadlocked by something like `expect(schedule(...), completes)`.
87 queue.onTasksComplete.catchError((e) {
88 if (state == TaskState.WAITING) _resultCompleter.completeError(e);
89 });
90
91 // catchError makes sure any error thrown by fn isn't top-leveled by virtue
92 // of being passed to the result future.
93 result.whenComplete(() {
94 _state = TaskState.DONE;
95 }).catchError((_) {});
78 } 96 }
79 97
80 /// Run [fn] as a child of this task. Returns a Future that will complete with 98 /// 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 99 /// the result of the child task. This task will not complete until [fn] has
82 /// finished. 100 /// finished.
83 Future runChild(fn(), String description) { 101 Future runChild(fn(), String description) {
84 var task = new Task._child(fn, description, this); 102 var task = new Task._child(fn, description, this);
85 children.add(task); 103 children.add(task);
86 if (_childGroup == null || _childGroup.completed) { 104 if (_childGroup == null || _childGroup.completed) {
87 _childGroup = new FutureGroup(); 105 _childGroup = new FutureGroup();
88 } 106 }
89 // Ignore errors in the FutureGroup; they'll get picked up via wrapFuture, 107 // 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. 108 // and we don't want them to short-circuit the other Futures.
91 _childGroup.add(task.result.catchError((_) {})); 109 _childGroup.add(task.result.catchError((_) {}));
92 task.fn(); 110 task.fn();
93 return task.result; 111 return task.result;
94 } 112 }
95 113
96 String toString() => description == null ? "#$_id" : description; 114 String toString() => description == null ? "#$_id" : description;
97 115
98 /// Returns a detailed representation of [queue] with this task highlighted. 116 /// Returns a detailed representation of [queue] with this task highlighted.
99 String generateTree() => queue.generateTree(this); 117 String generateTree() => queue.generateTree(this);
100 } 118 }
119
120 /// An enum of states for a [Task].
121 class TaskState {
122 /// The task is waiting to be run.
123 static const WAITING = const TaskState._("WAITING");
124
125 /// The task is currently running.
126 static const RUNNING = const TaskState._("RUNNING");
127
128 /// The task has finished running, either successfully or with an error.
129 static const DONE = const TaskState._("DONE");
130
131 /// The name of the state.
132 final String name;
133
134 const TaskState._(this.name);
135
136 String toString() => name;
137 }
OLDNEW
« no previous file with comments | « pkg/scheduled_test/lib/src/scheduled_future_matchers.dart ('k') | pkg/scheduled_test/lib/src/utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698