process_delayed_jobs_spec.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. class SampleDelayedJob < ApplicationJob
  4. def perform
  5. Rails.logger.debug 'performing SampleTestJob'
  6. end
  7. end
  8. RSpec.describe BackgroundServices::Service::ProcessDelayedJobs, ensure_threads_exited: true do
  9. before do
  10. stub_const "#{described_class}::SLEEP_IF_EMPTY", 1
  11. end
  12. let(:instance) { described_class.new(manager: nil) }
  13. describe '#run' do
  14. context 'with a queued job' do
  15. before do
  16. Delayed::Job.destroy_all
  17. SampleDelayedJob.perform_later
  18. end
  19. it 'processes a job' do
  20. expect do
  21. ensure_block_keeps_running do
  22. described_class.new(manager: nil).run
  23. end
  24. end.to change(Delayed::Job, :count).by(-1)
  25. end
  26. it 'runs loop multiple times', :aggregate_failures do
  27. allow(instance).to receive(:process_results)
  28. # Delayed::Worker uses `rescue Exception` heavily which would swallow our timeout errors,
  29. # causing the tests to fail. Avoid calling it.
  30. allow(Benchmark).to receive(:realtime).and_return(1)
  31. ensure_block_keeps_running { instance.run }
  32. expect(instance).to have_received(:process_results).at_least(1)
  33. end
  34. context 'when shutdown is requested' do
  35. before do
  36. allow(BackgroundServices).to receive(:shutdown_requested).and_return(true)
  37. end
  38. it 'does not start jobs' do
  39. expect { described_class.new(manager: nil).run }.not_to change(Delayed::Job, :count)
  40. end
  41. end
  42. end
  43. end
  44. describe '#process_results' do
  45. it 'sleeps & loops when no jobs processed', :aggregate_failures do
  46. allow(Rails.logger).to receive(:debug)
  47. instance.send(:process_results, [0, 0], 1)
  48. expect(Rails.logger).to have_received(:debug).with(no_args) do |&block|
  49. expect(block.call).to match(%r{loop})
  50. end
  51. end
  52. it 'loops immediatelly when there was anything to process', :aggregate_failures do
  53. allow(Rails.logger).to receive(:debug)
  54. instance.send(:process_results, [1, 0], 1)
  55. expect(Rails.logger).to have_received(:debug).with(no_args) do |&block|
  56. expect(block.call).to match(%r{jobs processed})
  57. end
  58. end
  59. end
  60. describe '.pre_run' do
  61. it 'cleans up DelayedJobs' do
  62. allow(described_class::CleanupAction).to receive(:cleanup_delayed_jobs)
  63. described_class.pre_run
  64. expect(described_class::CleanupAction).to have_received(:cleanup_delayed_jobs)
  65. end
  66. it 'cleans up ImportJobs' do
  67. allow(ImportJob).to receive(:cleanup_import_jobs)
  68. described_class.pre_run
  69. expect(ImportJob).to have_received(:cleanup_import_jobs)
  70. end
  71. it 'runs in scheduler context' do
  72. handle_info = nil
  73. allow(described_class)
  74. .to receive(:pre_launch).and_invoke(-> { handle_info = ApplicationHandleInfo.current })
  75. described_class.pre_run
  76. expect(handle_info).to eq 'scheduler'
  77. end
  78. end
  79. end