Add a detach command to detach without terminating (#21490)
* Add a detach command to detach without terminating (#21376) * Add a detach command to detach without terminating Fixes #21154. * Bump protocol version for app.detach * Tweak to detach/quit text * Change logPrefix to named param * Fix the text that the devicelab attach test looks for
This commit is contained in:
@@ -6,7 +6,6 @@ import 'package:file/file.dart';
|
||||
import 'package:flutter_tools/src/base/file_system.dart';
|
||||
|
||||
import '../src/common.dart';
|
||||
import '../src/context.dart';
|
||||
import 'test_data/basic_project.dart';
|
||||
import 'test_driver.dart';
|
||||
|
||||
@@ -18,24 +17,36 @@ void main() {
|
||||
setUp(() async {
|
||||
tempDir = fs.systemTempDirectory.createTempSync('flutter_attach_test.');
|
||||
await _project.setUpIn(tempDir);
|
||||
_flutterRun = new FlutterTestDriver(tempDir);
|
||||
_flutterAttach = new FlutterTestDriver(tempDir);
|
||||
_flutterRun = new FlutterTestDriver(tempDir, logPrefix: 'RUN');
|
||||
_flutterAttach = new FlutterTestDriver(tempDir, logPrefix: 'ATTACH');
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
// We can't call stop() on both of these because they'll both try to stop the
|
||||
// same app. Just quit the attach process and then send a stop to the original
|
||||
// process.
|
||||
await _flutterAttach.detach();
|
||||
await _flutterRun.stop();
|
||||
await _flutterAttach.quit();
|
||||
tryToDelete(tempDir);
|
||||
});
|
||||
|
||||
group('attached process', () {
|
||||
testUsingContext('can hot reload', () async {
|
||||
test('can hot reload', () async {
|
||||
await _flutterRun.run(withDebugger: true);
|
||||
await _flutterAttach.attach(_flutterRun.vmServicePort);
|
||||
await _flutterAttach.hotReload();
|
||||
});
|
||||
test('can detach, reattach, hot reload', () async {
|
||||
await _flutterRun.run(withDebugger: true);
|
||||
await _flutterAttach.attach(_flutterRun.vmServicePort);
|
||||
await _flutterAttach.detach();
|
||||
await _flutterAttach.attach(_flutterRun.vmServicePort);
|
||||
await _flutterAttach.hotReload();
|
||||
});
|
||||
test('killing process behaves the same as detach ', () async {
|
||||
await _flutterRun.run(withDebugger: true);
|
||||
await _flutterAttach.attach(_flutterRun.vmServicePort);
|
||||
await _flutterAttach.quit();
|
||||
_flutterAttach = new FlutterTestDriver(tempDir, logPrefix: 'ATTACH-2');
|
||||
await _flutterAttach.attach(_flutterRun.vmServicePort);
|
||||
await _flutterAttach.hotReload();
|
||||
});
|
||||
}, timeout: const Timeout.factor(6));
|
||||
}
|
||||
|
||||
@@ -23,9 +23,11 @@ const Duration appStartTimeout = Duration(seconds: 120);
|
||||
const Duration quitTimeout = Duration(seconds: 10);
|
||||
|
||||
class FlutterTestDriver {
|
||||
FlutterTestDriver(this._projectFolder);
|
||||
FlutterTestDriver(this._projectFolder, {String logPrefix}):
|
||||
this._logPrefix = logPrefix != null ? '$logPrefix: ' : '';
|
||||
|
||||
final Directory _projectFolder;
|
||||
final String _logPrefix;
|
||||
Process _proc;
|
||||
int _procPid;
|
||||
final StreamController<String> _stdout = new StreamController<String>.broadcast();
|
||||
@@ -49,7 +51,7 @@ class FlutterTestDriver {
|
||||
msg.length > maxLength ? msg.substring(0, maxLength) + '...' : msg;
|
||||
_allMessages.add(truncatedMsg);
|
||||
if (_printJsonAndStderr) {
|
||||
print(truncatedMsg);
|
||||
print('$_logPrefix$truncatedMsg');
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
@@ -162,6 +164,31 @@ class FlutterTestDriver {
|
||||
_throwErrorResponse('Hot ${fullRestart ? 'restart' : 'reload'} request failed');
|
||||
}
|
||||
|
||||
Future<int> detach() async {
|
||||
if (vmService != null) {
|
||||
_debugPrint('Closing VM service');
|
||||
await vmService.close()
|
||||
.timeout(quitTimeout,
|
||||
onTimeout: () { _debugPrint('VM Service did not quit within $quitTimeout'); });
|
||||
}
|
||||
if (_currentRunningAppId != null) {
|
||||
_debugPrint('Detaching from app');
|
||||
await Future.any<void>(<Future<void>>[
|
||||
_proc.exitCode,
|
||||
_sendRequest(
|
||||
'app.detach',
|
||||
<String, dynamic>{'appId': _currentRunningAppId}
|
||||
),
|
||||
]).timeout(
|
||||
quitTimeout,
|
||||
onTimeout: () { _debugPrint('app.detach did not return within $quitTimeout'); }
|
||||
);
|
||||
_currentRunningAppId = null;
|
||||
}
|
||||
_debugPrint('Waiting for process to end');
|
||||
return _proc.exitCode.timeout(quitTimeout, onTimeout: _killGracefully);
|
||||
}
|
||||
|
||||
Future<int> stop() async {
|
||||
if (vmService != null) {
|
||||
_debugPrint('Closing VM service');
|
||||
|
||||
Reference in New Issue
Block a user