3
3
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
4
4
5
5
from datetime import datetime
6
+ from unittest import mock
6
7
7
8
from freezegun import freeze_time
8
9
9
10
from odoo import SUPERUSER_ID , api
10
11
from odoo .tests .common import TransactionCase
11
12
from odoo .tools import mute_logger
12
13
14
+ from odoo .addons .queue_job .jobrunner import QueueJobRunner
15
+
13
16
14
17
class TestQueueJob (TransactionCase ):
15
18
def setUp (self ):
@@ -56,7 +59,6 @@ def test_queue_job_cron_trigger_enqueue_dependencies(self):
56
59
# if the state is "waiting_dependencies", it means the "enqueue_waiting()"
57
60
# step has not been done when the parent job has been done
58
61
self .assertEqual (job_record_depends .state , "done" , "Processed OK" )
59
- self .assertEqual (self .cron .nextcall , datetime (2022 , 2 , 22 , 22 , 22 , 22 ))
60
62
61
63
@freeze_time ("2022-02-22 22:22:22" )
62
64
def test_concurrent_cron_access (self ):
@@ -70,20 +72,7 @@ def test_concurrent_cron_access(self):
70
72
(self .cron .id ,),
71
73
log_exceptions = False ,
72
74
)
73
-
74
- delayable = self .env ["res.partner" ].delayable ().create ({"name" : "test" })
75
- delayable2 = self .env ["res.partner" ].delayable ().create ({"name" : "test2" })
76
- delayable .on_done (delayable2 )
77
- delayable .delay ()
78
- job_record = delayable ._generated_job .db_record ()
79
- job_record_depends = delayable2 ._generated_job .db_record ()
80
-
81
- self .env ["queue.job" ]._job_runner (commit = False )
82
-
83
- self .assertEqual (job_record .state , "done" , "Processed OK" )
84
- # if the state is "waiting_dependencies", it means the "enqueue_waiting()"
85
- # step has not been done when the parent job has been done
86
- self .assertEqual (job_record_depends .state , "done" , "Processed OK" )
75
+ self .env ["res.partner" ].delayable ().create ({"name" : "test" })
87
76
self .assertNotEqual (self .cron .nextcall , datetime (2022 , 2 , 22 , 22 , 22 , 22 ))
88
77
89
78
def test_acquire_one_job_use_priority (self ):
@@ -98,7 +87,9 @@ def test_acquire_one_job_use_priority(self):
98
87
with freeze_time ("2024-01-01 10:03:01" ):
99
88
self .env ["res.partner" ].with_delay (priority = 2 ).create ({"name" : "test" })
100
89
101
- self .assertEqual (self .env ["queue.job" ]._acquire_one_job (), job .db_record ())
90
+ self .assertEqual (
91
+ self .env ["queue.job" ]._acquire_one_job (commit = False ), job .db_record ()
92
+ )
102
93
103
94
def test_acquire_one_job_consume_the_oldest_first (self ):
104
95
with freeze_time ("2024-01-01 10:01:01" ):
@@ -112,4 +103,74 @@ def test_acquire_one_job_consume_the_oldest_first(self):
112
103
with freeze_time ("2024-01-01 10:03:01" ):
113
104
self .env ["res.partner" ].with_delay (priority = 30 ).create ({"name" : "test" })
114
105
115
- self .assertEqual (self .env ["queue.job" ]._acquire_one_job (), job .db_record ())
106
+ self .assertEqual (
107
+ self .env ["queue.job" ]._acquire_one_job (commit = False ), job .db_record ()
108
+ )
109
+
110
+ def test_acquire_one_job_starts_job (self ):
111
+ job = self .env ["res.partner" ].with_delay (priority = 1 ).create ({"name" : "test" })
112
+
113
+ result = self .env ["queue.job" ]._acquire_one_job (commit = False )
114
+
115
+ self .assertEqual (result , job .db_record ())
116
+ self .assertEqual (job .db_record ().state , "started" )
117
+
118
+ def test_acquire_one_job_do_not_overload_channel (self ):
119
+ runner = QueueJobRunner .from_environ_or_config ()
120
+ runner .channel_manager .get_channel_by_name (
121
+ "root.foobar" , autocreate = True
122
+ ).capacity = 2
123
+ job1 = (
124
+ self .env ["res.partner" ]
125
+ .with_delay (channel = "root.foobar" )
126
+ .create ({"name" : "test1" })
127
+ )
128
+ job2 = (
129
+ self .env ["res.partner" ]
130
+ .with_delay (channel = "root.foobar" )
131
+ .create ({"name" : "test2" })
132
+ )
133
+ self .env ["res.partner" ].with_delay (channel = "root.foobar" ).create (
134
+ {"name" : "test3" }
135
+ )
136
+
137
+ with mock .patch .object (
138
+ QueueJobRunner , "from_environ_or_config" , return_value = runner
139
+ ):
140
+ first_acquired_job = self .env ["queue.job" ]._acquire_one_job (commit = False )
141
+ second_acquired_job = self .env ["queue.job" ]._acquire_one_job (commit = False )
142
+ third_acquired_job = self .env ["queue.job" ]._acquire_one_job (commit = False )
143
+
144
+ self .assertEqual (first_acquired_job , job1 .db_record ())
145
+ self .assertEqual (second_acquired_job , job2 .db_record ())
146
+ self .assertEqual (third_acquired_job , self .env ["queue.job" ].browse ())
147
+
148
+ def test_acquire_one_job_root_capacity_ignored (self ):
149
+ runner = QueueJobRunner .from_environ_or_config ()
150
+ runner .channel_manager .get_channel_by_name ("root" , autocreate = True ).capacity = 0
151
+ job1 = (
152
+ self .env ["res.partner" ].with_delay (channel = "root" ).create ({"name" : "test1" })
153
+ )
154
+ job2 = (
155
+ self .env ["res.partner" ].with_delay (channel = "root" ).create ({"name" : "test2" })
156
+ )
157
+ job3 = (
158
+ self .env ["res.partner" ].with_delay (channel = "root" ).create ({"name" : "test3" })
159
+ )
160
+
161
+ with mock .patch .object (
162
+ QueueJobRunner , "from_environ_or_config" , return_value = runner
163
+ ):
164
+ first_acquired_job = self .env ["queue.job" ]._acquire_one_job (commit = False )
165
+ second_acquired_job = self .env ["queue.job" ]._acquire_one_job (commit = False )
166
+ third_acquired_job = self .env ["queue.job" ]._acquire_one_job (commit = False )
167
+
168
+ self .assertEqual (first_acquired_job , job1 .db_record ())
169
+ self .assertEqual (second_acquired_job , job2 .db_record ())
170
+ self .assertEqual (third_acquired_job , job3 .db_record ())
171
+
172
+ @freeze_time ("2022-02-22 22:22:22" )
173
+ def test_queue_job_creation_create_change_next_call (self ):
174
+ self .cron .nextcall = datetime (2021 , 1 , 21 , 21 , 21 , 21 )
175
+ self .env ["res.partner" ].with_delay ().create ({"name" : "test" })
176
+ self .assertNotEqual (self .cron .nextcall , datetime (2022 , 2 , 22 , 22 , 22 , 22 ))
0 commit comments