Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions lib/live_debugger/gen_servers/callback_tracing_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -199,30 +199,29 @@ defmodule LiveDebugger.GenServers.CallbackTracingServer do
end

defp do_publish(%Trace{} = trace) do
socket_id = trace.socket_id
pid = trace.pid
node_id = Trace.node_id(trace)
transport_pid = trace.transport_pid

fun = trace.function

socket_id
|> PubSubUtils.trace_topic(transport_pid, node_id, fun, :call)
pid
|> PubSubUtils.trace_topic(node_id, fun, :call)
|> PubSubUtils.broadcast({:new_trace, trace})
end

@spec do_publish_update(Trace.t()) :: :ok
defp do_publish_update(trace) do
socket_id = trace.socket_id
pid = trace.pid
node_id = Trace.node_id(trace)
transport_pid = trace.transport_pid
fun = trace.function

if fun == :render do
PubSubUtils.node_rendered_topic()
|> PubSubUtils.broadcast({:render_trace, trace})
end

socket_id
|> PubSubUtils.trace_topic(transport_pid, node_id, fun, :return)
pid
|> PubSubUtils.trace_topic(node_id, fun, :return)
|> PubSubUtils.broadcast({:updated_trace, trace})
end
end
9 changes: 5 additions & 4 deletions lib/live_debugger/gen_servers/state_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,15 @@ defmodule LiveDebugger.GenServers.StateServer do
end

defp publish_state_changed(%Trace{} = trace, channel_state) do
socket_id = trace.socket_id
transport_pid = trace.transport_pid
pid = trace.pid
node_id = trace.cid || trace.pid

PubSubUtils.state_changed_topic(socket_id, transport_pid, node_id)
pid
|> PubSubUtils.state_changed_topic(node_id)
|> PubSubUtils.broadcast({:state_changed, channel_state, trace})

PubSubUtils.state_changed_topic(socket_id, transport_pid)
pid
|> PubSubUtils.state_changed_topic()
|> PubSubUtils.broadcast({:state_changed, channel_state, trace})
end

Expand Down
41 changes: 16 additions & 25 deletions lib/live_debugger/utils/pubsub.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule LiveDebugger.Utils.PubSub do

@doc "Use `{:node_changed, node_id}` for broadcasting"
@spec node_changed_topic(socket_id :: String.t()) :: String.t()
def node_changed_topic(socket_id) when is_binary(socket_id) do
def node_changed_topic(socket_id) do
"lvdbg/#{socket_id}/node_changed"
end

Expand All @@ -44,50 +44,41 @@ defmodule LiveDebugger.Utils.PubSub do
"lvdbg/process_status"
end

@doc "Use `{:render_trace, trace}` for broadcasting."
@spec node_rendered_topic() :: String.t()
def node_rendered_topic() do
"lvdbg/node_rendered"
end

@doc "Use `{:state_changed, new_state, triggered_trace}` for broadcasting"
@spec state_changed_topic(
socket_id :: String.t(),
transport_pid :: pid()
) :: String.t()
def state_changed_topic(socket_id, transport_pid)
when is_pid(transport_pid) and is_binary(socket_id) do
"lvdbg/#{inspect(transport_pid)}/#{socket_id}/*/state_changed"
@spec state_changed_topic(pid :: pid()) :: String.t()
def state_changed_topic(pid) do
"lvdbg/#{inspect(pid)}/*/state_changed"
end

@doc "Use `{:state_changed, new_state, triggered_trace}` for broadcasting"
@spec state_changed_topic(
socket_id :: String.t(),
transport_pid :: pid(),
pid :: pid(),
node_id :: TreeNode.id()
) :: String.t()
def state_changed_topic(socket_id, transport_pid, node_id)
when is_pid(transport_pid) and is_binary(socket_id) do
"lvdbg/#{inspect(transport_pid)}/#{socket_id}/#{inspect(node_id)}/state_changed"
end

@doc "Use `{:render_trace, trace}` for broadcasting."
@spec node_rendered_topic() :: String.t()
def node_rendered_topic() do
"lvdbg/node_rendered"
def state_changed_topic(pid, node_id) do
"lvdbg/#{inspect(pid)}/#{inspect(node_id)}/state_changed"
end

