I've started trying to test ETSDB with Common Test and found that it wasn't terribly straightforward to test the Riak Core vnode. The vnode is managed by a Riak Core gen_fsm and isn't a built-in OTP behavior.
I wanted to include the Riak Core gen_fsm to make sure that I integrated it
properly. First you want to spin up the riak_core_vnode
with your vnode
implementation and save the config in the Pid.
init_per_testcase(_, Config) -> | |
{ok, Pid} = riak_core_vnode:start_link(etsdb_vnode, 0, []), | |
[{vnode, Pid}| Config]. |
Similarly to tear it down you should send a message to stop the FSM. This requires a
tear down call and adding a handler in your vnode to return a stop
.
end_per_testcase(_, Config) -> | |
Pid = ?config(vnode, Config), | |
send_command(Pid, stop), | |
ok. |
handle_command(stop, _Sender, _State) -> | |
{stop, normal, {}}; |
That includes the send_command
which is a variation from the Riak Core
source. It will handle sending the message in a way that can get the response
sent back to the sending process. Riak Core does some mucking around to deal
with running with the full application.
-include_lib("riak_core/include/riak_core_vnode.hrl"). | |
send_command(Pid, Request) -> | |
Ref = make_ref(), | |
gen_fsm:send_event(Pid, ?VNODE_REQ{request=Request, | |
sender={raw, Ref, self()}}), | |
{ok, Ref}. | |
get_response(Ref) -> | |
receive {Ref, M} -> {ok, M} | |
after 1000 -> error | |
end. |
Now you can call send_command
with the Pid of the FSM and with the ref
returned can pull that messages out of the mailbox!