Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2017 The ChromiumOS Authors |
Allen Li | 0291557 | 2017-07-12 18:05:45 -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 git_metrics.""" |
| 6 | |
| 7 | # pylint: disable=protected-access |
| 8 | |
| 9 | from __future__ import absolute_import |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 10 | |
Mike Frysinger | 166fea0 | 2021-02-12 05:30:33 -0500 | [diff] [blame] | 11 | from unittest import mock |
| 12 | |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 13 | from chromite.lib import cros_test_lib |
| 14 | from chromite.scripts.sysmon import mainlib |
| 15 | |
| 16 | |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 17 | class TestTimedCallback(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 18 | """Tests for _TimedCallback.""" |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 19 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 20 | def setUp(self): |
| 21 | patcher = mock.patch("time.time", autospec=True) |
| 22 | self.time = patcher.start() |
| 23 | self.addCleanup(patcher.stop) |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 24 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 25 | def test_initial_call_should_callback(self): |
| 26 | """Test that initial call goes through.""" |
| 27 | cb = mock.Mock([]) |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 28 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 29 | self.time.return_value = 0 |
| 30 | obj = mainlib._TimedCallback(cb, 10) |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 31 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 32 | obj() |
| 33 | cb.assert_called_once() |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 34 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 35 | def test_call_within_interval_should_not_callback(self): |
| 36 | """Test that call too soon does not callback.""" |
| 37 | cb = mock.Mock([]) |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 38 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 39 | self.time.return_value = 0 |
| 40 | obj = mainlib._TimedCallback(cb, 10) |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 41 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 42 | obj() |
| 43 | cb.assert_called_once() |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 44 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 45 | cb.reset_mock() |
| 46 | obj() |
| 47 | cb.assert_not_called() |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 48 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 49 | def test_call_after_interval_should_callback(self): |
| 50 | """Test that later call does callback.""" |
| 51 | cb = mock.Mock([]) |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 52 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 53 | self.time.return_value = 0 |
| 54 | obj = mainlib._TimedCallback(cb, 10) |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 55 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 56 | obj() |
| 57 | cb.assert_called_once() |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 58 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 59 | self.time.return_value = 10 |
| 60 | cb.reset_mock() |
| 61 | obj() |
| 62 | cb.assert_called_once() |