From 603172b3ecdc584ae7ff28209698e9aeff17ec75 Mon Sep 17 00:00:00 2001 From: zjs81 Date: Sat, 24 Oct 2026 23:38:15 -0700 Subject: [PATCH] Batch per-frame persistence so busy meshes don't stall the UI isolate Every received packet copy re-saved its channel's whole history, every unread change re-saved the channel list, and adverts/path records re-saved full contact and path-history blobs. Under load the SharedPreferences backlog starved the Dart event loop (frames and timers stopped), the radio's offline queue overflowed, and CPU stayed pinned after traffic ended. - Add CoalescedWrites: keeps the latest write per key and runs them once per window (750 ms; 5 s for path history). Writes start synchronously so stores still read the current scoped key. - Flush before the app is backgrounded, on disconnect, before switching radios, before the channel slot map changes, before channel history reloads, before deleteChannel drops a list, and before path history reloads an evicted contact. - Cap in-memory channel history at ChannelMessageStore.maxStoredMessages; storage already kept only that many, but the list grew unbounded during a session and every save grew with it. - Cache Contact.publicKeyHex and build pubKeyToHex from a hex-digit lookup; it was rebuilt on every equality/hash and per-advert contact scan. Load test (fake TCP radio, 350 contacts, 17 channels, 10-150 packets/s, Linux profile build): CPU at 10 packets/s 104% -> 45%, recovers to idle after traffic instead of staying at 160%, and queued messages drained went from 555 to 3237 over the same run. --- lib/connector/meshcore_connector.dart | 120 +++++++++++++++---- lib/connector/meshcore_protocol.dart | 10 +- lib/main.dart | 9 ++ lib/models/contact.dart | 4 +- lib/services/path_history_service.dart | 32 ++++- lib/utils/coalesced_writes.dart | 66 ++++++++++ test/connector/channel_slot_map_test.dart | 36 ++++++ test/connector/pub_key_hex_test.dart | 48 ++++++++ test/services/path_history_service_test.dart | 3 + test/utils/coalesced_writes_test.dart | 66 ++++++++++ 10 files changed, 365 insertions(+), 29 deletions(-) create mode 100644 lib/utils/coalesced_writes.dart create mode 100644 test/connector/pub_key_hex_test.dart create mode 100644 test/utils/coalesced_writes_test.dart diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index b1eaeda7..0784a1ed 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -67,6 +67,7 @@ import 'meshcore_uuids.dart'; import 'meshcore_protocol.dart'; import 'pending_command_replies.dart'; import 'raw_packet.dart'; +import '../utils/coalesced_writes.dart'; class DirectRepeater { static const int maxAgeMinutes = 30; // Max age for direct repeater info @@ -479,6 +480,9 @@ class MeshCoreConnector extends ChangeNotifier { BleDebugLogService? _bleDebugLogService; AppDebugLogService? _appDebugLogService; final ChannelMessageStore _channelMessageStore = ChannelMessageStore(); + // Per-frame persistence (channel history, conversations, contact lists) is + // batched here; see [CoalescedWrites]. + final CoalescedWrites _coalescedWrites = CoalescedWrites(); final MessageStore _messageStore = MessageStore(); final ChannelOrderStore _channelOrderStore = ChannelOrderStore(); final ChannelSettingsStore _channelSettingsStore = ChannelSettingsStore(); @@ -988,9 +992,40 @@ class MeshCoreConnector extends ChangeNotifier { Future _saveChannelMessages(int slot, List messages) { final id = _channelIdForSlot(slot); if (id == null) return Future.value(); + _coalescedWrites.cancel('channel:$id'); return _channelMessageStore.saveChannelMessages(id, messages); } + /// Queues a save of [slot]'s history. The write looks the channel up again + /// when it runs, so it persists whatever the history is by then and skips a + /// channel that was removed in the meantime. + void _scheduleChannelSave(int slot) { + final id = _channelIdForSlot(slot); + if (id == null) return; + _coalescedWrites.schedule('channel:$id', () { + final currentSlot = _slotForChannelId(id); + final messages = currentSlot == null + ? null + : _channelMessages[_canonicalSlot(currentSlot)]; + if (messages == null) return Future.value(); + return _channelMessageStore.saveChannelMessages(id, messages); + }); + } + + /// Queues a save of the conversation with [contactKeyHex]; skipped when the + /// conversation is gone by the time it runs. + void _scheduleConversationSave(String contactKeyHex) { + _coalescedWrites.schedule('conversation:$contactKeyHex', () { + final messages = _conversations[contactKeyHex]; + if (messages == null) return Future.value(); + return _messageStore.saveMessages(contactKeyHex, messages); + }); + } + + /// Runs queued persistence now. Called before the app is backgrounded and + /// whenever the stores are about to switch to another radio. + Future flushPendingWrites() => _coalescedWrites.flush(); + int getUnreadCountForContact(Contact contact) { if (contact.type == advTypeRepeater) return 0; return getUnreadCountForContactKey(contact.publicKeyHex); @@ -1244,6 +1279,17 @@ class MeshCoreConnector extends ChangeNotifier { notifyListeners(); } + /// Queues a save of the channel list, which carries the unread counts that + /// change on every received channel message. + void _scheduleChannelListSave() { + _coalescedWrites.schedule( + 'channels', + () => _channelStore.saveChannels( + _channels.isNotEmpty ? _channels : _cachedChannels, + ), + ); + } + void setChannelUnreadCount(int channelIndex, int count) { final channel = _findChannelByIndex(channelIndex); if (channel != null) { @@ -1251,11 +1297,7 @@ class MeshCoreConnector extends ChangeNotifier { channel.unreadCount = count; final total = _cachedChannelsUnreadTotal + (count - previousCount); _cachedChannelsUnreadTotal = total < 0 ? 0 : total; - unawaited( - _channelStore.saveChannels( - _channels.isNotEmpty ? _channels : _cachedChannels, - ), - ); + _scheduleChannelListSave(); notifyListeners(); } } @@ -1271,11 +1313,7 @@ class MeshCoreConnector extends ChangeNotifier { 'Channel ${channel.name.isNotEmpty ? channel.name : channelIndex} marked as read (was $previousCount unread)', tag: 'Unread', ); - unawaited( - _channelStore.saveChannels( - _channels.isNotEmpty ? _channels : _cachedChannels, - ), - ); + _scheduleChannelListSave(); _notificationService.clearChannelNotification( channelIndex, getTotalUnreadCount(), @@ -1535,6 +1573,9 @@ class MeshCoreConnector extends ChangeNotifier { /// Load persisted channel messages for a specific channel Future _loadChannelMessages(int channelIndex) async { + // The load replaces the in-memory history, so queued writes of it must + // reach storage (whose cache updates synchronously) first. + unawaited(_coalescedWrites.flush()); final channelId = _slotChannelIds[channelIndex]; final allMessages = channelId == null ? [] @@ -5137,7 +5178,7 @@ class MeshCoreConnector extends ChangeNotifier { ), ); } - unawaited(_persistDiscoveredContacts()); + _scheduleDiscoveredContactsPersist(); notifyListeners(); } @@ -5146,7 +5187,7 @@ class MeshCoreConnector extends ChangeNotifier { _discoveredContacts.removeWhere( (c) => c.publicKeyHex == contact.publicKeyHex, ); - unawaited(_persistDiscoveredContacts()); + _scheduleDiscoveredContactsPersist(); notifyListeners(); } @@ -5272,7 +5313,7 @@ class MeshCoreConnector extends ChangeNotifier { ), ); notifyListeners(); - unawaited(_persistDiscoveredContacts()); + _scheduleDiscoveredContactsPersist(); return true; } @@ -5717,6 +5758,9 @@ class MeshCoreConnector extends ChangeNotifier { '$unconfirmed', ); } + // Queued channel writes find their channel by slot; land them before + // the map moves, or a channel whose slot changed would never be saved. + unawaited(_coalescedWrites.flush()); _slotChannelIds ..clear() ..addAll(next); @@ -5842,6 +5886,7 @@ class MeshCoreConnector extends ChangeNotifier { ); await _loadChannelOrder(); final saved = await _channelStore.loadSlotChannelIds(); + unawaited(_coalescedWrites.flush()); _slotChannelIds ..clear() ..addAll(saved); @@ -5914,6 +5959,9 @@ class MeshCoreConnector extends ChangeNotifier { _findChannelByIndex(index)?.storageId ?? _slotChannelIds[index]; // Delete by setting empty name and zero PSK await sendFrame(buildSetChannelFrame(index, '', Uint8List(16))); + // A twin slot with the same PSK keeps this history, so queued writes of + // it land before the list is dropped; a full delete clears them below. + unawaited(_coalescedWrites.flush()); _channelMessages.remove(index); _processedChannelReactions.remove(index); try { @@ -5936,6 +5984,7 @@ class MeshCoreConnector extends ChangeNotifier { /// Removes everything persisted for [channelId]: messages, settings, /// region, images and its place in the channel order. Future _clearChannelStorage(String channelId) async { + _coalescedWrites.cancel('channel:$channelId'); await _channelMessageStore.clearChannelMessages(channelId); await _channelSettingsStore.clearChannel(channelId); await _channelRegionStore.clearRegion(channelId); @@ -6327,6 +6376,7 @@ class MeshCoreConnector extends ChangeNotifier { return; } + unawaited(_coalescedWrites.flush()); //set all the stores' public key so they can load the correct data _channelMessageStore.setPublicKeyHex = selfPublicKeyHex; _messageStore.setPublicKeyHex = selfPublicKeyHex; @@ -6953,14 +7003,27 @@ class MeshCoreConnector extends ChangeNotifier { } } + /// Queues a save of the contact list; adverts and contact replies arrive + /// many times a second on a busy mesh. Future _persistContacts() async { - await _contactStore.saveContacts(_contacts); + _coalescedWrites.schedule( + 'contacts', + () => _contactStore.saveContacts(_contacts), + ); } Future _persistDiscoveredContacts() async { + _coalescedWrites.cancel('discovered'); await _discoveryContactStore.saveContacts(_discoveredContacts); } + void _scheduleDiscoveredContactsPersist() { + _coalescedWrites.schedule( + 'discovered', + () => _discoveryContactStore.saveContacts(_discoveredContacts), + ); + } + int _latestContactLastmod() { if (_contacts.isEmpty) return 0; var latest = 0; @@ -8033,11 +8096,7 @@ class MeshCoreConnector extends ChangeNotifier { 'Channel ${channel.name.isNotEmpty ? channel.name : channelIndex} unread count incremented to ${channel.unreadCount}', tag: 'Unread', ); - unawaited( - _channelStore.saveChannels( - _channels.isNotEmpty ? _channels : _cachedChannels, - ), - ); + _scheduleChannelListSave(); } else { _appDebugLogService?.info( 'Channel $channelIndex not found in _channels (${_channels.length}) or _cachedChannels (${_cachedChannels.length})', @@ -8138,7 +8197,7 @@ class MeshCoreConnector extends ChangeNotifier { _messageStore.archive(pubKeyHex, messages.sublist(0, trimCount)); messages.removeRange(0, trimCount); } - _messageStore.saveMessages(pubKeyHex, messages); + _scheduleConversationSave(pubKeyHex); notifyListeners(); } @@ -8409,7 +8468,7 @@ class MeshCoreConnector extends ChangeNotifier { if (reactionApplied) { appLogger.info('Adding channel reaction, id: $reactionIdentifier'); // Save updated messages - _saveChannelMessages(channelIndex, messages); + _scheduleChannelSave(channelIndex); // Mark as processed _processedChannelReactions[channelIndex]!.add(reactionIdentifier); @@ -8514,10 +8573,14 @@ class MeshCoreConnector extends ChangeNotifier { } } else { messages.add(processedMessage); + // Storage keeps only the newest maxStoredMessages per channel, so older + // entries are gone after the next save anyway. Without this cap a long + // session on a busy channel grows the list, and every save, unbounded. + final overflow = messages.length - ChannelMessageStore.maxStoredMessages; + if (overflow > 0) messages.removeRange(0, overflow); } - // Save to persistent storage - _saveChannelMessages(channelIndex, messages); + _scheduleChannelSave(channelIndex); return isNew; } @@ -8684,6 +8747,7 @@ class MeshCoreConnector extends ChangeNotifier { } void _handleDisconnection() { + unawaited(_coalescedWrites.flush()); _resetLinkSessionState(); _notifySubscription?.cancel(); @@ -8755,6 +8819,9 @@ class MeshCoreConnector extends ChangeNotifier { /// Drops the conversation and channel history held in memory, so a /// different radio never shows or persists another radio's messages. void _clearSessionMessageState() { + // Writes start synchronously, so they still read this radio's history and + // store keys before both change below. + unawaited(_coalescedWrites.flush()); _conversations.clear(); _loadedConversationKeys.clear(); _channelMessages.clear(); @@ -8897,6 +8964,7 @@ class MeshCoreConnector extends ChangeNotifier { _notificationReplySubscription?.cancel(); _notificationService.releaseReplyPort(); _notifyListenersTimer?.cancel(); + unawaited(_coalescedWrites.flush()); _reconnectTimer?.cancel(); _batteryPollTimer?.cancel(); _gpsLocationPollTimer?.cancel(); @@ -9378,7 +9446,7 @@ class MeshCoreConnector extends ChangeNotifier { isActive: addActive, ); notifyListeners(); - unawaited(_persistDiscoveredContacts()); + _scheduleDiscoveredContactsPersist(); return; } @@ -9405,7 +9473,7 @@ class MeshCoreConnector extends ChangeNotifier { } _discoveredContacts.add(disContact); - unawaited(_persistDiscoveredContacts()); + _scheduleDiscoveredContactsPersist(); // Show notification for new contact (advertisement) if (_appSettingsService != null && !noNotify) { @@ -9435,7 +9503,7 @@ class MeshCoreConnector extends ChangeNotifier { void removeAllDiscoveredContacts() { _discoveredContacts.clear(); - unawaited(_persistDiscoveredContacts()); + _scheduleDiscoveredContactsPersist(); notifyListeners(); } diff --git a/lib/connector/meshcore_protocol.dart b/lib/connector/meshcore_protocol.dart index 126110ad..22476f63 100644 --- a/lib/connector/meshcore_protocol.dart +++ b/lib/connector/meshcore_protocol.dart @@ -497,8 +497,16 @@ String ackHashToHex(int ackHash) { } // Helper to convert public key to hex string +const String _hexDigits = '0123456789abcdef'; + String pubKeyToHex(Uint8List pubKey) { - return pubKey.map((b) => b.toRadixString(16).padLeft(2, '0')).join(); + final codes = Uint8List(pubKey.length * 2); + for (var i = 0; i < pubKey.length; i++) { + final b = pubKey[i]; + codes[i * 2] = _hexDigits.codeUnitAt(b >> 4); + codes[i * 2 + 1] = _hexDigits.codeUnitAt(b & 0x0F); + } + return String.fromCharCodes(codes); } // Helper to convert hex string to public key diff --git a/lib/main.dart b/lib/main.dart index db10b0fb..136adad1 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -423,6 +423,15 @@ class _MeshCoreAppState extends State with WidgetsBindingObserver { state == AppLifecycleState.hidden) { unawaited(widget.imageCodecService.handleMemoryPressure()); } + // Batched message/contact writes must land before the OS can suspend or + // kill a backgrounded app. + if (state == AppLifecycleState.inactive || + state == AppLifecycleState.paused || + state == AppLifecycleState.hidden || + state == AppLifecycleState.detached) { + unawaited(widget.connector.flushPendingWrites()); + unawaited(widget.pathHistoryService.flushPendingWrites()); + } if (state == AppLifecycleState.resumed) { widget.connector.syncAppBadge(); } diff --git a/lib/models/contact.dart b/lib/models/contact.dart index a14d91b1..27a276ca 100644 --- a/lib/models/contact.dart +++ b/lib/models/contact.dart @@ -41,7 +41,9 @@ class Contact { this.rawPacket, }) : lastMessageAt = lastMessageAt ?? lastSeen; - String get publicKeyHex => pubKeyToHex(publicKey); + /// Cached: it backs ==, hashCode and the per-frame contact lookups, and + /// rebuilding it allocated a 64-char string on every access. + late final String publicKeyHex = pubKeyToHex(publicKey); /// Uppercase hex of the first [byteWidth] public-key bytes: the hash this /// node appears as in routing paths. diff --git a/lib/services/path_history_service.dart b/lib/services/path_history_service.dart index 1de88941..d476a750 100644 --- a/lib/services/path_history_service.dart +++ b/lib/services/path_history_service.dart @@ -1,7 +1,10 @@ +import 'dart:async'; + import 'package:flutter/foundation.dart'; import '../models/contact.dart'; import '../models/path_history.dart'; import '../models/path_selection.dart'; +import '../utils/coalesced_writes.dart'; import 'storage_service.dart'; class PathHistoryService extends ChangeNotifier { @@ -10,6 +13,10 @@ class PathHistoryService extends ChangeNotifier { final Map _autoRotationIndex = {}; final Map _floodStats = {}; final Map> _pendingLoads = {}; + // A path record lands for most received messages, so saves are batched. + final CoalescedWrites _writes = CoalescedWrites( + delay: const Duration(seconds: 5), + ); // LRU cache eviction tracking static const int _maxCachedContacts = 50; @@ -23,6 +30,9 @@ class PathHistoryService extends ChangeNotifier { PathHistoryService(this._storage); void setDevicePublicKey(String publicKeyHex) { + // Queued saves use the storage key on entry, so they still land under + // the previous radio's key. + unawaited(_writes.flush()); _storage.setPublicKeyHex = publicKeyHex; _cache.clear(); _cacheAccessOrder.clear(); @@ -361,7 +371,12 @@ class PathHistoryService extends ChangeNotifier { _cache[contactPubKeyHex] = updatedHistory; _trackAccess(contactPubKeyHex); _evictIfNeeded(); - _storage.savePathHistory(contactPubKeyHex, updatedHistory); + // The snapshot is written, not the cache entry: the LRU may evict it + // before the batch runs. + _writes.schedule( + contactPubKeyHex, + () => _storage.savePathHistory(contactPubKeyHex, updatedHistory), + ); notifyListeners(); } @@ -404,6 +419,9 @@ class PathHistoryService extends ChangeNotifier { Future _loadHistoryFromStorage( String contactPubKeyHex, ) async { + // An evicted contact may still have a queued save; it reaches the prefs + // cache synchronously, so the load below reads it. + unawaited(_writes.flush()); return await _storage.loadPathHistory(contactPubKeyHex); } @@ -445,6 +463,7 @@ class PathHistoryService extends ChangeNotifier { _cacheAccessOrder.remove(contactPubKeyHex); _autoRotationIndex.remove(contactPubKeyHex); _floodStats.remove(contactPubKeyHex); + _writes.cancel(contactPubKeyHex); await _storage.clearPathHistory(contactPubKeyHex); _version++; notifyListeners(); @@ -465,6 +484,7 @@ class PathHistoryService extends ChangeNotifier { recentPaths: updatedPaths, ); + _writes.cancel(contactPubKeyHex); await _storage.savePathHistory(contactPubKeyHex, _cache[contactPubKeyHex]!); _version++; notifyListeners(); @@ -588,11 +608,21 @@ class PathHistoryService extends ChangeNotifier { } } + /// Runs queued saves now, e.g. before the app is backgrounded. + Future flushPendingWrites() => _writes.flush(); + + @override + void dispose() { + unawaited(_writes.flush()); + super.dispose(); + } + void clearAllHistories() { _cache.clear(); _cacheAccessOrder.clear(); _autoRotationIndex.clear(); _floodStats.clear(); + _writes.dispose(); _storage.clearAllPathHistories(); _version = 0; notifyListeners(); diff --git a/lib/utils/coalesced_writes.dart b/lib/utils/coalesced_writes.dart new file mode 100644 index 00000000..5b9a4e68 --- /dev/null +++ b/lib/utils/coalesced_writes.dart @@ -0,0 +1,66 @@ +import 'dart:async'; + +import 'app_logger.dart'; + +/// Collapses repeated persistence requests for the same key into one write. +/// +/// Busy meshes deliver many frames a second, and each one used to rewrite a +/// whole JSON blob (a channel's history, the contact list). Every write is a +/// full re-encode plus a SharedPreferences write: on Linux/Windows that +/// rewrites the entire prefs file synchronously, on Android/iOS it ships the +/// blob across the platform channel. Scheduling instead keeps only the latest +/// writer per key and runs them all once [delay] after the first request. +class CoalescedWrites { + CoalescedWrites({this.delay = const Duration(milliseconds: 750)}); + + final Duration delay; + final Map Function()> _pending = {}; + Timer? _timer; + + bool get hasPending => _pending.isNotEmpty; + + /// Queues [write] under [key], replacing any write already queued for it. + /// [write] should read the current state when it runs, not a snapshot. + void schedule(String key, Future Function() write) { + _pending[key] = write; + _timer ??= Timer(delay, () => unawaited(flush())); + } + + /// Drops the queued write for [key], e.g. because an immediate write of + /// newer state or a delete just superseded it. + void cancel(String key) { + _pending.remove(key); + } + + /// Runs every queued write now. All writes are started synchronously before + /// the first await, so a store that reads its scoped key on entry still sees + /// the key that was current when the write was queued. + Future flush() { + _timer?.cancel(); + _timer = null; + if (_pending.isEmpty) return Future.value(); + final writes = _pending.values.toList(); + _pending.clear(); + // Callers fire and forget, so a failed write is logged rather than left + // as an unhandled async error, and never stops the other writes. + final futures = >[]; + for (final write in writes) { + try { + futures.add(write().catchError(_logFailure)); + } catch (error) { + _logFailure(error); + } + } + return Future.wait(futures).then((_) {}); + } + + static void _logFailure(Object error) { + appLogger.warn('Coalesced write failed: $error', tag: 'Storage'); + } + + void dispose() { + _timer?.cancel(); + _timer = null; + _pending.clear(); + } +} diff --git a/test/connector/channel_slot_map_test.dart b/test/connector/channel_slot_map_test.dart index d2f217ce..563951a4 100644 --- a/test/connector/channel_slot_map_test.dart +++ b/test/connector/channel_slot_map_test.dart @@ -13,6 +13,7 @@ import 'package:meshcore_open/review_mode/review_script.dart'; import 'package:meshcore_open/services/message_retry_service.dart'; import 'package:meshcore_open/services/path_history_service.dart'; import 'package:meshcore_open/services/storage_service.dart'; +import 'package:meshcore_open/storage/channel_message_store.dart'; import 'package:meshcore_open/storage/prefs_manager.dart'; import 'package:pointycastle/export.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -185,6 +186,7 @@ void main() { expect(_channelAt(1).name, 'Hiking Group'); expect(_texts(1), isNot(contains('ghost message'))); + await _connector.flushPendingWrites(); // The old channel's history is kept under its own ID. expect(_storedTexts(ghostId), ['ghost message']); expect(_storedTexts(hikingId), contains(_queuedHikingText)); @@ -218,6 +220,7 @@ void main() { .firstWhere((m) => m.text == _queuedHikingText); expect(restored.channelIndex, 6); await _connector.deleteChannelMessage(restored); + await _connector.flushPendingWrites(); expect(_texts(6), isNot(contains(_queuedHikingText))); expect(_storedTexts(hikingId), isNot(contains(_queuedHikingText))); }); @@ -237,6 +240,7 @@ void main() { test('deleting a channel clears its stored history', () async { await _connect(_TestRadio()); await _waitFor(() => _texts(1).contains(_queuedHikingText)); + await _connector.flushPendingWrites(); expect(_storedTexts(hikingId), isNotEmpty); await _connector.deleteChannel(1); @@ -273,6 +277,7 @@ void main() { await _waitFor(() => _texts(1).contains(_queuedHikingText)); await _connector.deleteChannel(1); + await _connector.flushPendingWrites(); expect(_storedTexts(hikingId), contains(_queuedHikingText)); }, ); @@ -299,6 +304,7 @@ void main() { seed: () => _seedSlotMap({0: _id(_psk(0)), 1: ghostId, 2: _id(_psk(2))}), ); await _waitFor(() => _texts(1).contains('before the map')); + await _connector.flushPendingWrites(); expect(_storedTexts(hikingId), contains('before the map')); expect(_storedTexts(ghostId), isNot(contains('before the map'))); }); @@ -382,6 +388,7 @@ void main() { ); await _waitFor(() => _texts(1).contains('from the twin slot')); expect(_texts(5), contains('from the twin slot')); + await _connector.flushPendingWrites(); expect( _storedTexts(hikingId).where((t) => t == 'from the twin slot'), hasLength(1), @@ -400,7 +407,36 @@ void main() { ), ); await _waitFor(() => _texts(2).contains('heard over the air')); + await _connector.flushPendingWrites(); expect(_storedTexts(localId), contains('heard over the air')); expect(_texts(1), isNot(contains('heard over the air'))); }); + + test('a busy channel is capped at the stored-message limit in memory and ' + 'in storage', () async { + await _connect(_TestRadio()); + await _waitFor(() => _texts(1).contains(_queuedHikingText)); + const cap = ChannelMessageStore.maxStoredMessages; + final start = DateTime.now().millisecondsSinceEpoch ~/ 1000; + for (var i = 0; i < cap + 50; i++) { + _connector.handleFrameForTest( + ReviewFrames.channelMessage( + channelIndex: 1, + sender: 'Sam', + message: 'busy $i', + timestampSecs: start + i, + ), + ); + } + + final texts = _texts(1); + expect(texts, hasLength(cap)); + expect(texts.last, 'busy ${cap + 49}'); + // Nothing is written per frame; the batch lands on flush. + await _connector.flushPendingWrites(); + final stored = _storedTexts(hikingId); + expect(stored, hasLength(cap)); + expect(stored.first, 'busy 50'); + expect(stored.last, 'busy ${cap + 49}'); + }); } diff --git a/test/connector/pub_key_hex_test.dart b/test/connector/pub_key_hex_test.dart new file mode 100644 index 00000000..8a15e24e --- /dev/null +++ b/test/connector/pub_key_hex_test.dart @@ -0,0 +1,48 @@ +import 'dart:math'; +import 'dart:typed_data'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:meshcore_open/connector/meshcore_protocol.dart'; +import 'package:meshcore_open/models/contact.dart'; + +String _reference(Uint8List bytes) => + bytes.map((b) => b.toRadixString(16).padLeft(2, '0')).join(); + +void main() { + test('pubKeyToHex is lowercase, zero-padded hex for every byte value', () { + final all = Uint8List.fromList(List.generate(256, (i) => i)); + expect(pubKeyToHex(all), _reference(all)); + expect(pubKeyToHex(Uint8List(0)), ''); + expect(pubKeyToHex(Uint8List.fromList([0x00, 0x0a, 0xff])), '000aff'); + }); + + test('pubKeyToHex round-trips random keys through hexToPubKey', () { + final random = Random(7); + for (var i = 0; i < 200; i++) { + final key = Uint8List.fromList( + List.generate(32, (_) => random.nextInt(256)), + ); + final hex = pubKeyToHex(key); + expect(hex, _reference(key)); + expect(hexToPubKey(hex), key); + } + }); + + test('Contact.publicKeyHex is stable and drives equality', () { + final key = Uint8List.fromList(List.generate(32, (i) => i * 7)); + Contact make(String name) => Contact( + publicKey: key, + name: name, + type: 1, + pathLength: -1, + path: Uint8List(0), + lastSeen: DateTime(2026), + ); + final a = make('a'); + expect(a.publicKeyHex, _reference(key)); + expect(identical(a.publicKeyHex, a.publicKeyHex), isTrue); + expect(a, make('renamed')); + expect(a.hashCode, make('renamed').hashCode); + expect(a.copyWith(name: 'b').publicKeyHex, a.publicKeyHex); + }); +} diff --git a/test/services/path_history_service_test.dart b/test/services/path_history_service_test.dart index a5403b5f..d8e3f077 100644 --- a/test/services/path_history_service_test.dart +++ b/test/services/path_history_service_test.dart @@ -858,6 +858,8 @@ void main() { svc.recordPathResult(pubKey, sel, success: true, successIncrement: 0.5); } await _flush(); + // Saves are batched; a restart only follows a flush (app backgrounded). + await svc.flushPendingWrites(); final fresh = PathHistoryService(storage); fresh.recordPathResult(pubKey, sel, success: true, successIncrement: 0.5); @@ -871,6 +873,7 @@ void main() { test('getRecentPaths load does not drop a concurrent add', () async { final pubKey = _hex('k004'); await _seed(svc, pubKey, pathBytes: [0x01], weight: 1.0); + await svc.flushPendingWrites(); final fresh = PathHistoryService(storage); fresh.getRecentPaths(pubKey); diff --git a/test/utils/coalesced_writes_test.dart b/test/utils/coalesced_writes_test.dart new file mode 100644 index 00000000..3e014311 --- /dev/null +++ b/test/utils/coalesced_writes_test.dart @@ -0,0 +1,66 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:meshcore_open/utils/coalesced_writes.dart'; + +const _delay = Duration(milliseconds: 40); + +Future _wait(int ms) => Future.delayed(Duration(milliseconds: ms)); + +void main() { + test('repeated schedules for a key run only the latest write once', () async { + final writes = CoalescedWrites(delay: _delay); + final ran = []; + for (var i = 0; i < 100; i++) { + writes.schedule('channel:a', () async => ran.add('a$i')); + } + writes.schedule('contacts', () async => ran.add('contacts')); + + expect(ran, isEmpty); + await _wait(120); + expect(ran, ['a99', 'contacts']); + expect(writes.hasPending, isFalse); + }); + + test('a steady stream of schedules does not push the write back', () async { + final writes = CoalescedWrites(delay: _delay); + var count = 0; + final stop = DateTime.now().add(const Duration(milliseconds: 300)); + while (DateTime.now().isBefore(stop)) { + writes.schedule('k', () async => count++); + await _wait(5); + } + // Persistence keeps landing while frames keep arriving. + expect(count, greaterThanOrEqualTo(3)); + await writes.flush(); + }); + + test('flush starts every write synchronously and clears the queue', () { + final writes = CoalescedWrites(); + final ran = []; + writes.schedule('a', () async => ran.add('a')); + writes.schedule('b', () async => ran.add('b')); + + final done = writes.flush(); + // Stores read their scoped key on entry, before any await. + expect(ran, ['a', 'b']); + expect(writes.hasPending, isFalse); + return done; + }); + + test('cancel drops a queued write', () async { + final writes = CoalescedWrites(delay: _delay); + var ran = false; + writes.schedule('channel:a', () async => ran = true); + writes.cancel('channel:a'); + await _wait(120); + expect(ran, isFalse); + }); + + test('a failing write does not stop the others', () async { + final writes = CoalescedWrites(); + var ran = false; + writes.schedule('bad', () => throw StateError('boom')); + writes.schedule('good', () async => ran = true); + await writes.flush(); + expect(ran, isTrue); + }); +}