delayed_job_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. class SampleJob < ApplicationJob
  4. def perform
  5. true
  6. end
  7. end
  8. RSpec.describe MonitoringHelper::HealthChecker::DelayedJob do
  9. let(:instance) { described_class.new }
  10. describe '#check_health' do
  11. before do
  12. allow(instance).to receive(:failed_jobs)
  13. allow(instance).to receive(:failed_with_attempts)
  14. allow(instance).to receive(:total_jobs)
  15. instance.check_health
  16. end
  17. it 'checks failed jobs' do
  18. expect(instance).to have_received(:failed_jobs)
  19. end
  20. it 'checks failed jobs with attempts' do
  21. expect(instance).to have_received(:failed_with_attempts)
  22. end
  23. it 'checks total jobs' do
  24. expect(instance).to have_received(:total_jobs)
  25. end
  26. end
  27. describe '#scope' do
  28. it 'returns jobs with attempts' do
  29. _job1 = Delayed::Job.enqueue(SampleJob.new)
  30. job2 = Delayed::Job.enqueue(SampleJob.new)
  31. job2.update!(attempts: 5)
  32. expect(instance.send(:scope)).to contain_exactly(job2)
  33. end
  34. end
  35. describe '#failed_jobs' do
  36. before { stub_const("#{described_class}::FAILED_JOBS_THRESHOLD", 5) }
  37. it 'does nothing if failed jobs are under threshold' do
  38. 3.times { Delayed::Job.enqueue(SampleJob.new).update!(attempts: 5) }
  39. instance.send(:failed_jobs)
  40. expect(instance.response.issues).to be_blank
  41. end
  42. it 'adds issue if failed jobs over threshold' do
  43. 10.times { Delayed::Job.enqueue(SampleJob.new).update!(attempts: 5) }
  44. instance.send(:failed_jobs)
  45. expect(instance.response.issues.first).to eq '10 failing background jobs'
  46. end
  47. end
  48. describe '#failed_with_attempts' do
  49. it 'adds issue for failed jobs' do
  50. 10.times { Delayed::Job.enqueue(SampleJob.new).update!(attempts: 5) }
  51. instance.send(:failed_with_attempts)
  52. expect(instance.response.issues.first).to eq "Failed to run background job #1 'SampleJob' 10 time(s) with 50 attempt(s)."
  53. end
  54. end
  55. describe '#map_single_failed_job' do
  56. let(:job) { Delayed::Job.enqueue(SampleJob.new) }
  57. let(:hash) { {} }
  58. it 'starts collecting with empty hash' do
  59. job.update! attempts: 3
  60. instance.send(:map_single_failed_job, job, hash)
  61. expect(hash).to include('SampleJob' => { count: 1, attempts: 3 })
  62. end
  63. it 'adds details to existing hash' do
  64. job.update! attempts: 3
  65. hash['SampleJob'] = { count: 3, attempts: 123 }
  66. instance.send(:map_single_failed_job, job, hash)
  67. expect(hash).to include('SampleJob' => { count: 4, attempts: 126 })
  68. end
  69. end
  70. describe '#job_name' do
  71. it 'returns original job class name' do
  72. job = Delayed::Job.enqueue(SampleJob.new)
  73. expect(instance.send(:job_name, job)).to eq 'SampleJob'
  74. end
  75. end
  76. describe '#total_jobs' do
  77. before { stub_const("#{described_class}::TOTAL_JOBS_THRESHOLD", 4) }
  78. it 'does nothing if jobs count is bellow threshold' do
  79. 6.times { |i| Delayed::Job.enqueue(SampleJob.new).update!(created_at: (i * 4).minutes.ago) }
  80. end
  81. it 'adds issue if jobs count is over threshold' do
  82. 10.times { |i| Delayed::Job.enqueue(SampleJob.new).update!(created_at: (i * 4).minutes.ago) }
  83. end
  84. end
  85. end