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

Lib/test/lock_tests.py should not use time.time(), but time.monotonic() #79694

Closed
vstinner opened this issue Dec 16, 2018 · 6 comments
Closed
Labels
3.8 only security fixes tests Tests in the Lib/test dir

Comments

@vstinner
Copy link
Member

BPO 35513
Nosy @vstinner, @pablogsal, @miss-islington
PRs
  • bpo-35513, unittest: TextTestRunner uses time.perf_counter() #11180
  • bpo-35513: Replace time.time() with time.monotonic() in tests #11182
  • [3.7] bpo-35513: Replace time.time() with time.monotonic() in tests (GH-11182) #11185
  • [3.7] bpo-35513, unittest: TextTestRunner uses time.perf_counter() (GH-11180) #11188
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2018-12-17.10:54:06.897>
    created_at = <Date 2018-12-16.22:05:49.526>
    labels = ['3.8', 'tests']
    title = 'Lib/test/lock_tests.py should not use time.time(), but time.monotonic()'
    updated_at = <Date 2018-12-17.10:54:06.896>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2018-12-17.10:54:06.896>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-12-17.10:54:06.897>
    closer = 'vstinner'
    components = ['Tests']
    creation = <Date 2018-12-16.22:05:49.526>
    creator = 'vstinner'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 35513
    keywords = ['patch']
    message_count = 6.0
    messages = ['331939', '331951', '331954', '331968', '331975', '331976']
    nosy_count = 3.0
    nosy_names = ['vstinner', 'pablogsal', 'miss-islington']
    pr_nums = ['11180', '11182', '11185', '11188']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue35513'
    versions = ['Python 3.8']

    @vstinner
    Copy link
    Member Author

    https://buildbot.python.org/all/#/builders/145/builds/956

    Unhandled exception in thread started by <function Bunch.__init__.<locals>.task at 0x111775cb0>
    Traceback (most recent call last):
      File "/Users/buildbot/buildarea/3.x.billenstein-sierra/build/Lib/test/lock_tests.py", line 41, in task
        f()
      File "/Users/buildbot/buildarea/3.x.billenstein-sierra/build/Lib/test/lock_tests.py", line 591, in f
        self.assertTimeout(dt, 0.1)
      File "/Users/buildbot/buildarea/3.x.billenstein-sierra/build/Lib/test/lock_tests.py", line 80, in assertTimeout
        self.assertGreaterEqual(actual, expected * 0.6)
      File "/Users/buildbot/buildarea/3.x.billenstein-sierra/build/Lib/unittest/case.py", line 1283, in assertGreaterEqual
        self.fail(self._formatMessage(msg, standardMsg))
      File "/Users/buildbot/buildarea/3.x.billenstein-sierra/build/Lib/unittest/case.py", line 719, in fail
        raise self.failureException(msg)
    AssertionError: -0.24049997329711914 not greater than or equal to 0.06
    test_waitfor_timeout (test.test_threading.ConditionTests) ... FAIL

    test_waitfor_timeout():

                ...
                dt = time.time()
                result = cond.wait_for(lambda : state==4, timeout=0.1)
                dt = time.time() - dt
                self.assertFalse(result)
                self.assertTimeout(dt, 0.1)
                ...
    

    with:

        def assertTimeout(self, actual, expected):
            ...
            self.assertGreaterEqual(actual, expected * 0.6)
            ...

    It seems like time.time() gone backward on the buildbot. The test must use time.monotonic() to measure time difference.

    Attached PR fix the issue.

    @vstinner vstinner added 3.8 only security fixes tests Tests in the Lib/test dir labels Dec 16, 2018
    @vstinner
    Copy link
    Member Author

    New changeset 2cf4c20 by Victor Stinner in branch 'master':
    bpo-35513: Replace time.time() with time.monotonic() in tests (GH-11182)
    2cf4c20

    @miss-islington
    Copy link
    Contributor

    New changeset be69ff2 by Miss Islington (bot) in branch '3.7':
    bpo-35513: Replace time.time() with time.monotonic() in tests (GH-11182)
    be69ff2

    @vstinner
    Copy link
    Member Author

    New changeset 8db5b54 by Victor Stinner in branch 'master':
    bpo-35513, unittest: TextTestRunner uses time.perf_counter() (GH-11180)
    8db5b54

    @miss-islington
    Copy link
    Contributor

    New changeset 9ade4cb by Miss Islington (bot) in branch '3.7':
    bpo-35513, unittest: TextTestRunner uses time.perf_counter() (GH-11180)
    9ade4cb

    @vstinner
    Copy link
    Member Author

    I searched from "time.time" and "from time import(...)time" in the Python standard library (in the master branch) and it seems like all usage of time.time() are now appropriate. I close the issue.

    Sometimes, I'm not sure that if time.monotonic() or time.perf_counter() should be used, but at least both functions are monotonic and so are not affected by this issue (clock going backwards).

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes tests Tests in the Lib/test dir
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants