11# Copyright 2025 ACSONE SA/NV
22# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
33from contextlib import closing
4- from datetime import datetime , timedelta
54
65from odoo .tests .common import TransactionCase
76
109
1110
1211class TestRequeueDeadJob (TransactionCase ):
13- def create_dummy_job (self , uuid ):
14- """
15- Create dummy job for tests
16- """
17- return (
18- self .env ["queue.job" ]
19- .with_context (
20- _job_edit_sentinel = self .env ["queue.job" ].EDIT_SENTINEL ,
21- )
22- .create (
23- {
24- "uuid" : uuid ,
25- "user_id" : self .env .user .id ,
26- "state" : "pending" ,
27- "model_name" : "queue.job" ,
28- "method_name" : "write" ,
29- }
30- )
12+ def _get_demo_job (self , uuid ):
13+ # job created during load of demo data
14+ job = self .env ["queue.job" ].search (
15+ [
16+ ("uuid" , "=" , uuid ),
17+ ]
18+ )
19+
20+ self .assertTrue (
21+ job ,
22+ f"Demo data queue job { uuid } should be loaded in order"
23+ " to make this tests work" ,
3124 )
3225
26+ return job
27+
3328 def get_locks (self , uuid , cr = None ):
3429 """
3530 Retrieve lock rows
@@ -60,7 +55,8 @@ def get_locks(self, uuid, cr=None):
6055 return cr .fetchall ()
6156
6257 def test_add_lock_record (self ):
63- queue_job = self .create_dummy_job ("test_add_lock" )
58+ queue_job = self ._get_demo_job ("test_started_job" )
59+
6460 job_obj = Job .load (self .env , queue_job .uuid )
6561
6662 job_obj .set_started ()
@@ -71,7 +67,7 @@ def test_add_lock_record(self):
7167 self .assertEqual (1 , len (locks ))
7268
7369 def test_lock (self ):
74- queue_job = self .create_dummy_job ( "test_lock " )
70+ queue_job = self ._get_demo_job ( "test_started_job " )
7571 job_obj = Job .load (self .env , queue_job .uuid )
7672
7773 job_obj .set_started ()
@@ -81,9 +77,6 @@ def test_lock(self):
8177
8278 self .assertEqual (1 , len (locks ))
8379
84- # commit to update queue_job records in DB
85- self .env .cr .commit () # pylint: disable=E8102
86-
8780 job_obj .lock ()
8881
8982 with closing (self .env .registry .cursor ()) as new_cr :
@@ -92,28 +85,13 @@ def test_lock(self):
9285 # Row should be locked
9386 self .assertEqual (0 , len (locks ))
9487
95- # clean up
96- queue_job .unlink ()
97-
98- self .env .cr .commit () # pylint: disable=E8102
99-
100- # because we committed the cursor, the savepoint of the test method is
101- # gone, and this would break TransactionCase cleanups
102- self .cr .execute ("SAVEPOINT test_%d" % self ._savepoint_id )
103-
10488 def test_requeue_dead_jobs (self ):
105- uuid = "test_requeue_dead_jobs"
106-
107- queue_job = self .create_dummy_job (uuid )
89+ queue_job = self ._get_demo_job ("test_enqueued_job" )
10890 job_obj = Job .load (self .env , queue_job .uuid )
10991
11092 job_obj .set_enqueued ()
111- # simulate enqueuing was in the past
112- job_obj .date_enqueued = datetime .now () - timedelta (minutes = 1 )
11393 job_obj .set_started ()
114-
11594 job_obj .store ()
116- self .env .cr .commit () # pylint: disable=E8102
11795
11896 # requeue dead jobs using current cursor
11997 query = Database (self .env .cr .dbname )._query_requeue_dead_jobs ()
@@ -122,12 +100,4 @@ def test_requeue_dead_jobs(self):
122100 uuids_requeued = self .env .cr .fetchall ()
123101
124102 self .assertEqual (len (uuids_requeued ), 1 )
125- self .assertEqual (uuids_requeued [0 ][0 ], uuid )
126-
127- # clean up
128- queue_job .unlink ()
129- self .env .cr .commit () # pylint: disable=E8102
130-
131- # because we committed the cursor, the savepoint of the test method is
132- # gone, and this would break TransactionCase cleanups
133- self .cr .execute ("SAVEPOINT test_%d" % self ._savepoint_id )
103+ self .assertEqual (uuids_requeued [0 ][0 ], queue_job .uuid )
0 commit comments