require 'spec_helper' require 'delayed_job_active_record' require 'timecop' require 'roqua/probes/delayed_job_probe' describe Roqua::Probes::DelayedJobProbe do before { Timecop.freeze } after { Timecop.return } subject(:probe) { Roqua::Probes::MonitoringProbe.new } before do Roqua::Scheduling::CronJob.delete_all end describe 'call' do it 'sends data to AppSignal' do expect(Appsignal).to receive(:set_gauge).with('scheduler_delay_in_minutes', 10) expect(probe).to receive(:longest_delay_in_minutes).and_return(10) probe.call end end describe 'longest_delay' do it 'returns 0 if there are no schedules' do expect(probe.longest_delay_in_minutes).to eql(0) end it 'if there is a job that should have run 10 minutes ago' do Roqua::Scheduling::CronJob.create!( name: 'hourly', next_run_at: 10.minutes.ago ) expect(probe.longest_delay_in_minutes).to eql(10) end it 'if there is a job that should have run 10 minutes ago and the previous job was completed 15 minutes ago' do Roqua::Scheduling::CronJob.create!( name: 'hourly', next_run_at: 10.minutes.ago, completed_at: 15.minutes.ago ) expect(probe.longest_delay_in_minutes).to eql(10) end it 'if there is a job that should have run 10 minutes ago and was completed 5 minutes ago' do Roqua::Scheduling::CronJob.create!( name: 'hourly', next_run_at: 10.minutes.ago, completed_at: 5.minutes.ago ) expect(probe.longest_delay_in_minutes).to eql(0) end it 'when there is a job in the future that was never completed' do Roqua::Scheduling::CronJob.create!( name: 'hourly', next_run_at: 3.minutes.from_now ) expect(probe.longest_delay_in_minutes).to eql(0) end it 'when there is a job in the future that was completed' do Roqua::Scheduling::CronJob.create!( name: 'hourly', next_run_at: 3.minutes.from_now, completed_at: 1.minute.ago ) expect(probe.longest_delay_in_minutes).to eql(0) end end end