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
This commit is contained in:
davewiththenicehat 2021-04-19 17:36:42 -04:00
parent 0b7cae600a
commit 3cc14e2e4c
2 changed files with 41 additions and 1 deletions

View file

@ -365,6 +365,26 @@ class TaskHandler(object):
self.save() self.save()
return True 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): def do_task(self, task_id):
""" """
Execute the task (call its callback). Execute the task (call its callback).
@ -394,7 +414,7 @@ class TaskHandler(object):
else: # the task does not exist else: # the task does not exist
return False return False
if d: # it is remotely possible for a task to not have a deferral 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 d.cancel() # cancel the automated callback
else: # this task has no deferral, and should not be called else: # this task has no deferral, and should not be called
return False return False
@ -455,6 +475,7 @@ class Task:
pause(): Pause the callback of a task. pause(): Pause the callback of a task.
unpause(): Process all callbacks made since pause() was called. unpause(): Process all callbacks made since pause() was called.
do_task(): Execute the task (call its callback). do_task(): Execute the task (call its callback).
call(): Call the callback of this task.
remove(): Remove a task without executing it. remove(): Remove a task without executing it.
cancel(): Stop a task from automatically executing. cancel(): Stop a task from automatically executing.
active(): Check if a task is active (has not been called yet). 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) 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): def remove(self):
""" """
Remove a task without executing it. Remove a task without executing it.

View file

@ -333,8 +333,14 @@ class TestDelay(EvenniaTest):
self.char1.ndb.dummy_var = False self.char1.ndb.dummy_var = False
# test a persistent deferral, that completes after delay time # test a persistent deferral, that completes after delay time
t = utils.delay(timedelay, dummy_func, self.char1.dbref, persistent=True) 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(_TASK_HANDLER.active(t.get_id())) # test Task.get_id
self.assertTrue(t.active()) 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 _TASK_HANDLER.clock.advance(timedelay) # make time pass
self.assertTrue(t.called) # test Task.called property self.assertTrue(t.called) # test Task.called property
self.assertEqual(self.char1.ndb.dummy_var, 'dummy_func ran') self.assertEqual(self.char1.ndb.dummy_var, 'dummy_func ran')