-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_synchronizer.py
205 lines (145 loc) · 7.36 KB
/
test_synchronizer.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from typing import Any, cast
import pytest
import utcnow
def test_synchronizer_basic() -> None:
with utcnow.synchronizer as synchronizer:
assert utcnow.rfc3339_timestamp() == utcnow.rfc3339_timestamp("now")
assert utcnow.as_unixtime() == utcnow.as_unixtime("now")
assert utcnow.as_protobuf() == utcnow.as_protobuf("now")
assert utcnow.as_datetime() == utcnow.as_datetime("now")
assert synchronizer.datetime == utcnow.as_datetime()
assert synchronizer.time == utcnow.as_unixtime()
assert synchronizer.time_ns == int(utcnow.as_unixtime() * 1e6) * 1_000
with utcnow.utcnow.synchronizer as synchronizer:
assert utcnow.rfc3339_timestamp() == utcnow.rfc3339_timestamp("now")
assert utcnow.as_unixtime() == utcnow.as_unixtime("now")
assert utcnow.as_protobuf() == utcnow.as_protobuf("now")
assert utcnow.as_datetime() == utcnow.as_datetime("now")
assert synchronizer.datetime == utcnow.as_datetime()
assert synchronizer.time == utcnow.as_unixtime()
assert synchronizer.time_ns == int(utcnow.as_unixtime() * 1e6) * 1_000
def test_synchronizer_timediff() -> None:
assert utcnow.timediff("now", "now") == 0.0
assert utcnow.timediff("now", "+1h") == 3600.0
assert utcnow.timediff("-1h", "+0.5h") == 5400.0
with utcnow.synchronizer:
assert utcnow.timediff("now", "now") == 0.0
assert utcnow.timediff("now", "+1h") == 3600.0
assert utcnow.timediff("-1h", "+0.5h") == 5400.0
with utcnow.synchronizer("1984-08-01 20:50:33.414100+02:00"):
assert utcnow.timediff("now", "now") == 0.0
assert utcnow.timediff("now", "+1h") == 3600.0
assert utcnow.timediff("-1h", "+0.5h") == 5400.0
def test_synchronizer_now() -> None:
with utcnow.synchronizer("now"):
created_time = utcnow.rfc3339_timestamp()
expire_time = utcnow.rfc3339_timestamp("now", "+15m")
assert utcnow.timediff(created_time, expire_time, "seconds") == 900.0
def test_synchronizer_specific() -> None:
with utcnow.synchronizer("2022-01-01T01:00:00.000000Z") as synchronizer:
created_time = utcnow.rfc3339_timestamp()
expire_time = utcnow.rfc3339_timestamp("now", "+15m")
assert synchronizer.datetime == utcnow.as_datetime()
assert synchronizer.time == utcnow.as_unixtime()
assert synchronizer.time_ns == int(utcnow.as_unixtime() * 1e6) * 1_000
assert utcnow.timediff(created_time, expire_time, "seconds") == 900.0
assert created_time == "2022-01-01T01:00:00.000000Z"
assert expire_time == "2022-01-01T01:15:00.000000Z"
def test_synchronizer_precise_unixtime() -> None:
with utcnow.synchronizer(1695694079.9417229) as synchronizer:
created_time = utcnow.rfc3339_timestamp()
expire_time = utcnow.rfc3339_timestamp("now", "+15m")
assert synchronizer.datetime == utcnow.as_datetime()
assert synchronizer.time == utcnow.as_unixtime()
assert synchronizer.time_ns == int(utcnow.as_unixtime() * 1e6) * 1_000
assert synchronizer.time == 1695694079.941723
assert synchronizer.time_ns == 1695694079941723000
assert utcnow.timediff(created_time, expire_time, "seconds") == 900.0
assert created_time == "2023-09-26T02:07:59.941723Z"
assert expire_time == "2023-09-26T02:22:59.941723Z"
def test_synchronizer_approximate_unixtime() -> None:
with utcnow.synchronizer(1695694079.941723) as synchronizer:
created_time = utcnow.rfc3339_timestamp()
expire_time = utcnow.rfc3339_timestamp("now", "+15m")
assert synchronizer.datetime == utcnow.as_datetime()
assert synchronizer.time == utcnow.as_unixtime()
assert synchronizer.time_ns == int(utcnow.as_unixtime() * 1e6) * 1_000
assert synchronizer.time == 1695694079.941723
assert synchronizer.time_ns == 1695694079941723000
assert utcnow.timediff(created_time, expire_time, "seconds") == 900.0
assert created_time == "2023-09-26T02:07:59.941723Z"
assert expire_time == "2023-09-26T02:22:59.941723Z"
def test_synchronizer_only_modifier() -> None:
now = utcnow.rfc3339_timestamp()
with utcnow.synchronizer(utcnow.NOW, "+1h"):
timestamp = utcnow.rfc3339_timestamp()
assert 3601.0 > utcnow.timediff(now, timestamp, "seconds") >= 3600.0
with utcnow.synchronizer("-10s"):
timestamp = utcnow.rfc3339_timestamp()
assert -10.0 < utcnow.timediff(now, timestamp, "seconds") < -9.0
def test_synchronizer_expired() -> None:
synchronizer1 = utcnow.synchronizer("2022-01-01T01:00:00.000000Z")
synchronizer2 = utcnow.synchronizer("2022-01-01T02:00:00.000000Z")
assert synchronizer1 is not synchronizer2
assert "expired" in repr(synchronizer1)
assert "pending context" in repr(synchronizer2)
with pytest.raises(RuntimeError):
with synchronizer1 as synchronizer:
pass
assert utcnow.rfc3339_timestamp() != "2022-01-01T02:00:00.000000Z"
with synchronizer2 as synchronizer:
assert utcnow.rfc3339_timestamp() == "2022-01-01T02:00:00.000000Z"
assert synchronizer is utcnow.synchronizer
assert utcnow.rfc3339_timestamp() != "2022-01-01T02:00:00.000000Z"
with pytest.raises(RuntimeError):
with synchronizer2 as synchronizer:
pass
def test_synchronizer_reuse() -> None:
synchronizer = cast(Any, utcnow.synchronizer("2022-01-01T01:00:00.000000Z"))
with pytest.raises(RuntimeError):
synchronizer("2022-01-01T01:00:00.000000Z")
def test_synchronizer_repr() -> None:
synchronizer1 = utcnow.synchronizer()
assert "pending context" in repr(synchronizer1)
assert "pending context" not in repr(utcnow.synchronizer)
with synchronizer1 as synchronizer:
assert synchronizer is utcnow.synchronizer
assert synchronizer == utcnow.synchronizer
assert synchronizer is not synchronizer1
assert synchronizer != synchronizer1
assert "child" in repr(synchronizer1)
assert "active context" in repr(synchronizer1)
assert "main" in repr(synchronizer)
assert "active context" in repr(synchronizer)
assert "active context" not in repr(synchronizer1)
assert "active context" not in repr(synchronizer)
assert "deactivated context" in repr(synchronizer1)
with pytest.raises(RuntimeError):
with synchronizer1:
pass
def test_synchronizer_orphan() -> None:
synchronizer1 = utcnow.synchronizer("+10h")
assert "pending context" in repr(synchronizer1)
assert "pending context" not in repr(utcnow.synchronizer)
with utcnow.synchronizer as synchronizer:
assert synchronizer is utcnow.synchronizer
assert synchronizer is not synchronizer1
assert "child" in repr(synchronizer1)
assert "expired" in repr(synchronizer1)
assert "main" in repr(synchronizer)
assert "active context" in repr(synchronizer)
assert "expired" in repr(synchronizer1)
with pytest.raises(RuntimeError):
with synchronizer1:
pass
def test_synchronizer_nested() -> None:
with utcnow.synchronizer("2022-01-01T01:00:00.000000Z"):
with pytest.raises(RuntimeError):
with utcnow.synchronizer:
pass
with utcnow.synchronizer:
with pytest.raises(RuntimeError):
with utcnow.synchronizer:
pass
with pytest.raises(RuntimeError):
utcnow.synchronizer()