ai fix tcp reconnect test fail
This commit is contained in:
1 parent
631c77cafc
commit
050016f599
1 file changed
+23
-23
@@ -12,11 +12,15 @@ import 'package:meshcore_open/services/usb_serial_frame_codec.dart';
|
|||||||
import 'package:meshcore_open/storage/prefs_manager.dart';
|
import 'package:meshcore_open/storage/prefs_manager.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
Future<void> _waitFor(bool Function() condition, {int seconds = 10}) async {
|
Future<void> _waitFor(
|
||||||
|
bool Function() condition, {
|
||||||
|
int seconds = 10,
|
||||||
|
Duration pollInterval = const Duration(milliseconds: 25),
|
||||||
|
}) async {
|
||||||
final end = DateTime.now().add(Duration(seconds: seconds));
|
final end = DateTime.now().add(Duration(seconds: seconds));
|
||||||
while (!condition()) {
|
while (!condition()) {
|
||||||
if (DateTime.now().isAfter(end)) fail('Timed out waiting for condition');
|
if (DateTime.now().isAfter(end)) fail('Timed out waiting for condition');
|
||||||
await Future<void>.delayed(const Duration(milliseconds: 25));
|
await Future<void>.delayed(pollInterval);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -198,27 +202,23 @@ void main() {
|
|||||||
expect(_connector.isTcpTransportConnected, isTrue);
|
expect(_connector.isTcpTransportConnected, isTrue);
|
||||||
|
|
||||||
// The companion goes away entirely: attempts fail and are rescheduled.
|
// The companion goes away entirely: attempts fail and are rescheduled.
|
||||||
var sawRetryAttempt = false;
|
await companion.close();
|
||||||
void observeRetryAttempt() {
|
await _waitFor(
|
||||||
if (_connector.state == MeshCoreConnectionState.connecting) {
|
() =>
|
||||||
sawRetryAttempt = true;
|
_connector.state == MeshCoreConnectionState.disconnected &&
|
||||||
}
|
_connector.isAutoReconnectScheduled,
|
||||||
}
|
);
|
||||||
|
await _waitFor(
|
||||||
_connector.addListener(observeRetryAttempt);
|
() => !_connector.isAutoReconnectScheduled,
|
||||||
try {
|
pollInterval: const Duration(milliseconds: 1),
|
||||||
await companion.close();
|
);
|
||||||
// Wait for a retry to start and fail, then verify another backoff is
|
// The timer firing proves an attempt started; wait for its failure to
|
||||||
// scheduled. A fixed delay can observe the connector mid-attempt.
|
// finish and the next backoff to become active.
|
||||||
await _waitFor(
|
await _waitFor(
|
||||||
() =>
|
() =>
|
||||||
sawRetryAttempt &&
|
_connector.state == MeshCoreConnectionState.disconnected &&
|
||||||
_connector.state == MeshCoreConnectionState.disconnected &&
|
_connector.isAutoReconnectScheduled,
|
||||||
_connector.isAutoReconnectScheduled,
|
);
|
||||||
);
|
|
||||||
} finally {
|
|
||||||
_connector.removeListener(observeRetryAttempt);
|
|
||||||
}
|
|
||||||
expect(_connector.isTcpTransportConnected, isFalse);
|
expect(_connector.isTcpTransportConnected, isFalse);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in new issue
Block a user