Skip to content

Commit

Permalink
Pattern match on channel on stub functions (#354)
Browse files Browse the repository at this point in the history
Provides a better error for invalid argument

Co-authored-by: Paulo Valente <[email protected]>
  • Loading branch information
v0idpwn and polvalente authored Mar 25, 2024
1 parent 8aaf3d3 commit c677e75
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/grpc/stub.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ defmodule GRPC.Stub do
}

if req_stream do
def unquote(String.to_atom(func_name))(channel, opts \\ []) do
def unquote(String.to_atom(func_name))(%GRPC.Channel{} = channel, opts \\ []) do
GRPC.Stub.call(
unquote(service_mod),
unquote(Macro.escape(rpc)),
Expand All @@ -88,7 +88,7 @@ defmodule GRPC.Stub do
)
end
else
def unquote(String.to_atom(func_name))(channel, request, opts \\ []) do
def unquote(String.to_atom(func_name))(%GRPC.Channel{} = channel, request, opts \\ []) do
GRPC.Stub.call(
unquote(service_mod),
unquote(Macro.escape(rpc)),
Expand Down
8 changes: 8 additions & 0 deletions test/grpc/integration/stub_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ defmodule GRPC.Integration.StubTest do
end)
end

test "invalid channel function clause error" do
req = Helloworld.HelloRequest.new(name: "GRPC")

Check warning on line 72 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 23.x / Elixir 1.14.x

Helloworld.HelloRequest.new/1 is deprecated. Build the struct by hand with %MyMessage{...} or use struct/2

Check warning on line 72 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.14.x

Helloworld.HelloRequest.new/1 is deprecated. Build the struct by hand with %MyMessage{...} or use struct/2

Check warning on line 72 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.12.x

Helloworld.HelloRequest.new/1 is deprecated. Build the struct by hand with %MyMessage{...} or use struct/2

Check warning on line 72 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 23.x / Elixir 1.12.x

Helloworld.HelloRequest.new/1 is deprecated. Build the struct by hand with %MyMessage{...} or use struct/2

Check warning on line 72 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 23.x / Elixir 1.13.x

Helloworld.HelloRequest.new/1 is deprecated. Build the struct by hand with %MyMessage{...} or use struct/2

Check warning on line 72 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.13.x

Helloworld.HelloRequest.new/1 is deprecated. Build the struct by hand with %MyMessage{...} or use struct/2

Check warning on line 72 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.x / Elixir 1.15.x

Helloworld.HelloRequest.new/1 is deprecated. Build the struct by hand with %MyMessage{...} or use struct/2

Check warning on line 72 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.1.x / Elixir 1.14.x

Helloworld.HelloRequest.new/1 is deprecated. Build the struct by hand with %MyMessage{...} or use struct/2

Check warning on line 72 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.1.x / Elixir 1.15.x

Helloworld.HelloRequest.new/1 is deprecated. Build the struct by hand with %MyMessage{...} or use struct/2

Check warning on line 72 in test/grpc/integration/stub_test.exs

View workflow job for this annotation

GitHub Actions / OTP 26.1.x / Elixir 1.15.x

Helloworld.HelloRequest.new/1 is deprecated. Build the struct by hand with %MyMessage{...} or use struct/2

assert_raise FunctionClauseError, ~r/Helloworld.Greeter.Stub.say_hello/, fn ->
Helloworld.Greeter.Stub.say_hello(nil, req)
end
end

test "returns error when timeout" do
run_server(SlowServer, fn port ->
{:ok, channel} = GRPC.Stub.connect("localhost:#{port}")
Expand Down

0 comments on commit c677e75

Please sign in to comment.