[pigeon] Create a message call free InstanceManager when running unit tests#9395
Conversation
|
|
||
| object = null; | ||
| await forceGC(); | ||
| await forceGC(fullGcCycles: 2); |
There was a problem hiding this comment.
This test was failing on windows, so I added another cycle.
| late final void Function(int) onWeakReferenceRemoved; | ||
|
|
||
| static $dartInstanceManagerClassName _initInstance() { | ||
| if (Platform.environment['FLUTTER_TEST'] == 'true') { |
There was a problem hiding this comment.
Hm, is FLUTTER_TEST only set in unit tests? Or will this break flutter test integration test behavior?
There was a problem hiding this comment.
I forgot that integration tests also use flutter test. But I just tried it and it looks like this doesn't affect integration tests. It seems the environment variable is only for unit tests.
There was a problem hiding this comment.
This is probably the case because integration tests still need to use the real BinaryMessenger. When FLUTTER_TEST is set, it uses a test BinaryMessenger from TestWidgetsFlutterBinding.
Edit:
Actually I found a bit of info on it: https://api.flutter.dev/flutter/flutter_test/LiveTestWidgetsFlutterBinding-class.html. It looks like integration tests from flutter test is treated the same way as running tests with flutter run.
There was a problem hiding this comment.
Sounds good; LGTM then.
I wish we didn't have to embed special test logic in the production code, but it seems like the best option we have at the moment.
There was a problem hiding this comment.
Hmm would it be better to copy https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/foundation/_platform_io.dart#L30 then. And wrap the Platform.environment check in an assert so that it gets ignored in release mode.
static PigeonInstanceManager _initInstance() {
PigeonInstanceManager? instanceManager;
assert(() {
if (Platform.environment.containsKey('FLUTTER_TEST')) {
instanceManager = PigeonInstanceManager(onWeakReferenceRemoved: (_) {});
}
return true;
}());
if (instanceManager == null) {
// ...
}
return instanceManager!;
}flutter/packages@715a0a5...0ec4053 2025-06-19 engine-flutter-autoroll@skia.org Roll Flutter from 8303a96 to 85a9b4f (93 revisions) (flutter/packages#9457) 2025-06-19 32538273+ValentinVignal@users.noreply.github.com [go_router] Update sype safe routing topic to use mixin from go_router_builder 3.0.0 (flutter/packages#9422) 2025-06-19 august.oberhauser@swissinfo.ch [go_router] fix: PopScope.onPopInvokedWithResult not called in branch routes (flutter/packages#9245) 2025-06-18 10687576+bparrishMines@users.noreply.github.com [pigeon] Create a message call free InstanceManager when running unit tests (flutter/packages#9395) 2025-06-18 737941+loic-sharma@users.noreply.github.com [go_router] Use library prefix for meta (flutter/packages#9434) 2025-06-18 41765610+ahyangnb@users.noreply.github.com [go_router] fix Popping state and re-rendering scaffold at the same time doesn't update the URL on web [new] (flutter/packages#8352) 2025-06-18 robert.odrowaz@leancode.pl [camera_avfoundation] Fix incorrect types in image stream events (flutter/packages#9418) 2025-06-18 stuartmorgan@google.com [go_router_builder] Temporarily restrict `build` (flutter/packages#9453) If this roll has caused a breakage, revert this CL and stop the roller using the controls here: https://autoroll.skia.org/r/flutter-packages-flutter-autoroll Please CC flutter-ecosystem@google.com on the revert to ensure that a human is aware of the problem. To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
… tests (flutter#9395) To support hot restart, `PigeonInstanceManager.instance` makes a call to clear the native `InstanceManager` when it is instantiated. This was the source of unit test race condition flakes like flutter/flutter#164132. The current workaround is to `try/catch` the async `PlatformException` at the top of every test file or pass a test `InstanceManager` to every `ProxyApi` class: ```dart final MyNativeClass object = MyNativeClass( pigeon_instanceManager: PigeonInstanceManager( onWeakReferenceRemoved: (_) {} ), ); ``` This PR changes it to [detect a test ran as Flutter unit test](https://api.flutter.dev/flutter/flutter_test/TestWidgetsFlutterBinding/ensureInitialized.html) and sets the default `InstanceManager` with one that doesn't make any message calls. ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
flutter/packages@715a0a5...0ec4053 2025-06-19 engine-flutter-autoroll@skia.org Roll Flutter from 8303a96 to 85a9b4f (93 revisions) (flutter/packages#9457) 2025-06-19 32538273+ValentinVignal@users.noreply.github.com [go_router] Update sype safe routing topic to use mixin from go_router_builder 3.0.0 (flutter/packages#9422) 2025-06-19 august.oberhauser@swissinfo.ch [go_router] fix: PopScope.onPopInvokedWithResult not called in branch routes (flutter/packages#9245) 2025-06-18 10687576+bparrishMines@users.noreply.github.com [pigeon] Create a message call free InstanceManager when running unit tests (flutter/packages#9395) 2025-06-18 737941+loic-sharma@users.noreply.github.com [go_router] Use library prefix for meta (flutter/packages#9434) 2025-06-18 41765610+ahyangnb@users.noreply.github.com [go_router] fix Popping state and re-rendering scaffold at the same time doesn't update the URL on web [new] (flutter/packages#8352) 2025-06-18 robert.odrowaz@leancode.pl [camera_avfoundation] Fix incorrect types in image stream events (flutter/packages#9418) 2025-06-18 stuartmorgan@google.com [go_router_builder] Temporarily restrict `build` (flutter/packages#9453) If this roll has caused a breakage, revert this CL and stop the roller using the controls here: https://autoroll.skia.org/r/flutter-packages-flutter-autoroll Please CC flutter-ecosystem@google.com on the revert to ensure that a human is aware of the problem. To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
… tests (flutter#9395) To support hot restart, `PigeonInstanceManager.instance` makes a call to clear the native `InstanceManager` when it is instantiated. This was the source of unit test race condition flakes like flutter/flutter#164132. The current workaround is to `try/catch` the async `PlatformException` at the top of every test file or pass a test `InstanceManager` to every `ProxyApi` class: ```dart final MyNativeClass object = MyNativeClass( pigeon_instanceManager: PigeonInstanceManager( onWeakReferenceRemoved: (_) {} ), ); ``` This PR changes it to [detect a test ran as Flutter unit test](https://api.flutter.dev/flutter/flutter_test/TestWidgetsFlutterBinding/ensureInitialized.html) and sets the default `InstanceManager` with one that doesn't make any message calls. ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
To support hot restart,
PigeonInstanceManager.instancemakes a call to clear the nativeInstanceManagerwhen it is instantiated.This was the source of unit test race condition flakes like flutter/flutter#164132.
The current workaround is to
try/catchthe asyncPlatformExceptionat the top of every test file or pass a testInstanceManagerto everyProxyApiclass:This PR changes it to detect a test ran as Flutter unit test and sets the default
InstanceManagerwith one that doesn't make any message calls.Pre-Review Checklist
[shared_preferences]pubspec.yamlwith an appropriate new version according to the pub versioning philosophy, or I have commented below to indicate which version change exemption this PR falls under1.CHANGELOG.mdto add a description of the change, following repository CHANGELOG style, or I have commented below to indicate which CHANGELOG exemption this PR falls under1.///).If you need help, consider asking for advice on the #hackers-new channel on Discord.
Footnotes
Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2 ↩3