From dc67d29e883d35446af80dc2524b3496a31369e7 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Sun, 19 Apr 2026 00:02:06 +0200 Subject: [PATCH] Don't do real DNS checks during tests --- ios/Podfile.lock | 6 ----- lib/providers/network_provider.dart | 35 ++++++++++++++++------------- pubspec.lock | 4 ++-- pubspec.yaml | 3 +++ test/fake_connectivity.dart | 18 ++++++++++----- 5 files changed, 37 insertions(+), 29 deletions(-) diff --git a/ios/Podfile.lock b/ios/Podfile.lock index 3f3f97be6..7787c2218 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -17,8 +17,6 @@ PODS: - Flutter - FlutterMacOS - powersync-sqlite-core (~> 0.4.11) - - rive_common (0.0.1): - - Flutter - shared_preferences_foundation (0.0.1): - Flutter - FlutterMacOS @@ -62,7 +60,6 @@ DEPENDENCIES: - integration_test (from `.symlinks/plugins/integration_test/ios`) - package_info_plus (from `.symlinks/plugins/package_info_plus/ios`) - powersync_flutter_libs (from `.symlinks/plugins/powersync_flutter_libs/darwin`) - - rive_common (from `.symlinks/plugins/rive_common/ios`) - shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`) - sqlite3_flutter_libs (from `.symlinks/plugins/sqlite3_flutter_libs/darwin`) - url_launcher_ios (from `.symlinks/plugins/url_launcher_ios/ios`) @@ -90,8 +87,6 @@ EXTERNAL SOURCES: :path: ".symlinks/plugins/package_info_plus/ios" powersync_flutter_libs: :path: ".symlinks/plugins/powersync_flutter_libs/darwin" - rive_common: - :path: ".symlinks/plugins/rive_common/ios" shared_preferences_foundation: :path: ".symlinks/plugins/shared_preferences_foundation/darwin" sqlite3_flutter_libs: @@ -111,7 +106,6 @@ SPEC CHECKSUMS: package_info_plus: af8e2ca6888548050f16fa2f1938db7b5a5df499 powersync-sqlite-core: 116ecf3391859445f1477f8c230914e04d9874e7 powersync_flutter_libs: 20381b877038d27d8fa918f8c3fff08bfc3925d0 - rive_common: dd421daaf9ae69f0125aa761dd96abd278399952 shared_preferences_foundation: 7036424c3d8ec98dfe75ff1667cb0cd531ec82bb sqlite3: a51c07cf16e023d6c48abd5e5791a61a47354921 sqlite3_flutter_libs: b3e120efe9a82017e5552a620f696589ed4f62ab diff --git a/lib/providers/network_provider.dart b/lib/providers/network_provider.dart index aa1c21023..bd38e85d3 100644 --- a/lib/providers/network_provider.dart +++ b/lib/providers/network_provider.dart @@ -20,11 +20,30 @@ import 'dart:async'; import 'dart:io'; import 'package:connectivity_plus/connectivity_plus.dart'; +import 'package:flutter/foundation.dart'; import 'package:logging/logging.dart'; import 'package:riverpod_annotation/riverpod_annotation.dart'; part 'network_provider.g.dart'; +/// Returns whether the device can actually reach the public internet (vs. +/// just being attached to a network adapter — think captive portals). +/// +/// Defaults to a DNS lookup against `google.com`. Tests can swap this for a +/// deterministic stub via `installFakeConnectivity()` so the test runner +/// doesn't make real network calls. +@visibleForTesting +Future Function(Duration timeout) reachabilityCheck = _defaultReachabilityCheck; + +Future _defaultReachabilityCheck(Duration timeout) async { + try { + final result = await InternetAddress.lookup('google.com').timeout(timeout); + return result.isNotEmpty && result.first.rawAddress.isNotEmpty; + } catch (_) { + return false; + } +} + @Riverpod(keepAlive: true) class NetworkStatus extends _$NetworkStatus { final _logger = Logger('NetworkStatus'); @@ -61,14 +80,6 @@ class NetworkStatus extends _$NetworkStatus { Future check({Duration timeout = const Duration(seconds: 1)}) async { final conn = await Connectivity().checkConnectivity(); final ok = await _hasConnectionAndInternet(conn, timeout: timeout); - - // _hasConnectionAndInternet does a real DNS lookup that can take up to - // [timeout]. Tests routinely tear down the container before that returns, - // so guard against writing to a disposed notifier. - if (!ref.mounted) { - return ok; - } - state = ok; return ok; } @@ -80,12 +91,6 @@ class NetworkStatus extends _$NetworkStatus { if (!conn.any((c) => allowed.contains(c))) { return false; } - - try { - final result = await InternetAddress.lookup('google.com').timeout(timeout); - return result.isNotEmpty && result.first.rawAddress.isNotEmpty; - } catch (_) { - return false; - } + return reachabilityCheck(timeout); } } diff --git a/pubspec.lock b/pubspec.lock index 6ec458ea6..004c55b07 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -250,7 +250,7 @@ packages: source: hosted version: "7.1.1" connectivity_plus_platform_interface: - dependency: transitive + dependency: "direct dev" description: name: connectivity_plus_platform_interface sha256: "3c09627c536d22fd24691a905cdd8b14520de69da52c7a97499c8be5284a32ed" @@ -1037,7 +1037,7 @@ packages: source: hosted version: "3.1.6" plugin_platform_interface: - dependency: transitive + dependency: "direct dev" description: name: plugin_platform_interface sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02" diff --git a/pubspec.yaml b/pubspec.yaml index f827693fa..6c98de5ec 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -80,12 +80,15 @@ dev_dependencies: sdk: flutter build_runner: ^2.12.2 cider: ^0.2.9 + # Used to stub the connectivity_plus platform interface + connectivity_plus_platform_interface: ^2.1.0 drift_dev: ^2.31.0 flutter_lints: ^6.0.0 freezed: ^3.2.0 json_serializable: ^6.13.0 mockito: ^5.6.4 network_image_mock: ^2.1.1 + plugin_platform_interface: ^2.1.8 riverpod_generator: ^4.0.3 shared_preferences_platform_interface: ^2.0.0 diff --git a/test/fake_connectivity.dart b/test/fake_connectivity.dart index bd7aa20ca..1cc6b6fbd 100644 --- a/test/fake_connectivity.dart +++ b/test/fake_connectivity.dart @@ -18,20 +18,26 @@ import 'package:connectivity_plus_platform_interface/connectivity_plus_platform_interface.dart'; import 'package:plugin_platform_interface/plugin_platform_interface.dart'; +import 'package:wger/providers/network_provider.dart'; /// Swaps [ConnectivityPlatform.instance] with a fake that always reports -/// Wi-Fi and emits no change events. +/// Wi-Fi and emits no change events, *and* replaces the DNS reachability +/// check in [NetworkStatus] with a deterministic stub. /// -/// This is the officially recommended pattern for mocking `connectivity_plus` -/// in Dart VM unit tests — the plugin's method/event channels have no -/// implementation under the test runner, and any real call would throw -/// `MissingPluginException`. See: +/// Without this, `NetworkStatus.check()` would do a real `InternetAddress.lookup` +/// against `google.com` from every test that touches a provider depending on +/// the network state — slow, non-deterministic (CI vs no CI internet), and +/// hits an external host from the test runner. +/// +/// The [ConnectivityPlatform.instance] swap follows the officially recommended +/// pattern from connectivity_plus' own tests: /// https://github.com/fluttercommunity/plus_plugins/blob/main/packages/connectivity_plus/connectivity_plus/test/connectivity_test.dart /// -/// Call once per test file (top of `main()` is fine — the static swap sticks +/// Call once per test file (top of `main()` is fine — the static swaps stick /// for the entire file). void installFakeConnectivity() { ConnectivityPlatform.instance = _FakeConnectivityPlatform(); + reachabilityCheck = (_) async => true; } class _FakeConnectivityPlatform extends ConnectivityPlatform