Reputation: 990
I am trying to create a DynamicSupervisor
to supervise another GenServer
on request to it and then trying to test it.
Here is my code for NodeDynamicSupervisor
:
defmodule NodeDynamicSupervisor do
use DynamicSupervisor
def start_link() do
DynamicSupervisor.start_link(__MODULE__, :ok, name: __MODULE__)
end
def init(:ok) do
DynamicSupervisor.init(strategy: :one_for_one)
end
def add_node(private_key, public_key, address, num_of_coins) do
child_spec = {Node, {private_key, public_key, address, num_of_coins}}
DynamicSupervisor.start_child(__MODULE__, child_spec)
end
end
Here's how I'm testing it:
defmodule NodeCreationTest do
use ExUnit.Case
import ExUnit.CaptureIO
test "should create node" do
{:ok, node_pid} = NodeDynamicSupervisor.start_link()
capture_io(node_pid.add_node(private_key, public_key, address, 0))
end
end
And this is the error I get:
code: capture_io(node_pid.add_node(private_key, public_key, address, 0))
stacktrace:
:erlang.apply(#PID<0.163.0>, :add_node, [])
test/create_nodes_test.exs:12: (test)
Why am I not able to add a node and getting this error instead?
Upvotes: 1
Views: 738
Reputation: 75750
There are two issues with your code:
1. You can't call methods on a pid
:
You're calling node_pid.add_node(...)
in your test which is incorrect, when you should be calling the function from the module. Since your DynamicSupervisor
process is named (name: __MODULE__
) and your add_node/4
is already passing the process pid/name in the implementation, you can just call it directly:
NodeDynamicSupervisor.add_node(private_key, public_key, address, 0)
2. capture_io
takes a function as the argument:
In your case you're first calling the function and passing its result to the method. You need to call the method inside an anonymous function and pass that to capture_io/1
instead:
capture_io(fn ->
NodeDynamicSupervisor.add_node(private_key, public_key, address, 0)
end)
Upvotes: 3