diff --git a/grpc_client/lib/grpc/client/connection.ex b/grpc_client/lib/grpc/client/connection.ex index 98af91d9..a80a7dba 100644 --- a/grpc_client/lib/grpc/client/connection.ex +++ b/grpc_client/lib/grpc/client/connection.ex @@ -153,9 +153,27 @@ defmodule GRPC.Client.Connection do """ @spec connect(String.t(), keyword()) :: {:ok, Channel.t()} | {:error, any()} def connect(target, opts \\ []) do - ref = make_ref() + supervisor_pid = Process.whereis(GRPC.Client.Supervisor) - case build_initial_state(target, Keyword.merge(opts, ref: ref)) do + if is_nil(supervisor_pid) or !Process.alive?(supervisor_pid) do + raise """ + GRPC.Client.Supervisor is not running. Please ensure it is started as part of your + application's supervision tree: + + children = [ + {GRPC.Client.Supervisor, []} + ] + + opts = [strategy: :one_for_one, name: MyApp.Supervisor] + Supervisor.start_link(children, opts) + + You can also start it manually in scripts or test environments: + + {:ok, _pid} = DynamicSupervisor.start_link(strategy: :one_for_one, name: GRPC.Client.Supervisor) + """ + end + + case build_initial_state(target, opts) do {:ok, initial_state} -> ch = initial_state.virtual_channel @@ -307,7 +325,7 @@ defmodule GRPC.Client.Connection do opts = Keyword.validate!(opts, cred: nil, - ref: nil, + name: make_ref(), adapter: GRPC.Client.Adapters.Gun, adapter_opts: [], interceptors: [], @@ -333,7 +351,7 @@ defmodule GRPC.Client.Connection do virtual_channel = %Channel{ scheme: scheme, cred: cred, - ref: opts[:ref], + ref: opts[:name], adapter: adapter, interceptors: interceptors, codec: norm_opts[:codec], diff --git a/grpc_client/test/grpc/integration/stub_test.exs b/grpc_client/test/grpc/integration/stub_test.exs index bea07283..255a74ba 100644 --- a/grpc_client/test/grpc/integration/stub_test.exs +++ b/grpc_client/test/grpc/integration/stub_test.exs @@ -68,6 +68,21 @@ defmodule GRPC.Integration.StubTest do end) end + test "use a channel name to send a message" do + run_server(HelloServer, fn port -> + {:ok, _channel} = + GRPC.Client.Connection.connect("localhost:#{port}", + interceptors: [GRPC.Client.Interceptors.Logger], + name: :my_channel + ) + + name = "GRPC user!" + req = %Helloworld.HelloRequest{name: name} + {:ok, reply} = %GRPC.Channel{ref: :my_channel} |> Helloworld.Greeter.Stub.say_hello(req) + assert reply.message == "Hello, #{name}" + end) + end + test "invalid channel function clause error" do req = %Helloworld.HelloRequest{name: "GRPC"}