8000 test(capture): wait for the status condition, not a fixed event-loop budget by shroominic · Pull Request #203 · shroominic/OpenGlucose · GitHub
[go: up one dir, main page]

Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion openhealth/test/protocol_capture_status_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void main() {
timers.last
..fire()
..fire();
await pumpEventQueue(times: 40);
await _pumpUntil(() => _healthyStatuses(statuses).length >= 3);

final healthyBeforeConnect = _healthyStatuses(statuses);
expect(healthyBeforeConnect.length, greaterThanOrEqualTo(3));
Expand Down Expand Up @@ -365,6 +365,28 @@ const Set<String> _statusKeys = <String>{
'stopping',
};

/// Pumps the event loop until [condition] holds, or [timeout] elapses.
///
/// The status writer commits through real asynchronous work, so a fixed number
/// of event-loop turns is not a bound the runner honours under load: a loaded
/// host can leave the list short of the count the assertions below expect. This
/// waits on the condition instead and gives up after [timeout], so a real hang
/// still fails on the assertion that follows with the real evidence.
Future<void> _pumpUntil(
bool Function() condition, {
Duration timeout = const Duration(seconds: 5),
}) async {
final deadline = DateTime.now().add(timeout);
while (true) {
// Always give the writer at least one turn, then stop as soon as the
// condition holds instead of spending a fixed budget.
await pumpEventQueue(times: 5);
if (condition() || DateTime.now().isAfter(deadline)) {
return;
}
}
}

List<Map<String, Object?>> _healthyStatuses(
List<Map<String, Object?>> statuses,
) => statuses
Expand Down
Loading
0