I wrote some eunit tests on my gen_server:
-module(st_db_tests). -include_lib("eunit/include/eunit.hrl"). main_test_() -> {foreach, fun setup/0, fun cleanup/1, [ fun db_server_up/1 ]}. setup() -> {ok,Pid} = st_db:start_link(), Pid. cleanup(Pid) -> gen_server:call(Pid, stop). db_server_up(Pid) -> ?_assertMatch({[{<<"couchdb">>,<<"Welcome">>},{<<"version">>, _}]}, gen_server:call(Pid, test)).
When I do the test, I have this:
./rebar eunit suite=st_db_tests skip_deps=true ==> site_stater (eunit) Compiled test/st_db_tests.erl ... loading stuff ... =PROGRESS REPORT==== 27-Jun-2011::12:33:21 === supervisor: {local,kernel_safe_sup} started: [{pid,<0.127.0>}, {name,inet_gethost_native_sup}, {mfargs,{inet_gethost_native,start_link,[]}}, {restart_type,temporary}, {shutdown,1000}, {child_type,worker}] module 'st_db_tests' *** context cleanup failed *** ::exit:{normal,{gen_server,call,[<0.99.0>,stop]}} in function gen_server:call/2 ======================================================= Failed: 0. Skipped: 0. Passed: 1.
It looks like the test passed, but there is an error in clearing the context, what's wrong, right?)
How can i fix this?
PS: my gen_server
-module(st_db). -behaviour(gen_server). %% -------------------------------------------------------------------- %% Include files %% -------------------------------------------------------------------- %% -------------------------------------------------------------------- %% External exports -export([start_link/0]). %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). -record(state, {db_pid, couch_server_pid}). %% ==================================================================== %% External functions %% ==================================================================== %%-------------------------------------------------------------------- %% @doc Starts the server. %% %% @spec start_link() -> {ok, Pid} %% where %% Pid = pid() %% @end %%-------------------------------------------------------------------- start_link() -> gen_server:start_link({global, st_db}, ?MODULE, ["localhost", 5984, "site_stater"], []). %% ==================================================================== %% Server internal functions %% ==================================================================== %% -------------------------------------------------------------------- %% Function: init/1 %% Description: Initiates the server %% Returns: {ok, State} | %% {ok, State, Timeout} | %% ignore | %% {stop, Reason} %% -------------------------------------------------------------------- init([Server, Port, DB]) -> couchbeam:start(), CouchServer = couchbeam:server_connection(Server, Port, "", []), {ok, CouchDB} = couchbeam:open_or_create_db(CouchServer, DB, []), {ok,