summaryrefslogtreecommitdiff
path: root/spec/unit/scheduler/job_spec.rb
blob: c1d002cfca73714c93ba96cbfaef4429bb139f44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#! /usr/bin/env ruby
require 'spec_helper'
require 'puppet/scheduler'

describe Puppet::Scheduler::Job do
  let(:run_interval) { 10 }
  let(:job) { described_class.new(run_interval) }

  it "has a minimum run interval of 0" do
    Puppet::Scheduler::Job.new(-1).run_interval.should == 0
  end

  describe "when not run yet" do
    it "is ready" do
      job.ready?(2).should be
    end

    it "gives the time to next run as 0" do
      job.interval_to_next_from(2).should == 0
    end
  end

  describe "when run at least once" do
    let(:last_run) { 50 }

    before(:each) do
      job.run(last_run)
    end

    it "is ready when the time is greater than the last run plus the interval" do
      job.ready?(last_run + run_interval + 1).should be
    end

    it "is ready when the time is equal to the last run plus the interval" do
      job.ready?(last_run + run_interval).should be
    end

    it "is not ready when the time is less than the last run plus the interval" do
      job.ready?(last_run + run_interval - 1).should_not be
    end

    context "when calculating the next run" do
      it "returns the run interval if now == last run" do
        job.interval_to_next_from(last_run).should == run_interval
      end

      it "when time is between the last and next runs gives the remaining portion of the run_interval" do
        time_since_last_run = 2
        now = last_run + time_since_last_run
        job.interval_to_next_from(now).should == run_interval - time_since_last_run
      end

      it "when time is later than last+interval returns 0" do
        time_since_last_run = run_interval + 5
        now = last_run + time_since_last_run
        job.interval_to_next_from(now).should == 0
      end
    end
  end

  it "starts enabled" do
    job.enabled?.should be
  end

  it "can be disabled" do
    job.disable
    job.enabled?.should_not be
  end

  it "has the job instance as a parameter" do
    passed_job = nil
    job = Puppet::Scheduler::Job.new(run_interval) do |j|
      passed_job = j
    end
    job.run(5)

    passed_job.should eql(job)
  end
end