I’m new to Elixir and have been working through the getting started guide. In the Mix and OTP, DocTests, Patterns and with section, it talks about rewriting the KVServer.Command.run/1
function to be run(command, pid)
to facilitate unit testing.
To achieve this, the KVServer.serve/1
function would need to be re-written to pass this argument to the newly implemented run/2
function:
defp serve(socket, pid) do
msg =
with {:ok, data} <- read_line(socket),
{:ok, command} <- KVServer.Command.parse(data),
do: KVServer.Command.run(command, pid)
write_line(socket, msg)
serve(socket)
end
However, in the loop_acceptor/1
method, where we call the serve
function, we’d need to obtain the PID from the call to Task.Supervisor.start_child
and pass it as an argument to the function given as an argument to the Task.Supervisor.start_child
function call:
defp loop_acceptor(socket) do
{:ok, client} = :gen_tcp.accept(socket)
{:ok, pid} = Task.Supervisor.start_child(KVServer.TaskSupervisor, fn -> serve(client, pid) end)
:ok = :gen_tcp.controlling_process(client, pid)
loop_acceptor(socket)
end
I’m confused as to how to get this PID and pass it as an argument so that it can be passed to run/2
. Could anyone help shed some light on this?