[erlang-questions] testing asynchronous code

David Mercer <>
Mon Apr 19 16:51:08 CEST 2010

On Monday, April 19, 2010, Martin DeMello wrote:

> Anyone have general strategies or advice for testing code that makes
> heavy use of gen_server:cast? The best I could come up with was to
> write _sync versions of all my functions, with gen_server:cast
> replaced by gen_server:call, and then test those, but that seems both
> clunky and fragile.

I assume your issue is that you cannot test system state right after a cast
because its processing is asynchronous?  Now you could, say, wait a
"reasonable" amount of time to allow the cast to be processed and then
perform your assertions.

Alternatively, you might could[1] add a "sync" call into the server, which
does nothing, but you know when it returns that all prior casts have been
processed, and then you can make your asserts on the server or system state,
etc.  (If the casts are passing processing off to other processes, the sync
call would have to "sync" with them before returning.)



[1] I know that's not correct English, but it should be.

More information about the erlang-questions mailing list