blob: f1bf34e05b25b6be7b356677630b6f5519e9b264 [file] [log] [blame]
Stephen Warren68188202016-01-15 11:15:31 -07001# SPDX-License-Identifier: GPL-2.0
Tom Rini10e47792018-05-06 17:58:06 -04002# Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
Stephen Warren68188202016-01-15 11:15:31 -07003
4import pytest
5import time
6
Michal Simek3f090a22017-12-08 15:47:18 +01007"""
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
Heiko Schocher83b73fc2020-06-04 17:24:00 +020014# 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
Michal Simek3f090a22017-12-08 15:47:18 +010020"""
21
Simon Glassddba5202025-02-09 09:07:14 -070022def test_sleep(ubman):
Stephen Warren75e731e2016-01-26 13:41:30 -070023 """Test the sleep command, and validate that it sleeps for approximately
24 the correct amount of time."""
Stephen Warren68188202016-01-15 11:15:31 -070025
Simon Glassddba5202025-02-09 09:07:14 -070026 sleep_skip = ubman.config.env.get('env__sleep_accurate', True)
Michal Simek3f090a22017-12-08 15:47:18 +010027 if not sleep_skip:
28 pytest.skip('sleep is not accurate')
29
Simon Glassddba5202025-02-09 09:07:14 -070030 if ubman.config.buildconfig.get('config_cmd_sleep', 'n') != 'y':
Tom Rini42c62142016-10-14 19:12:31 -040031 pytest.skip('sleep command not supported')
Heiko Schocher83b73fc2020-06-04 17:24:00 +020032
Stephen Warren68188202016-01-15 11:15:31 -070033 # 3s isn't too long, but is enough to cross a few second boundaries.
Simon Glassddba5202025-02-09 09:07:14 -070034 sleep_time = ubman.config.env.get('env__sleep_time', 3)
35 sleep_margin = ubman.config.env.get('env__sleep_margin', 0.25)
Stephen Warren68188202016-01-15 11:15:31 -070036 tstart = time.time()
Simon Glassddba5202025-02-09 09:07:14 -070037 ubman.run_command('sleep %d' % sleep_time)
Stephen Warren68188202016-01-15 11:15:31 -070038 tend = time.time()
39 elapsed = tend - tstart
Heinrich Schuchardte2ecac22017-10-13 22:28:31 +020040 assert elapsed >= (sleep_time - 0.01)
Simon Glassddba5202025-02-09 09:07:14 -070041 if not ubman.config.gdbserver:
Heiko Schocher83b73fc2020-06-04 17:24:00 +020042 # margin is hopefully enough to account for any system overhead.
43 assert elapsed < (sleep_time + sleep_margin)
Love Kumare08bee92023-09-27 10:33:55 +053044
Andrew Goodbody039b0f02024-10-08 13:08:13 +010045@pytest.mark.buildconfigspec("cmd_time")
Simon Glassddba5202025-02-09 09:07:14 -070046def test_time(ubman):
Love Kumare08bee92023-09-27 10:33:55 +053047 """Test the time command, and validate that it gives approximately the
48 correct amount of command execution time."""
49
Simon Glassddba5202025-02-09 09:07:14 -070050 sleep_skip = ubman.config.env.get("env__sleep_accurate", True)
Love Kumare08bee92023-09-27 10:33:55 +053051 if not sleep_skip:
52 pytest.skip("sleep is not accurate")
53
Simon Glassddba5202025-02-09 09:07:14 -070054 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)
Love Kumare08bee92023-09-27 10:33:55 +053057 execute_time = float(output.split()[1])
58 assert sleep_time >= (execute_time - 0.01)
Simon Glassddba5202025-02-09 09:07:14 -070059 if not ubman.config.gdbserver:
Love Kumare08bee92023-09-27 10:33:55 +053060 # margin is hopefully enough to account for any system overhead.
61 assert sleep_time < (execute_time + sleep_margin)