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 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart = 2.8
import 'dart:async';
import 'package:dds/dds.dart' as dds;
......@@ -15,36 +13,33 @@ import 'logger.dart';
@visibleForTesting
Future<dds.DartDevelopmentService> Function(
Uri,
{bool enableAuthCodes,
Uri remoteVmServiceUri, {
bool enableAuthCodes,
bool ipv6,
Uri serviceUri,
Uri? serviceUri,
}) ddsLauncherCallback = dds.DartDevelopmentService.startDartDevelopmentService;
/// Helper class to launch a [dds.DartDevelopmentService]. Allows for us to
/// mock out this functionality for testing purposes.
class DartDevelopmentService {
dds.DartDevelopmentService _ddsInstance;
dds.DartDevelopmentService? _ddsInstance;
Uri get uri => _ddsInstance?.uri ?? _existingDdsUri;
Uri _existingDdsUri;
Uri? get uri => _ddsInstance?.uri ?? _existingDdsUri;
Uri? _existingDdsUri;
Future<void> get done => _completer.future;
final Completer<void> _completer = Completer<void>();
Future<void> startDartDevelopmentService(
Uri observatoryUri,
int hostPort,
bool ipv6,
bool disableServiceAuthCodes, {
@required Logger logger,
Uri observatoryUri, {
required Logger logger,
int? hostPort,
bool? ipv6,
bool? disableServiceAuthCodes,
}) async {
final Uri ddsUri = Uri(
scheme: 'http',
host: (ipv6 ?
io.InternetAddress.loopbackIPv6 :
io.InternetAddress.loopbackIPv4
).host,
host: (ipv6 == true ? io.InternetAddress.loopbackIPv6 : io.InternetAddress.loopbackIPv4).host,
port: hostPort ?? 0,
);
logger.printTrace(
......@@ -55,15 +50,15 @@ class DartDevelopmentService {
_ddsInstance = await ddsLauncherCallback(
observatoryUri,
serviceUri: ddsUri,
enableAuthCodes: !disableServiceAuthCodes,
ipv6: ipv6,
enableAuthCodes: disableServiceAuthCodes != true,
ipv6: ipv6 == true,
);
unawaited(_ddsInstance.done.whenComplete(() {
unawaited(_ddsInstance?.done.whenComplete(() {
if (!_completer.isCompleted) {
_completer.complete();
}
}));
logger.printTrace('DDS is listening at ${_ddsInstance.uri}.');
logger.printTrace('DDS is listening at ${_ddsInstance?.uri}.');
} on dds.DartDevelopmentServiceException catch (e) {
logger.printTrace('Warning: Failed to start DDS: ${e.message}');
if (e.errorCode == dds.DartDevelopmentServiceException.existingDdsInstanceError) {
......
......@@ -225,9 +225,9 @@ class FlutterDriverService extends DriverService {
try {
await device.dds.startDartDevelopmentService(
uri,
debuggingOptions.ddsPort,
ipv6,
debuggingOptions.disableServiceAuthCodes,
hostPort: debuggingOptions.ddsPort,
ipv6: ipv6,
disableServiceAuthCodes: debuggingOptions.disableServiceAuthCodes,
logger: _logger,
);
_vmServiceUri = device.dds.uri.toString();
......
......@@ -65,9 +65,9 @@ Future<void> _kDefaultDartDevelopmentServiceStarter(
) async {
await device.dds.startDartDevelopmentService(
observatoryUri,
0,
true,
disableServiceAuthCodes,
hostPort: 0,
ipv6: true,
disableServiceAuthCodes: disableServiceAuthCodes,
logger: globals.logger,
);
}
......
......@@ -263,9 +263,9 @@ class FlutterDevice {
try {
await device.dds.startDartDevelopmentService(
observatoryUri,
ddsPort,
ipv6,
disableServiceAuthCodes,
hostPort: ddsPort,
ipv6: ipv6,
disableServiceAuthCodes: disableServiceAuthCodes,
logger: globals.logger,
);
} on dds.DartDevelopmentServiceException catch (e, st) {
......
......@@ -767,11 +767,11 @@ class FakeDartDevelopmentService extends Fake implements DartDevelopmentService
@override
Future<void> startDartDevelopmentService(
Uri observatoryUri,
Uri observatoryUri, {
@required Logger logger,
int hostPort,
bool ipv6,
bool disableServiceAuthCodes, {
@required Logger logger,
bool disableServiceAuthCodes,
}) async {}
@override
......
......@@ -605,11 +605,11 @@ class FakeDartDevelopmentService extends Fake implements DartDevelopmentService
@override
Future<void> startDartDevelopmentService(
Uri observatoryUri,
Uri observatoryUri, {
@required Logger logger,
int hostPort,
bool ipv6,
bool disableServiceAuthCodes, {
@required Logger logger,
bool disableServiceAuthCodes,
}) async {
started = true;
}
......
......@@ -1008,7 +1008,13 @@ class FakeFuchsiaSdk extends Fake implements FuchsiaSdk {
class FakeDartDevelopmentService extends Fake implements DartDevelopmentService {
@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
Uri get uri => Uri.parse('example');
......
......@@ -5,6 +5,7 @@
// @dart = 2.8
import 'dart:async';
import 'dart:io';
import 'package:dds/dds.dart' as dds;
import 'package:file/memory.dart';
......@@ -1806,6 +1807,10 @@ void main() {
final FakeDevice device = FakeDevice()
..dds = DartDevelopmentService();
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:
'Existing VM service clients prevent DDS from taking control.',
);
......@@ -1843,11 +1848,47 @@ void main() {
}) 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 {
// See https://github.com/flutter/flutter/issues/72385 for context.
final FakeDevice device = FakeDevice()
..dds = DartDevelopmentService();
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');
};
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