Skip to content

Commit 008df0e

Browse files
authored
fix: catch exits from crashing genserver in tests (#444)
* fix: catch exits from crashing genserver * refactor: simplify test structurue * refactor: simplify test structure a lot * chore: format
1 parent b8927bd commit 008df0e

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

test/grpc/server/adapters/report_exception_test.exs

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ defmodule ExceptionServer do
88
{:ok, pid}
99
end
1010

11+
def start_link(pid) do
12+
GenServer.start_link(__MODULE__, pid)
13+
end
14+
1115
@impl true
1216
def handle_cast(:case_boom, state) do
1317
a = fn -> :ok end
@@ -51,41 +55,39 @@ defmodule GRPC.Server.Adapters.ReportExceptionTest do
5155
end
5256

5357
test "with case clause error" do
54-
{:ok, pid} = GenServer.start_link(ExceptionServer, self())
55-
58+
# no link to avoid the genserver crashing and propagating the exit signal to the test
59+
assert {:ok, pid} = start_supervised({ExceptionServer, self()})
5660
GenServer.cast(pid, :case_boom)
5761

58-
receive do
59-
{:boom, {_reason, stack} = err} ->
60-
assert %GRPC.Server.Adapters.ReportException{
61-
__exception__: true,
62-
adapter_extra: [req: :ok],
63-
kind: :error,
64-
reason: %CaseClauseError{term: :ok},
65-
stack: stack
66-
} == ReportException.new([{:req, :ok}], err)
67-
end
62+
assert_receive {:boom, {_reason, stack} = err}
63+
64+
assert %GRPC.Server.Adapters.ReportException{
65+
__exception__: true,
66+
adapter_extra: [req: :ok],
67+
kind: :error,
68+
reason: %CaseClauseError{term: :ok},
69+
stack: stack
70+
} == ReportException.new([{:req, :ok}], err)
6871
end
6972

7073
test "with badarg error" do
71-
{:ok, pid} = GenServer.start_link(ExceptionServer, self())
72-
74+
# no link to avoid the genserver crashing and propagating the exit signal to the test
75+
assert {:ok, pid} = start_supervised({ExceptionServer, self()})
7376
GenServer.cast(pid, :bad_arg_boom)
7477

75-
receive do
76-
{:boom, {_reason, stack} = err} ->
77-
assert %GRPC.Server.Adapters.ReportException{
78-
__exception__: true,
79-
adapter_extra: [req: :ok],
80-
kind: :error,
81-
reason: %ArgumentError{
82-
__exception__: true,
83-
message:
84-
"errors were found at the given arguments:\n\n * 1st argument: the table identifier does not refer to an existing ETS table\n * 2nd argument: not a tuple\n"
85-
},
86-
stack: stack
87-
} == ReportException.new([{:req, :ok}], err)
88-
end
78+
assert_receive {:boom, {_reason, stack} = err}
79+
80+
assert %GRPC.Server.Adapters.ReportException{
81+
__exception__: true,
82+
adapter_extra: [req: :ok],
83+
kind: :error,
84+
reason: %ArgumentError{
85+
__exception__: true,
86+
message:
87+
"errors were found at the given arguments:\n\n * 1st argument: the table identifier does not refer to an existing ETS table\n * 2nd argument: not a tuple\n"
88+
},
89+
stack: stack
90+
} == ReportException.new([{:req, :ok}], err)
8991
end
9092
end
9193
end

0 commit comments

Comments
 (0)