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