flutter_tester_device.dart 14.5 KB
Newer Older
1 2 3 4 5
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
6
import 'dart:io' as io; // flutter_ignore: dart_io_import;
7 8 9 10 11

import 'package:dds/dds.dart';
import 'package:meta/meta.dart';
import 'package:process/process.dart';
import 'package:stream_channel/stream_channel.dart';
12
import 'package:vm_service/vm_service.dart' as vm_service;
13 14 15 16 17 18 19

import '../base/file_system.dart';
import '../base/io.dart';
import '../base/logger.dart';
import '../base/platform.dart';
import '../convert.dart';
import '../device.dart';
20
import '../globals.dart' as globals;
21
import '../project.dart';
22
import '../resident_runner.dart';
23 24 25 26 27 28 29 30
import '../vmservice.dart';

import 'font_config_manager.dart';
import 'test_device.dart';

/// Implementation of [TestDevice] with the Flutter Tester over a [Process].
class FlutterTesterTestDevice extends TestDevice {
  FlutterTesterTestDevice({
31 32 33 34 35 36 37
    required this.id,
    required this.platform,
    required this.fileSystem,
    required this.processManager,
    required this.logger,
    required this.shellPath,
    required this.debuggingOptions,
38
    required this.enableVmService,
39 40 41 42 43 44 45
    required this.machine,
    required this.host,
    required this.testAssetDirectory,
    required this.flutterProject,
    required this.icudtlPath,
    required this.compileExpression,
    required this.fontConfigManager,
46
    required this.uriConverter,
47 48
  })  : assert(!debuggingOptions.startPaused || enableVmService),
        _gotProcessVmServiceUri = enableVmService
49
            ? Completer<Uri?>() : (Completer<Uri?>()..complete());
50 51 52 53 54 55 56 57 58

