-
Notifications
You must be signed in to change notification settings - Fork 28
perf: single dispute-chat REQ and constant-time session resolution #714
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 1 commit
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
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
132 changes: 132 additions & 0 deletions
132
test/features/disputes/dispute_chat_single_req_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,132 @@ | ||
| import 'dart:async'; | ||
|
|
||
| import 'package:dart_nostr/dart_nostr.dart'; | ||
| import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:mockito/mockito.dart'; | ||
| import 'package:mostro_mobile/data/models/nostr_event.dart'; | ||
| import 'package:mostro_mobile/data/models/session.dart'; | ||
| import 'package:mostro_mobile/data/repositories/event_storage.dart'; | ||
| import 'package:mostro_mobile/features/disputes/notifiers/dispute_chat_notifier.dart'; | ||
| import 'package:mostro_mobile/features/order/providers/order_notifier_provider.dart'; | ||
| import 'package:mostro_mobile/features/subscriptions/subscription_manager_provider.dart'; | ||
| import 'package:mostro_mobile/shared/notifiers/session_notifier.dart'; | ||
| import 'package:mostro_mobile/shared/providers/mostro_service_provider.dart'; | ||
| import 'package:mostro_mobile/shared/providers/session_notifier_provider.dart'; | ||
| import 'package:mostro_mobile/shared/providers/storage_providers.dart'; | ||
| import 'package:mostro_mobile/shared/utils/chat_keys.dart'; | ||
| import 'package:sembast/sembast_memory.dart'; | ||
| import 'package:shared_preferences/shared_preferences.dart'; | ||
| import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart'; | ||
| import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart'; | ||
|
|
||
| import '../../mocks.mocks.dart'; | ||
|
|
||
| /// The dispute chat notifier used to open its OWN kind-14 REQ — a duplicate | ||
| /// of the one `SubscriptionManager` already maintains for | ||
| /// `SubscriptionType.disputeChat` (whose stream nobody consumed) — and | ||
| /// resolved its session by scanning every session and instantiating an | ||
| /// `OrderNotifier` (DB sync + subscriptions) per candidate, on EVERY | ||
| /// incoming event. It now consumes the manager's stream and resolves the | ||
| /// session through the persisted `session.disputeId`. | ||
| void main() { | ||
| TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
|
||
| const disputeId = 'dispute-single-req'; | ||
| const orderId = 'order-single-req'; | ||
| final tradeKey = NostrKeyPairs( | ||
| private: | ||
| '0000000000000000000000000000000000000000000000000000000000000005', | ||
| ); | ||
| final adminKey = NostrKeyPairs( | ||
| private: | ||
| '0000000000000000000000000000000000000000000000000000000000000006', | ||
| ); | ||
|
|
||
| late Session session; | ||
| late StreamController<NostrEvent> disputeStream; | ||
| late MockSubscriptionManagerSpy manager; | ||
| late ProviderContainer container; | ||
|
|
||
| setUp(() async { | ||
| SharedPreferencesAsyncPlatform.instance = | ||
| InMemorySharedPreferencesAsync.empty(); | ||
|
|
||
| session = Session( | ||
| masterKey: tradeKey, | ||
| tradeKey: tradeKey, | ||
| keyIndex: 1, | ||
| fullPrivacy: false, | ||
| startTime: DateTime.now(), | ||
| orderId: orderId, | ||
| ) | ||
| ..disputeId = disputeId | ||
| ..setAdminPeer(adminKey.public); | ||
|
|
||
| disputeStream = StreamController<NostrEvent>.broadcast(); | ||
| manager = MockSubscriptionManagerSpy(); | ||
| when(manager.disputeChat).thenAnswer((_) => disputeStream.stream); | ||
|
|
||
| final db = await newDatabaseFactoryMemory() | ||
| .openDatabase('dispute_single_req.db'); | ||
|
|
||
| container = ProviderContainer(overrides: [ | ||
| sharedPreferencesProvider.overrideWithValue(SharedPreferencesAsync()), | ||
| eventStorageProvider.overrideWithValue(EventStorage(db: db)), | ||
| subscriptionManagerProvider.overrideWithValue(manager), | ||
| mostroServiceProvider | ||
| .overrideWith((ref) => throw UnimplementedError('unused')), | ||
| sessionNotifierProvider | ||
| .overrideWith((ref) => _FixedSessionNotifier(ref, [session])), | ||
| // The old resolution instantiated an OrderNotifier per session per | ||
| // event; the disputeId lookup must never need one. | ||
| orderNotifierProvider.overrideWith( | ||
| (ref, id) => | ||
| throw StateError('session resolution must not build notifiers'), | ||
| ), | ||
| ]); | ||
| addTearDown(container.dispose); | ||
| addTearDown(disputeStream.close); | ||
| }); | ||
|
|
||
| Future<NostrEvent> adminEnvelope(String text) { | ||
| final chatKeys = ChatKeys.fromSharedKey(session.adminSharedKey!); | ||
| final rumor = NostrEventExtensions.createChatRumor( | ||
| senderKeys: adminKey, | ||
| content: text, | ||
| ); | ||
| return rumor.chatWrap(chatKeys); | ||
| } | ||
|
|
||
| test('events from the shared manager stream reach the notifier', () async { | ||
| container.read(disputeChatNotifierProvider(disputeId).notifier); | ||
| await pumpEventQueue(times: 50); | ||
|
|
||
| disputeStream.add(await adminEnvelope('hola admin')); | ||
|
|
||
| final deadline = DateTime.now().add(const Duration(seconds: 5)); | ||
| while (container | ||
| .read(disputeChatNotifierProvider(disputeId)) | ||
| .messages | ||
| .isEmpty && | ||
| DateTime.now().isBefore(deadline)) { | ||
| await Future<void>.delayed(const Duration(milliseconds: 20)); | ||
| } | ||
|
|
||
| final messages = | ||
| container.read(disputeChatNotifierProvider(disputeId)).messages; | ||
| expect(messages, hasLength(1), | ||
| reason: 'the notifier must consume SubscriptionManager.disputeChat ' | ||
| 'instead of opening its own REQ, and resolve its session via ' | ||
| 'session.disputeId without touching order notifiers'); | ||
| expect(messages.single.content, 'hola admin'); | ||
| }); | ||
| } | ||
|
|
||
| /// Session list the notifier can read without touching storage. | ||
| class _FixedSessionNotifier extends SessionNotifier { | ||
| _FixedSessionNotifier(Ref ref, List<Session> sessions) | ||
| : super(ref, MockSessionStorage(), MockSettings()) { | ||
| state = sessions; | ||
| } | ||
| } |
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.