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 // VMOptions=--compile-all --error_on_bad_type --error_on_bad_override | 4 // VMOptions=--compile-all --error_on_bad_type --error_on_bad_override |
5 | 5 |
6 import 'package:observatory/service_io.dart'; | 6 import 'package:observatory/service_io.dart'; |
7 import 'package:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
8 import 'test_helper.dart'; | 8 import 'test_helper.dart'; |
9 import 'dart:async'; | 9 import 'dart:async'; |
10 | 10 |
11 void helper(i) { | 11 int counter = 0; |
12 print(i); | 12 |
| 13 void periodicTask(_) { |
| 14 counter++; |
| 15 counter++; // Line 15. We set our breakpoint here. |
| 16 counter++; |
| 17 if (counter % 300 == 0) { |
| 18 print('counter = $counter'); |
| 19 } |
13 } | 20 } |
14 | 21 |
15 void testFunction() { | 22 void startTimer() { |
16 int i = 0; | 23 new Timer.periodic(const Duration(milliseconds:10), periodicTask); |
17 while (true) { | |
18 if (++i % 100000000 == 0) { | |
19 helper(i); // line 18 | |
20 } | |
21 } | |
22 } | 24 } |
23 | 25 |
24 var tests = [ | 26 var tests = [ |
25 | 27 |
26 // Pause | 28 // Pause |
27 (Isolate isolate) { | 29 (Isolate isolate) async { |
28 Completer completer = new Completer(); | 30 Completer completer = new Completer(); |
29 var subscription; | 31 var subscription; |
30 subscription = isolate.vm.events.stream.listen((ServiceEvent event) { | 32 subscription = isolate.vm.events.stream.listen((ServiceEvent event) { |
31 if (event.eventType == ServiceEvent.kPauseInterrupted) { | 33 if (event.eventType == ServiceEvent.kPauseInterrupted) { |
32 subscription.cancel(); | 34 subscription.cancel(); |
33 completer.complete(); | 35 completer.complete(); |
34 } | 36 } |
35 }); | 37 }); |
36 isolate.pause(); | 38 isolate.pause(); |
37 return completer.future; | 39 await completer.future; |
38 }, | 40 }, |
39 | 41 |
40 // Resume | 42 // Resume |
41 (Isolate isolate) { | 43 (Isolate isolate) async { |
42 Completer completer = new Completer(); | 44 Completer completer = new Completer(); |
43 var subscription; | 45 var subscription; |
44 subscription = isolate.vm.events.stream.listen((ServiceEvent event) { | 46 subscription = isolate.vm.events.stream.listen((ServiceEvent event) { |
45 if (event.eventType == ServiceEvent.kResume) { | 47 if (event.eventType == ServiceEvent.kResume) { |
46 subscription.cancel(); | 48 subscription.cancel(); |
47 completer.complete(); | 49 completer.complete(); |
48 } | 50 } |
49 }); | 51 }); |
50 isolate.resume(); | 52 isolate.resume(); |
51 return completer.future; | 53 await completer.future; |
52 }, | 54 }, |
53 | 55 |
54 // Add breakpoint | 56 // Add breakpoint |
55 (Isolate isolate) { | 57 (Isolate isolate) async { |
56 return isolate.rootLib.load().then((_) { | 58 await isolate.rootLib.load(); |
57 // Set up a listener to wait for breakpoint events. | |
58 Completer completer = new Completer(); | |
59 var subscription; | |
60 subscription = isolate.vm.events.stream.listen((ServiceEvent event) { | |
61 if (event.eventType == ServiceEvent.kPauseBreakpoint) { | |
62 print('Breakpoint reached'); | |
63 subscription.cancel(); | |
64 completer.complete(); | |
65 } | |
66 }); | |
67 | 59 |
68 // Add the breakpoint. | 60 // Set up a listener to wait for breakpoint events. |
69 var script = isolate.rootLib.scripts[0]; | 61 Completer completer = new Completer(); |
70 return isolate.addBreakpoint(script, 18).then((result) { | 62 var subscription; |
71 expect(result is Breakpoint, isTrue); | 63 subscription = isolate.vm.events.stream.listen((ServiceEvent event) { |
72 Breakpoint bpt = result; | 64 if (event.eventType == ServiceEvent.kPauseBreakpoint) { |
73 expect(bpt.type, equals('Breakpoint')); | 65 print('Breakpoint reached'); |
74 expect(bpt.script.id, equals(script.id)); | 66 subscription.cancel(); |
75 expect(bpt.tokenPos, equals(58)); | 67 completer.complete(); |
76 expect(isolate.breakpoints.length, equals(1)); | 68 } |
77 return completer.future; // Wait for breakpoint events. | 69 }); |
78 }); | 70 |
79 }); | 71 var script = isolate.rootLib.scripts[0]; |
| 72 await script.load(); |
| 73 |
| 74 // Add the breakpoint. |
| 75 var result = await isolate.addBreakpoint(script, 15); |
| 76 expect(result is Breakpoint, isTrue); |
| 77 Breakpoint bpt = result; |
| 78 expect(bpt.type, equals('Breakpoint')); |
| 79 expect(bpt.script.id, equals(script.id)); |
| 80 expect(bpt.script.tokenToLine(bpt.tokenPos), equals(15)); |
| 81 expect(isolate.breakpoints.length, equals(1)); |
| 82 |
| 83 await completer.future; // Wait for breakpoint events. |
80 }, | 84 }, |
81 | 85 |
82 // Get the stack trace | 86 // We are at the breakpoint on line 15. |
83 (Isolate isolate) { | 87 (Isolate isolate) async { |
84 return isolate.getStack().then((ServiceMap stack) { | 88 ServiceMap stack = await isolate.getStack(); |
85 expect(stack.type, equals('Stack')); | 89 expect(stack.type, equals('Stack')); |
86 expect(stack['frames'].length, greaterThanOrEqualTo(1)); | 90 expect(stack['frames'].length, greaterThanOrEqualTo(1)); |
87 expect(stack['frames'][0]['function'].name, equals('testFunction')); | 91 |
88 expect(stack['frames'][0]['tokenPos'], equals(58)); | 92 Script script = stack['frames'][0]['script']; |
89 }); | 93 expect(script.name,endsWith('debugging_test.dart')); |
| 94 expect(script.tokenToLine(stack['frames'][0]['tokenPos']), equals(15)); |
90 }, | 95 }, |
91 | 96 |
92 // Stepping | 97 // Stepping |
93 (Isolate isolate) { | 98 (Isolate isolate) async { |
94 // Set up a listener to wait for breakpoint events. | 99 // Set up a listener to wait for breakpoint events. |
95 Completer completer = new Completer(); | 100 Completer completer = new Completer(); |
96 var subscription; | 101 var subscription; |
97 subscription = isolate.vm.events.stream.listen((ServiceEvent event) { | 102 subscription = isolate.vm.events.stream.listen((ServiceEvent event) { |
98 if (event.eventType == ServiceEvent.kPauseBreakpoint) { | 103 if (event.eventType == ServiceEvent.kPauseBreakpoint) { |
99 print('Breakpoint reached'); | 104 print('Breakpoint reached'); |
100 subscription.cancel(); | 105 subscription.cancel(); |
101 completer.complete(); | 106 completer.complete(); |
102 } | 107 } |
103 }); | 108 }); |
104 | 109 |
105 return isolate.stepInto().then((isolate) { | 110 await isolate.stepOver(); |
106 return completer.future; // Wait for breakpoint events. | 111 await completer.future; // Wait for breakpoint events. |
107 }); | |
108 }, | 112 }, |
109 | 113 |
110 // Get the stack trace again. Our position has updated. | 114 // We are now at line 16. |
111 (Isolate isolate) { | 115 (Isolate isolate) async { |
112 return isolate.getStack().then((ServiceMap stack) { | 116 ServiceMap stack = await isolate.getStack(); |
113 expect(stack.type, equals('Stack')); | 117 expect(stack.type, equals('Stack')); |
114 expect(stack['frames'].length, greaterThanOrEqualTo(2)); | 118 expect(stack['frames'].length, greaterThanOrEqualTo(1)); |
115 expect(stack['frames'][0]['function'].name, equals('testFunction')); | 119 |
116 expect(stack['frames'][0]['tokenPos'], equals(60)); | 120 Script script = stack['frames'][0]['script']; |
117 }); | 121 expect(script.name,endsWith('debugging_test.dart')); |
| 122 expect(script.tokenToLine(stack['frames'][0]['tokenPos']), equals(16)); |
118 }, | 123 }, |
119 | 124 |
120 // Remove breakpoint | 125 // Remove breakpoint |
121 (Isolate isolate) { | 126 (Isolate isolate) async { |
122 // Set up a listener to wait for breakpoint events. | 127 // Set up a listener to wait for breakpoint events. |
123 Completer completer = new Completer(); | 128 Completer completer = new Completer(); |
124 var subscription; | 129 var subscription; |
125 subscription = isolate.vm.events.stream.listen((ServiceEvent event) { | 130 subscription = isolate.vm.events.stream.listen((ServiceEvent event) { |
126 if (event.eventType == ServiceEvent.kBreakpointRemoved) { | 131 if (event.eventType == ServiceEvent.kBreakpointRemoved) { |
127 print('Breakpoint removed'); | 132 print('Breakpoint removed'); |
128 expect(isolate.breakpoints.length, equals(0)); | 133 expect(isolate.breakpoints.length, equals(0)); |
129 subscription.cancel(); | 134 subscription.cancel(); |
130 completer.complete(); | 135 completer.complete(); |
131 } | 136 } |
132 }); | 137 }); |
133 | 138 |
134 expect(isolate.breakpoints.length, equals(1)); | 139 expect(isolate.breakpoints.length, equals(1)); |
135 var bpt = isolate.breakpoints.values.first; | 140 var bpt = isolate.breakpoints.values.first; |
136 return isolate.removeBreakpoint(bpt).then((_) { | 141 await isolate.removeBreakpoint(bpt); |
137 return completer.future; | 142 await completer.future; |
138 }); | |
139 }, | 143 }, |
140 | 144 |
141 // Resume | 145 // Resume |
142 (Isolate isolate) { | 146 (Isolate isolate) async { |
143 Completer completer = new Completer(); | 147 Completer completer = new Completer(); |
144 var subscription; | 148 var subscription; |
145 subscription = isolate.vm.events.stream.listen((ServiceEvent event) { | 149 subscription = isolate.vm.events.stream.listen((ServiceEvent event) { |
146 if (event.eventType == ServiceEvent.kResume) { | 150 if (event.eventType == ServiceEvent.kResume) { |
147 subscription.cancel(); | 151 subscription.cancel(); |
148 completer.complete(); | 152 completer.complete(); |
149 } | 153 } |
150 }); | 154 }); |
151 isolate.resume(); | 155 isolate.resume(); |
152 return completer.future; | 156 await completer.future; |
153 }, | 157 }, |
154 | 158 |
155 // Add breakpoint at function entry | 159 // Add breakpoint at function entry |
156 (Isolate isolate) { | 160 (Isolate isolate) async { |
157 // Set up a listener to wait for breakpoint events. | 161 // Set up a listener to wait for breakpoint events. |
158 Completer completer = new Completer(); | 162 Completer completer = new Completer(); |
159 var subscription; | 163 var subscription; |
160 subscription = isolate.vm.events.stream.listen((ServiceEvent event) { | 164 subscription = isolate.vm.events.stream.listen((ServiceEvent event) { |
161 if (event.eventType == ServiceEvent.kPauseBreakpoint) { | 165 if (event.eventType == ServiceEvent.kPauseBreakpoint) { |
162 print('Breakpoint reached'); | 166 print('Breakpoint reached'); |
163 subscription.cancel(); | 167 subscription.cancel(); |
164 completer.complete(); | 168 completer.complete(); |
165 } | 169 } |
166 }); | 170 }); |
167 | 171 |
168 // Find a specific function. | 172 // Find a specific function. |
169 ServiceFunction function = isolate.rootLib.functions.firstWhere( | 173 ServiceFunction function = isolate.rootLib.functions.firstWhere( |
170 (f) => f.name == 'helper'); | 174 (f) => f.name == 'periodicTask'); |
171 expect(function, isNotNull); | 175 expect(function, isNotNull); |
172 | 176 |
173 // Add the breakpoint at function entry | 177 // Add the breakpoint at function entry |
174 return isolate.addBreakpointAtEntry(function).then((result) { | 178 var result = await isolate.addBreakpointAtEntry(function); |
175 expect(result is Breakpoint, isTrue); | 179 expect(result is Breakpoint, isTrue); |
176 Breakpoint bpt = result; | 180 Breakpoint bpt = result; |
177 expect(bpt.type, equals('Breakpoint')); | 181 expect(bpt.type, equals('Breakpoint')); |
178 expect(bpt.script.name, equals('debugging_test.dart')); | 182 expect(bpt.script.name, equals('debugging_test.dart')); |
179 expect(bpt.tokenPos, equals(29)); | 183 expect(bpt.script.tokenToLine(bpt.tokenPos), equals(14)); |
180 expect(isolate.breakpoints.length, equals(1)); | 184 expect(isolate.breakpoints.length, equals(1)); |
181 return completer.future; // Wait for breakpoint events. | 185 |
182 }); | 186 await completer.future; // Wait for breakpoint events. |
| 187 }, |
| 188 |
| 189 // We are now at line 14. |
| 190 (Isolate isolate) async { |
| 191 ServiceMap stack = await isolate.getStack(); |
| 192 expect(stack.type, equals('Stack')); |
| 193 expect(stack['frames'].length, greaterThanOrEqualTo(1)); |
| 194 |
| 195 Script script = stack['frames'][0]['script']; |
| 196 expect(script.name,endsWith('debugging_test.dart')); |
| 197 expect(script.tokenToLine(stack['frames'][0]['tokenPos']), equals(14)); |
183 }, | 198 }, |
184 | 199 |
185 ]; | 200 ]; |
186 | 201 |
187 main(args) => runIsolateTests(args, tests, testeeConcurrent: testFunction); | 202 main(args) => runIsolateTests(args, tests, testeeBefore: startTimer); |
OLD | NEW |