From 39daab2900d729bc7a2290d932de0da16ea5a660 Mon Sep 17 00:00:00 2001 From: Andrew Davies Date: Fri, 5 Oct 2018 12:31:05 -0700 Subject: [PATCH] [frdp] Add optional dart isolate filter. (#22590) * [frdp] Add optional dart isolate filter. It's now default to filter out non-flutter isolates when searching across Dart VM's. This is due to a possible issue wherein an Isolate for testing might have the same name as the flutter Isolate. In addition, logging messages have been added in dart_vm.dart for debugging. --- .../lib/src/dart/dart_vm.dart | 10 +++- .../test/src/dart/dart_vm_test.dart | 46 +++++++++++-------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/packages/fuchsia_remote_debug_protocol/lib/src/dart/dart_vm.dart b/packages/fuchsia_remote_debug_protocol/lib/src/dart/dart_vm.dart index 178ad9ea98..a2a60639ff 100644 --- a/packages/fuchsia_remote_debug_protocol/lib/src/dart/dart_vm.dart +++ b/packages/fuchsia_remote_debug_protocol/lib/src/dart/dart_vm.dart @@ -120,16 +120,24 @@ class DartVm { /// /// This is not limited to Isolates running Flutter, but to any Isolate on the /// VM. + /// + /// `includeNonFlutterIsolates` makes sure to add non-flutter Dart isolates, + /// and defaults to `false`. Future> getMainIsolatesByPattern( Pattern pattern, { Duration timeout = _kRpcTimeout, + bool includeNonFlutterIsolates = false, }) async { final Map jsonVmRef = await invokeRpc('getVM', timeout: timeout); final List result = []; for (Map jsonIsolate in jsonVmRef['isolates']) { final String name = jsonIsolate['name']; - if (name.contains(pattern)) { + // `:main()` is included at the end of a flutter isolate, whereas the + // name of a dart Isolate is concluded as if the name were a function. + if (name.contains(pattern) && + (includeNonFlutterIsolates || name.contains(RegExp(r':main\(\)')))) { + _log.fine('Found Isolate matching "$pattern": "$name"'); result.add(IsolateRef._fromJson(jsonIsolate, this)); } } diff --git a/packages/fuchsia_remote_debug_protocol/test/src/dart/dart_vm_test.dart b/packages/fuchsia_remote_debug_protocol/test/src/dart/dart_vm_test.dart index 31d1d323fe..af000ec525 100644 --- a/packages/fuchsia_remote_debug_protocol/test/src/dart/dart_vm_test.dart +++ b/packages/fuchsia_remote_debug_protocol/test/src/dart/dart_vm_test.dart @@ -85,9 +85,8 @@ void main() { }; Future mockVmConnectionFunction(Uri uri) { - when(mockPeer.sendRequest(any, any)) - .thenAnswer((_) => Future>( - () => flutterViewCannedResponses)); + when(mockPeer.sendRequest(any, any)).thenAnswer((_) => + Future>(() => flutterViewCannedResponses)); return Future(() => mockPeer); } @@ -141,9 +140,8 @@ void main() { }; Future mockVmConnectionFunction(Uri uri) { - when(mockPeer.sendRequest(any, any)) - .thenAnswer((_) => Future>( - () => flutterViewCannedResponses)); + when(mockPeer.sendRequest(any, any)).thenAnswer((_) => + Future>(() => flutterViewCannedResponses)); return Future(() => mockPeer); } @@ -189,8 +187,8 @@ void main() { }; Future mockVmConnectionFunction(Uri uri) { - when(mockPeer.sendRequest(any, any)) - .thenAnswer((_) => Future>( + when(mockPeer.sendRequest(any, any)).thenAnswer((_) => + Future>( () => flutterViewCannedResponseMissingId)); return Future(() => mockPeer); } @@ -220,24 +218,30 @@ void main() { { 'type': '@Isolate', 'fixedId': 'true', - 'id': 'isolates/1', - 'name': 'file://flutterBinary1:main()', + 'id': 'isolates/2', + 'name': '0:dart_name_pattern()', 'number': '2', }, { 'type': '@Isolate', 'fixedId': 'true', - 'id': 'isolates/2', + 'id': 'isolates/3', 'name': 'file://flutterBinary2:main()', 'number': '3', }, + { + 'type': '@Isolate', + 'fixedId': 'true', + 'id': 'isolates/4', + 'name': '0:some_other_dart_name_pattern()', + 'number': '4', + }, ], }; Future mockVmConnectionFunction(Uri uri) { - when(mockPeer.sendRequest(any, any)) - .thenAnswer((_) => - Future>(() => vmCannedResponse)); + when(mockPeer.sendRequest(any, any)).thenAnswer( + (_) => Future>(() => vmCannedResponse)); return Future(() => mockPeer); } @@ -245,9 +249,15 @@ void main() { final DartVm vm = await DartVm.connect(Uri.parse('http://whatever.com/ws')); expect(vm, isNot(null)); - final List isolates = + final List matchingFlutterIsolates = await vm.getMainIsolatesByPattern('flutterBinary'); - expect(isolates.length, 2); + expect(matchingFlutterIsolates.length, 1); + final List allFlutterIsolates = + await vm.getMainIsolatesByPattern(''); + expect(allFlutterIsolates.length, 2); + final List allIsolates = await vm.getMainIsolatesByPattern('', + includeNonFlutterIsolates: true); + expect(allIsolates.length, 4); }); test('invalid flutter view missing ID', () async { @@ -269,8 +279,8 @@ void main() { }; Future mockVmConnectionFunction(Uri uri) { - when(mockPeer.sendRequest(any, any)) - .thenAnswer((_) => Future>( + when(mockPeer.sendRequest(any, any)).thenAnswer((_) => + Future>( () => flutterViewCannedResponseMissingIsolateName)); return Future(() => mockPeer); }