Roll package:dds to 1.4.0 and update error handling (#66836)

This commit is contained in:
Ben Konyi
2020-09-29 10:28:17 -07:00
committed by GitHub
parent d693d2b9d2
commit 65a81c7cd3
13 changed files with 147 additions and 37 deletions

View File

@@ -2,9 +2,12 @@
// 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:dds/dds.dart' as dds;
import 'package:meta/meta.dart';
import 'common.dart';
import 'io.dart' as io;
import 'logger.dart';
@@ -18,6 +21,9 @@ class DartDevelopmentService {
Uri get uri => _ddsInstance.uri;
Future<void> get done => _completer.future;
final Completer<void> _completer = Completer<void>();
Future<void> startDartDevelopmentService(
Uri observatoryUri,
int hostPort,
@@ -43,9 +49,11 @@ class DartDevelopmentService {
enableAuthCodes: !disableServiceAuthCodes,
ipv6: ipv6,
);
unawaited(_ddsInstance.done.whenComplete(_completer.complete));
logger.printTrace('DDS is listening at ${_ddsInstance.uri}.');
} on dds.DartDevelopmentServiceException catch (e) {
logger.printError('Warning: Failed to start DDS: ${e.message}');
logger.printTrace('Warning: Failed to start DDS: ${e.message}');
_completer.complete();
rethrow;
}
}

View File

@@ -230,24 +230,44 @@ class FlutterDevice {
isWaitingForVm = true;
vm_service.VmService service;
if (!disableDds) {
await device.dds.startDartDevelopmentService(
observatoryUri,
ddsPort,
ipv6,
disableServiceAuthCodes,
);
// This first try block is meant to catch errors that occur during DDS startup
// (e.g., failure to bind to a port, failure to connect to the VM service,
// attaching to a VM service with existing clients, etc.).
try {
await device.dds.startDartDevelopmentService(
observatoryUri,
ddsPort,
ipv6,
disableServiceAuthCodes,
);
} on Exception catch (e) {
globals.printTrace('Fail to connect to service protocol: $observatoryUri: $e');
if (!completer.isCompleted && !_isListeningForObservatoryUri) {
completer.completeError('failed to connect to $observatoryUri');
}
return;
}
}
// This second try block handles cases where the VM service connection goes down
// before flutter_tools connects to DDS. The DDS `done` future completes when DDS
// shuts down, including after an error. If `done` completes before `connectToVmService`,
// something went wrong that caused DDS to shutdown early.
try {
service = await connectToVmService(
disableDds ? observatoryUri : device.dds.uri,
reloadSources: reloadSources,
restart: restart,
compileExpression: compileExpression,
reloadMethod: reloadMethod,
getSkSLMethod: getSkSLMethod,
printStructuredErrorLogMethod: printStructuredErrorLogMethod,
device: device,
);
service = await Future.any<dynamic>(
<Future<dynamic>>[
connectToVmService(
disableDds ? observatoryUri : device.dds.uri,
reloadSources: reloadSources,
restart: restart,
compileExpression: compileExpression,
reloadMethod: reloadMethod,
getSkSLMethod: getSkSLMethod,
printStructuredErrorLogMethod: printStructuredErrorLogMethod,
device: device,
),
device.dds.done.whenComplete(() => throw Exception('DDS shut down too early')),
]
) as vm_service.VmService;
} on Exception catch (exception) {
globals.printTrace('Fail to connect to service protocol: $observatoryUri: $exception');
if (!completer.isCompleted && !_isListeningForObservatoryUri) {

View File

@@ -11,7 +11,7 @@ dependencies:
# To update these, use "flutter update-packages --force-upgrade".
archive: 2.0.13
args: 1.6.0
dds: 1.3.5
dds: 1.4.0
dwds: 6.0.0
completion: 0.2.2
coverage: 0.14.1
@@ -113,4 +113,4 @@ dartdoc:
# Exclude this package from the hosted API docs.
nodoc: true
# PUBSPEC CHECKSUM: 8b89
# PUBSPEC CHECKSUM: 3d85

View File

@@ -90,8 +90,10 @@ void main() {
when(portForwarder.unforward(any))
.thenAnswer((_) async {});
when(device.dds).thenReturn(mockDds);
final Completer<void> noopCompleter = Completer<void>();
when(mockDds.startDartDevelopmentService(any, any, false, any)).thenReturn(null);
when(mockDds.uri).thenReturn(Uri.parse('http://localhost:8181'));
when(mockDds.done).thenAnswer((_) => noopCompleter.future);
final HttpClientRequest httpClientRequest = MockHttpClientRequest();
httpClient = MockHttpClient();
when(httpClient.putUrl(any))
@@ -299,6 +301,8 @@ void main() {
.thenReturn(portForwarder);
when(device.dds)
.thenReturn(mockDds);
final Completer<void> noopCompleter = Completer<void>();
when(mockDds.done).thenAnswer((_) => noopCompleter.future);
when(portForwarder.forward(devicePort, hostPort: anyNamed('hostPort')))
.thenAnswer((_) async => hostPort);
when(portForwarder.forwardedPorts)
@@ -379,6 +383,8 @@ void main() {
.thenReturn(portForwarder);
when(device.dds)
.thenReturn(mockDds);
final Completer<void> noopCompleter = Completer<void>();
when(mockDds.done).thenAnswer((_) => noopCompleter.future);
when(device.getLogReader(includePastLogs: anyNamed('includePastLogs')))
.thenAnswer((_) => mockLogReader);
when(portForwarder.forward(devicePort, hostPort: anyNamed('hostPort')))
@@ -449,6 +455,8 @@ void main() {
when(mockDds.startDartDevelopmentService(any, any, any, any))
.thenReturn(null);
when(mockDds.uri).thenReturn(Uri.parse('http://localhost:8181'));
final Completer<void> noopCompleter = Completer<void>();
when(mockDds.done).thenAnswer((_) => noopCompleter.future);
});
testUsingContext('succeeds in ipv4 mode', () async {

View File

@@ -2328,10 +2328,12 @@ void main() {
final MockDevice mockDevice = MockDevice();
final MockDartDevelopmentService mockDds = MockDartDevelopmentService();
final MockDeviceLogReader mockLogReader = MockDeviceLogReader();
final Completer<void> noopCompleter = Completer<void>();
when(mockDevice.getLogReader(app: anyNamed('app'))).thenReturn(mockLogReader);
when(mockDevice.dds).thenReturn(mockDds);
when(mockDds.startDartDevelopmentService(any, any, any, any)).thenReturn(null);
when(mockDds.uri).thenReturn(Uri.parse('http://localhost:8181'));
when(mockDds.done).thenAnswer((_) => noopCompleter.future);
final TestFlutterDevice flutterDevice = TestFlutterDevice(
mockDevice,

View File

@@ -0,0 +1,43 @@
// 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 'package:file/file.dart';
import '../src/common.dart';
import 'test_data/project_with_immediate_exit.dart';
import 'test_driver.dart';
import 'test_utils.dart';
void main() {
Directory tempDir;
final ProjectWithImmediateExit _project = ProjectWithImmediateExit();
FlutterRunTestDriver _flutter;
setUp(() async {
tempDir = createResolvedTempDirectorySync('run_test.');
await _project.setUpIn(tempDir);
_flutter = FlutterRunTestDriver(tempDir);
});
tearDown(() async {
tryToDelete(tempDir);
});
testWithoutContext('flutter_tools gracefully handles quick app shutdown', () async {
try {
await _flutter.run();
} on Exception {
expect(_flutter.lastErrorInfo, contains('Error connecting to the service protocol:'));
expect(
_flutter.lastErrorInfo.contains(
// Looks for stack trace entry of the form:
// test/integration.shard/test_driver.dart 379:18 FlutterTestDriver._waitFor.<fn>
RegExp('^(.+)\/([^\/]+)\.dart \d*:\d*\s*.*\$')
),
isFalse
);
}
});
}

View File

@@ -0,0 +1,31 @@
// 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 'project.dart';
class ProjectWithImmediateExit extends Project {
@override
final String pubspec = '''
name: test
environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
''';
@override
final String main = r'''
import 'dart:async';
import 'dart:io';
Future<void> main() async {
await Future.delayed(const Duration(milliseconds: 50));
exit(0);
}
''';
}

View File

@@ -376,7 +376,7 @@ abstract class FlutterTestDriver {
if (json['params'] != null && json['params']['trace'] != null) {
error.write('${json['params']['trace']}\n\n');
}
response.completeError(error.toString());
response.completeError(Exception(error.toString()));
}
});
@@ -536,7 +536,7 @@ class FlutterRunTestDriver extends FlutterTestDriver {
// fast.
unawaited(_process.exitCode.then((_) {
if (!prematureExitGuard.isCompleted) {
prematureExitGuard.completeError('Process exited prematurely: ${args.join(' ')}: $_errorBuffer');
prematureExitGuard.completeError(Exception('Process exited prematurely: ${args.join(' ')}: $_errorBuffer'));
}
}));
@@ -567,7 +567,7 @@ class FlutterRunTestDriver extends FlutterTestDriver {
_currentRunningAppId = (await started)['params']['appId'] as String;
prematureExitGuard.complete();
} on Exception catch (error, stackTrace) {
prematureExitGuard.completeError(error, stackTrace);
prematureExitGuard.completeError(Exception(error.toString()), stackTrace);
}
}());