Skip to content

Commit 5ac1f2c

Browse files
authored
Merge pull request #275 from splitio/feature/mtk-counter-fix
Feature/mtk counter fix
2 parents c337ce4 + 8ec39f8 commit 5ac1f2c

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

splitio/engine/impressions/strategies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,6 @@ def process_impressions(self, impressions):
9999
:rtype: list[tuple[splitio.models.impression.Impression, dict]]
100100
"""
101101
imps = [(self._observer.test_and_set(imp), attrs) for imp, attrs in impressions]
102-
self._counter.track([imp for imp, _ in imps])
102+
self._counter.track([imp for imp, _ in imps if imp.previous_time != None])
103103
this_hour = truncate_time(util.utctime_ms())
104104
return [i for i, _ in imps if i.previous_time is None or i.previous_time < this_hour], imps

tests/engine/test_impressions.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,6 @@ def test_standalone_optimized(self, mocker):
113113
assert imps == [Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now-3),
114114
Impression('k1', 'f2', 'on', 'l1', 123, None, utc_now-3)]
115115

116-
assert [Counter.CountPerFeature(k.feature, k.timeframe, v)
117-
for (k, v) in manager._strategy._counter._data.items()] == [
118-
Counter.CountPerFeature('f1', truncate_time(utc_now-3), 1),
119-
Counter.CountPerFeature('f2', truncate_time(utc_now-3), 1)]
120-
121116
# Tracking the same impression a ms later should be empty
122117
imps = manager.process_impressions([
123118
(Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now-2), None)
@@ -144,14 +139,26 @@ def test_standalone_optimized(self, mocker):
144139
Impression('k2', 'f1', 'on', 'l1', 123, None, utc_now-2, old_utc-1)]
145140

146141
assert len(manager._strategy._observer._cache._data) == 3 # distinct impressions seen
147-
assert len(manager._strategy._counter._data) == 3 # 2 distinct features. 1 seen in 2 different timeframes
142+
assert len(manager._strategy._counter._data) == 2 # 2 distinct features. 1 seen in 2 different timeframes
148143

149144
assert set(manager._strategy._counter.pop_all()) == set([
150-
Counter.CountPerFeature('f1', truncate_time(old_utc), 3),
151-
Counter.CountPerFeature('f2', truncate_time(old_utc), 1),
145+
Counter.CountPerFeature('f1', truncate_time(old_utc), 1),
152146
Counter.CountPerFeature('f1', truncate_time(utc_now), 2)
153147
])
154148

149+
# Test counting only from the second impression
150+
imps = manager.process_impressions([
151+
(Impression('k3', 'f3', 'on', 'l1', 123, None, utc_now-1), None)
152+
])
153+
assert set(manager._strategy._counter.pop_all()) == set([])
154+
155+
imps = manager.process_impressions([
156+
(Impression('k3', 'f3', 'on', 'l1', 123, None, utc_now-1), None)
157+
])
158+
assert set(manager._strategy._counter.pop_all()) == set([
159+
Counter.CountPerFeature('f3', truncate_time(utc_now), 1)
160+
])
161+
155162
def test_standalone_debug(self, mocker):
156163
"""Test impressions manager in debug mode with sdk in standalone mode."""
157164

@@ -291,10 +298,6 @@ def test_standalone_optimized_listener(self, mocker):
291298
])
292299
assert imps == [Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now-3),
293300
Impression('k1', 'f2', 'on', 'l1', 123, None, utc_now-3)]
294-
assert [Counter.CountPerFeature(k.feature, k.timeframe, v)
295-
for (k, v) in manager._strategy._counter._data.items()] == [
296-
Counter.CountPerFeature('f1', truncate_time(utc_now-3), 1),
297-
Counter.CountPerFeature('f2', truncate_time(utc_now-3), 1)]
298301

299302
# Tracking the same impression a ms later should return empty
300303
imps = manager.process_impressions([
@@ -322,11 +325,10 @@ def test_standalone_optimized_listener(self, mocker):
322325
Impression('k2', 'f1', 'on', 'l1', 123, None, utc_now-2, old_utc-1)]
323326

324327
assert len(manager._strategy._observer._cache._data) == 3 # distinct impressions seen
325-
assert len(manager._strategy._counter._data) == 3 # 2 distinct features. 1 seen in 2 different timeframes
328+
assert len(manager._strategy._counter._data) == 2 # 2 distinct features. 1 seen in 2 different timeframes
326329

327330
assert set(manager._strategy._counter.pop_all()) == set([
328-
Counter.CountPerFeature('f1', truncate_time(old_utc), 3),
329-
Counter.CountPerFeature('f2', truncate_time(old_utc), 1),
331+
Counter.CountPerFeature('f1', truncate_time(old_utc), 1),
330332
Counter.CountPerFeature('f1', truncate_time(utc_now), 2)
331333
])
332334

@@ -339,6 +341,19 @@ def test_standalone_optimized_listener(self, mocker):
339341
mocker.call(Impression('k2', 'f1', 'on', 'l1', 123, None, utc_now-2, old_utc-1), None)
340342
]
341343

344+
# Test counting only from the second impression
345+
imps = manager.process_impressions([
346+
(Impression('k3', 'f3', 'on', 'l1', 123, None, utc_now-1), None)
347+
])
348+
assert set(manager._strategy._counter.pop_all()) == set([])
349+
350+
imps = manager.process_impressions([
351+
(Impression('k3', 'f3', 'on', 'l1', 123, None, utc_now-1), None)
352+
])
353+
assert set(manager._strategy._counter.pop_all()) == set([
354+
Counter.CountPerFeature('f3', truncate_time(utc_now), 1)
355+
])
356+
342357
def test_standalone_debug_listener(self, mocker):
343358
"""Test impressions manager in optimized mode with sdk in standalone mode."""
344359

0 commit comments

Comments
 (0)