Skip to content

Commit d9001f5

Browse files
committed
refa(tests): simplify and improve cron tests
1 parent 89f74dc commit d9001f5

2 files changed

Lines changed: 43 additions & 54 deletions

File tree

test/sentry/integrations/oban/cron_test.exs

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,59 +15,61 @@ defmodule Sentry.Integrations.Oban.CronTest do
1515
end
1616

1717
setup do
18-
SentryTest.setup_sentry(dedup_events: false, environment_name: "test")
18+
SentryTest.setup_sentry(
19+
dedup_events: false,
20+
environment_name: "test",
21+
collect_envelopes: [type: "check_in"]
22+
)
1923
end
2024

2125
for event_type <- [:start, :stop, :exception] do
22-
test "ignores #{event_type} events without a cron meta", %{bypass: bypass} do
23-
Bypass.down(bypass)
26+
test "ignores #{event_type} events without a cron meta", %{ref: ref} do
2427
:telemetry.execute([:oban, :job, unquote(event_type)], %{}, %{job: %Oban.Job{}})
28+
refute_sentry_check_in(ref)
2529
end
2630

27-
test "ignores #{event_type} events without a cron_expr meta", %{bypass: bypass} do
28-
Bypass.down(bypass)
29-
31+
test "ignores #{event_type} events without a cron_expr meta", %{ref: ref} do
3032
:telemetry.execute([:oban, :job, unquote(event_type)], %{}, %{
3133
job: %Oban.Job{meta: %{"cron" => true}}
3234
})
33-
end
3435

35-
test "ignores #{event_type} events with a cron expr of @reboot", %{bypass: bypass} do
36-
Bypass.down(bypass)
36+
refute_sentry_check_in(ref)
37+
end
3738

39+
test "ignores #{event_type} events with a cron expr of @reboot", %{ref: ref} do
3840
:telemetry.execute([:oban, :job, unquote(event_type)], %{}, %{
3941
job: %Oban.Job{
4042
worker: "Sentry.MyWorker",
4143
meta: %{"cron" => true, "cron_expr" => "@reboot"}
4244
}
4345
})
46+
47+
refute_sentry_check_in(ref)
4448
end
4549

4650
test "ignores #{event_type} events with a cron expr of @reboot even with timezone", %{
47-
bypass: bypass
51+
ref: ref
4852
} do
49-
Bypass.down(bypass)
50-
5153
:telemetry.execute([:oban, :job, unquote(event_type)], %{}, %{
5254
job: %Oban.Job{
5355
worker: "Sentry.MyWorker",
5456
meta: %{"cron" => true, "cron_expr" => "@reboot", "cron_tz" => "Etc/UTC"}
5557
}
5658
})
57-
end
5859

59-
test "ignores #{event_type} events with a cron expr that is not a string", %{bypass: bypass} do
60-
Bypass.down(bypass)
60+
refute_sentry_check_in(ref)
61+
end
6162

63+
test "ignores #{event_type} events with a cron expr that is not a string", %{ref: ref} do
6264
:telemetry.execute([:oban, :job, unquote(event_type)], %{}, %{
6365
job: %Oban.Job{worker: "Sentry.MyWorker", meta: %{"cron" => true, "cron_expr" => 123}}
6466
})
67+
68+
refute_sentry_check_in(ref)
6569
end
6670
end
6771

