diff --git a/README.md b/README.md index 509bac1..a6bd63f 100644 --- a/README.md +++ b/README.md @@ -272,6 +272,23 @@ describe "#recalculate" do end ``` +And I might use the `in` or `at` statement to specify time: + +```ruby +describe "#recalculate" do + before do + ResqueSpec.reset! + end + + it "adds person.calculate to the Person queue" do + person.recalculate + + # Is it scheduled to be executed in 5 minutes or at 2010-02-14 06:00:00 ?? + expect(Person).to have_scheduled(person.id, :calculate).at_or_in(Time.mktime(2010,2,14,6,0,0), 5 * 60) + end +end +``` + You can also check the size of the schedule: ```ruby diff --git a/lib/resque_spec/matchers.rb b/lib/resque_spec/matchers.rb index b675dc7..c26d6d9 100644 --- a/lib/resque_spec/matchers.rb +++ b/lib/resque_spec/matchers.rb @@ -156,12 +156,20 @@ def schedule_queue_for(actual) @time_info = "in #{@interval} seconds" end + chain :at_or_in do |timestamp, interval| + @time = timestamp + @interval = interval + @time_info = "at #{@time} or in #{@interval} seconds" + end + match do |actual| schedule_queue_for(actual).any? do |entry| class_matches = entry[:class].to_s == actual.to_s args_match = match_args(expected_args, entry[:args]) - time_matches = if @time + time_matches = if @time and @interval + (entry[:time].to_i == @time.to_i) or (entry[:time].to_i == (entry[:stored_at] + @interval).to_i) + elsif @time entry[:time].to_i == @time.to_i elsif @interval entry[:time].to_i == (entry[:stored_at] + @interval).to_i diff --git a/spec/resque_spec/matchers_spec.rb b/spec/resque_spec/matchers_spec.rb index 93dcbe9..994ccf4 100644 --- a/spec/resque_spec/matchers_spec.rb +++ b/spec/resque_spec/matchers_spec.rb @@ -455,6 +455,39 @@ end end + context "with #at_or_in(timestamp, interval)" do + let(:interval) { 10 * 60 } + let(:scheduled_at) { Time.now + 30 * 60 } + + context "scheduling with #enqueue_in" do + before(:each) do + Resque.enqueue_in(interval, Person, first_name, last_name) + end + + it "returns true if arguments, timestamp or interval matches positive expectation" do + Person.should have_scheduled(first_name, last_name).at_or_in(scheduled_at, interval) + end + + it "returns true if arguments, timestamp, and interval matches negative expectation" do + Person.should_not have_scheduled(first_name, last_name).at_or_in(Time.now + 60 * 60, 100 * 60) + end + end + + context "scheduling with #enqueue_at" do + before(:each) do + Resque.enqueue_at(scheduled_at, Person, first_name, last_name) + end + + it "returns true if arguments, timestamp or interval matches positive expectation" do + Person.should have_scheduled(first_name, last_name).at_or_in(scheduled_at, interval) + end + + it "returns true if arguments, timestamp, and interval matches negative expectation" do + Person.should_not have_scheduled(first_name, last_name).at_or_in(Time.now + 60 * 60, 100 * 60) + end + end + end + context "with #queue(queue_name)" do let(:interval) { 10 * 60 }