forked from apache/cassandra-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_timestamps.py
More file actions
278 lines (225 loc) · 9.04 KB
/
test_timestamps.py
File metadata and controls
278 lines (225 loc) · 9.04 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# Copyright 2013-2016 DataStax, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
try:
import unittest2 as unittest
except ImportError:
import unittest # noqa
import mock
from cassandra import timestamps
import time
from threading import Thread, Lock
class _TimestampTestMixin(object):
@mock.patch('cassandra.timestamps.time')
def _call_and_check_results(self,
patched_time_module,
system_time_expected_stamp_pairs,
timestamp_generator=None):
"""
For each element in an iterable of (system_time, expected_timestamp)
pairs, call a :class:`cassandra.timestamps.MonotonicTimestampGenerator`
with system_times as the underlying time.time() result, then assert
that the result is expected_timestamp. Skips the check if
expected_timestamp is None.
"""
patched_time_module.time = mock.Mock()
system_times, expected_timestamps = zip(*system_time_expected_stamp_pairs)
patched_time_module.time.side_effect = system_times
tsg = timestamp_generator or timestamps.MonotonicTimestampGenerator()
for expected in expected_timestamps:
actual = tsg()
if expected is not None:
self.assertEqual(actual, expected)
# assert we patched timestamps.time.time correctly
with self.assertRaises(StopIteration):
tsg()
class TestTimestampGeneratorOutput(unittest.TestCase, _TimestampTestMixin):
"""
Mock time.time and test the output of MonotonicTimestampGenerator.__call__
given different patterns of changing results.
"""
def test_timestamps_during_and_after_same_system_time(self):
"""
Test that MonotonicTimestampGenerator's output increases by 1 when the
underlying system time is the same, then returns to normal when the
system time increases again.
@since 3.8.0
@expected_result Timestamps should increase monotonically over repeated system time.
@test_category timing
"""
self._call_and_check_results(
system_time_expected_stamp_pairs=(
(15.0, 15 * 1e6),
(15.0, 15 * 1e6 + 1),
(15.0, 15 * 1e6 + 2),
(15.01, 15.01 * 1e6))
)
def test_timestamps_during_and_after_backwards_system_time(self):
"""
Test that MonotonicTimestampGenerator's output increases by 1 when the
underlying system time goes backward, then returns to normal when the
system time increases again.
@since 3.8.0
@expected_result Timestamps should increase monotonically over system time going backwards.
@test_category timing
"""
self._call_and_check_results(
system_time_expected_stamp_pairs=(
(15.0, 15 * 1e6),
(13.0, 15 * 1e6 + 1),
(14.0, 15 * 1e6 + 2),
(13.5, 15 * 1e6 + 3),
(15.01, 15.01 * 1e6))
)
class TestTimestampGeneratorLogging(unittest.TestCase):
def setUp(self):
self.log_patcher = mock.patch('cassandra.timestamps.log')
self.addCleanup(self.log_patcher.stop)
self.patched_timestamp_log = self.log_patcher.start()
def assertLastCallArgRegex(self, call, pattern):
last_warn_args, last_warn_kwargs = call
self.assertEqual(len(last_warn_args), 1)
self.assertEqual(len(last_warn_kwargs), 0)
self.assertRegexpMatches(
last_warn_args[0],
pattern,
)
def test_basic_log_content(self):
"""
Tests there are logs
@since 3.8.0
@jira_ticket PYTHON-676
@expected_result logs
@test_category timing
"""
tsg = timestamps.MonotonicTimestampGenerator(
warning_threshold=1e-6,
warning_interval=1e-6
)
#The units of _last_warn is seconds
tsg._last_warn = 12
tsg._next_timestamp(20, tsg.last)
self.assertEqual(len(self.patched_timestamp_log.warn.call_args_list), 0)
tsg._next_timestamp(16, tsg.last)
self.assertEqual(len(self.patched_timestamp_log.warn.call_args_list), 1)
self.assertLastCallArgRegex(
self.patched_timestamp_log.warn.call_args,
r'Clock skew detected:.*\b16\b.*\b4\b.*\b20\b'
)
def test_disable_logging(self):
"""
Tests there are no logs when there is a clock skew if logging is disabled
@since 3.8.0
@jira_ticket PYTHON-676
@expected_result no logs
@test_category timing
"""
no_warn_tsg = timestamps.MonotonicTimestampGenerator(warn_on_drift=False)
no_warn_tsg.last = 100
no_warn_tsg._next_timestamp(99, no_warn_tsg.last)
self.assertEqual(len(self.patched_timestamp_log.warn.call_args_list), 0)
def test_warning_threshold_respected_no_logging(self):
"""
Tests there are no logs if `warning_threshold` is not exceeded
@since 3.8.0
@jira_ticket PYTHON-676
@expected_result no logs
@test_category timing
"""
tsg = timestamps.MonotonicTimestampGenerator(
warning_threshold=2e-6,
)
tsg.last, tsg._last_warn = 100, 97
tsg._next_timestamp(98, tsg.last)
self.assertEqual(len(self.patched_timestamp_log.warn.call_args_list), 0)
def test_warning_threshold_respected_logs(self):
"""
Tests there are logs if `warning_threshold` is exceeded
@since 3.8.0
@jira_ticket PYTHON-676
@expected_result logs
@test_category timing
"""
tsg = timestamps.MonotonicTimestampGenerator(
warning_threshold=1e-6,
warning_interval=1e-6
)
tsg.last, tsg._last_warn = 100, 97
tsg._next_timestamp(98, tsg.last)
self.assertEqual(len(self.patched_timestamp_log.warn.call_args_list), 1)
def test_warning_interval_respected_no_logging(self):
"""
Tests there is only one log in the interval `warning_interval`
@since 3.8.0
@jira_ticket PYTHON-676
@expected_result one log
@test_category timing
"""
tsg = timestamps.MonotonicTimestampGenerator(
warning_threshold=1e-6,
warning_interval=2e-6
)
tsg.last = 100
tsg._next_timestamp(70, tsg.last)
self.assertEqual(len(self.patched_timestamp_log.warn.call_args_list), 1)
tsg._next_timestamp(71, tsg.last)
self.assertEqual(len(self.patched_timestamp_log.warn.call_args_list), 1)
def test_warning_interval_respected_logs(self):
"""
Tests there are logs again if the
clock skew happens after`warning_interval`
@since 3.8.0
@jira_ticket PYTHON-676
@expected_result logs
@test_category timing
"""
tsg = timestamps.MonotonicTimestampGenerator(
warning_interval=1e-6,
warning_threshold=1e-6,
)
tsg.last = 100
tsg._next_timestamp(70, tsg.last)
self.assertEqual(len(self.patched_timestamp_log.warn.call_args_list), 1)
tsg._next_timestamp(72, tsg.last)
self.assertEqual(len(self.patched_timestamp_log.warn.call_args_list), 2)
class TestTimestampGeneratorMultipleThreads(unittest.TestCase):
def test_should_generate_incrementing_timestamps_for_all_threads(self):
"""
Tests when time is "stopped", values are assigned incrementally
@since 3.8.0
@jira_ticket PYTHON-676
@expected_result the returned values increase
@test_category timing
"""
lock = Lock()
def request_time():
for _ in range(timestamp_to_generate):
timestamp = tsg()
with lock:
generated_timestamps.append(timestamp)
tsg = timestamps.MonotonicTimestampGenerator()
fixed_time = 1
num_threads = 5
timestamp_to_generate = 1000
generated_timestamps = []
with mock.patch('time.time', new=mock.Mock(return_value=fixed_time)):
threads = []
for _ in range(num_threads):
threads.append(Thread(target=request_time))
for t in threads:
t.start()
for t in threads:
t.join()
self.assertEqual(len(generated_timestamps), num_threads * timestamp_to_generate)
for i, timestamp in enumerate(sorted(generated_timestamps)):
self.assertEqual(int(i + 1e6), timestamp)