|
| 1 | +import threading |
1 | 2 | import time
|
2 | 3 |
|
3 | 4 | from django.test import TestCase
|
@@ -45,16 +46,21 @@ def __eq__(self, other):
|
45 | 46 |
|
46 | 47 |
|
47 | 48 | @job()
|
48 |
| -def long_running_func(x): |
| 49 | +def func_with_param(x): |
49 | 50 | x.run()
|
50 | 51 |
|
51 | 52 |
|
| 53 | +@job(timeout=1) |
| 54 | +def long_running_func(): |
| 55 | + time.sleep(1000) |
| 56 | + |
| 57 | + |
52 | 58 | class JobDecoratorTest(TestCase):
|
53 | 59 | def setUp(self) -> None:
|
54 | 60 | get_queue("default").connection.flushall()
|
55 | 61 |
|
56 | 62 | def test_all_job_methods_registered(self):
|
57 |
| - self.assertEqual(5, len(JOB_METHODS_LIST)) |
| 63 | + self.assertEqual(6, len(JOB_METHODS_LIST)) |
58 | 64 |
|
59 | 65 | def test_job_decorator_no_params(self):
|
60 | 66 | test_job.delay()
|
@@ -104,23 +110,38 @@ def _assert_job_with_func_and_props(self, queue_name, expected_func, expected_re
|
104 | 110 |
|
105 | 111 | def test_job_decorator_bad_queue(self):
|
106 | 112 | with self.assertRaises(settings.QueueNotFoundError):
|
107 |
| - |
108 | 113 | @job("bad-queue")
|
109 | 114 | def test_job_bad_queue():
|
110 |
| - time.sleep(1) |
111 | 115 | return 1 + 1
|
112 | 116 |
|
113 | 117 | def test_job_decorator_delay_with_param(self):
|
114 | 118 | queue_name = "default"
|
115 |
| - long_running_func.delay(MyClass()) |
| 119 | + func_with_param.delay(MyClass()) |
116 | 120 |
|
117 | 121 | worker = create_worker(queue_name, burst=True)
|
118 | 122 | worker.work()
|
119 | 123 |
|
120 | 124 | jobs_list = worker.queues[0].get_all_jobs()
|
121 | 125 | self.assertEqual(1, len(jobs_list))
|
122 | 126 | job = jobs_list[0]
|
123 |
| - self.assertEqual(job.func, long_running_func) |
| 127 | + self.assertEqual(job.func, func_with_param) |
124 | 128 | self.assertEqual(job.kwargs, {})
|
125 | 129 | self.assertEqual(job.status, JobStatus.FINISHED)
|
126 | 130 | self.assertEqual(job.args, (MyClass(),))
|
| 131 | + |
| 132 | + def test_job_decorator_delay_with_param_worker_thread(self): |
| 133 | + queue_name = "default" |
| 134 | + |
| 135 | + long_running_func.delay() |
| 136 | + |
| 137 | + worker = create_worker(queue_name, burst=True) |
| 138 | + t = threading.Thread(target=worker.work) |
| 139 | + t.start() |
| 140 | + t.join() |
| 141 | + |
| 142 | + jobs_list = get_queue(queue_name).get_all_jobs() |
| 143 | + self.assertEqual(1, len(jobs_list)) |
| 144 | + j = jobs_list[0] |
| 145 | + self.assertEqual(j.func, long_running_func) |
| 146 | + self.assertEqual(j.kwargs, {}) |
| 147 | + self.assertEqual(j.status, JobStatus.FAILED) |
0 commit comments