-
-
Notifications
You must be signed in to change notification settings - Fork 514
/
Copy pathspec_helper.rb
294 lines (236 loc) · 6.59 KB
/
spec_helper.rb
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# frozen_string_literal: true
require "bundler/setup"
begin
require "debug/prelude"
rescue LoadError
end
# this enables sidekiq's server mode
require "sidekiq/cli"
MIN_SIDEKIQ_6 = Gem::Version.new(Sidekiq::VERSION) >= Gem::Version.new("6.0")
WITH_SIDEKIQ_7 = Gem::Version.new(Sidekiq::VERSION) >= Gem::Version.new("7.0")
WITH_SIDEKIQ_8 = Gem::Version.new(Sidekiq::VERSION) >= Gem::Version.new("8.0.0.beta")
WITH_SIDEKIQ_6 = MIN_SIDEKIQ_6 && !WITH_SIDEKIQ_7
require "sidekiq/embedded" if WITH_SIDEKIQ_7
if RUBY_VERSION.to_f >= 2.7 && MIN_SIDEKIQ_6
require 'sidekiq-cron'
require 'sidekiq-scheduler'
end
require "sentry-ruby"
require "sentry/test_helper"
# Fixing crash:
# activesupport-6.1.7.10/lib/active_support/logger_thread_safe_level.rb:16:in
# . `<module:LoggerThreadSafeLevel>': uninitialized constant ActiveSupport::LoggerThreadSafeLevel::Logger (NameError)
require "logger"
require 'simplecov'
SimpleCov.start do
project_name "sentry-sidekiq"
root File.join(__FILE__, "../../../")
coverage_dir File.join(__FILE__, "../../coverage")
end
if ENV["CI"]
require 'simplecov-cobertura'
SimpleCov.formatter = SimpleCov::Formatter::CoberturaFormatter
end
require "sentry-sidekiq"
DUMMY_DSN = 'http://12345:[email protected]/sentry/42'
RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"
# Disable RSpec exposing methods globally on `Module` and `main`
config.disable_monkey_patching!
config.expect_with :rspec do |c|
c.syntax = :expect
end
config.before :suite do
if WITH_SIDEKIQ_8
puts "*" * 100
puts "Running with Sidekiq 8.0.0.beta"
puts "*" * 100
end
end
config.before :each do
# Make sure we reset the env in case something leaks in
ENV.delete('SENTRY_DSN')
ENV.delete('SENTRY_CURRENT_ENV')
ENV.delete('SENTRY_ENVIRONMENT')
ENV.delete('SENTRY_RELEASE')
ENV.delete('sidekiq_ENV')
ENV.delete('RACK_ENV')
end
config.before :all do
silence_sidekiq
end
config.include(Sentry::TestHelper)
config.after :each do
reset_sentry_globals!
end
end
def silence_sidekiq
logger = Logger.new(nil)
if WITH_SIDEKIQ_7
Sidekiq.instance_variable_get(:@config).logger = logger
else
Sidekiq.logger = logger
end
end
def build_exception
1 / 0
rescue ZeroDivisionError => e
e
end
def build_exception_with_cause(cause = "exception a")
begin
raise cause
rescue
raise "exception b"
end
rescue RuntimeError => e
e
end
def build_exception_with_two_causes
begin
begin
raise "exception a"
rescue
raise "exception b"
end
rescue
raise "exception c"
end
rescue RuntimeError => e
e
end
def build_exception_with_recursive_cause
backtrace = []
exception = double("Exception")
allow(exception).to receive(:cause).and_return(exception)
allow(exception).to receive(:message).and_return("example")
allow(exception).to receive(:backtrace).and_return(backtrace)
exception
end
class HappyWorker
include Sidekiq::Worker
def perform
crumb = Sentry::Breadcrumb.new(message: "I'm happy!")
Sentry.add_breadcrumb(crumb)
Sentry.set_tags mood: 'happy'
end
end
class SadWorker
include Sidekiq::Worker
def perform
crumb = Sentry::Breadcrumb.new(message: "I'm sad!")
Sentry.add_breadcrumb(crumb)
Sentry.set_tags mood: 'sad'
raise "I'm sad!"
end
end
class HappyWorkerForCron < HappyWorker; end
class HappyWorkerForCronWithTimezone < HappyWorker; end
class HappyWorkerForScheduler < HappyWorker; end
class HappyWorkerForSchedulerWithTimezone < HappyWorker; end
class EveryHappyWorker < HappyWorker; end
class HappyWorkerWithHumanReadableCron < HappyWorker; end
class HappyWorkerWithSymbolName < HappyWorker; end
class HappyWorkerWithCron < HappyWorker
include Sentry::Cron::MonitorCheckIns
sentry_monitor_check_ins
end
class SadWorkerWithCron < SadWorker
include Sentry::Cron::MonitorCheckIns
sentry_monitor_check_ins slug: "failed_job", monitor_config: Sentry::Cron::MonitorConfig.from_crontab("5 * * * *")
end
class VerySadWorker
include Sidekiq::Worker
def perform
crumb = Sentry::Breadcrumb.new(message: "I'm very sad!")
Sentry.add_breadcrumb(crumb)
Sentry.set_tags mood: 'very sad'
raise "I'm very sad!"
end
end
class ReportingWorker
include Sidekiq::Worker
def perform
Sentry.capture_message("I have something to say!")
end
end
class TagsWorker
include Sidekiq::Worker
sidekiq_options tags: ["marvel", "dc"]
def perform; end
end
def new_processor
manager =
case
when WITH_SIDEKIQ_7
capsule = Sidekiq.instance_variable_get(:@config).default_capsule
Sidekiq::Manager.new(capsule)
when WITH_SIDEKIQ_6
Sidekiq[:queue] = ['default']
Sidekiq::Manager.new(Sidekiq)
else
Sidekiq::Manager.new({ queues: ['default'] })
end
manager.workers.first
end
class SidekiqConfigMock
include ::Sidekiq
attr_accessor :options
def initialize(options = {})
@options = DEFAULTS.merge(options)
end
def fetch(key, default = nil)
options.fetch(key, default)
end
def [](key)
options[key]
end
end
module VeryLongOuterModule
module VeryVeryVeryVeryLongInnerModule
class Job
end
end
end
# Sidekiq 7 has a Config class, but for Sidekiq 6, we'll mock it.
def sidekiq_config(opts)
WITH_SIDEKIQ_7 ? ::Sidekiq::Config.new(opts) : SidekiqConfigMock.new(opts)
end
def now_in_ms
::Process.clock_gettime(::Process::CLOCK_REALTIME, :millisecond)
end
def execute_worker(processor, klass, **options)
klass_options = klass.sidekiq_options_hash || {}
# for Ruby < 2.6
klass_options.each do |k, v|
options[k.to_sym] = v
end
jid = options.delete(:jid) || "123123"
timecop_delay = options.delete(:timecop_delay)
if WITH_SIDEKIQ_8
current_time_ms = now_in_ms
msg = Sidekiq.dump_json(created_at: current_time_ms, enqueued_at: current_time_ms, jid: jid, class: klass, args: [], **options)
else
msg = Sidekiq.dump_json(created_at: Time.now.to_f, enqueued_at: Time.now.to_f, jid: jid, class: klass, args: [], **options)
end
Timecop.freeze(timecop_delay) if timecop_delay
work = Sidekiq::BasicFetch::UnitOfWork.new('queue:default', msg)
process_work(processor, work)
ensure
Timecop.return if timecop_delay
end
def process_work(processor, work)
processor.send(:process, work)
rescue StandardError
# do nothing
end
def perform_basic_setup
Sentry.init do |config|
config.dsn = DUMMY_DSN
config.logger = ::Logger.new(nil)
config.background_worker_threads = 0
config.transport.transport_class = Sentry::DummyTransport
yield config if block_given?
end
end