bpo-36950: Add run_until in test.support.script_helper#13407
bpo-36950: Add run_until in test.support.script_helper#13407pierreglaser wants to merge 5 commits intopython:mainfrom
Conversation
| """Evaluates a condition until it becomes True, or a timeout is reached.""" | ||
| deadline = time.monotonic() + timeout | ||
| t = 0.1 | ||
| while time.monotonic() < deadline: |
There was a problem hiding this comment.
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.
tirkarthi
left a comment
There was a problem hiding this comment.
LGTM. Added a minor suggestion so that the function doc is hyperlinked in changelog.
Thanks for this utility.
Misc/NEWS.d/next/Tests/2019-05-18-18-33-02.bpo-36950.f-K73w.rst
Outdated
Show resolved
Hide resolved
Co-Authored-By: Xtreak <tir.karthi@gmail.com>
|
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.""" |
There was a problem hiding this comment.
I don't understand how I am supposed to use to ... run something.
There was a problem hiding this comment.
I'm sorry, I did not understand your comment. Can you re-phrase it?
There was a problem hiding this comment.
Example: https://bugs.python.org/issue36950#msg342751
Where do I put this code?
try:
smm = shared_memory.SharedMemory(name, create=False)
except FileNotFoundError:
break
There was a problem hiding this comment.
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)
breakWe 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?
|
@vstinner, should this one move forward? Thanks! |
|
I close the PR because of the lack of activity. The need for this function is not very clear at this point. |
@vstinner
https://bugs.python.org/issue36950