waitUntilReady method
Polls the container's built-in health-check status until 'healthy'.
Throws StateError immediately when the status becomes 'unhealthy' or
when no health check is configured on the image. Throws TimeoutException
when startupTimeout elapses before 'healthy' is reached.
Implementation
@override
Future<void> waitUntilReady(WaitStrategyTarget target) async {
final ready = await poll(() async {
await target.reload();
String? healthStatus;
try {
final info = await target.containerInfo();
healthStatus = info?.state?.health?.status;
} catch (e) {
stderr.writeln('testcontainers: error fetching health status: $e');
healthStatus = null;
}
if (healthStatus == null || healthStatus.isEmpty) {
throw StateError(
'Container has no health check configured: $target',
);
}
if (healthStatus == 'healthy') {
return true;
}
if (healthStatus == 'unhealthy') {
final (stdout, stderrBytes) = await target.logs();
final logs = utf8.decode(
Uint8List.fromList([...stdout, ...stderrBytes]),
allowMalformed: true,
);
throw StateError('Container is unhealthy. Logs: $logs');
}
return false; // 'starting' — keep waiting
});
if (!ready) {
throw TimeoutException(
'Container not healthy within $startupTimeout',
);
}
}