perf(home): 冷启动提前拉取首页数据

This commit is contained in:
Zhou Weidong committed 2026-10-20 19:51:41 +08:00
1 parent 1199db760f
commit 9b31d83e39
7 files changed
+255 -113

No files matched your search

+44 -14
View File
@@ -1,3 +1,5 @@
import 'dart:async';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart';
@@ -11,9 +13,13 @@ void main() {
SharedPreferencesAsyncPlatform.instance =
InMemorySharedPreferencesAsync.empty();
AppConfig.clearRuntimeBaseUrl();
AppConfig.ready = Future<void>.value();
});
tearDown(AppConfig.clearRuntimeBaseUrl);
tearDown(() {
AppConfig.clearRuntimeBaseUrl();
AppConfig.ready = Future<void>.value();
});
test('should_normalize_http_urls_and_reject_invalid_values', () {
expect(
@@ -60,20 +66,39 @@ void main() {
expect(AppConfig.devHostBaseUrl, 'http://192.168.31.187:8081');
});
test('should_keep_loopback_priority_when_probe_succeeds', () async {
final attempts = <String>[];
test(
'should_probe_all_candidates_in_parallel_before_applying_priority',
() async {
final attempts = <String>[];
var active = 0;
var maxActive = 0;
final allStarted = Completer<void>();
await AppConfig.probeAndApplyBaseUrl(
fallbackBaseUrl: AppConfig.devHostBaseUrl,
probe: (baseUrl) async {
attempts.add(baseUrl);
return baseUrl == AppConfig.lanBaseUrl;
},
);
final probing = AppConfig.probeAndApplyBaseUrl(
fallbackBaseUrl: AppConfig.devHostBaseUrl,
probe: (baseUrl) async {
attempts.add(baseUrl);
active++;
if (active > maxActive) maxActive = active;
if (active == 3) allStarted.complete();
await allStarted.future;
active--;
return baseUrl == AppConfig.lanBaseUrl ||
baseUrl == AppConfig.devHostBaseUrl;
},
);
expect(attempts, [AppConfig.lanBaseUrl]);
expect(AppConfig.apiBaseUrl, AppConfig.lanBaseUrl);
});
await probing.timeout(const Duration(seconds: 1));
expect(attempts, [
AppConfig.lanBaseUrl,
AppConfig.devHostBaseUrl,
AppConfig.publicBaseUrl,
]);
expect(maxActive, 3);
expect(AppConfig.apiBaseUrl, AppConfig.lanBaseUrl);
},
);
test('should_probe_loopback_dev_host_then_public_in_order', () async {
final attempts = <String>[];
@@ -125,7 +150,12 @@ void main() {
},
);
expect(attempts, [custom]);
expect(attempts, [
custom,
AppConfig.lanBaseUrl,
AppConfig.devHostBaseUrl,
AppConfig.publicBaseUrl,
]);
expect(AppConfig.apiBaseUrl, custom);
});