Skip to content

Commit 7f42116

Browse files
committed
chore(tests): refactor logger handler tests for improved isolation
1 parent bb6edb2 commit 7f42116

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

test/sentry/logger_handler_test.exs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ defmodule Sentry.LoggerHandlerTest do
1515
@moduletag :skip
1616
end
1717

18-
@handler_name :sentry_handler
19-
2018
setup :register_before_send
2119
setup :add_handler
2220

@@ -33,15 +31,15 @@ defmodule Sentry.LoggerHandlerTest do
3331
end)
3432
end
3533

36-
test "skips logs from a lower level than the configured one" do
34+
test "skips logs from a lower level than the configured one", %{handler_name: handler_name} do
3735
# Default level is :error, so this doesn't get reported.
3836
Logger.warning("Warning message")
3937

4038
# Change the level to :info and make sure that :debug messages are not reported.
41-
assert :ok = :logger.update_handler_config(@handler_name, :level, :info)
39+
assert :ok = :logger.update_handler_config(handler_name, :level, :info)
4240

4341
on_exit(fn ->
44-
:logger.update_handler_config(@handler_name, :level, :error)
42+
:logger.update_handler_config(handler_name, :level, :error)
4543
end)
4644

4745
Logger.debug("Debug message")
@@ -557,11 +555,12 @@ defmodule Sentry.LoggerHandlerTest do
557555
rate_limiting: [max_events: 2, interval: 150],
558556
capture_log_messages: true
559557
}
560-
test "works with changing config to disable rate limiting", %{sender_ref: ref} do
561-
assert {:ok, %{config: config}} = :logger.get_handler_config(@handler_name)
558+
test "works with changing config to disable rate limiting",
559+
%{sender_ref: ref, handler_name: handler_name} do
560+
assert {:ok, %{config: config}} = :logger.get_handler_config(handler_name)
562561

563562
:ok =
564-
:logger.update_handler_config(@handler_name, :config, put_in(config.rate_limiting, nil))
563+
:logger.update_handler_config(handler_name, :config, put_in(config.rate_limiting, nil))
565564

566565
for index <- 1..10 do
567566
message = "Message #{index}"
@@ -571,18 +570,19 @@ defmodule Sentry.LoggerHandlerTest do
571570
end
572571

573572
@tag handler_config: %{capture_log_messages: true}
574-
test "works with changing config to enable rate limiting", %{sender_ref: ref} do
573+
test "works with changing config to enable rate limiting",
574+
%{sender_ref: ref, handler_name: handler_name} do
575575
for index <- 1..10 do
576576
message = "Message #{index}"
577577
Logger.error(message)
578578
assert_receive {^ref, %{message: %{formatted: ^message}}}
579579
end
580580

581-
assert {:ok, %{config: config}} = :logger.get_handler_config(@handler_name)
581+
assert {:ok, %{config: config}} = :logger.get_handler_config(handler_name)
582582

583583
:ok =
584584
:logger.update_handler_config(
585-
@handler_name,
585+
handler_name,
586586
:config,
587587
put_in(config.rate_limiting, max_events: 1, interval: 100)
588588
)
@@ -598,12 +598,13 @@ defmodule Sentry.LoggerHandlerTest do
598598
rate_limiting: [max_events: 2, interval: 100],
599599
capture_log_messages: true
600600
}
601-
test "works with changing config to update rate limiting", %{sender_ref: ref} do
602-
assert {:ok, %{config: config}} = :logger.get_handler_config(@handler_name)
601+
test "works with changing config to update rate limiting",
602+
%{sender_ref: ref, handler_name: handler_name} do
603+
assert {:ok, %{config: config}} = :logger.get_handler_config(handler_name)
603604

604605
:ok =
605606
:logger.update_handler_config(
606-
@handler_name,
607+
handler_name,
607608
:config,
608609
put_in(config.rate_limiting, max_events: 1, interval: 100)
609610
)
@@ -619,12 +620,13 @@ defmodule Sentry.LoggerHandlerTest do
619620
rate_limiting: [max_events: 2, interval: 100],
620621
capture_log_messages: true
621622
}
622-
test "works with changing config but without changing rate limiting", %{sender_ref: ref} do
623-
assert {:ok, %{config: config}} = :logger.get_handler_config(@handler_name)
623+
test "works with changing config but without changing rate limiting",
624+
%{sender_ref: ref, handler_name: handler_name} do
625+
assert {:ok, %{config: config}} = :logger.get_handler_config(handler_name)
624626

625627
:ok =
626628
:logger.update_handler_config(
627-
@handler_name,
629+
handler_name,
628630
:config,
629631
put_in(config.rate_limiting, max_events: 2, interval: 100)
630632
)
@@ -662,8 +664,8 @@ defmodule Sentry.LoggerHandlerTest do
662664
@tag handler_config: %{
663665
sync_threshold: 2
664666
}
665-
test "cannot set discard_threshold and sync_threshold" do
666-
assert {:ok, %{config: config}} = :logger.get_handler_config(@handler_name)
667+
test "cannot set discard_threshold and sync_threshold", %{handler_name: handler_name} do
668+
assert {:ok, %{config: config}} = :logger.get_handler_config(handler_name)
667669

668670
assert {:error,
669671
{:callback_crashed,
@@ -673,7 +675,7 @@ defmodule Sentry.LoggerHandlerTest do
673675
":sync_threshold and :discard_threshold cannot be used together, one of them must be nil"
674676
}, _}}} =
675677
:logger.update_handler_config(
676-
@handler_name,
678+
handler_name,
677679
:config,
678680
Map.put(config, :discard_threshold, 1)
679681
)
@@ -717,17 +719,24 @@ defmodule Sentry.LoggerHandlerTest do
717719
end
718720

719721
defp add_handler(context) do
722+
# Use a unique handler name per test for proper isolation. This ensures
723+
# that events from one test's cleanup cannot be captured by another test's
724+
# handler, since each test has its own dedicated handler.
725+
handler_name = :"sentry_handler_#{System.unique_integer([:positive])}"
726+
720727
handler_config =
721728
case Map.fetch(context, :handler_config) do
722729
{:ok, config} -> %{config: config}
723730
:error -> %{}
724731
end
725732

726-
assert :ok = :logger.add_handler(@handler_name, Sentry.LoggerHandler, handler_config)
733+
assert :ok = :logger.add_handler(handler_name, Sentry.LoggerHandler, handler_config)
727734

728735
on_exit(fn ->
729-
_ = :logger.remove_handler(@handler_name)
736+
_ = :logger.remove_handler(handler_name)
730737
end)
738+
739+
%{handler_name: handler_name}
731740
end
732741

733742
defp run_and_catch_exit(test_genserver_pid, fun) do

0 commit comments

Comments
 (0)