@@ -123,22 +123,22 @@ class ProcessesLifecycleTest < ActiveSupport::TestCase
123123 no_pause = enqueue_store_result_job ( "no pause" )
124124 pause = enqueue_store_result_job ( "pause" , pause : SolidQueue . shutdown_timeout + 10 . second )
125125
126- wait_while_with_timeout ( 1 . second ) { SolidQueue ::ReadyExecution . count > 0 }
126+ wait_while_with_timeout ( 1 . second ) { SolidQueue ::ReadyExecution . count > 1 }
127127
128- signal_process ( @pid , :TERM , wait : 0.5 )
128+ signal_process ( @pid , :TERM , wait : 0.5 . seconds )
129129
130- sleep ( SolidQueue . shutdown_timeout + 0.5 . second )
130+ t = SolidQueue . shutdown_timeout + 1 . second
131131
132- assert_completed_job_results ( "no pause" )
133- assert_job_status ( no_pause , :finished )
132+ assert_job_status ( no_pause , :finished , timeout : t )
133+ assert_completed_job_results ( "no pause" , timeout : t )
134+
135+ # This job was left claimed as the worker was shutdown without a chance to terminate orderly
136+ assert_job_status ( pause , :claimed , timeout : t )
137+ assert_started_job_result ( "pause" , timeout : t )
134138
135- # This job was left claimed as the worker was shutdown without
136- # a chance to terminate orderly
137- assert_started_job_result ( "pause" )
138- assert_job_status ( pause , :claimed )
139139
140140 # The process running the long job couldn't deregister, the other did
141- assert_registered_workers_for ( :background )
141+ wait_for_registered_processes ( 1 , timeout : 3 . seconds )
142142
143143 # Now wait until the supervisor finishes for real, which will complete the cleanup
144144 wait_for_process_termination_with_timeout ( @pid , timeout : 1 . second )
@@ -300,27 +300,50 @@ def enqueue_store_result_job(value, queue_name = :background, **options)
300300 StoreResultJob . set ( queue : queue_name ) . perform_later ( value , **options )
301301 end
302302
303- def assert_completed_job_results ( value , queue_name = :background , count = 1 )
304- skip_active_record_query_cache do
305- assert_equal count , JobResult . where ( queue_name : queue_name , status : "completed" , value : value ) . count
303+ def assert_completed_job_results ( value , queue_name = :background , count = 1 , timeout : nil )
304+ if timeout
305+ wait_while_with_timeout! ( timeout ) { not check_job_result ( value , "completed" , queue_name , count ) }
306+ else
307+ check_job_result ( value , "completed" , queue_name , count )
308+ end
309+ end
310+
311+ def assert_started_job_result ( value , queue_name = :background , count = 1 , timeout : nil )
312+ if timeout
313+ wait_while_with_timeout! ( timeout ) { not check_job_result ( value , "started" , queue_name , count ) }
314+ else
315+ check_job_result ( value , "started" , queue_name , count )
306316 end
307317 end
308318
309- def assert_started_job_result ( value , queue_name = :background , count = 1 )
319+ def check_job_result ( value , status , queue_name = :background , count = 1 )
310320 skip_active_record_query_cache do
311- assert_equal count , JobResult . where ( queue_name : queue_name , status : "started" , value : value ) . count
321+ count == JobResult . where ( queue_name : queue_name , status : status , value : value ) . count
322+ end
323+ end
324+
325+ def assert_job_status ( active_job , status , timeout : nil )
326+ # Make sure we skip AR query cache. Otherwise the queries done here
327+ # might be cached and since we haven't done any non-SELECT queries
328+ # after they were cached on the connection used in the test, the cache
329+ # will still apply, even though the data returned by the cached queries
330+ # might have been deleted in the forked processes.
331+ if timeout
332+ wait_while_with_timeout! ( timeout ) { not check_job_status ( active_job , status ) }
333+ else
334+ assert check_job_status ( active_job , status )
312335 end
313336 end
314337
315- def assert_job_status ( active_job , status )
338+ def check_job_status ( active_job , status )
316339 # Make sure we skip AR query cache. Otherwise the queries done here
317340 # might be cached and since we haven't done any non-SELECT queries
318341 # after they were cached on the connection used in the test, the cache
319342 # will still apply, even though the data returned by the cached queries
320343 # might have been deleted in the forked processes.
321344 skip_active_record_query_cache do
322345 job = SolidQueue ::Job . find_by ( active_job_id : active_job . job_id )
323- assert job . public_send ( "#{ status } ?" )
346+ job . public_send ( "#{ status } ?" )
324347 end
325348 end
326349end
0 commit comments