Skip to content

Commit f12cc2c

Browse files
authored
Fix keyword arguments for MonitorCheckIns monkeypatch (#2199)
1 parent 9895426 commit f12cc2c

File tree

4 files changed

+80
-24
lines changed

4 files changed

+80
-24
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
## Unreleased
1+
## 5.15.1
22

33
### Features
44

55
- Expose `configuration.background_worker_max_queue` to control thread pool queue size [#2195](https://github.com/getsentry/sentry-ruby/pull/2195)
66

7+
### Bug Fixes
8+
9+
- Fix `Sentry::Cron::MonitorCheckIns` monkeypatch keyword arguments [#2199](https://github.com/getsentry/sentry-ruby/pull/2199)
10+
- Fixes [#2198](https://github.com/getsentry/sentry-ruby/issues/2198)
11+
712
## 5.15.0
813

914
### Features

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ ruby_version = Gem::Version.new(RUBY_VERSION)
99
if ruby_version >= Gem::Version.new("2.7.0")
1010
gem "debug", github: "ruby/debug", platform: :ruby
1111
gem "irb"
12+
# new release breaks on jruby
13+
gem "io-console", "0.6.0"
1214

1315
if ruby_version >= Gem::Version.new("3.0.0")
1416
gem "ruby-lsp-rspec"

sentry-ruby/lib/sentry/cron/monitor_check_ins.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module MonitorCheckIns
44
MAX_SLUG_LENGTH = 50
55

66
module Patch
7-
def perform(*args)
7+
def perform(*args, **opts)
88
slug = self.class.sentry_monitor_slug
99
monitor_config = self.class.sentry_monitor_config
1010

@@ -13,7 +13,8 @@ def perform(*args)
1313
monitor_config: monitor_config)
1414

1515
start = Sentry.utc_now.to_i
16-
ret = super
16+
# need to do this on ruby <= 2.6 sadly
17+
ret = method(:perform).super_method.arity == 0 ? super() : super
1718
duration = Sentry.utc_now.to_i - start
1819

1920
Sentry.capture_check_in(slug,

sentry-ruby/spec/sentry/cron/monitor_check_ins_spec.rb

Lines changed: 69 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@
33
RSpec.describe Sentry::Cron::MonitorCheckIns do
44
before { perform_basic_setup }
55

6+
shared_examples 'original_job' do
7+
it 'does the work' do
8+
expect(job).to receive(:work).with(1, 42, 99).and_call_original
9+
expect(job.perform(1)).to eq(142)
10+
end
11+
12+
it 'does the work with args' do
13+
expect(job).to receive(:work).with(1, 43, 100).and_call_original
14+
expect(job.perform(1, 43, c: 100)).to eq(144)
15+
end
16+
end
17+
618
context 'without including mixin' do
719
before do
820
job_class = Class.new do
9-
def work(a, b, c); end
21+
def work(a, b, c); a + b + c end
1022

1123
def perform(a, b = 42, c: 99)
1224
work(a, b, c)
@@ -18,10 +30,7 @@ def perform(a, b = 42, c: 99)
1830

1931
let(:job) { Job.new }
2032

21-
it 'does the work' do
22-
expect(job).to receive(:work).with(1, 42, 99)
23-
job.perform(1)
24-
end
33+
it_behaves_like 'original_job'
2534

2635
it 'does not call capture_check_in' do
2736
expect(Sentry).not_to receive(:capture_check_in)
@@ -37,7 +46,7 @@ def perform(a, b = 42, c: 99)
3746
job_class = Class.new do
3847
include mod
3948

40-
def work(a, b, c); end
49+
def work(a, b, c); a + b + c end
4150

4251
def perform(a, b = 42, c: 99)
4352
work(a, b, c)
@@ -49,10 +58,7 @@ def perform(a, b = 42, c: 99)
4958

5059
let(:job) { Job.new }
5160

52-
it 'does the work' do
53-
expect(job).to receive(:work).with(1, 42, 99)
54-
job.perform(1)
55-
end
61+
it_behaves_like 'original_job'
5662

5763
it 'does not prepend the patch' do
5864
expect(Job.ancestors.first).not_to eq(described_class::Patch)
@@ -81,7 +87,7 @@ def perform(a, b = 42, c: 99)
8187

8288
sentry_monitor_check_ins
8389

84-
def work(a, b, c); end
90+
def work(a, b, c); a + b + c end
8591

8692
def perform(a, b = 42, c: 99)
8793
work(a, b, c)
@@ -93,15 +99,55 @@ def perform(a, b = 42, c: 99)
9399

94100
let(:job) { Job.new }
95101

96-
it 'does the work' do
97-
expect(job).to receive(:work).with(1, 42, 99)
102+
it_behaves_like 'original_job'
103+
104+
it 'prepends the patch' do
105+
expect(Job.ancestors.first).to eq(described_class::Patch)
106+
end
107+
108+
it 'calls capture_check_in twice' do
109+
expect(Sentry).to receive(:capture_check_in).with(
110+
'job',
111+
:in_progress,
112+
hash_including(monitor_config: nil)
113+
).ordered.and_call_original
114+
115+
expect(Sentry).to receive(:capture_check_in).with(
116+
'job',
117+
:ok,
118+
hash_including(:check_in_id, monitor_config: nil, duration: 0)
119+
).ordered.and_call_original
120+
98121
job.perform(1)
99122
end
123+
end
124+
125+
context 'patched perform with arity 0 with default options' do
126+
before do
127+
mod = described_class
128+
129+
job_class = Class.new do
130+
include mod
131+
sentry_monitor_check_ins
132+
133+
def work; 42 end
134+
def perform; work end
135+
end
136+
137+
stub_const('Job', job_class)
138+
end
139+
140+
let(:job) { Job.new }
100141

101142
it 'prepends the patch' do
102143
expect(Job.ancestors.first).to eq(described_class::Patch)
103144
end
104145

146+
it 'does the work' do
147+
expect(job).to receive(:work).and_call_original
148+
expect(job.perform).to eq(42)
149+
end
150+
105151
it 'calls capture_check_in twice' do
106152
expect(Sentry).to receive(:capture_check_in).with(
107153
'job',
@@ -128,7 +174,7 @@ def perform(a, b = 42, c: 99)
128174

129175
sentry_monitor_check_ins
130176

131-
def work(a, b, c); end
177+
def work(a, b, c); a + b + c end
132178

133179
def perform(a, b = 42, c: 99)
134180
work(a, b, c)
@@ -156,7 +202,7 @@ def perform(a, b = 42, c: 99)
156202

157203
sentry_monitor_check_ins slug: 'custom_slug', monitor_config: conf
158204

159-
def work(a, b, c); end
205+
def work(a, b, c); a + b + c end
160206

161207
def perform(a, b = 42, c: 99)
162208
work(a, b, c)
@@ -168,10 +214,7 @@ def perform(a, b = 42, c: 99)
168214

169215
let(:job) { Job.new }
170216

171-
it 'does the work' do
172-
expect(job).to receive(:work).with(1, 42, 99)
173-
job.perform(1)
174-
end
217+
it_behaves_like 'original_job'
175218

176219
it 'prepends the patch' do
177220
expect(Job.ancestors.first).to eq(described_class::Patch)
@@ -226,8 +269,13 @@ def perform(a, b = 42, c: 99)
226269
let(:job) { Job.new }
227270

228271
it 'does the work' do
229-
expect(job).to receive(:work).with(1, 42, 99)
230-
job.perform(1)
272+
expect(job).to receive(:work).with(1, 42, 99).and_call_original
273+
expect { job.perform(1) }.to raise_error(ZeroDivisionError)
274+
end
275+
276+
it 'does the work with args' do
277+
expect(job).to receive(:work).with(1, 43, 100).and_call_original
278+
expect { job.perform(1, 43, c: 100) }.to raise_error(ZeroDivisionError)
231279
end
232280

233281
it 'prepends the patch' do

0 commit comments

Comments
 (0)