Skip to content

Commit

Permalink
Improve waiting for full process shutdown in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rosa committed Aug 25, 2024
1 parent 4afc5d1 commit 72da568
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 21 deletions.
3 changes: 1 addition & 2 deletions test/integration/forked_processes_lifecycle_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,7 @@ class ForkedProcessesLifecycleTest < ActiveSupport::TestCase

private
def assert_clean_termination
wait_for_registered_processes 0, timeout: 0.2.second
assert_no_registered_processes
wait_for_full_process_shutdown
assert_no_claimed_jobs
assert_not process_exists?(@pid)
end
Expand Down
2 changes: 0 additions & 2 deletions test/integration/puma/plugin_testing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ module PluginTesting

teardown do
terminate_process(@pid, signal: :INT) if process_exists?(@pid)

wait_for_registered_processes 0, timeout: 2.seconds
end
end

Expand Down
9 changes: 8 additions & 1 deletion test/test_helpers/processes_test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ def run_supervisor_as_fork(**options)
end
end

def wait_for_full_process_shutdown
wait_for_registered_processes(0, timeout: SolidQueue.shutdown_timeout + 0.2.seconds)
assert_no_registered_processes
end

def wait_for_registered_processes(count, timeout: 1.second)
wait_while_with_timeout(timeout) { SolidQueue::Process.count != count }
wait_while_with_timeout(timeout) do
SolidQueue::Process.count != count
end
end

def assert_no_registered_processes
Expand Down
7 changes: 3 additions & 4 deletions test/unit/async_supervisor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ class AsyncSupervisorTest < ActiveSupport::TestCase
assert_registered_processes(kind: "Dispatcher", supervisor_id: supervisor.process_id)

supervisor.stop

assert_no_registered_processes
wait_for_full_process_shutdown
end

test "start with provided configuration" do
Expand All @@ -24,8 +23,7 @@ class AsyncSupervisorTest < ActiveSupport::TestCase
assert_registered_processes(kind: "Dispatcher", supervisor_id: supervisor.process_id)

supervisor.stop

assert_no_registered_processes
wait_for_full_process_shutdown
end

test "failed orphaned executions" do
Expand All @@ -52,6 +50,7 @@ class AsyncSupervisorTest < ActiveSupport::TestCase
assert_registered_processes(kind: "Supervisor(async)")

supervisor.stop
wait_for_full_process_shutdown

assert_equal 0, SolidQueue::ClaimedExecution.count
assert_equal 3, SolidQueue::FailedExecution.count
Expand Down
21 changes: 12 additions & 9 deletions test/unit/dispatcher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class DispatcherTest < ActiveSupport::TestCase

teardown do
@dispatcher.stop
wait_for_full_process_shutdown
end

test "dispatcher is registered as process" do
Expand Down Expand Up @@ -57,7 +58,8 @@ class DispatcherTest < ActiveSupport::TestCase
with_active_record_logger(ActiveSupport::Logger.new(log)) do
with_polling(silence: false) do
@dispatcher.start
sleep 0.2
wait_for_registered_processes(1, timeout: 1.second)
sleep(0.3)
end
end

Expand All @@ -69,7 +71,8 @@ class DispatcherTest < ActiveSupport::TestCase
with_active_record_logger(ActiveSupport::Logger.new(log)) do
with_polling(silence: true) do
@dispatcher.start
sleep 0.2
wait_for_registered_processes(1, timeout: 1.second)
sleep(0.3)
end
end

Expand All @@ -80,13 +83,10 @@ class DispatcherTest < ActiveSupport::TestCase
with_active_record_logger(nil) do
with_polling(silence: true) do
@dispatcher.start
sleep 0.2
wait_for_registered_processes(1, timeout: 1.second)
sleep 0.3
end
end

@dispatcher.stop
wait_for_registered_processes(0, timeout: 1.second)
assert_no_registered_processes
end

test "run more than one instance of the dispatcher without recurring tasks" do
Expand All @@ -100,7 +100,7 @@ class DispatcherTest < ActiveSupport::TestCase
@dispatcher.start
another_dispatcher.start

wait_while_with_timeout(1.second) { SolidQueue::ScheduledExecution.any? }
wait_while_with_timeout(2.second) { SolidQueue::ScheduledExecution.any? }

assert_equal 0, SolidQueue::ScheduledExecution.count
assert_equal 15, SolidQueue::ReadyExecution.count
Expand All @@ -115,8 +115,11 @@ class DispatcherTest < ActiveSupport::TestCase
end

dispatchers.each(&:start)
sleep 2
wait_for_registered_processes(2, timeout: 2.second)
sleep 1.5

dispatchers.each(&:stop)
wait_for_full_process_shutdown

assert_equal SolidQueue::Job.count, SolidQueue::RecurringExecution.count
run_at_times = SolidQueue::RecurringExecution.all.map(&:run_at).sort
Expand Down
2 changes: 1 addition & 1 deletion test/unit/fork_supervisor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class ForkSupervisorTest < ActiveSupport::TestCase
assert File.exist?(@pidfile)
assert_equal pid, File.read(@pidfile).strip.to_i

wait_for_registered_processes(0)
wait_for_full_process_shutdown

pid = run_supervisor_as_fork
wait_for_registered_processes(4)
Expand Down
3 changes: 1 addition & 2 deletions test/unit/worker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ class WorkerTest < ActiveSupport::TestCase
end

@worker.stop
wait_for_registered_processes(0, timeout: 1.second)
assert_no_registered_processes
wait_for_full_process_shutdown
end

test "run inline" do
Expand Down

0 comments on commit 72da568

Please sign in to comment.