Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion queue_job/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def job_record_with_same_identity_key(self):
.search(
[
("identity_key", "=", self.identity_key),
("state", "in", [PENDING, ENQUEUED]),
("state", "in", [WAIT_DEPENDENCIES, PENDING, ENQUEUED, STARTED]),
],
limit=1,
)
Expand Down
14 changes: 9 additions & 5 deletions queue_job/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ def by_graph(job):
self._perform_graph_jobs(jobs)
else:
self._perform_single_jobs(jobs)
self.enqueued_jobs = []

def _perform_single_jobs(self, jobs):
# we probably don't want to replicate a perfect order here, but at
Expand All @@ -251,11 +252,14 @@ def _perform_graph_jobs(self, jobs):

def _add_job(self, *args, **kwargs):
job = Job(*args, **kwargs)
self.enqueued_jobs.append(job)

patcher = mock.patch.object(job, "store")
self._store_patchers.append(patcher)
patcher.start()
if not job.identity_key or all(
j.identity_key != job.identity_key for j in self.enqueued_jobs
):
self.enqueued_jobs.append(job)

patcher = mock.patch.object(job, "store")
self._store_patchers.append(patcher)
patcher.start()
Comment on lines +260 to +262

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

    with mock.patch.object(job, "store") as patcher:
        self._store_patchers.append(patcher)

Context Manager: Used the with statement to create a context for the mock patcher. This ensures that the patcher's cleanup (stop()) is automatically handled even if an exception occurs.

By using the with statement, you ensure that the patcher is properly started and stopped, avoiding potential issues with resource leaks or incorrect mocking behavior.


job_args = kwargs.pop("args", None) or ()
job_kwargs = kwargs.pop("kwargs", None) or {}
Expand Down
34 changes: 34 additions & 0 deletions test_queue_job/tests/test_delay_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,38 @@ def test_trap_jobs_on_with_delay_recordset_partial_properties(self):
),
)

def test_trap_with_identity_key(self):
with trap_jobs() as trap:
self.env["test.queue.job"].button_that_uses_with_delay()
trap.assert_jobs_count(1)
trap.assert_jobs_count(1, only=self.env["test.queue.job"].testing_method)

trap.assert_enqueued_job(
self.env["test.queue.job"].testing_method,
args=(1,),
kwargs={"foo": 2},
properties=dict(
channel="root.test",
description="Test",
eta=15,
identity_key=identity_exact,
max_retries=1,
priority=15,
),
)

# Should not enqueue again
self.env["test.queue.job"].button_that_uses_with_delay()
trap.assert_jobs_count(1)

trap.perform_enqueued_jobs()
# Should no longer be enqueued
trap.assert_jobs_count(0)

# Can now requeue
self.env["test.queue.job"].button_that_uses_with_delay()
trap.assert_jobs_count(1)

def test_trap_jobs_on_with_delay_assert_model_count_mismatch(self):
recordset = self.env["test.queue.job"].create({"name": "test"})
with trap_jobs() as trap:
Expand Down Expand Up @@ -219,6 +251,8 @@ def test_trap_jobs_perform(self):
# perform the jobs
trap.perform_enqueued_jobs()

trap.assert_jobs_count(0)

logs = self.env["ir.logging"].search(
[
("name", "=", "test_queue_job"),
Expand Down