Testing a LiveView with short-lived GenServer that accesses DB

Description

I am testing a Live View that requires a log in. It relies on Webhooks + PubSub, Oban jobs, and short-lived GenServers.

I’ve got it all working except the part with the GenServer. An Oban workflow uses a temporary GenServer that tracks the status of something based on a bunch of webhooks and then sends a message and shuts down, before the workflow continues.

The problem

I’m having an ownership issue with the DB connections.

  • When it’s :manual I get an error because the GenServer does DB lookups.
  • When I set it to :shared, I am always redirected to the sign in page. My user isn’t logged in (no idea why this is happening, but it does).

It’s very odd and I have no idea why this is happening.

Things I’ve tried:

  • :manual and {:shared, self()} as explained above
  • Adding a registry and sharing with :via

Help me

I haven’t figured out a good way to share with just the GenServer process. I tried with setting up a registry and using :via with an identifier I could know at the time, but it wouldn’t resolve because

a) sharing with :via doesn’t work, or
b) the timing doesn’t work because the genserver isn’t alive yet and there is no way for me to synchronize the genserver starting and ecto sandbox sharing.

Any ideas?

(And why does shared mode mess with my logged in session?)