Skip to content

Commit

Permalink
fix api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
filipecabaco committed Dec 10, 2024
1 parent cf7830a commit 5b554ec
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/realtime/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ defmodule Realtime.Api do
data: %{external_id: external_id}
})
when is_map_key(changes, :jwt_jwks) or is_map_key(changes, :jwt_secret) do
Phoenix.PubSub.broadcast!(Realtime.PubSub, "user_socket:" <> external_id, "disconnect")
RealtimeWeb.Endpoint.broadcast("user_socket:" <> external_id, "disconnect", %{})
end

defp maybe_trigger_disconnect(_), do: nil
Expand Down
14 changes: 8 additions & 6 deletions lib/realtime_web/channels/realtime_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,7 @@ defmodule RealtimeWeb.RealtimeChannel do
end
end

def handle_info(:disconnect, %{assigns: %{channel_name: channel_name}} = socket) do
Logger.info("Received operational call to disconnect channel")
push_system_message("system", socket, "ok", "Server requested disconnect", channel_name)
{:stop, :shutdown, socket}
end

@impl true
def handle_info(msg, socket) do
log_error("UnhandledSystemMessage", msg)
{:noreply, socket}
Expand Down Expand Up @@ -429,7 +424,14 @@ defmodule RealtimeWeb.RealtimeChannel do
end

@impl true
def terminate({:shutdown, :closed}, %{assigns: %{channel_name: channel_name}} = socket) do
:telemetry.execute([:prom_ex, :plugin, :realtime, :disconnected], %{})
push_system_message("system", socket, "ok", "Server requested disconnect", channel_name)
:ok
end

def terminate(reason, _state) do
IO.inspect(reason)
Logger.debug("Channel terminated with reason: " <> inspect(reason))
:telemetry.execute([:prom_ex, :plugin, :realtime, :disconnected], %{})
:ok
Expand Down
1 change: 1 addition & 0 deletions test/integration/rt_channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,7 @@ defmodule Realtime.Integration.RtChannelTest do

assert_receive %Phoenix.Socket.Message{event: "phx_reply"}, 500
assert_receive %Phoenix.Socket.Message{event: "presence_state"}, 500

tenant = Tenants.get_tenant_by_external_id(@external_id)
Realtime.Api.update_tenant(tenant, %{jwt_jwks: %{keys: ["potato"]}})

Expand Down
20 changes: 16 additions & 4 deletions test/realtime/api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ defmodule Realtime.ApiTest do

Enum.each(tenants, fn tenant ->
:ok =
Phoenix.PubSub.subscribe(Realtime.PubSub, "realtime:operations:" <> tenant.external_id)
RealtimeWeb.Endpoint.subscribe("user_socket:" <> tenant.external_id)
end)

%{tenants: tenants}
Expand Down Expand Up @@ -128,22 +128,34 @@ defmodule Realtime.ApiTest do
tenants: [tenant | _]
} do
assert {:ok, %Tenant{}} = Api.update_tenant(tenant, %{jwt_jwks: %{keys: ["test"]}})
assert_receive :disconnect

assert_receive %Phoenix.Socket.Broadcast{
topic: "user_socket:external_id1",
event: "disconnect"
}
end

test "update_tenant/2 with valid data and jwt_secret change will send disconnect event", %{
tenants: [tenant | _]
} do
assert {:ok, %Tenant{}} = Api.update_tenant(tenant, %{jwt_secret: "potato"})
assert_receive :disconnect

assert_receive %Phoenix.Socket.Broadcast{
topic: "user_socket:external_id1",
event: "disconnect"
}
end

test "update_tenant/2 with valid data but not updating jwt_secret or jwt_jwks won't send event",
%{
tenants: [tenant | _]
} do
assert {:ok, %Tenant{}} = Api.update_tenant(tenant, %{max_events_per_second: 100})
refute_receive :disconnect

refute_receive %Phoenix.Socket.Broadcast{
topic: "user_socket:external_id1",
event: "disconnect"
}
end

test "delete_tenant/1 deletes the tenant", %{tenants: [tenant | _]} do
Expand Down

0 comments on commit 5b554ec

Please sign in to comment.