forked from piyush01123/Daily-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol.py
40 lines (31 loc) · 876 Bytes
/
sol.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
import time
from threading import Timer
import unittest
def debounce(N):
def inner(func):
def debounced(*args, **kwargs):
def call_fn():
rv = func(*args, **kwargs)
return rv
try:
debounced.timer.cancel()
except AttributeError:
pass
debounced.timer = Timer(N, call_fn)
debounced.timer.start()
return debounced
return inner
class TestDebounce(unittest.TestCase):
def setUp(self):
self.count = 0
@debounce(10)
def increment(self):
self.count += 1
def test_debounce(self):
self.assertEqual(self.count, 0)
self.increment()
self.assertEqual(self.count, 0)
time.sleep(10.01)
self.assertEqual(self.count, 1)
if __name__ == '__main__':
unittest.main()