File: message_sending_spec.rb

package info (click to toggle)
ruby-delayed-job 4.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 388 kB
  • sloc: ruby: 2,780; makefile: 8
file content (147 lines) | stat: -rw-r--r-- 4,343 bytes parent folder | download | duplicates (5)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
require 'helper'

describe Delayed::MessageSending do
  it 'does not include ClassMethods along with MessageSending' do
    expect { ClassMethods }.to raise_error(NameError)
    expect(defined?(String::ClassMethods)).to eq(nil)
  end

  describe 'handle_asynchronously' do
    class Story
      def tell!(_arg); end
      handle_asynchronously :tell!
    end

    it 'aliases original method' do
      expect(Story.new).to respond_to(:tell_without_delay!)
      expect(Story.new).to respond_to(:tell_with_delay!)
    end

    it 'creates a PerformableMethod' do
      story = Story.create
      expect do
        job = story.tell!(1)
        expect(job.payload_object.class).to eq(Delayed::PerformableMethod)
        expect(job.payload_object.method_name).to eq(:tell_without_delay!)
        expect(job.payload_object.args).to eq([1])
      end.to(change { Delayed::Job.count })
    end

    describe 'with options' do
      class Fable
        cattr_accessor :importance
        def tell; end
        handle_asynchronously :tell, :priority => proc { importance }
      end

      it 'sets the priority based on the Fable importance' do
        Fable.importance = 10
        job = Fable.new.tell
        expect(job.priority).to eq(10)

        Fable.importance = 20
        job = Fable.new.tell
        expect(job.priority).to eq(20)
      end

      describe 'using a proc with parameters' do
        class Yarn
          attr_accessor :importance
          def spin; end
          handle_asynchronously :spin, :priority => proc { |y| y.importance }
        end

        it 'sets the priority based on the Fable importance' do
          job = Yarn.new.tap { |y| y.importance = 10 }.spin
          expect(job.priority).to eq(10)

          job = Yarn.new.tap { |y| y.importance = 20 }.spin
          expect(job.priority).to eq(20)
        end
      end
    end
  end

  context 'delay' do
    class FairyTail
      attr_accessor :happy_ending
      def self.princesses; end

      def tell
        @happy_ending = true
      end
    end

    after do
      Delayed::Worker.default_queue_name = nil
    end

    it 'creates a new PerformableMethod job' do
      expect do
        job = 'hello'.delay.count('l')
        expect(job.payload_object.class).to eq(Delayed::PerformableMethod)
        expect(job.payload_object.method_name).to eq(:count)
        expect(job.payload_object.args).to eq(['l'])
      end.to change { Delayed::Job.count }.by(1)
    end

    it 'sets default priority' do
      Delayed::Worker.default_priority = 99
      job = FairyTail.delay.to_s
      expect(job.priority).to eq(99)
    end

    it 'sets default queue name' do
      Delayed::Worker.default_queue_name = 'abbazabba'
      job = FairyTail.delay.to_s
      expect(job.queue).to eq('abbazabba')
    end

    it 'sets job options' do
      run_at = Time.parse('2010-05-03 12:55 AM')
      job = FairyTail.delay(:priority => 20, :run_at => run_at).to_s
      expect(job.run_at).to eq(run_at)
      expect(job.priority).to eq(20)
    end

    it 'does not delay the job when delay_jobs is false' do
      Delayed::Worker.delay_jobs = false
      fairy_tail = FairyTail.new
      expect do
        expect do
          fairy_tail.delay.tell
        end.to change(fairy_tail, :happy_ending).from(nil).to(true)
      end.not_to(change { Delayed::Job.count })
    end

    it 'does delay the job when delay_jobs is true' do
      Delayed::Worker.delay_jobs = true
      fairy_tail = FairyTail.new
      expect do
        expect do
          fairy_tail.delay.tell
        end.not_to change(fairy_tail, :happy_ending)
      end.to change { Delayed::Job.count }.by(1)
    end

    it 'does delay when delay_jobs is a proc returning true' do
      Delayed::Worker.delay_jobs = ->(_job) { true }
      fairy_tail = FairyTail.new
      expect do
        expect do
          fairy_tail.delay.tell
        end.not_to change(fairy_tail, :happy_ending)
      end.to change { Delayed::Job.count }.by(1)
    end

    it 'does not delay the job when delay_jobs is a proc returning false' do
      Delayed::Worker.delay_jobs = ->(_job) { false }
      fairy_tail = FairyTail.new
      expect do
        expect do
          fairy_tail.delay.tell
        end.to change(fairy_tail, :happy_ending).from(nil).to(true)
      end.not_to(change { Delayed::Job.count })
    end
  end
end