feat(#919): move the channel-history merge off the UI isolate
BlobStore.mergeOffThread runs the whole read-modify-write through compute(): stored JSON and incoming JSON in, merged JSON out, so only strings cross the isolate boundary and the object graph never does. Keeps the per-key serialisation that stops concurrent saves clobbering each other. channel_message_store.saveChannelMessages now encodes only the INCOMING messages on the UI isolate (normally one message, measured 0.002 ms) and hands the 5.38 MB decode-merge-encode to another isolate, where it measured 134 ms per save and crossed Android's 5s input-dispatch watchdog in a burst (#334). Fails closed: a stored blob that will not decode throws out of the merge, is logged, and the key is NOT written, which is the same refusal the inline code made. compute runs inline on web, which has no isolates; that is documented behaviour, not a silent fallback, and #920 removes the cost for every platform. _messageToJson, _messageFromJson, _mergeKey, _repeatToJson and _repeatFromJson became static so the top-level merge can call them; none touched instance state. 9 new tests cover merge equivalence, timestamp ordering, empty/absent blobs, the throw on corrupt JSON, fail-closed leaving the blob byte-identical, and three concurrent saves all surviving. Fixtures go through the store's own encoder rather than hand-written maps, after a hand-rolled fixture rotted on a non-null field. Refs #334, #917 Agent: TealGlen (session c61ce804) Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>feat/920-message-rows
parent
61b372bb13
commit
4ddb6a4358
@ -0,0 +1,220 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:drift/native.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/models/channel_message.dart';
|
||||
import 'package:meshcore_open/storage/channel_message_store.dart';
|
||||
import 'package:meshcore_open/storage/drift/blob_store.dart';
|
||||
import 'package:meshcore_open/storage/drift/offband_database.dart';
|
||||
import 'package:meshcore_open/storage/prefs_manager.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
/// #919: the blob read-modify-write moved off the UI isolate. These tests pin
|
||||
/// the two things that must not change with it: the merge is semantically what
|
||||
/// it was, and a blob that cannot be decoded is never overwritten.
|
||||
void main() {
|
||||
late OffbandDatabase db;
|
||||
|
||||
setUp(() async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
PrefsManager.reset();
|
||||
await PrefsManager.initialize();
|
||||
db = OffbandDatabase(NativeDatabase.memory());
|
||||
BlobStore.overrideForTest(BlobStore(db));
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await db.close();
|
||||
BlobStore.clearTestOverride();
|
||||
});
|
||||
|
||||
ChannelMessage msg(int i, {String? text}) => ChannelMessage(
|
||||
senderName: 's',
|
||||
text: text ?? 'm$i',
|
||||
timestamp: DateTime.fromMillisecondsSinceEpoch(1000 + i),
|
||||
isOutgoing: false,
|
||||
channelIndex: 0,
|
||||
messageId: 'id$i',
|
||||
);
|
||||
|
||||
// Fixtures go through the store's OWN encoder rather than a hand-written map.
|
||||
// A hand-rolled fixture silently rots the moment a non-null field is added to
|
||||
// the schema, which is exactly how the first run of this test failed.
|
||||
Future<String> encodeVia(List<ChannelMessage> ms) async {
|
||||
final fixtureDb = OffbandDatabase(NativeDatabase.memory());
|
||||
final previous = BlobStore.instance;
|
||||
BlobStore.overrideForTest(BlobStore(fixtureDb));
|
||||
try {
|
||||
final store = ChannelMessageStore()..setPublicKeyHex = 'f' * 20;
|
||||
await store.saveChannelMessages(0, ms);
|
||||
final keys = await BlobStore.instance.keysWithPrefix('channel_messages_');
|
||||
return (await BlobStore.instance.read(keys.single))!;
|
||||
} finally {
|
||||
BlobStore.overrideForTest(previous);
|
||||
await fixtureDb.close();
|
||||
}
|
||||
}
|
||||
|
||||
group('mergeChannelHistoryJson', () {
|
||||
test('adds incoming messages to the stored history', () async {
|
||||
final stored = await encodeVia([msg(1), msg(2)]);
|
||||
final incoming = await encodeVia([msg(3)]);
|
||||
|
||||
final out =
|
||||
jsonDecode(mergeChannelHistoryJson((stored, incoming)))
|
||||
as List<dynamic>;
|
||||
|
||||
expect(out.length, 3);
|
||||
expect(
|
||||
out.map((e) => (e as Map<String, dynamic>)['messageId']),
|
||||
containsAll(['id1', 'id2', 'id3']),
|
||||
);
|
||||
});
|
||||
|
||||
test('incoming wins on the same id, so edits are captured', () async {
|
||||
final stored = await encodeVia([msg(1, text: 'original')]);
|
||||
final incoming = await encodeVia([msg(1, text: 'edited')]);
|
||||
|
||||
final out =
|
||||
jsonDecode(mergeChannelHistoryJson((stored, incoming)))
|
||||
as List<dynamic>;
|
||||
|
||||
expect(
|
||||
out.length,
|
||||
1,
|
||||
reason: 'same messageId must upsert, not duplicate',
|
||||
);
|
||||
expect((out.single as Map<String, dynamic>)['text'], 'edited');
|
||||
});
|
||||
|
||||
test('result is sorted by timestamp regardless of input order', () async {
|
||||
final stored = await encodeVia([msg(9), msg(3)]);
|
||||
final incoming = await encodeVia([msg(5)]);
|
||||
|
||||
final out =
|
||||
jsonDecode(mergeChannelHistoryJson((stored, incoming)))
|
||||
as List<dynamic>;
|
||||
final stamps = out
|
||||
.map((e) => (e as Map<String, dynamic>)['timestamp'] as int)
|
||||
.toList();
|
||||
|
||||
expect(stamps, [...stamps]..sort());
|
||||
});
|
||||
|
||||
test(
|
||||
'an empty or absent stored blob merges into just the incoming',
|
||||
() async {
|
||||
final incoming = await encodeVia([msg(1)]);
|
||||
|
||||
for (final stored in <String?>[null, '']) {
|
||||
final out =
|
||||
jsonDecode(mergeChannelHistoryJson((stored, incoming)))
|
||||
as List<dynamic>;
|
||||
expect(out.length, 1, reason: 'stored=$stored');
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'THROWS on an undecodable stored blob rather than truncating',
|
||||
() async {
|
||||
final incoming = await encodeVia([msg(1)]);
|
||||
|
||||
// The whole point: swallowing this would merge into an empty base and
|
||||
// silently replace real history with one message.
|
||||
expect(
|
||||
() => mergeChannelHistoryJson(('{not json at all', incoming)),
|
||||
throwsA(anything),
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('BlobStore.mergeOffThread', () {
|
||||
test('writes the merged result', () async {
|
||||
final blobs = BlobStore.instance;
|
||||
await blobs.write('k', await encodeVia([msg(1)]));
|
||||
|
||||
final wrote = await blobs.mergeOffThread(
|
||||
'k',
|
||||
await encodeVia([msg(2)]),
|
||||
mergeChannelHistoryJson,
|
||||
);
|
||||
|
||||
expect(wrote, isTrue);
|
||||
final out = jsonDecode((await blobs.read('k'))!) as List<dynamic>;
|
||||
expect(out.length, 2);
|
||||
});
|
||||
|
||||
test(
|
||||
'FAILS CLOSED on a corrupt stored blob: returns false and leaves it byte-identical',
|
||||
() async {
|
||||
final blobs = BlobStore.instance;
|
||||
const corrupt = '{not json at all';
|
||||
await blobs.write('k', corrupt);
|
||||
|
||||
final wrote = await blobs.mergeOffThread(
|
||||
'k',
|
||||
await encodeVia([msg(1)]),
|
||||
mergeChannelHistoryJson,
|
||||
);
|
||||
|
||||
expect(
|
||||
wrote,
|
||||
isFalse,
|
||||
reason: 'a failed merge must not report success',
|
||||
);
|
||||
expect(
|
||||
await blobs.read('k'),
|
||||
corrupt,
|
||||
reason: 'the unreadable blob must survive untouched, not be replaced',
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('a merge returning null leaves the key untouched', () async {
|
||||
final blobs = BlobStore.instance;
|
||||
await blobs.write('k', 'original');
|
||||
|
||||
final wrote = await blobs.mergeOffThread('k', 'x', _alwaysNull);
|
||||
|
||||
expect(wrote, isFalse);
|
||||
expect(await blobs.read('k'), 'original');
|
||||
});
|
||||
|
||||
test('concurrent merges on one key do not clobber each other', () async {
|
||||
final blobs = BlobStore.instance;
|
||||
await blobs.write('k', await encodeVia([msg(0)]));
|
||||
|
||||
// Fired together: without the per-key serialisation one read-modify-write
|
||||
// would overwrite the other and a message would vanish.
|
||||
await Future.wait([
|
||||
blobs.mergeOffThread(
|
||||
'k',
|
||||
await encodeVia([msg(1)]),
|
||||
mergeChannelHistoryJson,
|
||||
),
|
||||
blobs.mergeOffThread(
|
||||
'k',
|
||||
await encodeVia([msg(2)]),
|
||||
mergeChannelHistoryJson,
|
||||
),
|
||||
blobs.mergeOffThread(
|
||||
'k',
|
||||
await encodeVia([msg(3)]),
|
||||
mergeChannelHistoryJson,
|
||||
),
|
||||
]);
|
||||
|
||||
final out = jsonDecode((await blobs.read('k'))!) as List<dynamic>;
|
||||
expect(
|
||||
out.length,
|
||||
4,
|
||||
reason: 'all three concurrent saves plus the original must survive',
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// Top-level so `compute` can send it.
|
||||
String? _alwaysNull((String?, String) input) => null;
|
||||
Loading…
Reference in new issue