6

我在我的 gen_server 上写了一些 eunit 测试:

-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)).

当我进行测试时,我有这个:

./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.

好像测试通过了,但是上下文清理有错误,这是不对的吧?)

我怎样才能解决这个问题?

PS:我的 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, #state{db_pid=CouchDB, couch_server_pid=CouchServer}}.


%% ====================================================================
%% DB manipulation functions
%% ====================================================================


%% --------------------------------------------------------------------
%% Function: handle_call/3
%% Description: Handling call messages
%% Returns: {reply, Reply, State}          |
%%          {reply, Reply, State, Timeout} |
%%          {noreply, State}               |
%%          {noreply, State, Timeout}      |
%%          {stop, Reason, Reply, State}   | (terminate/2 is called)
%%          {stop, Reason, State}            (terminate/2 is called)
%% --------------------------------------------------------------------
handle_call (test, _From, #state{couch_server_pid = Couch_server_pid} = State) ->
    {ok, Version} = couchbeam:server_info(Couch_server_pid),
    {reply, Version, State};


handle_call(stop, _From, State) -> 
    {stop, normal, State}.

%% --------------------------------------------------------------------
%% Function: handle_cast/2
%% Description: Handling cast messages
%% Returns: {noreply, State}          |
%%          {noreply, State, Timeout} |
%%          {stop, Reason, State}            (terminate/2 is called)
%% --------------------------------------------------------------------

handle_cast(_Msg, State) ->
    {noreply, State}.

%% --------------------------------------------------------------------
%% Function: handle_info/2
%% Description: Handling all non call/cast messages
%% Returns: {noreply, State}          |
%%          {noreply, State, Timeout} |
%%          {stop, Reason, State}            (terminate/2 is called)
%% --------------------------------------------------------------------
handle_info(_Info, State) ->
    {noreply, State}.

%% --------------------------------------------------------------------
%% Function: terminate/2
%% Description: Shutdown the server
%% Returns: any (ignored by gen_server)
%% --------------------------------------------------------------------
terminate(_Reason, _State) ->
    ok.

%% --------------------------------------------------------------------
%% Func: code_change/3
%% Purpose: Convert process state when code is changed
%% Returns: {ok, NewState}
%% --------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
    {ok, State}.
4

2 回答 2

7

handle_call/2函数中,您将返回:

{stop, normal, State}

而不是类似的东西:

{stop, normal, ok, State}

换句话说,你没有回复电话。客户端然后看到服务器终止(正常原因)并且它哭了。

没试过,但这是我的第一个猜测。

于 2011-06-27T09:50:43.543 回答
4

可能是测试在gen_server进程有时间关闭之前完成。gen_server链接到测试进程(因为它以 开头),当测试完成时,该gen_server:start_link/4进程仍在运行并被 EUnit 杀死。

即使您按照 Roberto 的建议返回okusing ,执行速度也可能比测试清理的速度慢。{stop, normal, ok, State}gen_serverterminate/2

我通常在我的测试或拆解中使用这样的函数来等待进程:

wait_for_exit(Pid) ->
    MRef = erlang:monitor(process, Pid),
    receive {'DOWN', MRef, _, _, _} -> ok end.

EUnit 的默认超时值为 5000 毫秒,如果此功能阻塞时间过长,则会触发该超时。

于 2011-06-27T10:00:44.190 回答