Repository navigation
Expand file tree
/
Copy pathtest_split_sync.py
More file actions
134 lines (119 loc) · 4.55 KB
/
Copy pathtest_split_sync.py
File metadata and controls
134 lines (119 loc) · 4.55 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
"""Split syncrhonization task test module."""
import threading
import time
from splitio.api import APIException
from splitio.api.commons import FetchOptions
from splitio.tasks import split_sync
from splitio.storage import SplitStorage
from splitio.models.splits import Split
from splitio.sync.split import SplitSynchronizer
class SplitSynchronizationTests(object):
"""Split synchronization task test cases."""
def test_normal_operation(self, mocker):
"""Test the normal operation flow."""
storage = mocker.Mock(spec=SplitStorage)
def change_number_mock():
change_number_mock._calls += 1
if change_number_mock._calls == 1:
return -1
return 123
change_number_mock._calls = 0
storage.get_change_number.side_effect = change_number_mock
class flag_set_filter():
def should_filter():
return False
def intersect(sets):
return True
storage.flag_set_filter = flag_set_filter
storage.flag_set_filter.flag_sets = {}
storage.flag_set_filter.sorted_flag_sets = []
api = mocker.Mock()
splits = [{
'changeNumber': 123,
'trafficTypeName': 'user',
'name': 'some_name',
'trafficAllocation': 100,
'trafficAllocationSeed': 123456,
'seed': 321654,
'status': 'ACTIVE',
'killed': False,
'defaultTreatment': 'off',
'algo': 2,
'conditions': [
{
'partitions': [
{'treatment': 'on', 'size': 50},
{'treatment': 'off', 'size': 50}
],
'contitionType': 'WHITELIST',
'label': 'some_label',
'matcherGroup': {
'matchers': [
{
'matcherType': 'WHITELIST',
'whitelistMatcherData': {
'whitelist': ['k1', 'k2', 'k3']
},
'negate': False,
}
],
'combiner': 'AND'
}
}
]
}]
def get_changes(*args, **kwargs):
get_changes.called += 1
if get_changes.called == 1:
return {
'splits': splits,
'since': -1,
'till': 123
}
else:
return {
'splits': [],
'since': 123,
'till': 123
}
get_changes.called = 0
fetch_options = FetchOptions(True)
api.fetch_splits.side_effect = get_changes
split_synchronizer = SplitSynchronizer(api, storage)
task = split_sync.SplitSynchronizationTask(split_synchronizer.synchronize_splits, 0.5)
task.start()
time.sleep(0.7)
assert task.is_running()
stop_event = threading.Event()
task.stop(stop_event)
stop_event.wait()
assert not task.is_running()
assert api.fetch_splits.mock_calls[0][1][0] == -1
assert api.fetch_splits.mock_calls[0][1][1].cache_control_headers == True
assert api.fetch_splits.mock_calls[1][1][0] == 123
assert api.fetch_splits.mock_calls[1][1][1].cache_control_headers == True
inserted_split = storage.update.mock_calls[0][1][0][0]
assert isinstance(inserted_split, Split)
assert inserted_split.name == 'some_name'
def test_that_errors_dont_stop_task(self, mocker):
"""Test that if fetching splits fails at some_point, the task will continue running."""
storage = mocker.Mock(spec=SplitStorage)
api = mocker.Mock()
def run(x):
run._calls += 1
if run._calls == 1:
return {'splits': [], 'since': -1, 'till': -1}
if run._calls == 2:
return {'splits': [], 'since': -1, 'till': -1}
raise APIException("something broke")
run._calls = 0
api.fetch_splits.side_effect = run
storage.get_change_number.return_value = -1
split_synchronizer = SplitSynchronizer(api, storage)
task = split_sync.SplitSynchronizationTask(split_synchronizer.synchronize_splits, 0.5)
task.start()
time.sleep(0.1)
assert task.is_running()
time.sleep(1)
assert task.is_running()
task.stop()