blob: bb5dc6341d077dd378b6a304c6f2d356024ae6ce [file] [log] [blame]
Allen Li02915572017-07-12 18:05:45 -07001# Copyright 2017 The Chromium OS Authors. All rights reserved.
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
9from __future__ import absolute_import
Allen Li02915572017-07-12 18:05:45 -070010
Mike Frysinger166fea02021-02-12 05:30:33 -050011from unittest import mock
12
Allen Li02915572017-07-12 18:05:45 -070013from chromite.lib import cros_test_lib
14from chromite.scripts.sysmon import mainlib
15
16
Allen Li02915572017-07-12 18:05:45 -070017class TestTimedCallback(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060018 """Tests for _TimedCallback."""
Allen Li02915572017-07-12 18:05:45 -070019
Alex Klein1699fab2022-09-08 08:46:06 -060020 def setUp(self):
21 patcher = mock.patch("time.time", autospec=True)
22 self.time = patcher.start()
23 self.addCleanup(patcher.stop)
Allen Li02915572017-07-12 18:05:45 -070024
Alex Klein1699fab2022-09-08 08:46:06 -060025 def test_initial_call_should_callback(self):
26 """Test that initial call goes through."""
27 cb = mock.Mock([])
Allen Li02915572017-07-12 18:05:45 -070028
Alex Klein1699fab2022-09-08 08:46:06 -060029 self.time.return_value = 0
30 obj = mainlib._TimedCallback(cb, 10)
Allen Li02915572017-07-12 18:05:45 -070031
Alex Klein1699fab2022-09-08 08:46:06 -060032 obj()
33 cb.assert_called_once()
Allen Li02915572017-07-12 18:05:45 -070034
Alex Klein1699fab2022-09-08 08:46:06 -060035 def test_call_within_interval_should_not_callback(self):
36 """Test that call too soon does not callback."""
37 cb = mock.Mock([])
Allen Li02915572017-07-12 18:05:45 -070038
Alex Klein1699fab2022-09-08 08:46:06 -060039 self.time.return_value = 0
40 obj = mainlib._TimedCallback(cb, 10)
Allen Li02915572017-07-12 18:05:45 -070041
Alex Klein1699fab2022-09-08 08:46:06 -060042 obj()
43 cb.assert_called_once()
Allen Li02915572017-07-12 18:05:45 -070044
Alex Klein1699fab2022-09-08 08:46:06 -060045 cb.reset_mock()
46 obj()
47 cb.assert_not_called()
Allen Li02915572017-07-12 18:05:45 -070048
Alex Klein1699fab2022-09-08 08:46:06 -060049 def test_call_after_interval_should_callback(self):
50 """Test that later call does callback."""
51 cb = mock.Mock([])
Allen Li02915572017-07-12 18:05:45 -070052
Alex Klein1699fab2022-09-08 08:46:06 -060053 self.time.return_value = 0
54 obj = mainlib._TimedCallback(cb, 10)
Allen Li02915572017-07-12 18:05:45 -070055
Alex Klein1699fab2022-09-08 08:46:06 -060056 obj()
57 cb.assert_called_once()
Allen Li02915572017-07-12 18:05:45 -070058
Alex Klein1699fab2022-09-08 08:46:06 -060059 self.time.return_value = 10
60 cb.reset_mock()
61 obj()
62 cb.assert_called_once()