Skip to content
This repository
Newer
Older
100644 110 lines (81 sloc) 3.835 kb
75b49dc1 »
2008-02-17 Initial extraction
1 require File.dirname(__FILE__) + '/database'
2
3
4 class SimpleJob
5 cattr_accessor :runs; self.runs = 0
6 def perform; @@runs += 1; end
7 end
8
9 class ErrorJob
10 cattr_accessor :runs; self.runs = 0
11 def perform; raise 'did not work'; end
12 end
13
14 describe Delayed::Job do
15
16 before :each do
17 reset_db
18 end
19
20 it "should set run_at automatically" do
21 Delayed::Job.create.run_at.should_not == nil
22 end
23
24 it "should raise ArgumentError when handler doesn't respond_to :perform" do
25 lambda { Delayed::Job.enqueue(Object.new) }.should raise_error(ArgumentError)
26 end
27
28 it "should increase count after enqueuing items" do
29 Delayed::Job.enqueue SimpleJob.new
30 Delayed::Job.count.should == 1
31 end
32
33 it "should return nil when peeking on empty table" do
34 Delayed::Job.peek.should == nil
35 end
36
37 it "should return a job when peeking a table with jobs in it" do
38 Delayed::Job.enqueue SimpleJob.new
39 Delayed::Job.peek.class.should == Delayed::Job
40 end
41
42 it "should return an array of jobs when peek is called with a count larger than zero" do
43 Delayed::Job.enqueue SimpleJob.new
44 Delayed::Job.peek(2).class.should == Array
45 end
46
47 it "should call perform on jobs when running work_off" do
48 SimpleJob.runs.should == 0
49
50 Delayed::Job.enqueue SimpleJob.new
51 Delayed::Job.work_off(1)
52
53 SimpleJob.runs.should == 1
54 end
55
56 it "should re-schedule by about 5 minutes when it fails to execute properly" do
57 Delayed::Job.enqueue ErrorJob.new
58 runner = Delayed::Job.work_off(1)
59 runner.success.should == 0
60 runner.failure.should == 1
61
62 job = Delayed::Job.find(:first)
63 job.last_error.should == 'did not work'
64 job.attempts.should == 1
65 job.run_at.should > Time.now + 4.minutes
66 job.run_at.should < Time.now + 6.minutes
67 end
68
69 it "should raise an DeserializationError when the job class is totally unknown" do
70
71 job = Delayed::Job.new
72 job['handler'] = "--- !ruby/object:JobThatDoesNotExist {}"
73
74 lambda { job.perform }.should raise_error(Delayed::DeserializationError)
75 end
76
77 it "should try to load the class when it is unknown at the time of the deserialization" do
78 job = Delayed::Job.new
79 job['handler'] = "--- !ruby/object:JobThatDoesNotExist {}"
80
81 job.should_receive(:attempt_to_load).with('JobThatDoesNotExist').and_return(true)
82
83 lambda { job.perform }.should raise_error(Delayed::DeserializationError)
84 end
85
86 it "should try include the namespace when loading unknown objects" do
87 job = Delayed::Job.new
88 job['handler'] = "--- !ruby/object:Delayed::JobThatDoesNotExist {}"
89 job.should_receive(:attempt_to_load).with('Delayed::JobThatDoesNotExist').and_return(true)
90 lambda { job.perform }.should raise_error(Delayed::DeserializationError)
91 end
92
93
94 it "should also try to load structs when they are unknown (raises TypeError)" do
95 job = Delayed::Job.new
96 job['handler'] = "--- !ruby/struct:JobThatDoesNotExist {}"
97
98 job.should_receive(:attempt_to_load).with('JobThatDoesNotExist').and_return(true)
99
100 lambda { job.perform }.should raise_error(Delayed::DeserializationError)
101 end
102
103 it "should try include the namespace when loading unknown structs" do
104 job = Delayed::Job.new
105 job['handler'] = "--- !ruby/struct:Delayed::JobThatDoesNotExist {}"
106 job.should_receive(:attempt_to_load).with('Delayed::JobThatDoesNotExist').and_return(true)
107 lambda { job.perform }.should raise_error(Delayed::DeserializationError)
108 end
109
110 end
111
112
113
114
115
116
117
118
119
120
121
122
Something went wrong with that request. Please try again.