process_scheduled_jobs_spec.rb 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 }
  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. it 'keeps running jobs', ensure_threads_exited: true do
  15. allow(instance).to receive(:run_jobs)
  16. ensure_block_keeps_running { instance.run }
  17. expect(instance).to have_received(:run_jobs).at_least(2)
  18. end
  19. end
  20. describe '#run_jobs' do
  21. let(:log) { [] }
  22. before do
  23. allow_any_instance_of(described_class::Manager).to receive(:run) do
  24. log << :run_called
  25. end
  26. end
  27. it 'runs manager for each active job' do
  28. scheduler_1
  29. instance.send(:run_jobs)
  30. expect(log).to be_one
  31. end
  32. end
  33. describe '#scope' do
  34. it 'returns active scheduled jobs by priority' do
  35. scheduler_1 && scheduler_2 && scheduler_3
  36. expect(instance.send(:scope)).to eq [scheduler_2, scheduler_1]
  37. end
  38. end
  39. end