Skip to content

fix: wait_for_logs can now fail early when the container stops - #682

Merged
alexanderankin merged 2 commits into
testcontainers:mainfrom
ArthurBook:waiting-utils-raise-early-on-exit
Aug 14, 2024
Merged

alexanderankin merged 2 commits into
testcontainers:mainfrom
ArthurBook:waiting-utils-raise-early-on-exit

Conversation

@ArthurBook

Copy link
Copy Markdown
Contributor

Addresses my suggestion made in issue 681.

This PR adds a flag that checks is the status is not running and raises a RuntimeError to avoid waiting for logs after the container already has exited. The idea is to save wait time when there is a long startup time in case the container fails early.

from testcontainers.core import container, waiting_utils

if __name__ == "__main__":
    waiting_utils.wait_for_logs(
        container.DockerContainer("flyway/flyway").start(),
        r"Successfully applied \d+ migrations to schema",
        timeout=10,
        raise_on_exit=True,
    )

# > RuntimeError(f"Container exited before emitting logs satisfying predicate")
# ( Raised almost immediately ) 

@codecov

codecov Bot commented Aug 14, 2024

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.

Please upload report for BASE (main@c7d9b81). Learn more about missing BASE report.

Files Patch % Lines
core/testcontainers/core/waiting_utils.py 0.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #682   +/-   ##
=======================================
  Coverage        ?   78.07%           
=======================================
  Files           ?       12           
  Lines           ?      602           
  Branches        ?       89           
=======================================
  Hits            ?      470           
  Misses          ?      106           
  Partials        ?       26           

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

@ArthurBook
ArthurBook force-pushed the waiting-utils-raise-early-on-exit branch from 9ff855b to 8bc2cc4 Compare August 14, 2024 21:38
@alexanderankin alexanderankin changed the title feat: add flag for exiting early while waiting for logs fix: wait_for_logs can now fail early when the container stops Aug 14, 2024
@alexanderankin

Copy link
Copy Markdown
Member

why is true is not the default, any background on this?

@alexanderankin
alexanderankin merged commit 925329d into testcontainers:main Aug 14, 2024
@ArthurBook

Copy link
Copy Markdown
Contributor Author

why is true is not the default, any background on this?

I think it would be a good default.
Kept it False just to not change existing functionality without explicit opt-in.

@alexanderankin

Copy link
Copy Markdown
Member

i would like to roll it out everywhere else, wonder what would be a good strategy for that, unless i am overthinking it, in which case we should just do it.

@kiview

kiview commented Aug 15, 2024

Copy link
Copy Markdown
Member

TBH, I think the false behavior is more a bug than actual expected behavior. If I were to make the call, I would not only make true the default, but also don't make it user configurable at all (can any of you come up with a use case were the false behavior is useful?).

@alexanderankin

Copy link
Copy Markdown
Member

pr title = "fix: wait_for_logs can now fail early when the container stops"

next pr title = ${pr title/can/WILL/}

@ArthurBook

ArthurBook commented Aug 15, 2024 •

Copy link
Copy Markdown
Contributor Author

Hi!
Agree with above, for all our use cases, the expected behavior would be to raise RuntimeError if the container exited. I think its a good idea to drop the raise_on_exit param and fix behavior to True.

Another option is to allow user to define the set of Container.status that (don't) cause a fail. This would allow for a bit more configurability. WDYT?

@ArthurBook
ArthurBook deleted the waiting-utils-raise-early-on-exit branch August 20, 2024 15:46
alexanderankin pushed a commit that referenced this pull request Sep 3, 2024
🤖 I have created a release *beep* *boop*
---


##
[4.8.1](testcontainers-v4.8.0...testcontainers-v4.8.1)
(2024-08-18)


### Bug Fixes

* **generic:** Update the FastAPI install on genric module doctest
samples
([#686](#686))
([5216b02](5216b02))
* **mssql:** use glob to find mssql-tools folder since it moves
([#685](#685))
([4912725](4912725)),
closes
[#666](#666)
* wait_for_logs can now fail early when the container stops
([#682](#682))
([925329d](925329d))


### Documentation

* Add a more advance usecase documentation for ServerContainer
([#688](#688))
([2cf5a9f](2cf5a9f))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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.

3 participants