[erlang-questions] Testing gen_statem timers

Fred Hebert mononcqc@REDACTED
Fri Aug 16 15:40:08 CEST 2019


On Fri, Aug 16, 2019 at 7:07 AM Jesper Louis Andersen <
jesper.louis.andersen@REDACTED> wrote:

>
> Second, timers are the scourge of testing. They are side-effects which
> means they are usually handled by mocking. And mocking is never the answer
> if there is a decent way around it. However, if a timer turns into a
> message, then we can choose to have timers handled outside the component
> and then send messages into the component. This helps testing, as we can
> run with a time-compression component, removing typical long waits in our
> test cases. In fact, separation of effects from computation is almost
> always a win for code in general.
>
> This has been my general go-to strategy, but note that this isn't really
doable with a gen_statem since the timer events have their own type of
message, which you cannot fake in a generic manner (the same way you would
just do it with a timeout message in other behaviours). You at the very
least need to add a special injection-creating callback that uses the
next_event action to inject a timeout of the type you want, and the way
-ifdefs work in Erlang, that clause can't optionally be compiled out for
production.

Other types of mocking or time handling otherwise need to be aware of
implementation details in gen_statem to know which clocks to mock and
advance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190816/781c7c2a/attachment.htm>


More information about the erlang-questions mailing list