[erlang-questions] testing asynchronous code

Martin DeMello martindemello@REDACTED
Wed Apr 21 15:59:52 CEST 2010


On Wed, Apr 21, 2010 at 2:07 AM, Scott Lystig Fritchie
<fritchie@REDACTED> wrote:
>
> It's quite unusual to have a gen_server send itself a message using
> gen_server:cast().  I don't know why your code would do it...

It's not, it's sending a message to another gen_server. It does it
that way simply because anyone calling the other gen_server directly
wants to do it asynchronously, and so I had implemented handle_cast
for the cascaded operations, and not handle_call. But now that you
mention it, it does seem like a code smell anyway, since only the top
level calling code should really need a cast, and can do everything
synchronously "behind the scenes". I'm new to the language, and still
in the process of getting a feel for what good and bad erlang code
looks like, so I appreciate the tips.

> ... but I confess that it is very occasionally useful to use
> gen_server:cast() or "self() ! {some_message_tag, ReminderData}" to
> remind yourself to do something that is very inconvenient to deal with
> at this instant in time.  When your call stack pops the normal way and
> returns control to gen_server's message handling loop, then you can
> conveniently handle that reminder data via YourModule:handle_cast() or
> YourModule:handle_info().

That's a useful trick :) Bookmarked in case I ever need it.

martin


More information about the erlang-questions mailing list