forked from tobi/delayed_job
/
message_sending_spec.rb
89 lines (77 loc) · 2.5 KB
/
message_sending_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require 'spec_helper'
describe Delayed::MessageSending do
describe "handle_asynchronously" do
class Story < ActiveRecord::Base
def tell!(arg)
end
handle_asynchronously :tell!
end
it "should alias original method" do
Story.new.should respond_to(:tell_without_delay!)
Story.new.should respond_to(:tell_with_delay!)
end
it "should create a PerformableMethod" do
story = Story.create!
lambda {
job = story.tell!(1)
job.payload_object.class.should == Delayed::PerformableMethod
job.payload_object.method_name.should == :tell_without_delay!
job.payload_object.args.should == [1]
}.should change { Delayed::Job.count }
end
describe 'with options' do
class Fable
class << self
attr_accessor :importance
end
def tell
end
handle_asynchronously :tell, :priority => Proc.new { self.importance }
end
it 'should set the priority based on the Fable importance' do
Fable.importance = 10
job = Fable.new.tell
job.priority.should == 10
Fable.importance = 20
job = Fable.new.tell
job.priority.should == 20
end
describe 'using a proc with parament' do
class Yarn
attr_accessor :importance
def spin
end
handle_asynchronously :spin, :priority => Proc.new {|y| y.importance }
end
it 'should set the priority based on the Fable importance' do
job = Yarn.new.tap {|y| y.importance = 10 }.spin
job.priority.should == 10
job = Yarn.new.tap {|y| y.importance = 20 }.spin
job.priority.should == 20
end
end
end
end
context "delay" do
it "should create a new PerformableMethod job" do
lambda {
job = "hello".delay.count('l')
job.payload_object.class.should == Delayed::PerformableMethod
job.payload_object.method_name.should == :count
job.payload_object.args.should == ['l']
}.should change { Delayed::Job.count }.by(1)
end
it "should set default priority" do
Delayed::Worker.default_priority = 99
job = Object.delay.to_s
job.priority.should == 99
Delayed::Worker.default_priority = 0
end
it "should set job options" do
run_at = Time.parse('2010-05-03 12:55 AM')
job = Object.delay(:priority => 20, :run_at => run_at).to_s
job.run_at.should == run_at
job.priority.should == 20
end
end
end