55class BatchLifecycleTest < ActiveSupport ::TestCase
66 FailingJobError = Class . new ( RuntimeError )
77
8- def assert_finished_in_order ( *finishables )
9- finishables . each_cons ( 2 ) do |finished1 , finished2 |
10- assert_equal finished1 . finished_at < finished2 . finished_at , true
11- end
12- end
13-
14- def job! ( active_job )
15- SolidQueue ::Job . find_by! ( active_job_id : active_job . job_id )
16- end
17-
188 setup do
199 @_on_thread_error = SolidQueue . on_thread_error
2010 SolidQueue . on_thread_error = silent_on_thread_error_for ( [ FailingJobError ] , @_on_thread_error )
2111 @worker = SolidQueue ::Worker . new ( queues : "background" , threads : 3 )
2212 @dispatcher = SolidQueue ::Dispatcher . new ( batch_size : 10 , polling_interval : 0.2 )
13+ SolidQueue ::Batch . maintenance_queue_name = "background"
2314 end
2415
2516 teardown do
@@ -34,6 +25,7 @@ def job!(active_job)
3425
3526 ApplicationJob . enqueue_after_transaction_commit = false if defined? ( ApplicationJob . enqueue_after_transaction_commit )
3627 SolidQueue . preserve_finished_jobs = true
28+ SolidQueue ::Batch . maintenance_queue_name = nil
3729 end
3830
3931 class BatchOnSuccessJob < ApplicationJob
@@ -86,40 +78,12 @@ def perform
8678 end
8779 end
8880
89- test "empty batches never finish" do
90- # `enqueue_after_transaction_commit` makes it difficult to tell if a batch is empty, or if the
91- # jobs are waiting to be run after commit.
92- # If we could tell deterministically, we could enqueue an EmptyJob to make sure the batches
93- # don't hang forever.
94- SolidQueue ::Batch . enqueue ( on_success : BatchOnSuccessJob . new ( "3" ) ) do
95- SolidQueue ::Batch . enqueue ( on_success : BatchOnSuccessJob . new ( "2" ) ) do
96- SolidQueue ::Batch . enqueue ( on_success : BatchOnSuccessJob . new ( "1" ) ) { }
97- SolidQueue ::Batch . enqueue ( on_success : BatchOnSuccessJob . new ( "1.1" ) ) { }
98- end
99- end
100-
101- @dispatcher . start
102- @worker . start
103-
104- wait_for_batches_to_finish_for ( 2 . seconds )
105- wait_for_jobs_to_finish_for ( 1 . second )
106-
107- assert_equal [ ] , JobBuffer . values
108- assert_equal 4 , SolidQueue ::Batch . pending . count
109- end
110-
11181 test "nested batches finish from the inside out" do
11282 batch2 = batch3 = batch4 = nil
11383 batch1 = SolidQueue ::Batch . enqueue ( on_success : BatchOnSuccessJob . new ( "3" ) ) do
114- SolidQueue ::Batch ::EmptyJob . perform_later
11584 batch2 = SolidQueue ::Batch . enqueue ( on_success : BatchOnSuccessJob . new ( "2" ) ) do
116- SolidQueue ::Batch ::EmptyJob . perform_later
117- batch3 = SolidQueue ::Batch . enqueue ( on_success : BatchOnSuccessJob . new ( "1" ) ) do
118- SolidQueue ::Batch ::EmptyJob . perform_later
119- end
120- batch4 = SolidQueue ::Batch . enqueue ( on_success : BatchOnSuccessJob . new ( "1.1" ) ) do
121- SolidQueue ::Batch ::EmptyJob . perform_later
122- end
85+ batch3 = SolidQueue ::Batch . enqueue ( on_success : BatchOnSuccessJob . new ( "1" ) ) { }
86+ batch4 = SolidQueue ::Batch . enqueue ( on_success : BatchOnSuccessJob . new ( "1.1" ) ) { }
12387 end
12488 end
12589
@@ -188,10 +152,11 @@ def perform
188152
189153 wait_for_batches_to_finish_for ( 2 . seconds )
190154
155+ jobs = batch_jobs ( batch1 , batch2 , batch3 )
191156 assert_equal [ "hey" , "ho" , "let's go" ] , JobBuffer . values . sort
192157 assert_equal 3 , SolidQueue ::Batch . finished . count
193- assert_equal 3 , SolidQueue :: Job . finished . count
194- assert_equal 3 , SolidQueue :: Job . count
158+ assert_equal 3 , jobs . finished . count
159+ assert_equal 3 , jobs . count
195160 assert_finished_in_order ( batch3 . reload , batch2 . reload , batch1 . reload )
196161 assert_finished_in_order ( job! ( job3 ) , batch3 )
197162 assert_finished_in_order ( job! ( job2 ) , batch2 )
@@ -301,8 +266,8 @@ def perform
301266 end
302267
303268 assert_equal false , batch1 . reload . finished?
304- assert_equal 1 , SolidQueue :: Job . count
305- assert_equal 0 , SolidQueue :: Job . finished . count
269+ assert_equal 1 , batch1 . jobs . count
270+ assert_equal 0 , batch1 . jobs . finished . count
306271
307272 @dispatcher . start
308273 @worker . start
@@ -360,4 +325,18 @@ def perform(batch)
360325 JobBuffer . add "Hi failure #{ batch . batch_id } !"
361326 end
362327 end
328+
329+ def assert_finished_in_order ( *finishables )
330+ finishables . each_cons ( 2 ) do |finished1 , finished2 |
331+ assert_equal finished1 . finished_at < finished2 . finished_at , true
332+ end
333+ end
334+
335+ def job! ( active_job )
336+ SolidQueue ::Job . find_by! ( active_job_id : active_job . job_id )
337+ end
338+
339+ def batch_jobs ( *batches )
340+ SolidQueue ::Job . where ( batch_id : batches . map ( &:batch_id ) )
341+ end
363342end
0 commit comments