Add connectedVmServiceUri service extension and set from flutter_tools (#74534)
* Add connectedVmServiceUri service extension and set from flutter_tools
This commit is contained in:
@@ -131,6 +131,7 @@ abstract class ResidentWebRunner extends ResidentRunner {
|
||||
@override
|
||||
Future<Map<String, dynamic>> invokeFlutterExtensionRpcRawOnFirstIsolate(
|
||||
String method, {
|
||||
FlutterDevice device,
|
||||
Map<String, dynamic> params,
|
||||
}) async {
|
||||
final vmservice.Response response =
|
||||
|
||||
@@ -846,13 +846,12 @@ abstract class ResidentRunner {
|
||||
// runner to support a single flutter device.
|
||||
Future<Map<String, dynamic>> invokeFlutterExtensionRpcRawOnFirstIsolate(
|
||||
String method, {
|
||||
FlutterDevice device,
|
||||
Map<String, dynamic> params,
|
||||
}) async {
|
||||
final List<FlutterView> views = await flutterDevices
|
||||
.first
|
||||
.vmService.getFlutterViews();
|
||||
return flutterDevices
|
||||
.first
|
||||
device ??= flutterDevices.first;
|
||||
final List<FlutterView> views = await device.vmService.getFlutterViews();
|
||||
return device
|
||||
.vmService
|
||||
.invokeFlutterExtensionRpcRaw(
|
||||
method,
|
||||
@@ -1286,21 +1285,60 @@ abstract class ResidentRunner {
|
||||
|
||||
Future<void> maybeCallDevToolsUriServiceExtension() async {
|
||||
_devToolsLauncher ??= DevtoolsLauncher.instance;
|
||||
if (_devToolsLauncher.activeDevToolsServer != null) {
|
||||
|
||||
await Future.wait(<Future<vm_service.Isolate>>[
|
||||
if (_devToolsLauncher?.activeDevToolsServer != null) {
|
||||
await Future.wait(<Future<void>>[
|
||||
for (final FlutterDevice device in flutterDevices)
|
||||
waitForExtension(device.vmService, 'ext.flutter.activeDevToolsServerAddress'),
|
||||
_callDevToolsUriExtension(device),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _callDevToolsUriExtension(FlutterDevice device) async {
|
||||
if (_devToolsLauncher == null) {
|
||||
return;
|
||||
}
|
||||
await waitForExtension(device.vmService, 'ext.flutter.activeDevToolsServerAddress');
|
||||
try {
|
||||
unawaited(invokeFlutterExtensionRpcRawOnFirstIsolate(
|
||||
'ext.flutter.activeDevToolsServerAddress',
|
||||
device: device,
|
||||
params: <String, dynamic>{
|
||||
'value': _devToolsLauncher.activeDevToolsServer.uri.toString(),
|
||||
},
|
||||
));
|
||||
} on Exception catch (e) {
|
||||
globals.printError(
|
||||
'Failed to set DevTools server address: ${e.toString()}. Deep links to'
|
||||
' DevTools will not show in Flutter errors.',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> callConnectedVmServiceUriExtension() async {
|
||||
await Future.wait(<Future<void>>[
|
||||
for (final FlutterDevice device in flutterDevices)
|
||||
_callConnectedVmServiceExtension(device),
|
||||
]);
|
||||
}
|
||||
|
||||
Future<void> _callConnectedVmServiceExtension(FlutterDevice device) async {
|
||||
if (device.vmService.httpAddress != null || device.vmService.wsAddress != null) {
|
||||
final Uri uri = device.vmService.httpAddress ?? device.vmService.wsAddress;
|
||||
await waitForExtension(device.vmService, 'ext.flutter.connectedVmServiceUri');
|
||||
try {
|
||||
unawaited(invokeFlutterExtensionRpcRawOnFirstIsolate(
|
||||
'ext.flutter.activeDevToolsServerAddress',
|
||||
'ext.flutter.connectedVmServiceUri',
|
||||
device: device,
|
||||
params: <String, dynamic>{
|
||||
'value': _devToolsLauncher.activeDevToolsServer.uri.toString(),
|
||||
'value': uri.toString(),
|
||||
},
|
||||
));
|
||||
} on Exception catch (e) {
|
||||
globals.printError(e.toString());
|
||||
globals.printError(
|
||||
'Failed to set vm service URI: ${e.toString()}. Deep links to DevTools'
|
||||
' will not show in Flutter errors.',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,6 +123,7 @@ class ColdRunner extends ResidentRunner {
|
||||
|
||||
if (debuggingEnabled) {
|
||||
unawaited(maybeCallDevToolsUriServiceExtension());
|
||||
unawaited(callConnectedVmServiceUriExtension());
|
||||
}
|
||||
|
||||
appStartedCompleter?.complete();
|
||||
@@ -168,6 +169,7 @@ class ColdRunner extends ResidentRunner {
|
||||
}
|
||||
|
||||
unawaited(maybeCallDevToolsUriServiceExtension());
|
||||
unawaited(callConnectedVmServiceUriExtension());
|
||||
|
||||
appStartedCompleter?.complete();
|
||||
if (stayResident) {
|
||||
|
||||
@@ -217,6 +217,7 @@ class HotRunner extends ResidentRunner {
|
||||
}
|
||||
|
||||
unawaited(maybeCallDevToolsUriServiceExtension());
|
||||
unawaited(callConnectedVmServiceUriExtension());
|
||||
|
||||
final Stopwatch initialUpdateDevFSsTimer = Stopwatch()..start();
|
||||
final UpdateFSReport devfsResult = await _updateDevFS(fullRestart: true);
|
||||
@@ -635,6 +636,7 @@ class HotRunner extends ResidentRunner {
|
||||
globals.printStatus('Restarted application in ${getElapsedAsMilliseconds(timer.elapsed)}.');
|
||||
}
|
||||
unawaited(maybeCallDevToolsUriServiceExtension());
|
||||
unawaited(callConnectedVmServiceUriExtension());
|
||||
return result;
|
||||
}
|
||||
final OperationResult result = await _hotReloadHelper(
|
||||
|
||||
@@ -44,6 +44,7 @@ final vm_service.Isolate fakeUnpausedIsolate = vm_service.Isolate(
|
||||
),
|
||||
breakpoints: <vm_service.Breakpoint>[],
|
||||
exceptionPauseMode: null,
|
||||
extensionRPCs: <String>[],
|
||||
libraries: <vm_service.LibraryRef>[
|
||||
vm_service.LibraryRef(
|
||||
id: '1',
|
||||
@@ -128,6 +129,11 @@ const FakeVmServiceRequest setAssetBundlePath = FakeVmServiceRequest(
|
||||
}
|
||||
);
|
||||
|
||||
const FakeVmServiceRequest listenToExtensionStream = FakeVmServiceRequest(
|
||||
method: 'streamListen',
|
||||
args: <String, Object>{'streamId': 'Extension'},
|
||||
);
|
||||
|
||||
void main() {
|
||||
final Uri testUri = Uri.parse('foo://bar');
|
||||
Testbed testbed;
|
||||
@@ -2384,7 +2390,19 @@ void main() {
|
||||
testUsingContext('HotRunner writes vm service file when providing debugging option', () => testbed.run(() async {
|
||||
fakeVmServiceHost = FakeVmServiceHost(requests: <VmServiceExpectation>[
|
||||
listViews,
|
||||
listenToExtensionStream,
|
||||
FakeVmServiceRequest(
|
||||
method: 'getVM',
|
||||
jsonResponse: fakeVM.toJson(),
|
||||
),
|
||||
listViews,
|
||||
FakeVmServiceRequest(
|
||||
method: 'getIsolate',
|
||||
args: <String, Object>{
|
||||
'isolateId': '1',
|
||||
},
|
||||
jsonResponse: fakeUnpausedIsolate.toJson(),
|
||||
),
|
||||
setAssetBundlePath,
|
||||
]);
|
||||
setWsAddress(testUri, fakeVmServiceHost.vmService);
|
||||
@@ -2413,7 +2431,19 @@ void main() {
|
||||
testUsingContext('HotRunner copies compiled app.dill to cache during startup', () => testbed.run(() async {
|
||||
fakeVmServiceHost = FakeVmServiceHost(requests: <VmServiceExpectation>[
|
||||
listViews,
|
||||
listenToExtensionStream,
|
||||
FakeVmServiceRequest(
|
||||
method: 'getVM',
|
||||
jsonResponse: fakeVM.toJson(),
|
||||
),
|
||||
listViews,
|
||||
FakeVmServiceRequest(
|
||||
method: 'getIsolate',
|
||||
args: <String, Object>{
|
||||
'isolateId': '1',
|
||||
},
|
||||
jsonResponse: fakeUnpausedIsolate.toJson(),
|
||||
),
|
||||
setAssetBundlePath,
|
||||
]);
|
||||
setWsAddress(testUri, fakeVmServiceHost.vmService);
|
||||
@@ -2443,7 +2473,19 @@ void main() {
|
||||
testUsingContext('HotRunner copies compiled app.dill to cache during startup with dart defines', () => testbed.run(() async {
|
||||
fakeVmServiceHost = FakeVmServiceHost(requests: <VmServiceExpectation>[
|
||||
listViews,
|
||||
listenToExtensionStream,
|
||||
FakeVmServiceRequest(
|
||||
method: 'getVM',
|
||||
jsonResponse: fakeVM.toJson(),
|
||||
),
|
||||
listViews,
|
||||
FakeVmServiceRequest(
|
||||
method: 'getIsolate',
|
||||
args: <String, Object>{
|
||||
'isolateId': '1',
|
||||
},
|
||||
jsonResponse: fakeUnpausedIsolate.toJson(),
|
||||
),
|
||||
setAssetBundlePath,
|
||||
]);
|
||||
setWsAddress(testUri, fakeVmServiceHost.vmService);
|
||||
@@ -2481,7 +2523,19 @@ void main() {
|
||||
testUsingContext('HotRunner copies compiled app.dill to cache during startup with null safety', () => testbed.run(() async {
|
||||
fakeVmServiceHost = FakeVmServiceHost(requests: <VmServiceExpectation>[
|
||||
listViews,
|
||||
listenToExtensionStream,
|
||||
FakeVmServiceRequest(
|
||||
method: 'getVM',
|
||||
jsonResponse: fakeVM.toJson(),
|
||||
),
|
||||
listViews,
|
||||
FakeVmServiceRequest(
|
||||
method: 'getIsolate',
|
||||
args: <String, Object>{
|
||||
'isolateId': '1',
|
||||
},
|
||||
jsonResponse: fakeUnpausedIsolate.toJson(),
|
||||
),
|
||||
setAssetBundlePath,
|
||||
]);
|
||||
setWsAddress(testUri, fakeVmServiceHost.vmService);
|
||||
@@ -2519,7 +2573,19 @@ void main() {
|
||||
testUsingContext('HotRunner does not copy app.dill if a dillOutputPath is given', () => testbed.run(() async {
|
||||
fakeVmServiceHost = FakeVmServiceHost(requests: <VmServiceExpectation>[
|
||||
listViews,
|
||||
listenToExtensionStream,
|
||||
FakeVmServiceRequest(
|
||||
method: 'getVM',
|
||||
jsonResponse: fakeVM.toJson(),
|
||||
),
|
||||
listViews,
|
||||
FakeVmServiceRequest(
|
||||
method: 'getIsolate',
|
||||
args: <String, Object>{
|
||||
'isolateId': '1',
|
||||
},
|
||||
jsonResponse: fakeUnpausedIsolate.toJson(),
|
||||
),
|
||||
setAssetBundlePath,
|
||||
]);
|
||||
setWsAddress(testUri, fakeVmServiceHost.vmService);
|
||||
@@ -2550,7 +2616,19 @@ void main() {
|
||||
testUsingContext('HotRunner copies compiled app.dill to cache during startup with --track-widget-creation', () => testbed.run(() async {
|
||||
fakeVmServiceHost = FakeVmServiceHost(requests: <VmServiceExpectation>[
|
||||
listViews,
|
||||
listenToExtensionStream,
|
||||
FakeVmServiceRequest(
|
||||
method: 'getVM',
|
||||
jsonResponse: fakeVM.toJson(),
|
||||
),
|
||||
listViews,
|
||||
FakeVmServiceRequest(
|
||||
method: 'getIsolate',
|
||||
args: <String, Object>{
|
||||
'isolateId': '1',
|
||||
},
|
||||
jsonResponse: fakeUnpausedIsolate.toJson(),
|
||||
),
|
||||
setAssetBundlePath,
|
||||
]);
|
||||
setWsAddress(testUri, fakeVmServiceHost.vmService);
|
||||
@@ -2652,6 +2730,18 @@ void main() {
|
||||
testUsingContext('ColdRunner writes vm service file when providing debugging option', () => testbed.run(() async {
|
||||
fakeVmServiceHost = FakeVmServiceHost(requests: <VmServiceExpectation>[
|
||||
listViews,
|
||||
listenToExtensionStream,
|
||||
FakeVmServiceRequest(
|
||||
method: 'getVM',
|
||||
jsonResponse: fakeVM.toJson(),
|
||||
),
|
||||
FakeVmServiceRequest(
|
||||
method: 'getIsolate',
|
||||
args: <String, Object>{
|
||||
'isolateId': '1',
|
||||
},
|
||||
jsonResponse: fakeUnpausedIsolate.toJson(),
|
||||
),
|
||||
]);
|
||||
globals.fs.file(globals.fs.path.join('lib', 'main.dart')).createSync(recursive: true);
|
||||
setWsAddress(testUri, fakeVmServiceHost.vmService);
|
||||
@@ -2670,6 +2760,9 @@ void main() {
|
||||
return 0;
|
||||
});
|
||||
await residentRunner.run();
|
||||
// Await a short delay so that we don't try to exit before all the expected
|
||||
// VM service requests have been fired.
|
||||
await Future<void>.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
expect(await globals.fs.file('foo').readAsString(), testUri.toString());
|
||||
expect(fakeVmServiceHost.hasRemainingExpectations, false);
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:file/file.dart';
|
||||
import 'package:vm_service/vm_service.dart';
|
||||
|
||||
import '../src/common.dart';
|
||||
import 'test_data/basic_project.dart';
|
||||
@@ -77,25 +78,41 @@ void main() {
|
||||
await _flutterRun.run(
|
||||
startPaused: true,
|
||||
withDebugger: true,
|
||||
additionalCommandArgs: <String>['--devtools-server-address', 'http://127.0.0.1:9105'],
|
||||
);
|
||||
await _flutterRun.resume();
|
||||
await pollForServiceExtensionValue(
|
||||
await pollForServiceExtensionValue<String>(
|
||||
testDriver: _flutterRun,
|
||||
extension: 'ext.flutter.activeDevToolsServerAddress',
|
||||
continuePollingValue: '',
|
||||
expectedValue: 'http://127.0.0.1:9100',
|
||||
matches: equals('http://127.0.0.1:9105'),
|
||||
);
|
||||
await pollForServiceExtensionValue<String>(
|
||||
testDriver: _flutterRun,
|
||||
extension: 'ext.flutter.connectedVmServiceUri',
|
||||
continuePollingValue: '',
|
||||
matches: isNotEmpty,
|
||||
);
|
||||
|
||||
final Response response = await _flutterRun.callServiceExtension('ext.flutter.connectedVmServiceUri');
|
||||
final String vmServiceUri = response.json['value'] as String;
|
||||
|
||||
// Attach with a different DevTools server address.
|
||||
await _flutterAttach.attach(
|
||||
_flutterRun.vmServicePort,
|
||||
additionalCommandArgs: <String>['--devtools-server-address', 'http://127.0.0.1:9110'],
|
||||
);
|
||||
await pollForServiceExtensionValue(
|
||||
await pollForServiceExtensionValue<String>(
|
||||
testDriver: _flutterAttach,
|
||||
extension: 'ext.flutter.activeDevToolsServerAddress',
|
||||
continuePollingValue: '',
|
||||
expectedValue: 'http://127.0.0.1:9110',
|
||||
matches: equals('http://127.0.0.1:9110'),
|
||||
);
|
||||
});
|
||||
await pollForServiceExtensionValue<String>(
|
||||
testDriver: _flutterRun,
|
||||
extension: 'ext.flutter.connectedVmServiceUri',
|
||||
continuePollingValue: '',
|
||||
matches: equals(vmServiceUri),
|
||||
);
|
||||
}, timeout: const Timeout.factor(4));
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import 'package:flutter_tools/src/base/io.dart';
|
||||
import 'package:process/process.dart';
|
||||
|
||||
import '../src/common.dart';
|
||||
import '../src/context.dart';
|
||||
import 'test_data/basic_project.dart';
|
||||
import 'test_driver.dart';
|
||||
import 'test_utils.dart';
|
||||
@@ -61,11 +62,17 @@ void main() {
|
||||
additionalCommandArgs: <String>['--devtools-server-address', 'http://127.0.0.1:9110'],
|
||||
);
|
||||
await _flutter.resume();
|
||||
await pollForServiceExtensionValue(
|
||||
await pollForServiceExtensionValue<String>(
|
||||
testDriver: _flutter,
|
||||
extension: 'ext.flutter.activeDevToolsServerAddress',
|
||||
continuePollingValue: '',
|
||||
expectedValue: 'http://127.0.0.1:9110',
|
||||
matches: equals('http://127.0.0.1:9110'),
|
||||
);
|
||||
});
|
||||
await pollForServiceExtensionValue<String>(
|
||||
testDriver: _flutter,
|
||||
extension: 'ext.flutter.connectedVmServiceUri',
|
||||
continuePollingValue: '',
|
||||
matches: isNotEmpty,
|
||||
);
|
||||
}, timeout: const Timeout.factor(4));
|
||||
}
|
||||
|
||||
@@ -53,6 +53,7 @@ abstract class FlutterTestDriver {
|
||||
final StringBuffer _errorBuffer = StringBuffer();
|
||||
String _lastResponse;
|
||||
Uri _vmServiceWsUri;
|
||||
int _attachPort;
|
||||
bool _hasExited = false;
|
||||
|
||||
VmService _vmService;
|
||||
@@ -173,8 +174,9 @@ abstract class FlutterTestDriver {
|
||||
String extension, {
|
||||
Map<String, dynamic> args = const <String, dynamic>{},
|
||||
}) async {
|
||||
final VmService vmService = await vmServiceConnectUri('ws://localhost:$vmServicePort/ws');
|
||||
final Isolate isolate = await waitForExtension(vmService, 'ext.flutter.activeDevToolsServerAddress');
|
||||
final int port = _vmServiceWsUri != null ? vmServicePort : _attachPort;
|
||||
final VmService vmService = await vmServiceConnectUri('ws://localhost:$port/ws');
|
||||
final Isolate isolate = await waitForExtension(vmService, extension);
|
||||
return await vmService.callServiceExtension(
|
||||
extension,
|
||||
isolateId: isolate.id,
|
||||
@@ -508,6 +510,7 @@ class FlutterRunTestDriver extends FlutterTestDriver {
|
||||
bool singleWidgetReloads = false,
|
||||
List<String> additionalCommandArgs,
|
||||
}) async {
|
||||
_attachPort = port;
|
||||
await _setupProcess(
|
||||
<String>[
|
||||
'attach',
|
||||
@@ -526,6 +529,7 @@ class FlutterRunTestDriver extends FlutterTestDriver {
|
||||
pauseOnExceptions: pauseOnExceptions,
|
||||
pidFile: pidFile,
|
||||
singleWidgetReloads: singleWidgetReloads,
|
||||
attachPort: port,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -538,6 +542,7 @@ class FlutterRunTestDriver extends FlutterTestDriver {
|
||||
bool pauseOnExceptions = false,
|
||||
bool singleWidgetReloads = false,
|
||||
File pidFile,
|
||||
int attachPort,
|
||||
}) async {
|
||||
assert(!startPaused || withDebugger);
|
||||
await super._setupProcess(
|
||||
@@ -582,6 +587,13 @@ class FlutterRunTestDriver extends FlutterTestDriver {
|
||||
}
|
||||
}
|
||||
|
||||
// In order to call service extensions from test runners started with
|
||||
// attach, we need to store the port that the test runner was attached
|
||||
// to.
|
||||
if (_vmServiceWsUri == null && attachPort != null) {
|
||||
_attachPort = attachPort;
|
||||
}
|
||||
|
||||
// Now await the started event; if it had already happened the future will
|
||||
// have already completed.
|
||||
_currentRunningAppId = (await started)['params']['appId'] as String;
|
||||
|
||||
@@ -81,16 +81,20 @@ Future<void> pollForServiceExtensionValue<T>({
|
||||
@required FlutterTestDriver testDriver,
|
||||
@required String extension,
|
||||
@required T continuePollingValue,
|
||||
@required T expectedValue,
|
||||
@required Matcher matches,
|
||||
String valueKey = 'value',
|
||||
}) async {
|
||||
for (int i = 10; i < 10; i++) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
final Response response = await testDriver.callServiceExtension(extension);
|
||||
if (response.json[valueKey] == continuePollingValue) {
|
||||
if (response.json[valueKey] as T == continuePollingValue) {
|
||||
await Future<void>.delayed(const Duration(seconds: 1));
|
||||
} else {
|
||||
expect(response.json[valueKey], equals(expectedValue));
|
||||
break;
|
||||
expect(response.json[valueKey] as T, matches);
|
||||
return;
|
||||
}
|
||||
}
|
||||
fail(
|
||||
'Did not find expected value for service extension \'$extension\'. All call'
|
||||
' attempts responded with \'$continuePollingValue\'.',
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user