-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest_telemetry_model.py
More file actions
662 lines (601 loc) · 42 KB
/
Copy pathtest_telemetry_model.py
File metadata and controls
662 lines (601 loc) · 42 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
"""Telemetry model test module."""
import os
import random
import pytest
from splitio.models.telemetry import StorageType, OperationMode, MethodLatencies, MethodExceptions, \
HTTPLatencies, HTTPErrors, LastSynchronization, TelemetryCounters, TelemetryConfig, \
StreamingEvent, StreamingEvents, MethodExceptionsAsync, HTTPLatenciesAsync, HTTPErrorsAsync, LastSynchronizationAsync, \
TelemetryCountersAsync, TelemetryConfigAsync, StreamingEventsAsync, MethodLatenciesAsync, UpdateFromSSE
import splitio.models.telemetry as ModelTelemetry
class TelemetryModelTests(object):
"""Telemetry model test cases."""
def test_latency_bucket_index(self):
for i in range(50000):
latency = random.randint(10, 9987885)
old_bucket = 0
result_bucket = 0
counter = -1
for j in ModelTelemetry.BUCKETS:
counter += 1
if old_bucket == 0:
if latency < j:
old_bucket = 0
break
old_bucket = j
continue
if counter == ModelTelemetry.MAX_LATENCY_BUCKET_COUNT - 1:
result_bucket = 22
break
if latency > old_bucket and latency <= j:
result_bucket = counter
break
old_bucket = j
print(latency, old_bucket, j)
assert(result_bucket == ModelTelemetry.get_latency_bucket_index(latency))
def test_storage_type_and_operation_mode(self, mocker):
assert(StorageType.MEMORY.value == 'memory')
assert(StorageType.REDIS.value == 'redis')
assert(OperationMode.STANDALONE.value == 'standalone')
assert(OperationMode.CONSUMER.value == 'consumer')
def test_method_latencies(self, mocker):
method_latencies = MethodLatencies()
method_latencies.pop_all() # should not raise exception
for method in ModelTelemetry.MethodExceptionsAndLatencies:
method_latencies.add_latency(method, 50)
if method.value == 'treatment':
assert(method_latencies._treatment[ModelTelemetry.get_latency_bucket_index(50)] == 1)
elif method.value == 'treatments':
assert(method_latencies._treatments[ModelTelemetry.get_latency_bucket_index(50)] == 1)
elif method.value == 'treatment_with_config':
assert(method_latencies._treatment_with_config[ModelTelemetry.get_latency_bucket_index(50)] == 1)
elif method.value == 'treatments_with_config':
assert(method_latencies._treatments_with_config[ModelTelemetry.get_latency_bucket_index(50)] == 1)
elif method.value == 'treatments_by_flag_set':
assert(method_latencies._treatments_by_flag_set[ModelTelemetry.get_latency_bucket_index(50)] == 1)
elif method.value == 'treatments_by_flag_sets':
assert(method_latencies._treatments_by_flag_sets[ModelTelemetry.get_latency_bucket_index(50)] == 1)
elif method.value == 'treatments_with_config_by_flag_set':
assert(method_latencies._treatments_with_config_by_flag_set[ModelTelemetry.get_latency_bucket_index(50)] == 1)
elif method.value == 'treatments_with_config_by_flag_sets':
assert(method_latencies._treatments_with_config_by_flag_sets[ModelTelemetry.get_latency_bucket_index(50)] == 1)
elif method.value == 'track':
assert(method_latencies._track[ModelTelemetry.get_latency_bucket_index(50)] == 1)
method_latencies.add_latency(method, 50000000)
if method.value == 'treatment':
assert(method_latencies._treatment[ModelTelemetry.get_latency_bucket_index(50000000)] == 1)
if method.value == 'treatments':
assert(method_latencies._treatments[ModelTelemetry.get_latency_bucket_index(50000000)] == 1)
if method.value == 'treatment_with_config':
assert(method_latencies._treatment_with_config[ModelTelemetry.get_latency_bucket_index(50000000)] == 1)
if method.value == 'treatments_with_config':
assert(method_latencies._treatments_with_config[ModelTelemetry.get_latency_bucket_index(50000000)] == 1)
elif method.value == 'treatments_by_flag_set':
assert(method_latencies._treatments_by_flag_set[ModelTelemetry.get_latency_bucket_index(50000000)] == 1)
elif method.value == 'treatments_by_flag_sets':
assert(method_latencies._treatments_by_flag_sets[ModelTelemetry.get_latency_bucket_index(50000000)] == 1)
elif method.value == 'treatments_with_config_by_flag_set':
assert(method_latencies._treatments_with_config_by_flag_set[ModelTelemetry.get_latency_bucket_index(50000000)] == 1)
elif method.value == 'treatments_with_config_by_flag_sets':
assert(method_latencies._treatments_with_config_by_flag_sets[ModelTelemetry.get_latency_bucket_index(50000000)] == 1)
if method.value == 'track':
assert(method_latencies._track[ModelTelemetry.get_latency_bucket_index(50000000)] == 1)
method_latencies.pop_all()
assert(method_latencies._track == [0] * 23)
assert(method_latencies._treatment == [0] * 23)
assert(method_latencies._treatments == [0] * 23)
assert(method_latencies._treatment_with_config == [0] * 23)
assert(method_latencies._treatments_with_config == [0] * 23)
assert(method_latencies._treatments_by_flag_set == [0] * 23)
assert(method_latencies._treatments_by_flag_sets == [0] * 23)
assert(method_latencies._treatments_with_config_by_flag_set == [0] * 23)
assert(method_latencies._treatments_with_config_by_flag_sets == [0] * 23)
method_latencies.add_latency(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENT, 10)
[method_latencies.add_latency(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS, 20) for i in range(2)]
method_latencies.add_latency(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENT_WITH_CONFIG, 50)
method_latencies.add_latency(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG, 20)
[method_latencies.add_latency(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS_BY_FLAG_SET, 20) for i in range(3)]
[method_latencies.add_latency(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS_BY_FLAG_SETS, 20) for i in range(4)]
[method_latencies.add_latency(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG_BY_FLAG_SET, 20) for i in range(5)]
[method_latencies.add_latency(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS, 20) for i in range(6)]
method_latencies.add_latency(ModelTelemetry.MethodExceptionsAndLatencies.TRACK, 20)
latencies = method_latencies.pop_all()
assert(latencies == {'methodLatencies': {'treatment': [1] + [0] * 22,
'treatments': [2] + [0] * 22,
'treatment_with_config': [1] + [0] * 22,
'treatments_with_config': [1] + [0] * 22,
'treatments_by_flag_set': [3] + [0] * 22,
'treatments_by_flag_sets': [4] + [0] * 22,
'treatments_with_config_by_flag_set': [5] + [0] * 22,
'treatments_with_config_by_flag_sets': [6] + [0] * 22,
'track': [1] + [0] * 22}})
def test_http_latencies(self, mocker):
http_latencies = HTTPLatencies()
http_latencies.pop_all() # should not raise exception
for resource in ModelTelemetry.HTTPExceptionsAndLatencies:
if self._get_http_latency(resource, http_latencies) == None:
continue
http_latencies.add_latency(resource, 50)
assert(self._get_http_latency(resource, http_latencies)[ModelTelemetry.get_latency_bucket_index(50)] == 1)
http_latencies.add_latency(resource, 50000000)
assert(self._get_http_latency(resource, http_latencies)[ModelTelemetry.get_latency_bucket_index(50000000)] == 1)
for j in range(10):
latency = random.randint(1001, 4987885)
current_count = self._get_http_latency(resource, http_latencies)[ModelTelemetry.get_latency_bucket_index(latency)]
[http_latencies.add_latency(resource, latency) for i in range(2)]
assert(self._get_http_latency(resource, http_latencies)[ModelTelemetry.get_latency_bucket_index(latency)] == 2 + current_count)
http_latencies.pop_all()
assert(http_latencies._event == [0] * 23)
assert(http_latencies._impression == [0] * 23)
assert(http_latencies._impression_count == [0] * 23)
assert(http_latencies._segment == [0] * 23)
assert(http_latencies._split == [0] * 23)
assert(http_latencies._telemetry == [0] * 23)
assert(http_latencies._token == [0] * 23)
http_latencies.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.SPLIT, 10)
[http_latencies.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.IMPRESSION, i) for i in [10, 20]]
http_latencies.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.SEGMENT, 40)
http_latencies.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.IMPRESSION_COUNT, 60)
http_latencies.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.EVENT, 90)
http_latencies.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.TELEMETRY, 70)
[http_latencies.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.TOKEN, i) for i in [10, 15]]
latencies = http_latencies.pop_all()
assert(latencies == {'httpLatencies': {'split': [1] + [0] * 22, 'segment': [1] + [0] * 22, 'impression': [2] + [0] * 22, 'impressionCount': [1] + [0] * 22, 'event': [1] + [0] * 22, 'telemetry': [1] + [0] * 22, 'token': [2] + [0] * 22}})
def _get_http_latency(self, resource, storage):
if resource == ModelTelemetry.HTTPExceptionsAndLatencies.SPLIT:
return storage._split
elif resource == ModelTelemetry.HTTPExceptionsAndLatencies.SEGMENT:
return storage._segment
elif resource == ModelTelemetry.HTTPExceptionsAndLatencies.IMPRESSION:
return storage._impression
elif resource == ModelTelemetry.HTTPExceptionsAndLatencies.IMPRESSION_COUNT:
return storage._impression_count
elif resource == ModelTelemetry.HTTPExceptionsAndLatencies.EVENT:
return storage._event
elif resource == ModelTelemetry.HTTPExceptionsAndLatencies.TELEMETRY:
return storage._telemetry
elif resource == ModelTelemetry.HTTPExceptionsAndLatencies.TOKEN:
return storage._token
else:
return
def test_method_exceptions(self, mocker):
method_exception = MethodExceptions()
exceptions = method_exception.pop_all() # should not raise exception
[method_exception.add_exception(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENT) for i in range(2)]
method_exception.add_exception(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS)
method_exception.add_exception(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENT_WITH_CONFIG)
[method_exception.add_exception(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG) for i in range(5)]
[method_exception.add_exception(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS_BY_FLAG_SET) for i in range(6)]
[method_exception.add_exception(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS_BY_FLAG_SETS) for i in range(7)]
[method_exception.add_exception(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG_BY_FLAG_SET) for i in range(8)]
[method_exception.add_exception(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS) for i in range(9)]
[method_exception.add_exception(ModelTelemetry.MethodExceptionsAndLatencies.TRACK) for i in range(3)]
exceptions = method_exception.pop_all()
assert(method_exception._treatment == 0)
assert(method_exception._treatments == 0)
assert(method_exception._treatment_with_config == 0)
assert(method_exception._treatments_with_config == 0)
assert(method_exception._treatments_by_flag_set == 0)
assert(method_exception._treatments_by_flag_sets == 0)
assert(method_exception._treatments_with_config_by_flag_set == 0)
assert(method_exception._treatments_with_config_by_flag_sets == 0)
assert(method_exception._track == 0)
assert(exceptions == {'methodExceptions': {'treatment': 2,
'treatments': 1,
'treatment_with_config': 1,
'treatments_with_config': 5,
'treatments_by_flag_set': 6,
'treatments_by_flag_sets': 7,
'treatments_with_config_by_flag_set': 8,
'treatments_with_config_by_flag_sets': 9,
'track': 3}})
def test_http_errors(self, mocker):
http_error = HTTPErrors()
errors = http_error.pop_all() # should not raise exception
[http_error.add_error(ModelTelemetry.HTTPExceptionsAndLatencies.SEGMENT, str(i)) for i in [500, 501, 502]]
[http_error.add_error(ModelTelemetry.HTTPExceptionsAndLatencies.SPLIT, str(i)) for i in [400, 401, 402]]
http_error.add_error(ModelTelemetry.HTTPExceptionsAndLatencies.IMPRESSION, '502')
[http_error.add_error(ModelTelemetry.HTTPExceptionsAndLatencies.IMPRESSION_COUNT, str(i)) for i in [501, 502]]
http_error.add_error(ModelTelemetry.HTTPExceptionsAndLatencies.EVENT, '501')
http_error.add_error(ModelTelemetry.HTTPExceptionsAndLatencies.TELEMETRY, '505')
[http_error.add_error(ModelTelemetry.HTTPExceptionsAndLatencies.TOKEN, '502') for i in range(5)]
errors = http_error.pop_all()
assert(errors == {'httpErrors': {'split': {'400': 1, '401': 1, '402': 1}, 'segment': {'500': 1, '501': 1, '502': 1},
'impression': {'502': 1}, 'impressionCount': {'501': 1, '502': 1},
'event': {'501': 1}, 'telemetry': {'505': 1}, 'token': {'502': 5}}})
assert(http_error._split == {})
assert(http_error._segment == {})
assert(http_error._impression == {})
assert(http_error._impression_count == {})
assert(http_error._event == {})
assert(http_error._telemetry == {})
def test_last_synchronization(self, mocker):
last_synchronization = LastSynchronization()
last_synchronization.get_all() # should not raise exception
last_synchronization.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.SPLIT, 10)
last_synchronization.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.IMPRESSION, 20)
last_synchronization.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.SEGMENT, 40)
last_synchronization.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.IMPRESSION_COUNT, 60)
last_synchronization.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.EVENT, 90)
last_synchronization.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.TELEMETRY, 70)
last_synchronization.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.TOKEN, 15)
assert(last_synchronization.get_all() == {'lastSynchronizations': {'split': 10, 'segment': 40, 'impression': 20, 'impressionCount': 60, 'event': 90, 'telemetry': 70, 'token': 15}})
def test_telemetry_counters(self):
telemetry_counter = TelemetryCounters()
assert(telemetry_counter._impressions_queued == 0)
assert(telemetry_counter._impressions_deduped == 0)
assert(telemetry_counter._impressions_dropped == 0)
assert(telemetry_counter._events_dropped == 0)
assert(telemetry_counter._events_queued == 0)
assert(telemetry_counter._auth_rejections == 0)
assert(telemetry_counter._token_refreshes == 0)
assert(telemetry_counter._update_from_sse == {})
assert(telemetry_counter.get_session_length() == 0)
telemetry_counter.record_session_length(20)
assert(telemetry_counter.get_session_length() == 20)
assert(telemetry_counter.pop_auth_rejections() == 0)
[telemetry_counter.record_auth_rejections() for i in range(5)]
auth_rejections = telemetry_counter.pop_auth_rejections()
assert(telemetry_counter._auth_rejections == 0)
assert(auth_rejections == 5)
assert(telemetry_counter.pop_token_refreshes() == 0)
[telemetry_counter.record_token_refreshes() for i in range(3)]
token_refreshes = telemetry_counter.pop_token_refreshes()
assert(telemetry_counter._token_refreshes == 0)
assert(token_refreshes == 3)
assert(telemetry_counter.get_counter_stats(ModelTelemetry.CounterConstants.IMPRESSIONS_QUEUED) == 0)
assert(telemetry_counter.get_counter_stats(ModelTelemetry.CounterConstants.IMPRESSIONS_DEDUPED) == 0)
assert(telemetry_counter.get_counter_stats(ModelTelemetry.CounterConstants.IMPRESSIONS_DROPPED) == 0)
assert(telemetry_counter.get_counter_stats(ModelTelemetry.CounterConstants.EVENTS_QUEUED) == 0)
assert(telemetry_counter.get_counter_stats(ModelTelemetry.CounterConstants.EVENTS_DROPPED) == 0)
telemetry_counter.record_impressions_value(ModelTelemetry.CounterConstants.IMPRESSIONS_QUEUED, 10)
assert(telemetry_counter._impressions_queued == 10)
telemetry_counter.record_impressions_value(ModelTelemetry.CounterConstants.IMPRESSIONS_DEDUPED, 14)
assert(telemetry_counter._impressions_deduped == 14)
telemetry_counter.record_impressions_value(ModelTelemetry.CounterConstants.IMPRESSIONS_DROPPED, 2)
assert(telemetry_counter._impressions_dropped == 2)
telemetry_counter.record_events_value(ModelTelemetry.CounterConstants.EVENTS_QUEUED, 30)
assert(telemetry_counter._events_queued == 30)
telemetry_counter.record_events_value(ModelTelemetry.CounterConstants.EVENTS_DROPPED, 1)
assert(telemetry_counter._events_dropped == 1)
telemetry_counter.record_update_from_sse(UpdateFromSSE.SPLIT_UPDATE)
assert(telemetry_counter._update_from_sse[UpdateFromSSE.SPLIT_UPDATE.value] == 1)
updates = telemetry_counter.pop_update_from_sse(UpdateFromSSE.SPLIT_UPDATE)
assert(telemetry_counter._update_from_sse[UpdateFromSSE.SPLIT_UPDATE.value] == 0)
assert(updates == 1)
def test_streaming_event(self, mocker):
streaming_event = StreamingEvent((ModelTelemetry.StreamingEventTypes.CONNECTION_ESTABLISHED, 'split', 1234))
assert(streaming_event.type == ModelTelemetry.StreamingEventTypes.CONNECTION_ESTABLISHED.value)
assert(streaming_event.data == 'split')
assert(streaming_event.time == 1234)
def test_streaming_events(self, mocker):
streaming_events = StreamingEvents()
events = streaming_events.pop_streaming_events() # should not raise exception
streaming_events.record_streaming_event((ModelTelemetry.StreamingEventTypes.CONNECTION_ESTABLISHED, 'split', 1234))
streaming_events.record_streaming_event((ModelTelemetry.StreamingEventTypes.STREAMING_STATUS, 'split', 1234))
events = streaming_events.pop_streaming_events()
assert(streaming_events._streaming_events == [])
assert(events == {'streamingEvents': [{'e': ModelTelemetry.StreamingEventTypes.CONNECTION_ESTABLISHED.value, 'd': 'split', 't': 1234},
{'e': ModelTelemetry.StreamingEventTypes.STREAMING_STATUS.value, 'd': 'split', 't': 1234}]})
def test_telemetry_config(self):
telemetry_config = TelemetryConfig()
stats = telemetry_config.get_stats() # should not raise exception
config = {'operationMode': 'standalone',
'streamingEnabled': True,
'impressionsQueueSize': 100,
'eventsQueueSize': 200,
'impressionsMode': 'DEBUG',''
'impressionListener': None,
'featuresRefreshRate': 30,
'segmentsRefreshRate': 30,
'impressionsRefreshRate': 60,
'eventsPushRate': 60,
'metricsRefreshRate': 10,
'storageType': None,
'flagSetsFilter': None
}
telemetry_config.record_config(config, {}, 5, 2)
assert(telemetry_config.get_stats() == {'oM': 0,
'sT': telemetry_config._get_storage_type(config['operationMode'], config['storageType']),
'sE': config['streamingEnabled'],
'rR': {'sp': 30, 'se': 30, 'im': 60, 'ev': 60, 'te': 10},
'uO': {'s': False, 'e': False, 'a': False, 'st': False, 't': False},
'iQ': config['impressionsQueueSize'],
'eQ': config['eventsQueueSize'],
'iM': telemetry_config._get_impressions_mode(config['impressionsMode']),
'iL': True if config['impressionListener'] is not None else False,
'hp': telemetry_config._check_if_proxy_detected(),
'tR': 0,
'nR': 0,
'bT': 0,
'aF': 0,
'rF': 0,
'fsT': 5,
'fsI': 2}
)
telemetry_config.record_ready_time(10)
assert(telemetry_config._time_until_ready == 10)
assert(telemetry_config.get_bur_time_outs() == 0)
[telemetry_config.record_bur_time_out() for i in range(2)]
assert(telemetry_config.get_bur_time_outs() == 2)
assert(telemetry_config.get_non_ready_usage() == 0)
[telemetry_config.record_not_ready_usage() for i in range(5)]
assert(telemetry_config.get_non_ready_usage() == 5)
os.environ["https_proxy"] = "some_host_ip"
assert(telemetry_config._check_if_proxy_detected() == True)
del os.environ["https_proxy"]
assert(telemetry_config._check_if_proxy_detected() == False)
os.environ["HTTPS_proxy"] = "some_host_ip"
assert(telemetry_config._check_if_proxy_detected() == True)
del os.environ["HTTPS_proxy"]
assert(telemetry_config._check_if_proxy_detected() == False)
class TelemetryModelAsyncTests(object):
"""Telemetry model async test cases."""
@pytest.mark.asyncio
async def test_method_latencies(self, mocker):
method_latencies = await MethodLatenciesAsync.create()
for method in ModelTelemetry.MethodExceptionsAndLatencies:
await method_latencies.add_latency(method, 50)
if method.value == 'treatment':
assert(method_latencies._treatment[ModelTelemetry.get_latency_bucket_index(50)] == 1)
elif method.value == 'treatments':
assert(method_latencies._treatments[ModelTelemetry.get_latency_bucket_index(50)] == 1)
elif method.value == 'treatment_with_config':
assert(method_latencies._treatment_with_config[ModelTelemetry.get_latency_bucket_index(50)] == 1)
elif method.value == 'treatments_with_config':
assert(method_latencies._treatments_with_config[ModelTelemetry.get_latency_bucket_index(50)] == 1)
elif method.value == 'treatments_by_flag_set':
assert(method_latencies._treatments_by_flag_set[ModelTelemetry.get_latency_bucket_index(50)] == 1)
elif method.value == 'treatments_by_flag_sets':
assert(method_latencies._treatments_by_flag_sets[ModelTelemetry.get_latency_bucket_index(50)] == 1)
elif method.value == 'treatments_with_config_by_flag_set':
assert(method_latencies._treatments_with_config_by_flag_set[ModelTelemetry.get_latency_bucket_index(50)] == 1)
elif method.value == 'treatments_with_config_by_flag_sets':
assert(method_latencies._treatments_with_config_by_flag_sets[ModelTelemetry.get_latency_bucket_index(50)] == 1)
elif method.value == 'track':
assert(method_latencies._track[ModelTelemetry.get_latency_bucket_index(50)] == 1)
await method_latencies.add_latency(method, 50000000)
if method.value == 'treatment':
assert(method_latencies._treatment[ModelTelemetry.get_latency_bucket_index(50000000)] == 1)
if method.value == 'treatments':
assert(method_latencies._treatments[ModelTelemetry.get_latency_bucket_index(50000000)] == 1)
if method.value == 'treatment_with_config':
assert(method_latencies._treatment_with_config[ModelTelemetry.get_latency_bucket_index(50000000)] == 1)
if method.value == 'treatments_with_config':
assert(method_latencies._treatments_with_config[ModelTelemetry.get_latency_bucket_index(50000000)] == 1)
elif method.value == 'treatments_by_flag_set':
assert(method_latencies._treatments_by_flag_set[ModelTelemetry.get_latency_bucket_index(50000000)] == 1)
elif method.value == 'treatments_by_flag_sets':
assert(method_latencies._treatments_by_flag_sets[ModelTelemetry.get_latency_bucket_index(50000000)] == 1)
elif method.value == 'treatments_with_config_by_flag_set':
assert(method_latencies._treatments_with_config_by_flag_set[ModelTelemetry.get_latency_bucket_index(50000000)] == 1)
elif method.value == 'treatments_with_config_by_flag_sets':
assert(method_latencies._treatments_with_config_by_flag_sets[ModelTelemetry.get_latency_bucket_index(50000000)] == 1)
if method.value == 'track':
assert(method_latencies._track[ModelTelemetry.get_latency_bucket_index(50000000)] == 1)
await method_latencies.pop_all()
assert(method_latencies._track == [0] * 23)
assert(method_latencies._treatment == [0] * 23)
assert(method_latencies._treatments == [0] * 23)
assert(method_latencies._treatment_with_config == [0] * 23)
assert(method_latencies._treatments_with_config == [0] * 23)
assert(method_latencies._treatments_by_flag_set == [0] * 23)
assert(method_latencies._treatments_by_flag_sets == [0] * 23)
assert(method_latencies._treatments_with_config_by_flag_set == [0] * 23)
assert(method_latencies._treatments_with_config_by_flag_sets == [0] * 23)
await method_latencies.add_latency(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENT, 10)
[await method_latencies.add_latency(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS, 20) for i in range(2)]
await method_latencies.add_latency(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENT_WITH_CONFIG, 50)
await method_latencies.add_latency(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG, 20)
[await method_latencies.add_latency(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS_BY_FLAG_SET, 20) for i in range(3)]
[await method_latencies.add_latency(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS_BY_FLAG_SETS, 20) for i in range(4)]
[await method_latencies.add_latency(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG_BY_FLAG_SET, 20) for i in range(5)]
[await method_latencies.add_latency(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS, 20) for i in range(6)]
await method_latencies.add_latency(ModelTelemetry.MethodExceptionsAndLatencies.TRACK, 20)
latencies = await method_latencies.pop_all()
assert(latencies == {'methodLatencies': {'treatment': [1] + [0] * 22,
'treatments': [2] + [0] * 22,
'treatment_with_config': [1] + [0] * 22,
'treatments_with_config': [1] + [0] * 22,
'treatments_by_flag_set': [3] + [0] * 22,
'treatments_by_flag_sets': [4] + [0] * 22,
'treatments_with_config_by_flag_set': [5] + [0] * 22,
'treatments_with_config_by_flag_sets': [6] + [0] * 22,
'track': [1] + [0] * 22}})
@pytest.mark.asyncio
async def test_http_latencies(self, mocker):
http_latencies = await HTTPLatenciesAsync.create()
for resource in ModelTelemetry.HTTPExceptionsAndLatencies:
if self._get_http_latency(resource, http_latencies) == None:
continue
await http_latencies.add_latency(resource, 50)
assert(self._get_http_latency(resource, http_latencies)[ModelTelemetry.get_latency_bucket_index(50)] == 1)
await http_latencies.add_latency(resource, 50000000)
assert(self._get_http_latency(resource, http_latencies)[ModelTelemetry.get_latency_bucket_index(50000000)] == 1)
for j in range(10):
latency = random.randint(1001, 4987885)
current_count = self._get_http_latency(resource, http_latencies)[ModelTelemetry.get_latency_bucket_index(latency)]
[await http_latencies.add_latency(resource, latency) for i in range(2)]
assert(self._get_http_latency(resource, http_latencies)[ModelTelemetry.get_latency_bucket_index(latency)] == 2 + current_count)
await http_latencies.pop_all()
assert(http_latencies._event == [0] * 23)
assert(http_latencies._impression == [0] * 23)
assert(http_latencies._impression_count == [0] * 23)
assert(http_latencies._segment == [0] * 23)
assert(http_latencies._split == [0] * 23)
assert(http_latencies._telemetry == [0] * 23)
assert(http_latencies._token == [0] * 23)
await http_latencies.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.SPLIT, 10)
[await http_latencies.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.IMPRESSION, i) for i in [10, 20]]
await http_latencies.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.SEGMENT, 40)
await http_latencies.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.IMPRESSION_COUNT, 60)
await http_latencies.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.EVENT, 90)
await http_latencies.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.TELEMETRY, 70)
[await http_latencies.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.TOKEN, i) for i in [10, 15]]
latencies = await http_latencies.pop_all()
assert(latencies == {'httpLatencies': {'split': [1] + [0] * 22, 'segment': [1] + [0] * 22, 'impression': [2] + [0] * 22, 'impressionCount': [1] + [0] * 22, 'event': [1] + [0] * 22, 'telemetry': [1] + [0] * 22, 'token': [2] + [0] * 22}})
def _get_http_latency(self, resource, storage):
if resource == ModelTelemetry.HTTPExceptionsAndLatencies.SPLIT:
return storage._split
elif resource == ModelTelemetry.HTTPExceptionsAndLatencies.SEGMENT:
return storage._segment
elif resource == ModelTelemetry.HTTPExceptionsAndLatencies.IMPRESSION:
return storage._impression
elif resource == ModelTelemetry.HTTPExceptionsAndLatencies.IMPRESSION_COUNT:
return storage._impression_count
elif resource == ModelTelemetry.HTTPExceptionsAndLatencies.EVENT:
return storage._event
elif resource == ModelTelemetry.HTTPExceptionsAndLatencies.TELEMETRY:
return storage._telemetry
elif resource == ModelTelemetry.HTTPExceptionsAndLatencies.TOKEN:
return storage._token
else:
return
@pytest.mark.asyncio
async def test_method_exceptions(self, mocker):
method_exception = await MethodExceptionsAsync.create()
[await method_exception.add_exception(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENT) for i in range(2)]
await method_exception.add_exception(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS)
await method_exception.add_exception(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENT_WITH_CONFIG)
[await method_exception.add_exception(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG) for i in range(5)]
[await method_exception.add_exception(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS_BY_FLAG_SET) for i in range(6)]
[await method_exception.add_exception(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS_BY_FLAG_SETS) for i in range(7)]
[await method_exception.add_exception(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG_BY_FLAG_SET) for i in range(8)]
[await method_exception.add_exception(ModelTelemetry.MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS) for i in range(9)]
[await method_exception.add_exception(ModelTelemetry.MethodExceptionsAndLatencies.TRACK) for i in range(3)]
exceptions = await method_exception.pop_all()
assert(method_exception._treatment == 0)
assert(method_exception._treatments == 0)
assert(method_exception._treatment_with_config == 0)
assert(method_exception._treatments_with_config == 0)
assert(method_exception._treatments_by_flag_set == 0)
assert(method_exception._treatments_by_flag_sets == 0)
assert(method_exception._treatments_with_config_by_flag_set == 0)
assert(method_exception._treatments_with_config_by_flag_sets == 0)
assert(method_exception._track == 0)
assert(exceptions == {'methodExceptions': {'treatment': 2,
'treatments': 1,
'treatment_with_config': 1,
'treatments_with_config': 5,
'treatments_by_flag_set': 6,
'treatments_by_flag_sets': 7,
'treatments_with_config_by_flag_set': 8,
'treatments_with_config_by_flag_sets': 9,
'track': 3}})
@pytest.mark.asyncio
async def test_http_errors(self, mocker):
http_error = await HTTPErrorsAsync.create()
[await http_error.add_error(ModelTelemetry.HTTPExceptionsAndLatencies.SEGMENT, str(i)) for i in [500, 501, 502]]
[await http_error.add_error(ModelTelemetry.HTTPExceptionsAndLatencies.SPLIT, str(i)) for i in [400, 401, 402]]
await http_error.add_error(ModelTelemetry.HTTPExceptionsAndLatencies.IMPRESSION, '502')
[await http_error.add_error(ModelTelemetry.HTTPExceptionsAndLatencies.IMPRESSION_COUNT, str(i)) for i in [501, 502]]
await http_error.add_error(ModelTelemetry.HTTPExceptionsAndLatencies.EVENT, '501')
await http_error.add_error(ModelTelemetry.HTTPExceptionsAndLatencies.TELEMETRY, '505')
[await http_error.add_error(ModelTelemetry.HTTPExceptionsAndLatencies.TOKEN, '502') for i in range(5)]
errors = await http_error.pop_all()
assert(errors == {'httpErrors': {'split': {'400': 1, '401': 1, '402': 1}, 'segment': {'500': 1, '501': 1, '502': 1},
'impression': {'502': 1}, 'impressionCount': {'501': 1, '502': 1},
'event': {'501': 1}, 'telemetry': {'505': 1}, 'token': {'502': 5}}})
assert(http_error._split == {})
assert(http_error._segment == {})
assert(http_error._impression == {})
assert(http_error._impression_count == {})
assert(http_error._event == {})
assert(http_error._telemetry == {})
@pytest.mark.asyncio
async def test_last_synchronization(self, mocker):
last_synchronization = await LastSynchronizationAsync.create()
await last_synchronization.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.SPLIT, 10)
await last_synchronization.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.IMPRESSION, 20)
await last_synchronization.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.SEGMENT, 40)
await last_synchronization.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.IMPRESSION_COUNT, 60)
await last_synchronization.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.EVENT, 90)
await last_synchronization.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.TELEMETRY, 70)
await last_synchronization.add_latency(ModelTelemetry.HTTPExceptionsAndLatencies.TOKEN, 15)
assert(await last_synchronization.get_all() == {'lastSynchronizations': {'split': 10, 'segment': 40, 'impression': 20, 'impressionCount': 60, 'event': 90, 'telemetry': 70, 'token': 15}})
@pytest.mark.asyncio
async def test_telemetry_counters(self):
telemetry_counter = await TelemetryCountersAsync.create()
assert(telemetry_counter._impressions_queued == 0)
assert(telemetry_counter._impressions_deduped == 0)
assert(telemetry_counter._impressions_dropped == 0)
assert(telemetry_counter._events_dropped == 0)
assert(telemetry_counter._events_queued == 0)
assert(telemetry_counter._auth_rejections == 0)
assert(telemetry_counter._token_refreshes == 0)
assert(telemetry_counter._update_from_sse == {})
await telemetry_counter.record_session_length(20)
assert(await telemetry_counter.get_session_length() == 20)
[await telemetry_counter.record_auth_rejections() for i in range(5)]
auth_rejections = await telemetry_counter.pop_auth_rejections()
assert(telemetry_counter._auth_rejections == 0)
assert(auth_rejections == 5)
[await telemetry_counter.record_token_refreshes() for i in range(3)]
token_refreshes = await telemetry_counter.pop_token_refreshes()
assert(telemetry_counter._token_refreshes == 0)
assert(token_refreshes == 3)
await telemetry_counter.record_impressions_value(ModelTelemetry.CounterConstants.IMPRESSIONS_QUEUED, 10)
assert(telemetry_counter._impressions_queued == 10)
await telemetry_counter.record_impressions_value(ModelTelemetry.CounterConstants.IMPRESSIONS_DEDUPED, 14)
assert(telemetry_counter._impressions_deduped == 14)
await telemetry_counter.record_impressions_value(ModelTelemetry.CounterConstants.IMPRESSIONS_DROPPED, 2)
assert(telemetry_counter._impressions_dropped == 2)
await telemetry_counter.record_events_value(ModelTelemetry.CounterConstants.EVENTS_QUEUED, 30)
assert(telemetry_counter._events_queued == 30)
await telemetry_counter.record_events_value(ModelTelemetry.CounterConstants.EVENTS_DROPPED, 1)
assert(telemetry_counter._events_dropped == 1)
await telemetry_counter.record_update_from_sse(UpdateFromSSE.SPLIT_UPDATE)
assert(telemetry_counter._update_from_sse[UpdateFromSSE.SPLIT_UPDATE.value] == 1)
updates = await telemetry_counter.pop_update_from_sse(UpdateFromSSE.SPLIT_UPDATE)
assert(telemetry_counter._update_from_sse[UpdateFromSSE.SPLIT_UPDATE.value] == 0)
assert(updates == 1)
@pytest.mark.asyncio
async def test_streaming_events(self, mocker):
streaming_events = await StreamingEventsAsync.create()
await streaming_events.record_streaming_event((ModelTelemetry.StreamingEventTypes.CONNECTION_ESTABLISHED, 'split', 1234))
await streaming_events.record_streaming_event((ModelTelemetry.StreamingEventTypes.STREAMING_STATUS, 'split', 1234))
events = await streaming_events.pop_streaming_events()
assert(streaming_events._streaming_events == [])
assert(events == {'streamingEvents': [{'e': ModelTelemetry.StreamingEventTypes.CONNECTION_ESTABLISHED.value, 'd': 'split', 't': 1234},
{'e': ModelTelemetry.StreamingEventTypes.STREAMING_STATUS.value, 'd': 'split', 't': 1234}]})
@pytest.mark.asyncio
async def test_telemetry_config(self):
telemetry_config = await TelemetryConfigAsync.create()
config = {'operationMode': 'standalone',
'streamingEnabled': True,
'impressionsQueueSize': 100,
'eventsQueueSize': 200,
'impressionsMode': 'DEBUG',''
'impressionListener': None,
'featuresRefreshRate': 30,
'segmentsRefreshRate': 30,
'impressionsRefreshRate': 60,
'eventsPushRate': 60,
'metricsRefreshRate': 10,
'storageType': None,
'flagSetsFilter': None
}
await telemetry_config.record_config(config, {}, 5, 2)
assert(await telemetry_config.get_stats() == {'oM': 0,
'sT': telemetry_config._get_storage_type(config['operationMode'], config['storageType']),
'sE': config['streamingEnabled'],
'rR': {'sp': 30, 'se': 30, 'im': 60, 'ev': 60, 'te': 10},
'uO': {'s': False, 'e': False, 'a': False, 'st': False, 't': False},
'iQ': config['impressionsQueueSize'],
'eQ': config['eventsQueueSize'],
'iM': telemetry_config._get_impressions_mode(config['impressionsMode']),
'iL': True if config['impressionListener'] is not None else False,
'hp': telemetry_config._check_if_proxy_detected(),
'tR': 0,
'nR': 0,
'bT': 0,
'aF': 0,
'rF': 0,
'fsT': 5,
'fsI': 2}
)
await telemetry_config.record_ready_time(10)
assert(telemetry_config._time_until_ready == 10)
[await telemetry_config.record_bur_time_out() for i in range(2)]
assert(await telemetry_config.get_bur_time_outs() == 2)
[await telemetry_config.record_not_ready_usage() for i in range(5)]
assert(await telemetry_config.get_non_ready_usage() == 5)