Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 1 | # 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 | |
| 9 | from __future__ import absolute_import |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 10 | |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 11 | from chromite.lib import cros_test_lib |
| 12 | from chromite.scripts.sysmon import mainlib |
Mike Frysinger | 40ffb53 | 2021-02-12 07:36:08 -0500 | [diff] [blame] | 13 | from chromite.third_party import mock |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 14 | |
| 15 | |
Allen Li | 0291557 | 2017-07-12 18:05:45 -0700 | [diff] [blame] | 16 | class 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() |