Skip to content

Commit 577d385

Browse files
committed
Improve and speed up tests
1 parent b427704 commit 577d385

19 files changed

+168
-202
lines changed

config/config.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ if config_env() == :test do
77
enable_source_code_context: true,
88
root_source_code_paths: [File.cwd!()],
99
source_code_exclude_patterns: [],
10-
hackney_opts: [recv_timeout: 50],
10+
hackney_opts: [recv_timeout: 50, pool: :sentry_pool],
1111
send_result: :sync,
12-
send_max_attempts: 1
12+
send_max_attempts: 1,
13+
dedup_events: false
1314

1415
config :logger, backends: []
1516
end

lib/sentry/config.ex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,16 @@ defmodule Sentry.Config do
462462
def put_config(key, value) when is_atom(key) do
463463
case NimbleOptions.validate([{key, value}], @opts_schema) do
464464
{:ok, options} ->
465-
options |> Keyword.take([key]) |> persist()
465+
renamed_key =
466+
case key do
467+
:before_send_event -> :before_send
468+
other -> other
469+
end
470+
471+
options
472+
|> handle_deprecated_before_send()
473+
|> Keyword.take([renamed_key])
474+
|> persist()
466475

467476
{:error, error} ->
468477
raise ArgumentError, """

lib/sentry/dedupe.ex

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ defmodule Sentry.Dedupe do
1010
@ttl_millisec 30_000
1111

1212
@spec start_link(keyword()) :: GenServer.on_start()
13-
def start_link(opts) when is_list(opts) do
14-
ttl_millisec = Keyword.get(opts, :ttl_millisec, @ttl_millisec)
15-
GenServer.start_link(__MODULE__, ttl_millisec, name: __MODULE__)
13+
def start_link([] = _opts) do
14+
GenServer.start_link(__MODULE__, nil, name: __MODULE__)
1615
end
1716

1817
@spec insert(Event.t()) :: :new | :existing
@@ -27,28 +26,31 @@ defmodule Sentry.Dedupe do
2726
end
2827
end
2928

30-
## State
31-
defstruct [:ttl_millisec]
32-
3329
## Callbacks
3430

3531
@impl true
36-
def init(ttl_millisec) do
32+
def init(nil) do
3733
_table = :ets.new(@ets, [:named_table, :public, :set])
38-
Process.send_after(self(), :sweep, @sweep_interval_millisec)
39-
{:ok, %__MODULE__{ttl_millisec: ttl_millisec}}
34+
schedule_sweep()
35+
{:ok, :no_state}
4036
end
4137

4238
@impl true
43-
def handle_info(:sweep, %__MODULE__{} = state) do
39+
def handle_info({:sweep, ttl_millisec}, state) do
4440
now = System.system_time(:millisecond)
4541

4642
# All rows (which are {hash, inserted_at}) with an inserted_at older than
4743
# now - @ttl_millisec.
48-
match_spec = [{{:"$1", :"$2"}, [], [{:<, :"$2", now - state.ttl_millisec}]}]
44+
match_spec = [{{:"$1", :"$2"}, [], [{:<, :"$2", now - ttl_millisec}]}]
4945
_ = :ets.select_delete(@ets, match_spec)
5046

51-
Process.send_after(self(), :sweep, @sweep_interval_millisec)
47+
schedule_sweep()
5248
{:noreply, state}
5349
end
50+
51+
## Helpers
52+
53+
defp schedule_sweep do
54+
Process.send_after(self(), {:sweep, @ttl_millisec}, @sweep_interval_millisec)
55+
end
5456
end

test/context_test.exs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ defmodule Sentry.ContextTest do
8080
@tag start_app: false
8181
test "passing in tags context as option overrides Context and Application config" do
8282
Context.set_tags_context(%{"key" => "345", "key1" => "123"})
83-
modify_app_env(tags: %{"key" => "overridden", "key2" => "1234", "key3" => "12345"})
84-
83+
put_test_config(tags: %{"key" => "overridden", "key2" => "1234", "key3" => "12345"})
8584
event = Event.create_event(tags: %{"key" => "123"})
8685

8786
assert event.tags == %{"key" => "123", "key1" => "123", "key2" => "1234", "key3" => "12345"}

test/envelope_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ defmodule Sentry.EnvelopeTest do
134134

135135
describe "to_binary/1" do
136136
test "encodes an envelope" do
137-
modify_app_env(environment_name: "test")
138-
137+
put_test_config(environment_name: "test")
139138
event = Event.create_event([])
139+
140140
envelope = Envelope.new([event])
141141

142142
assert {:ok, encoded} = Envelope.to_binary(envelope)

test/event_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ defmodule Sentry.EventTest do
290290
end
291291

292292
test "sets :in_app to true when configured" do
293-
modify_app_env(in_app_module_allow_list: [Sentry, :random, Sentry.Submodule])
293+
put_test_config(in_app_module_allow_list: [Sentry, :random, Sentry.Submodule])
294294
exception = RuntimeError.exception("error")
295295

296296
event =

test/logger_backend_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ defmodule Sentry.LoggerBackendTest do
339339
pid = self()
340340
ref = make_ref()
341341

342-
modify_app_env(
342+
put_test_config(
343343
before_send: fn event ->
344344
send(pid, {ref, event})
345345
false

test/mix/sentry.package_source_code_test.exs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
defmodule Mix.Tasks.Sentry.PackageSourceCodeTest do
22
use ExUnit.Case, async: false
33

4+
import Sentry.TestHelpers
5+
46
setup do
5-
shell = Mix.shell()
6-
Mix.shell(Mix.Shell.Process)
7-
on_exit(fn -> Mix.shell(shell) end)
7+
set_mix_shell(Mix.Shell.Process)
88
end
99

1010
test "packages source code into :sentry's priv directory" do
11+
put_test_config(
12+
root_source_code_paths: [File.cwd!()],
13+
enable_source_code_context: true
14+
)
15+
1116
expected_path =
1217
[Application.app_dir(:sentry), "priv", "sentry.map"]
1318
|> Path.join()

test/mix/sentry.send_test_event_test.exs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defmodule Mix.Tasks.Sentry.SendTestEventTest do
55
import Sentry.TestHelpers
66

77
test "prints if :dsn is not set" do
8-
modify_app_env(dsn: nil)
8+
put_test_config(dsn: nil, hackney_opts: [], environment_name: "some_env")
99

1010
output =
1111
capture_io(fn ->
@@ -14,8 +14,8 @@ defmodule Mix.Tasks.Sentry.SendTestEventTest do
1414

1515
assert output =~ """
1616
Client configuration:
17-
current environment_name: "test"
18-
hackney_opts: [recv_timeout: 50]
17+
current environment_name: "some_env"
18+
hackney_opts: []
1919
"""
2020

2121
assert output =~ ~s(Event not sent because the :dsn option is not set)
@@ -32,7 +32,11 @@ defmodule Mix.Tasks.Sentry.SendTestEventTest do
3232
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
3333
end)
3434

35-
modify_app_env(dsn: "http://public:secret@localhost:#{bypass.port}/1")
35+
put_test_config(
36+
dsn: "http://public:secret@localhost:#{bypass.port}/1",
37+
environment_name: "test",
38+
hackney_opts: []
39+
)
3640

3741
output =
3842
capture_io(fn ->
@@ -45,7 +49,7 @@ defmodule Mix.Tasks.Sentry.SendTestEventTest do
4549
public_key: public
4650
secret_key: secret
4751
current environment_name: "test"
48-
hackney_opts: [recv_timeout: 50]
52+
hackney_opts: []
4953
"""
5054