68-
test "captures start events with monitor config", %{bypass: bypass} do
69-
ref = SentryTest.setup_bypass_envelope_collector(bypass, type: "check_in")
70-
72+
test "captures start events with monitor config", %{ref: ref} do
7173
:telemetry.execute([:oban, :job, :start], %{}, %{
7274
job: %Oban.Job{
7375
worker: "Sentry.MyWorker",
@@ -111,9 +113,7 @@ defmodule Sentry.Integrations.Oban.CronTest do
111113
{"@annually", "year"}
112114
] do
113115
test "captures stop events with monitor config and state of #{inspect(oban_state)} and frequency of #{frequency}",
114-
%{bypass: bypass} do
115-
ref = SentryTest.setup_bypass_envelope_collector(bypass, type: "check_in")
116-
116+
%{ref: ref} do
117117
duration = System.convert_time_unit(12_099, :millisecond, :native)
118118

119119
:telemetry.execute([:oban, :job, :stop], %{duration: duration}, %{
@@ -146,9 +146,7 @@ defmodule Sentry.Integrations.Oban.CronTest do
146146
end
147147
end
148148

149-
test "captures exception events with monitor config", %{bypass: bypass} do
150-
ref = SentryTest.setup_bypass_envelope_collector(bypass, type: "check_in")
151-
149+
test "captures exception events with monitor config", %{ref: ref} do
152150
duration = System.convert_time_unit(12_099, :millisecond, :native)
153151

154152
:telemetry.execute([:oban, :job, :exception], %{duration: duration}, %{
@@ -179,7 +177,7 @@ defmodule Sentry.Integrations.Oban.CronTest do
179177
)
180178
end
181179

182-
test "uses default monitor configuration in Sentry's config if present", %{bypass: bypass} do
180+
test "uses default monitor configuration in Sentry's config if present", %{ref: ref} do
183181
put_test_config(
184182
integrations: [
185183
monitor_config_defaults: [
@@ -190,8 +188,6 @@ defmodule Sentry.Integrations.Oban.CronTest do
190188
]
191189
)
192190

193-
ref = SentryTest.setup_bypass_envelope_collector(bypass, type: "check_in")
194-
195191
:telemetry.execute([:oban, :job, :exception], %{duration: 0}, %{
196192
state: :success,
197193
job: %Oban.Job{
@@ -219,9 +215,7 @@ defmodule Sentry.Integrations.Oban.CronTest do
219215

220216
@tag attach_opts: [monitor_slug_generator: {__MODULE__, :custom_name_generator}]
221217
test "monitor_slug is not affected if the custom monitor_name_generator does not target the worker",
222-
%{bypass: bypass} do
223-
ref = SentryTest.setup_bypass_envelope_collector(bypass, type: "check_in")
224-
218+
%{ref: ref} do
225219
:telemetry.execute([:oban, :job, :start], %{}, %{
226220
job: %Oban.Job{
227221
worker: "Sentry.MyWorker",
@@ -236,9 +230,8 @@ defmodule Sentry.Integrations.Oban.CronTest do
236230

237231
@tag attach_opts: [monitor_slug_generator: {__MODULE__, :custom_name_generator}]
238232
test "monitor_slug is set based on the custom monitor_name_generator if it targets the worker",
239-
%{bypass: bypass} do
233+
%{ref: ref} do
240234
client_name = "my-client"
241-
ref = SentryTest.setup_bypass_envelope_collector(bypass, type: "check_in")
242235

243236
:telemetry.execute([:oban, :job, :start], %{}, %{
244237
job: %Oban.Job{
@@ -253,9 +246,7 @@ defmodule Sentry.Integrations.Oban.CronTest do
253246
assert_sentry_report(check_in_body, monitor_slug: "sentry-client-worker-my-client")
254247
end
255248

256-
test "custom options overide job metadata", %{bypass: bypass} do
257-
ref = SentryTest.setup_bypass_envelope_collector(bypass, type: "check_in")
258-
249+
test "custom options overide job metadata", %{ref: ref} do
259250
defmodule WorkerWithCustomOptions do
260251
use Oban.Worker
261252

test/sentry/integrations/quantum/cron_test.exs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,36 @@ defmodule Sentry.Integrations.Quantum.CronTest do
1616
end
1717

1818
setup do
19-
SentryTest.setup_sentry(dedup_events: false, environment_name: "test")
19+
SentryTest.setup_sentry(
20+
dedup_events: false,
21+
environment_name: "test",
22+
collect_envelopes: [type: "check_in"]
23+
)
2024
end
2125

2226
for event_type <- [:start, :stop, :exception] do
23-
test "ignores #{event_type} events without a cron meta", %{bypass: bypass} do
24-
Bypass.down(bypass)
25-
27+
test "ignores #{event_type} events without a cron meta", %{ref: ref} do
2628
:telemetry.execute([:quantum, :job, unquote(event_type)], %{}, %{
2729
job: Scheduler.new_job(name: :test_job)
2830
})
29-
end
3031

31-
test "ignores #{event_type} events with a cron expr of @reboot", %{bypass: bypass} do
32-
Bypass.down(bypass)
32+
refute_sentry_check_in(ref)
33+
end
3334

35+
test "ignores #{event_type} events with a cron expr of @reboot", %{ref: ref} do
3436
:telemetry.execute([:quantum, :job, unquote(event_type)], %{}, %{
3537
job:
3638
Scheduler.new_job(
3739
name: :reboot_job,
3840
schedule: Crontab.CronExpression.Parser.parse!("@reboot")
3941
)
4042
})
43+
44+
refute_sentry_check_in(ref)
4145
end
4246
end
4347

44-
test "captures start events with monitor config", %{bypass: bypass} do
45-
ref = SentryTest.setup_bypass_envelope_collector(bypass, type: "check_in")
48+
test "captures start events with monitor config", %{ref: ref} do
4649
span_ref = make_ref()
4750

4851
:telemetry.execute([:quantum, :job, :start], %{}, %{
@@ -73,8 +76,7 @@ defmodule Sentry.Integrations.Quantum.CronTest do
7376
)
7477
end
7578

76-
test "captures exception events with monitor config", %{bypass: bypass} do
77-
ref = SentryTest.setup_bypass_envelope_collector(bypass, type: "check_in")
79+
test "captures exception events with monitor config", %{ref: ref} do
7880
span_ref = make_ref()
7981

8082
duration = System.convert_time_unit(12_099, :millisecond, :native)
@@ -108,8 +110,7 @@ defmodule Sentry.Integrations.Quantum.CronTest do
108110
)
109111
end
110112

111-
test "captures stop events with monitor config", %{bypass: bypass} do
112-
ref = SentryTest.setup_bypass_envelope_collector(bypass, type: "check_in")
113+
test "captures stop events with monitor config", %{ref: ref} do
113114
span_ref = make_ref()
114115

115116
duration = System.convert_time_unit(12_099, :millisecond, :native)
@@ -146,8 +147,7 @@ defmodule Sentry.Integrations.Quantum.CronTest do
146147
{:some_job, "quantum-some-job"},
147148
{MyApp.MyJob, "quantum-my-app-my-job"}
148149
] do
149-
test "works for a job named #{inspect(job_name)}", %{bypass: bypass} do
150-
ref = SentryTest.setup_bypass_envelope_collector(bypass, type: "check_in")
150+
test "works for a job named #{inspect(job_name)}", %{ref: ref} do
151151
span_ref = make_ref()
152152

153153
duration = System.convert_time_unit(12_099, :millisecond, :native)
@@ -166,8 +166,7 @@ defmodule Sentry.Integrations.Quantum.CronTest do
166166
end
167167
end
168168

169-
test "works for a job without the name", %{bypass: bypass} do
170-
ref = SentryTest.setup_bypass_envelope_collector(bypass, type: "check_in")
169+
test "works for a job without the name", %{ref: ref} do
171170
span_ref = make_ref()
172171

173172
duration = System.convert_time_unit(12_099, :millisecond, :native)
@@ -181,8 +180,7 @@ defmodule Sentry.Integrations.Quantum.CronTest do
181180
assert_sentry_report(check_in_body, monitor_slug: "quantum-generic-job")
182181
end
183182

184-
test "start event and same ref stop event have same check-in id", %{bypass: bypass} do
185-
ref = SentryTest.setup_bypass_envelope_collector(bypass, type: "check_in")
183+
test "start event and same ref stop event have same check-in id", %{ref: ref} do
186184
span_ref = make_ref()
187185
id = CheckInIDMappings.lookup_or_insert_new("quantum-#{:erlang.phash2(span_ref)}")
188186

0 commit comments

Comments
 (0)