-
Notifications
You must be signed in to change notification settings - Fork 14
fix: avoid concurrent modification during event dispatch #128
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
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,123 @@ | ||
| import 'dart:typed_data'; | ||
|
|
||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:iris_method_channel/iris_method_channel.dart'; | ||
|
|
||
| class _FakePlatformBindingsDelegate extends PlatformBindingsDelegateInterface { | ||
| @override | ||
| int callApi(IrisMethodCall methodCall, IrisApiEngineHandle apiEnginePtr, IrisApiParamHandle param) => 0; | ||
|
|
||
| @override | ||
| Future<CallApiResult> callApiAsync(IrisMethodCall methodCall, IrisApiEngineHandle apiEnginePtr, IrisApiParamHandle param) async => CallApiResult(data: {}, irisReturnCode: 0); | ||
|
|
||
| @override | ||
| CreateApiEngineResult createApiEngine(List<InitilizationArgProvider> args) => CreateApiEngineResult(IrisApiEngineHandle(0)); | ||
|
|
||
| @override | ||
| IrisEventHandlerHandle createIrisEventHandler(IrisCEventHandlerHandle eventHandler) => IrisEventHandlerHandle(0); | ||
|
|
||
| @override | ||
| void destroyIrisEventHandler(IrisEventHandlerHandle handler) {} | ||
|
|
||
| @override | ||
| void destroyNativeApiEngine(IrisApiEngineHandle apiEnginePtr) {} | ||
|
|
||
| @override | ||
| void initialize() {} | ||
| } | ||
|
|
||
| class _FakePlatformBindingsProvider extends PlatformBindingsProvider { | ||
| @override | ||
| PlatformBindingsDelegateInterface provideNativeBindingDelegate() { | ||
| return _FakePlatformBindingsDelegate(); | ||
| } | ||
| } | ||
|
|
||
| void main() { | ||
| TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
|
||
| test('ConcurrentModificationError should not be thrown when adding/removing handlers during event dispatch', () async { | ||
| final provider = _FakePlatformBindingsProvider(); | ||
| final irisMethodChannel = IrisMethodChannel(provider); | ||
|
|
||
| // Logic Tester that mimics the FIXED IrisMethodChannel event loop | ||
| void simulateEventLoop(IrisMethodChannel channel, IrisEventMessage message) { | ||
| bool handled = false; | ||
| // We use the same snapshotting logic as in the fixed IrisMethodChannel | ||
| for (final sub in channel.scopedEventHandlers.values) { | ||
| final scopedObjects = sub as DisposableScopedObjects; | ||
| for (final es in scopedObjects.values) { | ||
| final EventHandlerHolder eh = es as EventHandlerHolder; | ||
| final handlersSnapshot = eh.getEventHandlers(); | ||
|
|
||
| for (final e in handlersSnapshot.toList()) { | ||
| if (!eh.getEventHandlers().contains(e)) { | ||
| continue; | ||
| } | ||
| if (e.handleEvent(message.event, message.data, message.buffers)) { | ||
| handled = true; | ||
| } | ||
| } | ||
|
|
||
| if (handled) { | ||
| break; | ||
| } | ||
| } | ||
| if (handled) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const key = TypedScopedKey(Object); | ||
| final subScopedObjects = irisMethodChannel.scopedEventHandlers.putIfAbsent(key, () => DisposableScopedObjects()) as DisposableScopedObjects; | ||
| final eventKey = EventHandlerHolderKey(registerName: 'test_event', unregisterName: 'test_event'); | ||
| final holder = subScopedObjects.putIfAbsent(eventKey, () => EventHandlerHolder(key: eventKey)) as EventHandlerHolder; | ||
|
|
||
| // 1. Test removing self during event handling | ||
| late EventLoopEventHandler handlerToRemove; | ||
| bool handlerCalled = false; | ||
|
|
||
| handlerToRemove = _TestEventHandler((eventName, eventData, buffers) { | ||
| handlerCalled = true; | ||
| holder.removeEventHandler(handlerToRemove); | ||
| return true; | ||
|
ZGaopeng marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| holder.addEventHandler(handlerToRemove); | ||
|
|
||
| // This should NOT throw error | ||
| simulateEventLoop(irisMethodChannel, const IrisEventMessage('test_event', '{}', [])); | ||
| expect(handlerCalled, true); | ||
|
ZGaopeng marked this conversation as resolved.
|
||
| expect(holder.getEventHandlers().length, 0); | ||
|
|
||
| // 2. Test adding another handler during event handling | ||
| bool firstHandlerCalled = false; | ||
| final firstHandler = _TestEventHandler((eventName, eventData, buffers) { | ||
| firstHandlerCalled = true; | ||
| holder.addEventHandler(_TestEventHandler((_, __, ___) => true)); | ||
| return false; | ||
| }); | ||
|
|
||
| holder.addEventHandler(firstHandler); | ||
|
|
||
| simulateEventLoop(irisMethodChannel, const IrisEventMessage('test_event', '{}', [])); | ||
| expect(firstHandlerCalled, true); | ||
|
ZGaopeng marked this conversation as resolved.
|
||
| expect(holder.getEventHandlers().length, 2); // 1 (firstHandler) + 1 (added handler) | ||
| }); | ||
| } | ||
|
|
||
| typedef HandleEventCallback = bool Function(String eventName, String eventData, List<Uint8List> buffers); | ||
|
|
||
| class _TestEventHandler extends EventLoopEventHandler with ScopedDisposableObjectMixin implements DisposableObject { | ||
| _TestEventHandler(this.callback); | ||
| final HandleEventCallback callback; | ||
|
|
||
| @override | ||
| bool handleEventInternal(String eventName, String eventData, List<Uint8List> buffers) { | ||
| return callback(eventName, eventData, buffers); | ||
| } | ||
|
|
||
| @override | ||
| Future<void> dispose() async {} | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.