Nema Ga
Nema Ga

Reputation: 2600

Elixir testing genserver - Passing parent PID

Is it considered bad practice to have parent_pid argument on most handlecast and handleinfo functions in production ?

I am testing like this:

parent = self()
GenServer.cast(UserServer.via_tuple(user.id), {:update_direct, parent})
assert_receive :updating_failed, 2000

And then genserver(with simple retry mock):

def handle_cast(..... parent) do
   case updated do
     false -> Process.send_after(self(), {:update_retry, ... parent, retries + 1}, 500)
     true -> ...
    state
   end
end

And finally in handle_info :update_retry I send message back to awaiting test(parent):

send parent, :updating_failed

Upvotes: 0

Views: 361

Answers (1)

Ryan Bigg
Ryan Bigg

Reputation: 107728

If you want to reply back to a parent process, then you should be using handle_call:

def handle_call(data, from, state) do
  case updated do
       false -> Process.send_after(from, {:update_retry, ... parent, retries + 1}, 500)
       true -> ...
      state
  end
  {:reply, :ok, state}
end

Upvotes: 1

Related Questions