From d9bee653f693b72e76ede628fdc214d7dd30d17a Mon Sep 17 00:00:00 2001 From: Brandon Duff Date: Tue, 18 Jul 2023 11:04:37 -0400 Subject: [PATCH] Handle impl function raising by raising in the client process - Raising in the server would give the wrong behavior Co-Authored-By: JB Steadman --- lib/protomock.ex | 7 ++++++- test/protomock_test.exs | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/protomock.ex b/lib/protomock.ex index 4786d50..e148834 100644 --- a/lib/protomock.ex +++ b/lib/protomock.ex @@ -385,6 +385,7 @@ defmodule ProtoMock do ref -> receive do + {^ref, {:protomock_error, e}} -> raise e {^ref, response} -> response end end @@ -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) diff --git a/test/protomock_test.exs b/test/protomock_test.exs index 88580e2..61a532c 100644 --- a/test/protomock_test.exs +++ b/test/protomock_test.exs @@ -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