-
Notifications
You must be signed in to change notification settings - Fork 1
/
fakesleep.py
59 lines (43 loc) · 1.59 KB
/
fakesleep.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
"""provides a fake sleep() for use in tests"""
import time as time_mod
originals = {name: getattr(time_mod, name) for name in
['time', 'sleep', 'gmtime', 'localtime', 'ctime', 'asctime', 'strftime']}
epoch = None #: the fake current time
def reset(seconds=None):
"""reset the global fake time to the real current time
:arg float seconds: fake current time, in seconds since the epoch. If None, use the real current time.
"""
global epoch
epoch = seconds if seconds is not None else originals['time']()
reset()
def time():
return epoch
def sleep(seconds):
global epoch
epoch += seconds
def gmtime(seconds=None):
return originals['gmtime'](seconds if seconds is not None else epoch)
def localtime(seconds=None):
return originals['localtime'](seconds if seconds is not None else epoch)
def ctime(seconds=None):
return originals['ctime'](seconds if seconds is not None else epoch)
def asctime(t=None):
return originals['asctime'](t if t is not None else localtime())
def strftime(format, t=None):
return originals['strftime'](format, t if t is not None else localtime())
def monkey_patch():
"""monkey patch `time` module to use out versions"""
reset()
time_mod.time = time
time_mod.sleep = sleep
time_mod.gmtime = gmtime
time_mod.localtime = localtime
time_mod.ctime = ctime
time_mod.asctime = asctime
time_mod.strftime = strftime
def monkey_restore():
"""restore real versions. Inverse of `monkey_patch`"""
for k, v in originals.items():
setattr(time_mod, k, v)
global epoch
epoch = None