merged cont.
[opensuse:yast-rest-service.git] / webservice / vendor / plugins / delayed_job / spec / delayed_method_spec.rb
1 require File.dirname(__FILE__) + '/database'
2
3 class SimpleJob
4   cattr_accessor :runs; self.runs = 0
5   def perform; @@runs += 1; end
6 end
7
8 class RandomRubyObject  
9   def say_hello
10     'hello'
11   end
12 end
13
14 class ErrorObject
15
16   def throw
17     raise ActiveRecord::RecordNotFound, '...'
18     false
19   end
20
21 end
22
23 class StoryReader
24
25   def read(story)
26     "Epilog: #{story.tell}"
27   end
28
29 end
30
31 class StoryReader
32
33   def read(story)
34     "Epilog: #{story.tell}"
35   end
36
37 end
38
39 describe 'random ruby objects' do
40   before       { Delayed::Job.delete_all }
41
42   it "should respond_to :send_later method" do
43
44     RandomRubyObject.new.respond_to?(:send_later)
45
46   end
47
48   it "should raise a ArgumentError if send_later is called but the target method doesn't exist" do
49     lambda { RandomRubyObject.new.send_later(:method_that_deos_not_exist) }.should raise_error(NoMethodError)
50   end
51
52   it "should add a new entry to the job table when send_later is called on it" do
53     Delayed::Job.count.should == 0
54
55     RandomRubyObject.new.send_later(:to_s)
56
57     Delayed::Job.count.should == 1
58   end
59
60   it "should add a new entry to the job table when send_later is called on the class" do
61     Delayed::Job.count.should == 0
62
63     RandomRubyObject.send_later(:to_s)
64
65     Delayed::Job.count.should == 1
66   end
67
68   it "should run get the original method executed when the job is performed" do
69
70     RandomRubyObject.new.send_later(:say_hello)
71
72     Delayed::Job.count.should == 1
73   end
74
75   it "should ignore ActiveRecord::RecordNotFound errors because they are permanent" do
76
77     ErrorObject.new.send_later(:throw)
78
79     Delayed::Job.count.should == 1
80
81     Delayed::Job.reserve_and_run_one_job
82
83     Delayed::Job.count.should == 0
84
85   end
86
87   it "should store the object as string if its an active record" do
88     story = Story.create :text => 'Once upon...'
89     story.send_later(:tell)
90
91     job =  Delayed::Job.find(:first)
92     job.payload_object.class.should   == Delayed::PerformableMethod
93     job.payload_object.object.should  == "AR:Story:#{story.id}"
94     job.payload_object.method.should  == :tell
95     job.payload_object.args.should    == []
96     job.payload_object.perform.should == 'Once upon...'
97   end
98
99   it "should store arguments as string if they an active record" do
100
101     story = Story.create :text => 'Once upon...'
102
103     reader = StoryReader.new
104     reader.send_later(:read, story)
105
106     job =  Delayed::Job.find(:first)
107     job.payload_object.class.should   == Delayed::PerformableMethod
108     job.payload_object.method.should  == :read
109     job.payload_object.args.should    == ["AR:Story:#{story.id}"]
110     job.payload_object.perform.should == 'Epilog: Once upon...'
111   end                 
112   
113   it "should call send later on methods which are wrapped with handle_asynchronously" do
114     story = Story.create :text => 'Once upon...'
115   
116     Delayed::Job.count.should == 0
117   
118     story.whatever(1, 5)
119   
120     Delayed::Job.count.should == 1
121     job =  Delayed::Job.find(:first)
122     job.payload_object.class.should   == Delayed::PerformableMethod
123     job.payload_object.method.should  == :whatever_without_send_later
124     job.payload_object.args.should    == [1, 5]
125     job.payload_object.perform.should == 'Once upon...'
126   end
127
128 end