  /// Used for logging to identify the test that is currently being executed.
  final int id;
  final Platform platform;
  final FileSystem fileSystem;
  final ProcessManager processManager;
  final Logger logger;
  final String shellPath;
  final DebuggingOptions debuggingOptions;
59
  final bool enableVmService;
60 61 62 63 64 65
  final bool? machine;
  final InternetAddress? host;
  final String? testAssetDirectory;
  final FlutterProject? flutterProject;
  final String? icudtlPath;
  final CompileExpression? compileExpression;
66
  final FontConfigManager fontConfigManager;
67
  final UriConverter? uriConverter;
68

69
  final Completer<Uri?> _gotProcessVmServiceUri;
70 71
  final Completer<int> _exitCode = Completer<int>();

72 73
  Process? _process;
  HttpServer? _server;
74
  DevtoolsLauncher? _devToolsLauncher;
75

76 77 78 79
  /// Starts the device.
  ///
  /// [entrypointPath] is the path to the entrypoint file which must be compiled
  /// as a dill.
80
  @override
81
  Future<StreamChannel<String>> start(String entrypointPath) async {
82 83 84 85 86 87 88
    assert(!_exitCode.isCompleted);
    assert(_process == null);
    assert(_server == null);

    // Prepare our WebSocket server to talk to the engine subprocess.
    // Let the server choose an unused port.
    _server = await bind(host, /*port*/ 0);
89
    logger.printTrace('test $id: test harness socket server is running at port:${_server!.port}');
90 91
    final List<String> command = <String>[
      shellPath,
92
      if (enableVmService) ...<String>[
93 94 95 96 97 98 99 100 101
        // Some systems drive the _FlutterPlatform class in an unusual way, where
        // only one test file is processed at a time, and the operating
        // environment hands out specific ports ahead of time in a cooperative
        // manner, where we're only allowed to open ports that were given to us in
        // advance like this. For those esoteric systems, we have this feature
        // whereby you can create _FlutterPlatform with a pair of ports.
        //
        // I mention this only so that you won't be tempted, as I was, to apply
        // the obvious simplification to this code and remove this entire feature.
102
        '--vm-service-port=${debuggingOptions.enableDds ? 0 : debuggingOptions.hostVmServicePort }',
103 104 105 106
        if (debuggingOptions.startPaused) '--start-paused',
        if (debuggingOptions.disableServiceAuthCodes) '--disable-service-auth-codes',
      ]
      else
107
        '--disable-vm-service',
108
      if (host!.type == InternetAddressType.IPv6) '--ipv6',
109 110 111
      if (icudtlPath != null) '--icu-data-file-path=$icudtlPath',
      '--enable-checked-mode',
      '--verify-entry-points',
112 113 114 115 116 117 118
      if (debuggingOptions.enableImpeller == ImpellerStatus.enabled)
        '--enable-impeller'
      else
        ...<String>[
          '--enable-software-rendering',
          '--skia-deterministic-rendering',
        ],
119 120
      if (debuggingOptions.enableDartProfiling)
        '--enable-dart-profiling',
121 122
      '--non-interactive',
      '--use-test-fonts',
123
      '--disable-asset-fonts',
124
      '--packages=${debuggingOptions.buildInfo.packagesPath}',
125 126
      if (testAssetDirectory != null)
        '--flutter-assets-dir=$testAssetDirectory',
127 128
      if (debuggingOptions.nullAssertions)
        '--dart-flags=--null_assertions',
129
      ...debuggingOptions.dartEntrypointArgs,
130
      entrypointPath,
131 132 133 134 135 136 137 138
    ];

    // If the FLUTTER_TEST environment variable has been set, then pass it on
    // for package:flutter_test to handle the value.
    //
    // If FLUTTER_TEST has not been set, assume from this context that this
    // call was invoked by the command 'flutter test'.
    final String flutterTest = platform.environment.containsKey('FLUTTER_TEST')
139
        ? platform.environment['FLUTTER_TEST']!
140 141 142 143
        : 'true';
    final Map<String, String> environment = <String, String>{
      'FLUTTER_TEST': flutterTest,
      'FONTCONFIG_FILE': fontConfigManager.fontConfigFile.path,
144 145
      'SERVER_PORT': _server!.port.toString(),
      'APP_NAME': flutterProject?.manifest.appName ?? '',
146 147
      if (debuggingOptions.enableImpeller == ImpellerStatus.enabled)
        'FLUTTER_TEST_IMPELLER': 'true',
148
      if (testAssetDirectory != null)
149
        'UNIT_TEST_ASSETS': testAssetDirectory!,
150 151 152 153 154 155
    };

    logger.printTrace('test $id: Starting flutter_tester process with command=$command, environment=$environment');
    _process = await processManager.start(command, environment: environment);

    // Unawaited to update state.
156 157
    unawaited(_process!.exitCode.then((int exitCode) {
      logger.printTrace('test $id: flutter_tester process at pid ${_process!.pid} exited with code=$exitCode');
158 159 160
      _exitCode.complete(exitCode);
    }));

161
    logger.printTrace('test $id: Started flutter_tester process at pid ${_process!.pid}');
162 163

    // Pipe stdout and stderr from the subprocess to our printStatus console.
164
    // We also keep track of what VM Service port the engine used, if any.
165
    _pipeStandardStreamsToConsole(
166
      process: _process!,
167 168
      reportVmServiceUri: (Uri detectedUri) async {
        assert(!_gotProcessVmServiceUri.isCompleted);
169 170 171
        assert(debuggingOptions.hostVmServicePort == null ||
            debuggingOptions.hostVmServicePort == detectedUri.port);

172
        Uri? forwardingUri;
173 174
        DartDevelopmentService? dds;

175
        if (debuggingOptions.enableDds) {
176
          logger.printTrace('test $id: Starting Dart Development Service');
177
          dds = await startDds(
178 179 180
            detectedUri,
            uriConverter: uriConverter,
          );
181 182 183 184 185 186 187
          forwardingUri = dds.uri;
          logger.printTrace('test $id: Dart Development Service started at ${dds.uri}, forwarding to VM service at ${dds.remoteVmServiceUri}.');
        } else {
          forwardingUri = detectedUri;
        }

        logger.printTrace('Connecting to service protocol: $forwardingUri');
188
        final FlutterVmService vmService = await connectToVmServiceImpl(
189
          forwardingUri!,
190
          compileExpression: compileExpression,
191
          logger: logger,
192
        );
193 194 195 196 197 198
        logger.printTrace('test $id: Successfully connected to service protocol: $forwardingUri');
        if (debuggingOptions.serveObservatory) {
          try {
            await vmService.callMethodWrapper('_serveObservatory');
          } on vm_service.RPCError {
            logger.printWarning('Unable to enable Observatory');
199
          }
200
        }
201

202
        if (debuggingOptions.startPaused && !machine!) {
203 204 205 206
          logger.printStatus('The Dart VM service is listening on $forwardingUri');
          await _startDevTools(forwardingUri, dds);
          logger.printStatus('');
          logger.printStatus('The test process has been started. Set any relevant breakpoints and then resume the test in the debugger.');
207
        }
208
        _gotProcessVmServiceUri.complete(forwardingUri);
209 210 211 212 213 214 215
      },
    );

    return remoteChannel;
  }

