def __init__(self, synchronize_segments, period): """ Clas constructor. :param synchronize_segments: handler for syncing segments :type synchronize_segments: func """ self._task = asynctask.AsyncTask(synchronize_segments, period, on_init=None)
def __init__(self, filename, storage, period, ready_event): """ Class constructor. :param filename: File to parse splits from. :type filename: str :param storage: Split storage :type storage: splitio.storage.SplitStorage :param ready_event: Eevent to set when sync is done. :type ready_event: threading.Event """ self._filename = filename self._ready_event = ready_event self._storage = storage self._period = period self._task = asynctask.AsyncTask(self._update_splits, period, self._on_start)
def test_default_task_flow(self, mocker): """Test the default execution flow of an asynctask.""" main_func = mocker.Mock() on_init = mocker.Mock() on_stop = mocker.Mock() on_stop_event = threading.Event() task = asynctask.AsyncTask(main_func, 0.5, on_init, on_stop) task.start() time.sleep(1) assert task.running() task.stop(on_stop_event) on_stop_event.wait() assert on_stop_event.is_set() assert 0 < len(main_func.mock_calls) <= 2 assert len(on_init.mock_calls) == 1 assert len(on_stop.mock_calls) == 1 assert not task.running()
def test_force_run(self, mocker): """Test that if the on_init callback fails, the task never runs.""" main_func = mocker.Mock() on_init = mocker.Mock() on_stop = mocker.Mock() on_stop_event = threading.Event() task = asynctask.AsyncTask(main_func, 5, on_init, on_stop) task.start() time.sleep(1) assert task.running() task.force_execution() task.force_execution() task.stop(on_stop_event) on_stop_event.wait(1) assert on_stop_event.isSet() assert on_init.mock_calls == [mocker.call()] assert on_stop.mock_calls == [mocker.call()] assert len(main_func.mock_calls) == 2 assert not task.running()
def test_on_stop_failure_ends_gacefully(self, mocker): """Test that if the on_init callback fails, the task never runs.""" def raise_exception(): raise Exception('something') main_func = mocker.Mock() on_init = mocker.Mock() on_stop = mocker.Mock() on_stop.side_effect = raise_exception on_stop_event = threading.Event() task = asynctask.AsyncTask(main_func, 0.1, on_init, on_stop) task.start() time.sleep(1) task.stop(on_stop_event) on_stop_event.wait(1) assert on_stop_event.isSet() assert on_init.mock_calls == [mocker.call()] assert on_stop.mock_calls == [mocker.call()] assert 9 <= len(main_func.mock_calls) <= 10
def __init__(self, segment_api, segment_storage, split_storage, period, event): #pylint: disable=too-many-arguments """ Clas constructor. :param segment_api: API to retrieve segments from backend. :type segment_api: splitio.api.SegmentApi :param segment_storage: Segment storage reference. :type segment_storage: splitio.storage.SegmentStorage :param event: Event to signal when all segments have finished initial sync. :type event: threading.Event """ self._logger = logging.getLogger(self.__class__.__name__) self._worker_pool = workerpool.WorkerPool(20, self._ensure_segment_is_updated) self._task = asynctask.AsyncTask(self._main, period, on_init=self._on_init) self._segment_api = segment_api self._segment_storage = segment_storage self._split_storage = split_storage self._event = event self._pending_initialization = []
def test_on_init_failure_aborts_task(self, mocker): """Test that if the on_init callback fails, the task never runs.""" def raise_exception(): raise Exception('something') main_func = mocker.Mock() on_init = mocker.Mock() on_init.side_effect = raise_exception on_stop = mocker.Mock() on_stop_event = threading.Event() task = asynctask.AsyncTask(main_func, 0.1, on_init, on_stop) task.start() time.sleep(0.5) assert not task.running() # Since on_init fails, task never starts task.stop(on_stop_event) on_stop_event.wait(1) assert on_stop_event.is_set() assert on_init.mock_calls == [mocker.call()] assert on_stop.mock_calls == [mocker.call()] assert main_func.mock_calls == [] assert not task.running()
def test_main_exception_skips_iteration(self, mocker): """Test that an exception in the main func only skips current iteration.""" def raise_exception(): raise Exception('something') main_func = mocker.Mock() main_func.side_effect = raise_exception on_init = mocker.Mock() on_stop = mocker.Mock() on_stop_event = threading.Event() task = asynctask.AsyncTask(main_func, 0.1, on_init, on_stop) task.start() time.sleep(1) assert task.running() task.stop(on_stop_event) on_stop_event.wait() assert on_stop_event.is_set() assert 9 <= len(main_func.mock_calls) <= 10 assert len(on_init.mock_calls) == 1 assert len(on_stop.mock_calls) == 1 assert not task.running()