1#!/usr/bin/env vpython3 2# Copyright 2024 The Chromium Authors 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5"""File for testing modification_waiter.py.""" 6 7import unittest 8import unittest.mock as mock 9 10from modification_waiter import ModificationWaiter 11 12 13class ModificationWaiterTest(unittest.TestCase): 14 """Test ModificationWaiter.""" 15 16 @mock.patch('modification_waiter.time.sleep') 17 @mock.patch('modification_waiter.time.time', side_effect=[0, 60]) 18 def test_timeout(self, time_patch, sleep_patch) -> None: 19 """The behavior when timeout happens, it shouldn't call other functions 20 in the case.""" 21 ModificationWaiter('/').__exit__(None, None, None) 22 self.assertEqual(time_patch.call_count, 2) 23 self.assertEqual(sleep_patch.call_count, 0) 24 25 @mock.patch('modification_waiter.time.sleep') 26 @mock.patch('modification_waiter.time.time', side_effect=[0, 5]) 27 @mock.patch('modification_waiter.os.path.getmtime', side_effect=[0]) 28 def test_quiet(self, getmtime_patch, time_patch, sleep_patch) -> None: 29 """The behavior when no modifications happen during the last 5 seconds. 30 """ 31 ModificationWaiter('/').__exit__(None, None, None) 32 self.assertEqual(time_patch.call_count, 2) 33 self.assertEqual(getmtime_patch.call_count, 1) 34 self.assertEqual(sleep_patch.call_count, 0) 35 36 @mock.patch('modification_waiter.time.sleep') 37 @mock.patch('modification_waiter.time.time', side_effect=[5, 10]) 38 @mock.patch('modification_waiter.os.path.getmtime', side_effect=[0]) 39 def test_mod_before_now(self, getmtime_patch, time_patch, 40 sleep_patch) -> None: 41 """The behavior when no modifications happen before current time. 42 """ 43 ModificationWaiter('/').__exit__(None, None, None) 44 self.assertEqual(time_patch.call_count, 2) 45 self.assertEqual(getmtime_patch.call_count, 1) 46 self.assertEqual(sleep_patch.call_count, 0) 47 48 @mock.patch('modification_waiter.time.sleep') 49 @mock.patch('modification_waiter.time.time', side_effect=[0, 5, 10]) 50 @mock.patch('modification_waiter.os.path.getmtime', side_effect=[5, 5]) 51 def test_mod_after_now(self, getmtime_patch, time_patch, 52 sleep_patch) -> None: 53 """The behavior when a modification happens after current time. 54 """ 55 ModificationWaiter('/').__exit__(None, None, None) 56 self.assertEqual(time_patch.call_count, 3) 57 self.assertEqual(getmtime_patch.call_count, 2) 58 self.assertEqual(sleep_patch.call_count, 1) 59 60 @mock.patch('modification_waiter.time.sleep') 61 @mock.patch('modification_waiter.time.time', side_effect=[0, 5, 10, 15]) 62 @mock.patch('modification_waiter.os.path.getmtime', 63 side_effect=[5, 10, 10]) 64 def test_mod_twice_after_now(self, getmtime_patch, time_patch, 65 sleep_patch) -> None: 66 """The behavior when a modification happens after current time. 67 """ 68 ModificationWaiter('/').__exit__(None, None, None) 69 self.assertEqual(time_patch.call_count, 4) 70 self.assertEqual(getmtime_patch.call_count, 3) 71 self.assertEqual(sleep_patch.call_count, 2) 72 73 @mock.patch('modification_waiter.time.sleep') 74 @mock.patch('modification_waiter.time.time', side_effect=[10, 5, 10, 15]) 75 @mock.patch('modification_waiter.os.path.getmtime', 76 side_effect=[5, 10, 10]) 77 def test_decreased_time(self, getmtime_patch, time_patch, 78 sleep_patch) -> None: 79 """The behavior when time.time() returns decreased values.""" 80 ModificationWaiter('/').__exit__(None, None, None) 81 self.assertEqual(time_patch.call_count, 4) 82 self.assertEqual(getmtime_patch.call_count, 3) 83 self.assertEqual(sleep_patch.call_count, 2) 84 85 @mock.patch('modification_waiter.time.sleep') 86 @mock.patch('modification_waiter.time.time', side_effect=[0, 5, 10, 15]) 87 @mock.patch('modification_waiter.os.path.getmtime', side_effect=[5, 10, 5]) 88 def test_decreased_mtime(self, getmtime_patch, time_patch, 89 sleep_patch) -> None: 90 """The behavior when path.getmtime returns decreased values.""" 91 ModificationWaiter('/').__exit__(None, None, None) 92 self.assertEqual(time_patch.call_count, 4) 93 self.assertEqual(getmtime_patch.call_count, 3) 94 self.assertEqual(sleep_patch.call_count, 2) 95 96 97if __name__ == '__main__': 98 unittest.main() 99