Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to use different sessions in Examples.SessionTest #14

Merged
merged 14 commits into from
Feb 16, 2024
Merged
4 changes: 2 additions & 2 deletions lib/zenohex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ defmodule Zenohex do
"""
@spec open(Config.t()) :: {:ok, Session.t()} | {:error, reason :: any()}
def open(config \\ %Config{}) do
if System.get_env("SCOUTING_DELAY") == "0" do
Nif.zenoh_open(%Config{scouting: %Scouting{delay: 0}})
if delay = System.get_env("SCOUTING_DELAY") do
Nif.zenoh_open(%Config{scouting: %Scouting{delay: String.to_integer(delay)}})
else
Nif.zenoh_open(config)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/zenohex/examples/publisher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ defmodule Zenohex.Examples.Publisher do
Start Publisher.
"""
def start_link(args \\ %{}) when is_map(args) do
session = Map.get(args, :session, Zenohex.open!())
session = Map.get(args, :session) || Zenohex.open!()
key_expr = Map.get(args, :key_expr, "zenohex/examples/pub")
Supervisor.start_link(__MODULE__, %{session: session, key_expr: key_expr}, name: __MODULE__)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/zenohex/examples/pull_subscriber.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ defmodule Zenohex.Examples.PullSubscriber do
Start PullSubscriber.
"""
def start_link(args \\ %{}) when is_map(args) do
session = Map.get(args, :session, Zenohex.open!())
session = Map.get(args, :session) || Zenohex.open!()
key_expr = Map.get(args, :key_expr, "zenohex/examples/**")
callback = Map.get(args, :callback, &Logger.debug(inspect(&1)))

Expand Down
2 changes: 1 addition & 1 deletion lib/zenohex/examples/queryable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule Zenohex.Examples.Queryable do
Start Queryable.
"""
def start_link(args \\ %{}) when is_map(args) do
session = Map.get(args, :session, Zenohex.open!())
session = Map.get(args, :session) || Zenohex.open!()
key_expr = Map.get(args, :key_expr, "zenohex/examples/**")
callback = Map.get(args, :callback, &Logger.debug(inspect(&1)))

Expand Down
10 changes: 7 additions & 3 deletions lib/zenohex/examples/queryable/impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ defmodule Zenohex.Examples.Queryable.Impl do
{:ok, queryable} = Session.declare_queryable(session, key_expr)
state = %{queryable: queryable, callback: callback}

send(self(), :loop)
recv_timeout(state)

{:ok, state}
end

def handle_info(:loop, state) do
recv_timeout(state)

{:noreply, state}
end

def recv_timeout(state) do
case Queryable.recv_timeout(state.queryable) do
{:ok, query} ->
state.callback.(query)
Expand All @@ -37,7 +43,5 @@ defmodule Zenohex.Examples.Queryable.Impl do
{:error, error} ->
Logger.error(inspect(error))
end

