forked from OCA/queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add method to patch a method to be automatically delayed
This patch method has to be called in ``_register_hook``. When a method is patched, any call to the method will not directly execute the method's body, but will instead enqueue a job. When a ``context_key`` is set when calling ``_patch_job_auto_delay``, the patched method is automatically delayed only when this key is ``True`` in the caller's context. It is advised to patch the method with a ``context_key``, because making the automatic delay *in any case* can produce nasty and unexpected side effects (e.g. another module calls the method and expects it to be computed before doing something else, expecting a result, ...). A typical use case is when a method in a module we don't control is called synchronously in the middle of another method, and we'd like all the calls to this method become asynchronous. It relies on OCA#274 that deprecates the `@job` decorator.
- Loading branch information
1 parent
1ef520e
commit e2883c1
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from . import test_autovacuum | ||
from . import test_job | ||
from . import test_job_auto_delay | ||
from . import test_job_channels | ||
from . import test_related_actions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Copyright 2020 Camptocamp SA | ||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html) | ||
|
||
from odoo.tests.common import tagged | ||
|
||
from odoo.addons.queue_job.job import Job | ||
|
||
from .common import JobCommonCase | ||
|
||
|
||
@tagged("post_install", "-at_install") | ||
class TestJobAutoDelay(JobCommonCase): | ||
"""Test auto delay of jobs""" | ||
|
||
def test_auto_delay(self): | ||
"""method decorated by @job_auto_delay is automatically delayed""" | ||
result = self.env["test.queue.job"].delay_me(1, kwarg=2) | ||
self.assertTrue(isinstance(result, Job)) | ||
self.assertEqual(result.args, (1,)) | ||
self.assertEqual(result.kwargs, {"kwarg": 2}) | ||
|
||
def test_auto_delay_options(self): | ||
"""method automatically delayed une <method>_job_options arguments""" | ||
result = self.env["test.queue.job"].delay_me_options() | ||
self.assertTrue(isinstance(result, Job)) | ||
self.assertEqual(result.identity_key, "my_job_identity") | ||
|
||
def test_auto_delay_inside_job(self): | ||
"""when a delayed job is processed, it must not delay itself""" | ||
job_ = self.env["test.queue.job"].delay_me(1, kwarg=2) | ||
self.assertTrue(job_.perform(), (1, 2)) | ||
|
||
def test_auto_delay_force_sync(self): | ||
"""method forced to run synchronously""" | ||
result = ( | ||
self.env["test.queue.job"] | ||
.with_context(_job_force_sync=True) | ||
.delay_me(1, kwarg=2) | ||
) | ||
self.assertTrue(result, (1, 2)) | ||
|
||
def test_auto_delay_context_key_set(self): | ||
"""patched with context_key delays only if context keys is set""" | ||
result = ( | ||
self.env["test.queue.job"] | ||
.with_context(auto_delay_delay_me_context_key=True) | ||
.delay_me_context_key() | ||
) | ||
self.assertTrue(isinstance(result, Job)) | ||
|
||
def test_auto_delay_context_key_unset(self): | ||
"""patched with context_key do not delay if context keys is not set""" | ||
result = self.env["test.queue.job"].delay_me_context_key() | ||
self.assertEqual(result, "ok") |