|
| 1 | +"""UWSGI Task wrappers test module.""" |
| 2 | +#pylint: disable=no-self-use,protected-access |
| 3 | +from splitio.storage import SplitStorage |
| 4 | +from splitio.tasks.split_sync import SplitSynchronizationTask |
| 5 | +from splitio.tasks.impressions_sync import ImpressionsSyncTask |
| 6 | +from splitio.tasks.events_sync import EventsSyncTask |
| 7 | +from splitio.tasks.telemetry_sync import TelemetrySynchronizationTask |
| 8 | +from splitio.tasks.util.workerpool import WorkerPool |
| 9 | +from splitio.storage.uwsgi import UWSGISplitStorage |
| 10 | +from splitio.tasks.uwsgi_wrappers import uwsgi_update_splits, uwsgi_update_segments, \ |
| 11 | + uwsgi_report_events, uwsgi_report_impressions, uwsgi_report_telemetry |
| 12 | + |
| 13 | + |
| 14 | +class NonCatchableException(BaseException): |
| 15 | + """Exception to be used to stop sync task's infinite loop.""" |
| 16 | + |
| 17 | + pass |
| 18 | + |
| 19 | + |
| 20 | +class TaskWrappersTests(object): |
| 21 | + """Task wrappers task test cases.""" |
| 22 | + |
| 23 | + def test_update_splits(self, mocker): |
| 24 | + """Test split sync task wrapper.""" |
| 25 | + data = {'executions': 0} |
| 26 | + def _update_splits_side_effect(*_, **__): |
| 27 | + data['executions'] += 1 |
| 28 | + if data['executions'] > 1: |
| 29 | + raise NonCatchableException('asd') |
| 30 | + |
| 31 | + stmock = mocker.Mock(spec=SplitSynchronizationTask) |
| 32 | + stmock._update_splits.side_effect = _update_splits_side_effect |
| 33 | + stmock_class = mocker.Mock(spec=SplitSynchronizationTask) |
| 34 | + stmock_class.return_value = stmock |
| 35 | + mocker.patch('splitio.tasks.uwsgi_wrappers.SplitSynchronizationTask', new=stmock_class) |
| 36 | + |
| 37 | + try: |
| 38 | + uwsgi_update_splits({'apikey' : 'asd', 'featuresRefreshRate': 1}) |
| 39 | + except NonCatchableException: |
| 40 | + # Make sure that the task was called before being forced to stop. |
| 41 | + assert data['executions'] > 1 |
| 42 | + assert len(stmock._update_splits.mock_calls) > 1 |
| 43 | + |
| 44 | + def test_update_segments(self, mocker): |
| 45 | + """Test split sync task wrapper.""" |
| 46 | + data = {'executions': 0} |
| 47 | + def _submit_work(*_, **__): |
| 48 | + data['executions'] += 1 |
| 49 | + # we mock 2 segments, so we expect this to be called at least twice before ending. |
| 50 | + if data['executions'] > 2: |
| 51 | + raise NonCatchableException('asd') |
| 52 | + |
| 53 | + wpmock = mocker.Mock(spec=WorkerPool) |
| 54 | + wpmock.submit_work.side_effect = _submit_work |
| 55 | + wpmock_class = mocker.Mock(spec=WorkerPool) |
| 56 | + wpmock_class.return_value = wpmock |
| 57 | + mocker.patch('splitio.tasks.uwsgi_wrappers.workerpool.WorkerPool', new=wpmock_class) |
| 58 | + |
| 59 | + mocked_update_segment = mocker.patch.object(SplitStorage, 'get_segment_names') |
| 60 | + mocked_update_segment.return_value = ['segment1', 'segment2'] |
| 61 | + mocked_split_storage_instance = UWSGISplitStorage(True) |
| 62 | + split_storage_mock = mocker.Mock(spec=UWSGISplitStorage) |
| 63 | + split_storage_mock.return_value = mocked_split_storage_instance |
| 64 | + |
| 65 | + mocker.patch('splitio.tasks.uwsgi_wrappers.UWSGISplitStorage', new=split_storage_mock) |
| 66 | + |
| 67 | + try: |
| 68 | + uwsgi_update_segments({'apikey' : 'asd', 'segmentsRefreshRate': 1}) |
| 69 | + except NonCatchableException: |
| 70 | + # Make sure that the task was called before being forced to stop. |
| 71 | + assert data['executions'] > 2 |
| 72 | + assert len(wpmock.submit_work.mock_calls) > 2 |
| 73 | + |
| 74 | + def test_post_impressions(self, mocker): |
| 75 | + """Test split sync task wrapper.""" |
| 76 | + data = {'executions': 0} |
| 77 | + def _report_impressions_side_effect(*_, **__): |
| 78 | + data['executions'] += 1 |
| 79 | + if data['executions'] > 1: |
| 80 | + raise NonCatchableException('asd') |
| 81 | + |
| 82 | + stmock = mocker.Mock(spec=ImpressionsSyncTask) |
| 83 | + stmock._send_impressions.side_effect = _report_impressions_side_effect |
| 84 | + stmock_class = mocker.Mock(spec=ImpressionsSyncTask) |
| 85 | + stmock_class.return_value = stmock |
| 86 | + mocker.patch('splitio.tasks.uwsgi_wrappers.ImpressionsSyncTask', new=stmock_class) |
| 87 | + try: |
| 88 | + uwsgi_report_impressions({'apikey' : 'asd', 'impressionsRefreshRate': 1}) |
| 89 | + except NonCatchableException: |
| 90 | + # Make sure that the task was called before being forced to stop. |
| 91 | + assert data['executions'] > 1 |
| 92 | + # TODO: Test impressions flushing. |
| 93 | + |
| 94 | + def test_post_events(self, mocker): |
| 95 | + """Test split sync task wrapper.""" |
| 96 | + data = {'executions': 0} |
| 97 | + def _send_events_side_effect(*_, **__): |
| 98 | + data['executions'] += 1 |
| 99 | + if data['executions'] > 1: |
| 100 | + raise NonCatchableException('asd') |
| 101 | + |
| 102 | + stmock = mocker.Mock(spec=EventsSyncTask) |
| 103 | + stmock._send_events.side_effect = _send_events_side_effect |
| 104 | + stmock_class = mocker.Mock(spec=EventsSyncTask) |
| 105 | + stmock_class.return_value = stmock |
| 106 | + mocker.patch('splitio.tasks.uwsgi_wrappers.EventsSyncTask', new=stmock_class) |
| 107 | + try: |
| 108 | + uwsgi_report_events({'apikey' : 'asd', 'eventsRefreshRate': 1}) |
| 109 | + except NonCatchableException: |
| 110 | + # Make sure that the task was called before being forced to stop. |
| 111 | + assert data['executions'] > 1 |
| 112 | + # TODO: Test impressions flushing. |
| 113 | + |
| 114 | + def test_post_telemetry(self, mocker): |
| 115 | + """Test split sync task wrapper.""" |
| 116 | + data = {'executions': 0} |
| 117 | + def _flush_telemetry_side_effect(*_, **__): |
| 118 | + data['executions'] += 1 |
| 119 | + if data['executions'] > 1: |
| 120 | + raise NonCatchableException('asd') |
| 121 | + |
| 122 | + stmock = mocker.Mock(spec=TelemetrySynchronizationTask) |
| 123 | + stmock._flush_telemetry.side_effect = _flush_telemetry_side_effect |
| 124 | + stmock_class = mocker.Mock(spec=TelemetrySynchronizationTask) |
| 125 | + stmock_class.return_value = stmock |
| 126 | + mocker.patch('splitio.tasks.uwsgi_wrappers.TelemetrySynchronizationTask', new=stmock_class) |
| 127 | + try: |
| 128 | + uwsgi_report_telemetry({'apikey' : 'asd', 'metricsRefreshRate': 1}) |
| 129 | + except NonCatchableException: |
| 130 | + # Make sure that the task was called before being forced to stop. |
| 131 | + assert data['executions'] > 1 |
0 commit comments