1# SPDX-License-Identifier: GPL-2.0
2# Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
3
4import pytest
5import time
6
7"""
8Note: This test doesn't rely on boardenv_* configuration values but they can
9change test behavior.
10
11# Setup env__sleep_accurate to False if time is not accurate on your platform
12env__sleep_accurate = False
13
14# Setup env__sleep_time time in seconds board is set to sleep
15env__sleep_time = 3
16
17# Setup env__sleep_margin set a margin for any system overhead
18env__sleep_margin = 0.25
19
20"""
21
22def test_sleep(ubman):
23    """Test the sleep command, and validate that it sleeps for approximately
24    the correct amount of time."""
25
26    sleep_skip = ubman.config.env.get('env__sleep_accurate', True)
27    if not sleep_skip:
28        pytest.skip('sleep is not accurate')
29
30    if ubman.config.buildconfig.get('config_cmd_sleep', 'n') != 'y':
31        pytest.skip('sleep command not supported')
32
33    # 3s isn't too long, but is enough to cross a few second boundaries.
34    sleep_time = ubman.config.env.get('env__sleep_time', 3)
35    sleep_margin = ubman.config.env.get('env__sleep_margin', 0.25)
36    tstart = time.time()
37    ubman.run_command('sleep %d' % sleep_time)
38    tend = time.time()
39    elapsed = tend - tstart
40    assert elapsed >= (sleep_time - 0.01)
41    if not ubman.config.gdbserver:
42        # margin is hopefully enough to account for any system overhead.
43        assert elapsed < (sleep_time + sleep_margin)
44
45@pytest.mark.buildconfigspec("cmd_time")
46def test_time(ubman):
47    """Test the time command, and validate that it gives approximately the
48    correct amount of command execution time."""
49
50    sleep_skip = ubman.config.env.get("env__sleep_accurate", True)
51    if not sleep_skip:
52        pytest.skip("sleep is not accurate")
53
54    sleep_time = ubman.config.env.get("env__sleep_time", 10)
55    sleep_margin = ubman.config.env.get("env__sleep_margin", 0.25)
56    output = ubman.run_command("time sleep %d" % sleep_time)
57    execute_time = float(output.split()[1])
58    assert sleep_time >= (execute_time - 0.01)
59    if not ubman.config.gdbserver:
60        # margin is hopefully enough to account for any system overhead.
61        assert sleep_time < (execute_time + sleep_margin)
62