Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 120 lines (90 sloc) 4.087 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
0581928 @technoweenie remove excess whitespace
technoweenie authored
14
15 before :each do
75b49dc @tobi Initial extraction
authored
16 reset_db
0581928 @technoweenie remove excess whitespace
technoweenie authored
17 end
75b49dc @tobi Initial extraction
authored
18
19 it "should set run_at automatically" do
b9dc92b @tobi Don't save jobs with empty handlers
authored
20 Delayed::Job.create(:payload_object => ErrorJob.new ).run_at.should_not == nil
0581928 @technoweenie remove excess whitespace
technoweenie authored
21 end
75b49dc @tobi Initial extraction
authored
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
0581928 @technoweenie remove excess whitespace
technoweenie authored
26
75b49dc @tobi Initial extraction
authored
27 it "should increase count after enqueuing items" do
0581928 @technoweenie remove excess whitespace
technoweenie authored
28 Delayed::Job.enqueue SimpleJob.new
75b49dc @tobi Initial extraction
authored
29 Delayed::Job.count.should == 1
30 end
0581928 @technoweenie remove excess whitespace
technoweenie authored
31
32 it "should call perform on jobs when running work_off" do
75b49dc @tobi Initial extraction
authored
33 SimpleJob.runs.should == 0
0581928 @technoweenie remove excess whitespace
technoweenie authored
34
35 Delayed::Job.enqueue SimpleJob.new
8ec934e @tobi Removed the global lock. Jobs can now processed in parallel by runnin…
authored
36 Delayed::Job.work_off
0581928 @technoweenie remove excess whitespace
technoweenie authored
37
38 SimpleJob.runs.should == 1
39 end
40
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
42 Delayed::Job.enqueue ErrorJob.new
43 runner = Delayed::Job.work_off(1)
8ec934e @tobi Removed the global lock. Jobs can now processed in parallel by runnin…
authored
44
75b49dc @tobi Initial extraction
authored
45 job = Delayed::Job.find(:first)
46 job.last_error.should == 'did not work'
47 job.attempts.should == 1
0581928 @technoweenie remove excess whitespace
technoweenie authored
48 job.run_at.should > Time.now
49 job.run_at.should < Time.now + 6.minutes
50 end
51
75b49dc @tobi Initial extraction
authored
52 it "should raise an DeserializationError when the job class is totally unknown" do
53
54 job = Delayed::Job.new
55 job['handler'] = "--- !ruby/object:JobThatDoesNotExist {}"
56
0581928 @technoweenie remove excess whitespace
technoweenie authored
57 lambda { job.payload_object.perform }.should raise_error(Delayed::DeserializationError)
75b49dc @tobi Initial extraction
authored
58 end
59
60 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
61 job = Delayed::Job.new
75b49dc @tobi Initial extraction
authored
62 job['handler'] = "--- !ruby/object:JobThatDoesNotExist {}"
63
64 job.should_receive(:attempt_to_load).with('JobThatDoesNotExist').and_return(true)
0581928 @technoweenie remove excess whitespace
technoweenie authored
65
66 lambda { job.payload_object.perform }.should raise_error(Delayed::DeserializationError)
67 end
68
75b49dc @tobi Initial extraction
authored
69 it "should try include the namespace when loading unknown objects" do
0581928 @technoweenie remove excess whitespace
technoweenie authored
70 job = Delayed::Job.new
75b49dc @tobi Initial extraction
authored
71 job['handler'] = "--- !ruby/object:Delayed::JobThatDoesNotExist {}"
0581928 @technoweenie remove excess whitespace
technoweenie authored
72 job.should_receive(:attempt_to_load).with('Delayed::JobThatDoesNotExist').and_return(true)
73 lambda { job.payload_object.perform }.should raise_error(Delayed::DeserializationError)
74 end
75
75b49dc @tobi Initial extraction
authored
76 it "should also try to load structs when they are unknown (raises TypeError)" do
0581928 @technoweenie remove excess whitespace
technoweenie authored
77 job = Delayed::Job.new
75b49dc @tobi Initial extraction
authored
78 job['handler'] = "--- !ruby/struct:JobThatDoesNotExist {}"
79
80 job.should_receive(:attempt_to_load).with('JobThatDoesNotExist').and_return(true)
0581928 @technoweenie remove excess whitespace
technoweenie authored
81
82 lambda { job.payload_object.perform }.should raise_error(Delayed::DeserializationError)
83 end
84
75b49dc @tobi Initial extraction
authored
85 it "should try include the namespace when loading unknown structs" do
0581928 @technoweenie remove excess whitespace
technoweenie authored
86 job = Delayed::Job.new
75b49dc @tobi Initial extraction
authored
87 job['handler'] = "--- !ruby/struct:Delayed::JobThatDoesNotExist {}"
0581928 @technoweenie remove excess whitespace
technoweenie authored
88 job.should_receive(:attempt_to_load).with('Delayed::JobThatDoesNotExist').and_return(true)
89 lambda { job.payload_object.perform }.should raise_error(Delayed::DeserializationError)
90 end
91
5998931 Renamed locked_until to locked_at. We now store when we start a given…
Tobias Lütke authored
92 describe "when another worker is already performing an task, it" do
0581928 @technoweenie remove excess whitespace
technoweenie authored
93
8ec934e @tobi Removed the global lock. Jobs can now processed in parallel by runnin…
authored
94 before :each do
95 Delayed::Job.worker_name = 'worker1'
5998931 Renamed locked_until to locked_at. We now store when we start a given…
Tobias Lütke authored
96 @job = Delayed::Job.create :payload_object => SimpleJob.new, :locked_by => 'worker1', :locked_at => Time.now.utc
8ec934e @tobi Removed the global lock. Jobs can now processed in parallel by runnin…
authored
97 end
0581928 @technoweenie remove excess whitespace
technoweenie authored
98
99 it "should not allow a second worker to get exclusive access" do
100 lambda { @job.lock_exclusively! 4.hours, 'worker2' }.should raise_error(Delayed::Job::LockError)
101 end
102
103 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
104 @job.locked_at = 5.hours.ago
105 @job.save
106
0581928 @technoweenie remove excess whitespace
technoweenie authored
107 @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
108 @job.reload
109 @job.locked_by.should == 'worker2'
110 @job.locked_at.should > 1.minute.ago
111 end
8ec934e @tobi Removed the global lock. Jobs can now processed in parallel by runnin…
authored
112
0581928 @technoweenie remove excess whitespace
technoweenie authored
113 it "should be able to get exclusive access again when the worker name is the same" do
114 @job.lock_exclusively! Time.now + 20, 'worker1'
5998931 Renamed locked_until to locked_at. We now store when we start a given…
Tobias Lütke authored
115 @job.lock_exclusively! Time.now + 21, 'worker1'
0581928 @technoweenie remove excess whitespace
technoweenie authored
116 @job.lock_exclusively! Time.now + 22, 'worker1'
117 end
8ec934e @tobi Removed the global lock. Jobs can now processed in parallel by runnin…
authored
118 end
75b49dc @tobi Initial extraction
authored
119
0581928 @technoweenie remove excess whitespace
technoweenie authored
120 end
Something went wrong with that request. Please try again.