taskhandler verified do_task causes errors if called manually (early)
Any usage of taskhandler's method outside of the deferred instance calling it results in errors.
Referencing: It's easier to access these tasks (should it be necessary) using
`evennia.scripts.taskhandler.TASK_HANDLER`
I can see it is intended to be used this way.
More importantly usage of the global reactor would require usage of reactorbuilder API which is recomended for building reactors only.
Commiting notes before switching to twisted's documented methods for making and testing deferrals.
In short I need to get an instance of reactor's callLater. Creating and working with that call later will allow me to test taskhandler and make it function as intended.
Usage of utils.delay will not change.
This commit is contained in:
parent
f445cfb355
commit
f62ff2015f
3 changed files with 78 additions and 6 deletions
|
|
@ -6,13 +6,15 @@ TODO: Not nearly all utilities are covered yet.
|
|||
"""
|
||||
|
||||
import os.path
|
||||
|
||||
import mock
|
||||
|
||||
from django.test import TestCase
|
||||
from datetime import datetime
|
||||
from twisted.internet import task, reactor
|
||||
|
||||
from evennia.utils.ansi import ANSIString
|
||||
from evennia.utils import utils
|
||||
from evennia.utils.test_resources import EvenniaTest
|
||||
|
||||
|
||||
class TestIsIter(TestCase):
|
||||
|
|
@ -292,3 +294,41 @@ class LatinifyTest(TestCase):
|
|||
byte_str = utils.to_bytes(self.example_str)
|
||||
result = utils.latinify(byte_str)
|
||||
self.assertEqual(result, self.expected_output)
|
||||
|
||||
|
||||
_TASK_HANDLER = None
|
||||
|
||||
|
||||
def dummy_func(obj):
|
||||
"""
|
||||
Used in TestDelay.
|
||||
|
||||
A function that:
|
||||
can be serialized
|
||||
uses no memory references
|
||||
uses evennia objects
|
||||
"""
|
||||
# get a reference of object
|
||||
from evennia.objects.models import ObjectDB
|
||||
obj = ObjectDB.objects.object_search(obj)
|
||||
obj = obj[0]
|
||||
# make changes to object
|
||||
obj.ndb.dummy_var = 'dummy_func ran'
|
||||
|
||||
|
||||
class TestDelay(EvenniaTest):
|
||||
"""
|
||||
Test utils.delay.
|
||||
"""
|
||||
|
||||
def test_delay(self):
|
||||
# get a reference of TASK_HANDLER
|
||||
global _TASK_HANDLER
|
||||
if _TASK_HANDLER is None:
|
||||
from evennia.scripts.taskhandler import TASK_HANDLER as _TASK_HANDLER
|
||||
self.char1.ndb.dummy_var = False
|
||||
# test a persistent deferral
|
||||
task_id = utils.delay(1, dummy_func, self.char1.dbref, persistent=True)
|
||||
_TASK_HANDLER.do_task(task_id) # run the deferred task
|
||||
self.assertEqual(self.char1.ndb.dummy_var, 'dummy_func ran')
|
||||
self.char1.ndb.dummy_var = False
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue