Skip to content

fix(Nats): Use healthz API for readiness probe#1679

Merged
HofmeisterAn merged 3 commits intotestcontainers:developfrom
eriblo01:patch-1
Apr 15, 2026
Merged

fix(Nats): Use healthz API for readiness probe#1679
HofmeisterAn merged 3 commits intotestcontainers:developfrom
eriblo01:patch-1

Conversation

@eriblo01
Copy link
Copy Markdown
Contributor

@eriblo01 eriblo01 commented Apr 15, 2026

What does this PR do?

This PR changes the wait strategy to await a 200 OK for the /healthz endpoint, rather than simply checking the logs on the container.

Why is it important?

When using the Nats Testcontainer, the StartAsync() returns before the container is ready to accept connections, resulting in NATS.Client.Core.NatsException: can not connect uris: nats://127.0.0.1:<<port>>. This is a problem on OSX with Rancher Desktop, not sure if it's a problem with Docker Desktop. Talking to NATS Server (see issue below) they recommend waiting differently.

Related issues

Summary by CodeRabbit

  • Bug Fixes
    • Improved NATS container startup detection: confirms readiness via HTTP health check (GET /healthz on management port 8222) instead of relying on container log output.

@eriblo01 eriblo01 requested a review from HofmeisterAn as a code owner April 15, 2026 09:45
@netlify
Copy link
Copy Markdown

netlify bot commented Apr 15, 2026

Deploy Preview for testcontainers-dotnet ready!

Name Link
🔨 Latest commit 06a9402
🔍 Latest deploy log https://app.netlify.com/projects/testcontainers-dotnet/deploys/69dfc9372b210b0008d4ce4b
😎 Deploy Preview https://deploy-preview-1679--testcontainers-dotnet.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 15, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: e2d55d21-d4d5-467f-8903-ff7f4b74cb97

📥 Commits

Reviewing files that changed from the base of the PR and between 1936958 and 06a9402.

📒 Files selected for processing (1)
  • src/Testcontainers.Nats/NatsBuilder.cs

Walkthrough

Replaced the NATS container readiness check from a log-based wait for "Server is ready" to an HTTP-based wait that polls http://<container>:8222/healthz (uses Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(...)). No public API/signature changes.

Changes

Cohort / File(s) Summary
NATS Container Wait Strategy
src/Testcontainers.Nats/NatsBuilder.cs
Replaced Wait.ForUnixContainer().UntilMessageIsLogged("Server is ready") with Wait.ForUnixContainer().UntilHttpRequestIsSucceeded("http://{container}:{NatsHttpManagementPort}/healthz") to verify readiness via the management /healthz endpoint. (+2/-1 lines)

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

Suggested labels

bug

Poem

🐰 I sniffed the logs, then took a hop,
I pinged /healthz—no need to stop.
Port eight-two-two replied in tune,
The NATS container hummed—ready soon! 🎉

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the main change: replacing the log-based wait strategy with an HTTP-based health check using the /healthz API.
Description check ✅ Passed The description covers the required sections: 'What does this PR do?' and 'Why is it important?' are both present and complete, with clear explanations of the change and its motivation.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/Testcontainers.Nats/NatsBuilder.cs (1)

113-113: Use the existing port constant in the HTTP wait strategy.

This works, but replacing the literal 8222 with NatsHttpManagementPort avoids duplication and future drift.

♻️ Suggested change
-            .WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request => request.ForPort(8222).ForPath("/healthz")));
+            .WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request => request.ForPort(NatsHttpManagementPort).ForPath("/healthz")));
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/Testcontainers.Nats/NatsBuilder.cs` at line 113, Replace the hard-coded
port 8222 in the HTTP wait strategy with the existing port constant
NatsHttpManagementPort: locate the call chain in NatsBuilder where
WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request =>
request.ForPort(8222).ForPath("/healthz"))) is used and change
request.ForPort(8222) to request.ForPort(NatsHttpManagementPort) so the wait
strategy uses the shared constant.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/Testcontainers.Nats/NatsBuilder.cs`:
- Line 113: Replace the hard-coded port 8222 in the HTTP wait strategy with the
existing port constant NatsHttpManagementPort: locate the call chain in
NatsBuilder where
WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request =>
request.ForPort(8222).ForPath("/healthz"))) is used and change
request.ForPort(8222) to request.ForPort(NatsHttpManagementPort) so the wait
strategy uses the shared constant.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: f397a78b-abd2-43fa-a5c0-12e30e910838

📥 Commits

Reviewing files that changed from the base of the PR and between a2f5717 and 05732a1.

📒 Files selected for processing (1)
  • src/Testcontainers.Nats/NatsBuilder.cs

@eriblo01 eriblo01 changed the title fix: change to a recommended wait strategy Update the container wait strategy for the NATS container Apr 15, 2026
@HofmeisterAn HofmeisterAn changed the title Update the container wait strategy for the NATS container fix(Nats): Use healthz API for readiness probe Apr 15, 2026
@HofmeisterAn HofmeisterAn added the bug Something isn't working label Apr 15, 2026
Copy link
Copy Markdown
Collaborator

@HofmeisterAn HofmeisterAn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@eriblo01
Copy link
Copy Markdown
Contributor Author

Thanks!

Cheers! And thanks for applying the code standard, sloppy of me. Seems we need a second reviewer now though.

@HofmeisterAn HofmeisterAn merged commit fd87bb8 into testcontainers:develop Apr 15, 2026
12 checks passed
@eriblo01 eriblo01 deleted the patch-1 branch April 16, 2026 04:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants