When testing some asynchronous behavior, you should never rely on letting your test sleep for a long enough time. You should use some polling mechanism, instead.
%% bad
bad(Config) ->
something:that(kicks, off, a, {background, task}),
timer:sleep(1000),
done = that_task:status().
%% good
good(Config) ->
something:that(kicks, off, a, {background, task}),
ktn_task:wait_for(fun() -> that_task:status() end, done).
Reasoning: If you use timer:sleep/1 with a number that's too small, scheduler load or other factors may randomly make your test fail. On the other hand, using a number that's big enough to ensure test success may waste lots of time in each run thus slowing down your test suite and your development process as a whole. Using ktn_task:wait_for/2 or similar alternatives (i.e. periodically polling for results until the right one is found), you get the best of both worlds: you don't fail too early but you also don't wait too much.
Do not use
timer:sleep/1
in testsReasoning: If you use
timer:sleep/1
with a number that's too small, scheduler load or other factors may randomly make your test fail. On the other hand, using a number that's big enough to ensure test success may waste lots of time in each run thus slowing down your test suite and your development process as a whole. Usingktn_task:wait_for/2
or similar alternatives (i.e. periodically polling for results until the right one is found), you get the best of both worlds: you don't fail too early but you also don't wait too much.