Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add at_or_in matcher #125

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion lib/resque_spec/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions spec/resque_spec/matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down