5155
assert output =~ "Sending test event..."
@@ -62,10 +66,14 @@ defmodule Mix.Tasks.Sentry.SendTestEventTest do
6266
Plug.Conn.resp(conn, 500, ~s<{"id": "340"}>)
6367
end)
6468

65-
modify_app_env(
66-
dsn: "http://public:secret@localhost:#{bypass.port}/1",
67-
request_retries: []
68-
)
69+
original_retries =
70+
Application.get_env(:sentry, :request_retries, Sentry.Transport.default_retries())
71+
72+
on_exit(fn -> Application.put_env(:sentry, :request_retries, original_retries) end)
73+
74+
Application.put_env(:sentry, :request_retries, [])
75+
76+
put_test_config(dsn: "http://public:secret@localhost:#{bypass.port}/1")
6977

7078
assert_raise Mix.Error, ~r/Error sending event/, fn ->
7179
capture_io(fn -> Mix.Tasks.Sentry.SendTestEvent.run([]) end)

test/plug_capture_test.exs

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ defmodule Sentry.PlugCaptureTest do
5050
end
5151
end
5252

53+
setup_all do
54+
Application.put_env(:sentry, PhoenixEndpoint,
55+
render_errors: [view: Sentry.ErrorView, accepts: ~w(html)]
56+
)
57+
58+
:ok
59+
end
60+
5361
test "sends error to Sentry" do
5462
bypass = Bypass.open()
5563

@@ -61,7 +69,7 @@ defmodule Sentry.PlugCaptureTest do
6169
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
6270
end)
6371

64-
modify_app_env(dsn: "http://public:secret@localhost:#{bypass.port}/1")
72+
put_test_config(dsn: "http://public:secret@localhost:#{bypass.port}/1")
6573

6674
assert_raise(Plug.Conn.WrapperError, "** (RuntimeError) Error", fn ->
6775
conn(:get, "/error_route")
@@ -80,7 +88,7 @@ defmodule Sentry.PlugCaptureTest do
8088
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
8189
end)
8290

83-
modify_app_env(dsn: "http://public:secret@localhost:#{bypass.port}/1")
91+
put_test_config(dsn: "http://public:secret@localhost:#{bypass.port}/1")
8492

8593
catch_throw(
8694
conn(:get, "/throw_route")
@@ -99,7 +107,7 @@ defmodule Sentry.PlugCaptureTest do
99107
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
100108
end)
101109

