Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions grpc_client/lib/grpc/client/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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: [],
Expand All @@ -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],
Expand Down
15 changes: 15 additions & 0 deletions grpc_client/test/grpc/integration/stub_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand Down
Loading