[erlang-questions] testing side effects of asynchronous code

Motiejus Jakštys <>
Mon Jan 2 12:20:56 CET 2012


On Mon, Jan 02, 2012 at 11:02:47AM -0000, Gustav Simonsson wrote:
> 
> You could use meck and do a manual passthrough, i.e. something like:
> meck:expect(gen_gwserver, ack_sent, fun(Ref) -> some_async_test_signalling, gen_gwserver:ack_sent(Ref) end).
> 
> In the context of testing I don't see why one would rule this possibility out except for reasons of aesthetics.

Hi,

Minor fix: gen_gwserver:ack_sent(Ref) should be changed with
meck:passthrough([Ref]), like this:

meck:expect(gen_gwserver, ack_sent,
    fun(Ref) -> some_async_test_signalling, meck:passthrough([Ref]) end).

You are right, this would work. However, I would like an abstraction for
it, which would include both mecking and test signal catching, because
we are using it quite often.

Abstraction would need either using something other than meck, or
changing expect(Mod::atom(), Func::atom(), Expect::fun()) to accept
expect(Mod::atom(), Func::atom(), Arity::pos_int(), Expect::fun())

Expect :: function/1 which takes a list of arguments passed to mocked
function.

Without abstraction what we could get now:

yadda_test() ->
    MasterPid = self(), % unsure, this might be not necessary
    meck:expect(gen_gwserver, ack_sent,
        fun(Ref) -> MasterPid ! done, meck:passthrough([Ref]) end),
    gateway_api:send(Msg),
    receive done -> ok end
    ?assertSomething?

If we don't get anything better than that, it's fine. But it's much less
elegant than:

yadda_test() ->
    ?call_and_sleep(gateway_api:send(Message),
        gen_gwserver, ack_sent, 1),
    ?assertSomething?

Ideas?

Thanks
Motiejus



More information about the erlang-questions mailing list