102-
modify_app_env(dsn: "http://public:secret@localhost:#{bypass.port}/1")
110+
put_test_config(dsn: "http://public:secret@localhost:#{bypass.port}/1")
103111

104112
catch_exit(
105113
conn(:get, "/exit_route")
@@ -122,7 +130,7 @@ defmodule Sentry.PlugCaptureTest do
122130
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
123131
end)
124132

125-
modify_app_env(dsn: "http://public:secret@localhost:#{bypass.port}/1")
133+
put_test_config(dsn: "http://public:secret@localhost:#{bypass.port}/1")
126134

127135
assert_raise(Plug.Conn.WrapperError, "** (RuntimeError) Error", fn ->
128136
conn(:get, "/error_route")
@@ -141,7 +149,7 @@ defmodule Sentry.PlugCaptureTest do
141149
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
142150
end)
143151

144-
modify_app_env(dsn: "http://public:secret@localhost:#{bypass.port}/1")
152+
put_test_config(dsn: "http://public:secret@localhost:#{bypass.port}/1")
145153

146154
assert_raise(Plug.Conn.WrapperError, "** (RuntimeError) Error", fn ->
147155
conn(:get, "/error_route")
@@ -175,12 +183,7 @@ defmodule Sentry.PlugCaptureTest do
175183
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
176184
end)
177185

178-
modify_app_env(
179-
dsn: "http://public:secret@localhost:#{bypass.port}/1",
180-
"#{__MODULE__.PhoenixEndpoint}": [
181-
render_errors: [view: Sentry.ErrorView, accepts: ~w(html)]
182-
]
183-
)
186+
put_test_config(dsn: "http://public:secret@localhost:#{bypass.port}/1")
184187

185188
{:ok, _} = PhoenixEndpoint.start_link()
186189

@@ -205,12 +208,7 @@ defmodule Sentry.PlugCaptureTest do
205208
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
206209
end)
207210

208-
modify_app_env(
209-
dsn: "http://public:secret@localhost:#{bypass.port}/1",
210-
"#{__MODULE__.PhoenixEndpoint}": [
211-
render_errors: [view: Sentry.ErrorView, accepts: ~w(html)]
212-
]
213-
)
211+
put_test_config(dsn: "http://public:secret@localhost:#{bypass.port}/1")
214212

215213
{:ok, _} = PhoenixEndpoint.start_link()
216214

@@ -232,12 +230,7 @@ defmodule Sentry.PlugCaptureTest do
232230
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
233231
end)
234232

235-
modify_app_env(
236-
dsn: "http://public:secret@localhost:#{bypass.port}/1",
237-
"#{__MODULE__.PhoenixEndpoint}": [
238-
render_errors: [view: Sentry.ErrorView, accepts: ~w(html)]
239-
]
240-
)
233+
put_test_config(dsn: "http://public:secret@localhost:#{bypass.port}/1")
241234

242235
{:ok, _} = PhoenixEndpoint.start_link()
243236

@@ -269,7 +262,7 @@ defmodule Sentry.PlugCaptureTest do
269262
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
270263
end)
271264

272-
modify_app_env(dsn: "http://public:secret@localhost:#{bypass.port}/1")
265+
put_test_config(dsn: "http://public:secret@localhost:#{bypass.port}/1")
273266

274267
start_supervised!(PhoenixEndpoint)
275268

@@ -300,12 +293,7 @@ defmodule Sentry.PlugCaptureTest do
300293
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
301294
end)
302295

303-
modify_app_env(
304-
dsn: "http://public:secret@localhost:#{bypass.port}/1",
305-
"#{__MODULE__.PhoenixEndpoint}": [
306-
render_errors: [view: Sentry.ErrorView, accepts: ~w(html)]
307-
]
308-
)
296+
put_test_config(dsn: "http://public:secret@localhost:#{bypass.port}/1")
309297

310298
{:ok, _} = PhoenixEndpoint.start_link()
311299

@@ -337,7 +325,7 @@ defmodule Sentry.PlugCaptureTest do
337325
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
338326
end)
339327

340-
modify_app_env(dsn: "http://public:secret@localhost:#{bypass.port}/1")
328+
put_test_config(dsn: "http://public:secret@localhost:#{bypass.port}/1")
341329

342330
conn = conn(:get, "/error_route")
343331

@@ -363,12 +351,7 @@ defmodule Sentry.PlugCaptureTest do
363351
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
364352
end)
365353

366-
modify_app_env(
367-
dsn: "http://public:secret@localhost:#{bypass.port}/1",
368-
"#{__MODULE__.PhoenixEndpoint}": [
369-
render_errors: [view: Sentry.ErrorView, accepts: ~w(html)]
370-
]
371-
)
354+
put_test_config(dsn: "http://public:secret@localhost:#{bypass.port}/1")
372355

373356
{:ok, _} = PhoenixEndpoint.start_link()
374357

0 commit comments

Comments
 (0)