|
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 events_sync |
7 | 9 | from splitio.storage import EventStorage |
8 | 10 | from splitio.models.events import Event |
9 | 11 | from splitio.api.events import EventsAPI |
10 | | -from splitio.sync.event import EventSynchronizer |
| 12 | +from splitio.sync.event import EventSynchronizer, EventSynchronizerAsync |
| 13 | +from splitio.optional.loaders import asyncio |
11 | 14 |
|
12 | 15 |
|
13 | 16 | class EventsSyncTests(object): |
@@ -40,3 +43,47 @@ def test_normal_operation(self, mocker): |
40 | 43 | stop_event.wait(5) |
41 | 44 | assert stop_event.is_set() |
42 | 45 | assert len(api.flush_events.mock_calls) > calls_now |
| 46 | + |
| 47 | + |
| 48 | +class EventsSyncAsyncTests(object): |
| 49 | + """Impressions Syncrhonization task async test cases.""" |
| 50 | + |
| 51 | + @pytest.mark.asyncio |
| 52 | + async def test_normal_operation(self, mocker): |
| 53 | + """Test that the task works properly under normal circumstances.""" |
| 54 | + self.events = [ |
| 55 | + Event('key1', 'user', 'purchase', 5.3, 123456, None), |
| 56 | + Event('key2', 'user', 'purchase', 5.3, 123456, None), |
| 57 | + Event('key3', 'user', 'purchase', 5.3, 123456, None), |
| 58 | + Event('key4', 'user', 'purchase', 5.3, 123456, None), |
| 59 | + Event('key5', 'user', 'purchase', 5.3, 123456, None), |
| 60 | + ] |
| 61 | + storage = mocker.Mock(spec=EventStorage) |
| 62 | + self.called = False |
| 63 | + async def pop_many(*args): |
| 64 | + self.called = True |
| 65 | + return self.events |
| 66 | + storage.pop_many = pop_many |
| 67 | + |
| 68 | + api = mocker.Mock(spec=EventsAPI) |
| 69 | + self.flushed_events = None |
| 70 | + self.count = 0 |
| 71 | + async def flush_events(events): |
| 72 | + self.count += 1 |
| 73 | + self.flushed_events = events |
| 74 | + return HttpResponse(200, '', {}) |
| 75 | + api.flush_events = flush_events |
| 76 | + |
| 77 | + event_synchronizer = EventSynchronizerAsync(api, storage, 5) |
| 78 | + task = events_sync.EventsSyncTaskAsync(event_synchronizer.synchronize_events, 1) |
| 79 | + task.start() |
| 80 | + await asyncio.sleep(2) |
| 81 | + |
| 82 | + assert task.is_running() |
| 83 | + assert self.called |
| 84 | + assert self.flushed_events == self.events |
| 85 | + |
| 86 | + calls_now = self.count |
| 87 | + await task.stop() |
| 88 | + assert not task.is_running() |
| 89 | + assert self.count > calls_now |
0 commit comments