-
-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add tests to Realtime.ErlSysMonTest * Add tests to Realtime.SignalHandler * Add tests to Realtime.Telemetry.Logger * Add tests to RealtimeWeb.MetricsController * Add tests to Realtime.MetricsCleaner * Add tests to RealtimeWeb.RealtimeChannel.Logging * Add tests to Realtime.Logs * Skip check on Phoenix telemetry code * Deleted unused RealtimeWeb.TenantMetricsController
- Loading branch information
1 parent
c09b328
commit 3f1114b
Showing
17 changed files
with
279 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule Realtime.LogsTest do | ||
use ExUnit.Case | ||
|
||
describe "Jason.Encoder implementation" do | ||
test "encodes DBConnection.ConnectionError" do | ||
error = %DBConnection.ConnectionError{ | ||
message: "connection lost", | ||
reason: :timeout, | ||
severity: :error | ||
} | ||
|
||
encoded = Jason.encode!(error) | ||
assert encoded =~ "message: \"connection lost\"" | ||
assert encoded =~ "reason: :timeout" | ||
assert encoded =~ "severity: :error" | ||
end | ||
|
||
test "encodes Postgrex.Error" do | ||
error = %Postgrex.Error{ | ||
message: "relation not found", | ||
postgres: %{ | ||
code: "42P01", | ||
schema: "public", | ||
table: "users" | ||
} | ||
} | ||
|
||
encoded = Jason.encode!(error) | ||
assert encoded =~ "message: \"relation not found\"" | ||
assert encoded =~ "schema: \"public\"" | ||
assert encoded =~ "table: \"users\"" | ||
assert encoded =~ "code: \"42P01\"" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
defmodule Realtime.MetricsCleanerTest do | ||
use Realtime.DataCase, async: false | ||
alias Realtime.MetricsCleaner | ||
|
||
setup do | ||
interval = Application.get_env(:realtime, :metrics_cleaner_schedule_timer_in_ms) | ||
Application.put_env(:realtime, :metrics_cleaner_schedule_timer_in_ms, 100) | ||
tenant = tenant_fixture() | ||
|
||
on_exit(fn -> | ||
Application.put_env(:realtime, :metrics_cleaner_schedule_timer_in_ms, interval) | ||
end) | ||
|
||
%{tenant: tenant} | ||
end | ||
|
||
describe "metrics cleanup" do | ||
test "cleans up metrics for users that have been disconnected", %{ | ||
tenant: %{external_id: external_id} | ||
} do | ||
start_supervised!(MetricsCleaner) | ||
{:ok, _} = Realtime.Tenants.Connect.lookup_or_start_connection(external_id) | ||
# Wait for promex to collect the metrics | ||
Process.sleep(6000) | ||
|
||
Realtime.Telemetry.execute( | ||
[:realtime, :connections], | ||
%{connected: 10, connected_cluster: 10, limit: 100}, | ||
%{tenant: external_id} | ||
) | ||
|
||
assert Realtime.PromEx.Metrics | ||
|> :ets.select([{{{:_, %{tenant: :"$1"}}, :_}, [], [:"$1"]}]) | ||
|> Enum.any?(&(&1 == external_id)) | ||
|
||
Realtime.Tenants.Connect.shutdown(external_id) | ||
Process.sleep(200) | ||
|
||
refute Realtime.PromEx.Metrics | ||
|> :ets.select([{{{:_, %{tenant: :"$1"}}, :_}, [], [:"$1"]}]) | ||
|> Enum.any?(&(&1 == external_id)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
defmodule Realtime.ErlSysMonTest do | ||
use ExUnit.Case | ||
import ExUnit.CaptureLog | ||
alias Realtime.ErlSysMon | ||
|
||
describe "system monitoring" do | ||
setup do | ||
Logger.configure(level: :warning) | ||
on_exit(fn -> Logger.configure(level: :error) end) | ||
end | ||
|
||
test "logs system monitor events" do | ||
start_supervised!({ErlSysMon, [{:long_message_queue, {1, 10}}]}) | ||
|
||
assert capture_log(fn -> | ||
Task.async(fn -> Enum.map(1..10000, &send(self(), &1)) end) | ||
|> Task.await() | ||
end) =~ "Realtime.ErlSysMon message: " | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
defmodule Realtime.SignalHandlerTest do | ||
use ExUnit.Case | ||
import ExUnit.CaptureLog | ||
alias Realtime.SignalHandler | ||
import Mock | ||
|
||
defmodule FakeHandler do | ||
def handle_event(:sigterm, _state), do: :ok | ||
end | ||
|
||
setup do | ||
Logger.configure(level: :warning) | ||
|
||
on_exit(fn -> | ||
Logger.configure(level: :error) | ||
Application.put_env(:realtime, :shutdown_in_progress, false) | ||
end) | ||
end | ||
|
||
describe "signal handling" do | ||
test "sends signal to handler_mod" do | ||
with_mock FakeHandler, handle_event: fn :sigterm, _state -> :ok end do | ||
{:ok, state} = SignalHandler.init({%{handler_mod: FakeHandler}, :ok}) | ||
|
||
assert capture_log(fn -> SignalHandler.handle_event(:sigterm, state) end) =~ | ||
"SignalHandler: :sigterm received" | ||
|
||
assert_called_exactly(FakeHandler.handle_event(:sigterm, :_), 1) | ||
end | ||
end | ||
end | ||
|
||
describe "shutdown_in_progress?/1" do | ||
test "shutdown_in_progress? returns error when shutdown is in progress" do | ||
Application.put_env(:realtime, :shutdown_in_progress, true) | ||
assert SignalHandler.shutdown_in_progress?() == {:error, :shutdown_in_progress} | ||
end | ||
|
||
test "shutdown_in_progress? returns ok when no shutdown in progress" do | ||
Application.put_env(:realtime, :shutdown_in_progress, false) | ||
assert SignalHandler.shutdown_in_progress?() == :ok | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
defmodule Realtime.Telemetry.LoggerTest do | ||
use ExUnit.Case | ||
import ExUnit.CaptureLog | ||
alias Realtime.Telemetry.Logger, as: TelemetryLogger | ||
|
||
setup do | ||
Logger.configure(level: :info) | ||
on_exit(fn -> Logger.configure(level: :error) end) | ||
end | ||
|
||
describe "logger backend initialization" do | ||
test "logs on telemetry event" do | ||
start_link_supervised!({TelemetryLogger, handler_id: "telemetry-logger-test"}) | ||
|
||
assert capture_log(fn -> | ||
:telemetry.execute([:realtime, :connections], %{count: 1}, %{tenant: "tenant"}) | ||
end) =~ "Billing metrics: [:realtime, :connections]" | ||
end | ||
|
||
test "ignores events without tenant" do | ||
start_link_supervised!({TelemetryLogger, handler_id: "telemetry-logger-test"}) | ||
|
||
refute capture_log(fn -> | ||
:telemetry.execute([:realtime, :connections], %{count: 1}, %{}) | ||
end) =~ "Billing metrics: [:realtime, :connections]" | ||
end | ||
end | ||
end |
Oops, something went wrong.