@doc """
It stands for `transport_pid/socket_id/node_id/function/type`.

It gives you traces of given callback in given node in given LiveView
Used to update assigns based on render callback and for filtering traces

Use `{:new_trace, trace}` or `{:updated_trace, trace}` for broadcasting.
"""
@spec trace_topic(
socket_id :: String.t(),
transport_pid :: pid(),
pid :: pid(),
node_id :: TreeNode.id(),
fun :: atom(),
type :: :call | :return
) :: String.t()
def trace_topic(socket_id, transport_pid, node_id, fun, type \\ :call) do
"#{inspect(transport_pid)}/#{socket_id}/#{inspect(node_id)}/#{inspect(fun)}/#{inspect(type)}"
def trace_topic(pid, node_id, fun, type \\ :call) do
"#{inspect(pid)}/#{inspect(node_id)}/#{inspect(fun)}/#{inspect(type)}"
end

@spec impl() :: module()
Expand Down
9 changes: 3 additions & 6 deletions lib/live_debugger_web/helpers/tracing_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,13 @@ defmodule LiveDebuggerWeb.Helpers.TracingHelper do
|> Enum.flat_map(fn {function, _} ->
[
PubSubUtils.trace_topic(
lv_process.socket_id,
lv_process.transport_pid,
lv_process.pid,
node_id,
function,
:call
),
PubSubUtils.trace_topic(
lv_process.socket_id,
lv_process.transport_pid,
lv_process.pid,
node_id,
function,
:return
Expand All @@ -167,8 +165,7 @@ defmodule LiveDebuggerWeb.Helpers.TracingHelper do
socket.assigns.current_filters.functions
|> Enum.map(fn {function, _} ->
PubSubUtils.trace_topic(
lv_process.socket_id,
lv_process.transport_pid,
lv_process.pid,
node_id,
function,
type
Expand Down
4 changes: 2 additions & 2 deletions lib/live_debugger_web/live/sidebar_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ defmodule LiveDebuggerWeb.SidebarLive do
|> PubSubUtils.node_changed_topic()
|> PubSubUtils.subscribe!()

lv_process.socket_id
|> PubSubUtils.state_changed_topic(lv_process.transport_pid)
lv_process.pid
|> PubSubUtils.state_changed_topic()
|> PubSubUtils.subscribe!()
end

Expand Down
12 changes: 6 additions & 6 deletions lib/live_debugger_web/live/state_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ defmodule LiveDebuggerWeb.StateLive do
|> PubSubUtils.node_changed_topic()
|> PubSubUtils.subscribe!()

lv_process.socket_id
|> PubSubUtils.state_changed_topic(lv_process.transport_pid, node_id)
lv_process.pid
|> PubSubUtils.state_changed_topic(node_id)
|> PubSubUtils.subscribe!()
end

Expand Down Expand Up @@ -104,12 +104,12 @@ defmodule LiveDebuggerWeb.StateLive do
lv_process = socket.assigns.lv_process
old_node_id = socket.assigns.node_id

lv_process.socket_id
|> PubSubUtils.state_changed_topic(lv_process.transport_pid, old_node_id)
lv_process.pid
|> PubSubUtils.state_changed_topic(old_node_id)
|> PubSubUtils.unsubscribe()

lv_process.socket_id
|> PubSubUtils.state_changed_topic(lv_process.transport_pid, new_node_id)
lv_process.pid
|> PubSubUtils.state_changed_topic(new_node_id)
|> PubSubUtils.subscribe!()

socket
Expand Down
4 changes: 2 additions & 2 deletions test/gen_servers/callback_tracing_server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ defmodule LiveDebugger.GenServers.CallbackTracingServerTest do
table = :ets.new(:test_table, [:ordered_set, :public])

expected_trace_call_topic =
PubSubUtils.trace_topic(socket_id, transport_pid, pid, fun, :call)
PubSubUtils.trace_topic(pid, pid, fun, :call)

expected_trace_return_topic =
PubSubUtils.trace_topic(socket_id, transport_pid, pid, fun, :return)
PubSubUtils.trace_topic(pid, pid, fun, :return)

MockEtsTableServer
|> expect(:table!, 2, fn ^pid -> table end)
Expand Down
22 changes: 8 additions & 14 deletions test/gen_servers/state_server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,18 @@ defmodule LiveDebugger.GenServers.StateServerTest do
describe "handle_info/2" do
test "handles component deleted trace and updates state" do
pid = :c.pid(0, 1, 0)
transport_pid = :c.pid(0, 7, 0)
socket_id = "socket_id"

:ets.new(StateServer.ets_table_name(), [:named_table, :public, :ordered_set])
:ets.insert(StateServer.ets_table_name(), {inspect(pid), :old_state})

trace =
Fakes.trace(
function: :render,
pid: pid,
transport_pid: transport_pid,
socket_id: socket_id
pid: pid
)

state_changed_node_topic = PubSubUtils.state_changed_topic(socket_id, transport_pid, pid)
state_changed_topic = PubSubUtils.state_changed_topic(socket_id, transport_pid)
state_changed_node_topic = PubSubUtils.state_changed_topic(pid, pid)
state_changed_topic = PubSubUtils.state_changed_topic(pid)

state = Fakes.state()

Expand All @@ -68,21 +65,18 @@ defmodule LiveDebugger.GenServers.StateServerTest do

test "handles render trace and updates state" do
pid = :c.pid(0, 1, 0)
transport_pid = :c.pid(0, 7, 0)
socket_id = "socket_id"

:ets.new(StateServer.ets_table_name(), [:named_table, :public, :ordered_set])
:ets.insert(StateServer.ets_table_name(), {inspect(pid), :old_state})

trace =
Fakes.trace(
function: :render,
pid: pid,
transport_pid: transport_pid,
socket_id: socket_id
pid: pid
)

state_changed_node_topic = PubSubUtils.state_changed_topic(socket_id, transport_pid, pid)
state_changed_topic = PubSubUtils.state_changed_topic(socket_id, transport_pid)
state_changed_node_topic = PubSubUtils.state_changed_topic(pid, pid)
state_changed_topic = PubSubUtils.state_changed_topic(pid)

state = Fakes.state()

Expand Down
15 changes: 11 additions & 4 deletions test/live_debugger/channel_dashboard_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ defmodule LiveDebugger.ChannelDashboardTest do
|> click(button("conditional-button"))

debugger
|> assert_has(many_assigns_15_node_button())
|> assert_has(assigns_entry(key: "show_child?", value: "true"))
|> assert_has(traces(count: 4))
|> click(conditional_component_6_node_button())
Expand Down Expand Up @@ -344,9 +345,15 @@ defmodule LiveDebugger.ChannelDashboardTest do

defp reset_filters_button(), do: css("button[phx-click=\"reset\"]")

defp conditional_component_5_node_button(),
do: css("#tree-node-button-5-component-tree-sidebar-content")
defp conditional_component_5_node_button() do
css("#tree-node-button-5-component-tree-sidebar-content")
end

defp conditional_component_6_node_button() do
css("#tree-node-button-6-component-tree-sidebar-content")
end

defp conditional_component_6_node_button(),
do: css("#tree-node-button-6-component-tree-sidebar-content")
defp many_assigns_15_node_button() do
css("#tree-node-button-15-component-tree-sidebar-content")
end
end
15 changes: 7 additions & 8 deletions test/utils/pubsub_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,18 @@ defmodule LiveDebugger.Utils.PubSubTest do
end

test "trace_topic/4" do
socket_id = "phx-GBsi_6M7paYhySQj"
transport_pid = :c.pid(0, 1, 0)
pid = :c.pid(0, 1, 0)
node_id = :c.pid(0, 2, 0)
fun = :handle_info

assert "#PID<0.1.0>/phx-GBsi_6M7paYhySQj/#PID<0.2.0>/:handle_info/:call" =
PubSubUtils.trace_topic(socket_id, transport_pid, node_id, fun)
assert "#PID<0.1.0>/#PID<0.2.0>/:handle_info/:call" =
PubSubUtils.trace_topic(pid, node_id, fun)

assert "#PID<0.1.0>/phx-GBsi_6M7paYhySQj/#PID<0.2.0>/:handle_info/:call" =
PubSubUtils.trace_topic(socket_id, transport_pid, node_id, fun, :call)
assert "#PID<0.1.0>/#PID<0.2.0>/:handle_info/:call" =
PubSubUtils.trace_topic(pid, node_id, fun, :call)

assert "#PID<0.1.0>/phx-GBsi_6M7paYhySQj/#PID<0.2.0>/:handle_info/:return" =
PubSubUtils.trace_topic(socket_id, transport_pid, node_id, fun, :return)
assert "#PID<0.1.0>/#PID<0.2.0>/:handle_info/:return" =
PubSubUtils.trace_topic(pid, node_id, fun, :return)
end

describe "mock" do
Expand Down