From 59ef94f5cd2a49d2e79d8aca98d6f5595f51053c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20Ju=C3=A1rez?= Date: Tue, 27 Sep 2016 14:11:55 -0500 Subject: [PATCH 1/2] Add at_or_in matcher --- README.md | 17 +++++++++++++++++ lib/resque_spec/matchers.rb | 10 +++++++++- spec/resque_spec/matchers_spec.rb | 21 +++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) 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..9d6141a 100644 --- a/spec/resque_spec/matchers_spec.rb +++ b/spec/resque_spec/matchers_spec.rb @@ -455,6 +455,27 @@ end end + context "with #at_or_in(timestamp, interval)" do + let(:interval) { 10 * 60 } + let(:scheduled_at) { Time.now + 30 * 60 } + + before(:each) do + if [true, false].sample + Resque.enqueue_in(interval, Person, first_name, last_name) + else + Resque.enqueue_at(scheduled_at, Person, first_name, last_name) + end + 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 "with #queue(queue_name)" do let(:interval) { 10 * 60 } From ebaffc26f2fe3d893cbd49433963bdaff61fc9f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20Ju=C3=A1rez?= Date: Mon, 24 Oct 2016 16:41:19 -0500 Subject: [PATCH 2/2] Remove sample method from specs, added deterministic tests --- spec/resque_spec/matchers_spec.rb | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/spec/resque_spec/matchers_spec.rb b/spec/resque_spec/matchers_spec.rb index 9d6141a..994ccf4 100644 --- a/spec/resque_spec/matchers_spec.rb +++ b/spec/resque_spec/matchers_spec.rb @@ -459,20 +459,32 @@ let(:interval) { 10 * 60 } let(:scheduled_at) { Time.now + 30 * 60 } - before(:each) do - if [true, false].sample + context "scheduling with #enqueue_in" do + before(:each) do Resque.enqueue_in(interval, Person, first_name, last_name) - else - Resque.enqueue_at(scheduled_at, Person, first_name, last_name) end - 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) + 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 - 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) + 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