Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 143 lines (98 sloc) 3.551 kb
75b49dc @tobi Initial extraction
authored
1 require File.dirname(__FILE__) + '/database'
b9dc92b @tobi Don't save jobs with empty handlers
authored
2
75b49dc @tobi Initial extraction
authored
3 class SimpleJob
0581928 @technoweenie remove excess whitespace
technoweenie authored
4 cattr_accessor :runs; self.runs = 0
75b49dc @tobi Initial extraction
authored
5 def perform; @@runs += 1; end
6 end
7
8 class RandomRubyObject
9 def say_hello
10 'hello'
0581928 @technoweenie remove excess whitespace
technoweenie authored
11 end
12 end
b9dc92b @tobi Don't save jobs with empty handlers
authored
13
a67dc38 make send_later work for module methods
helbig@mediapeers.com authored
14 module RandomModule
15 def self.say_hello
16 'hello'
17 end
18 end
19
b9dc92b @tobi Don't save jobs with empty handlers
authored
20 class ErrorObject
0581928 @technoweenie remove excess whitespace
technoweenie authored
21
b9dc92b @tobi Don't save jobs with empty handlers
authored
22 def throw
0581928 @technoweenie remove excess whitespace
technoweenie authored
23 raise ActiveRecord::RecordNotFound, '...'
b9dc92b @tobi Don't save jobs with empty handlers
authored
24 false
25 end
0581928 @technoweenie remove excess whitespace
technoweenie authored
26
27 end
75b49dc @tobi Initial extraction
authored
28
29 class StoryReader
0581928 @technoweenie remove excess whitespace
technoweenie authored
30
75b49dc @tobi Initial extraction
authored
31 def read(story)
0581928 @technoweenie remove excess whitespace
technoweenie authored
32 "Epilog: #{story.tell}"
75b49dc @tobi Initial extraction
authored
33 end
0581928 @technoweenie remove excess whitespace
technoweenie authored
34
75b49dc @tobi Initial extraction
authored
35 end
36
b9dc92b @tobi Don't save jobs with empty handlers
authored
37 class StoryReader
0581928 @technoweenie remove excess whitespace
technoweenie authored
38
b9dc92b @tobi Don't save jobs with empty handlers
authored
39 def read(story)
0581928 @technoweenie remove excess whitespace
technoweenie authored
40 "Epilog: #{story.tell}"
b9dc92b @tobi Don't save jobs with empty handlers
authored
41 end
0581928 @technoweenie remove excess whitespace
technoweenie authored
42
b9dc92b @tobi Don't save jobs with empty handlers
authored
43 end
75b49dc @tobi Initial extraction
authored
44
45 describe 'random ruby objects' do
05e95f8 @technoweenie make tests run over 2x faster by creating the test db just once
technoweenie authored
46 before { Delayed::Job.delete_all }
75b49dc @tobi Initial extraction
authored
47
48 it "should respond_to :send_later method" do
0581928 @technoweenie remove excess whitespace
technoweenie authored
49
50 RandomRubyObject.new.respond_to?(:send_later)
51
52 end
53
75b49dc @tobi Initial extraction
authored
54 it "should raise a ArgumentError if send_later is called but the target method doesn't exist" do
55 lambda { RandomRubyObject.new.send_later(:method_that_deos_not_exist) }.should raise_error(NoMethodError)
56 end
0581928 @technoweenie remove excess whitespace
technoweenie authored
57
58 it "should add a new entry to the job table when send_later is called on it" do
75b49dc @tobi Initial extraction
authored
59 Delayed::Job.count.should == 0
0581928 @technoweenie remove excess whitespace
technoweenie authored
60
75b49dc @tobi Initial extraction
authored
61 RandomRubyObject.new.send_later(:to_s)
62
63 Delayed::Job.count.should == 1
64 end
0581928 @technoweenie remove excess whitespace
technoweenie authored
65
d76d34a @technoweenie allow Delayed::PerformableMethod to work with class methods. Foo.send_l...
technoweenie authored
66 it "should add a new entry to the job table when send_later is called on the class" do
67 Delayed::Job.count.should == 0
68
69 RandomRubyObject.send_later(:to_s)
70
71 Delayed::Job.count.should == 1
72 end
73
75b49dc @tobi Initial extraction
authored
74 it "should run get the original method executed when the job is performed" do
0581928 @technoweenie remove excess whitespace
technoweenie authored
75
75b49dc @tobi Initial extraction
authored
76 RandomRubyObject.new.send_later(:say_hello)
0581928 @technoweenie remove excess whitespace
technoweenie authored
77
78 Delayed::Job.count.should == 1
79 end
b9dc92b @tobi Don't save jobs with empty handlers
authored
80
81 it "should ignore ActiveRecord::RecordNotFound errors because they are permanent" do
82
0581928 @technoweenie remove excess whitespace
technoweenie authored
83 ErrorObject.new.send_later(:throw)
84
85 Delayed::Job.count.should == 1
86
266fc15 @dmag refactor job object
dmag authored
87 Delayed::Job.reserve_and_run_one_job
0581928 @technoweenie remove excess whitespace
technoweenie authored
88
266fc15 @dmag refactor job object
dmag authored
89 Delayed::Job.count.should == 0
0581928 @technoweenie remove excess whitespace
technoweenie authored
90
91 end
92
93 it "should store the object as string if its an active record" do
94 story = Story.create :text => 'Once upon...'
95 story.send_later(:tell)
96
8ec934e @tobi Removed the global lock. Jobs can now processed in parallel by running m...
authored
97 job = Delayed::Job.find(:first)
98 job.payload_object.class.should == Delayed::PerformableMethod
05e95f8 @technoweenie make tests run over 2x faster by creating the test db just once
technoweenie authored
99 job.payload_object.object.should == "AR:Story:#{story.id}"
8ec934e @tobi Removed the global lock. Jobs can now processed in parallel by running m...
authored
100 job.payload_object.method.should == :tell
0581928 @technoweenie remove excess whitespace
technoweenie authored
101 job.payload_object.args.should == []
8ec934e @tobi Removed the global lock. Jobs can now processed in parallel by running m...
authored
102 job.payload_object.perform.should == 'Once upon...'
0581928 @technoweenie remove excess whitespace
technoweenie authored
103 end
104
75b49dc @tobi Initial extraction
authored
105 it "should store arguments as string if they an active record" do
0581928 @technoweenie remove excess whitespace
technoweenie authored
106
107 story = Story.create :text => 'Once upon...'
108
109 reader = StoryReader.new
75b49dc @tobi Initial extraction
authored
110 reader.send_later(:read, story)
0581928 @technoweenie remove excess whitespace
technoweenie authored
111
8ec934e @tobi Removed the global lock. Jobs can now processed in parallel by running m...
authored
112 job = Delayed::Job.find(:first)
113 job.payload_object.class.should == Delayed::PerformableMethod
114 job.payload_object.method.should == :read
05e95f8 @technoweenie make tests run over 2x faster by creating the test db just once
technoweenie authored
115 job.payload_object.args.should == ["AR:Story:#{story.id}"]
0581928 @technoweenie remove excess whitespace
technoweenie authored
116 job.payload_object.perform.should == 'Epilog: Once upon...'
dbc5e37 @tobi Added Module#handle_asynchronously that allows you to mark methods for a...
authored
117 end
118
a67dc38 make send_later work for module methods
helbig@mediapeers.com authored
119 it "should store the object as string if it's a module" do
120 RandomModule.send_later(:say_hello)
121 job = Delayed::Job.find(:first)
122 job.payload_object.method.should == :say_hello
123 job.payload_object.object.should == "CLASS:RandomModule"
124 job.payload_object.perform.should == 'hello'
125 end
126
dbc5e37 @tobi Added Module#handle_asynchronously that allows you to mark methods for a...
authored
127 it "should call send later on methods which are wrapped with handle_asynchronously" do
128 story = Story.create :text => 'Once upon...'
129
130 Delayed::Job.count.should == 0
131
132 story.whatever(1, 5)
133
134 Delayed::Job.count.should == 1
135 job = Delayed::Job.find(:first)
136 job.payload_object.class.should == Delayed::PerformableMethod
137 job.payload_object.method.should == :whatever_without_send_later
138 job.payload_object.args.should == [1, 5]
139 job.payload_object.perform.should == 'Once upon...'
75b49dc @tobi Initial extraction
authored
140 end
0581928 @technoweenie remove excess whitespace
technoweenie authored
141
266fc15 @dmag refactor job object
dmag authored
142 end
Something went wrong with that request. Please try again.