-
Notifications
You must be signed in to change notification settings - Fork 388
Fix auto select main isolate #9756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rishika0212
wants to merge
5
commits into
flutter:master
Choose a base branch
from
rishika0212:fix-auto-select-main-isolate
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
062845e
Fix main isolate auto-selection for test suite isolates
rishika0212 fcd42b9
added release notes
rishika0212 45fa504
Auto-select test suite isolate for paused dart tests
rishika0212 9c1cf10
fix: auto-select test_suite isolate as main
rishika0212 8775ece
address review feedback: clarify comment and fix test file ordering
rishika0212 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
252 changes: 252 additions & 0 deletions
252
packages/devtools_app_shared/test/service/isolate_manager_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,252 @@ | ||
| // Copyright 2026 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd. | ||
|
|
||
| import 'dart:async'; | ||
|
|
||
| import 'package:devtools_app_shared/src/service/isolate_manager.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:vm_service/vm_service.dart'; | ||
|
|
||
| void main() { | ||
| group('IsolateManager._computeMainIsolate', () { | ||
| late IsolateManager manager; | ||
| final fakeServices = <_FakeVmService>[]; | ||
|
|
||
| setUp(() { | ||
| manager = IsolateManager(); | ||
| }); | ||
|
|
||
| tearDown(() { | ||
| manager.handleVmServiceClosed(); | ||
| for (final fakeService in fakeServices) { | ||
| unawaited(fakeService.dispose()); | ||
| } | ||
| fakeServices.clear(); | ||
| }); | ||
|
|
||
| test( | ||
| 'selects test_suite isolate instead of test runner when running tests', | ||
| () async { | ||
| // Simulates the isolate list seen when connecting to a test run: | ||
| // - 'main' is the test runner isolate (wrong choice) | ||
| // - 'test_suite:...' is where user code actually runs (correct choice) | ||
| // - 'vm-service' is infrastructure | ||
| final testRunnerRef = _makeRef('main', 'isolates/1'); | ||
| final testSuiteRef = _makeRef( | ||
| 'test_suite:file:///tmp/dart_test.kernel.dill', | ||
| 'isolates/2', | ||
| ); | ||
| final vmServiceRef = _makeRef('vm-service', 'isolates/3'); | ||
|
|
||
| final fakeService = _FakeVmService({ | ||
| testRunnerRef.id!: _makeIsolate(testRunnerRef), | ||
| testSuiteRef.id!: _makeIsolate(testSuiteRef), | ||
| vmServiceRef.id!: _makeIsolate(vmServiceRef), | ||
| }); | ||
| fakeServices.add(fakeService); | ||
|
|
||
| manager.vmServiceOpened(fakeService); | ||
| await manager.init([testRunnerRef, testSuiteRef, vmServiceRef]); | ||
|
|
||
| expect( | ||
| manager.selectedIsolate.value?.name, | ||
| equals('test_suite:file:///tmp/dart_test.kernel.dill'), | ||
| reason: | ||
| 'Should auto-select the test_suite isolate, not the test runner', | ||
| ); | ||
| expect( | ||
| manager.mainIsolate.value?.name, | ||
| equals('test_suite:file:///tmp/dart_test.kernel.dill'), | ||
| reason: 'Main isolate should also resolve to the test_suite isolate', | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| test('selects main isolate for normal (non-test) app runs', () async { | ||
| final mainRef = _makeRef('main', 'isolates/1'); | ||
| final vmServiceRef = _makeRef('vm-service', 'isolates/2'); | ||
|
|
||
| final fakeService = _FakeVmService({ | ||
| mainRef.id!: _makeIsolate(mainRef), | ||
| vmServiceRef.id!: _makeIsolate(vmServiceRef), | ||
| }); | ||
| fakeServices.add(fakeService); | ||
|
|
||
| manager.vmServiceOpened(fakeService); | ||
| await manager.init([mainRef, vmServiceRef]); | ||
|
|
||
| expect( | ||
| manager.selectedIsolate.value?.name, | ||
| equals('main'), | ||
| reason: 'Should select the main isolate for normal app runs', | ||
| ); | ||
| }); | ||
|
|
||
| test('selects isolate containing :main( for dart scripts', () async { | ||
| final scriptRef = _makeRef('foo.dart:main()', 'isolates/1'); | ||
|
|
||
| final fakeService = _FakeVmService({ | ||
| scriptRef.id!: _makeIsolate(scriptRef), | ||
| }); | ||
| fakeServices.add(fakeService); | ||
|
|
||
| manager.vmServiceOpened(fakeService); | ||
| await manager.init([scriptRef]); | ||
|
|
||
| expect( | ||
| manager.selectedIsolate.value?.name, | ||
| equals('foo.dart:main()'), | ||
| ); | ||
| }); | ||
|
|
||
| test( | ||
| 'selects test isolate by root library when test_suite prefix is absent', | ||
| () async { | ||
| final testRunnerRef = _makeRef('main', 'isolates/1'); | ||
| final userTestRef = _makeRef('isolate-2', 'isolates/2'); | ||
| final vmServiceRef = _makeRef('vm-service', 'isolates/3'); | ||
|
|
||
| final fakeService = _FakeVmService({ | ||
| testRunnerRef.id!: _makeIsolate( | ||
| testRunnerRef, | ||
| rootLibraryUri: 'file:///tmp/dart_test.kernel.abcd/test.dart', | ||
| ), | ||
| userTestRef.id!: _makeIsolate( | ||
| userTestRef, | ||
| rootLibraryUri: 'package:my_app/foo_test.dart', | ||
| ), | ||
| vmServiceRef.id!: _makeIsolate( | ||
| vmServiceRef, | ||
| rootLibraryUri: 'dart:developer', | ||
| ), | ||
| }); | ||
| fakeServices.add(fakeService); | ||
|
|
||
| manager.vmServiceOpened(fakeService); | ||
| await manager.init([testRunnerRef, userTestRef, vmServiceRef]); | ||
|
|
||
| expect( | ||
| manager.selectedIsolate.value?.name, | ||
| equals('isolate-2'), | ||
| reason: 'Should choose user test isolate using root library metadata', | ||
| ); | ||
| expect( | ||
| manager.mainIsolate.value?.name, | ||
| equals('isolate-2'), | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| test( | ||
| 'promotes main isolate from test runner to test suite on isolate start', | ||
| () async { | ||
| final testRunnerRef = _makeRef('main', 'isolates/1'); | ||
| final testSuiteRef = _makeRef( | ||
| 'test_suite:file:///tmp/dart_test.kernel.dill', | ||
| 'isolates/2', | ||
| ); | ||
|
|
||
| final fakeService = _FakeVmService({ | ||
| testRunnerRef.id!: _makeIsolate(testRunnerRef), | ||
| testSuiteRef.id!: _makeIsolate(testSuiteRef), | ||
| }); | ||
| fakeServices.add(fakeService); | ||
|
|
||
| manager.vmServiceOpened(fakeService); | ||
| await manager.init(const []); | ||
|
|
||
| await fakeService.emitIsolateStart(testRunnerRef); | ||
| expect(manager.selectedIsolate.value?.name, equals('main')); | ||
| expect(manager.mainIsolate.value?.name, equals('main')); | ||
|
|
||
| await fakeService.emitIsolateStart(testSuiteRef); | ||
| expect( | ||
| manager.selectedIsolate.value?.name, | ||
| equals('test_suite:file:///tmp/dart_test.kernel.dill'), | ||
| reason: | ||
| 'Should switch selection to test_suite isolate once it starts', | ||
| ); | ||
| expect( | ||
| manager.mainIsolate.value?.name, | ||
| equals('test_suite:file:///tmp/dart_test.kernel.dill'), | ||
| ); | ||
| }, | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| /// Minimal fake VmService for IsolateManager tests. | ||
| class _FakeVmService extends Fake implements VmService { | ||
| _FakeVmService(this.isolates); | ||
|
|
||
| /// Map of isolate id -> Isolate to return from getIsolate(). | ||
| final Map<String, Isolate> isolates; | ||
|
|
||
| final _isolateEventController = StreamController<Event>.broadcast(); | ||
|
|
||
| @override | ||
| Stream<Event> get onIsolateEvent => _isolateEventController.stream; | ||
|
|
||
| @override | ||
| Stream<Event> get onDebugEvent => const Stream.empty(); | ||
|
|
||
| @override | ||
| Future<Isolate> getIsolate(String isolateId) async { | ||
| return isolates[isolateId] ?? | ||
| Isolate.parse({ | ||
| 'id': isolateId, | ||
| 'runnable': true, | ||
| 'extensionRPCs': <String>[], | ||
| })!; | ||
| } | ||
|
|
||
| @override | ||
| Future<Success> resume(String isolateId, {String? step, int? frameIndex}) => | ||
| Future.value(Success()); | ||
|
|
||
| Future<void> emitIsolateStart(IsolateRef isolateRef) async { | ||
| _isolateEventController.add( | ||
| Event.parse({ | ||
| 'type': 'Event', | ||
| 'kind': EventKind.kIsolateStart, | ||
| 'isolate': { | ||
| 'type': '@Isolate', | ||
| 'id': isolateRef.id, | ||
| 'name': isolateRef.name, | ||
| 'isSystemIsolate': isolateRef.isSystemIsolate, | ||
| }, | ||
| })!, | ||
| ); | ||
| await Future<void>.delayed(Duration.zero); | ||
| } | ||
|
|
||
| @override | ||
| Future<void> dispose() async { | ||
| await _isolateEventController.close(); | ||
| } | ||
| } | ||
|
|
||
| /// Creates a minimal runnable [Isolate] for a given [IsolateRef]. | ||
| Isolate _makeIsolate(IsolateRef ref, {String? rootLibraryUri}) { | ||
| final json = <String, Object?>{ | ||
| 'id': ref.id, | ||
| 'name': ref.name, | ||
| 'type': '@Isolate', | ||
| 'runnable': true, | ||
| 'extensionRPCs': <String>[], | ||
| if (rootLibraryUri != null) | ||
| 'rootLib': { | ||
| 'type': '@Library', | ||
| 'id': 'libraries/0', | ||
| 'uri': rootLibraryUri, | ||
| }, | ||
| }; | ||
|
|
||
| return Isolate.parse(json)!; | ||
| } | ||
|
|
||
| /// Creates an [IsolateRef] with the given name and id. | ||
| IsolateRef _makeRef(String name, String id) { | ||
| return IsolateRef.parse({'name': name, 'id': id, 'isSystemIsolate': false})!; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jakemac53 added you for review to look at the logic that detects the test isolate.