OLD | NEW |
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 /// A test library for testing test libraries? We must go deeper. | 5 /// A test library for testing test libraries? We must go deeper. |
6 /// | 6 /// |
7 /// Since unit testing code tends to use a lot of global state, it can be tough | 7 /// Since unit testing code tends to use a lot of global state, it can be tough |
8 /// to test. This library manages it by running each test case in a child | 8 /// to test. This library manages it by running each test case in a child |
9 /// isolate, then reporting the results back to the parent isolate. | 9 /// isolate, then reporting the results back to the parent isolate. |
10 library metatest; | 10 library metatest; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 String _testToRun; | 100 String _testToRun; |
101 | 101 |
102 /// The port with which the child isolate should communicate with the parent | 102 /// The port with which the child isolate should communicate with the parent |
103 /// isolate. `null` in the parent isolate. Not set until [_inChildIsolate] | 103 /// isolate. `null` in the parent isolate. Not set until [_inChildIsolate] |
104 /// completes. | 104 /// completes. |
105 SendPort _replyTo; | 105 SendPort _replyTo; |
106 | 106 |
107 /// The cached [Future] for [_inChildIsolate]. | 107 /// The cached [Future] for [_inChildIsolate]. |
108 Future<bool> _inChildIsolateFuture; | 108 Future<bool> _inChildIsolateFuture; |
109 | 109 |
| 110 /// The initial message received by the isolate. |
| 111 var _initialMessage; |
| 112 |
| 113 void metaTestInit(message) { |
| 114 _initialMessage = message; |
| 115 } |
| 116 |
110 /// Returns whether or not we're running in a child isolate that's supposed to | 117 /// Returns whether or not we're running in a child isolate that's supposed to |
111 /// run a test. | 118 /// run a test. |
112 Future<bool> get _inChildIsolate { | 119 Future<bool> get _inChildIsolate { |
113 if (_inChildIsolateFuture != null) return _inChildIsolateFuture; | 120 if (_inChildIsolateFuture != null) return _inChildIsolateFuture; |
114 | 121 |
115 var completer = new Completer(); | 122 if (_initialMessage == null) { |
116 port.receive((message, replyTo) { | 123 _inChildIsolateFuture = new Future.value(false); |
117 _testToRun = message['testToRun']; | 124 } else { |
118 _executable = message['executable']; | 125 _testToRun = _initialMessage['testToRun']; |
119 _replyTo = replyTo; | 126 _executable = _initialMessage['executable']; |
120 port.close(); | 127 _replyTo = _initialMessage['replyTo']; |
121 completer.complete(true); | 128 _inChildIsolateFuture = new Future.value(true); |
122 }); | 129 } |
123 | |
124 // TODO(nweiz): don't use a timeout here once issue 8416 is fixed. | |
125 _inChildIsolateFuture = timeout(completer.future, 500, () { | |
126 port.close(); | |
127 return false; | |
128 }); | |
129 return _inChildIsolateFuture; | 130 return _inChildIsolateFuture; |
130 } | 131 } |
131 | 132 |
132 /// Runs the test described by [description] in its own isolate. Returns a map | 133 /// Runs the test described by [description] in its own isolate. Returns a map |
133 /// describing the results of that test run. | 134 /// describing the results of that test run. |
134 Future<Map> _runInIsolate(String description) { | 135 Future<Map> _runInIsolate(String description) { |
| 136 var replyPort = new ReceivePort(); |
135 // TODO(nweiz): Don't use path here once issue 8440 is fixed. | 137 // TODO(nweiz): Don't use path here once issue 8440 is fixed. |
136 var future = spawnUri(path.join(path.current, Platform.script)).call({ | 138 return Isolate.spawnUri(Uri.parse(path.join(path.current, Platform.script)), |
| 139 [], { |
137 'testToRun': description, | 140 'testToRun': description, |
138 'executable': Platform.executable | 141 'executable': Platform.executable, |
139 }); | 142 'replyTo': replyPort.sendPort |
140 // TODO(nweiz): Remove this timeout once issue 8417 is fixed and we can | 143 }).then((_) { |
141 // capture top-level exceptions. | 144 var future = replyPort.first; |
142 return timeout(future, 30 * 1000, () { | 145 |
143 throw 'Timed out waiting for test to complete.'; | 146 // TODO(nweiz): Remove this timeout once issue 8417 is fixed and we can |
| 147 // capture top-level exceptions. |
| 148 return timeout(future, 30 * 1000, () { |
| 149 throw 'Timed out waiting for test to complete.'; |
| 150 }); |
144 }); | 151 }); |
145 } | 152 } |
146 | 153 |
147 /// Returns whether [results] (a test result map) describes a test run in which | 154 /// Returns whether [results] (a test result map) describes a test run in which |
148 /// an error occurred. | 155 /// an error occurred. |
149 bool _hasError(Map results) { | 156 bool _hasError(Map results) { |
150 return results['errors'] > 0 || results['uncaughtError'] != null || | 157 return results['errors'] > 0 || results['uncaughtError'] != null || |
151 (results['passed'] == 0 && results['failed'] == 0); | 158 (results['passed'] == 0 && results['failed'] == 0); |
152 } | 159 } |
153 | 160 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 "uncaughtError": uncaughtError, | 220 "uncaughtError": uncaughtError, |
214 "results": results.map((testCase) => { | 221 "results": results.map((testCase) => { |
215 "description": testCase.description, | 222 "description": testCase.description, |
216 "message": testCase.message, | 223 "message": testCase.message, |
217 "result": testCase.result, | 224 "result": testCase.result, |
218 "stackTrace": testCase.stackTrace | 225 "stackTrace": testCase.stackTrace |
219 }).toList() | 226 }).toList() |
220 }); | 227 }); |
221 } | 228 } |
222 } | 229 } |
OLD | NEW |