Files
meshcore-open/test/utils/coalesced_writes_test.dart
T
zjs81 603172b3ec 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.
2026-10-24 23:38:28 -07:00

67 lines
2.1 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/utils/coalesced_writes.dart';
const _delay = Duration(milliseconds: 40);
Future<void> _wait(int ms) => Future<void>.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 = <String>[];
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 = <String>[];
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);
});
}