Skip to content

Commit eb03e9d

Browse files
committed
compile time check
1 parent aff3f66 commit eb03e9d

1 file changed

Lines changed: 58 additions & 52 deletions

File tree

lib/phoenix_live_view/test/live_view_test.ex

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,70 +1895,76 @@ defmodule Phoenix.LiveViewTest do
18951895
end)
18961896
end
18971897

1898-
@doc false
1899-
def __start_assert_will_receive__(%View{pid: pid}) do
1900-
if not (Code.ensure_loaded?(:trace) and function_exported?(:trace, :session_create, 3)) do
1901-
raise "assert_will_receive requires Erlang/OTP 27 or later"
1902-
end
1903-
1904-
ref = make_ref()
1905-
parent = self()
1906-
tracer = spawn(fn -> assert_will_receive_loop(parent, ref) end)
1907-
session = :trace.session_create(:phoenix_live_view_test, tracer, [])
1898+
if Code.ensure_loaded?(:trace) and function_exported?(:trace, :session_create, 3) do
1899+
@doc false
1900+
def __start_assert_will_receive__(%View{pid: pid}) do
1901+
ref = make_ref()
1902+
parent = self()
1903+
tracer = spawn(fn -> assert_will_receive_loop(parent, ref) end)
1904+
session = :trace.session_create(:phoenix_live_view_test, tracer, [])
19081905

1909-
try do
1910-
:trace.process(session, pid, true, [:receive])
1911-
{ref, {tracer, session}}
1912-
rescue
1913-
exception ->
1914-
__stop_assert_will_receive__(ref, {tracer, session})
1915-
reraise exception, __STACKTRACE__
1916-
catch
1917-
kind, reason ->
1918-
__stop_assert_will_receive__(ref, {tracer, session})
1919-
:erlang.raise(kind, reason, __STACKTRACE__)
1906+
try do
1907+
:trace.process(session, pid, true, [:receive])
1908+
{ref, {tracer, session}}
1909+
rescue
1910+
exception ->
1911+
__stop_assert_will_receive__(ref, {tracer, session})
1912+
reraise exception, __STACKTRACE__
1913+
catch
1914+
kind, reason ->
1915+
__stop_assert_will_receive__(ref, {tracer, session})
1916+
:erlang.raise(kind, reason, __STACKTRACE__)
1917+
end
19201918
end
1921-
end
19221919

1923-
@doc false
1924-
def __stop_assert_will_receive__(ref, {tracer, session}) do
1925-
:trace.session_destroy(session)
1920+
@doc false
1921+
def __stop_assert_will_receive__(ref, {tracer, session}) do
1922+
:trace.session_destroy(session)
19261923

1927-
monitor_ref = Process.monitor(tracer)
1928-
send(tracer, {ref, :stop})
1924+
monitor_ref = Process.monitor(tracer)
1925+
send(tracer, {ref, :stop})
19291926

1930-
receive do
1931-
{:DOWN, ^monitor_ref, :process, ^tracer, _reason} ->
1932-
:ok
1933-
after
1934-
5_000 ->
1935-
Process.exit(tracer, :kill)
1927+
receive do
1928+
{:DOWN, ^monitor_ref, :process, ^tracer, _reason} ->
1929+
:ok
1930+
after
1931+
5_000 ->
1932+
Process.exit(tracer, :kill)
19361933

1937-
receive do
1938-
{:DOWN, ^monitor_ref, :process, ^tracer, _reason} -> :ok
1939-
end
1940-
end
1934+
receive do
1935+
{:DOWN, ^monitor_ref, :process, ^tracer, _reason} -> :ok
1936+
end
1937+
end
19411938

1942-
flush_assert_will_receive(ref)
1943-
end
1939+
flush_assert_will_receive(ref)
1940+
end
19441941

1945-
defp assert_will_receive_loop(parent, ref) do
1946-
receive do
1947-
{:trace, _pid, :receive, message} ->
1948-
send(parent, {ref, message})
1949-
assert_will_receive_loop(parent, ref)
1942+
defp assert_will_receive_loop(parent, ref) do
1943+
receive do
1944+
{:trace, _pid, :receive, message} ->
1945+
send(parent, {ref, message})
1946+
assert_will_receive_loop(parent, ref)
19501947

1951-
{^ref, :stop} ->
1952-
:ok
1948+
{^ref, :stop} ->
1949+
:ok
1950+
end
19531951
end
1954-
end
19551952

1956-
defp flush_assert_will_receive(ref) do
1957-
receive do
1958-
{^ref, _message} -> flush_assert_will_receive(ref)
1959-
after
1960-
0 -> :ok
1953+
defp flush_assert_will_receive(ref) do
1954+
receive do
1955+
{^ref, _message} -> flush_assert_will_receive(ref)
1956+
after
1957+
0 -> :ok
1958+
end
1959+
end
1960+
else
1961+
@doc false
1962+
def __start_assert_will_receive__(%View{}) do
1963+
raise "assert_will_receive requires Erlang/OTP 27 or later"
19611964
end
1965+
1966+
@doc false
1967+
def __stop_assert_will_receive__(_ref, _tracer), do: :ok
19621968
end
19631969

19641970
@doc """

0 commit comments

Comments
 (0)