testing/web-platform/tests/common/sleep.py
author Erik Anderson <Erik.Anderson@microsoft.com>
Tue, 05 Mar 2019 12:13:21 +0000
changeset 525150 78d3aefc44182764ee01790175826e4d800a29d8
parent 517845 5a4e9e40a6a52bfe1729408a09447f694a99620c
child 525311 66b1aba3fb4372ec47986e728b1da9b32dd3d368
permissions -rw-r--r--
Bug 1528023 [wpt PR 15375] - ensure sleep_at_least is guaranteed to sleep for the requested duration, a=testonly Automatic update from web-platform-tests ensure sleep_at_least is guaranteed to sleep for the requested duration Microsoft runs tests in a Hyper-V VM. The test external/wpt/resource-timing/resource-timing-level1.sub.html leverages wptserve's SyntheticResponse.py to request that each step of a response is delayed by 250ms. In our environment, even with the extra 15ms "padding" in sleep.py, we're seeing the timer return 5ms early which in turn causes the test to fail. This change updates the sleep_at_least function to, in a loop, explicitly check that it slept long enough and, if not, sleep again for a short interval. Change-Id: I2dbd8e6bd586dc661b45f94454d3b6b4613db1a5 Reviewed-on: https://chromium-review.googlesource.com/c/1469655 Reviewed-by: Robert Ma <robertma@chromium.org> Reviewed-by: Yoav Weiss <yoavweiss@chromium.org> Commit-Queue: Erik Anderson <Erik.Anderson@microsoft.com> Cr-Commit-Position: refs/heads/master@{#631969} -- wpt-commits: 8f7ccae78b9a0e36aca3618f62e5bf558f4775d4 wpt-pr: 15375

import time

# sleep can be lower than requested value in some platforms: https://bugs.python.org/issue31539
# We add padding here to compensate for that.
sleep_padding = 15.0

def sleep_at_least(sleep_in_ms):
    sleep_until = time.time() + (sleep_in_ms / 1E3)
    time.sleep((sleep_in_ms + sleep_padding) / 1E3)
    # Check if the padding was sufficient; if not, sleep again.
    while time.time() < sleep_until:
        time.sleep(sleep_padding / 1E3)