11"""Telemetry Worker tests."""
22import unittest .mock as mock
3- import json
4- from splitio .sync .telemetry import TelemetrySynchronizer , InMemoryTelemetrySubmitter
5- from splitio .engine .telemetry import TelemetryEvaluationConsumer , TelemetryInitConsumer , TelemetryRuntimeConsumer , TelemetryStorageConsumer
6- from splitio .storage .inmemmory import InMemoryTelemetryStorage , InMemorySegmentStorage , InMemorySplitStorage
3+ import pytest
4+
5+ from splitio .sync .telemetry import TelemetrySynchronizer , TelemetrySynchronizerAsync , InMemoryTelemetrySubmitter , InMemoryTelemetrySubmitterAsync
6+ from splitio .engine .telemetry import TelemetryStorageConsumer , TelemetryStorageConsumerAsync
7+ from splitio .storage .inmemmory import InMemoryTelemetryStorage , InMemoryTelemetryStorageAsync , InMemorySegmentStorage , InMemorySegmentStorageAsync , InMemorySplitStorage , InMemorySplitStorageAsync
78from splitio .models .splits import Split , Status
89from splitio .models .segments import Segment
9- from splitio .models .telemetry import StreamingEvents
10+ from splitio .models .telemetry import StreamingEvents , StreamingEventsAsync
1011from splitio .api .telemetry import TelemetryAPI
1112
1213class TelemetrySynchronizerTests (object ):
@@ -24,6 +25,31 @@ def test_synchronize_stats(self, mocker):
2425 telemetry_synchronizer .synchronize_stats ()
2526 assert (mocker .called )
2627
28+
29+ class TelemetrySynchronizerAsyncTests (object ):
30+ """Telemetry synchronizer async test cases."""
31+
32+ @pytest .mark .asyncio
33+ async def test_synchronize_config (self , mocker ):
34+ telemetry_synchronizer = TelemetrySynchronizerAsync (InMemoryTelemetrySubmitterAsync (mocker .Mock (), mocker .Mock (), mocker .Mock (), mocker .Mock ()))
35+ self .called = False
36+ async def synchronize_config ():
37+ self .called = True
38+ telemetry_synchronizer .synchronize_config = synchronize_config
39+ await telemetry_synchronizer .synchronize_config ()
40+ assert (self .called )
41+
42+ @pytest .mark .asyncio
43+ async def test_synchronize_stats (self , mocker ):
44+ telemetry_synchronizer = TelemetrySynchronizer (InMemoryTelemetrySubmitter (mocker .Mock (), mocker .Mock (), mocker .Mock (), mocker .Mock ()))
45+ self .called = False
46+ async def synchronize_stats ():
47+ self .called = True
48+ telemetry_synchronizer .synchronize_stats = synchronize_stats
49+ await telemetry_synchronizer .synchronize_stats ()
50+ assert (self .called )
51+
52+
2753class TelemetrySubmitterTests (object ):
2854 """Telemetry submitter test cases."""
2955
@@ -136,3 +162,118 @@ def record_stats(*args, **kwargs):
136162 "skC" : 0 ,
137163 "t" : ['tag1' ]
138164 })
165+
166+
167+ class TelemetrySubmitterAsyncTests (object ):
168+ """Telemetry submitter async test cases."""
169+
170+ @pytest .mark .asyncio
171+ async def test_synchronize_telemetry (self , mocker ):
172+ api = mocker .Mock (spec = TelemetryAPI )
173+ telemetry_storage = await InMemoryTelemetryStorageAsync .create ()
174+ telemetry_consumer = TelemetryStorageConsumerAsync (telemetry_storage )
175+ split_storage = InMemorySplitStorageAsync ()
176+ await split_storage .put (Split ('split1' , 1234 , 1 , False , 'user' , Status .ACTIVE , 123 ))
177+ segment_storage = InMemorySegmentStorageAsync ()
178+ await segment_storage .put (Segment ('segment1' , [], 123 ))
179+ telemetry_submitter = InMemoryTelemetrySubmitterAsync (telemetry_consumer , split_storage , segment_storage , api )
180+
181+ telemetry_storage ._counters ._impressions_queued = 100
182+ telemetry_storage ._counters ._impressions_deduped = 30
183+ telemetry_storage ._counters ._impressions_dropped = 0
184+ telemetry_storage ._counters ._events_queued = 20
185+ telemetry_storage ._counters ._events_dropped = 10
186+ telemetry_storage ._counters ._auth_rejections = 1
187+ telemetry_storage ._counters ._token_refreshes = 3
188+ telemetry_storage ._counters ._session_length = 3
189+
190+ telemetry_storage ._method_exceptions ._treatment = 10
191+ telemetry_storage ._method_exceptions ._treatments = 1
192+ telemetry_storage ._method_exceptions ._treatment_with_config = 5
193+ telemetry_storage ._method_exceptions ._treatments_with_config = 1
194+ telemetry_storage ._method_exceptions ._track = 3
195+
196+ telemetry_storage ._last_synchronization ._split = 5
197+ telemetry_storage ._last_synchronization ._segment = 3
198+ telemetry_storage ._last_synchronization ._impression = 10
199+ telemetry_storage ._last_synchronization ._impression_count = 0
200+ telemetry_storage ._last_synchronization ._event = 4
201+ telemetry_storage ._last_synchronization ._telemetry = 0
202+ telemetry_storage ._last_synchronization ._token = 3
203+
204+ telemetry_storage ._http_sync_errors ._split = {'500' : 3 , '501' : 2 }
205+ telemetry_storage ._http_sync_errors ._segment = {'401' : 1 }
206+ telemetry_storage ._http_sync_errors ._impression = {'500' : 1 }
207+ telemetry_storage ._http_sync_errors ._impression_count = {'401' : 5 }
208+ telemetry_storage ._http_sync_errors ._event = {'404' : 10 }
209+ telemetry_storage ._http_sync_errors ._telemetry = {'501' : 3 }
210+ telemetry_storage ._http_sync_errors ._token = {'505' : 11 }
211+
212+ telemetry_storage ._streaming_events = await StreamingEventsAsync .create ()
213+ telemetry_storage ._tags = ['tag1' ]
214+
215+ telemetry_storage ._method_latencies ._treatment = [1 ] + [0 ] * 22
216+ telemetry_storage ._method_latencies ._treatments = [0 ] * 23
217+ telemetry_storage ._method_latencies ._treatment_with_config = [0 ] * 23
218+ telemetry_storage ._method_latencies ._treatments_with_config = [0 ] * 23
219+ telemetry_storage ._method_latencies ._track = [0 ] * 23
220+
221+ telemetry_storage ._http_latencies ._split = [1 ] + [0 ] * 22
222+ telemetry_storage ._http_latencies ._segment = [0 ] * 23
223+ telemetry_storage ._http_latencies ._impression = [0 ] * 23
224+ telemetry_storage ._http_latencies ._impression_count = [0 ] * 23
225+ telemetry_storage ._http_latencies ._event = [0 ] * 23
226+ telemetry_storage ._http_latencies ._telemetry = [0 ] * 23
227+ telemetry_storage ._http_latencies ._token = [0 ] * 23
228+
229+ await telemetry_storage .record_config ({'operationMode' : 'inmemory' ,
230+ 'storageType' : None ,
231+ 'streamingEnabled' : True ,
232+ 'impressionsQueueSize' : 100 ,
233+ 'eventsQueueSize' : 200 ,
234+ 'impressionsMode' : 'DEBUG' ,
235+ 'impressionListener' : None ,
236+ 'featuresRefreshRate' : 30 ,
237+ 'segmentsRefreshRate' : 30 ,
238+ 'impressionsRefreshRate' : 60 ,
239+ 'eventsPushRate' : 60 ,
240+ 'metricsRefreshRate' : 10 ,
241+ 'activeFactoryCount' : 1 ,
242+ 'notReady' : 0 ,
243+ 'timeUntilReady' : 1
244+ }, {}
245+ )
246+ self .formatted_config = ""
247+ async def record_init (* args , ** kwargs ):
248+ self .formatted_config = args [0 ]
249+ api .record_init = record_init
250+
251+ await telemetry_submitter .synchronize_config ()
252+ assert (self .formatted_config == await telemetry_submitter ._telemetry_init_consumer .get_config_stats ())
253+
254+ async def record_stats (* args , ** kwargs ):
255+ self .formatted_stats = args [0 ]
256+ api .record_stats = record_stats
257+
258+ await telemetry_submitter .synchronize_stats ()
259+ assert (self .formatted_stats == {
260+ "iQ" : 100 ,
261+ "iDe" : 30 ,
262+ "iDr" : 0 ,
263+ "eQ" : 20 ,
264+ "eD" : 10 ,
265+ "lS" : {"sp" : 5 , "se" : 3 , "im" : 10 , "ic" : 0 , "ev" : 4 , "te" : 0 , "to" : 3 },
266+ "t" : ["tag1" ],
267+ "hE" : {"sp" : {"500" : 3 , "501" : 2 }, "se" : {"401" : 1 }, "im" : {"500" : 1 }, "ic" : {"401" : 5 }, "ev" : {"404" : 10 }, "te" : {"501" : 3 }, "to" : {"505" : 11 }},
268+ "hL" : {"sp" : [1 ] + [0 ] * 22 , "se" : [0 ] * 23 , "im" : [0 ] * 23 , "ic" : [0 ] * 23 , "ev" : [0 ] * 23 , "te" : [0 ] * 23 , "to" : [0 ] * 23 },
269+ "aR" : 1 ,
270+ "tR" : 3 ,
271+ "sE" : [],
272+ "sL" : 3 ,
273+ "mE" : {"t" : 10 , "ts" : 1 , "tc" : 5 , "tcs" : 1 , "tr" : 3 },
274+ "mL" : {"t" : [1 ] + [0 ] * 22 , "ts" : [0 ] * 23 , "tc" : [0 ] * 23 , "tcs" : [0 ] * 23 , "tr" : [0 ] * 23 },
275+ "spC" : 1 ,
276+ "seC" : 1 ,
277+ "skC" : 0 ,
278+ "t" : ['tag1' ]
279+ })
0 commit comments