Skip to content
This repository
Newer
Older
100644 137 lines (98 sloc) 5.296 kb
75b49dc1 »
2008-02-17 Initial extraction
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 ErrorJob
9 cattr_accessor :runs; self.runs = 0
10 def perform; raise 'did not work'; end
11 end
12
13 describe Delayed::Job do
14
15 before :each do
16 reset_db
17 end
18
19 it "should set run_at automatically" do
b9dc92b9 »
2008-03-24 Don't save jobs with empty handlers
20 Delayed::Job.create(:payload_object => ErrorJob.new ).run_at.should_not == nil
75b49dc1 »
2008-02-17 Initial extraction
21 end
22
23 it "should raise ArgumentError when handler doesn't respond_to :perform" do
24 lambda { Delayed::Job.enqueue(Object.new) }.should raise_error(ArgumentError)
25 end
26
27 it "should increase count after enqueuing items" do
28 Delayed::Job.enqueue SimpleJob.new
29 Delayed::Job.count.should == 1
30 end
31
32 it "should call perform on jobs when running work_off" do
33 SimpleJob.runs.should == 0
34
35 Delayed::Job.enqueue SimpleJob.new
8ec934ee »
2008-03-23 Removed the global lock. Jobs can now processed in parallel by runnin…
36 Delayed::Job.work_off
75b49dc1 »
2008-02-17 Initial extraction
37
38 SimpleJob.runs.should == 1
39 end
40
2309a943 »
2008-02-17 Increase job rescheduling timeouts
41 it "should re-schedule by about 1 second at first and increment this more and more minutes when it fails to execute properly" do
75b49dc1 »
2008-02-17 Initial extraction
42 Delayed::Job.enqueue ErrorJob.new
a6e374e0 »
2008-07-23 Fixed a nasty bug that allowed several job runners to get the exclusi…
43 Delayed::Job.work_off(1)
44
45
46 job = Delayed::Job.find(:first)
47
48 job.last_error.should == 'did not work'
49 job.attempts.should == 1
50
51 job.run_at.should > Delayed::Job.db_time_now - 10.minutes
52 job.run_at.should < Delayed::Job.db_time_now + 10.minutes
53 end
54
75b49dc1 »
2008-02-17 Initial extraction
55
56 it "should raise an DeserializationError when the job class is totally unknown" do
57
58 job = Delayed::Job.new
59 job['handler'] = "--- !ruby/object:JobThatDoesNotExist {}"
60
8ec934ee »
2008-03-23 Removed the global lock. Jobs can now processed in parallel by runnin…
61 lambda { job.payload_object.perform }.should raise_error(Delayed::DeserializationError)
75b49dc1 »
2008-02-17 Initial extraction
62 end
63
64 it "should try to load the class when it is unknown at the time of the deserialization" do
65 job = Delayed::Job.new
66 job['handler'] = "--- !ruby/object:JobThatDoesNotExist {}"
67
68 job.should_receive(:attempt_to_load).with('JobThatDoesNotExist').and_return(true)
69
8ec934ee »
2008-03-23 Removed the global lock. Jobs can now processed in parallel by runnin…
70 lambda { job.payload_object.perform }.should raise_error(Delayed::DeserializationError)
75b49dc1 »
2008-02-17 Initial extraction
71 end
72
73 it "should try include the namespace when loading unknown objects" do
74 job = Delayed::Job.new
75 job['handler'] = "--- !ruby/object:Delayed::JobThatDoesNotExist {}"
76 job.should_receive(:attempt_to_load).with('Delayed::JobThatDoesNotExist').and_return(true)
8ec934ee »
2008-03-23 Removed the global lock. Jobs can now processed in parallel by runnin…
77 lambda { job.payload_object.perform }.should raise_error(Delayed::DeserializationError)
75b49dc1 »
2008-02-17 Initial extraction
78 end
79
80
81 it "should also try to load structs when they are unknown (raises TypeError)" do
82 job = Delayed::Job.new
83 job['handler'] = "--- !ruby/struct:JobThatDoesNotExist {}"
84
85 job.should_receive(:attempt_to_load).with('JobThatDoesNotExist').and_return(true)
86
8ec934ee »
2008-03-23 Removed the global lock. Jobs can now processed in parallel by runnin…
87 lambda { job.payload_object.perform }.should raise_error(Delayed::DeserializationError)
75b49dc1 »
2008-02-17 Initial extraction
88 end
89
90 it "should try include the namespace when loading unknown structs" do
91 job = Delayed::Job.new
92 job['handler'] = "--- !ruby/struct:Delayed::JobThatDoesNotExist {}"
93 job.should_receive(:attempt_to_load).with('Delayed::JobThatDoesNotExist').and_return(true)
8ec934ee »
2008-03-23 Removed the global lock. Jobs can now processed in parallel by runnin…
94 lambda { job.payload_object.perform }.should raise_error(Delayed::DeserializationError)
75b49dc1 »
2008-02-17 Initial extraction
95 end
a6e374e0 »
2008-07-23 Fixed a nasty bug that allowed several job runners to get the exclusi…
96
97 it "should be removed if it failed more than MAX_ATTEMPTS times" do
98 @job = Delayed::Job.create :payload_object => SimpleJob.new, :attempts => 50
99 @job.should_receive(:destroy)
100 @job.reschedule 'FAIL'
101 end
8ec934ee »
2008-03-23 Removed the global lock. Jobs can now processed in parallel by runnin…
102
59989313 » Tobias Lütke
2008-06-21 Renamed locked_until to locked_at. We now store when we start a given…
103 describe "when another worker is already performing an task, it" do
8ec934ee »
2008-03-23 Removed the global lock. Jobs can now processed in parallel by runnin…
104
105 before :each do
106 Delayed::Job.worker_name = 'worker1'
a6e374e0 »
2008-07-23 Fixed a nasty bug that allowed several job runners to get the exclusi…
107 @job = Delayed::Job.create :payload_object => SimpleJob.new, :locked_by => 'worker1', :locked_at => Delayed::Job.db_time_now - 5.minutes
8ec934ee »
2008-03-23 Removed the global lock. Jobs can now processed in parallel by runnin…
108 end
109
59989313 » Tobias Lütke
2008-06-21 Renamed locked_until to locked_at. We now store when we start a given…
110 it "should not allow a second worker to get exclusive access" do
111 lambda { @job.lock_exclusively! 4.hours, 'worker2' }.should raise_error(Delayed::Job::LockError)
112 end
a6e374e0 »
2008-07-23 Fixed a nasty bug that allowed several job runners to get the exclusi…
113
114 it "should not allow a second worker to get exclusive access if the timeout has passed" do
115
116 @job.lock_exclusively! 1.minute, 'worker2'
117
118 end
59989313 » Tobias Lütke
2008-06-21 Renamed locked_until to locked_at. We now store when we start a given…
119
120 it "should be able to get access to the task if it was started more then max_age ago" do
121 @job.locked_at = 5.hours.ago
122 @job.save
123
124 @job.lock_exclusively! 4.hours, 'worker2'
125 @job.reload
126 @job.locked_by.should == 'worker2'
127 @job.locked_at.should > 1.minute.ago
128 end
8ec934ee »
2008-03-23 Removed the global lock. Jobs can now processed in parallel by runnin…
129
130 it "should be able to get exclusive access again when the worker name is the same" do
a6e374e0 »
2008-07-23 Fixed a nasty bug that allowed several job runners to get the exclusi…
131 @job.lock_exclusively! 5.minutes, 'worker1'
132 @job.lock_exclusively! 5.minutes, 'worker1'
133 @job.lock_exclusively! 5.minutes, 'worker1'
8ec934ee »
2008-03-23 Removed the global lock. Jobs can now processed in parallel by runnin…
134 end
135 end
a6e374e0 »
2008-07-23 Fixed a nasty bug that allowed several job runners to get the exclusi…
136
75b49dc1 »
2008-02-17 Initial extraction
137 end
138
139
140
141
142
143
144
145
146
147
148
149
Something went wrong with that request. Please try again.