Move send controls into Routing and split retry limits
Direct and flood attempts are separate settings, available with auto route rotation off. Saved single retry limits migrate to that split, and reset restores the whole Routing section. Co-authored-by: zjs81 <zjs81@users.noreply.github.com>
This commit is contained in:
69 files changed
+2199
-784
No files matched your search
@@ -0,0 +1,168 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'package:meshcore_open/models/contact.dart';
|
||||
import 'package:meshcore_open/models/message.dart';
|
||||
import 'package:meshcore_open/models/path_selection.dart';
|
||||
import 'package:meshcore_open/services/app_settings_service.dart';
|
||||
import 'package:meshcore_open/services/message_retry_service.dart';
|
||||
import 'package:meshcore_open/storage/prefs_manager.dart';
|
||||
|
||||
Contact _contact({int pathLength = 1, List<int> path = const [0x11]}) =>
|
||||
Contact(
|
||||
publicKey: Uint8List.fromList(List<int>.generate(32, (i) => i + 1)),
|
||||
name: 'Test',
|
||||
type: 1,
|
||||
pathLength: pathLength,
|
||||
path: Uint8List.fromList(path),
|
||||
lastSeen: DateTime.now(),
|
||||
);
|
||||
|
||||
void main() {
|
||||
setUp(() async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
await PrefsManager.initialize();
|
||||
});
|
||||
|
||||
test('rotation sends direct attempts, then flood, then stops', () async {
|
||||
final settings = AppSettingsService();
|
||||
await settings.loadSettings();
|
||||
await settings.updateSettings(
|
||||
settings.settings.copyWith(
|
||||
autoRouteRotationEnabled: true,
|
||||
directRetries: 1,
|
||||
floodRetries: 1,
|
||||
),
|
||||
);
|
||||
|
||||
final service = MessageRetryService(
|
||||
sentWatchdogGrace: const Duration(milliseconds: 5),
|
||||
);
|
||||
addTearDown(service.dispose);
|
||||
final sends = <int>[];
|
||||
final thresholds = <int>[];
|
||||
service.initialize(
|
||||
RetryServiceConfig(
|
||||
sendMessage: (_, _, attempt, _) async {
|
||||
sends.add(attempt);
|
||||
},
|
||||
addMessage: (_, _) {},
|
||||
updateMessage: (_) {},
|
||||
calculateTimeout: (_, _, {String? contactKey, int? deviceTimeoutMs}) =>
|
||||
15,
|
||||
appSettingsService: settings,
|
||||
selectRetryPath: (_, attemptIndex, directRetries, _) {
|
||||
thresholds.add(directRetries);
|
||||
if (attemptIndex >= directRetries) {
|
||||
return const PathSelection(
|
||||
pathBytes: [],
|
||||
hopCount: -1,
|
||||
useFlood: true,
|
||||
);
|
||||
}
|
||||
return const PathSelection(
|
||||
pathBytes: [0x22],
|
||||
hopCount: 1,
|
||||
useFlood: false,
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
await service.sendMessageWithRetry(contact: _contact(), text: 'hello');
|
||||
await pumpEventQueue();
|
||||
await Future<void>.delayed(const Duration(milliseconds: 1400));
|
||||
|
||||
expect(sends, [0, 1]);
|
||||
expect(thresholds, [1, 1]);
|
||||
});
|
||||
|
||||
test('rotation off keeps retrying a direct path without flooding', () async {
|
||||
final settings = AppSettingsService();
|
||||
await settings.loadSettings();
|
||||
await settings.updateSettings(
|
||||
settings.settings.copyWith(
|
||||
autoRouteRotationEnabled: false,
|
||||
directRetries: 2,
|
||||
floodRetries: 5,
|
||||
),
|
||||
);
|
||||
|
||||
final service = MessageRetryService(
|
||||
sentWatchdogGrace: const Duration(milliseconds: 5),
|
||||
);
|
||||
addTearDown(service.dispose);
|
||||
final sends = <int>[];
|
||||
var picked = false;
|
||||
service.initialize(
|
||||
RetryServiceConfig(
|
||||
sendMessage: (_, _, attempt, _) async {
|
||||
sends.add(attempt);
|
||||
},
|
||||
addMessage: (_, _) {},
|
||||
updateMessage: (_) {},
|
||||
calculateTimeout: (_, _, {String? contactKey, int? deviceTimeoutMs}) =>
|
||||
15,
|
||||
appSettingsService: settings,
|
||||
selectRetryPath: (_, _, _, _) {
|
||||
picked = true;
|
||||
return const PathSelection(
|
||||
pathBytes: [],
|
||||
hopCount: -1,
|
||||
useFlood: true,
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
await service.sendMessageWithRetry(contact: _contact(), text: 'hello');
|
||||
await pumpEventQueue();
|
||||
await Future<void>.delayed(const Duration(milliseconds: 1400));
|
||||
|
||||
expect(sends, [0, 1]);
|
||||
expect(picked, isFalse);
|
||||
});
|
||||
|
||||
test('a flood path without rotation stops after the flood budget', () async {
|
||||
final settings = AppSettingsService();
|
||||
await settings.loadSettings();
|
||||
await settings.updateSettings(
|
||||
settings.settings.copyWith(
|
||||
autoRouteRotationEnabled: false,
|
||||
directRetries: 4,
|
||||
floodRetries: 1,
|
||||
),
|
||||
);
|
||||
|
||||
final service = MessageRetryService(
|
||||
sentWatchdogGrace: const Duration(milliseconds: 5),
|
||||
);
|
||||
addTearDown(service.dispose);
|
||||
final latest = <Message>[];
|
||||
final sends = <int>[];
|
||||
service.initialize(
|
||||
RetryServiceConfig(
|
||||
sendMessage: (_, _, attempt, _) async {
|
||||
sends.add(attempt);
|
||||
},
|
||||
addMessage: (_, message) => latest.add(message),
|
||||
updateMessage: (message) => latest.add(message),
|
||||
calculateTimeout: (_, _, {String? contactKey, int? deviceTimeoutMs}) =>
|
||||
15,
|
||||
appSettingsService: settings,
|
||||
),
|
||||
);
|
||||
|
||||
await service.sendMessageWithRetry(
|
||||
contact: _contact(pathLength: -1, path: const []),
|
||||
text: 'hello',
|
||||
);
|
||||
await pumpEventQueue();
|
||||
await Future<void>.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
expect(sends, [0]);
|
||||
expect(latest.last.status, MessageStatus.failed);
|
||||
});
|
||||
}
|
||||
@@ -56,7 +56,12 @@ void main() {
|
||||
);
|
||||
|
||||
test('missing RESP_CODE_SENT times out and retries', () async {
|
||||
await service.sendMessageWithRetry(contact: _contact(), text: 'hello');
|
||||
// A direct path uses the direct budget (default 4). A flood path would
|
||||
// stop after the default single flood attempt.
|
||||
await service.sendMessageWithRetry(
|
||||
contact: _contact(pathLength: 1, path: const [0x11]),
|
||||
text: 'hello',
|
||||
);
|
||||
await pumpEventQueue();
|
||||
expect(sends.map((s) => s.attempt), [0]);
|
||||
|
||||
@@ -80,7 +85,10 @@ void main() {
|
||||
});
|
||||
|
||||
test('late ACK for an attempt whose SENT was lost still delivers', () async {
|
||||
await service.sendMessageWithRetry(contact: _contact(), text: 'hello');
|
||||
await service.sendMessageWithRetry(
|
||||
contact: _contact(pathLength: 1, path: const [0x11]),
|
||||
text: 'hello',
|
||||
);
|
||||
await pumpEventQueue();
|
||||
|
||||
// Let the watchdog fire; the retry is now waiting on its backoff.
|
||||
|
||||
@@ -121,7 +121,7 @@ void main() {
|
||||
final pick = svc.selectPathForAttempt(
|
||||
pubKey,
|
||||
attemptIndex: attemptIndex,
|
||||
maxRetries: 5,
|
||||
directRetries: 4,
|
||||
recentSelections: recent,
|
||||
);
|
||||
final key = pick.useFlood
|
||||
@@ -139,7 +139,7 @@ void main() {
|
||||
final selection = svc.selectPathForAttempt(
|
||||
pubKey,
|
||||
attemptIndex: 0,
|
||||
maxRetries: 5,
|
||||
directRetries: 4,
|
||||
);
|
||||
expect(selection.useFlood, isTrue);
|
||||
});
|
||||
@@ -150,7 +150,7 @@ void main() {
|
||||
final selection = svc.selectPathForAttempt(
|
||||
pubKey,
|
||||
attemptIndex: 0,
|
||||
maxRetries: 0,
|
||||
directRetries: 0,
|
||||
);
|
||||
expect(selection.useFlood, isTrue);
|
||||
});
|
||||
@@ -163,7 +163,7 @@ void main() {
|
||||
final selection = svc.selectPathForAttempt(
|
||||
pubKey,
|
||||
attemptIndex: i,
|
||||
maxRetries: 5,
|
||||
directRetries: 4,
|
||||
);
|
||||
expect(
|
||||
selection.useFlood,
|
||||
@@ -274,7 +274,7 @@ void main() {
|
||||
final pick = svc.selectPathForAttempt(
|
||||
pubKey,
|
||||
attemptIndex: 0,
|
||||
maxRetries: 5,
|
||||
directRetries: 4,
|
||||
);
|
||||
expect(pick.useFlood, isFalse);
|
||||
expect(pick.hopCount, 0);
|
||||
@@ -299,21 +299,28 @@ void main() {
|
||||
expect(resolved.pathBytes, isEmpty);
|
||||
});
|
||||
|
||||
test('final attempt is always flood regardless of known paths', () async {
|
||||
test('attempts at and after directRetries flood', () async {
|
||||
final pubKey = _hex('ef01');
|
||||
await _seed(svc, pubKey, pathBytes: [0x01], hopCount: 1);
|
||||
|
||||
for (final retries in [1, 2, 5, 10]) {
|
||||
final lastAttempt = svc.selectPathForAttempt(
|
||||
for (final direct in [0, 1, 4, 9]) {
|
||||
final flooded = svc.selectPathForAttempt(
|
||||
pubKey,
|
||||
attemptIndex: retries - 1,
|
||||
maxRetries: retries,
|
||||
attemptIndex: direct,
|
||||
directRetries: direct,
|
||||
);
|
||||
expect(
|
||||
lastAttempt.useFlood,
|
||||
flooded.useFlood,
|
||||
isTrue,
|
||||
reason: 'maxRetries=$retries: last attempt must be flood',
|
||||
reason: 'directRetries=$direct: that attempt floods',
|
||||
);
|
||||
if (direct == 0) continue;
|
||||
final routed = svc.selectPathForAttempt(
|
||||
pubKey,
|
||||
attemptIndex: direct - 1,
|
||||
directRetries: direct,
|
||||
);
|
||||
expect(routed.useFlood, isFalse);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -205,7 +205,7 @@ void main() {
|
||||
final pick = svc.selectPathForAttempt(
|
||||
contact,
|
||||
attemptIndex: attempt,
|
||||
maxRetries: maxRetries,
|
||||
directRetries: maxRetries - 1,
|
||||
recentSelections: used,
|
||||
context: context,
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user