  @override
216 217
  Future<Uri?> get vmServiceUri {
    return _gotProcessVmServiceUri.future;
218 219 220 221 222 223 224
  }

  @override
  Future<void> kill() async {
    logger.printTrace('test $id: Terminating flutter_tester process');
    _process?.kill(io.ProcessSignal.sigkill);

225 226 227
    logger.printTrace('test $id: Shutting down DevTools server');
    await _devToolsLauncher?.close();

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    logger.printTrace('test $id: Shutting down test harness socket server');
    await _server?.close(force: true);
    await finished;
  }

  @override
  Future<void> get finished async {
    final int exitCode = await _exitCode.future;

    // On Windows, the [exitCode] and the terminating signal have no correlation.
    if (platform.isWindows) {
      return;
    }

    // ProcessSignal.SIGKILL. Negative because signals are returned as negative
    // exit codes.
    if (exitCode == -9) {
      // We expect SIGKILL (9) because we could have tried to [kill] it.
      return;
    }
    throw TestDeviceException(_getExitCodeMessage(exitCode), StackTrace.current);
  }

  Uri get _ddsServiceUri {
    return Uri(
      scheme: 'http',
254
      host: (host!.type == InternetAddressType.IPv6 ?
255 256 257 258 259 260 261 262 263
        InternetAddress.loopbackIPv6 :
        InternetAddress.loopbackIPv4
      ).host,
      port: debuggingOptions.hostVmServicePort ?? 0,
    );
  }

  @visibleForTesting
  @protected
264
  Future<DartDevelopmentService> startDds(Uri uri, {UriConverter? uriConverter}) {
265 266 267 268
    return DartDevelopmentService.startDartDevelopmentService(
      uri,
      serviceUri: _ddsServiceUri,
      enableAuthCodes: !debuggingOptions.disableServiceAuthCodes,
269
      ipv6: host!.type == InternetAddressType.IPv6,
270
      uriConverter: uriConverter,
271 272 273
    );
  }

274 275 276 277 278 279 280 281 282 283 284 285 286 287
  @visibleForTesting
  @protected
  Future<FlutterVmService> connectToVmServiceImpl(
    Uri httpUri, {
    CompileExpression? compileExpression,
    required Logger logger,
  }) {
    return connectToVmService(
      httpUri,
      compileExpression: compileExpression,
      logger: logger,
    );
  }

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
  Future<void> _startDevTools(Uri forwardingUri, DartDevelopmentService? dds) async {
    _devToolsLauncher = DevtoolsLauncher.instance;
    logger.printTrace('test $id: Serving DevTools...');
    final DevToolsServerAddress? devToolsServerAddress = await _devToolsLauncher?.serve();

    if (devToolsServerAddress == null) {
      logger.printTrace('test $id: Failed to start DevTools');
      return;
    }
    await _devToolsLauncher?.ready;
    logger.printTrace('test $id: DevTools is being served at ${devToolsServerAddress.uri}');

    // Notify the DDS instance that there's a DevTools instance available so it can correctly
    // redirect DevTools related requests.
    dds?.setExternalDevToolsUri(devToolsServerAddress.uri!);

    final Uri devToolsUri = devToolsServerAddress.uri!.replace(
      // Use query instead of queryParameters to avoid unnecessary encoding.
      query: 'uri=$forwardingUri',
    );
    logger.printStatus('The Flutter DevTools debugger and profiler is available at: $devToolsUri');
  }

311 312 313 314 315
  /// Binds an [HttpServer] serving from `host` on `port`.
  ///
  /// Only intended to be overridden in tests.
  @protected
  @visibleForTesting
316
  Future<HttpServer> bind(InternetAddress? host, int port) => HttpServer.bind(host, port);
317 318 319 320 321 322 323

