def test_task_prepare(): result = object() c = Callable(result=result) t = Task(id="task-id") t.prepare(c) status = t.getStatus() assert status == { "result": result, "state": {"code": 0, "message": "OK"}, "task": {"id": "task-id", "state": "finished"} } assert c.is_finished()
def async_task(called, task_id): task = Task(id=task_id) t = concurrent.thread(task.prepare, args=(called, )) t.start() try: called.wait_until_running() yield task finally: called.finish() t.join(timeout=1) oop.stop()
def test_task_queued(): t = Task(id="task-id") tm = TaskManager() result = object() c = Callable(result=result) # Simulate async storage APIs scheduling another function with the # task manager. def async_call(): tm.scheduleJob(t, c) # Schedule job t.prepare(async_call) # Check that task is queued assert tm._task is t status = t.getStatus() assert status == { "result": "", "state": { "code": 0, "message": "Task is initializing" }, "task": { "id": "task-id", "state": "queued" } } # Check that job callable was not called yet assert not c.is_finished() # Invoke the job run t.commit() # Check task final status status = t.getStatus() assert status == { "result": result, "state": { "code": 0, "message": "1 jobs completed successfully" }, "task": { "id": "task-id", "state": "finished" } } # Check that job callable was called assert c.is_finished()
def loadDumpedTasks(self, store): if not os.path.exists(store): self.log.debug("task dump path %s does not exist.", store) return # taskID is the root part of each (root.ext) entry in the dump task dir tasksIDs = set(os.path.splitext(tid)[0] for tid in os.listdir(store)) for taskID in tasksIDs: self.log.debug("Loading dumped task %s", taskID) try: t = Task.loadTask(store, taskID) t.setPersistence(store, str(t.persistPolicy), str(t.cleanPolicy)) self._unqueuedTasks.append(t) except Exception: self.log.error("taskManager: Skipping directory: %s", taskID, exc_info=True) continue
def test_task_save_load(tmpdir, add_recovery): # Run async task c = Callable(hang_timeout=WAIT_TIMEOUT) with async_task(c, "task-id") as orig_task: orig_task.setRecoveryPolicy("auto") # Set persistency for task store = str(tmpdir) orig_task.setPersistence(store) # Add recovery to task r = add_recovery(orig_task, "fakerecovery", ["arg1", "arg2"]) # Simulate original task being interrupted by improper shutdown orig_task.store = None # Load task from storage loaded_task = Task.loadTask(store, "task-id") # Recover loaded task assert r.args is None loaded_task.recover() # Wait for recovery to finish assert loaded_task.wait(timeout=WAIT_TIMEOUT), "Task failed to finish" # Assert recovery procedure was executed assert r.args == ("arg1", "arg2") # Check task final status status = loaded_task.getStatus() # TODO: Figure why task message still indicates initialization assert status == { "result": "", "state": { "code": 0, "message": "Task is initializing" }, "task": { "id": "task-id", "state": "recovered" } }
def fake_task(monkeypatch): """ Create fake task, expected in various places in the code. In the real code a task is created for every HSM public call by the dispatcher. """ monkeypatch.setattr(threadlocal.vars, 'task', Task("fake-task-id"))
def _getTask(self, taskID): Task.validateID(taskID) t = self._tasks.get(taskID, None) if t is None: raise se.UnknownTask(taskID) return t
def __getTask(self, taskID): Task.validateID(taskID) t = self._tasks.get(taskID, None) if t is None: raise se.UnknownTask(taskID) return t
def test_recovery_list(): # Check push pop single recovery t = Task(id="task-id") recovery1 = Recovery("name_1", "storage_1", "task_1", "func_1", []) t.pushRecovery(recovery1) assert t.popRecovery() is recovery1 # Check replace recovery by another t.pushRecovery(recovery1) recovery2 = Recovery("name_2", "storage_2", "task_2", "func_2", []) t.replaceRecoveries(recovery2) assert t.popRecovery() is recovery2 assert t.popRecovery() is None # Check replace recovery over an empty list t.replaceRecoveries(recovery1) assert t.popRecovery() is recovery1 # Check clearing recoveries t.pushRecovery(recovery1) t.pushRecovery(recovery2) t.clearRecoveries() assert t.popRecovery() is None