|
8 | 8 | from splitio.api import APIException |
9 | 9 | from splitio.storage import EventStorage |
10 | 10 | from splitio.models.events import Event |
11 | | -from splitio.sync.event import EventSynchronizer |
| 11 | +from splitio.sync.event import EventSynchronizer, EventSynchronizerAsync |
12 | 12 |
|
13 | 13 |
|
14 | 14 | class EventsSynchronizerTests(object): |
@@ -66,3 +66,66 @@ def run(x): |
66 | 66 | event_synchronizer.synchronize_events() |
67 | 67 | assert run._called == 1 |
68 | 68 | assert event_synchronizer._failed.qsize() == 0 |
| 69 | + |
| 70 | + |
| 71 | +class EventsSynchronizerAsyncTests(object): |
| 72 | + """Events synchronizer async test cases.""" |
| 73 | + |
| 74 | + @pytest.mark.asyncio |
| 75 | + async def test_synchronize_events_error(self, mocker): |
| 76 | + storage = mocker.Mock(spec=EventStorage) |
| 77 | + async def pop_many(*args): |
| 78 | + return [ |
| 79 | + Event('key1', 'user', 'purchase', 5.3, 123456, None), |
| 80 | + Event('key2', 'user', 'purchase', 5.3, 123456, None), |
| 81 | + ] |
| 82 | + storage.pop_many = pop_many |
| 83 | + |
| 84 | + api = mocker.Mock() |
| 85 | + async def run(x): |
| 86 | + raise APIException("something broke") |
| 87 | + |
| 88 | + api.flush_events = run |
| 89 | + event_synchronizer = EventSynchronizerAsync(api, storage, 5) |
| 90 | + await event_synchronizer.synchronize_events() |
| 91 | + assert event_synchronizer._failed.qsize() == 2 |
| 92 | + |
| 93 | + @pytest.mark.asyncio |
| 94 | + async def test_synchronize_events_empty(self, mocker): |
| 95 | + storage = mocker.Mock(spec=EventStorage) |
| 96 | + async def pop_many(*args): |
| 97 | + return [] |
| 98 | + storage.pop_many = pop_many |
| 99 | + |
| 100 | + api = mocker.Mock() |
| 101 | + async def run(x): |
| 102 | + run._called += 1 |
| 103 | + |
| 104 | + run._called = 0 |
| 105 | + api.flush_events = run |
| 106 | + event_synchronizer = EventSynchronizerAsync(api, storage, 5) |
| 107 | + await event_synchronizer.synchronize_events() |
| 108 | + assert run._called == 0 |
| 109 | + |
| 110 | + @pytest.mark.asyncio |
| 111 | + async def test_synchronize_impressions(self, mocker): |
| 112 | + storage = mocker.Mock(spec=EventStorage) |
| 113 | + async def pop_many(*args): |
| 114 | + return [ |
| 115 | + Event('key1', 'user', 'purchase', 5.3, 123456, None), |
| 116 | + Event('key2', 'user', 'purchase', 5.3, 123456, None), |
| 117 | + ] |
| 118 | + storage.pop_many = pop_many |
| 119 | + |
| 120 | + api = mocker.Mock() |
| 121 | + async def run(x): |
| 122 | + run._called += 1 |
| 123 | + return HttpResponse(200, '', {}) |
| 124 | + |
| 125 | + api.flush_events.side_effect = run |
| 126 | + run._called = 0 |
| 127 | + |
| 128 | + event_synchronizer = EventSynchronizerAsync(api, storage, 5) |
| 129 | + await event_synchronizer.synchronize_events() |
| 130 | + assert run._called == 1 |
| 131 | + assert event_synchronizer._failed.qsize() == 0 |
0 commit comments