Phillipp
Hey,
I have a group of GenServers which form a “unit” together with its parent Supervisor. One GenServer holds the state, another one sends data to external services and a third one is responsible for terminating the whole unit after a certain amount of inactivity.
Currently, if there is no input for 30 minutes, the “Timeout” GenServer terminates the parent Supervisor and therefore the whole unit. However, if the unit is in pause mode, the 30-minute timeout gets ignored and a 24-hour timeout gets started which is just a fallback to eventually clean up the system.
Right now, that whole thing is “untested”, which simply means it has no automated tests. It works fine but it’s also not that complicated at the moment. It might get a bit more features in the future tho.
I would like to write some simple integration tests but I am unsure how to deal with the timing aspect.
One thing to note is that I can manually override the timers in the Timeout GenServer. That was needed to reinitiate them after a reboot (e.g. deployment). I am sure that comes to my advantage in regards to testing but I still have no idea how to best structure the test.
The quick and dirty way would be this:
- Start “unit”
- Send input to “unit”
- Manually override timeouts
Process.sleep()in Test module- Assert expected state
But using Process.sleep() in the test Module (even if it’s for 1 second) feels quite dirty.
Any other suggestions?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #ai
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
Instead of sleep/assert in the test monitor the supervisor of the unit and be notified when it stops. When the supervisor stoped everything should also be gone if properly linked, but you could also check that afterwards.
Phillipp
That is a good idea. In general, in can just check the root DynamicSupervisor for all its children to check if the Supervisor specific to the unit is gone. That would be step 5 of my provided example.
So, I should Monitor the unit Supervisor and use assert_receive instead of
Process.sleep(), right? Isassert_receiveblocking the process as well? If so, there is not a huge difference to sleep/assert and it exposes a bit more of the underlaying mechanics (since I assert for the Supervisor to shutdown instead of asking the system for the list of running units).LostKobrakai
From the implementation I’d say it’s way different from the fact that you need to wait it’s not. But given that you want to test a timeout I’m not sure how you want to prevent waiting for things to happen. Also the monitor will send the test a message as soon as the supervisor exists, so no need to guess a
sleep()duration and no need for sleeping longer than needed.Phillipp
Ok, that’s a good point. I will go that route then.
Qqwy
I have been in this situation as well, and what I did was to: