NFIBrokerage / slipstream

A slick WebSocket client for Phoenix Channels
https://hex.pm/packages/slipstream
Apache License 2.0
160 stars 18 forks source link

How to test synchronous api #59

Open namxam opened 1 year ago

namxam commented 1 year ago

I know it is not really an issue, but right now I am a bit lost and could need some help :)

I have a synchronous handle_call/3 callback like this:

  def handle_call({:track_event, name, payload}, _from, %{assigns: %{channel: channel}} = socket) do
    {:ok, ref} = push(socket, channel, name, payload)
    result = case await_reply(ref) do
      {:error, :timeout} -> :error
      reply -> reply
    end
    {:reply, result, socket}
  end

How would I test such a function? connect_and_assert_join/4 works as expected. But I do not know how to implement assert_push/4 and reply/3. They work, but I always trigger the callbacks timeout. I could test the handle_call/3 function directly, but that triggers a process attempted to call itself error.

the-mikedavis commented 1 year ago

Could you share a snippet of how you are testing this? I think the problem might be that the GenServer call that hits this handle_call branch will block the caller until the reply is sent. So you may need the call to reply/3 to happen in a separate process.