2014-01-30 00:21:38 -05:00
|
|
|
# encoding: utf-8
|
2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2014-01-30 00:21:38 -05:00
|
|
|
require 'scheduler/scheduler'
|
|
|
|
|
|
|
|
describe Scheduler::ScheduleInfo do
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
let(:manager) { Scheduler::Manager.new }
|
2014-03-14 13:02:21 -04:00
|
|
|
|
|
|
|
context "every" do
|
|
|
|
class RandomJob
|
|
|
|
extend ::Scheduler::Schedule
|
2014-01-30 00:21:38 -05:00
|
|
|
|
2014-03-14 13:02:21 -04:00
|
|
|
every 1.hour
|
2014-01-30 00:21:38 -05:00
|
|
|
|
2014-03-14 13:02:21 -04:00
|
|
|
def perform
|
|
|
|
# work_it
|
|
|
|
end
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
|
2014-03-14 13:02:21 -04:00
|
|
|
before do
|
|
|
|
@info = manager.schedule_info(RandomJob)
|
|
|
|
@info.del!
|
|
|
|
end
|
2014-01-30 00:21:38 -05:00
|
|
|
|
2014-03-14 13:02:21 -04:00
|
|
|
after do
|
|
|
|
manager.stop!
|
2016-06-06 05:29:11 -04:00
|
|
|
$redis.del manager.class.queue_key
|
2014-03-14 13:02:21 -04:00
|
|
|
end
|
2014-01-30 00:21:38 -05:00
|
|
|
|
2014-03-14 13:02:21 -04:00
|
|
|
it "is a scheduled job" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(RandomJob).to be_scheduled
|
2014-03-14 13:02:21 -04:00
|
|
|
end
|
2014-01-30 00:21:38 -05:00
|
|
|
|
2014-03-14 13:02:21 -04:00
|
|
|
it 'starts off invalid' do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(@info.valid?).to eq(false)
|
2014-03-14 13:02:21 -04:00
|
|
|
end
|
2014-01-30 00:21:38 -05:00
|
|
|
|
2014-03-14 13:02:21 -04:00
|
|
|
it 'will have a due date in the next 5 minutes if it was blank' do
|
|
|
|
@info.schedule!
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(@info.valid?).to eq(true)
|
|
|
|
expect(@info.next_run).to be_within(5.minutes).of(Time.now.to_i)
|
2014-03-14 13:02:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'will have a due date within the next hour if it just ran' do
|
|
|
|
@info.prev_run = Time.now.to_i
|
|
|
|
@info.schedule!
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(@info.valid?).to eq(true)
|
|
|
|
expect(@info.next_run).to be_within(1.hour * manager.random_ratio).of(Time.now.to_i + 1.hour)
|
2014-03-14 13:02:21 -04:00
|
|
|
end
|
2014-01-30 00:21:38 -05:00
|
|
|
|
2014-03-14 13:02:21 -04:00
|
|
|
it 'is invalid if way in the future' do
|
|
|
|
@info.next_run = Time.now.to_i + 1.year
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(@info.valid?).to eq(false)
|
2014-03-14 13:02:21 -04:00
|
|
|
end
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
|
2014-03-14 13:02:21 -04:00
|
|
|
context "daily" do
|
|
|
|
|
|
|
|
class DailyJob
|
|
|
|
extend ::Scheduler::Schedule
|
2017-07-24 13:00:15 -04:00
|
|
|
daily at: 11.hours
|
2014-03-14 13:02:21 -04:00
|
|
|
|
|
|
|
def perform
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2017-07-24 13:00:15 -04:00
|
|
|
freeze_time Time.parse("2010-01-10 10:00:00")
|
|
|
|
|
2014-03-14 13:02:21 -04:00
|
|
|
@info = manager.schedule_info(DailyJob)
|
|
|
|
@info.del!
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
manager.stop!
|
2016-06-06 05:29:11 -04:00
|
|
|
$redis.del manager.class.queue_key
|
2014-03-14 13:02:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is a scheduled job" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(DailyJob).to be_scheduled
|
2014-03-14 13:02:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "starts off invalid" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(@info.valid?).to eq(false)
|
2014-03-14 13:02:21 -04:00
|
|
|
end
|
|
|
|
|
2017-07-24 11:03:03 -04:00
|
|
|
it "will have a due date at the appropriate time if blank" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(@info.next_run).to eq(nil)
|
2014-03-14 13:02:21 -04:00
|
|
|
@info.schedule!
|
2017-07-24 11:03:03 -04:00
|
|
|
|
|
|
|
expect(JSON.parse($redis.get(@info.key))["next_run"])
|
2017-07-24 13:08:17 -04:00
|
|
|
.to eq((Time.zone.now.midnight + 11.hours).to_i)
|
2017-07-24 11:03:03 -04:00
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(@info.valid?).to eq(true)
|
2014-03-14 13:02:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is invalid if way in the future' do
|
|
|
|
@info.next_run = Time.now.to_i + 1.year
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(@info.valid?).to eq(false)
|
2014-03-14 13:02:21 -04:00
|
|
|
end
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|