提交 4511e0c9 编写于 作者: J John McCutchan

Add heart beat integration tests for Observatory in sky_shell

上级 8a701198
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
main() {
}
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
library observatory_sky_shell_launcher;
import 'dart:async';
import 'dart:convert';
import 'dart:io';
class ShellProcess {
final Completer _observatoryUriCompleter = new Completer();
final Process _process;
ShellProcess(this._process) {
assert(_process != null);
// Scan stdout and scrape the Observatory Uri.
_process.stdout.transform(UTF8.decoder)
.transform(new LineSplitter()).listen((line) {
const String observatoryUriPrefix = 'Observatory listening on ';
if (line.startsWith(observatoryUriPrefix)) {
Uri uri = Uri.parse(line.substring(observatoryUriPrefix.length));
_observatoryUriCompleter.complete(uri);
}
});
}
Future kill() async {
if (_process == null) {
return false;
}
return _process.kill();
}
Future<Uri> waitForObservatory() async {
return _observatoryUriCompleter.future;
}
}
class ShellLauncher {
final List<String> args = [
'--observatory-port=0',
];
final String shellExecutablePath;
final String mainDartPath;
ShellLauncher(this.shellExecutablePath,
this.mainDartPath,
List<String> extraArgs) {
if (extraArgs is List) {
args.addAll(extraArgs);
}
args.add(mainDartPath);
}
Future<ShellProcess> launch() async {
try {
var process = await Process.start(shellExecutablePath, args);
return new ShellProcess(process);
} catch (e) {
print('Error launching shell: $e');
}
return null;
}
}
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
library observatory_sky_shell_service_client;
import 'dart:async';
import 'dart:convert';
class ServiceClient {
ServiceClient(this.client) {
client.listen(_onData,
onError: _onError,
cancelOnError: true);
}
Future<Map> invokeRPC(String method, [Map params]) async {
var key = _createKey();
var request = JSON.encode({
'jsonrpc': '2.0',
'method': method,
'params': params == null ? {} : params,
'id': key,
});
client.add(request);
var completer = new Completer();
_outstanding_requests[key] = completer;
print('-> $key ($method)');
return completer.future;
}
String _createKey() {
var key = '$_id';
_id++;
return key;
}
void _onData(String message) {
var response = JSON.decode(message);
var key = response['id'];
print('<- $key');
var completer = _outstanding_requests.remove(key);
assert(completer != null);
var result = response['result'];
var error = response['error'];
if (error != null) {
assert(result == null);
completer.completeError(error);
} else {
assert(result != null);
completer.complete(result);
}
}
void _onError(error) {
print('WebSocket error: $error');
}
final WebSocket client;
final Map<String, Completer> _outstanding_requests = <String, Completer>{};
var _id = 1;
}
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This is a minimal dependency heart beat test for Observatory.
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'launcher.dart';
import 'service_client.dart';
class Expect {
static equals(dynamic actual, dynamic expected) {
if (actual != expected) {
throw 'Expected $actual == $expected';
}
}
static contains(String needle, String haystack) {
if (!haystack.contains(needle)) {
throw 'Expected $haystack to contain $needle';
}
}
static isTrue(bool tf) {
if (tf != true) {
throw 'Expected $a to be true';
}
}
static isFalse(bool tf) {
if (tf != false) {
throw 'Expected $a to be false';
}
}
static notExecuted() {
throw 'Should not have hit';
}
static isNotNull(dynamic a) {
if (a == null) {
throw 'Expected $a to not be null';
}
}
}
Future<String> readResponse(HttpClientResponse response) {
var completer = new Completer();
var contents = new StringBuffer();
response.transform(UTF8.decoder).listen((String data) {
contents.write(data);
}, onDone: () => completer.complete(contents.toString()));
return completer.future;
}
// Test accessing the service protocol over http.
Future testHttpProtocolRequest(Uri uri) async {
uri = uri.replace(path: 'getVM');
HttpClient client = new HttpClient();
HttpClientRequest request = await client.getUrl(uri);
HttpClientResponse response = await request.close();
Expect.equals(response.statusCode, 200);
Map responseAsMap = JSON.decode(await readResponse(response));
Expect.equals(responseAsMap['jsonrpc'], "2.0");
client.close();
}
// Test accessing the service protocol over ws.
Future testWebSocketProtocolRequest(Uri uri) async {
uri = uri.replace(scheme: 'ws', path: 'ws');
WebSocket webSocketClient = await WebSocket.connect(uri.toString());
ServiceClient serviceClient = new ServiceClient(webSocketClient);
Map response = await serviceClient.invokeRPC('getVM');
Expect.equals(response['type'], 'VM');
try {
await serviceClient.invokeRPC('BART_SIMPSON');
Expect.notExecuted();
} catch (e) {
// Method not found.
Expect.equals(e['code'], -32601);
}
}
// Test accessing an Observatory UI asset.
Future testHttpAssetRequest(Uri uri) async {
uri = uri.replace(path: 'third_party/trace_viewer_full.html');
HttpClient client = new HttpClient();
HttpClientRequest request = await client.getUrl(uri);
HttpClientResponse response = await request.close();
Expect.equals(response.statusCode, 200);
await response.drain();
client.close();
}
typedef Future TestFunction(Uri uri);
final List<TestFunction> tests = [
testHttpProtocolRequest,
testWebSocketProtocolRequest,
testHttpAssetRequest
];
main(List<String> args) async {
if (args.length < 2) {
print('Usage: dart ${Platform.script} '
'<sky_shell_executable> <main_dart> ...');
return;
}
final String shellExecutablePath = args[0];
final String mainDartPath = args[1];
final List<String> extraArgs = args.length <= 2 ? [] : args.sublist(2);
ShellLauncher launcher =
new ShellLauncher(shellExecutablePath,
mainDartPath,
extraArgs);
ShellProcess process = await launcher.launch();
Uri uri = await process.waitForObservatory();
try {
for (var i = 0; i < tests.length; i++) {
print('Executing test ${i+1}/${tests.length}');
await tests[i](uri);
}
} catch (e) {
print('Observatory test failure: $e');
exitCode = -1;
}
await process.kill();
}
echo "Testing sky_shell Observatory..."
PATH="$HOME/depot_tools:$PATH"
SKY_SHELL_BIN=out/Debug/sky_shell
OBSERVATORY_TEST=sky/shell/testing/observatory/test.dart
EMPTY_MAIN=sky/shell/testing/observatory/empty_main.dart
dart $OBSERVATORY_TEST $SKY_SHELL_BIN $EMPTY_MAIN
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册