Queue accepted Settings choices before replacement reads

This commit is contained in:
2026-11-08 01:13:27 +00:00
parent 0ddc1dd2f8
commit eee3b70684
4 changed files with 98 additions and 94 deletions
@@ -48,7 +48,6 @@ class DelayedStore implements SettingsTopicStore {
}
class SharedTopicBackend {
final key = Object();
final firstWrite = Completer<void>();
final writes = <String>[];
String? value;
@@ -58,14 +57,13 @@ class SharedQueuedTopicStore implements SettingsTopicStore {
SharedQueuedTopicStore(this.backend);
final SharedTopicBackend backend;
@override
Future<String?> load() =>
orderedLocalStorage(backend.key, () async => backend.value);
Future<String?> load() async => backend.value;
@override
Future<void> save(String topic) => orderedLocalStorage(backend.key, () async {
Future<void> save(String topic) async {
backend.writes.add(topic);
if (backend.writes.length == 1) await backend.firstWrite.future;
backend.value = topic;
});
}
}
void main() {
@@ -151,63 +149,66 @@ void main() {
await tester.pumpAndSettle();
expect(find.text('notifications page'), findsOneWidget);
});
testWidgets('recreated owner wins over disposed owner queued selections', (
for (final newerChoice in [false, true]) {
testWidgets('recreation preserves accepted B; newer choice: $newerChoice', (
tester,
) async {
final backend = SharedTopicBackend();
final store = SharedQueuedTopicStore(backend);
await tester.pumpWidget(host(store: store));
await tester.pumpAndSettle();
await selectSettingsTopic(tester, 'Connection');
expect(backend.writes, ['connection']);
await selectSettingsTopic(tester, 'Notifications');
expect(backend.writes, ['connection']);
await tester.pumpWidget(const SizedBox.shrink());
await tester.pumpWidget(host(store: store));
if (newerChoice) await selectSettingsTopic(tester, 'Tools');
backend.firstWrite.complete();
await tester.pumpAndSettle();
expect(
find.text(newerChoice ? 'tools page' : 'notifications page'),
findsOneWidget,
);
expect(backend.value, newerChoice ? 'tools' : 'notifications');
expect(backend.writes, [
'connection',
'notifications',
if (newerChoice) 'tools',
]);
});
}
testWidgets('production replacement reads after all accepted old writes', (
tester,
) async {
final backend = SharedTopicBackend();
await tester.pumpWidget(host(store: SharedQueuedTopicStore(backend)));
await tester.pumpAndSettle();
await selectSettingsTopic(tester, 'Connection');
expect(backend.writes, ['connection']);
await selectSettingsTopic(tester, 'Notifications');
expect(backend.writes, [
'connection',
], reason: 'old second choice waits behind first write');
await tester.pumpWidget(const SizedBox.shrink());
await tester.pumpWidget(host(store: SharedQueuedTopicStore(backend)));
// Restoration waits for the old write, while this new owner remains usable.
await selectSettingsTopic(tester, 'Tools');
backend.firstWrite.complete();
await tester.pumpAndSettle();
expect(find.text('tools page'), findsOneWidget);
expect(backend.value, 'tools');
expect(backend.writes, ['connection', 'tools']);
});
testWidgets(
'production reads await pending writes and disposed saves are skipped',
(tester) async {
SharedPreferences.setMockInitialValues({});
var current = true;
final store = SharedPreferencesSettingsTopicStore(
'ownership-check',
canSave: () => current,
);
final gate = Completer<void>();
final oldWrite = orderedLocalStorage(store.key, () async {
await gate.future;
await (await SharedPreferences.getInstance()).setString(
store.key,
'connection',
);
});
var readFinished = false;
final read = store.load().then((value) {
readFinished = true;
return value;
});
final staleQueuedSave = store.save('notifications');
await tester.pump();
expect(readFinished, isFalse);
current = false;
gate.complete();
await oldWrite;
expect(await read, 'connection');
await staleQueuedSave;
expect(
(await SharedPreferences.getInstance()).getString(store.key),
SharedPreferences.setMockInitialValues({});
const oldStore = SharedPreferencesSettingsTopicStore('ownership-check');
final gate = Completer<void>();
final oldA = orderedLocalStorage(oldStore.key, () async {
await gate.future;
await (await SharedPreferences.getInstance()).setString(
oldStore.key,
'connection',
);
},
);
});
final oldB = oldStore.save('notifications');
final replacement = SharedPreferencesSettingsTopicStore('ownership-check');
var readFinished = false;
final read = replacement.load().then((value) {
readFinished = true;
return value;
});
await tester.pump();
expect(readFinished, isFalse);
gate.complete();
await oldA;
await oldB;
expect(await read, 'notifications');
await replacement.save('tools');
expect(
(await SharedPreferences.getInstance()).getString(oldStore.key),
'tools',
);
});
}