Stephen Warren | 1c8b4d5 | 2016-01-15 11:15:31 -0700 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0 |
Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 2 | # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. |
Stephen Warren | 1c8b4d5 | 2016-01-15 11:15:31 -0700 | [diff] [blame] | 3 | |
| 4 | import pytest |
| 5 | import time |
| 6 | |
Michal Simek | 040f5f1 | 2017-12-08 15:47:18 +0100 | [diff] [blame] | 7 | """ |
| 8 | Note: This test doesn't rely on boardenv_* configuration values but they can |
| 9 | change test behavior. |
| 10 | |
| 11 | # Setup env__sleep_accurate to False if time is not accurate on your platform |
| 12 | env__sleep_accurate = False |
| 13 | |
Heiko Schocher | fa91467 | 2020-06-04 17:24:00 +0200 | [diff] [blame] | 14 | # Setup env__sleep_time time in seconds board is set to sleep |
| 15 | env__sleep_time = 3 |
| 16 | |
| 17 | # Setup env__sleep_margin set a margin for any system overhead |
| 18 | env__sleep_margin = 0.25 |
| 19 | |
Michal Simek | 040f5f1 | 2017-12-08 15:47:18 +0100 | [diff] [blame] | 20 | """ |
| 21 | |
Stephen Warren | 1c8b4d5 | 2016-01-15 11:15:31 -0700 | [diff] [blame] | 22 | def test_sleep(u_boot_console): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 23 | """Test the sleep command, and validate that it sleeps for approximately |
| 24 | the correct amount of time.""" |
Stephen Warren | 1c8b4d5 | 2016-01-15 11:15:31 -0700 | [diff] [blame] | 25 | |
Michal Simek | 040f5f1 | 2017-12-08 15:47:18 +0100 | [diff] [blame] | 26 | sleep_skip = u_boot_console.config.env.get('env__sleep_accurate', True) |
| 27 | if not sleep_skip: |
| 28 | pytest.skip('sleep is not accurate') |
| 29 | |
Tom Rini | 2b2c6e5 | 2016-10-14 19:12:31 -0400 | [diff] [blame] | 30 | if u_boot_console.config.buildconfig.get('config_cmd_misc', 'n') != 'y': |
| 31 | pytest.skip('sleep command not supported') |
Heiko Schocher | fa91467 | 2020-06-04 17:24:00 +0200 | [diff] [blame] | 32 | |
Stephen Warren | 1c8b4d5 | 2016-01-15 11:15:31 -0700 | [diff] [blame] | 33 | # 3s isn't too long, but is enough to cross a few second boundaries. |
Heiko Schocher | fa91467 | 2020-06-04 17:24:00 +0200 | [diff] [blame] | 34 | sleep_time = u_boot_console.config.env.get('env__sleep_time', 3) |
| 35 | sleep_margin = u_boot_console.config.env.get('env__sleep_margin', 0.25) |
Stephen Warren | 1c8b4d5 | 2016-01-15 11:15:31 -0700 | [diff] [blame] | 36 | tstart = time.time() |
| 37 | u_boot_console.run_command('sleep %d' % sleep_time) |
| 38 | tend = time.time() |
| 39 | elapsed = tend - tstart |
Heinrich Schuchardt | 6c7c3dc | 2017-10-13 22:28:31 +0200 | [diff] [blame] | 40 | assert elapsed >= (sleep_time - 0.01) |
Stephen Warren | 89ab841 | 2016-02-04 16:11:50 -0700 | [diff] [blame] | 41 | if not u_boot_console.config.gdbserver: |
Heiko Schocher | fa91467 | 2020-06-04 17:24:00 +0200 | [diff] [blame] | 42 | # margin is hopefully enough to account for any system overhead. |
| 43 | assert elapsed < (sleep_time + sleep_margin) |
Love Kumar | 34124ad | 2023-09-27 10:33:55 +0530 | [diff] [blame] | 44 | |
| 45 | @pytest.mark.buildconfigspec("cmd_misc") |
| 46 | def test_time(u_boot_console): |
| 47 | """Test the time command, and validate that it gives approximately the |
| 48 | correct amount of command execution time.""" |
| 49 | |
| 50 | sleep_skip = u_boot_console.config.env.get("env__sleep_accurate", True) |
| 51 | if not sleep_skip: |
| 52 | pytest.skip("sleep is not accurate") |
| 53 | |
| 54 | sleep_time = u_boot_console.config.env.get("env__sleep_time", 10) |
| 55 | sleep_margin = u_boot_console.config.env.get("env__sleep_margin", 0.25) |
| 56 | output = u_boot_console.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 u_boot_console.config.gdbserver: |
| 60 | # margin is hopefully enough to account for any system overhead. |
| 61 | assert sleep_time < (execute_time + sleep_margin) |