Phillipp

Phillipp

Best strategy to test GenServers which use timers (sends_after)

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:

  1. Start “unit”
  2. Send input to “unit”
  3. Manually override timeouts
  4. Process.sleep() in Test module
  5. Assert expected state

But using Process.sleep() in the test Module (even if it’s for 1 second) feels quite dirty.

Any other suggestions?

Marked As Solved

LostKobrakai

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.

Also Liked

Qqwy

Qqwy

TypeCheck Core Team

I have been in this situation as well, and what I did was to:

  • Allow timers to be configured in some way (like e.g. using Specify. This was actually one of the reasons why I created that library).
  • In my tests, reduce the timeouts from the hours, minutes or seconds range to a couple of milliseconds
    • Keep in mind that the timeout should be long enough such that race conditions should not occur. In essence, it should be an order of magnitude larger than the time it takes to do the actual non-timeout work. But making it more than this one order of magnitude means that your tests will be slower than necessary.
  • Now in your tests, let your test listener sleep for a duration slightly longer than the configured timeout (or e.g. use assert_receive), and it will be able to properly test that the GenServer(s) time out as expected.

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement