|
2 | 2 |
|
3 | 3 | import threading |
4 | 4 | import time |
| 5 | +import pytest |
| 6 | + |
5 | 7 | from splitio.api.client import HttpResponse |
6 | 8 | from splitio.tasks import impressions_sync |
7 | 9 | from splitio.storage import ImpressionStorage |
8 | 10 | from splitio.models.impressions import Impression |
9 | 11 | from splitio.api.impressions import ImpressionsAPI |
10 | | -from splitio.sync.impression import ImpressionSynchronizer, ImpressionsCountSynchronizer |
| 12 | +from splitio.sync.impression import ImpressionSynchronizer, ImpressionsCountSynchronizer, ImpressionSynchronizerAsync, ImpressionsCountSynchronizerAsync |
11 | 13 | from splitio.engine.impressions.manager import Counter |
| 14 | +from splitio.optional.loaders import asyncio |
12 | 15 |
|
13 | | -class ImpressionsSyncTests(object): |
| 16 | +class ImpressionsSyncTaskTests(object): |
14 | 17 | """Impressions Syncrhonization task test cases.""" |
15 | 18 |
|
16 | 19 | def test_normal_operation(self, mocker): |
@@ -44,7 +47,52 @@ def test_normal_operation(self, mocker): |
44 | 47 | assert len(api.flush_impressions.mock_calls) > calls_now |
45 | 48 |
|
46 | 49 |
|
47 | | -class ImpressionsCountSyncTests(object): |
| 50 | +class ImpressionsSyncTaskAsyncTests(object): |
| 51 | + """Impressions Syncrhonization task test cases.""" |
| 52 | + |
| 53 | + @pytest.mark.asyncio |
| 54 | + async def test_normal_operation(self, mocker): |
| 55 | + """Test that the task works properly under normal circumstances.""" |
| 56 | + storage = mocker.Mock(spec=ImpressionStorage) |
| 57 | + impressions = [ |
| 58 | + Impression('key1', 'split1', 'on', 'l1', 123456, 'b1', 321654), |
| 59 | + Impression('key2', 'split1', 'on', 'l1', 123456, 'b1', 321654), |
| 60 | + Impression('key3', 'split2', 'off', 'l1', 123456, 'b1', 321654), |
| 61 | + Impression('key4', 'split2', 'on', 'l1', 123456, 'b1', 321654), |
| 62 | + Impression('key5', 'split3', 'off', 'l1', 123456, 'b1', 321654) |
| 63 | + ] |
| 64 | + self.pop_called = 0 |
| 65 | + async def pop_many(*args): |
| 66 | + self.pop_called += 1 |
| 67 | + return impressions |
| 68 | + storage.pop_many = pop_many |
| 69 | + |
| 70 | + api = mocker.Mock(spec=ImpressionsAPI) |
| 71 | + self.flushed = None |
| 72 | + self.called = 0 |
| 73 | + async def flush_impressions(imps): |
| 74 | + self.called += 1 |
| 75 | + self.flushed = imps |
| 76 | + return HttpResponse(200, '', {}) |
| 77 | + api.flush_impressions = flush_impressions |
| 78 | + |
| 79 | + impression_synchronizer = ImpressionSynchronizerAsync(api, storage, 5) |
| 80 | + task = impressions_sync.ImpressionsSyncTaskAsync( |
| 81 | + impression_synchronizer.synchronize_impressions, |
| 82 | + 1 |
| 83 | + ) |
| 84 | + task.start() |
| 85 | + await asyncio.sleep(2) |
| 86 | + assert task.is_running() |
| 87 | + assert self.pop_called == 1 |
| 88 | + assert self.flushed == impressions |
| 89 | + |
| 90 | + calls_now = self.called |
| 91 | + await task.stop() |
| 92 | + assert self.called > calls_now |
| 93 | + |
| 94 | + |
| 95 | +class ImpressionsCountSyncTaskTests(object): |
48 | 96 | """Impressions Syncrhonization task test cases.""" |
49 | 97 |
|
50 | 98 | def test_normal_operation(self, mocker): |
@@ -77,3 +125,48 @@ def test_normal_operation(self, mocker): |
77 | 125 | stop_event.wait(5) |
78 | 126 | assert stop_event.is_set() |
79 | 127 | assert len(api.flush_counters.mock_calls) > calls_now |
| 128 | + |
| 129 | + |
| 130 | +class ImpressionsCountSyncTaskAsyncTests(object): |
| 131 | + """Impressions Syncrhonization task test cases.""" |
| 132 | + |
| 133 | + @pytest.mark.asyncio |
| 134 | + async def test_normal_operation(self, mocker): |
| 135 | + """Test that the task works properly under normal circumstances.""" |
| 136 | + counter = mocker.Mock(spec=Counter) |
| 137 | + counters = [ |
| 138 | + Counter.CountPerFeature('f1', 123, 2), |
| 139 | + Counter.CountPerFeature('f2', 123, 123), |
| 140 | + Counter.CountPerFeature('f1', 456, 111), |
| 141 | + Counter.CountPerFeature('f2', 456, 222) |
| 142 | + ] |
| 143 | + self._pop_called = 0 |
| 144 | + async def pop_all(): |
| 145 | + self._pop_called += 1 |
| 146 | + return counters |
| 147 | + counter.pop_all = pop_all |
| 148 | + |
| 149 | + api = mocker.Mock(spec=ImpressionsAPI) |
| 150 | + self.flushed = None |
| 151 | + self.called = 0 |
| 152 | + async def flush_counters(imps): |
| 153 | + self.called += 1 |
| 154 | + self.flushed = imps |
| 155 | + return HttpResponse(200, '', {}) |
| 156 | + api.flush_counters = flush_counters |
| 157 | + |
| 158 | + impressions_sync.ImpressionsCountSyncTaskAsync._PERIOD = 1 |
| 159 | + impression_synchronizer = ImpressionsCountSynchronizerAsync(api, counter) |
| 160 | + task = impressions_sync.ImpressionsCountSyncTaskAsync( |
| 161 | + impression_synchronizer.synchronize_counters |
| 162 | + ) |
| 163 | + task.start() |
| 164 | + await asyncio.sleep(2) |
| 165 | + assert task.is_running() |
| 166 | + |
| 167 | + assert self._pop_called == 1 |
| 168 | + assert self.flushed == counters |
| 169 | + |
| 170 | + calls_now = self.called |
| 171 | + await task.stop() |
| 172 | + assert self.called > calls_now |
0 commit comments