Unverified Commit 2d550329 authored by Jenn Magder's avatar Jenn Magder Committed by GitHub

Migrate dds.dart to null safety (#88382)

parent cc380b9c
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart = 2.8
import 'dart:async'; import 'dart:async';
import 'package:dds/dds.dart' as dds; import 'package:dds/dds.dart' as dds;
...@@ -15,36 +13,33 @@ import 'logger.dart'; ...@@ -15,36 +13,33 @@ import 'logger.dart';
@visibleForTesting @visibleForTesting
Future<dds.DartDevelopmentService> Function( Future<dds.DartDevelopmentService> Function(
Uri, Uri remoteVmServiceUri, {
{bool enableAuthCodes, bool enableAuthCodes,
bool ipv6, bool ipv6,
Uri serviceUri, Uri? serviceUri,
}) ddsLauncherCallback = dds.DartDevelopmentService.startDartDevelopmentService; }) ddsLauncherCallback = dds.DartDevelopmentService.startDartDevelopmentService;
/// Helper class to launch a [dds.DartDevelopmentService]. Allows for us to /// Helper class to launch a [dds.DartDevelopmentService]. Allows for us to
/// mock out this functionality for testing purposes. /// mock out this functionality for testing purposes.
class DartDevelopmentService { class DartDevelopmentService {
dds.DartDevelopmentService _ddsInstance; dds.DartDevelopmentService? _ddsInstance;
Uri get uri => _ddsInstance?.uri ?? _existingDdsUri; Uri? get uri => _ddsInstance?.uri ?? _existingDdsUri;
Uri _existingDdsUri; Uri? _existingDdsUri;
Future<void> get done => _completer.future; Future<void> get done => _completer.future;
final Completer<void> _completer = Completer<void>(); final Completer<void> _completer = Completer<void>();
Future<void> startDartDevelopmentService( Future<void> startDartDevelopmentService(
Uri observatoryUri, Uri observatoryUri, {
int hostPort, required Logger logger,
bool ipv6, int? hostPort,
bool disableServiceAuthCodes, { bool? ipv6,
@required Logger logger, bool? disableServiceAuthCodes,
}) async { }) async {
final Uri ddsUri = Uri( final Uri ddsUri = Uri(
scheme: 'http', scheme: 'http',
host: (ipv6 ? host: (ipv6 == true ? io.InternetAddress.loopbackIPv6 : io.InternetAddress.loopbackIPv4).host,
io.InternetAddress.loopbackIPv6 :
io.InternetAddress.loopbackIPv4
).host,
port: hostPort ?? 0, port: hostPort ?? 0,
); );
logger.printTrace( logger.printTrace(
...@@ -55,15 +50,15 @@ class DartDevelopmentService { ...@@ -55,15 +50,15 @@ class DartDevelopmentService {
_ddsInstance = await ddsLauncherCallback( _ddsInstance = await ddsLauncherCallback(
observatoryUri, observatoryUri,
serviceUri: ddsUri, serviceUri: ddsUri,
enableAuthCodes: !disableServiceAuthCodes, enableAuthCodes: disableServiceAuthCodes != true,
ipv6: ipv6, ipv6: ipv6 == true,
); );
unawaited(_ddsInstance.done.whenComplete(() { unawaited(_ddsInstance?.done.whenComplete(() {
if (!_completer.isCompleted) { if (!_completer.isCompleted) {
_completer.complete(); _completer.complete();
} }
})); }));
logger.printTrace('DDS is listening at ${_ddsInstance.uri}.'); logger.printTrace('DDS is listening at ${_ddsInstance?.uri}.');
} on dds.DartDevelopmentServiceException catch (e) { } on dds.DartDevelopmentServiceException catch (e) {
logger.printTrace('Warning: Failed to start DDS: ${e.message}'); logger.printTrace('Warning: Failed to start DDS: ${e.message}');
if (e.errorCode == dds.DartDevelopmentServiceException.existingDdsInstanceError) { if (e.errorCode == dds.DartDevelopmentServiceException.existingDdsInstanceError) {
......
...@@ -225,9 +225,9 @@ class FlutterDriverService extends DriverService { ...@@ -225,9 +225,9 @@ class FlutterDriverService extends DriverService {
try { try {
await device.dds.startDartDevelopmentService( await device.dds.startDartDevelopmentService(
uri, uri,
debuggingOptions.ddsPort, hostPort: debuggingOptions.ddsPort,
ipv6, ipv6: ipv6,
debuggingOptions.disableServiceAuthCodes, disableServiceAuthCodes: debuggingOptions.disableServiceAuthCodes,
logger: _logger, logger: _logger,
); );
_vmServiceUri = device.dds.uri.toString(); _vmServiceUri = device.dds.uri.toString();
......
...@@ -65,9 +65,9 @@ Future<void> _kDefaultDartDevelopmentServiceStarter( ...@@ -65,9 +65,9 @@ Future<void> _kDefaultDartDevelopmentServiceStarter(
) async { ) async {
await device.dds.startDartDevelopmentService( await device.dds.startDartDevelopmentService(
observatoryUri, observatoryUri,
0, hostPort: 0,
true, ipv6: true,
disableServiceAuthCodes, disableServiceAuthCodes: disableServiceAuthCodes,
logger: globals.logger, logger: globals.logger,
); );
} }
......
...@@ -263,9 +263,9 @@ class FlutterDevice { ...@@ -263,9 +263,9 @@ class FlutterDevice {
try { try {
await device.dds.startDartDevelopmentService( await device.dds.startDartDevelopmentService(
observatoryUri, observatoryUri,
ddsPort, hostPort: ddsPort,
ipv6, ipv6: ipv6,
disableServiceAuthCodes, disableServiceAuthCodes: disableServiceAuthCodes,
logger: globals.logger, logger: globals.logger,
); );
} on dds.DartDevelopmentServiceException catch (e, st) { } on dds.DartDevelopmentServiceException catch (e, st) {
......
...@@ -767,11 +767,11 @@ class FakeDartDevelopmentService extends Fake implements DartDevelopmentService ...@@ -767,11 +767,11 @@ class FakeDartDevelopmentService extends Fake implements DartDevelopmentService
@override @override
Future<void> startDartDevelopmentService( Future<void> startDartDevelopmentService(
Uri observatoryUri, Uri observatoryUri, {
@required Logger logger,
int hostPort, int hostPort,
bool ipv6, bool ipv6,
bool disableServiceAuthCodes, { bool disableServiceAuthCodes,
@required Logger logger,
}) async {} }) async {}
@override @override
......
...@@ -605,11 +605,11 @@ class FakeDartDevelopmentService extends Fake implements DartDevelopmentService ...@@ -605,11 +605,11 @@ class FakeDartDevelopmentService extends Fake implements DartDevelopmentService
@override @override
Future<void> startDartDevelopmentService( Future<void> startDartDevelopmentService(
Uri observatoryUri, Uri observatoryUri, {
@required Logger logger,
int hostPort, int hostPort,
bool ipv6, bool ipv6,
bool disableServiceAuthCodes, { bool disableServiceAuthCodes,
@required Logger logger,
}) async { }) async {
started = true; started = true;
} }
......
...@@ -1008,7 +1008,13 @@ class FakeFuchsiaSdk extends Fake implements FuchsiaSdk { ...@@ -1008,7 +1008,13 @@ class FakeFuchsiaSdk extends Fake implements FuchsiaSdk {
class FakeDartDevelopmentService extends Fake implements DartDevelopmentService { class FakeDartDevelopmentService extends Fake implements DartDevelopmentService {
@override @override
Future<void> startDartDevelopmentService(Uri observatoryUri, int hostPort, bool ipv6, bool disableServiceAuthCodes, {Logger logger}) async { } Future<void> startDartDevelopmentService(
Uri observatoryUri, {
@required Logger logger,
int hostPort,
bool ipv6,
bool disableServiceAuthCodes,
}) async {}
@override @override
Uri get uri => Uri.parse('example'); Uri get uri => Uri.parse('example');
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
// @dart = 2.8 // @dart = 2.8
import 'dart:async'; import 'dart:async';
import 'dart:io';
import 'package:dds/dds.dart' as dds; import 'package:dds/dds.dart' as dds;
import 'package:file/memory.dart'; import 'package:file/memory.dart';
...@@ -1806,6 +1807,10 @@ void main() { ...@@ -1806,6 +1807,10 @@ void main() {
final FakeDevice device = FakeDevice() final FakeDevice device = FakeDevice()
..dds = DartDevelopmentService(); ..dds = DartDevelopmentService();
ddsLauncherCallback = (Uri uri, {bool enableAuthCodes, bool ipv6, Uri serviceUri}) { ddsLauncherCallback = (Uri uri, {bool enableAuthCodes, bool ipv6, Uri serviceUri}) {
expect(uri, Uri(scheme: 'foo', host: 'bar'));
expect(enableAuthCodes, isTrue);
expect(ipv6, isFalse);
expect(serviceUri, Uri(scheme: 'http', host: '127.0.0.1', port: 0));
throw FakeDartDevelopmentServiceException(message: throw FakeDartDevelopmentServiceException(message:
'Existing VM service clients prevent DDS from taking control.', 'Existing VM service clients prevent DDS from taking control.',
); );
...@@ -1843,11 +1848,47 @@ void main() { ...@@ -1843,11 +1848,47 @@ void main() {
}) async => FakeVmServiceHost(requests: <VmServiceExpectation>[]).vmService, }) async => FakeVmServiceHost(requests: <VmServiceExpectation>[]).vmService,
})); }));
testUsingContext('Host VM service ipv6 defaults', () => testbed.run(() async {
fakeVmServiceHost = FakeVmServiceHost(requests: <VmServiceExpectation>[]);
final FakeDevice device = FakeDevice()
..dds = DartDevelopmentService();
final Completer<void>done = Completer<void>();
ddsLauncherCallback = (Uri uri, {bool enableAuthCodes, bool ipv6, Uri serviceUri}) async {
expect(uri, Uri(scheme: 'foo', host: 'bar'));
expect(enableAuthCodes, isFalse);
expect(ipv6, isTrue);
expect(serviceUri, Uri(scheme: 'http', host: '::1', port: 0));
done.complete();
return null;
};
final TestFlutterDevice flutterDevice = TestFlutterDevice(
device,
observatoryUris: Stream<Uri>.value(testUri),
);
await flutterDevice.connect(allowExistingDdsInstance: true, ipv6: true, disableServiceAuthCodes: true);
await done.future;
}, overrides: <Type, Generator>{
VMServiceConnector: () => (Uri httpUri, {
ReloadSources reloadSources,
Restart restart,
CompileExpression compileExpression,
GetSkSLMethod getSkSLMethod,
PrintStructuredErrorLogMethod printStructuredErrorLogMethod,
io.CompressionOptions compression,
Device device,
Logger logger,
}) async => FakeVmServiceHost(requests: <VmServiceExpectation>[]).vmService,
}));
testUsingContext('Failed DDS start outputs error message', () => testbed.run(() async { testUsingContext('Failed DDS start outputs error message', () => testbed.run(() async {
// See https://github.com/flutter/flutter/issues/72385 for context. // See https://github.com/flutter/flutter/issues/72385 for context.
final FakeDevice device = FakeDevice() final FakeDevice device = FakeDevice()
..dds = DartDevelopmentService(); ..dds = DartDevelopmentService();
ddsLauncherCallback = (Uri uri, {bool enableAuthCodes, bool ipv6, Uri serviceUri}) { ddsLauncherCallback = (Uri uri, {bool enableAuthCodes, bool ipv6, Uri serviceUri}) {
expect(uri, Uri(scheme: 'foo', host: 'bar'));
expect(enableAuthCodes, isTrue);
expect(ipv6, isFalse);
expect(serviceUri, Uri(scheme: 'http', host: '127.0.0.1', port: 0));
throw FakeDartDevelopmentServiceException(message: 'No URI'); throw FakeDartDevelopmentServiceException(message: 'No URI');
}; };
final TestFlutterDevice flutterDevice = TestFlutterDevice( final TestFlutterDevice flutterDevice = TestFlutterDevice(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment