This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: add an helper to wait for an event with a timeout
Type: Stage: resolved
Components: Tests Versions: Python 3.8, Python 3.7
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: pierreglaser, vstinner
Priority: normal Keywords: patch

Created on 2019-05-17 18:21 by vstinner, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 13407 closed pierreglaser, 2019-05-18 18:30
Messages (5)
msg342751 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-05-17 18:21
The commit cbe72d842646ded2454784679231e3d1e6252e72 is a good example:

            deadline = time.monotonic() + 60
            t = 0.1
            while time.monotonic() < deadline:
                t = min(t*2, 5)
                    smm = shared_memory.SharedMemory(name, create=False)
                except FileNotFoundError:
                raise AssertionError("A SharedMemory segment was leaked after"
                                     " a process was abruptly terminated.")

It would be nice to convert this code pattern into an helper function in It's common to have to wait for something in tests.
msg342804 - (view) Author: Pierre Glaser (pierreglaser) * Date: 2019-05-18 17:31
Lib/test/test_asyncio/ defines a similar helper:

def run_until(loop, pred, timeout=30):
    deadline = time.monotonic() + timeout
    while not pred():
        if timeout is not None:
            timeout = deadline - time.monotonic()
            if timeout <= 0:
                raise futures.TimeoutError()

If we trim the ``loop`` usage, we have a rather simple helper can be used to rewrite a decent number of tests, such as:

- _TestBarrier._test_reset_f
- _TestPoolWorkerLifetime.test_pool_worker_lifetime
- TestSyncManagerTypes.test_wait_proc_exit
- fork_wait. ForkWait.test_wait
- FailingInitializerMixin.test_initializer
- ForkTest.waitimpl
- HandlerTests.test_post_fork_child_no_deadlock
- test_os. Win32KillTests._kill
- StressTest.test_stress_delivery_dependent
- StressTest.test_stress_delivery_simulatenous
- Wait4Test.wait_impl

As well as some top-level commands in:
- some top level instructions
- subprocessdata/
- support/

I also witnessed some slightly more complex patterns that does not easily fit into the asyncio helper:

# FNTREINTLTest._lock
while True:  # synchronize the subprocess
    dt = time.monotonic() - start_time
    if dt > 60.0:
	raise Exception("failed to sync child in %.1f sec" %
	lock_func(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
	lock_func(f, fcntl.LOCK_UN)
    except BlockingIOError:

Which is also (IMO) the case for the lines quoted by Victor.

However, such more complex structures do not seem to appear that often, so sticking to run_until and moving it to may be enough.
msg342805 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-05-18 17:45
Would you be interested to propose a PR? Maybe raise an assertion error by
default, but allow to customize it in the API?
msg342808 - (view) Author: Pierre Glaser (pierreglaser) * Date: 2019-05-18 18:37
Just did so.
msg394481 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-05-26 22:36
I close the issue because of the lack of activity. The need for this function is not very clear at this point.
Date User Action Args
2022-04-11 14:59:15adminsetgithub: 81131
2021-05-26 22:36:55vstinnersetstatus: open -> closed
resolution: out of date
messages: + msg394481

stage: patch review -> resolved
2019-05-18 18:37:05pierreglasersetmessages: + msg342808
2019-05-18 18:30:13pierreglasersetkeywords: + patch
stage: patch review
pull_requests: + pull_request13318
2019-05-18 17:45:11vstinnersetmessages: + msg342805
2019-05-18 17:31:52pierreglasersetmessages: + msg342804
2019-05-18 13:08:52pierreglasersetnosy: + pierreglaser
2019-05-17 18:21:48vstinnercreate