xref: /openbmc/u-boot/test/py/tests/test_sleep.py (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
11c8b4d5fSStephen Warren# SPDX-License-Identifier: GPL-2.0
2*83d290c5STom Rini# Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
31c8b4d5fSStephen Warren
41c8b4d5fSStephen Warrenimport pytest
51c8b4d5fSStephen Warrenimport time
61c8b4d5fSStephen Warren
7040f5f10SMichal Simek"""
8040f5f10SMichal SimekNote: This test doesn't rely on boardenv_* configuration values but they can
9040f5f10SMichal Simekchange test behavior.
10040f5f10SMichal Simek
11040f5f10SMichal Simek# Setup env__sleep_accurate to False if time is not accurate on your platform
12040f5f10SMichal Simekenv__sleep_accurate = False
13040f5f10SMichal Simek
14040f5f10SMichal Simek"""
15040f5f10SMichal Simek
161c8b4d5fSStephen Warrendef test_sleep(u_boot_console):
17e8debf39SStephen Warren    """Test the sleep command, and validate that it sleeps for approximately
18e8debf39SStephen Warren    the correct amount of time."""
191c8b4d5fSStephen Warren
20040f5f10SMichal Simek    sleep_skip = u_boot_console.config.env.get('env__sleep_accurate', True)
21040f5f10SMichal Simek    if not sleep_skip:
22040f5f10SMichal Simek        pytest.skip('sleep is not accurate')
23040f5f10SMichal Simek
242b2c6e51STom Rini    if u_boot_console.config.buildconfig.get('config_cmd_misc', 'n') != 'y':
252b2c6e51STom Rini        pytest.skip('sleep command not supported')
261c8b4d5fSStephen Warren    # 3s isn't too long, but is enough to cross a few second boundaries.
271c8b4d5fSStephen Warren    sleep_time = 3
281c8b4d5fSStephen Warren    tstart = time.time()
291c8b4d5fSStephen Warren    u_boot_console.run_command('sleep %d' % sleep_time)
301c8b4d5fSStephen Warren    tend = time.time()
311c8b4d5fSStephen Warren    elapsed = tend - tstart
326c7c3dccSHeinrich Schuchardt    assert elapsed >= (sleep_time - 0.01)
3389ab8410SStephen Warren    if not u_boot_console.config.gdbserver:
341c8b4d5fSStephen Warren        # 0.25s margin is hopefully enough to account for any system overhead.
3589ab8410SStephen Warren        assert elapsed < (sleep_time + 0.25)
36