def test_raise_error_if_acquired(): uid = '11' Lock._acquire(uid, '12', 'a') clear_cache() with pytest.raises(RuntimeError): with Lock(uid, '13'): assert True
def test_lock_released_exception(): uid = '11' with pytest.raises(Exception): with Lock(uid, uid): raise Exception new_lock = Lock._acquire(uid, '12', 'a') assert new_lock.who_is_locking() == '12'
def test_time_sleep_called(msleep): uid = '11' Lock._acquire(uid, '12', 'a') clear_cache() sleep_time = 5 with pytest.raises(RuntimeError): with Lock(uid, '13', 1, waiter=Waiter(sleep_time)): assert True msleep.assert_called_once_with(uid, '13')
def soft_stop(self, ctxt, plan_uid): with Lock(plan_uid, str(get_current_ident()), retries=20, waiter=Waiter(1)): plan = graph.get_graph(plan_uid) for n in plan: if plan.node[n]['status'] in (states.PENDING.name, states.ERROR_RETRY.name): plan.node[n]['status'] = states.SKIPPED.name graph.update_graph(plan)
def next(self, ctxt, plan_uid): with Lock(plan_uid, str(get_current_ident()), retries=20, waiter=Waiter(1)): log.debug('Received *next* event for %s', plan_uid) plan = graph.get_graph(plan_uid) if len(plan) == 0: raise ValueError('Plan {} is empty'.format(plan_uid)) rst = self._next(plan) for task_name in rst: self._do_scheduling(plan, task_name) graph.update_graph(plan) log.debug('Scheduled tasks %r', rst) # process tasks with tasks client return rst
def update_next(self, ctxt, status, errmsg): log.debug('Received update for TASK %s - %s %s', ctxt['task_id'], status, errmsg) plan_uid, task_name = ctxt['task_id'].rsplit(':', 1) with Lock(plan_uid, str(get_current_ident()), retries=20, waiter=Waiter(1)): plan = graph.get_graph(plan_uid) self._do_update(plan, task_name, status, errmsg=errmsg) rst = self._next(plan) for task_name in rst: self._do_scheduling(plan, task_name) graph.update_graph(plan) log.debug('Scheduled tasks %r', rst) return rst
def test_lock_acquired_released(): uid = '11' with Lock(uid, uid, waiter=Waiter(1)): clear_cache() assert Lock._acquire(uid, '12', 'a').who_is_locking() == '11' assert Lock._acquire(uid, '12', 'a').who_is_locking() == '12'