Skip to content

feat(core): Wait strategies foundation #838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

terry-docker
Copy link
Contributor

@terry-docker terry-docker commented Jun 27, 2025

Aligns testcontainers-python with the testcontainers community standard wait strategy pattern used across Java, Go, and other implementations. This provides a consistent developer experience and better maintainability.

Examples

From:

wait_for_logs(container, "Test Sample Image")

To:

from testcontainers.core.wait_strategies import LogMessageWaitStrategy

container.waiting_for(LogMessageWaitStrategy("Server started"))

Backward Compatibility

No breaking changes - all existing code continues working
Deprecation warnings added to wait_for_logs() and @wait_container_is_ready
Clear migration path provided in warning messages

New:

core/testcontainers/core/wait_strategies.py - Strategy implementations
core/tests/test_wait_strategies*.py - Comprehensive test coverage

Modified:

core/testcontainers/core/container.py - Added waiting_for() method
core/testcontainers/compose/compose.py - Added compose wait strategy support
core/testcontainers/core/waiting_utils.py - Base classes and protocol

Future Strategies to quickly follow

Foundation enables community-standard wait strategies:
HttpWaitStrategy, HealthcheckWaitStrategy, PortWaitStrategy, CompositeWaitStrategy

Testing

Unit tests with parameterized scenarios
Integration tests with real Docker containers
Protocol compliance verification
Backward compatibility validation

@terry-docker
Copy link
Contributor Author

Notes: Mypy errors got crazy and circular, so I skipped them for now to focus on getting the wait strategies up without the massive changes caused by bringing those core files inline with mypy.

Also the comments are representing some strategies not included here, but I do have working versions ready to be brought in 🥳

@terry-docker terry-docker changed the title Wait strategies foundation feat(core): Wait strategies foundation Jun 27, 2025
@Tranquility2
Copy link
Contributor

I assume mypy needs all the good stuff on #810 🙂

@terry-docker terry-docker force-pushed the wait-strategy-foundation branch from 1bc760a to 4bf23d8 Compare June 27, 2025 15:17
Copy link

codecov bot commented Jun 27, 2025

Codecov Report

❌ Patch coverage is 78.26087% with 35 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@d40473f). Learn more about missing BASE report.

Files with missing lines Patch % Lines
core/testcontainers/core/waiting_utils.py 79.38% 16 Missing and 4 partials ⚠️
core/testcontainers/core/wait_strategies.py 76.59% 8 Missing and 3 partials ⚠️
core/testcontainers/core/container.py 76.47% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #838   +/-   ##
=======================================
  Coverage        ?   81.79%           
=======================================
  Files           ?       14           
  Lines           ?      890           
  Branches        ?      140           
=======================================
  Hits            ?      728           
  Misses          ?      127           
  Partials        ?       35           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@alexanderankin
Copy link
Member

if i can merge 810 and rebase this on top of that that would be ideal. i hope to be done with #810 on sunday or monday and this can follow shortly after

@terry-docker
Copy link
Contributor Author

Sure, I can add the missing new mypy types on this PR too.

Copy link
Contributor

@rhoban13 rhoban13 left a comment

Choose a reason for hiding this comment

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

This is great - thanks!


def wait_container_is_ready(*transient_exceptions) -> Callable:
class WaitStrategyTarget(Protocol):
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if WaitStrategyTarget & WaitStrategy might be split into their own module - somewhat separating it from the now deprecated functions in here. That might help simplify when time comes for its eventual removal.

Copy link
Member

@alexanderankin alexanderankin Jun 28, 2025

Choose a reason for hiding this comment

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

If we can avoid changing files it preserves git history better. If this file was called wait or waiting it would have been better but I don't see removing utils or reorganizing code a super high priority

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was on the fence about this, they're needed for the backwards compatibility but I honestly don't have strong feelings about where to have WaitStrategyTarget and WaitStrategy live

@alexanderankin
Copy link
Member

made some progress on conflicts here today

@alexanderankin
Copy link
Member

if i cant figure out how to make this go away, i might just disable code coverage at all - it is supposed to be useful tool not a procrustean bed (https://www.dictionary.com/browse/procrustean-bed)

image

im trying to copy code out of the ui and its not working because its in the way. </vent>

@alexanderankin
Copy link
Member

@alexanderankin alexanderankin force-pushed the wait-strategy-foundation branch from 4bf23d8 to ee4f335 Compare July 31, 2025 19:40
ambitda
ambitda previously approved these changes Jul 31, 2025
@alexanderankin alexanderankin force-pushed the wait-strategy-foundation branch 2 times, most recently from dd156fa to 3e6dd71 Compare July 31, 2025 20:07
@alexanderankin
Copy link
Member

@terry-docker if you want to edit the git history that is up to you or however you want to merge this - currently it does not give you credit but it is mergeable - i will take a look when i have time

@terry-docker terry-docker force-pushed the wait-strategy-foundation branch from 3e6dd71 to 2de97d9 Compare August 6, 2025 17:50
@terry-docker terry-docker merged commit 003046b into testcontainers:main Aug 6, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants