|
| 1 | +# Copyright 2026 ACSONE SA/NV |
| 2 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 3 | +import logging |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +from odoo.tests import tagged |
| 7 | + |
| 8 | +from odoo.addons.queue_job.controllers.main import RunJobController |
| 9 | + |
| 10 | +from .common import JobCommonCase |
| 11 | + |
| 12 | + |
| 13 | +@tagged("post_install", "-at_install") |
| 14 | +class TestRequeueDeadJob(JobCommonCase): |
| 15 | + def test_acquire_enqueued_job(self): |
| 16 | + job_record = self._get_demo_job(uuid="test_enqueued_job") |
| 17 | + self.assertFalse( |
| 18 | + self.env["queue.job.lock"].search( |
| 19 | + [("queue_job_id", "=", job_record.id)], |
| 20 | + ), |
| 21 | + "A job lock record should not exist at this point", |
| 22 | + ) |
| 23 | + with mock.patch.object( |
| 24 | + self.env.cr, "commit", mock.Mock(side_effect=self.env.flush_all) |
| 25 | + ) as mock_commit: |
| 26 | + job = RunJobController._acquire_job(self.env, job_uuid="test_enqueued_job") |
| 27 | + mock_commit.assert_called_once() |
| 28 | + self.assertIsNotNone(job) |
| 29 | + self.assertEqual(job.uuid, "test_enqueued_job") |
| 30 | + self.assertEqual(job.state, "started") |
| 31 | + self.assertTrue( |
| 32 | + self.env["queue.job.lock"].search( |
| 33 | + [("queue_job_id", "=", job_record.id)] |
| 34 | + ), |
| 35 | + "A job lock record should exist at this point", |
| 36 | + ) |
| 37 | + |
| 38 | + def test_acquire_started_job(self): |
| 39 | + with ( |
| 40 | + mock.patch.object( |
| 41 | + self.env.cr, "commit", mock.Mock(side_effect=self.env.flush_all) |
| 42 | + ) as mock_commit, |
| 43 | + self.assertLogs(level=logging.WARNING) as logs, |
| 44 | + ): |
| 45 | + job = RunJobController._acquire_job(self.env, "test_started_job") |
| 46 | + mock_commit.assert_not_called() |
| 47 | + self.assertIsNone(job) |
| 48 | + self.assertIn( |
| 49 | + "was requested to run job test_started_job, but it does not exist", |
| 50 | + logs.output[0], |
| 51 | + ) |
0 commit comments