public
Description: Database based asynchronously priority queue system -- Extracted from Shopify
Homepage: http://www.shopify.com
Clone URL: git://github.com/tobi/delayed_job.git
Search Repo:
Tobias Lütke (author)
Mon Mar 24 09:01:03 -0700 2008
commit  b9dc92b9ca14e7cf2ec892c3ca3a1cb006989838
tree    6973231e0d44f9848631cd753b25463e6923c8b5
parent  0880b0f0924b41562b19b4b03a89b85bf660f02d
delayed_job / spec / delayed_method_spec.rb
100644 119 lines (79 sloc) 2.892 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
require File.dirname(__FILE__) + '/database'
 
if not defined?(:ActiveRecord)
  module ActiveRecord
    class RecordNotFound < StandardError
    end
  end
end
                     
 
class SimpleJob
  cattr_accessor :runs; self.runs = 0
  def perform; @@runs += 1; end
end
 
class RandomRubyObject
  def say_hello
    'hello'
  end
end
 
class ErrorObject
  
  def throw
    raise ActiveRecord::RecordNotFound, '...'
    false
  end
  
end
 
class StoryReader
  
  def read(story)
    "Epilog: #{story.tell}"
  end
  
end
 
class StoryReader
  
  def read(story)
    "Epilog: #{story.tell}"
  end
  
end
 
                       
describe 'random ruby objects' do
  
  before { reset_db }
 
  it "should respond_to :send_later method" do
                                           
    RandomRubyObject.new.respond_to?(:send_later)
    
  end
  
  it "should raise a ArgumentError if send_later is called but the target method doesn't exist" do
    lambda { RandomRubyObject.new.send_later(:method_that_deos_not_exist) }.should raise_error(NoMethodError)
  end
  
  it "should add a new entry to the job table when send_later is called on it" do
    Delayed::Job.count.should == 0
    
    RandomRubyObject.new.send_later(:to_s)
 
    Delayed::Job.count.should == 1
  end
  
  it "should run get the original method executed when the job is performed" do
    
    RandomRubyObject.new.send_later(:say_hello)
                               
    Delayed::Job.count.should == 1
  end
 
  it "should ignore ActiveRecord::RecordNotFound errors because they are permanent" do
    
    ErrorObject.new.send_later(:throw)
 
    Delayed::Job.count.should == 1
    
    output = nil
    
    Delayed::Job.reserve do |e|
      output = e.perform
    end
    
    output.should == true
                               
  end
  
  it "should store the object as string if its an active record" do
    story = Story.create :text => 'Once upon...'
    story.send_later(:tell)
    
    job = Delayed::Job.find(:first)
    job.payload_object.class.should == Delayed::PerformableMethod
    job.payload_object.object.should == 'AR:Story:1'
    job.payload_object.method.should == :tell
    job.payload_object.args.should == []
    job.payload_object.perform.should == 'Once upon...'
  end
  
  it "should store arguments as string if they an active record" do
    
    story = Story.create :text => 'Once upon...'
    
    reader = StoryReader.new
    reader.send_later(:read, story)
    
    job = Delayed::Job.find(:first)
    job.payload_object.class.should == Delayed::PerformableMethod
    job.payload_object.method.should == :read
    job.payload_object.args.should == ['AR:Story:1']
    job.payload_object.perform.should == 'Epilog: Once upon...'
  end
  
end