Skip to content

Commit 7d09c2b

Browse files
fishcakezJosé Valim
authored and
José Valim
committed
Wait for message before crash in exception tests
Signed-off-by: José Valim <[email protected]>
1 parent f386467 commit 7d09c2b

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

lib/elixir/test/elixir/exception_test.exs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,16 @@ defmodule ExceptionTest do
264264
end
265265

266266
test "format_exit with call with exception" do
267+
Process.flag(:trap_exit, true)
267268
# Fake reason to prevent error_logger printing to stdout
268-
fsm_reason = {%ArgumentError{}, [{:not_a_real_module, :function, 0, []}]}
269-
reason = try do
270-
:gen_fsm.sync_send_event(spawn(fn() ->
271-
Process.sleep(200)
272-
exit(fsm_reason)
273-
end), :hello)
274-
catch
275-
:exit, reason -> reason
276-
end
269+
exit_reason = {%ArgumentError{}, [{:not_a_real_module, :function, 0, []}]}
270+
exit_fun = fn() -> receive do: (_ -> exit(exit_reason)) end
271+
reason =
272+
try do
273+
:gen_fsm.sync_send_event(spawn_link(exit_fun), :hello)
274+
catch
275+
:exit, reason -> reason
276+
end
277277

278278
formatted = Exception.format_exit(reason)
279279
assert formatted =~ ~r"exited in: :gen_fsm\.sync_send_event\(#PID<\d+\.\d+\.\d+>, :hello\)"
@@ -283,14 +283,16 @@ defmodule ExceptionTest do
283283
end
284284

285285
test "format_exit with nested calls" do
286+
Process.flag(:trap_exit, true)
286287
# Fake reason to prevent error_logger printing to stdout
287-
event_fun = fn() ->
288-
Process.sleep(200)
289-
exit(:normal)
290-
end
291-
server_pid = spawn(fn()-> :gen_event.call(spawn(event_fun), :handler, :hello) end)
288+
exit_fun = fn() -> receive do: (_ -> exit(:normal)) end
289+
outer_pid =
290+
spawn_link(fn()->
291+
Process.flag(:trap_exit, true)
292+
:gen_event.call(spawn_link(exit_fun), :handler, :hello)
293+
end)
292294
reason = try do
293-
:gen_server.call(server_pid, :hi)
295+
:gen_server.call(outer_pid, :hi)
294296
catch
295297
:exit, reason -> reason
296298
end
@@ -302,15 +304,17 @@ defmodule ExceptionTest do
302304
end
303305

304306
test "format_exit with nested calls and exception" do
307+
Process.flag(:trap_exit, true)
305308
# Fake reason to prevent error_logger printing to stdout
306-
event_reason = {%ArgumentError{}, [{:not_a_real_module, :function, 0, []}]}
307-
event_fun = fn() ->
308-
Process.sleep(200)
309-
exit(event_reason)
310-
end
311-
server_pid = spawn(fn()-> :gen_event.call(spawn(event_fun), :handler, :hello) end)
309+
exit_reason = {%ArgumentError{}, [{:not_a_real_module, :function, 0, []}]}
310+
exit_fun = fn() -> receive do: (_ -> exit(exit_reason)) end
311+
outer_pid =
312+
spawn_link(fn()->
313+
Process.flag(:trap_exit, true)
314+
:gen_event.call(spawn_link(exit_fun), :handler, :hello)
315+
end)
312316
reason = try do
313-
:gen_server.call(server_pid, :hi)
317+
:gen_server.call(outer_pid, :hi)
314318
catch
315319
:exit, reason -> reason
316320
end

0 commit comments

Comments
 (0)