Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
181 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'resque_spec' | ||
|
||
module ResqueSpec | ||
|
||
def scheduled?(klass, time, *args) | ||
schedule_for(klass).any? {|entry| entry[:klass] == klass && entry[:time] == time && entry[:args] == args} | ||
end | ||
|
||
def scheduled_anytime?(klass, *args) | ||
schedule_for(klass).any? {|entry| entry[:klass] == klass && entry[:args] == args} | ||
end | ||
|
||
def schedule_for(klass) | ||
name = queue_name(klass).to_s << "_scheduled" | ||
queues[name] | ||
end | ||
|
||
module ResqueScheduler | ||
def enqueue_at(time, klass, *args) | ||
ResqueSpec.schedule_for(klass) << {:klass => klass, :time => time, :args => args} | ||
end | ||
end | ||
end | ||
|
||
Resque.extend(ResqueSpec::ResqueScheduler) | ||
|
||
Spec::Matchers.define :have_scheduled do |*expected_args| | ||
match do |actual| | ||
ResqueSpec.scheduled_anytime?(actual, *expected_args) | ||
end | ||
|
||
failure_message_for_should do |actual| | ||
"expected that #{actual} would have [#{expected_args.join(', ')}] queued" | ||
end | ||
|
||
failure_message_for_should_not do |actual| | ||
"expected that #{actual} would not have [#{expected_args.join(', ')}] queued" | ||
end | ||
|
||
description do | ||
"have scheduled arguments" | ||
end | ||
end | ||
|
||
Spec::Matchers.define :have_scheduled_at do |*expected_args| | ||
match do |actual| | ||
ResqueSpec.scheduled?(actual, *expected_args) | ||
end | ||
|
||
failure_message_for_should do |actual| | ||
"expected that #{actual} would have [#{expected_args.join(', ')}] queued" | ||
end | ||
|
||
failure_message_for_should_not do |actual| | ||
"expected that #{actual} would not have [#{expected_args.join(', ')}] queued" | ||
end | ||
|
||
description do | ||
"have scheduled at the given time the arguments" | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
require 'spec_helper' | ||
|
||
describe "ResqueSchedulerSpec" do | ||
before do | ||
ResqueSpec.reset! | ||
end | ||
|
||
let(:first_name) { 'Les' } | ||
let(:last_name) { 'Hill' } | ||
let(:scheduled_at) { Time.now + 5 * 60 } | ||
|
||
describe "scheduled?" do | ||
it "returns true if the arguments were queued" do | ||
Resque.enqueue_at(scheduled_at, Person, first_name, last_name) | ||
ResqueSpec.scheduled?(Person, scheduled_at, first_name, last_name).should be | ||
end | ||
|
||
it "returns false if the arguments were not queued" do | ||
ResqueSpec.scheduled?(Person, scheduled_at, first_name, last_name).should_not be | ||
end | ||
end | ||
|
||
describe "scheduled_anytime?" do | ||
it "returns true if the arguments were queued" do | ||
Resque.enqueue_at(scheduled_at, Person, first_name, last_name) | ||
ResqueSpec.scheduled_anytime?(Person, first_name, last_name).should be | ||
end | ||
|
||
it "returns false if the arguments were not queued" do | ||
ResqueSpec.scheduled_anytime?(Person, first_name, last_name).should_not be | ||
end | ||
end | ||
|
||
describe "#schedule_for" do | ||
it "raises if there is no schedule queue defined for a class" do | ||
expect do | ||
ResqueSpec.schedule_for(Address) | ||
end.should raise_exception(::Resque::NoQueueError) | ||
end | ||
|
||
it "recognizes a queue defined as a class instance variable" do | ||
expect do | ||
ResqueSpec.schedule_for(Person) | ||
end.should_not raise_exception(::Resque::NoQueueError) | ||
end | ||
|
||
it "recognizes a queue defined as a class method" do | ||
expect do | ||
ResqueSpec.schedule_for(Account) | ||
end.should_not raise_exception(::Resque::NoQueueError) | ||
end | ||
|
||
it "has an empty array if nothing queued for a class" do | ||
ResqueSpec.schedule_for(Person).should == [] | ||
end | ||
|
||
it "allows additions" do | ||
ResqueSpec.schedule_for(Person) << 'queued' | ||
ResqueSpec.schedule_for(Person).should_not be_empty | ||
end | ||
end | ||
|
||
describe "Resque" do | ||
describe "#enqueue_at" do | ||
|
||
before do | ||
Resque.enqueue_at(scheduled_at, Person, first_name, last_name) | ||
end | ||
|
||
it "adds to the scheduled queue hash" do | ||
ResqueSpec.schedule_for(Person).should_not be_empty | ||
end | ||
|
||
it "sets the klass on the queue" do | ||
ResqueSpec.schedule_for(Person).first.should include(:klass => Person) | ||
end | ||
|
||
it "sets the arguments on the queue" do | ||
ResqueSpec.schedule_for(Person).first.should include(:args => [first_name, last_name]) | ||
end | ||
|
||
it "sets the time on the scheduled queue" do | ||
ResqueSpec.schedule_for(Person).first.should include(:time => scheduled_at) | ||
end | ||
|
||
end | ||
end | ||
|
||
context "Matchers" do | ||
before do | ||
Resque.enqueue_at(scheduled_at, Person, first_name, last_name) | ||
end | ||
|
||
describe "#have_scheduled_at" do | ||
it "returns true if the arguments are found in the queue" do | ||
Person.should have_scheduled_at(scheduled_at, first_name, last_name) | ||
end | ||
|
||
it "returns false if the arguments are not found in the queue" do | ||
Person.should_not have_scheduled_at(scheduled_at, last_name, first_name) | ||
end | ||
end | ||
|
||
describe "#have_scheduled" do | ||
it "returns true if the arguments are found in the queue" do | ||
Person.should have_scheduled(first_name, last_name) | ||
end | ||
|
||
it "returns false if the arguments are not found in the queue" do | ||
Person.should_not have_scheduled(last_name, first_name) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters