-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_task.py
More file actions
42 lines (33 loc) · 1.17 KB
/
test_task.py
File metadata and controls
42 lines (33 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import unittest
import time
import datetime
import tornado.ioloop
from tasks.task import TASKS
from tasks import task, apply_async
class TestUtils(unittest.TestCase):
def test_task(self):
@task
def t():
pass
target_id = "%s.%s" % (t.__module__, t.__name__)
self.assertEqual(target_id in TASKS, True)
def test_apply_async(self):
@task
def t1(name, value):
self.assertEqual(name == "hi", True)
self.assertEqual(value == "word", True)
@task
def t2(t):
self.assertEqual(time.time() - t > 3, True)
def callback(_, msg):
self.assertEqual(msg == b'OK', True)
def run_test_task():
loop = tornado.ioloop.IOLoop.current()
try:
apply_async(t1, callback=callback, args=("hi", "word"))
apply_async(t2, callback=callback, delay=3000, args=(time.time(),))
finally:
_loop.add_timeout(datetime.timedelta(seconds=9), loop.stop)
_loop = tornado.ioloop.IOLoop.current()
_loop.add_timeout(datetime.timedelta(seconds=3), run_test_task)
_loop.start()