testcontainers / testcontainers-dotnet

A library to support tests with throwaway instances of Docker containers for all compatible .NET Standard versions.
https://dotnet.testcontainers.org
MIT License
3.73k stars 262 forks source link

[Enhancement]: Timeout for wait strategies #1011

Closed maaex closed 11 months ago

maaex commented 11 months ago

Problem

There currently seems to be no timeout on wait strategies leading to the possibility of a test getting stuck in an endless wait.

Solution

The WaitStrategy.WaitUntilAsync method already has a Timeout argument. Currently the call to it is hardcoded to Timeout.InfiniteTimeSpan. I propose making it configurable for the user, possibly by adding a Timeout property to the IWaitUntil interface.

Benefit

This would give the user a fail-safe by allowing configurable timeouts WaitStrategies

Alternatives

One alternative could be to change the current WaitStrategy from Infinite to some long number, though I think its hard to find a good number here due to the numerous use cases.

Another alternative could be to rely on the testing frameworks' timeouts.

Would you like to help contributing this enhancement?

Yes

HofmeisterAn commented 11 months ago

There currently seems to be no timeout on wait strategies leading to the possibility of a test getting stuck in an endless wait.

You can pass a cancellation token to the container's start method like this:

https://github.com/testcontainers/testcontainers-dotnet/blob/c7d8cfdc2f52241bb2df4617484d3c6041a3281b/tests/Testcontainers.Tests/Unit/Configurations/WaitUntilMessageIsLoggedTest.cs#L29

I began working on improving the wait strategy API and adding more convenient features some time ago, but I haven't had the time to finalize and review it (refactor-wait-strategy-api). This issue is related to https://github.com/testcontainers/testcontainers-dotnet/issues/827. I would prefer to continue the discussion in this issue to maintain clarity and avoid losing track.

maaex commented 11 months ago

You can pass a cancellation token to the container's start method like this:

Thanks, I didn't realize this existed. That will be enough to solve the issue in my case.