spec/gaspar/gaspar_spec.rb in gaspar-0.1.2 vs spec/gaspar/gaspar_spec.rb in gaspar-0.1.3

- old
+ new

@@ -46,15 +46,15 @@ it "should run callbacks" do callbacks = [] Gaspar.configure do before_each { callbacks.push "before" } after_each { callbacks.push "after" } - around_each {|&block| callbacks.push "around"; block.call } + around_each {|gaspar, blk| callbacks.push "around"; blk.call } - every("0.35s", "run callbacks") { callbacks.push "inside" } + every("1s", "run callbacks") { callbacks.push "inside" } end.start!(redis) - sleep(0.4) + sleep(1.5) callbacks.should == %w(before around inside after) end it "should not require a name if a symbol is passed to a job" do @@ -64,11 +64,11 @@ end.start!(redis) }.to_not raise_error end it "should enqueue a job" do - Time.stub!(:now).and_return Time.at(1351061802) + Time.stub(:now).and_return Time.at(1351061802) Gaspar.any_instance.stub(:drift).and_return(0) Gaspar.configure do every "5m", :Foobar end scheduler = double(:scheduler) @@ -82,12 +82,19 @@ Gaspar.configure do cron("* * * * *") { puts "Doing stuff" } end.start!(redis) end + it "should properly calculate the period for a #cron invocation" do + Gaspar.any_instance.should_receive(:schedule).with(:cron, "59 * * * *", [], {period: 3600}) + Gaspar.configure do + cron("59 * * * *") { puts "Doing stuff" } + end.start!(redis) + end + it "should quantitize #every to the next timeslice" do - Time.stub!(:now).and_return Time.at(1351061802) + Time.stub(:now).and_return Time.at(1351061802) Gaspar.any_instance.stub(:drift).and_return(0) Gaspar.any_instance.should_receive(:schedule).with(:every, "5m", [], {:first_at => Time.at(1351062000), :period => 300.0}) Gaspar.configure do every("5m") { puts "Doing stuff" } end.start!(redis) @@ -112,30 +119,31 @@ end it "should process jobs" do value = 0 sleep 0.4 - Gaspar.configure do + g = Gaspar.configure do every "0.35s", "update variable" do value += 1 end - end.start!(redis) + end value.should == 0 - sleep(0.4) + g.start!(redis) + sleep(1.5) value.should > 0 end it "should prevent jobs from running multiple times for the same time period" do value = 0 sleep 0.4 Gaspar.configure do - every("0.35s", "update variable with lock") { value += 1 } - every("0.35s", "update variable with lock") { value += 1 } - every("0.35s", "update variable with lock") { value += 1 } + every("1s", "update variable with lock") { value += 1 } + every("1s", "update variable with lock") { value += 1 } + every("1s", "update variable with lock") { value += 1 } end.start!(redis) value.should == 0 - sleep(0.4) + sleep(1.5) value.should == 1 end end @@ -182,10 +190,10 @@ end context "instance methods" do describe "#sync_watches" do it "should compute drift" do - Time.stub!(:now).and_return(150) + Time.stub(:now).and_return(150) redis = double :redis, :setnx => false, :get => "100", :ttl => (3.2e8.to_i - 25) instance = Gaspar.send(:new) instance.instance_variable_set(:@redis, redis) instance.send :sync_watches instance.drift.should == 25