OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 unittest.backend.live_test_controller; | 5 library unittest.backend.live_test_controller; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 | 9 |
10 import 'package:stack_trace/stack_trace.dart'; | 10 import 'package:stack_trace/stack_trace.dart'; |
(...skipping 14 matching lines...) Expand all Loading... |
25 | 25 |
26 State get state => _controller._state; | 26 State get state => _controller._state; |
27 | 27 |
28 Stream<State> get onStateChange => | 28 Stream<State> get onStateChange => |
29 _controller._onStateChangeController.stream; | 29 _controller._onStateChangeController.stream; |
30 | 30 |
31 List<AsyncError> get errors => new UnmodifiableListView(_controller._errors); | 31 List<AsyncError> get errors => new UnmodifiableListView(_controller._errors); |
32 | 32 |
33 Stream<AsyncError> get onError => _controller._onErrorController.stream; | 33 Stream<AsyncError> get onError => _controller._onErrorController.stream; |
34 | 34 |
| 35 Stream<String> get onPrint => _controller._onPrintController.stream; |
| 36 |
35 Future get onComplete => _controller.completer.future; | 37 Future get onComplete => _controller.completer.future; |
36 | 38 |
37 Future run() => _controller._run(); | 39 Future run() => _controller._run(); |
38 | 40 |
39 Future close() => _controller._close(); | 41 Future close() => _controller._close(); |
40 | 42 |
41 _LiveTest(this._controller); | 43 _LiveTest(this._controller); |
42 } | 44 } |
43 | 45 |
44 /// A controller that drives a [LiveTest]. | 46 /// A controller that drives a [LiveTest]. |
(...skipping 24 matching lines...) Expand all Loading... |
69 /// This may be `null`. | 71 /// This may be `null`. |
70 final AsyncFunction _onClose; | 72 final AsyncFunction _onClose; |
71 | 73 |
72 /// The list of errors caught by the test. | 74 /// The list of errors caught by the test. |
73 final _errors = new List<AsyncError>(); | 75 final _errors = new List<AsyncError>(); |
74 | 76 |
75 /// The current state of the test. | 77 /// The current state of the test. |
76 var _state = const State(Status.pending, Result.success); | 78 var _state = const State(Status.pending, Result.success); |
77 | 79 |
78 /// The controller for [LiveTest.onStateChange]. | 80 /// The controller for [LiveTest.onStateChange]. |
79 final _onStateChangeController = new StreamController<State>.broadcast(); | 81 /// |
| 82 /// This is synchronous to ensure that events are well-ordered across multiple |
| 83 /// streams. |
| 84 final _onStateChangeController = new StreamController<State> |
| 85 .broadcast(sync: true); |
80 | 86 |
81 /// The controller for [LiveTest.onError]. | 87 /// The controller for [LiveTest.onError]. |
82 final _onErrorController = new StreamController<AsyncError>.broadcast(); | 88 /// |
| 89 /// This is synchronous to ensure that events are well-ordered across multiple |
| 90 /// streams. |
| 91 final _onErrorController = new StreamController<AsyncError> |
| 92 .broadcast(sync: true); |
| 93 |
| 94 /// The controller for [LiveTest.onPrint]. |
| 95 /// |
| 96 /// This is synchronous to ensure that events are well-ordered across multiple |
| 97 /// streams. |
| 98 final _onPrintController = new StreamController<String>.broadcast(sync: true); |
83 | 99 |
84 /// The completer for [LiveTest.onComplete]; | 100 /// The completer for [LiveTest.onComplete]; |
85 final completer = new Completer(); | 101 final completer = new Completer(); |
86 | 102 |
87 /// Whether [run] has been called. | 103 /// Whether [run] has been called. |
88 var _runCalled = false; | 104 var _runCalled = false; |
89 | 105 |
90 /// Whether [close] has been called. | 106 /// Whether [close] has been called. |
91 bool get _isClosed => _onErrorController.isClosed; | 107 bool get _isClosed => _onErrorController.isClosed; |
92 | 108 |
(...skipping 30 matching lines...) Expand all Loading... |
123 /// | 139 /// |
124 /// If [newState] is different than the old state, this both sets | 140 /// If [newState] is different than the old state, this both sets |
125 /// [LiveTest.state] and emits the new state via [LiveTest.onStateChanged]. If | 141 /// [LiveTest.state] and emits the new state via [LiveTest.onStateChanged]. If |
126 /// it's not different, this does nothing. | 142 /// it's not different, this does nothing. |
127 void setState(State newState) { | 143 void setState(State newState) { |
128 if (_state == newState) return; | 144 if (_state == newState) return; |
129 _state = newState; | 145 _state = newState; |
130 _onStateChangeController.add(newState); | 146 _onStateChangeController.add(newState); |
131 } | 147 } |
132 | 148 |
| 149 /// Emits a line printed by the test over [LiveTest.onPrint]. |
| 150 void print(String line) => _onPrintController.add(line); |
| 151 |
133 /// A wrapper for [_onRun] that ensures that it follows the guarantees for | 152 /// A wrapper for [_onRun] that ensures that it follows the guarantees for |
134 /// [LiveTest.run]. | 153 /// [LiveTest.run]. |
135 Future _run() { | 154 Future _run() { |
136 if (_runCalled) { | 155 if (_runCalled) { |
137 throw new StateError("LiveTest.run() may not be called more than once."); | 156 throw new StateError("LiveTest.run() may not be called more than once."); |
138 } | 157 } |
139 _runCalled = true; | 158 _runCalled = true; |
140 | 159 |
141 _onRun(); | 160 _onRun(); |
142 return liveTest.onComplete; | 161 return liveTest.onComplete; |
143 } | 162 } |
144 | 163 |
145 /// A wrapper for [_onClose] that ensures that all controllers are closed. | 164 /// A wrapper for [_onClose] that ensures that all controllers are closed. |
146 Future _close() { | 165 Future _close() { |
147 if (_isClosed) return new Future.value(); | 166 if (_isClosed) return new Future.value(); |
148 _onStateChangeController.close(); | 167 _onStateChangeController.close(); |
149 _onErrorController.close(); | 168 _onErrorController.close(); |
150 if (!completer.isCompleted) completer.complete(); | 169 if (!completer.isCompleted) completer.complete(); |
151 | 170 |
152 if (_onClose != null) return new Future.sync(_onClose); | 171 if (_onClose != null) return new Future.sync(_onClose); |
153 return new Future.value(); | 172 return new Future.value(); |
154 } | 173 } |
155 } | 174 } |
OLD | NEW |