-
Notifications
You must be signed in to change notification settings - Fork 51
fix(llc): call accept race fix #1258
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
Brazol
wants to merge
4
commits into
main
Choose a base branch
from
fix/call-accept-race
base: main
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.
+230
−5
Open
Changes from all commits
Commits
Show all changes
4 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
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -42,16 +42,16 @@ mixin StateLifecycleMixin on StateNotifier<CallState> { | |||||
| ); | ||||||
| } | ||||||
|
|
||||||
| void lifecycleCallAccepted() { | ||||||
| void lifecycleCallAccepted({bool accepted = true}) { | ||||||
| final status = state.status; | ||||||
| if (status is! CallStatusIncoming || status.acceptedByMe) { | ||||||
| if (status is! CallStatusIncoming || status.acceptedByMe == accepted) { | ||||||
| _logger.w( | ||||||
| () => '[lifecycleCallAccepted] rejected (invalid status): $status', | ||||||
| ); | ||||||
| return; | ||||||
| } | ||||||
| state = state.copyWith( | ||||||
| status: CallStatus.incoming(acceptedByMe: true), | ||||||
| status: CallStatus.incoming(acceptedByMe: accepted), | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think its always going to be true |
||||||
| ); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
219 changes: 219 additions & 0 deletions
219
packages/stream_video/test/src/call/call_accept_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,219 @@ | ||
| // ignore_for_file: avoid_redundant_argument_values | ||
|
|
||
| import 'dart:async'; | ||
|
|
||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:internet_connection_checker_plus/internet_connection_checker_plus.dart'; | ||
| import 'package:mocktail/mocktail.dart'; | ||
| import 'package:stream_video/src/call/state/call_state_notifier.dart'; | ||
| import 'package:stream_video/src/shared_emitter.dart'; | ||
| import 'package:stream_video/stream_video.dart'; | ||
|
|
||
| import '../../test_helpers.dart'; | ||
|
|
||
| void main() { | ||
| setUpAll(() { | ||
| TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
|
||
| registerFallbackValue( | ||
| StreamCallCid.from( | ||
| type: StreamCallType.defaultType(), | ||
| id: 'fallback-call-id', | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| group('Call.accept optimistic acceptance', () { | ||
| late StreamVideo streamVideo; | ||
| late MockCoordinatorClient mockCoordinatorClient; | ||
|
|
||
| setUp(() { | ||
| mockCoordinatorClient = MockCoordinatorClient(); | ||
|
|
||
| when( | ||
| () => mockCoordinatorClient.events, | ||
| ).thenReturn(MutableSharedEmitterImpl<CoordinatorEvent>()); | ||
|
|
||
| streamVideo = StreamVideo.create( | ||
| 'test-api-key', | ||
| user: User.regular(userId: 'test-user', name: 'Test User'), | ||
| userToken: | ||
| 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiSm9obiBEb2UifQ.hrrtiYCtfs2cowE2sx2dypxoXhsEE8pQl-V6Nq4i8qU', | ||
| options: StreamVideoOptions( | ||
| allowMultipleActiveCalls: true, | ||
| autoConnect: false, | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| tearDown(() async { | ||
| await StreamVideo.reset(); | ||
| }); | ||
|
|
||
| Call createIncomingCall() { | ||
| final ringingCallCid = StreamCallCid.from( | ||
| id: 'ringing-call', | ||
| type: StreamCallType.defaultType(), | ||
| ); | ||
|
|
||
| final ringingData = CallRingingData( | ||
| callCid: ringingCallCid, | ||
| ringing: true, | ||
| metadata: CallMetadata( | ||
| cid: ringingCallCid, | ||
| details: createTestCallDetails(createdByUserId: 'other-user'), | ||
| settings: const CallSettings(), | ||
| session: const CallSessionData(), | ||
| users: const {}, | ||
| members: const {}, | ||
| ), | ||
| ); | ||
|
|
||
| return Call.fromRinging( | ||
| data: ringingData, | ||
| coordinatorClient: mockCoordinatorClient, | ||
| streamVideo: streamVideo, | ||
| networkMonitor: InternetConnection.createInstance(), | ||
| ); | ||
| } | ||
|
|
||
| test( | ||
| 'marks call as accepted before coordinator accept completes', | ||
| () async { | ||
| final acceptCompleter = Completer<Result<None>>(); | ||
| when( | ||
| () => mockCoordinatorClient.acceptCall(cid: any(named: 'cid')), | ||
| ).thenAnswer((_) => acceptCompleter.future); | ||
|
|
||
| final call = createIncomingCall(); | ||
| final acceptFuture = call.accept(); | ||
|
|
||
| final statusWhilePending = | ||
| call.state.value.status as CallStatusIncoming; | ||
| expect(statusWhilePending.acceptedByMe, isTrue); | ||
|
|
||
| acceptCompleter.complete(const Result.success(none)); | ||
| final result = await acceptFuture; | ||
|
|
||
| expect(result.isSuccess, isTrue); | ||
| final statusAfterSuccess = | ||
| call.state.value.status as CallStatusIncoming; | ||
| expect(statusAfterSuccess.acceptedByMe, isTrue); | ||
| }, | ||
| ); | ||
|
|
||
| test('reverts acceptedByMe when coordinator accept fails', () async { | ||
| when( | ||
| () => mockCoordinatorClient.acceptCall(cid: any(named: 'cid')), | ||
| ).thenAnswer((_) async => Result.error('network error')); | ||
|
|
||
| final call = createIncomingCall(); | ||
| final result = await call.accept(); | ||
|
|
||
| expect(result.isFailure, isTrue); | ||
| final status = call.state.value.status as CallStatusIncoming; | ||
| expect(status.acceptedByMe, isFalse); | ||
| }); | ||
|
|
||
| test('allows retry after failed accept', () async { | ||
| var acceptAttempts = 0; | ||
| when( | ||
| () => mockCoordinatorClient.acceptCall(cid: any(named: 'cid')), | ||
| ).thenAnswer((_) async { | ||
| acceptAttempts++; | ||
| if (acceptAttempts == 1) { | ||
| return Result.error('network error'); | ||
| } | ||
| return const Result.success(none); | ||
| }); | ||
|
|
||
| final call = createIncomingCall(); | ||
|
|
||
| final firstResult = await call.accept(); | ||
| expect(firstResult.isFailure, isTrue); | ||
| expect( | ||
| (call.state.value.status as CallStatusIncoming).acceptedByMe, | ||
| isFalse, | ||
| ); | ||
|
|
||
| final secondResult = await call.accept(); | ||
| expect(secondResult.isSuccess, isTrue); | ||
| expect( | ||
| (call.state.value.status as CallStatusIncoming).acceptedByMe, | ||
| isTrue, | ||
| ); | ||
| expect(acceptAttempts, 2); | ||
| }); | ||
| }); | ||
|
|
||
| group('CallStateNotifier.lifecycleCallAccepted', () { | ||
| late CallStateNotifier stateNotifier; | ||
|
|
||
| setUp(() { | ||
| stateNotifier = CallStateNotifier( | ||
| CallState( | ||
| callCid: StreamCallCid.from( | ||
| type: StreamCallType.defaultType(), | ||
| id: 'test-call', | ||
| ), | ||
| currentUserId: 'current-user', | ||
| preferences: DefaultCallPreferences(), | ||
| ).copyWith( | ||
| status: CallStatus.incoming(acceptedByMe: false), | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| tearDown(() { | ||
| stateNotifier.dispose(); | ||
| }); | ||
|
|
||
| test('marks incoming call as accepted', () { | ||
| stateNotifier.lifecycleCallAccepted(); | ||
|
|
||
| final status = stateNotifier.state.status as CallStatusIncoming; | ||
| expect(status.acceptedByMe, isTrue); | ||
| }); | ||
|
|
||
| test('reverts accepted call to not accepted', () { | ||
| stateNotifier.state = stateNotifier.state.copyWith( | ||
| status: CallStatus.incoming(acceptedByMe: true), | ||
| ); | ||
|
|
||
| stateNotifier.lifecycleCallAccepted(accepted: false); | ||
|
|
||
| final status = stateNotifier.state.status as CallStatusIncoming; | ||
| expect(status.acceptedByMe, isFalse); | ||
| }); | ||
|
|
||
| test('ignores duplicate accept transition', () { | ||
| stateNotifier.lifecycleCallAccepted(); | ||
| final acceptedState = stateNotifier.state; | ||
|
|
||
| stateNotifier.lifecycleCallAccepted(); | ||
|
|
||
| expect(stateNotifier.state, same(acceptedState)); | ||
| }); | ||
|
|
||
| test('ignores duplicate revert transition', () { | ||
| stateNotifier.lifecycleCallAccepted(accepted: false); | ||
|
|
||
| expect(stateNotifier.state.status, isA<CallStatusIncoming>()); | ||
| expect( | ||
| (stateNotifier.state.status as CallStatusIncoming).acceptedByMe, | ||
| isFalse, | ||
| ); | ||
| }); | ||
|
|
||
| test('ignores transition when call is not incoming', () { | ||
| stateNotifier.state = stateNotifier.state.copyWith( | ||
| status: CallStatus.connected(), | ||
| ); | ||
| final connectedState = stateNotifier.state; | ||
|
|
||
| stateNotifier.lifecycleCallAccepted(); | ||
|
|
||
| expect(stateNotifier.state, same(connectedState)); | ||
| }); | ||
| }); | ||
| } |
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.