Skip to content

bpo-36950: Add run_until in test.support.script_helper#13407

Closed
pierreglaser wants to merge 5 commits intopython:mainfrom
pierreglaser:add-run-until-in-script-helper
Closed

bpo-36950: Add run_until in test.support.script_helper#13407
pierreglaser wants to merge 5 commits intopython:mainfrom
pierreglaser:add-run-until-in-script-helper

Conversation

@pierreglaser
Copy link
Copy Markdown
Contributor

@pierreglaser pierreglaser commented May 18, 2019

"""Evaluates a condition until it becomes True, or a timeout is reached."""
deadline = time.monotonic() + timeout
t = 0.1
while time.monotonic() < deadline:
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

For infinitesimal timeout values, the while loop will never be entered, and condition will not be evaluated even one single time. Not such a big deal, but maybe worth noting.

Copy link
Copy Markdown
Member

@tirkarthi tirkarthi left a comment

Choose a reason for hiding this comment

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

LGTM. Added a minor suggestion so that the function doc is hyperlinked in changelog.

Thanks for this utility.

Co-Authored-By: Xtreak <tir.karthi@gmail.com>
@pierreglaser
Copy link
Copy Markdown
Contributor Author

Thanks for the review @tirkarthi!



def run_until(condition, timeout=60, error_msg=None):
"""Evaluates a condition until it becomes True, or a timeout is reached."""
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't understand how I am supposed to use to ... run something.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm sorry, I did not understand your comment. Can you re-phrase it?

Copy link
Copy Markdown
Member

@vstinner vstinner May 20, 2019

Choose a reason for hiding this comment

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

Example: https://bugs.python.org/issue36950#msg342751

Where do I put this code?

                try:
                    smm = shared_memory.SharedMemory(name, create=False)
                except FileNotFoundError:
                    break

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is what is explained here.
From what I saw in the stdlib tests, there are not so many cases that are that complex.
Many tests use much simpler conditions (usually, they check the result of a function)

Example: in _test_multiprocessing.py:

    def wait_proc_exit(self):
        # Only the manager process should be returned by active_children()
        # but this can take a bit on slow machines, so wait a few seconds
        # if there are other children too (see #17395).
        join_process(self.proc)
        start_time = time.monotonic()
        t = 0.01
        while len(multiprocessing.active_children()) > 1:
            time.sleep(t)
            t *= 2
            dt = time.monotonic() - start_time
            if dt >= 5.0:
                test.support.environment_altered = True
                print("Warning -- multiprocessing.Manager still has %s active "
                      "children after %s seconds"
                      % (multiprocessing.active_children(), dt),
                      file=sys.stderr)
                break

We could change this test into

def wait_proc_exit(self):
    join_process(self.proc)
    run_until(lambda: len(multiprocessing.active_children()) == 1, timeout=5)

You can see a exhaustive list of the tests that can be re-written using the current implementation of run_until here: https://bugs.python.org/issue36950#msg342751. As I said, I did not find so many more complex test cases such as the one you are pointing out.

Knowing this, do we still want a more powerful run_until?

@csabella csabella requested a review from vstinner June 1, 2019 12:04
@csabella
Copy link
Copy Markdown
Contributor

@vstinner, should this one move forward? Thanks!

@vstinner
Copy link
Copy Markdown
Member

I close the PR because of the lack of activity. The need for this function is not very clear at this point.

@vstinner vstinner closed this May 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants