Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2017 The ChromiumOS Authors |
Allen Li | c54362d | 2017-07-12 18:21:26 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Unit tests for loop.""" |
| 6 | |
| 7 | # pylint: disable=protected-access |
| 8 | |
| 9 | from __future__ import absolute_import |
Allen Li | c54362d | 2017-07-12 18:21:26 -0700 | [diff] [blame] | 10 | |
| 11 | import contextlib |
Mike Frysinger | 166fea0 | 2021-02-12 05:30:33 -0500 | [diff] [blame] | 12 | from unittest import mock |
Allen Li | c54362d | 2017-07-12 18:21:26 -0700 | [diff] [blame] | 13 | |
Allen Li | c54362d | 2017-07-12 18:21:26 -0700 | [diff] [blame] | 14 | from chromite.lib import cros_test_lib |
| 15 | from chromite.scripts.sysmon import loop |
| 16 | |
| 17 | |
Allen Li | c54362d | 2017-07-12 18:21:26 -0700 | [diff] [blame] | 18 | class _MockTime(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 19 | """Mock time and sleep. |
Allen Li | c54362d | 2017-07-12 18:21:26 -0700 | [diff] [blame] | 20 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 21 | Provides mock behavior for time.time() and time.sleep() |
Allen Li | c54362d | 2017-07-12 18:21:26 -0700 | [diff] [blame] | 22 | """ |
Allen Li | c54362d | 2017-07-12 18:21:26 -0700 | [diff] [blame] | 23 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 24 | def __init__(self, sleep_delta): |
| 25 | """Instantiate instance. |
Allen Li | c54362d | 2017-07-12 18:21:26 -0700 | [diff] [blame] | 26 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 27 | Args: |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 28 | sleep_delta: Modify sleep time by this many seconds. But sleep will |
| 29 | always be at least 1. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 30 | """ |
| 31 | self.current_time = 0 |
| 32 | self._sleep_delta = sleep_delta |
| 33 | |
| 34 | def time(self): |
| 35 | return self.current_time |
| 36 | |
| 37 | def sleep(self, secs): |
| 38 | actual_sleep = max(secs + self._sleep_delta, 1) |
| 39 | self.current_time += actual_sleep |
| 40 | return actual_sleep |
Allen Li | c54362d | 2017-07-12 18:21:26 -0700 | [diff] [blame] | 41 | |
| 42 | |
| 43 | @contextlib.contextmanager |
| 44 | def _patch_time(sleep_delta): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 45 | """Mock out time and sleep. |
Allen Li | c54362d | 2017-07-12 18:21:26 -0700 | [diff] [blame] | 46 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 47 | Patches behavior for time.time() and time.sleep() |
| 48 | """ |
| 49 | mock_time = _MockTime(sleep_delta) |
| 50 | with mock.patch("time.time", mock_time.time), mock.patch( |
| 51 | "time.sleep", mock_time.sleep |
| 52 | ): |
| 53 | yield mock_time |
Allen Li | c54362d | 2017-07-12 18:21:26 -0700 | [diff] [blame] | 54 | |
| 55 | |
| 56 | class TestForceSleep(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 57 | """Tests for _force_sleep.""" |
Allen Li | c54362d | 2017-07-12 18:21:26 -0700 | [diff] [blame] | 58 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 59 | def test__force_sleep_at_least_given_secs(self): |
| 60 | with _patch_time(sleep_delta=-7) as mock_time: |
| 61 | loop._force_sleep(10) |
| 62 | self.assertGreaterEqual(mock_time.current_time, 10) |