  @protected
  @visibleForTesting
  Future<StreamChannel<String>> get remoteChannel async {
    assert(_server != null);

    try {
324
      final HttpRequest firstRequest = await _server!.first;
325 326 327 328 329 330 331 332 333 334
      final WebSocket webSocket = await WebSocketTransformer.upgrade(firstRequest);
      return _webSocketToStreamChannel(webSocket);
    } on Exception catch (error, stackTrace) {
      throw TestDeviceException('Unable to connect to flutter_tester process: $error', stackTrace);
    }
  }

  @override
  String toString() {
    final String status = _process != null
335
        ? 'pid: ${_process!.pid}, ${_exitCode.isCompleted ? 'exited' : 'running'}'
336 337 338 339 340
        : 'not started';
    return 'Flutter Tester ($status) for test $id';
  }

  void _pipeStandardStreamsToConsole({
341
    required Process process,
342
    required Future<void> Function(Uri uri) reportVmServiceUri,
343 344 345 346 347 348 349 350 351 352 353 354
  }) {
    for (final Stream<List<int>> stream in <Stream<List<int>>>[
      process.stderr,
      process.stdout,
    ]) {
      stream
          .transform<String>(utf8.decoder)
          .transform<String>(const LineSplitter())
          .listen(
            (String line) async {
          logger.printTrace('test $id: Shell: $line');

355
          final Match? match = globals.kVMServiceMessageRegExp.firstMatch(line);
356
          if (match != null) {
357
            try {
358
              final Uri uri = Uri.parse(match[1]!);
359
              await reportVmServiceUri(uri);
360
            } on Exception catch (error) {
361
              logger.printError('Could not parse shell VM Service port message: $error');
362
            }
363
          } else {
364 365
            logger.printStatus('Shell: $line');
          }
366

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
        },
        onError: (dynamic error) {
          logger.printError('shell console stream for process pid ${process.pid} experienced an unexpected error: $error');
        },
        cancelOnError: true,
      );
    }
  }
}

String _getExitCodeMessage(int exitCode) {
  switch (exitCode) {
    case 1:
      return 'Shell subprocess cleanly reported an error. Check the logs above for an error message.';
    case 0:
      return 'Shell subprocess ended cleanly. Did main() call exit()?';
    case -0x0f: // ProcessSignal.SIGTERM
      return 'Shell subprocess crashed with SIGTERM ($exitCode).';
    case -0x0b: // ProcessSignal.SIGSEGV
      return 'Shell subprocess crashed with segmentation fault.';
    case -0x06: // ProcessSignal.SIGABRT
      return 'Shell subprocess crashed with SIGABRT ($exitCode).';
    case -0x02: // ProcessSignal.SIGINT
      return 'Shell subprocess terminated by ^C (SIGINT, $exitCode).';
    default:
      return 'Shell subprocess crashed with unexpected exit code $exitCode.';
  }
}

StreamChannel<String> _webSocketToStreamChannel(WebSocket webSocket) {
  final StreamChannelController<String> controller = StreamChannelController<String>();

  controller.local.stream
      .map<dynamic>((String message) => message as dynamic)
      .pipe(webSocket);
  webSocket
      // We're only communicating with string encoded JSON.
404
      .map<String>((dynamic message) => message as String)
405 406 407 408
      .pipe(controller.local.sink);

  return controller.foreign;
}