Handling Phoenix Presence during testing

Reviving the thread because a) there’s been more recent advancements that can help anyone landing here and b) I still have trouble making tests involving Phoenix.Presence deterministic.

In my particular case, my fetcher doesn’t use the DB but calls an external API, which I’m trying to mock with TestServer (context TestServer - No fuzz mocking of third-party services - #6 by rhcarvalho), but I believe the underlying trouble is the same.


First, summarizing the knowledge from the thread, in 2017 @luizpvasc and @sorentwo suggested something like:

ref = leave(socket)
assert_reply ref, :ok
:timer.sleep(10)

In late 2019, 2020, this GitHub issue brings more light into the problem:

Quoting José Valim:

The issue is that once the test terminates, the channel process will terminate, the presence process will notice the channel termination, and then invoke the callbacks without the database.

All of this happens async, so it is hard to make it sync. I am not sure at the moment how to fix those.

The conversation goes on and a new API has been added along with some docs:

In 2021, @ruslandoga notices something I’ve experienced as well in practice, that the new Presence.fetchers_pids() might pick up an empty list and so adds some sleep time before calling it:

on_exit(fn ->
    :timer.sleep(10)  #### WAIT FOR FETCHER PROCESSES TO BE STARTED ####
    for pid <- RumblWeb.Presence.fetchers_pids()  do
      ref = Process.monitor(pid)
      assert_receive {:DOWN, ^ref, _, _, _}, 1000
    end
  end)

I used GitHub Search to see what people are doing in the open: Code search results · GitHub

The first hit for me is LiveBeat which does use a 100ms sleep:

Second hit is NervesHub that just follows the documentation and has no sleep:

Going down the list I find both cases of with and without sleep, with different amounts of sleep. And found this commit message from @gpreston which reinforces people don’t know what to do :slight_smile: I don’t know what to do… the sleep time still feels non-deterministic, racy.


TODO:

  • I’m tempted to suggest updating the docs with the sleep before calling fetchers_pid
  • Discuss what else can we do. Could tests reasonable synchronize with Presence fetchers? Can we write tests knowing that a certain fetcher will be called exactly N number of times? Is that a bad idea to begin with?

Would love to learn more about this corner of Elixir. There are so many pieces involved in making Presence work that understanding how everything fits together (and points of synchronization) is no easy feat :slight_smile:

1 Like