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 // VMOptions=--compile_all --error_on_bad_type --error_on_bad_override --checked | 4 // VMOptions=--compile_all --error_on_bad_type --error_on_bad_override --checked |
5 | 5 |
6 library test_helper; | 6 library test_helper; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:convert'; | 9 import 'dart:convert'; |
10 import 'dart:io'; | 10 import 'dart:io'; |
11 import 'package:observatory/service_io.dart'; | 11 import 'package:observatory/service_io.dart'; |
| 12 import 'package:unittest/unittest.dart'; |
12 | 13 |
13 bool _isWebSocketDisconnect(e) { | 14 bool _isWebSocketDisconnect(e) { |
14 return e is NetworkRpcException; | 15 return e is NetworkRpcException; |
15 } | 16 } |
16 | 17 |
17 // This invocation should set up the state being tested. | 18 // This invocation should set up the state being tested. |
18 const String _TESTEE_MODE_FLAG = "--testee-mode"; | 19 const String _TESTEE_MODE_FLAG = "--testee-mode"; |
19 | 20 |
20 class _TestLauncher { | 21 class _TestLauncher { |
21 Process process; | 22 Process process; |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 vm.getEventStream(streamId).then((stream) { | 149 vm.getEventStream(streamId).then((stream) { |
149 var subscription; | 150 var subscription; |
150 subscription = stream.listen((ServiceEvent event) { | 151 subscription = stream.listen((ServiceEvent event) { |
151 handler(event, subscription, completer); | 152 handler(event, subscription, completer); |
152 }); | 153 }); |
153 }); | 154 }); |
154 return completer.future; | 155 return completer.future; |
155 } | 156 } |
156 | 157 |
157 | 158 |
158 Future<Isolate> hasStoppedAtBreakpoint(Isolate isolate) async { | 159 Future<Isolate> hasStoppedAtBreakpoint(Isolate isolate) { |
159 await isolate.reload(); // Might have missed pauseEvent. | |
160 | |
161 if ((isolate.pauseEvent != null) && | |
162 (isolate.pauseEvent.kind == ServiceEvent.kPauseBreakpoint)) { | |
163 // Already waiting at a breakpoint. | |
164 print('Breakpoint reached'); | |
165 return new Future.value(isolate); | |
166 } | |
167 | |
168 // Set up a listener to wait for breakpoint events. | 160 // Set up a listener to wait for breakpoint events. |
169 Completer completer = new Completer(); | 161 Completer completer = new Completer(); |
170 isolate.vm.getEventStream(VM.kDebugStream).then((stream) { | 162 isolate.vm.getEventStream(VM.kDebugStream).then((stream) { |
171 var subscription; | 163 var subscription; |
172 subscription = stream.listen((ServiceEvent event) { | 164 subscription = stream.listen((ServiceEvent event) { |
| 165 print("Event: $event"); |
173 if (event.kind == ServiceEvent.kPauseBreakpoint) { | 166 if (event.kind == ServiceEvent.kPauseBreakpoint) { |
174 print('Breakpoint reached'); | 167 print('Breakpoint reached'); |
175 subscription.cancel(); | 168 subscription.cancel(); |
176 completer.complete(); | 169 if (completer != null) { |
| 170 completer.complete(isolate); |
| 171 completer = null; |
| 172 } |
177 } | 173 } |
178 }); | 174 }); |
| 175 |
| 176 // Pause may have happened before we subscribed. |
| 177 isolate.reload().then((_) { |
| 178 if ((isolate.pauseEvent != null) && |
| 179 (isolate.pauseEvent.kind == ServiceEvent.kPauseBreakpoint)) { |
| 180 // Already waiting at a breakpoint. |
| 181 print('Breakpoint reached'); |
| 182 subscription.cancel(); |
| 183 if (completer != null) { |
| 184 completer.complete(isolate); |
| 185 completer = null; |
| 186 } |
| 187 } |
| 188 }); |
179 }); | 189 }); |
180 | 190 |
181 return completer.future; // Will complete when breakpoint hit. | 191 return completer.future; // Will complete when breakpoint hit. |
182 } | 192 } |
183 | 193 |
184 | 194 |
| 195 // Currying is your friend. |
| 196 IsolateTest setBreakpointAtLine(int line) { |
| 197 return (Isolate isolate) async { |
| 198 print("Setting breakpoint for line $line"); |
| 199 Library lib = await isolate.rootLibrary.load(); |
| 200 Script script = lib.scripts.single; |
| 201 |
| 202 Breakpoint bpt = await isolate.addBreakpoint(script, line); |
| 203 print("Breakpoint is $bpt"); |
| 204 expect(bpt, isNotNull); |
| 205 expect(bpt is Breakpoint, isTrue); |
| 206 }; |
| 207 } |
| 208 |
| 209 IsolateTest stoppedAtLine(int line) { |
| 210 return (Isolate isolate) async { |
| 211 print("Checking we are at line $line"); |
| 212 |
| 213 ServiceMap stack = await isolate.getStack(); |
| 214 expect(stack.type, equals('Stack')); |
| 215 expect(stack['frames'].length, greaterThanOrEqualTo(1)); |
| 216 |
| 217 Frame top = stack['frames'][0]; |
| 218 Script script = await top.location.script.load(); |
| 219 expect(script.tokenToLine(top.location.tokenPos), equals(line)); |
| 220 }; |
| 221 } |
| 222 |
| 223 |
185 Future<Isolate> resumeIsolate(Isolate isolate) { | 224 Future<Isolate> resumeIsolate(Isolate isolate) { |
186 Completer completer = new Completer(); | 225 Completer completer = new Completer(); |
187 isolate.vm.getEventStream(VM.kDebugStream).then((stream) { | 226 isolate.vm.getEventStream(VM.kDebugStream).then((stream) { |
188 var subscription; | 227 var subscription; |
189 subscription = stream.listen((ServiceEvent event) { | 228 subscription = stream.listen((ServiceEvent event) { |
190 if (event.kind == ServiceEvent.kResume) { | 229 if (event.kind == ServiceEvent.kResume) { |
191 subscription.cancel(); | 230 subscription.cancel(); |
192 completer.complete(); | 231 completer.complete(); |
193 } | 232 } |
194 }); | 233 }); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 }, onError: (e, st) { | 288 }, onError: (e, st) { |
250 process.requestExit(); | 289 process.requestExit(); |
251 if (!_isWebSocketDisconnect(e)) { | 290 if (!_isWebSocketDisconnect(e)) { |
252 print('Unexpected exception in service tests: $e $st'); | 291 print('Unexpected exception in service tests: $e $st'); |
253 throw e; | 292 throw e; |
254 } | 293 } |
255 }); | 294 }); |
256 }); | 295 }); |
257 } | 296 } |
258 } | 297 } |
OLD | NEW |