-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest_unique_keys_sync.py
More file actions
106 lines (89 loc) · 5.53 KB
/
Copy pathtest_unique_keys_sync.py
File metadata and controls
106 lines (89 loc) · 5.53 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
"""Split Worker tests."""
import unittest.mock as mock
import pytest
from splitio.engine.impressions.adapters import InMemorySenderAdapter, InMemorySenderAdapterAsync
from splitio.engine.impressions.unique_keys_tracker import UniqueKeysTracker, UniqueKeysTrackerAsync
from splitio.sync.unique_keys import UniqueKeysSynchronizer, ClearFilterSynchronizer, UniqueKeysSynchronizerAsync, ClearFilterSynchronizerAsync
class UniqueKeysSynchronizerTests(object):
"""Unique keys synchronizer test cases."""
def test_sync_unique_keys_chunks(self, mocker):
total_mtks = 5010 # Use number higher than 5000, which is the default max_bulk_size
unique_keys_tracker = UniqueKeysTracker()
for i in range(0 , total_mtks):
unique_keys_tracker.track('key'+str(i)+'', 'feature1')
sender_adapter = InMemorySenderAdapter(mocker.Mock())
unique_keys_synchronizer = UniqueKeysSynchronizer(sender_adapter, unique_keys_tracker)
cache, cache_size = unique_keys_synchronizer._uniqe_keys_tracker.get_cache_info_and_pop_all()
assert(cache_size > unique_keys_synchronizer._max_bulk_size)
bulks = unique_keys_synchronizer._split_cache_to_bulks(cache)
assert(len(bulks) == int(total_mtks / unique_keys_synchronizer._max_bulk_size) + 1)
for i in range(0 , int(total_mtks / unique_keys_synchronizer._max_bulk_size)):
if i > int(total_mtks / unique_keys_synchronizer._max_bulk_size):
assert(len(bulks[i]['feature1']) == (total_mtks - unique_keys_synchronizer._max_bulk_size))
else:
assert(len(bulks[i]['feature1']) == unique_keys_synchronizer._max_bulk_size)
@mock.patch('splitio.engine.impressions.adapters.InMemorySenderAdapter.record_unique_keys')
def test_sync_unique_keys_send_all(self, mtk_mocker):
mtk_mocker.side_effect = self.mocked_record_unique_keys
total_mtks = 5010 # Use number higher than 5000, which is the default max_bulk_size
unique_keys_tracker = UniqueKeysTracker()
for i in range(0 , total_mtks):
unique_keys_tracker.track('key'+str(i)+'', 'feature1')
sender_adapter = InMemorySenderAdapter(mock.Mock())
unique_keys_synchronizer = UniqueKeysSynchronizer(sender_adapter, unique_keys_tracker)
unique_keys_synchronizer.send_all()
assert(mtk_mocker.call_count == int(total_mtks / unique_keys_synchronizer._max_bulk_size) + 1)
def mocked_record_unique_keys(self, cache):
return mock.Mock()
def test_clear_all_filter(self, mocker):
unique_keys_tracker = UniqueKeysTracker()
total_mtks = 50
for i in range(0 , total_mtks):
unique_keys_tracker.track('key'+str(i)+'', 'feature1')
clear_filter_sync = ClearFilterSynchronizer(unique_keys_tracker)
clear_filter_sync.clear_all()
for i in range(0 , total_mtks):
assert(not unique_keys_tracker._filter.contains('feature1key'+str(i)))
class UniqueKeysSynchronizerAsyncTests(object):
"""Unique keys synchronizer async test cases."""
@pytest.mark.asyncio
async def test_sync_unique_keys_chunks(self, mocker):
total_mtks = 5010 # Use number higher than 5000, which is the default max_bulk_size
unique_keys_tracker = UniqueKeysTrackerAsync()
for i in range(0 , total_mtks):
await unique_keys_tracker.track('key'+str(i)+'', 'feature1')
sender_adapter = InMemorySenderAdapterAsync(mocker.Mock())
unique_keys_synchronizer = UniqueKeysSynchronizerAsync(sender_adapter, unique_keys_tracker)
cache, cache_size = await unique_keys_synchronizer._uniqe_keys_tracker.get_cache_info_and_pop_all()
assert(cache_size > unique_keys_synchronizer._max_bulk_size)
bulks = unique_keys_synchronizer._split_cache_to_bulks(cache)
assert(len(bulks) == int(total_mtks / unique_keys_synchronizer._max_bulk_size) + 1)
for i in range(0 , int(total_mtks / unique_keys_synchronizer._max_bulk_size)):
if i > int(total_mtks / unique_keys_synchronizer._max_bulk_size):
assert(len(bulks[i]['feature1']) == (total_mtks - unique_keys_synchronizer._max_bulk_size))
else:
assert(len(bulks[i]['feature1']) == unique_keys_synchronizer._max_bulk_size)
@pytest.mark.asyncio
async def test_sync_unique_keys_send_all(self):
total_mtks = 5010 # Use number higher than 5000, which is the default max_bulk_size
unique_keys_tracker = UniqueKeysTrackerAsync()
for i in range(0 , total_mtks):
await unique_keys_tracker.track('key'+str(i)+'', 'feature1')
sender_adapter = InMemorySenderAdapterAsync(mock.Mock())
self.call_count = 0
async def record_unique_keys(*args):
self.call_count += 1
sender_adapter.record_unique_keys = record_unique_keys
unique_keys_synchronizer = UniqueKeysSynchronizerAsync(sender_adapter, unique_keys_tracker)
await unique_keys_synchronizer.send_all()
assert(self.call_count == int(total_mtks / unique_keys_synchronizer._max_bulk_size) + 1)
@pytest.mark.asyncio
async def test_clear_all_filter(self, mocker):
unique_keys_tracker = UniqueKeysTrackerAsync()
total_mtks = 50
for i in range(0 , total_mtks):
await unique_keys_tracker.track('key'+str(i)+'', 'feature1')
clear_filter_sync = ClearFilterSynchronizerAsync(unique_keys_tracker)
await clear_filter_sync.clear_all()
for i in range(0 , total_mtks):
assert(not unique_keys_tracker._filter.contains('feature1key'+str(i)))