Skip to content

Commit c3df22f

Browse files
authored
Improve flakiness of tests (#728)
1 parent ae34e99 commit c3df22f

File tree

5 files changed

+30
-19
lines changed

5 files changed

+30
-19
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ env:
1414
jobs:
1515
test:
1616
name: Test (Elixir ${{ matrix.elixir }}, OTP ${{ matrix.otp }})
17-
1817
runs-on: ubuntu-20.04
1918
strategy:
19+
fail-fast: false
2020
matrix:
2121
# https://hexdocs.pm/elixir/compatibility-and-deprecations.html#compatibility-between-elixir-and-erlang-otp
2222
include:

test/logger_backend_test.exs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ defmodule Sentry.LoggerBackendTest do
134134

135135
ref = register_before_send()
136136

137+
start_supervised!(Sentry.ExamplePlugApplication, restart: :temporary)
138+
137139
:hackney.get("http://127.0.0.1:8003/error_route", [], "", [])
138140
assert_receive {^ref, _event}, 1000
139141
after
@@ -273,8 +275,7 @@ defmodule Sentry.LoggerBackendTest do
273275
Logger.debug("Debug")
274276

275277
assert_receive {^ref, event}
276-
277-
assert event.message.formatted =~ "Error"
278+
assert_formatted_message_matches(event, "Error")
278279
after
279280
Logger.configure_backend(Sentry.LoggerBackend, level: :error, capture_log_messages: false)
280281
end
@@ -308,7 +309,7 @@ defmodule Sentry.LoggerBackendTest do
308309
Logger.error("Error", callers: [dead_pid, nil])
309310

310311
assert_receive {^ref, event}
311-
assert event.message.formatted =~ "Error"
312+
assert_formatted_message_matches(event, "Error")
312313
end
313314

314315
test "doesn't log events with :sentry as a domain" do
@@ -367,4 +368,15 @@ defmodule Sentry.LoggerBackendTest do
367368
defp test_genserver_invalid_fun(pid) do
368369
TestGenServer.run_async(pid, fn _state -> apply(NaiveDateTime, :from_erl, [{}, {}, {}]) end)
369370
end
371+
372+
defp assert_formatted_message_matches(event, string) do
373+
assert %Sentry.Event{} = event
374+
375+
assert Map.get(event.message, :formatted, "") =~ string, """
376+
Expected the event to have a filled-in message containing the word "Error", but
377+
instead the whole event was:
378+
379+
#{inspect(event, pretty: true, limit: :infinity)}
380+
"""
381+
end
370382
end

test/sentry/logger_handler_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ defmodule Sentry.LoggerHandlerTest do
9191
@tag handler_config: %{excluded_domains: []}
9292
test "sends two errors when a Plug process crashes if cowboy domain is not excluded",
9393
%{sender_ref: ref} do
94+
start_supervised!(Sentry.ExamplePlugApplication, restart: :temporary)
95+
9496
:hackney.get("http://127.0.0.1:8003/error_route", [], "", [])
97+
9598
assert_receive {^ref, _event}, 1000
9699
end
97100
end

test/support/example_plug_application.ex

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ defmodule Sentry.ExamplePlugApplication do
33
use Sentry.PlugCapture
44
use Plug.ErrorHandler
55

6+
import ExUnit.Assertions
7+
68
plug Plug.Parsers, parsers: [:multipart, :urlencoded]
79
plug Sentry.PlugContext
810
plug :match
911
plug :dispatch
1012

11-
get "/error_route" do
12-
_ = conn
13-
raise RuntimeError, "Error"
13+
@spec child_spec(keyword()) :: Supervisor.child_spec()
14+
def child_spec([]) do
15+
Supervisor.child_spec(
16+
{Plug.Cowboy, scheme: :http, plug: __MODULE__, options: [port: 8003]},
17+
[]
18+
)
1419
end
1520

1621
get "/exit_route" do
@@ -23,16 +28,9 @@ defmodule Sentry.ExamplePlugApplication do
2328
throw(:test)
2429
end
2530

26-
post "/error_route" do
27-
_ = conn
28-
raise RuntimeError, "Error"
29-
end
30-
3131
get "/spawn_error_route" do
32-
spawn(fn ->
33-
raise "Error"
34-
end)
35-
32+
{_pid, ref} = spawn_monitor(fn -> raise "Error" end)
33+
assert_receive {:DOWN, ^ref, _, _, _}
3634
send_resp(conn, 200, "")
3735
end
3836

test/test_helper.exs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
ExUnit.start(assert_receive_timeout: 500)
1+
ExUnit.start(assert_receive_timeout: 1000)
22

33
File.rm_rf!(Sentry.Sources.path_of_packaged_source_code())
44

55
ExUnit.after_suite(fn _ ->
66
File.rm_rf!(Sentry.Sources.path_of_packaged_source_code())
77
end)
8-
9-
{:ok, _} = Plug.Cowboy.http(Sentry.ExamplePlugApplication, [], port: 8003)

0 commit comments

Comments
 (0)