process_scheduled_jobs_spec.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe BackgroundServices::Service::ProcessScheduledJobs do
  4. let(:instance) { described_class.new(manager: nil) }
  5. let(:scheduler_1) { create(:scheduler, active: true, prio: 5) }
  6. let(:scheduler_2) { create(:scheduler, active: true, prio: 1) }
  7. let(:scheduler_3) { create(:scheduler, active: false, prio: 3) }
  8. before do
  9. stub_const("#{described_class}::SLEEP_AFTER_JOB_START", 0)
  10. stub_const("#{described_class}::SLEEP_AFTER_LOOP", 0)
  11. Scheduler.destroy_all
  12. end
  13. describe '#run' do
  14. before do
  15. allow(instance).to receive(:run_jobs)
  16. end
  17. it 'keeps running jobs', ensure_threads_exited: true do
  18. ensure_block_keeps_running { instance.run }
  19. expect(instance).to have_received(:run_jobs).at_least(2)
  20. end
  21. context 'when shutdown is requested' do
  22. before do
  23. allow(BackgroundServices).to receive(:shutdown_requested).and_return(true)
  24. end
  25. it 'does not start jobs' do
  26. instance.run
  27. expect(instance).not_to have_received(:run_jobs)
  28. end
  29. end
  30. end
  31. describe '#run_jobs' do
  32. let(:log) { [] }
  33. before do
  34. allow_any_instance_of(described_class::Manager).to receive(:run) do
  35. log << :run_called
  36. end
  37. scheduler_1
  38. end
  39. it 'runs manager for each active job' do
  40. instance.send(:run_jobs)
  41. expect(log).to be_one
  42. end
  43. context 'when shutdown is requested' do
  44. before do
  45. allow(BackgroundServices).to receive(:shutdown_requested).and_return(true)
  46. end
  47. it 'does not run managers' do
  48. instance.send(:run_jobs)
  49. expect(log).to be_empty
  50. end
  51. end
  52. end
  53. describe '#scope' do
  54. it 'returns active scheduled jobs by priority' do
  55. scheduler_1 && scheduler_2 && scheduler_3
  56. expect(instance.send(:scope)).to eq [scheduler_2, scheduler_1]
  57. end
  58. end
  59. end