-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest_telemetry.py
More file actions
302 lines (266 loc) · 15.8 KB
/
Copy pathtest_telemetry.py
File metadata and controls
302 lines (266 loc) · 15.8 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
"""Telemetry Worker tests."""
import unittest.mock as mock
import pytest
import queue
import asyncio
from splitio.sync.telemetry import TelemetrySynchronizer, TelemetrySynchronizerAsync, InMemoryTelemetrySubmitter, InMemoryTelemetrySubmitterAsync
from splitio.engine.telemetry import TelemetryStorageConsumer, TelemetryStorageConsumerAsync
from splitio.storage.inmemmory import InMemoryTelemetryStorage, InMemoryTelemetryStorageAsync, InMemorySegmentStorage, InMemorySegmentStorageAsync, InMemorySplitStorage, InMemorySplitStorageAsync
from splitio.models.splits import Split, Status
from splitio.models.segments import Segment
from splitio.models.telemetry import StreamingEvents, StreamingEventsAsync
from splitio.api.telemetry import TelemetryAPI
class TelemetrySynchronizerTests(object):
"""Telemetry synchronizer test cases."""
@mock.patch('splitio.sync.telemetry.InMemoryTelemetrySubmitter.synchronize_config')
def test_synchronize_config(self, mocker):
telemetry_synchronizer = TelemetrySynchronizer(InMemoryTelemetrySubmitter(mocker.Mock(), mocker.Mock(), mocker.Mock(), mocker.Mock()))
telemetry_synchronizer.synchronize_config()
assert(mocker.called)
@mock.patch('splitio.sync.telemetry.InMemoryTelemetrySubmitter.synchronize_stats')
def test_synchronize_stats(self, mocker):
telemetry_synchronizer = TelemetrySynchronizer(InMemoryTelemetrySubmitter(mocker.Mock(), mocker.Mock(), mocker.Mock(), mocker.Mock()))
telemetry_synchronizer.synchronize_stats()
assert(mocker.called)
class TelemetrySynchronizerAsyncTests(object):
"""Telemetry synchronizer async test cases."""
@pytest.mark.asyncio
async def test_synchronize_config(self, mocker):
telemetry_synchronizer = TelemetrySynchronizerAsync(InMemoryTelemetrySubmitterAsync(mocker.Mock(), mocker.Mock(), mocker.Mock(), mocker.Mock()))
self.called = False
async def synchronize_config():
self.called = True
telemetry_synchronizer.synchronize_config = synchronize_config
await telemetry_synchronizer.synchronize_config()
assert(self.called)
@pytest.mark.asyncio
async def test_synchronize_stats(self, mocker):
telemetry_synchronizer = TelemetrySynchronizer(InMemoryTelemetrySubmitter(mocker.Mock(), mocker.Mock(), mocker.Mock(), mocker.Mock()))
self.called = False
async def synchronize_stats():
self.called = True
telemetry_synchronizer.synchronize_stats = synchronize_stats
await telemetry_synchronizer.synchronize_stats()
assert(self.called)
class TelemetrySubmitterTests(object):
"""Telemetry submitter test cases."""
def test_synchronize_telemetry(self, mocker):
api = mocker.Mock(spec=TelemetryAPI)
telemetry_storage = InMemoryTelemetryStorage()
telemetry_consumer = TelemetryStorageConsumer(telemetry_storage)
events_queue = queue.Queue()
split_storage = InMemorySplitStorage(events_queue)
split_storage.update([Split('split1', 1234, 1, False, 'user', Status.ACTIVE, 123)], [], -1)
segment_storage = InMemorySegmentStorage(events_queue)
segment_storage.put(Segment('segment1', [], 123))
telemetry_submitter = InMemoryTelemetrySubmitter(telemetry_consumer, split_storage, segment_storage, api)
telemetry_storage._counters._impressions_queued = 100
telemetry_storage._counters._impressions_deduped = 30
telemetry_storage._counters._impressions_dropped = 0
telemetry_storage._counters._events_queued = 20
telemetry_storage._counters._events_dropped = 10
telemetry_storage._counters._auth_rejections = 1
telemetry_storage._counters._token_refreshes = 3
telemetry_storage._counters._session_length = 3
telemetry_storage._counters._update_from_sse['sp'] = 3
telemetry_storage._method_exceptions._treatment = 10
telemetry_storage._method_exceptions._treatments = 1
telemetry_storage._method_exceptions._treatment_with_config = 5
telemetry_storage._method_exceptions._treatments_with_config = 1
telemetry_storage._method_exceptions._treatments_by_flag_set = 2
telemetry_storage._method_exceptions._treatments_by_flag_sets = 3
telemetry_storage._method_exceptions._treatments_with_config_by_flag_set = 4
telemetry_storage._method_exceptions._treatments_with_config_by_flag_sets = 6
telemetry_storage._method_exceptions._track = 3
telemetry_storage._last_synchronization._split = 5
telemetry_storage._last_synchronization._segment = 3
telemetry_storage._last_synchronization._impression = 10
telemetry_storage._last_synchronization._impression_count = 0
telemetry_storage._last_synchronization._event = 4
telemetry_storage._last_synchronization._telemetry = 0
telemetry_storage._last_synchronization._token = 3
telemetry_storage._http_sync_errors._split = {'500': 3, '501': 2}
telemetry_storage._http_sync_errors._segment = {'401': 1}
telemetry_storage._http_sync_errors._impression = {'500': 1}
telemetry_storage._http_sync_errors._impression_count = {'401': 5}
telemetry_storage._http_sync_errors._event = {'404': 10}
telemetry_storage._http_sync_errors._telemetry = {'501': 3}
telemetry_storage._http_sync_errors._token = {'505': 11}
telemetry_storage._streaming_events = StreamingEvents()
telemetry_storage._tags = ['tag1']
telemetry_storage._method_latencies._treatment = [1] + [0] * 22
telemetry_storage._method_latencies._treatments = [0] * 23
telemetry_storage._method_latencies._treatment_with_config = [0] * 23
telemetry_storage._method_latencies._treatments_with_config = [0] * 23
telemetry_storage._method_latencies._treatments_by_flag_set = [1] + [0] * 22
telemetry_storage._method_latencies._treatments_by_flag_sets = [0] * 23
telemetry_storage._method_latencies._treatments_with_config_by_flag_set = [1] + [0] * 22
telemetry_storage._method_latencies._treatments_with_config_by_flag_sets = [0] * 23
telemetry_storage._method_latencies._track = [0] * 23
telemetry_storage._http_latencies._split = [1] + [0] * 22
telemetry_storage._http_latencies._segment = [0] * 23
telemetry_storage._http_latencies._impression = [0] * 23
telemetry_storage._http_latencies._impression_count = [0] * 23
telemetry_storage._http_latencies._event = [0] * 23
telemetry_storage._http_latencies._telemetry = [0] * 23
telemetry_storage._http_latencies._token = [0] * 23
telemetry_storage.record_config({'operationMode': 'inmemory',
'storageType': None,
'streamingEnabled': True,
'impressionsQueueSize': 100,
'eventsQueueSize': 200,
'impressionsMode': 'DEBUG',
'impressionListener': None,
'featuresRefreshRate': 30,
'segmentsRefreshRate': 30,
'impressionsRefreshRate': 60,
'eventsPushRate': 60,
'metricsRefreshRate': 10,
'activeFactoryCount': 1,
'notReady': 0,
'timeUntilReady': 1
}, {}, 0, 0
)
self.formatted_config = ""
def record_init(*args, **kwargs):
self.formatted_config = args[0]
api.record_init.side_effect = record_init
telemetry_submitter.synchronize_config()
assert(self.formatted_config == telemetry_submitter._telemetry_init_consumer.get_config_stats())
def record_stats(*args, **kwargs):
self.formatted_stats = args[0]
api.record_stats.side_effect = record_stats
telemetry_submitter.synchronize_stats()
assert(self.formatted_stats == {
"iQ": 100,
"iDe": 30,
"iDr": 0,
"eQ": 20,
"eD": 10,
"lS": {"sp": 5, "se": 3, "im": 10, "ic": 0, "ev": 4, "te": 0, "to": 3},
"t": ["tag1"],
"hE": {"sp": {"500": 3, "501": 2}, "se": {"401": 1}, "im": {"500": 1}, "ic": {"401": 5}, "ev": {"404": 10}, "te": {"501": 3}, "to": {"505": 11}},
"hL": {"sp": [1] + [0] * 22, "se": [0] * 23, "im": [0] * 23, "ic": [0] * 23, "ev": [0] * 23, "te": [0] * 23, "to": [0] * 23},
"aR": 1,
"tR": 3,
"sE": [],
"sL": 3,
"mE": {"t": 10, "ts": 1, "tc": 5, "tcs": 1, "tf": 2, "tfs": 3, "tcf": 4, "tcfs": 6, "tr": 3},
"mL": {"t": [1] + [0] * 22, "ts": [0] * 23, "tc": [0] * 23, "tcs": [0] * 23, "tf": [1] + [0] * 22, "tfs": [0] * 23, "tcf": [1] + [0] * 22, "tcfs": [0] * 23, "tr": [0] * 23},
"spC": 1,
"seC": 1,
"skC": 0,
"ufs": {"rbs": 0, "sp": 3},
"t": ['tag1']
})
class TelemetrySubmitterAsyncTests(object):
"""Telemetry submitter async test cases."""
@pytest.mark.asyncio
async def test_synchronize_telemetry(self, mocker):
api = mocker.Mock(spec=TelemetryAPI)
telemetry_storage = await InMemoryTelemetryStorageAsync.create()
telemetry_consumer = TelemetryStorageConsumerAsync(telemetry_storage)
split_storage = InMemorySplitStorageAsync(asyncio.Queue())
await split_storage.update([Split('split1', 1234, 1, False, 'user', Status.ACTIVE, 123)], [], -1)
segment_storage = InMemorySegmentStorageAsync(asyncio.Queue())
await segment_storage.put(Segment('segment1', [], 123))
telemetry_submitter = InMemoryTelemetrySubmitterAsync(telemetry_consumer, split_storage, segment_storage, api)
telemetry_storage._counters._impressions_queued = 100
telemetry_storage._counters._impressions_deduped = 30
telemetry_storage._counters._impressions_dropped = 0
telemetry_storage._counters._events_queued = 20
telemetry_storage._counters._events_dropped = 10
telemetry_storage._counters._auth_rejections = 1
telemetry_storage._counters._token_refreshes = 3
telemetry_storage._counters._session_length = 3
telemetry_storage._counters._update_from_sse['sp'] = 3
telemetry_storage._method_exceptions._treatment = 10
telemetry_storage._method_exceptions._treatments = 1
telemetry_storage._method_exceptions._treatment_with_config = 5
telemetry_storage._method_exceptions._treatments_with_config = 1
telemetry_storage._method_exceptions._treatments_by_flag_set = 2
telemetry_storage._method_exceptions._treatments_by_flag_sets = 3
telemetry_storage._method_exceptions._treatments_with_config_by_flag_set = 4
telemetry_storage._method_exceptions._treatments_with_config_by_flag_sets = 6
telemetry_storage._method_exceptions._track = 3
telemetry_storage._last_synchronization._split = 5
telemetry_storage._last_synchronization._segment = 3
telemetry_storage._last_synchronization._impression = 10
telemetry_storage._last_synchronization._impression_count = 0
telemetry_storage._last_synchronization._event = 4
telemetry_storage._last_synchronization._telemetry = 0
telemetry_storage._last_synchronization._token = 3
telemetry_storage._http_sync_errors._split = {'500': 3, '501': 2}
telemetry_storage._http_sync_errors._segment = {'401': 1}
telemetry_storage._http_sync_errors._impression = {'500': 1}
telemetry_storage._http_sync_errors._impression_count = {'401': 5}
telemetry_storage._http_sync_errors._event = {'404': 10}
telemetry_storage._http_sync_errors._telemetry = {'501': 3}
telemetry_storage._http_sync_errors._token = {'505': 11}
telemetry_storage._streaming_events = await StreamingEventsAsync.create()
telemetry_storage._tags = ['tag1']
telemetry_storage._method_latencies._treatment = [1] + [0] * 22
telemetry_storage._method_latencies._treatments = [0] * 23
telemetry_storage._method_latencies._treatment_with_config = [0] * 23
telemetry_storage._method_latencies._treatments_with_config = [0] * 23
telemetry_storage._method_latencies._treatments_by_flag_set = [1] + [0] * 22
telemetry_storage._method_latencies._treatments_by_flag_sets = [0] * 23
telemetry_storage._method_latencies._treatments_with_config_by_flag_set = [1] + [0] * 22
telemetry_storage._method_latencies._treatments_with_config_by_flag_sets = [0] * 23
telemetry_storage._method_latencies._track = [0] * 23
telemetry_storage._http_latencies._split = [1] + [0] * 22
telemetry_storage._http_latencies._segment = [0] * 23
telemetry_storage._http_latencies._impression = [0] * 23
telemetry_storage._http_latencies._impression_count = [0] * 23
telemetry_storage._http_latencies._event = [0] * 23
telemetry_storage._http_latencies._telemetry = [0] * 23
telemetry_storage._http_latencies._token = [0] * 23
await telemetry_storage.record_config({'operationMode': 'inmemory',
'storageType': None,
'streamingEnabled': True,
'impressionsQueueSize': 100,
'eventsQueueSize': 200,
'impressionsMode': 'DEBUG',
'impressionListener': None,
'featuresRefreshRate': 30,
'segmentsRefreshRate': 30,
'impressionsRefreshRate': 60,
'eventsPushRate': 60,
'metricsRefreshRate': 10,
'activeFactoryCount': 1,
'notReady': 0,
'timeUntilReady': 1
}, {}, 0, 0
)
self.formatted_config = ""
async def record_init(*args, **kwargs):
self.formatted_config = args[0]
api.record_init = record_init
await telemetry_submitter.synchronize_config()
assert(self.formatted_config == await telemetry_submitter._telemetry_init_consumer.get_config_stats())
async def record_stats(*args, **kwargs):
self.formatted_stats = args[0]
api.record_stats = record_stats
await telemetry_submitter.synchronize_stats()
assert(self.formatted_stats == {
"iQ": 100,
"iDe": 30,
"iDr": 0,
"eQ": 20,
"eD": 10,
"lS": {"sp": 5, "se": 3, "im": 10, "ic": 0, "ev": 4, "te": 0, "to": 3},
"t": ["tag1"],
"hE": {"sp": {"500": 3, "501": 2}, "se": {"401": 1}, "im": {"500": 1}, "ic": {"401": 5}, "ev": {"404": 10}, "te": {"501": 3}, "to": {"505": 11}},
"hL": {"sp": [1] + [0] * 22, "se": [0] * 23, "im": [0] * 23, "ic": [0] * 23, "ev": [0] * 23, "te": [0] * 23, "to": [0] * 23},
"aR": 1,
"tR": 3,
"sE": [],
"sL": 3,
"mE": {"t": 10, "ts": 1, "tc": 5, "tcs": 1, "tf": 2, "tfs": 3, "tcf": 4, "tcfs": 6, "tr": 3},
"mL": {"t": [1] + [0] * 22, "ts": [0] * 23, "tc": [0] * 23, "tcs": [0] * 23, "tf": [1] + [0] * 22, "tfs": [0] * 23, "tcf": [1] + [0] * 22, "tcfs": [0] * 23, "tr": [0] * 23},
"spC": 1,
"seC": 1,
"skC": 0,
"ufs": {"rbs": 0, "sp": 3},
"t": ['tag1']
})