This reverts commit 297c7b5cf2.
This commit is contained in:
@@ -41,9 +41,8 @@ bool _isTaskRegistered = false;
|
||||
/// It is OK for a [task] to perform many things. However, only one task can be
|
||||
/// registered per Dart VM.
|
||||
Future<TaskResult> task(TaskFunction task) async {
|
||||
if (_isTaskRegistered) {
|
||||
if (_isTaskRegistered)
|
||||
throw StateError('A task is already registered');
|
||||
}
|
||||
|
||||
_isTaskRegistered = true;
|
||||
|
||||
@@ -60,18 +59,16 @@ Future<TaskResult> task(TaskFunction task) async {
|
||||
|
||||
class _TaskRunner {
|
||||
_TaskRunner(this.task) {
|
||||
registerExtension('ext.cocoonRunTask', (String method, Map<String, String> parameters) async {
|
||||
registerExtension('ext.cocoonRunTask',
|
||||
(String method, Map<String, String> parameters) async {
|
||||
final Duration taskTimeout = parameters.containsKey('timeoutInMinutes')
|
||||
? Duration(minutes: int.parse(parameters['timeoutInMinutes']))
|
||||
: null;
|
||||
// This is only expected to be passed in unit test runs so they do not
|
||||
// kill the Dart process that is running them.
|
||||
final bool runProcessCleanup = parameters['runProcessCleanup'] != 'false';
|
||||
final bool enableConfig = parameters['enableConfig'] != 'false';
|
||||
final TaskResult result = await run(taskTimeout, runProcessCleanup: runProcessCleanup, enableConfig: enableConfig);
|
||||
? Duration(minutes: int.parse(parameters['timeoutInMinutes']))
|
||||
: null;
|
||||
final TaskResult result = await run(taskTimeout);
|
||||
return ServiceExtensionResponse.result(json.encode(result.toJson()));
|
||||
});
|
||||
registerExtension('ext.cocoonRunnerReady', (String method, Map<String, String> parameters) async {
|
||||
registerExtension('ext.cocoonRunnerReady',
|
||||
(String method, Map<String, String> parameters) async {
|
||||
return ServiceExtensionResponse.result('"ready"');
|
||||
});
|
||||
}
|
||||
@@ -90,77 +87,59 @@ class _TaskRunner {
|
||||
/// Signals that this task runner finished running the task.
|
||||
Future<TaskResult> get whenDone => _completer.future;
|
||||
|
||||
Future<TaskResult> run(Duration taskTimeout, {
|
||||
bool runProcessCleanup = true,
|
||||
bool enableConfig = true,
|
||||
}) async {
|
||||
Future<TaskResult> run(Duration taskTimeout) async {
|
||||
try {
|
||||
_taskStarted = true;
|
||||
print('Running task with a timeout of $taskTimeout.');
|
||||
final String exe = Platform.isWindows ? '.exe' : '';
|
||||
Set<RunningProcessInfo> beforeRunningDartInstances;
|
||||
if (runProcessCleanup) {
|
||||
section('Checking running Dart$exe processes');
|
||||
beforeRunningDartInstances = await getRunningProcesses(
|
||||
processName: 'dart$exe',
|
||||
).toSet();
|
||||
final Set<RunningProcessInfo> allProcesses = await getRunningProcesses().toSet();
|
||||
beforeRunningDartInstances.forEach(print);
|
||||
for (final RunningProcessInfo info in allProcesses) {
|
||||
if (info.commandLine.contains('iproxy')) {
|
||||
print('[LEAK]: ${info.commandLine} ${info.creationDate} ${info.pid} ');
|
||||
}
|
||||
section('Checking running Dart$exe processes');
|
||||
final Set<RunningProcessInfo> beforeRunningDartInstances = await getRunningProcesses(
|
||||
processName: 'dart$exe',
|
||||
).toSet();
|
||||
final Set<RunningProcessInfo> allProcesses = await getRunningProcesses().toSet();
|
||||
beforeRunningDartInstances.forEach(print);
|
||||
for (final RunningProcessInfo info in allProcesses) {
|
||||
if (info.commandLine.contains('iproxy')) {
|
||||
print('[LEAK]: ${info.commandLine} ${info.creationDate} ${info.pid} ');
|
||||
}
|
||||
}
|
||||
if (enableConfig) {
|
||||
print('enabling configs for macOS, Linux, Windows, and Web...');
|
||||
final int configResult = await exec(
|
||||
path.join(flutterDirectory.path, 'bin', 'flutter'),
|
||||
<String>[
|
||||
'config',
|
||||
'-v',
|
||||
'--enable-macos-desktop',
|
||||
'--enable-windows-desktop',
|
||||
'--enable-linux-desktop',
|
||||
'--enable-web',
|
||||
if (localEngine != null) ...<String>['--local-engine', localEngine],
|
||||
],
|
||||
canFail: true);
|
||||
if (configResult != 0) {
|
||||
print('Failed to enable configuration, tasks may not run.');
|
||||
}
|
||||
} else {
|
||||
section('Skipping flutter config. You should only see this in devicelab unit tests');
|
||||
print('enabling configs for macOS, Linux, Windows, and Web...');
|
||||
final int configResult = await exec(path.join(flutterDirectory.path, 'bin', 'flutter'), <String>[
|
||||
'config',
|
||||
'-v',
|
||||
'--enable-macos-desktop',
|
||||
'--enable-windows-desktop',
|
||||
'--enable-linux-desktop',
|
||||
'--enable-web',
|
||||
if (localEngine != null) ...<String>['--local-engine', localEngine],
|
||||
], canFail: true);
|
||||
if (configResult != 0) {
|
||||
print('Failed to enable configuration, tasks may not run.');
|
||||
}
|
||||
|
||||
Future<TaskResult> futureResult = _performTask();
|
||||
if (taskTimeout != null) {
|
||||
if (taskTimeout != null)
|
||||
futureResult = futureResult.timeout(taskTimeout);
|
||||
}
|
||||
|
||||
TaskResult result = await futureResult;
|
||||
|
||||
if (runProcessCleanup) {
|
||||
section('Checking running Dart$exe processes after task...');
|
||||
final List<RunningProcessInfo> afterRunningDartInstances = await getRunningProcesses(
|
||||
processName: 'dart$exe',
|
||||
).toList();
|
||||
for (final RunningProcessInfo info in afterRunningDartInstances) {
|
||||
if (!beforeRunningDartInstances.contains(info)) {
|
||||
print('$info was leaked by this test.');
|
||||
if (result is TaskResultCheckProcesses) {
|
||||
result = TaskResult.failure('This test leaked dart processes');
|
||||
}
|
||||
final bool killed = await killProcess(info.pid);
|
||||
if (!killed) {
|
||||
print('Failed to kill process ${info.pid}.');
|
||||
} else {
|
||||
print('Killed process id ${info.pid}.');
|
||||
}
|
||||
section('Checking running Dart$exe processes after task...');
|
||||
final List<RunningProcessInfo> afterRunningDartInstances = await getRunningProcesses(
|
||||
processName: 'dart$exe',
|
||||
).toList();
|
||||
for (final RunningProcessInfo info in afterRunningDartInstances) {
|
||||
if (!beforeRunningDartInstances.contains(info)) {
|
||||
print('$info was leaked by this test.');
|
||||
if (result is TaskResultCheckProcesses) {
|
||||
result = TaskResult.failure('This test leaked dart processes');
|
||||
}
|
||||
final bool killed = await killProcess(info.pid);
|
||||
if (!killed) {
|
||||
print('Failed to kill process ${info.pid}.');
|
||||
} else {
|
||||
print('Killed process id ${info.pid}.');
|
||||
}
|
||||
}
|
||||
} else {
|
||||
section('Skipping Dart process cleanup. You should only see this in devicelab unit tests');
|
||||
}
|
||||
_completer.complete(result);
|
||||
return result;
|
||||
@@ -170,10 +149,8 @@ class _TaskRunner {
|
||||
print(stackTrace);
|
||||
return TaskResult.failure('Task timed out after $taskTimeout');
|
||||
} finally {
|
||||
if (runProcessCleanup) {
|
||||
await checkForRebootRequired();
|
||||
await forceQuitRunningProcesses();
|
||||
}
|
||||
await checkForRebootRequired();
|
||||
await forceQuitRunningProcesses();
|
||||
_closeKeepAlivePort();
|
||||
}
|
||||
}
|
||||
@@ -211,9 +188,8 @@ class _TaskRunner {
|
||||
/// Causes the Dart VM to stay alive until a request to run the task is
|
||||
/// received via the VM service protocol.
|
||||
void keepVmAliveUntilTaskRunRequested() {
|
||||
if (_taskStarted) {
|
||||
if (_taskStarted)
|
||||
throw StateError('Task already started.');
|
||||
}
|
||||
|
||||
// Merely creating this port object will cause the VM to stay alive and keep
|
||||
// the VM service server running until the port is disposed of.
|
||||
@@ -242,15 +218,17 @@ class _TaskRunner {
|
||||
completer.complete(await task());
|
||||
}, onError: (dynamic taskError, Chain taskErrorStack) {
|
||||
final String message = 'Task failed: $taskError';
|
||||
stderr..writeln(message)..writeln('\nStack trace:')..writeln(taskErrorStack.terse);
|
||||
stderr
|
||||
..writeln(message)
|
||||
..writeln('\nStack trace:')
|
||||
..writeln(taskErrorStack.terse);
|
||||
// IMPORTANT: We're completing the future _successfully_ but with a value
|
||||
// that indicates a task failure. This is intentional. At this point we
|
||||
// are catching errors coming from arbitrary (and untrustworthy) task
|
||||
// code. Our goal is to convert the failure into a readable message.
|
||||
// Propagating it further is not useful.
|
||||
if (!completer.isCompleted) {
|
||||
if (!completer.isCompleted)
|
||||
completer.complete(TaskResult.failure(message));
|
||||
}
|
||||
});
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
@@ -6,61 +6,13 @@ import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:vm_service_client/vm_service_client.dart';
|
||||
|
||||
import 'package:flutter_devicelab/framework/utils.dart';
|
||||
import 'package:flutter_devicelab/framework/adb.dart';
|
||||
|
||||
import 'cocoon.dart';
|
||||
import 'task_result.dart';
|
||||
|
||||
Future<void> runTasks(
|
||||
List<String> taskNames, {
|
||||
bool exitOnFirstTestFailure = false,
|
||||
bool silent = false,
|
||||
String deviceId,
|
||||
String gitBranch,
|
||||
String localEngine,
|
||||
String localEngineSrcPath,
|
||||
String luciBuilder,
|
||||
String resultsPath,
|
||||
List<String> taskArgs,
|
||||
}) async {
|
||||
for (final String taskName in taskNames) {
|
||||
section('Running task "$taskName"');
|
||||
final TaskResult result = await runTask(
|
||||
taskName,
|
||||
deviceId: deviceId,
|
||||
localEngine: localEngine,
|
||||
localEngineSrcPath: localEngineSrcPath,
|
||||
silent: silent,
|
||||
taskArgs: taskArgs,
|
||||
);
|
||||
|
||||
print('Task result:');
|
||||
print(const JsonEncoder.withIndent(' ').convert(result));
|
||||
section('Finished task "$taskName"');
|
||||
|
||||
if (resultsPath != null) {
|
||||
final Cocoon cocoon = Cocoon();
|
||||
await cocoon.writeTaskResultToFile(
|
||||
builderName: luciBuilder,
|
||||
gitBranch: gitBranch,
|
||||
result: result,
|
||||
resultsPath: resultsPath,
|
||||
);
|
||||
}
|
||||
|
||||
if (!result.succeeded) {
|
||||
exitCode = 1;
|
||||
if (exitOnFirstTestFailure) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs a task in a separate Dart VM and collects the result using the VM
|
||||
/// service protocol.
|
||||
///
|
||||
@@ -69,22 +21,17 @@ Future<void> runTasks(
|
||||
///
|
||||
/// Running the task in [silent] mode will suppress standard output from task
|
||||
/// processes and only print standard errors.
|
||||
///
|
||||
/// [taskArgs] are passed to the task executable for additional configuration.
|
||||
Future<TaskResult> runTask(
|
||||
String taskName, {
|
||||
bool silent = false,
|
||||
String localEngine,
|
||||
String localEngineSrcPath,
|
||||
String deviceId,
|
||||
List<String> taskArgs,
|
||||
@visibleForTesting Map<String, String> isolateParams = const <String, String>{},
|
||||
}) async {
|
||||
final String taskExecutable = 'bin/tasks/$taskName.dart';
|
||||
|
||||
if (!file(taskExecutable).existsSync()) {
|
||||
if (!file(taskExecutable).existsSync())
|
||||
throw 'Executable Dart file not found: $taskExecutable';
|
||||
}
|
||||
|
||||
final Process runner = await startProcess(
|
||||
dartBin,
|
||||
@@ -95,10 +42,10 @@ Future<TaskResult> runTask(
|
||||
if (localEngine != null) '-DlocalEngine=$localEngine',
|
||||
if (localEngineSrcPath != null) '-DlocalEngineSrcPath=$localEngineSrcPath',
|
||||
taskExecutable,
|
||||
...?taskArgs,
|
||||
],
|
||||
environment: <String, String>{
|
||||
if (deviceId != null) DeviceIdEnvName: deviceId,
|
||||
if (deviceId != null)
|
||||
DeviceIdEnvName: deviceId,
|
||||
},
|
||||
);
|
||||
|
||||
@@ -116,9 +63,8 @@ Future<TaskResult> runTask(
|
||||
.listen((String line) {
|
||||
if (!uri.isCompleted) {
|
||||
final Uri serviceUri = parseServiceUri(line, prefix: 'Observatory listening on ');
|
||||
if (serviceUri != null) {
|
||||
if (serviceUri != null)
|
||||
uri.complete(serviceUri);
|
||||
}
|
||||
}
|
||||
if (!silent) {
|
||||
stdout.writeln('[$taskName] [STDOUT] $line');
|
||||
@@ -134,15 +80,13 @@ Future<TaskResult> runTask(
|
||||
|
||||
try {
|
||||
final VMIsolateRef isolate = await _connectToRunnerIsolate(await uri.future);
|
||||
final Map<String, dynamic> taskResultJson =
|
||||
await isolate.invokeExtension('ext.cocoonRunTask', isolateParams) as Map<String, dynamic>;
|
||||
final Map<String, dynamic> taskResultJson = await isolate.invokeExtension('ext.cocoonRunTask') as Map<String, dynamic>;
|
||||
final TaskResult taskResult = TaskResult.fromJson(taskResultJson);
|
||||
await runner.exitCode;
|
||||
return taskResult;
|
||||
} finally {
|
||||
if (!runnerFinished) {
|
||||
if (!runnerFinished)
|
||||
runner.kill(ProcessSignal.sigkill);
|
||||
}
|
||||
await stdoutSub.cancel();
|
||||
await stderrSub.cancel();
|
||||
}
|
||||
@@ -154,7 +98,8 @@ Future<VMIsolateRef> _connectToRunnerIsolate(Uri vmServiceUri) async {
|
||||
if (vmServiceUri.pathSegments.isNotEmpty) vmServiceUri.pathSegments[0],
|
||||
'ws',
|
||||
];
|
||||
final String url = vmServiceUri.replace(scheme: 'ws', pathSegments: pathSegments).toString();
|
||||
final String url = vmServiceUri.replace(scheme: 'ws', pathSegments:
|
||||
pathSegments).toString();
|
||||
final Stopwatch stopwatch = Stopwatch()..start();
|
||||
|
||||
while (true) {
|
||||
@@ -167,9 +112,8 @@ Future<VMIsolateRef> _connectToRunnerIsolate(Uri vmServiceUri) async {
|
||||
final VM vm = await client.getVM();
|
||||
final VMIsolateRef isolate = vm.isolates.single;
|
||||
final String response = await isolate.invokeExtension('ext.cocoonRunnerReady') as String;
|
||||
if (response != 'ready') {
|
||||
if (response != 'ready')
|
||||
throw 'not ready yet';
|
||||
}
|
||||
return isolate;
|
||||
} catch (error) {
|
||||
if (stopwatch.elapsed > const Duration(seconds: 10))
|
||||
|
||||
@@ -7,20 +7,13 @@ import 'dart:io';
|
||||
|
||||
/// A result of running a single task.
|
||||
class TaskResult {
|
||||
TaskResult.empty()
|
||||
: succeeded = true,
|
||||
data = null,
|
||||
detailFiles = null,
|
||||
benchmarkScoreKeys = null,
|
||||
message = 'No tests run';
|
||||
|
||||
/// Constructs a successful result.
|
||||
TaskResult.success(this.data, {
|
||||
this.benchmarkScoreKeys = const <String>[],
|
||||
this.detailFiles = const <String>[],
|
||||
this.message = 'success',
|
||||
})
|
||||
: succeeded = true {
|
||||
: succeeded = true,
|
||||
message = 'success' {
|
||||
const JsonEncoder prettyJson = JsonEncoder.withIndent(' ');
|
||||
if (benchmarkScoreKeys != null) {
|
||||
for (final String key in benchmarkScoreKeys) {
|
||||
@@ -56,7 +49,6 @@ class TaskResult {
|
||||
return TaskResult.success(json['data'] as Map<String, dynamic>,
|
||||
benchmarkScoreKeys: benchmarkScoreKeys,
|
||||
detailFiles: detailFiles,
|
||||
message: json['reason'] as String,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -114,8 +106,7 @@ class TaskResult {
|
||||
json['data'] = data;
|
||||
json['detailFiles'] = detailFiles;
|
||||
json['benchmarkScoreKeys'] = benchmarkScoreKeys;
|
||||
}
|
||||
if (message != null || !succeeded) {
|
||||
} else {
|
||||
json['reason'] = message;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user