inst-jobs 3.1.2 → 3.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,287 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module UnlessInJob
4
- class << self
5
- attr_accessor :runs
6
-
7
- def run
8
- self.runs += 1
9
- end
10
-
11
- def run_later
12
- delay(synchronous: Delayed::Job.in_delayed_job?).run
13
- end
14
- end
15
- end
16
-
17
- shared_examples_for "random ruby objects" do
18
- def set_queue(name) # rubocop:disable Naming/AccessorMethodName
19
- old_name = Delayed::Settings.queue
20
- Delayed::Settings.queue = name
21
- ensure
22
- Delayed::Settings.queue = old_name
23
- end
24
-
25
- it "respond_toes :delay method" do
26
- Object.new.respond_to?(:delay)
27
- end
28
-
29
- it "raises a ArgumentError if delay is called but the target method doesn't exist" do
30
- expect { Object.new.delay.method_that_deos_not_exist }.to raise_error(NoMethodError)
31
- end
32
-
33
- it "adds a new entry to the job table when delay is called on it" do
34
- expect { Object.new.delay.to_s }.to change { Delayed::Job.jobs_count(:current) }.by(1)
35
- end
36
-
37
- it "adds a new entry to the job table when delay is called on it with a queue" do
38
- expect { Object.new.delay(queue: "testqueue").to_s }.to change {
39
- Delayed::Job.jobs_count(:current, "testqueue")
40
- }.by(1)
41
- end
42
-
43
- it "adds a new entry to the job table when delay is called on the class" do
44
- expect { Object.delay.to_s }.to change { Delayed::Job.jobs_count(:current) }.by(1)
45
- end
46
-
47
- it "adds a new entry to the job table when delay is called on the class with a queue" do
48
- expect { Object.delay(queue: "testqueue").to_s }.to change { Delayed::Job.jobs_count(:current, "testqueue") }.by(1)
49
- end
50
-
51
- context "class methods" do
52
- context "handle_asynchronously" do
53
- it "works with default_async" do
54
- klass = Class.new do
55
- attr_reader :ran
56
-
57
- def test_method
58
- @ran = true
59
- end
60
- handle_asynchronously :test_method
61
- end
62
- obj = klass.new
63
- expect { obj.test_method }.to change { Delayed::Job.jobs_count(:current) }.by(1)
64
- expect(obj.ran).to be_falsey
65
- expect { obj.test_method(synchronous: true) }.not_to(change { Delayed::Job.jobs_count(:current) })
66
- expect(obj.ran).to be true
67
- end
68
-
69
- it "must work with enqueue args that are lambdas" do
70
- klass = Class.new do
71
- attr_reader :ran
72
-
73
- def test_method
74
- @ran = true
75
- end
76
- handle_asynchronously :test_method, singleton: ->(obj) { "foobar:#{obj.object_id}" }
77
- end
78
-
79
- obj = klass.new
80
- expect { obj.test_method }.to change { Delayed::Job.jobs_count(:current) }.by(1)
81
- end
82
-
83
- it "must work with kwargs in the original method" do
84
- klass = Class.new do
85
- attr_reader :run
86
-
87
- def test_method(my_kwarg: nil)
88
- @run = my_kwarg
89
- end
90
- handle_asynchronously :test_method
91
-
92
- def other_test(arg)
93
- @foo = arg
94
- end
95
- handle_asynchronously :other_test
96
- end
97
-
98
- obj = klass.new
99
- obj.test_method(my_kwarg: "foo", synchronous: true)
100
- expect(obj.run).to eq "foo"
101
- end
102
-
103
- it "sends along enqueue args and args" do
104
- klass = Class.new do
105
- attr_accessor :ran
106
-
107
- def test_method(*args)
108
- @ran = args
109
- end
110
- handle_asynchronously(:test_method, enqueue_arg1: :thing)
111
- end
112
- obj = klass.new
113
- method = double
114
-
115
- expect(Delayed::PerformableMethod).to receive(:new)
116
- .with(obj,
117
- :test_method,
118
- args: [1, 2, 3],
119
- kwargs: { synchronous: true },
120
- on_failure: nil,
121
- on_permanent_failure: nil,
122
- sender: obj)
123
- .and_return(method)
124
- expect(Delayed::Job).to receive(:enqueue).with(method, enqueue_arg1: :thing)
125
- obj.test_method(1, 2, 3)
126
-
127
- expect(Delayed::PerformableMethod).to receive(:new)
128
- .with(obj,
129
- :test_method,
130
- args: [4],
131
- kwargs: { synchronous: true },
132
- on_failure: nil,
133
- on_permanent_failure: nil,
134
- sender: obj)
135
- .and_return(method)
136
- expect(Delayed::Job).to receive(:enqueue).with(method, enqueue_arg1: :thing)
137
- obj.test_method(4)
138
-
139
- expect(obj.ran).to be_nil
140
- obj.test_method(7, synchronous: true)
141
- expect(obj.ran).to eq([7])
142
- obj.ran = nil
143
- expect(obj.ran).to be_nil
144
- obj.test_method(8, 9, synchronous: true)
145
- expect(obj.ran).to eq([8, 9])
146
- end
147
-
148
- it "handles punctuation correctly" do
149
- klass = Class.new do
150
- attr_reader :ran
151
-
152
- def test_method?
153
- @ran = true
154
- end
155
- handle_asynchronously :test_method?
156
- end
157
- obj = klass.new
158
- expect { obj.test_method? }.to change { Delayed::Job.jobs_count(:current) }.by(1)
159
- expect(obj.ran).to be_falsey
160
- expect { obj.test_method?(synchronous: true) }.not_to(change { Delayed::Job.jobs_count(:current) })
161
- expect(obj.ran).to be true
162
- end
163
-
164
- it "handles assignment punctuation correctly" do
165
- klass = Class.new do
166
- attr_reader :ran
167
-
168
- def test_method=(val)
169
- @ran = val
170
- end
171
- handle_asynchronously :test_method=
172
- end
173
- obj = klass.new
174
- expect { obj.test_method = 3 }.to change { Delayed::Job.jobs_count(:current) }.by(1)
175
- expect(obj.ran).to be_nil
176
- expect { obj.send(:test_method=, 5, synchronous: true) }.not_to(change { Delayed::Job.jobs_count(:current) })
177
- expect(obj.ran).to eq(5)
178
- end
179
-
180
- it "correctlies sort out method accessibility" do
181
- klass1 = Class.new do
182
- def test_method; end
183
- handle_asynchronously :test_method
184
- end
185
-
186
- klass2 = Class.new do
187
- protected
188
-
189
- def test_method; end
190
- handle_asynchronously :test_method
191
- end
192
-
193
- klass3 = Class.new do
194
- private
195
-
196
- def test_method; end
197
- handle_asynchronously :test_method
198
- end
199
-
200
- expect(klass1.public_method_defined?(:test_method)).to be true
201
- expect(klass2.protected_method_defined?(:test_method)).to be true
202
- expect(klass3.private_method_defined?(:test_method)).to be true
203
- end
204
- end
205
- end
206
-
207
- it "calls send later on methods which are wrapped with handle_asynchronously" do
208
- story = Story.create text: "Once upon..."
209
-
210
- expect { story.whatever(1, 5) }.to change { Delayed::Job.jobs_count(:current) }.by(1)
211
-
212
- job = Delayed::Job.list_jobs(:current, 1).first
213
- expect(job.payload_object.class).to eq(Delayed::PerformableMethod)
214
- expect(job.payload_object.method).to eq(:whatever)
215
- expect(job.payload_object.args).to eq([1, 5])
216
- expect(job.payload_object.kwargs).to eq({ synchronous: true })
217
- expect(job.payload_object.perform).to eq("Once upon...")
218
- end
219
-
220
- context "delay" do
221
- it "uses the default queue if there is one" do
222
- set_queue("testqueue") do
223
- "string".delay.reverse
224
- job = Delayed::Job.list_jobs(:current, 1).first
225
- expect(job.queue).to eq("testqueue")
226
-
227
- "string".delay(queue: nil).reverse
228
- job2 = Delayed::Job.list_jobs(:current, 2).last
229
- expect(job2.queue).to eq("testqueue")
230
- end
231
- end
232
-
233
- it "requires a queue" do
234
- expect { set_queue(nil) }.to raise_error(ArgumentError)
235
- end
236
- end
237
-
238
- context "delay with run_at" do
239
- it "queues a new job" do
240
- expect do
241
- "string".delay(run_at: 1.hour.from_now).length
242
- end.to change { Delayed::Job.jobs_count(:future) }.by(1)
243
- end
244
-
245
- it "schedules the job in the future" do
246
- time = 1.hour.from_now
247
- "string".delay(run_at: time).length
248
- job = Delayed::Job.list_jobs(:future, 1).first
249
- expect(job.run_at.to_i).to eq(time.to_i)
250
- end
251
-
252
- it "stores payload as PerformableMethod" do
253
- "string".delay(run_at: 1.hour.from_now).count("r")
254
- job = Delayed::Job.list_jobs(:future, 1).first
255
- expect(job.payload_object.class).to eq(Delayed::PerformableMethod)
256
- expect(job.payload_object.method).to eq(:count)
257
- expect(job.payload_object.args).to eq(["r"])
258
- expect(job.payload_object.perform).to eq(1)
259
- end
260
-
261
- it "uses the default queue if there is one" do
262
- set_queue("testqueue") do
263
- "string".delay(run_at: 1.hour.from_now).reverse
264
- job = Delayed::Job.list_jobs(:current, 1).first
265
- expect(job.queue).to eq("testqueue")
266
- end
267
- end
268
- end
269
-
270
- describe "delay with synchronous argument" do
271
- before do
272
- UnlessInJob.runs = 0
273
- end
274
-
275
- it "performs immediately if in job" do
276
- UnlessInJob.delay.run_later
277
- job = Delayed::Job.list_jobs(:current, 1).first
278
- job.invoke_job
279
- expect(UnlessInJob.runs).to eq(1)
280
- end
281
-
282
- it "queues up for later if not in job" do
283
- UnlessInJob.run_later
284
- expect(UnlessInJob.runs).to eq(0)
285
- end
286
- end
287
- end
@@ -1,75 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- shared_examples_for "Delayed::PerformableMethod" do
4
- it "does not ignore ActiveRecord::RecordNotFound errors because they are not always permanent" do
5
- story = Story.create text: "Once upon..."
6
- p = Delayed::PerformableMethod.new(story, :tell)
7
- story.destroy
8
- expect { YAML.load(p.to_yaml) }.to raise_error(Delayed::Backend::RecordNotFound)
9
- end
10
-
11
- it "stores the object using native YAML even if its an active record" do
12
- story = Story.create text: "Once upon..."
13
- p = Delayed::PerformableMethod.new(story, :tell)
14
- expect(p.class).to eq(Delayed::PerformableMethod)
15
- expect(p.object).to eq(story)
16
- expect(p.method).to eq(:tell)
17
- expect(p.args).to eq([])
18
- expect(p.perform).to eq("Once upon...")
19
- end
20
-
21
- it "allows class methods to be called on ActiveRecord models" do
22
- Story.create!(text: "Once upon a...")
23
- p = Delayed::PerformableMethod.new(Story, :count)
24
- expect { expect(p.send(:perform)).to be 1 }.not_to raise_error
25
- end
26
-
27
- it "allows class methods to be called" do
28
- p = Delayed::PerformableMethod.new(StoryReader, :reverse, args: ["ohai"])
29
- expect { expect(p.send(:perform)).to eq("iaho") }.not_to raise_error
30
- end
31
-
32
- it "allows module methods to be called" do
33
- p = Delayed::PerformableMethod.new(MyReverser, :reverse, args: ["ohai"])
34
- expect { expect(p.send(:perform)).to eq("iaho") }.not_to raise_error
35
- end
36
-
37
- it "stores arguments as native YAML if they are active record objects" do
38
- story = Story.create text: "Once upon..."
39
- reader = StoryReader.new
40
- p = Delayed::PerformableMethod.new(reader, :read, args: [story])
41
- expect(p.class).to eq(Delayed::PerformableMethod)
42
- expect(p.method).to eq(:read)
43
- expect(p.args).to eq([story])
44
- expect(p.perform).to eq("Epilog: Once upon...")
45
- end
46
-
47
- it "deeplies de-AR-ize arguments in full name" do
48
- story = Story.create text: "Once upon..."
49
- reader = StoryReader.new
50
- p = Delayed::PerformableMethod.new(reader, :read, args: [["arg1", story, { [:key, 1] => story }]])
51
- expect(p.full_name).to eq(
52
- "StoryReader#read([\"arg1\", Story.find(#{story.id}), {[:key, 1] => Story.find(#{story.id})}])"
53
- )
54
- end
55
-
56
- it "calls the on_failure callback" do
57
- story = Story.create text: "wat"
58
- p = Delayed::PerformableMethod.new(story, :tell, on_failure: :text=)
59
- p.send(:on_failure, "fail")
60
- expect(story.text).to eq("fail")
61
- end
62
-
63
- it "calls the on_permanent_failure callback" do
64
- story = Story.create text: "wat"
65
- p = Delayed::PerformableMethod.new(story, :tell, on_permanent_failure: :text=)
66
- p.send(:on_permanent_failure, "fail_frd")
67
- expect(story.text).to eq("fail_frd")
68
- end
69
-
70
- it "can still generate a name with no kwargs" do
71
- story = Story.create text: "wat"
72
- p = Delayed::PerformableMethod.new(story, :tell, kwargs: nil)
73
- expect(p.full_name).to eq("Story.find(#{story.id}).tell()")
74
- end
75
- end