blob: 9bed5b335c3d786fe2d13d211a349b60836be5d7 [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
Allen Li02915572017-07-12 18:05:45 -070011from chromite.lib import cros_test_lib
12from chromite.scripts.sysmon import mainlib
Mike Frysinger40ffb532021-02-12 07:36:08 -050013from chromite.third_party import mock
Allen Li02915572017-07-12 18:05:45 -070014
15
Allen Li02915572017-07-12 18:05:45 -070016class TestTimedCallback(cros_test_lib.TestCase):
17 """Tests for _TimedCallback."""
18
19 def setUp(self):
20 patcher = mock.patch('time.time', autospec=True)
21 self.time = patcher.start()
22 self.addCleanup(patcher.stop)
23
24 def test_initial_call_should_callback(self):
25 """Test that initial call goes through."""
26 cb = mock.Mock([])
27
28 self.time.return_value = 0
29 obj = mainlib._TimedCallback(cb, 10)
30
31 obj()
32 cb.assert_called_once()
33
34 def test_call_within_interval_should_not_callback(self):
35 """Test that call too soon does not callback."""
36 cb = mock.Mock([])
37
38 self.time.return_value = 0
39 obj = mainlib._TimedCallback(cb, 10)
40
41 obj()
42 cb.assert_called_once()
43
44 cb.reset_mock()
45 obj()
46 cb.assert_not_called()
47
48 def test_call_after_interval_should_callback(self):
49 """Test that later call does callback."""
50 cb = mock.Mock([])
51
52 self.time.return_value = 0
53 obj = mainlib._TimedCallback(cb, 10)
54
55 obj()
56 cb.assert_called_once()
57
58 self.time.return_value = 10
59 cb.reset_mock()
60 obj()
61 cb.assert_called_once()