diff --git a/lib/services/local_network_access.dart b/lib/services/local_network_access.dart index 6a6b9e10..f04cc4ac 100644 --- a/lib/services/local_network_access.dart +++ b/lib/services/local_network_access.dart @@ -33,6 +33,11 @@ class LocalNetworkAccess { static DateTime? _cachedAt; static Future? _inFlight; + /// Bumped by [invalidate]. A probe that started in an earlier generation + /// describes the world before whatever prompted the invalidation, so it must + /// not write its answer back into the cache. + static int _generation = 0; + /// The last known status without probing, or null if nothing is cached. static LocalNetworkStatus? get cached => _isFresh ? _cached : null; @@ -58,6 +63,7 @@ class LocalNetworkAccess { } static Future _probe() async { + final generation = _generation; LocalNetworkStatus result; try { result = await LocalNetworkPermission.check(timeout: probeTimeout); @@ -66,8 +72,13 @@ class LocalNetworkAccess { // A broken probe must not lock the user out of their trainer app. result = LocalNetworkStatus.unknown; } - _cached = result; - _cachedAt = now(); + // Still answer whoever is awaiting this probe, but a stale generation must + // not repopulate the cache: the user has been to System Settings since it + // started, so this result describes the permission as it was beforehand. + if (generation == _generation) { + _cached = result; + _cachedAt = now(); + } return result; } @@ -76,8 +87,12 @@ class LocalNetworkAccess { /// Call this whenever the user has been somewhere they could have changed /// the answer — the System Settings trip in particular. static void invalidate() { + _generation++; _cached = null; _cachedAt = null; + // Drop the in-flight probe too, so the next caller starts a fresh one + // rather than being handed the pre-invalidation answer. + _inFlight = null; } /// Whether local-network features should be allowed to run. @@ -92,7 +107,6 @@ class LocalNetworkAccess { @visibleForTesting static void resetForTest() { invalidate(); - _inFlight = null; now = DateTime.now; } } diff --git a/test/services/local_network_access_test.dart b/test/services/local_network_access_test.dart index 5f743f6e..e1ff92e2 100644 --- a/test/services/local_network_access_test.dart +++ b/test/services/local_network_access_test.dart @@ -96,6 +96,35 @@ void main() { expect(probes, 1); }); + test('a probe in flight when the cache is invalidated does not repopulate it', () async { + // The real sequence: a probe starts, the user leaves for System Settings + // and flips the toggle, and the pre-Settings answer lands afterwards. It + // must not become the cached truth. + final gate = Completer(); + mockNative(() => gate.future); + + final pending = LocalNetworkAccess.status(); + LocalNetworkAccess.invalidate(); + gate.complete('denied'); + expect(await pending, LocalNetworkStatus.denied, reason: 'the caller still gets its answer'); + + expect(LocalNetworkAccess.cached, isNull, reason: 'but the stale answer must not come back'); + }); + + test('invalidate makes the next caller start a fresh probe', () async { + final gate = Completer(); + mockNative(() => gate.future); + final stale = LocalNetworkAccess.status(); + + LocalNetworkAccess.invalidate(); + mockNative(() => 'granted'); + expect(await LocalNetworkAccess.status(), LocalNetworkStatus.granted); + + gate.complete('denied'); + await stale; + expect(LocalNetworkAccess.cached, LocalNetworkStatus.granted); + }); + test('a probe that failed does not wedge later probes', () async { mockNative(() => throw PlatformException(code: 'probe_unavailable')); expect(await LocalNetworkAccess.status(), LocalNetworkStatus.unknown); diff --git a/test/utils/local_network_requirement_test.dart b/test/utils/local_network_requirement_test.dart index 7cc0e3af..0ee61f6a 100644 --- a/test/utils/local_network_requirement_test.dart +++ b/test/utils/local_network_requirement_test.dart @@ -2,8 +2,8 @@ import 'dart:io'; import 'package:bike_control/gen/l10n.dart'; import 'package:bike_control/main.dart' show screenshotMode; -import 'package:bike_control/services/local_network_access.dart'; import 'package:bike_control/bluetooth/emulation/emulated_ble_platform.dart'; +import 'package:bike_control/services/local_network_access.dart'; import 'package:bike_control/utils/core.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:shared_preferences/shared_preferences.dart';