Skip to content

Commit eff0043

Browse files
committed
[IMP] queue_job: use state constant in lock function
1 parent 886ee8f commit eff0043

File tree

5 files changed

+56
-19
lines changed

5 files changed

+56
-19
lines changed

queue_job/job.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,11 @@ def lock(self) -> bool:
261261
queue_job
262262
WHERE
263263
uuid = %s
264-
AND state='started'
264+
AND state = %s
265265
)
266266
FOR UPDATE SKIP LOCKED;
267267
""",
268-
[self.uuid],
268+
[self.uuid, STARTED],
269269
)
270270

271271
# 1 job should be locked

test_queue_job/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from . import test_acquire_job
12
from . import test_autovacuum
23
from . import test_delayable
34
from . import test_dependencies

test_queue_job/tests/common.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,13 @@ def _create_job(self):
2020
stored = Job.db_records_from_uuids(self.env, [test_job.uuid])
2121
self.assertEqual(len(stored), 1)
2222
return stored
23+
24+
def _get_demo_job(self, uuid):
25+
# job created during load of demo data
26+
job = self.env["queue.job"].search([("uuid", "=", uuid)], limit=1)
27+
self.assertTrue(
28+
job,
29+
f"Demo data queue job {uuid!r} should be loaded in order "
30+
"to make this test work",
31+
)
32+
return job
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2026 ACSONE SA/NV
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3+
from unittest import mock
4+
5+
from odoo.tests import tagged
6+
7+
from odoo.addons.queue_job.controllers.main import RunJobController
8+
9+
from .common import JobCommonCase
10+
11+
12+
@tagged("post_install", "-at_install")
13+
class TestRequeueDeadJob(JobCommonCase):
14+
def test_acquire_enqueued_job(self):
15+
job_record = self._get_demo_job(uuid="test_enqueued_job")
16+
self.assertFalse(
17+
self.env["queue.job.lock"].search(
18+
[("queue_job_id", "=", job_record.id)],
19+
),
20+
"A job lock record should not exist at this point",
21+
)
22+
with mock.patch.object(
23+
self.env.cr, "commit", mock.Mock(side_effect=self.env.flush_all)
24+
) as mock_commit:
25+
job = RunJobController._acquire_job(self.env, job_uuid="test_enqueued_job")
26+
mock_commit.assert_called_once()
27+
self.assertIsNotNone(job)
28+
self.assertEqual(job.uuid, "test_enqueued_job")
29+
self.assertEqual(job.state, "started")
30+
self.assertTrue(
31+
self.env["queue.job.lock"].search(
32+
[("queue_job_id", "=", job_record.id)]
33+
),
34+
"A job lock record should exist at this point",
35+
)
36+
37+
def test_acquire_started_job(self):
38+
with mock.patch.object(
39+
self.env.cr, "commit", mock.Mock(side_effect=self.env.flush_all)
40+
) as mock_commit:
41+
job = RunJobController._acquire_job(self.env, "test_started_job")
42+
mock_commit.assert_not_called()
43+
self.assertIsNone(job)

test_queue_job/tests/test_requeue_dead_job.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,6 @@
1313

1414
@tagged("post_install", "-at_install")
1515
class TestRequeueDeadJob(JobCommonCase):
16-
def _get_demo_job(self, uuid):
17-
# job created during load of demo data
18-
job = self.env["queue.job"].search(
19-
[
20-
("uuid", "=", uuid),
21-
],
22-
limit=1,
23-
)
24-
25-
self.assertTrue(
26-
job,
27-
f"Demo data queue job {uuid} should be loaded in order"
28-
" to make this tests work",
29-
)
30-
31-
return job
32-
3316
def get_locks(self, uuid, cr=None):
3417
"""
3518
Retrieve lock rows

0 commit comments

Comments
 (0)