|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | + |
| 5 | +class LifecycleHooksTest < ActiveSupport::TestCase |
| 6 | + self.use_transactional_tests = false |
| 7 | + |
| 8 | + test "run lifecycle hooks" do |
| 9 | + SolidQueue.on_start { JobResult.create!(status: :hook_called, value: :start) } |
| 10 | + SolidQueue.on_stop { JobResult.create!(status: :hook_called, value: :stop) } |
| 11 | + |
| 12 | + SolidQueue.on_worker_start { JobResult.create!(status: :hook_called, value: :worker_start) } |
| 13 | + SolidQueue.on_worker_stop { JobResult.create!(status: :hook_called, value: :worker_stop) } |
| 14 | + |
| 15 | + pid = run_supervisor_as_fork(load_configuration_from: { workers: [ { queues: "*" } ] }) |
| 16 | + wait_for_registered_processes(4) |
| 17 | + |
| 18 | + terminate_process(pid) |
| 19 | + wait_for_registered_processes(0) |
| 20 | + |
| 21 | + results = skip_active_record_query_cache do |
| 22 | + assert_equal 4, JobResult.count |
| 23 | + JobResult.last(4) |
| 24 | + end |
| 25 | + |
| 26 | + assert_equal "hook_called", results.map(&:status).first |
| 27 | + assert_equal [ "start", "stop", "worker_start", "worker_stop" ], results.map(&:value).sort |
| 28 | + ensure |
| 29 | + SolidQueue::Supervisor.clear_hooks |
| 30 | + SolidQueue::Worker.clear_hooks |
| 31 | + end |
| 32 | + |
| 33 | + test "handle errors on lifecycle hooks" do |
| 34 | + previous_on_thread_error, SolidQueue.on_thread_error = SolidQueue.on_thread_error, ->(error) { JobResult.create!(status: :error, value: error.message) } |
| 35 | + SolidQueue.on_start { raise RuntimeError, "everything is broken" } |
| 36 | + |
| 37 | + pid = run_supervisor_as_fork |
| 38 | + wait_for_registered_processes(4) |
| 39 | + |
| 40 | + terminate_process(pid) |
| 41 | + wait_for_registered_processes(0) |
| 42 | + |
| 43 | + result = skip_active_record_query_cache { JobResult.last } |
| 44 | + |
| 45 | + assert_equal "error", result.status |
| 46 | + assert_equal "everything is broken", result.value |
| 47 | + ensure |
| 48 | + SolidQueue.on_thread_error = previous_on_thread_error |
| 49 | + SolidQueue::Supervisor.clear_hooks |
| 50 | + SolidQueue::Worker.clear_hooks |
| 51 | + end |
| 52 | +end |
0 commit comments