From b3c319e4f1e702c17d7dece963d0ba7ba03508da Mon Sep 17 00:00:00 2001 From: Danny Tuppeny Date: Wed, 25 Jul 2018 17:29:35 +0100 Subject: [PATCH] Add integration tests to ensure flutter run does not quit at startup (#19473) * Add tests to ensure flutter run does not quit at startup These tests require https://github.com/dart-lang/vm_service_client/pull/38 to be merged and currently the third test (pause-on-exceptions) fails because of a bug. * Remove TODO Probably makes most sense this way for now. * Address PR comments --- .../test/integration/lifetime_test.dart | 47 +++++++++++++++++++ .../test/integration/test_driver.dart | 29 ++++++++---- 2 files changed, 66 insertions(+), 10 deletions(-) create mode 100644 packages/flutter_tools/test/integration/lifetime_test.dart diff --git a/packages/flutter_tools/test/integration/lifetime_test.dart b/packages/flutter_tools/test/integration/lifetime_test.dart new file mode 100644 index 0000000000..ccb460bd74 --- /dev/null +++ b/packages/flutter_tools/test/integration/lifetime_test.dart @@ -0,0 +1,47 @@ +// Copyright 2018 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. + +import 'dart:async'; + +import 'package:file/file.dart'; +import 'package:flutter_tools/src/base/file_system.dart'; +import 'package:test/test.dart'; + +import 'test_data/basic_project.dart'; +import 'test_driver.dart'; + +BasicProject _project = new BasicProject(); +FlutterTestDriver _flutter; + +/// This duration is arbitrary but is ideally: +/// a) long enough to ensure that if the app is crashing at startup, we notice +/// b) as short as possible, to avoid inflating build times +const Duration requiredLifespan = Duration(seconds: 5); + +void main() { + group('flutter run', () { + setUp(() async { + final Directory tempDir = await fs.systemTempDirectory.createTemp('test_app'); + await _project.setUpIn(tempDir); + _flutter = new FlutterTestDriver(tempDir); + }); + + tearDown(() async { + await _flutter.stop(); + _project.cleanup(); + }); + + test('does not terminate when a debugger is attached', () async { + await _flutter.run(withDebugger: true); + await new Future.delayed(requiredLifespan); + expect(_flutter.hasExited, equals(false)); + }); + + test('does not terminate when a debugger is attached and pause-on-exceptions', () async { + await _flutter.run(withDebugger: true, pauseOnExceptions: true); + await new Future.delayed(requiredLifespan); + expect(_flutter.hasExited, equals(false)); + }); + }, timeout: const Timeout.factor(3)); +} diff --git a/packages/flutter_tools/test/integration/test_driver.dart b/packages/flutter_tools/test/integration/test_driver.dart index 6f1da541d3..99e74005ad 100644 --- a/packages/flutter_tools/test/integration/test_driver.dart +++ b/packages/flutter_tools/test/integration/test_driver.dart @@ -34,12 +34,14 @@ class FlutterTestDriver { String _currentRunningAppId; Uri _vmServiceWsUri; int _vmServicePort; + bool _hasExited = false; FlutterTestDriver(this._projectFolder); VMServiceClient vmService; String get lastErrorInfo => _errorBuffer.toString(); int get vmServicePort => _vmServicePort; + bool get hasExited => _hasExited; String _debugPrint(String msg) { const int maxLength = 500; @@ -52,19 +54,16 @@ class FlutterTestDriver { return msg; } - // TODO(dantup): Is there a better way than spawning a proc? This breaks debugging.. - // However, there's a lot of logic inside RunCommand that wouldn't be good - // to duplicate here. - Future run({bool withDebugger = false}) async { + Future run({bool withDebugger = false, bool pauseOnExceptions = false}) async { await _setupProcess([ 'run', '--machine', '-d', 'flutter-tester', - ], withDebugger: withDebugger); + ], withDebugger: withDebugger, pauseOnExceptions: pauseOnExceptions); } - Future attach(int port, {bool withDebugger = false}) async { + Future attach(int port, {bool withDebugger = false, bool pauseOnExceptions = false}) async { await _setupProcess([ 'attach', '--machine', @@ -72,10 +71,10 @@ class FlutterTestDriver { 'flutter-tester', '--debug-port', '$port', - ], withDebugger: withDebugger); + ], withDebugger: withDebugger, pauseOnExceptions: pauseOnExceptions); } - Future _setupProcess(List args, {bool withDebugger = false}) async { + Future _setupProcess(List args, {bool withDebugger = false, bool pauseOnExceptions = false}) async { final String flutterBin = fs.path.join(getFlutterRoot(), 'bin', 'flutter'); _debugPrint('Spawning flutter $args in ${_projectFolder.path}'); @@ -88,6 +87,7 @@ class FlutterTestDriver { workingDirectory: _projectFolder.path, environment: {'FLUTTER_TEST': 'true'}); + _proc.exitCode.then((_) => _hasExited = true); _transformToLines(_proc.stdout).listen((String line) => _stdout.add(line)); _transformToLines(_proc.stderr).listen((String line) => _stderr.add(line)); @@ -128,6 +128,9 @@ class FlutterTestDriver { // expected by tests. Tests will reload/restart as required if they need // to hit breakpoints, etc. await waitForPause(); + if (pauseOnExceptions) { + await (await getFlutterIsolate()).setExceptionPauseMode(VMExceptionPauseMode.unhandled); + } await resume(wait: false); } @@ -188,9 +191,15 @@ class FlutterTestDriver { return _proc.exitCode; } - Future addBreakpoint(String path, int line) async { + Future getFlutterIsolate() async { + // Currently these tests only have a single isolate. If this + // ceases to be the case, this code will need changing. final VM vm = await vmService.getVM(); - final VMIsolate isolate = await vm.isolates.first.load(); + return await vm.isolates.first.load(); + } + + Future addBreakpoint(String path, int line) async { + final VMIsolate isolate = await getFlutterIsolate(); _debugPrint('Sending breakpoint for $path:$line'); await isolate.addBreakpoint(path, line); }