{:noreply, state}
end
end
6 changes: 1 addition & 5 deletions lib/zenohex/examples/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,10 @@ defmodule Zenohex.Examples.Session do
Start Session.
"""
def start_link(args \\ %{}) when is_map(args) do
session = Map.get(args, :session, Zenohex.open!())
session = Map.get(args, :session) || Zenohex.open!()
Supervisor.start_link(__MODULE__, %{session: session}, name: __MODULE__)
end

@doc "Get session."
@spec session() :: Zenohex.Session.t()
defdelegate session(), to: Session.Impl

@doc "Put data."
@spec put(String.t(), integer() | float() | binary()) :: :ok
defdelegate put(key_expr, value), to: Session.Impl
Expand Down
8 changes: 0 additions & 8 deletions lib/zenohex/examples/session/impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ defmodule Zenohex.Examples.Session.Impl do
GenServer.start_link(__MODULE__, args, name: __MODULE__)
end

def session() do
GenServer.call(__MODULE__, :session)
end

def put(key_expr, value) do
GenServer.call(__MODULE__, {:put, key_expr, value})
end
Expand Down Expand Up @@ -40,10 +36,6 @@ defmodule Zenohex.Examples.Session.Impl do
}}
end

def handle_call(:session, _from, state) do
{:reply, state.session, state}
end

def handle_call({:put, key_expr, value}, _from, state) do
ret = Zenohex.Session.put(state.session, key_expr, value)
{:reply, ret, state}
Expand Down
2 changes: 1 addition & 1 deletion lib/zenohex/examples/storage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule Zenohex.Examples.Storage do
Start storage.
"""
def start_link(args \\ %{}) when is_map(args) do
session = Map.get(args, :session, Zenohex.open!())
session = Map.get(args, :session) || Zenohex.open!()
key_expr = Map.get(args, :key_expr, "zenohex/examples/**")
Supervisor.start_link(__MODULE__, %{session: session, key_expr: key_expr}, name: __MODULE__)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/zenohex/examples/subscriber.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule Zenohex.Examples.Subscriber do
Start Subscriber.
"""
def start_link(args \\ %{}) when is_map(args) do
session = Map.get(args, :session, Zenohex.open!())
session = Map.get(args, :session) || Zenohex.open!()
key_expr = Map.get(args, :key_expr, "zenohex/examples/**")
callback = Map.get(args, :callback, &Logger.debug(inspect(&1)))

Expand Down
9 changes: 6 additions & 3 deletions lib/zenohex/examples/subscriber/impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ defmodule Zenohex.Examples.Subscriber.Impl do
{:ok, subscriber} = Session.declare_subscriber(session, key_expr)
state = %{subscriber: subscriber, callback: callback}

send(self(), :loop)
recv_timeout(state)

{:ok, state}
end

def handle_info(:loop, state) do
recv_timeout(state)
{:noreply, state}
end

defp recv_timeout(state) do
case Subscriber.recv_timeout(state.subscriber) do
{:ok, sample} ->
state.callback.(sample)
Expand All @@ -37,7 +42,5 @@ defmodule Zenohex.Examples.Subscriber.Impl do
{:error, error} ->
Logger.error(inspect(error))
end

{:noreply, state}
end
end
3 changes: 1 addition & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,13 @@ defmodule Zenohex.MixProject do
"""
=================================================================
HEY, ZENOHEX DEVELOPER. TO REDUCE TESTING TIME,
WE COMPILE WITH API_OPEN_SESSION_DELAY=0 AND SET SCOUTING DELAY 0
WE COMPILE WITH API_OPEN_SESSION_DELAY=0
=================================================================
"""
|> String.trim_trailing()
|> Mix.shell().info()

:ok = System.put_env("API_OPEN_SESSION_DELAY", "0")
:ok = System.put_env("SCOUTING_DELAY", "0")

Mix.Task.run("compile", ["--force"])
end
Expand Down
15 changes: 6 additions & 9 deletions test/zenohex/examples/session_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@ defmodule Zenohex.Examples.SessionTest do
alias Zenohex.Examples.Queryable
alias Zenohex.Query
alias Zenohex.Sample
alias Zenohex.Config
alias Zenohex.Config.Scouting

setup do
start_supervised!({Session, %{}})
start_supervised!(
{Session, %{session: Zenohex.open!(%Config{scouting: %Scouting{delay: 0}})}}
)

# NOTE: Use the same session for Subscriber or Queryable, to run unit tests even if you only have a loopback interface.
#
# - If you only have a loopback interface(lo), the test will always fail if you use different sessions.
# Because the peer cannot scout the another peer on lo.
# - Even if you have a network interface other than loopback,
# using different sessions may cause the test to fail depending on the scouting.

%{session: Session.session()}
%{session: Zenohex.open!(%Config{scouting: %Scouting{delay: 30}})}
end

describe "put/2" do
Expand Down