halkyon/apps/learn/halkyon_learn_flutter/test/support/eventually.dart

17 lines
578 B
Dart
Raw Permalink Normal View History

import 'package:flutter_test/flutter_test.dart';
/// Waits until [condition] holds, checking every few milliseconds, and fails
/// after [timeout]: for what follows a real timer, without betting on how
/// fast a busy machine runs.
Future<void> eventually(
bool Function() condition, {
Duration timeout = const Duration(seconds: 10),
}) async {
final stopwatch = Stopwatch()..start();
while (!condition()) {
if (stopwatch.elapsed > timeout) {
fail('Still not so after $timeout.');
}
await Future<void>.delayed(const Duration(milliseconds: 10));
}
}