Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 98f6c2d

Browse files
authored
Fix shell launcher test (dart2 compliant and stop polling) (#5009)
* Make test Dart2 compliant * Use service and debug events instead of polling to wait for isolate to start, run and resume. * Refactor into _onServiceEvent. Wait for 'paused' event instead of 'isolate runnable'.
1 parent 75851f0 commit 98f6c2d

File tree

4 files changed

+147
-106
lines changed

4 files changed

+147
-106
lines changed

shell/testing/observatory/empty_main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
main() {
5+
void main() {
66
}

shell/testing/observatory/launcher.dart

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,23 @@ import 'dart:convert';
99
import 'dart:io';
1010

1111
class ShellProcess {
12-
final Completer _observatoryUriCompleter = new Completer();
12+
final Completer<Uri> _observatoryUriCompleter = new Completer<Uri>();
1313
final Process _process;
1414

15-
ShellProcess(this._process) {
16-
assert(_process != null);
15+
ShellProcess(this._process) : assert(_process != null) {
1716
// Scan stdout and scrape the Observatory Uri.
18-
_process.stdout.transform(UTF8.decoder)
19-
.transform(new LineSplitter()).listen((line) {
17+
_process.stdout.transform(utf8.decoder)
18+
.transform(const LineSplitter()).listen((String line) {
2019
const String observatoryUriPrefix = 'Observatory listening on ';
2120
if (line.startsWith(observatoryUriPrefix)) {
2221
print(line);
23-
Uri uri = Uri.parse(line.substring(observatoryUriPrefix.length));
22+
final Uri uri = Uri.parse(line.substring(observatoryUriPrefix.length));
2423
_observatoryUriCompleter.complete(uri);
2524
}
2625
});
2726
}
2827

29-
Future kill() async {
28+
Future<bool> kill() async {
3029
if (_process == null) {
3130
return false;
3231
}
@@ -39,7 +38,7 @@ class ShellProcess {
3938
}
4039

4140
class ShellLauncher {
42-
final List<String> args = [
41+
final List<String> args = <String>[
4342
'--observatory-port=0',
4443
'--non-interactive',
4544
'--run-forever',
@@ -60,13 +59,13 @@ class ShellLauncher {
6059

6160
Future<ShellProcess> launch() async {
6261
try {
63-
List<String> shellArguments = [];
62+
final List<String> shellArguments = <String>[];
6463
if (startPaused) {
6564
shellArguments.add('--start-paused');
6665
}
6766
shellArguments.addAll(args);
6867
print('Launching $shellExecutablePath $shellArguments');
69-
var process = await Process.start(shellExecutablePath, shellArguments);
68+
final Process process = await Process.start(shellExecutablePath, shellArguments);
7069
return new ShellProcess(process);
7170
} catch (e) {
7271
print('Error launching shell: $e');

shell/testing/observatory/service_client.dart

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,100 @@
44

55
library observatory_sky_shell_service_client;
66

7-
87
import 'dart:async';
98
import 'dart:convert';
9+
import 'dart:io';
10+
1011

1112
class ServiceClient {
12-
ServiceClient(this.client) {
13+
Completer<dynamic> isolateStartedId;
14+
Completer<dynamic> isolatePausedId;
15+
Completer<dynamic> isolateResumeId;
16+
17+
ServiceClient(this.client, {this.isolateStartedId, this.isolatePausedId,
18+
this.isolateResumeId}) {
1319
client.listen(_onData,
1420
onError: _onError,
1521
cancelOnError: true);
1622
}
1723

18-
Future<Map> invokeRPC(String method, [Map params]) async {
19-
var key = _createKey();
20-
var request = JSON.encode({
24+
Future<Map<String, dynamic>> invokeRPC(String method, [Map<String, dynamic> params]) async {
25+
final String key = _createKey();
26+
final String request = json.encode(<String, dynamic>{
2127
'jsonrpc': '2.0',
2228
'method': method,
23-
'params': params == null ? {} : params,
29+
'params': params == null ? <String, dynamic>{} : params,
2430
'id': key,
2531
});
2632
client.add(request);
27-
var completer = new Completer();
28-
_outstanding_requests[key] = completer;
33+
final Completer<Map<String, dynamic>> completer = new Completer<Map<String, dynamic>>();
34+
_outstandingRequests[key] = completer;
2935
print('-> $key ($method)');
3036
return completer.future;
3137
}
3238

3339
String _createKey() {
34-
var key = '$_id';
40+
final String key = '$_id';
3541
_id++;
3642
return key;
3743
}
3844

39-
void _onData(String message) {
40-
var response = JSON.decode(message);
41-
var key = response['id'];
42-
print('<- $key');
43-
var completer = _outstanding_requests.remove(key);
44-
assert(completer != null);
45-
var result = response['result'];
46-
var error = response['error'];
47-
if (error != null) {
48-
assert(result == null);
49-
completer.completeError(error);
45+
void _onData(dynamic message) {
46+
final Map<String, dynamic> response = json.decode(message);
47+
final dynamic key = response['id'];
48+
if (key != null) {
49+
print('<- $key');
50+
final dynamic completer = _outstandingRequests.remove(key);
51+
assert(completer != null);
52+
final dynamic result = response['result'];
53+
final dynamic error = response['error'];
54+
if (error != null) {
55+
assert(result == null);
56+
completer.completeError(error);
57+
} else {
58+
assert(result != null);
59+
completer.complete(result);
60+
}
5061
} else {
51-
assert(result != null);
52-
completer.complete(result);
62+
if (response['method'] == 'streamNotify') {
63+
_onServiceEvent(response['params']);
64+
}
65+
}
66+
}
67+
68+
void _onServiceEvent(Map<String, dynamic> params) {
69+
if (params == null) {
70+
return;
71+
}
72+
final Map<String, dynamic> event = params['event'];
73+
if (event == null || event['type'] != 'Event') {
74+
return;
75+
}
76+
final dynamic isolateId = event['isolate']['id'];
77+
switch (params['streamId']) {
78+
case 'Isolate':
79+
if (event['kind'] == 'IsolateStarted') {
80+
isolateStartedId?.complete(isolateId);
81+
}
82+
break;
83+
case 'Debug':
84+
switch (event['kind']) {
85+
case 'Resume':
86+
isolateResumeId?.complete(isolateId);
87+
break;
88+
case 'PauseStart':
89+
isolatePausedId?.complete(isolateId);
90+
break;
91+
}
92+
break;
5393
}
5494
}
5595

56-
void _onError(error) {
96+
void _onError(dynamic error) {
5797
print('WebSocket error: $error');
5898
}
5999

60100
final WebSocket client;
61-
final Map<String, Completer> _outstanding_requests = <String, Completer>{};
62-
var _id = 1;
101+
final Map<String, Completer<dynamic>> _outstandingRequests = <String, Completer<dynamic>>{};
102+
int _id = 1;
63103
}

0 commit comments

Comments
 (0)