Skip to content

Commit

Permalink
Handle impl function raising by raising in the client process
Browse files Browse the repository at this point in the history
- Raising in the server would give the wrong behavior

Co-Authored-By: JB Steadman <[email protected]>
  • Loading branch information
brandonduff and jbsf2 committed Jul 18, 2023
1 parent ee41e62 commit d9bee65
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/protomock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ defmodule ProtoMock do

ref ->
receive do
{^ref, {:protomock_error, e}} -> raise e
{^ref, response} -> response
end
end
Expand Down Expand Up @@ -453,7 +454,11 @@ defmodule ProtoMock do
ref = make_ref()

Task.async(fn ->
response = Kernel.apply(impl, args)
response = try do
Kernel.apply(impl, args)
rescue
e -> {:protomock_error, e}
end
send(from_pid, {ref, response})
end)

Expand Down
10 changes: 10 additions & 0 deletions test/protomock_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,16 @@ defmodule ProtoMockTest do
end
end

describe "invoke" do
test "raises errors that the impl function raises in the client" do
protomock =
ProtoMock.new()
|> ProtoMock.stub(&Calculator.add/3, fn _x, _y -> raise "crash" end)

assert_raise RuntimeError, "crash", fn -> Calculator.add(protomock, 1, 2) end
end
end

defp mock_add() do
mock_add(ProtoMock.new())
end
Expand Down

0 comments on commit d9bee65

Please sign in to comment.