Files
meshcore-open/test/utils/coalesced_writes_test.dart
T
zjs81 2d56d5e349 Store histories and the tile index in SQLite
Message, channel and route histories move from whole-blob preferences to
indexed SQLite rows on native platforms, so chats page from storage instead
of loading a full conversation. Web keeps the preferences store. Existing
histories are imported once on the first launch.

Linux and Windows keep the map tile index in SQLite and import the old JSON
index on first use.

Unreadable legacy data no longer blocks saves, migrations or startup: it is
left in place and skipped. If the database cannot be opened, the run keeps
histories in preferences and the next launch merges them in.

Also adds a performance profiling reference and a script that clears the
Dart HTTP profile.
2026-11-09 22:32:40 -07:00

109 lines
3.3 KiB
Dart

import 'dart:async';
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);
});
test('flush waits for writes already started by the timer', () async {
final writes = CoalescedWrites(delay: Duration.zero);
final started = Completer<void>();
final persisted = Completer<void>();
writes.schedule('history', () {
started.complete();
return persisted.future;
});
await started.future;
var finished = false;
final flush = writes.flush().then((_) => finished = true);
await _wait(0);
expect(finished, isFalse);
persisted.complete();
await flush;
expect(finished, isTrue);
});
test(
'a later flush waits for both existing and newly queued writes',
() async {
final writes = CoalescedWrites();
final first = Completer<void>();
final second = Completer<void>();
writes.schedule('first', () => first.future);
final firstFlush = writes.flush();
writes.schedule('second', () => second.future);
var finished = false;
final secondFlush = writes.flush().then((_) => finished = true);
second.complete();
await _wait(0);
expect(finished, isFalse);
first.complete();
await Future.wait([firstFlush, secondFlush]);
expect(finished, isTrue);
},
);
}