Skip to content

Commit

Permalink
test: integration test for stats on other nodes (#560)
Browse files Browse the repository at this point in the history
  • Loading branch information
hauleth authored Feb 4, 2025
1 parent 61a0d68 commit 43d6c9f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 38 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.1
2.3.2
2 changes: 1 addition & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ config :supavisor, Supavisor.Vault,

# Print only warnings and errors during test
config :logger, :console,
level: :none,
level: String.to_atom(System.get_env("LOGGER_LEVEL", "none")),
metadata: [:error_code, :file, :line, :pid, :project, :user, :mode]

# Initialize plugs at runtime for faster test compilation
Expand Down
107 changes: 71 additions & 36 deletions test/supavisor/client_handler/stats_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,47 @@ defmodule Supavisor.ClientHandler.StatsTest do

# Listen on Telemetry events
setup ctx do
ref = make_ref()

:telemetry.attach(
{ctx.test, :client},
[:supavisor, :client, :network, :stat],
&__MODULE__.handle_event/4,
self()
{self(), ref}
)

:telemetry.attach(
{ctx.test, :db},
[:supavisor, :db, :network, :stat],
&__MODULE__.handle_event/4,
self()
{self(), ref}
)

on_exit(fn ->
:telemetry.detach({ctx.test, :client})
:telemetry.detach({ctx.test, :db})
end)

{:ok, telemetry: ref}
end

def handle_event([:supavisor, name, :network, :stat], measurement, meta, pid) do
send(pid, {:telemetry, {name, measurement, meta}})
def handle_event([:supavisor, name, :network, :stat], measurement, meta, {pid, ref}) do
send(pid, {ref, {name, measurement, meta}, Node.self()})
end

setup ctx do
create_instance([__MODULE__, ctx.line])
if !ctx[:external_id] do
create_instance([__MODULE__, ctx.line])
else
{:ok, db: "postgres", user: "postgres.#{ctx.external_id}"}
end
end

# Connect to the instance
setup ctx do
conn =
start_supervised!(
{Postgrex,
{SingleConnection,
hostname: "localhost",
port: Application.fetch_env!(:supavisor, :proxy_port_transaction),
database: ctx.db,
Expand All @@ -49,39 +57,36 @@ defmodule Supavisor.ClientHandler.StatsTest do
end

describe "client network usage" do
test "increase on query", ctx do
external_id = ctx.external_id

assert {:ok, _} = Postgrex.query(ctx.conn, "SELECT 1", [])
test "increase on query", %{telemetry: telemetry, conn: conn, external_id: external_id} do
assert {:ok, _} = SingleConnection.query(conn, "SELECT 1")

assert_receive {:telemetry,
{:client, %{recv_oct: recv, send_oct: sent}, %{tenant: ^external_id}}}
assert_receive {^telemetry,
{:client, %{recv_oct: recv, send_oct: sent}, %{tenant: ^external_id}}, _}

assert recv > 0
assert sent > 0
end

test "increase on just auth", ctx do
external_id = ctx.external_id

assert_receive {:telemetry,
{:client, %{recv_oct: recv, send_oct: sent}, %{tenant: ^external_id}}}
test "increase on just auth", %{external_id: external_id, telemetry: telemetry} do
assert_receive {^telemetry,
{:client, %{recv_oct: recv, send_oct: sent}, %{tenant: ^external_id}}, _}

assert recv > 0
assert sent > 0
end

test "do not not increase if other tenant is used", ctx do
external_id = ctx.external_id

test "do not not increase if other tenant is used", %{
external_id: external_id,
telemetry: telemetry
} do
{:ok, other} = create_instance([__MODULE__, "another"])

# Cleanup initial data related to sign in
assert_receive {:telemetry, {:client, _, %{tenant: ^external_id}}}
assert_receive {^telemetry, {:client, _, %{tenant: ^external_id}}, _}

other_conn =
start_supervised!(
{Postgrex,
{SingleConnection,
hostname: "localhost",
port: Application.fetch_env!(:supavisor, :proxy_port_transaction),
database: other.db,
Expand All @@ -90,46 +95,76 @@ defmodule Supavisor.ClientHandler.StatsTest do
id: :postgrex_another
)

assert {:ok, _} = Postgrex.query(other_conn, "SELECT 1", [])
assert {:ok, _} = SingleConnection.query(other_conn, "SELECT 1")

refute_receive {^telemetry, {:client, _, %{tenant: ^external_id}}, _}
end

@tag external_id: "proxy_tenant1"
test "another instance do not send events here", %{telemetry: telemetry} = ctx do
assert {:ok, _pid, node} = Supavisor.Support.Cluster.start_node()

Check warning on line 105 in test/supavisor/client_handler/stats_test.exs

View workflow job for this annotation

GitHub Actions / Code style

Nested modules could be aliased at the top of the invoking module.

:erpc.call(node, :telemetry, :attach, [
{ctx.test, :client},
[:supavisor, :client, :network, :stat],
&__MODULE__.handle_event/4,
self()
])

other_conn =
start_supervised!(
{SingleConnection,
hostname: "localhost",
port: Application.fetch_env!(:supavisor, :secondary_proxy_port),
database: ctx.db,
username: ctx.user,
password: "postgres"},
id: :postgrex_another
)

assert {:ok, _} = SingleConnection.query(other_conn, "SELECT 1")

this = Node.self()

refute_receive {:telemetry, {:client, _, %{tenant: ^external_id}}}
refute_receive {^telemetry, {:client, _, %{tenant: "proxy_tenant1"}}, ^node}
assert_receive {^telemetry, {:client, _, %{tenant: "proxy_tenant1"}}, ^this}, 1000
end
end

describe "server network usage" do
test "increase on query", ctx do
test "increase on query", %{telemetry: telemetry} = ctx do
external_id = ctx.external_id

assert {:ok, _} = Postgrex.query(ctx.conn, "SELECT 1", [])
assert {:ok, _} = SingleConnection.query(ctx.conn, "SELECT 1")

assert_receive {:telemetry,
{:db, %{recv_oct: recv, send_oct: sent}, %{tenant: ^external_id}}}
assert_receive {^telemetry,
{:db, %{recv_oct: recv, send_oct: sent}, %{tenant: ^external_id}}, _}

assert recv > 0
assert sent > 0
end

test "increase on just auth", ctx do
test "increase on just auth", %{telemetry: telemetry} = ctx do
external_id = ctx.external_id

assert_receive {:telemetry,
{:db, %{recv_oct: recv, send_oct: sent}, %{tenant: ^external_id}}}
assert_receive {^telemetry,
{:db, %{recv_oct: recv, send_oct: sent}, %{tenant: ^external_id}}, _}

assert recv > 0
assert sent > 0
end

test "do not not increase if other tenant is used", ctx do
test "do not not increase if other tenant is used", %{telemetry: telemetry} = ctx do
external_id = ctx.external_id

{:ok, other} = create_instance([__MODULE__, "another"])

# Cleanup initial data related to sign in
assert_receive {:telemetry, {:db, _, %{tenant: ^external_id}}}
assert_receive {^telemetry, {:db, _, %{tenant: ^external_id}}, _}

other_conn =
start_supervised!(
{Postgrex,
{SingleConnection,
hostname: "localhost",
port: Application.fetch_env!(:supavisor, :proxy_port_transaction),
database: other.db,
Expand All @@ -138,9 +173,9 @@ defmodule Supavisor.ClientHandler.StatsTest do
id: :postgrex_another
)

assert {:ok, _} = Postgrex.query(other_conn, "SELECT 1", [])
assert {:ok, _} = SingleConnection.query(other_conn, "SELECT 1")

refute_receive {:telemetry, {:db, _, %{tenant: ^external_id}}}
refute_receive {^telemetry, {:db, _, %{tenant: ^external_id}}, _}
end
end
end
7 changes: 7 additions & 0 deletions test/support/fixtures/single_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ defmodule SingleConnection do

@behaviour P.SimpleConnection

def query(pid, query) do
case P.SimpleConnection.call(pid, {:query, query}) do
[%P.Result{} = result] -> {:ok, result}
other -> {:error, other}
end
end

def child_spec(conf) do
%{
id: {__MODULE__, System.unique_integer()},
Expand Down

0 comments on commit 43d6c9f

Please sign in to comment.