From 3cc14e2e4ccfb173d644541c349ec3cba298df9c Mon Sep 17 00:00:00 2001 From: davewiththenicehat <54369722+davewiththenicehat@users.noreply.github.com> Date: Mon, 19 Apr 2021 17:36:42 -0400 Subject: [PATCH] task handler call_task, Task.call methods created task handler call_task, Task.call methods created Added unit tests for these methods. All evennia unit tests pass --- evennia/scripts/taskhandler.py | 36 ++++++++++++++++++++++++++++++- evennia/utils/tests/test_utils.py | 6 ++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/evennia/scripts/taskhandler.py b/evennia/scripts/taskhandler.py index c9233adbd8..c0905a5e09 100644 --- a/evennia/scripts/taskhandler.py +++ b/evennia/scripts/taskhandler.py @@ -365,6 +365,26 @@ class TaskHandler(object): self.save() return True + def call_task(self, task_id): + """ + Call the callback of a task. + Leave the task unaffected otherwise. + This does not use the task's deferred instance. + The only requirement is that the task exist in task handler. + + Args: + task_id (int): an existing task ID. + + Returns: + False (bool): if the task does not exist in task handler. + ?: The return of the task's callback. + """ + if task_id in self.tasks: + date, callback, args, kwargs, persistent, d = self.tasks.get(task_id) + else: # the task does not exist + return False + return callback(*args, **kwargs) + def do_task(self, task_id): """ Execute the task (call its callback). @@ -394,7 +414,7 @@ class TaskHandler(object): else: # the task does not exist return False if d: # it is remotely possible for a task to not have a deferral - if not d.called: # the task has not been called yet + if not d.called: # the task's deferred has not been called yet d.cancel() # cancel the automated callback else: # this task has no deferral, and should not be called return False @@ -455,6 +475,7 @@ class Task: pause(): Pause the callback of a task. unpause(): Process all callbacks made since pause() was called. do_task(): Execute the task (call its callback). + call(): Call the callback of this task. remove(): Remove a task without executing it. cancel(): Stop a task from automatically executing. active(): Check if a task is active (has not been called yet). @@ -530,6 +551,19 @@ class Task: """ return TASK_HANDLER.do_task(self.task_id) + def call(self): + """ + Call the callback of this task. + Leave the task unaffected otherwise. + This does not use the task's deferred instance. + The only requirement is that the task exist in task handler. + + Returns: + False (bool): if the task does not exist in task handler. + ?: The return of the task's callback. + """ + return TASK_HANDLER.call_task(self.task_id) + def remove(self): """ Remove a task without executing it. diff --git a/evennia/utils/tests/test_utils.py b/evennia/utils/tests/test_utils.py index 5c71fe19bf..fa67bc768f 100644 --- a/evennia/utils/tests/test_utils.py +++ b/evennia/utils/tests/test_utils.py @@ -333,8 +333,14 @@ class TestDelay(EvenniaTest): self.char1.ndb.dummy_var = False # test a persistent deferral, that completes after delay time t = utils.delay(timedelay, dummy_func, self.char1.dbref, persistent=True) + # call the task early to test Task.call and TaskHandler.call_task + result = t.call() + self.assertTrue(result) + del result + self.assertEqual(self.char1.ndb.dummy_var, 'dummy_func ran') self.assertTrue(_TASK_HANDLER.active(t.get_id())) # test Task.get_id self.assertTrue(t.active()) + self.char1.ndb.dummy_var = False # Set variable to continue completion after delay time test. _TASK_HANDLER.clock.advance(timedelay) # make time pass self.assertTrue(t.called) # test Task.called property self.assertEqual(self.char1.ndb.dummy_var, 'dummy_func ran')