Repository navigation
Expand file tree
/
Copy pathtest_events_sync.py
More file actions
89 lines (76 loc) · 3.29 KB
/
Copy pathtest_events_sync.py
File metadata and controls
89 lines (76 loc) · 3.29 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
"""Impressions synchronization task test module."""
import threading
import time
import pytest
from splitio.api.client import HttpResponse
from splitio.tasks import events_sync
from splitio.storage import EventStorage
from splitio.models.events import Event
from splitio.api.events import EventsAPI
from splitio.sync.event import EventSynchronizer, EventSynchronizerAsync
from splitio.optional.loaders import asyncio
class EventsSyncTests(object):
"""Impressions Syncrhonization task test cases."""
def test_normal_operation(self, mocker):
"""Test that the task works properly under normal circumstances."""
storage = mocker.Mock(spec=EventStorage)
events = [
Event('key1', 'user', 'purchase', 5.3, 123456, None),
Event('key2', 'user', 'purchase', 5.3, 123456, None),
Event('key3', 'user', 'purchase', 5.3, 123456, None),
Event('key4', 'user', 'purchase', 5.3, 123456, None),
Event('key5', 'user', 'purchase', 5.3, 123456, None),
]
storage.pop_many.return_value = events
api = mocker.Mock(spec=EventsAPI)
api.flush_events.return_value = HttpResponse(200, '', {})
event_synchronizer = EventSynchronizer(api, storage, 5)
task = events_sync.EventsSyncTask(event_synchronizer.synchronize_events, 1)
task.start()
time.sleep(2)
assert task.is_running()
assert storage.pop_many.mock_calls[0] == mocker.call(5)
assert api.flush_events.mock_calls[0] == mocker.call(events)
stop_event = threading.Event()
calls_now = len(api.flush_events.mock_calls)
task.stop(stop_event)
stop_event.wait(5)
assert stop_event.is_set()
assert len(api.flush_events.mock_calls) > calls_now
class EventsSyncAsyncTests(object):
"""Impressions Syncrhonization task async test cases."""
@pytest.mark.asyncio
async def test_normal_operation(self, mocker):
"""Test that the task works properly under normal circumstances."""
self.events = [
Event('key1', 'user', 'purchase', 5.3, 123456, None),
Event('key2', 'user', 'purchase', 5.3, 123456, None),
Event('key3', 'user', 'purchase', 5.3, 123456, None),
Event('key4', 'user', 'purchase', 5.3, 123456, None),
Event('key5', 'user', 'purchase', 5.3, 123456, None),
]
storage = mocker.Mock(spec=EventStorage)
self.called = False
async def pop_many(*args):
self.called = True
return self.events
storage.pop_many = pop_many
api = mocker.Mock(spec=EventsAPI)
self.flushed_events = None
self.count = 0
async def flush_events(events):
self.count += 1
self.flushed_events = events
return HttpResponse(200, '', {})
api.flush_events = flush_events
event_synchronizer = EventSynchronizerAsync(api, storage, 5)
task = events_sync.EventsSyncTaskAsync(event_synchronizer.synchronize_events, 1)
task.start()
await asyncio.sleep(2)
assert task.is_running()
assert self.called
assert self.flushed_events == self.events
calls_now = self.count
await task.stop()
assert not task.is_running()
assert self.count > calls_now