Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 192 lines (140 sloc) 6.267 kB
75b49dc @tobi Initial extraction
authored
1 require File.dirname(__FILE__) + '/database'
2
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
0581928 @technoweenie remove excess whitespace
technoweenie authored
6 end
75b49dc @tobi Initial extraction
authored
7
8 class ErrorJob
0581928 @technoweenie remove excess whitespace
technoweenie authored
9 cattr_accessor :runs; self.runs = 0
10 def perform; raise 'did not work'; end
75b49dc @tobi Initial extraction
authored
11 end
12
13 describe Delayed::Job do
249c5a9 @tobi Added min/max priority levels for workers. This allows you to have de…
authored
14 before do
15 Delayed::Job.max_priority = nil
16 Delayed::Job.min_priority = nil
17
18 Delayed::Job.delete_all
19 end
20
21 before(:each) do
22 SimpleJob.runs = 0
23 end
75b49dc @tobi Initial extraction
authored
24
25 it "should set run_at automatically" do
b9dc92b @tobi Don't save jobs with empty handlers
authored
26 Delayed::Job.create(:payload_object => ErrorJob.new ).run_at.should_not == nil
0581928 @technoweenie remove excess whitespace
technoweenie authored
27 end
75b49dc @tobi Initial extraction
authored
28
29 it "should raise ArgumentError when handler doesn't respond_to :perform" do
30 lambda { Delayed::Job.enqueue(Object.new) }.should raise_error(ArgumentError)
31 end
0581928 @technoweenie remove excess whitespace
technoweenie authored
32
75b49dc @tobi Initial extraction
authored
33 it "should increase count after enqueuing items" do
0581928 @technoweenie remove excess whitespace
technoweenie authored
34 Delayed::Job.enqueue SimpleJob.new
75b49dc @tobi Initial extraction
authored
35 Delayed::Job.count.should == 1
36 end
0581928 @technoweenie remove excess whitespace
technoweenie authored
37
38 it "should call perform on jobs when running work_off" do
75b49dc @tobi Initial extraction
authored
39 SimpleJob.runs.should == 0
0581928 @technoweenie remove excess whitespace
technoweenie authored
40
41 Delayed::Job.enqueue SimpleJob.new
8ec934e @tobi Removed the global lock. Jobs can now processed in parallel by runnin…
authored
42 Delayed::Job.work_off
0581928 @technoweenie remove excess whitespace
technoweenie authored
43
44 SimpleJob.runs.should == 1
45 end
75b49dc @tobi Initial extraction
authored
46
2309a94 @tobi Increase job rescheduling timeouts
authored
47 it "should re-schedule by about 1 second at first and increment this more and more minutes when it fails to execute properly" do
0581928 @technoweenie remove excess whitespace
technoweenie authored
48 Delayed::Job.enqueue ErrorJob.new
0c084e5 @technoweenie merge
technoweenie authored
49 Delayed::Job.work_off(1)
8ec934e @tobi Removed the global lock. Jobs can now processed in parallel by runnin…
authored
50
75b49dc @tobi Initial extraction
authored
51 job = Delayed::Job.find(:first)
a6e374e @tobi Fixed a nasty bug that allowed several job runners to get the exclusi…
authored
52
75b49dc @tobi Initial extraction
authored
53 job.last_error.should == 'did not work'
54 job.attempts.should == 1
0c084e5 @technoweenie merge
technoweenie authored
55
56 job.run_at.should > Delayed::Job.db_time_now - 10.minutes
57 job.run_at.should < Delayed::Job.db_time_now + 10.minutes
0581928 @technoweenie remove excess whitespace
technoweenie authored
58 end
59
75b49dc @tobi Initial extraction
authored
60 it "should raise an DeserializationError when the job class is totally unknown" do
61
62 job = Delayed::Job.new
63 job['handler'] = "--- !ruby/object:JobThatDoesNotExist {}"
64
0581928 @technoweenie remove excess whitespace
technoweenie authored
65 lambda { job.payload_object.perform }.should raise_error(Delayed::DeserializationError)
75b49dc @tobi Initial extraction
authored
66 end
67
68 it "should try to load the class when it is unknown at the time of the deserialization" do
0581928 @technoweenie remove excess whitespace
technoweenie authored
69 job = Delayed::Job.new
75b49dc @tobi Initial extraction
authored
70 job['handler'] = "--- !ruby/object:JobThatDoesNotExist {}"
71
72 job.should_receive(:attempt_to_load).with('JobThatDoesNotExist').and_return(true)
0581928 @technoweenie remove excess whitespace
technoweenie authored
73
74 lambda { job.payload_object.perform }.should raise_error(Delayed::DeserializationError)
75 end
76
75b49dc @tobi Initial extraction
authored
77 it "should try include the namespace when loading unknown objects" do
0581928 @technoweenie remove excess whitespace
technoweenie authored
78 job = Delayed::Job.new
75b49dc @tobi Initial extraction
authored
79 job['handler'] = "--- !ruby/object:Delayed::JobThatDoesNotExist {}"
0581928 @technoweenie remove excess whitespace
technoweenie authored
80 job.should_receive(:attempt_to_load).with('Delayed::JobThatDoesNotExist').and_return(true)
81 lambda { job.payload_object.perform }.should raise_error(Delayed::DeserializationError)
82 end
83
75b49dc @tobi Initial extraction
authored
84 it "should also try to load structs when they are unknown (raises TypeError)" do
0581928 @technoweenie remove excess whitespace
technoweenie authored
85 job = Delayed::Job.new
75b49dc @tobi Initial extraction
authored
86 job['handler'] = "--- !ruby/struct:JobThatDoesNotExist {}"
87
88 job.should_receive(:attempt_to_load).with('JobThatDoesNotExist').and_return(true)
0581928 @technoweenie remove excess whitespace
technoweenie authored
89
90 lambda { job.payload_object.perform }.should raise_error(Delayed::DeserializationError)
91 end
92
75b49dc @tobi Initial extraction
authored
93 it "should try include the namespace when loading unknown structs" do
0581928 @technoweenie remove excess whitespace
technoweenie authored
94 job = Delayed::Job.new
75b49dc @tobi Initial extraction
authored
95 job['handler'] = "--- !ruby/struct:Delayed::JobThatDoesNotExist {}"
0c084e5 @technoweenie merge
technoweenie authored
96
0581928 @technoweenie remove excess whitespace
technoweenie authored
97 job.should_receive(:attempt_to_load).with('Delayed::JobThatDoesNotExist').and_return(true)
98 lambda { job.payload_object.perform }.should raise_error(Delayed::DeserializationError)
75b49dc @tobi Initial extraction
authored
99 end
a6e374e @tobi Fixed a nasty bug that allowed several job runners to get the exclusi…
authored
100
101 it "should be removed if it failed more than MAX_ATTEMPTS times" do
102 @job = Delayed::Job.create :payload_object => SimpleJob.new, :attempts => 50
0c084e5 @technoweenie merge
technoweenie authored
103 @job.should_receive(:destroy)
104 @job.reschedule 'FAIL'
0581928 @technoweenie remove excess whitespace
technoweenie authored
105 end
106
5998931 Renamed locked_until to locked_at. We now store when we start a given…
Tobias Lütke authored
107 describe "when another worker is already performing an task, it" do
0581928 @technoweenie remove excess whitespace
technoweenie authored
108
8ec934e @tobi Removed the global lock. Jobs can now processed in parallel by runnin…
authored
109 before :each do
110 Delayed::Job.worker_name = 'worker1'
a6e374e @tobi Fixed a nasty bug that allowed several job runners to get the exclusi…
authored
111 @job = Delayed::Job.create :payload_object => SimpleJob.new, :locked_by => 'worker1', :locked_at => Delayed::Job.db_time_now - 5.minutes
8ec934e @tobi Removed the global lock. Jobs can now processed in parallel by runnin…
authored
112 end
0581928 @technoweenie remove excess whitespace
technoweenie authored
113
114 it "should not allow a second worker to get exclusive access" do
115 lambda { @job.lock_exclusively! 4.hours, 'worker2' }.should raise_error(Delayed::Job::LockError)
5998931 Renamed locked_until to locked_at. We now store when we start a given…
Tobias Lütke authored
116 end
a6e374e @tobi Fixed a nasty bug that allowed several job runners to get the exclusi…
authored
117
0c084e5 @technoweenie merge
technoweenie authored
118 it "should not allow a second worker to get exclusive access if the timeout has passed" do
a6e374e @tobi Fixed a nasty bug that allowed several job runners to get the exclusi…
authored
119
120 @job.lock_exclusively! 1.minute, 'worker2'
121
122 end
5998931 Renamed locked_until to locked_at. We now store when we start a given…
Tobias Lütke authored
123
0581928 @technoweenie remove excess whitespace
technoweenie authored
124 it "should be able to get access to the task if it was started more then max_age ago" do
5998931 Renamed locked_until to locked_at. We now store when we start a given…
Tobias Lütke authored
125 @job.locked_at = 5.hours.ago
126 @job.save
127
0581928 @technoweenie remove excess whitespace
technoweenie authored
128 @job.lock_exclusively! 4.hours, 'worker2'
5998931 Renamed locked_until to locked_at. We now store when we start a given…
Tobias Lütke authored
129 @job.reload
130 @job.locked_by.should == 'worker2'
131 @job.locked_at.should > 1.minute.ago
132 end
8ec934e @tobi Removed the global lock. Jobs can now processed in parallel by runnin…
authored
133
75b49dc @tobi Initial extraction
authored
134
8ec934e @tobi Removed the global lock. Jobs can now processed in parallel by runnin…
authored
135 it "should be able to get exclusive access again when the worker name is the same" do
a6e374e @tobi Fixed a nasty bug that allowed several job runners to get the exclusi…
authored
136 @job.lock_exclusively! 5.minutes, 'worker1'
0c084e5 @technoweenie merge
technoweenie authored
137 @job.lock_exclusively! 5.minutes, 'worker1'
138 @job.lock_exclusively! 5.minutes, 'worker1'
8ec934e @tobi Removed the global lock. Jobs can now processed in parallel by runnin…
authored
139 end
249c5a9 @tobi Added min/max priority levels for workers. This allows you to have de…
authored
140 end
141
142 context "worker prioritization" do
143
144 before(:each) do
145 Delayed::Job.max_priority = nil
146 Delayed::Job.min_priority = nil
147 end
148
149 it "should only work_off jobs that are >= min_priority" do
150 Delayed::Job.min_priority = -5
151 Delayed::Job.max_priority = 5
152 SimpleJob.runs.should == 0
153
154 Delayed::Job.enqueue SimpleJob.new, -10
155 Delayed::Job.enqueue SimpleJob.new, 0
156 Delayed::Job.work_off
157
158 SimpleJob.runs.should == 1
159 end
160
161 it "should only work_off jobs that are <= max_priority" do
162 Delayed::Job.min_priority = -5
163 Delayed::Job.max_priority = 5
164 SimpleJob.runs.should == 0
165
166 Delayed::Job.enqueue SimpleJob.new, 10
167 Delayed::Job.enqueue SimpleJob.new, 0
168
169 Delayed::Job.work_off
170
171 SimpleJob.runs.should == 1
172 end
173
8ec934e @tobi Removed the global lock. Jobs can now processed in parallel by runnin…
authored
174 end
249c5a9 @tobi Added min/max priority levels for workers. This allows you to have de…
authored
175
4b9b079 @rares active record changes and fixing some small bugs.
rares authored
176 context "when retreiving jobs" do
177 before(:each) do
178 @simple_job = SimpleJob.new
179 @job = Delayed::Job.create :payload_object => @simple_job, :locked_by => 'worker1', :locked_at => Delayed::Job.db_time_now - 5.minutes
180 end
181
182 it "should return jobs that haven't been processed yet" do
183 SimpleJob.runs.should == 0
184 # Delayed::Job.should_receive(:find_available).once.with(5).and_return([@job])
185 Delayed::Job.should_receive(:reserve).once.and_yield(@job.payload_object)
186 Delayed::Job.work_off(1)
187 SimpleJob.runs.should == 1
188 end
189
190 end
191
0581928 @technoweenie remove excess whitespace
technoweenie authored
192 end
Something went wrong with that request. Please try again.