Sha256: 568c19ad8d322eb67077195dcccf13d83b96b8e5643104e92aa88e66dbe0353b

Contents?: true

Size: 1.9 KB

Versions: 21

Compression:

Stored size: 1.9 KB

Contents

require 'rspec/core'
require 'abstractivator/schedule/schedule'
require 'abstractivator/schedule/schedule_runner'
require_relative './em_util'

describe ScheduleRunner do
  let(:task) {double}
  let(:interval) {0.1}

  before(:each) do
    allow(task).to receive(:interval_seconds) {interval}
    allow(task).to receive(:before_waiting)
    allow(task).to receive(:pump) {EventMachine}
    allow(task).to receive(:schedule) {Schedule.new('00:00-24:00')}
  end

  describe '::run_on_schedule' do
    it 'waits for an interval before starting' do
      em do
        start = Time.now
        ScheduleRunner.run_on_schedule(task) do
          expect(Time.now - start).to be_within(ten_percent_of(interval)).of(interval)
          done
        end
      end
    end
    it 'runs periodically' do
      em do
        last = Time.now
        n = 0
        ScheduleRunner.run_on_schedule(task) do
          n += 1
          expect(Time.now - last).to be_within(ten_percent_of(interval)).of(interval)
          last = Time.now
          done if n == 3
        end
      end
    end
    it 'only runs during scheduled times' do
      em do
        start = Time.now + 3 * 60
        stop = Time.now + 4 * 60
        allow(task).to receive(:schedule) {Schedule.new("#{start.hour}:#{start.min}-#{stop.hour}:#{stop.min}")}
        orig_add_timer = EventMachine.method(:add_timer)
        expect(EventMachine).to receive(:add_timer).with(interval) {|s, &block| orig_add_timer.(s, &block)}.ordered
        expect(EventMachine).to receive(:add_timer).with(3 * 60) {done}.ordered
        ScheduleRunner.run_on_schedule(task) {}
      end
    end
    it 'calls before_waiting before waiting' do
      em do
        expect(task).to receive(:before_waiting).with(interval).ordered
        expect(EventMachine).to receive(:add_timer) {done}.ordered
        ScheduleRunner.run_on_schedule(task) {}
      end
    end
  end

  def ten_percent_of(x)
    x / 10.0
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
abstractivator-0.19.0 spec/lib/abstractivator/schedule/schedule_runner_spec.rb
abstractivator-0.18.0 spec/lib/abstractivator/schedule/schedule_runner_spec.rb
abstractivator-0.17.0 spec/lib/abstractivator/schedule/schedule_runner_spec.rb
abstractivator-0.16.0 spec/lib/abstractivator/schedule/schedule_runner_spec.rb
abstractivator-0.15.0 spec/lib/abstractivator/schedule/schedule_runner_spec.rb
abstractivator-0.14.0 spec/lib/abstractivator/schedule/schedule_runner_spec.rb
abstractivator-0.13.0 spec/lib/abstractivator/schedule/schedule_runner_spec.rb
abstractivator-0.12.0 spec/lib/abstractivator/schedule/schedule_runner_spec.rb
abstractivator-0.11.0 spec/lib/abstractivator/schedule/schedule_runner_spec.rb
abstractivator-0.10.0 spec/lib/abstractivator/schedule/schedule_runner_spec.rb
abstractivator-0.8.0 spec/lib/abstractivator/schedule/schedule_runner_spec.rb
abstractivator-0.7.0 spec/lib/abstractivator/schedule/schedule_runner_spec.rb
abstractivator-0.6.0 spec/lib/abstractivator/schedule/schedule_runner_spec.rb
abstractivator-0.5.0 spec/lib/abstractivator/schedule/schedule_runner_spec.rb
abstractivator-0.4.1 spec/lib/abstractivator/schedule/schedule_runner_spec.rb
abstractivator-0.4.0 spec/lib/abstractivator/schedule/schedule_runner_spec.rb
abstractivator-0.3.1 spec/lib/abstractivator/schedule/schedule_runner_spec.rb
abstractivator-0.3.0 spec/lib/abstractivator/schedule/schedule_runner_spec.rb
abstractivator-0.2.0 spec/lib/abstractivator/schedule/schedule_runner_spec.rb
abstractivator-0.1.1 spec/lib/abstractivator/schedule/schedule_runner_spec.rb