Skip to content

Commit

Permalink
Adds a test case for whereis_pid
Browse files Browse the repository at this point in the history
  • Loading branch information
Qqwy committed May 26, 2022
1 parent 329c60e commit 91e12e3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions rustler_tests/lib/rustler_test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ defmodule RustlerTest do
def threaded_sleep(_), do: err()

def send_all(_, _), do: err()
def whereis_pid(_), do: err()
def sublists(_), do: err()

def tuple_echo(_), do: err()
Expand Down
1 change: 1 addition & 0 deletions rustler_tests/native/rustler_test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ rustler::init!(
test_thread::threaded_fac,
test_thread::threaded_sleep,
test_env::send_all,
test_env::whereis_pid,
test_env::sublists,
test_codegen::tuple_echo,
test_codegen::record_echo,
Expand Down
6 changes: 6 additions & 0 deletions rustler_tests/native/rustler_test/src/test_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ pub fn send_all<'a>(env: Env<'a>, pids: Vec<LocalPid>, msg: Term<'a>) -> Term<'a
msg
}

#[rustler::nif]
pub fn whereis_pid<'a>(env: Env<'a>, term: Term<'a>) -> Term<'a> {
let result = env.whereis_pid(term);
result.encode(env)
}

#[rustler::nif]
pub fn sublists<'a>(env: Env<'a>, list: Term<'a>) -> NifResult<Atom> {
// This is a "threaded NIF": it spawns a thread that sends a message back
Expand Down
10 changes: 10 additions & 0 deletions rustler_tests/test/env_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,14 @@ defmodule RustlerTest.EnvTest do
]
end
end

test "whereis_pid" do
assert self() == RustlerTest.whereis_pid(self())

{:ok, agent_pid} = Agent.start(fn -> 42 end, name: MyAgentName)
assert agent_pid == RustlerTest.whereis_pid(MyAgentName)

assert nil == RustlerTest.whereis_pid("not a PID")
assert nil == RustlerTest.whereis_pid(:not_a_registered_name)
end
end

0 comments on commit 91e12e3

Please sign in to comment.