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