Skip to content
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

Use proper timeout in test_examples.py #35

Merged
merged 1 commit into from
May 17, 2024

Conversation

pniedzielski
Copy link
Collaborator

When the broker is slow to terminate, this test may fail. Rather than using sleep and polling whether the process has sucessfully terminated, as we’ve done in this test previously, we can instead use the timeout argument of Popen.communicate to wait for the broker to terminate.

This patch does two things: first, it increases the timeout after the broker has been terminated until we decide to kill the broker from 0.1 seconds to 2.0 seconds. This is still a short wait, but it gives a slow broker much more time to gracefully shut down. Second, it switches from sleep-and-poll to using the Popen.communicate function with a timeout to wait until the process has terminated. The exact idiom used in this patch is given in the documentation for this function; the double call to communicate is explained there by the note:

If the process does not terminate after timeout seconds, a
`TimeoutExpired` exception will be raised.  Catching this
exception and retrying communication will not lose any output.

Because of this, the two calls to communicate are safe, and we only need the output of the latest call.

@pniedzielski pniedzielski added bug Something isn't working skip news No news entry is required labels May 16, 2024
@pniedzielski pniedzielski requested a review from hallfox May 16, 2024 20:28
@pniedzielski pniedzielski force-pushed the test-examples-timeouts branch from d366f8b to 2dd45ad Compare May 16, 2024 20:29
When the broker is slow to terminate, this test may fail.  Rather than
using `sleep` and polling whether the process has sucessfully
terminated, as we’ve done in this test previously, we can instead use
the `timeout` argument of `Popen.communicate` to wait for the broker
to terminate.

This patch does two things: first, it increases the timeout after the
broker has been terminated until we decide to kill the broker from 0.1
seconds to 2.0 seconds.  This is still a short wait, but it gives a
slow broker much more time to gracefully shut down.  Second, it
switches from `sleep`-and-poll to using the `Popen.communicate`
function with a timeout to wait until the process has terminated.  The
exact idiom used in this patch is given in the [documentation for this
function][communicate]; the double call to `communicate` is explained
there by the note:

    If the process does not terminate after timeout seconds, a
    `TimeoutExpired` exception will be raised.  Catching this
    exception and retrying communication will not lose any output.

Because of this, the two calls to `communicate` are safe, and we only
need the output of the latest call.

[communicate]: https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate

Signed-off-by: Patrick M. Niedzielski <[email protected]>
@pniedzielski pniedzielski force-pushed the test-examples-timeouts branch from 2dd45ad to 31e90c8 Compare May 16, 2024 20:54
@pniedzielski pniedzielski marked this pull request as ready for review May 16, 2024 20:57
@pniedzielski pniedzielski requested a review from a team as a code owner May 16, 2024 20:57
Copy link
Collaborator

@hallfox hallfox left a comment

Choose a reason for hiding this comment

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

Looks good! Am I understanding correctly that a consequence of this communicate pattern is that this test is tolerant to a process that's failing to terminate?

@pniedzielski
Copy link
Collaborator Author

Looks good! Am I understanding correctly that a consequence of this communicate pattern is that this test is tolerant to a process that's failing to terminate?

Yes (so far as I understand the subprocess docs I linked)---if the broker is failing to terminate, the test forcibly kills it, and then we can still see what the issue was later. Looks a little strange with the multiple communicates unfortunately.

@pniedzielski pniedzielski merged commit d3165dc into bloomberg:main May 17, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working skip news No news entry is required
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants