1

我有一个测试模块来测试我的Table模块。我的Table模块在终止时调用:

terminate(_, State = {Board, Status, Players}) ->
  gen_server:stop(Board),
  ...stopping other processes,
  io:format("Table Terminating.~p~n", [State]),
  ok.

这是我的代码中唯一会停止Board进程的部分。

运行测试后,大约一分钟后我会得到这个:

=ERROR REPORT==== 21-Jul-2017::22:28:40 ===
** Generic server <0.92.0> terminating
** Last message in was []
** When Server state == [[{spawn,x,none},
                          {recent,x,none},
                          {empty,null,none},
                          {empty,null,none},
                          {empty,null,none}],
                         [{empty,null,none},
                          {empty,null,none},
                          {empty,null,none},
                          {ridge,null,none},
                          {empty,null,none}],
                         [{empty,null,none},
                          {empty,null,none},
                          {ridge,null,none},
                          {empty,null,none},
                          {empty,null,none}],
                         [{empty,null,none},
                          {ridge,null,none},
                          {empty,null,none},
                          {empty,null,none},
                          {empty,null,none}],
                         [{empty,null,none},
                          {empty,null,none},
                          {empty,null,none},
                          {empty,null,none},
                          {spawn,o,none}]]
** Reason for termination ==
** {terminated,[{io,format,
                    [<0.90.0>,"Board.~p~n",
                     [[[{spawn,x,none},
                        {recent,x,none},
                        {empty,null,none},
                        {empty,null,none},
                        {empty,null,none}],
                       [{empty,null,none},
                        {empty,null,none},
                        {empty,null,none},
                        {ridge,null,none},
                        {empty,null,none}],
                       [{empty,null,none},
                        {empty,null,none},
                        {ridge,null,none},
                        {empty,null,none},
                        {empty,null,none}],
                       [{empty,null,none},
                        {ridge,null,none},
                        {empty,null,none},
                        {empty,null,none},
                        {empty,null,none}],
                       [{empty,null,none},
                        {empty,null,none},
                        {empty,null,none},
                        {empty,null,none},
                        {spawn,o,none}]]]],
                    []},
                {board,terminate,2,[{file,"board.erl"},{line,319}]},
                {gen_server,try_terminate,3,
                            [{file,"gen_server.erl"},{line,629}]},
                {gen_server,terminate,7,[{file,"gen_server.erl"},{line,795}]},
                {proc_lib,init_p_do_apply,3,
                          [{file,"proc_lib.erl"},{line,247}]}]

看起来Board模块未正确终止。的Board终止是这样的:

terminate(normal, State) ->
    io:format("Board.~p~n", [State]),
    ok.

我试图在一个独立的模块中重现这个b

-module(b).
-compile(export_all).

init([]) -> {ok, {1, 2}}.

terminate(_, State) ->
  io:format("Table Terminating.~p~n", [State]),
  ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}. 

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

handle_info(Msg, State) ->
  io:format("Unexpected message: ~p~n",[Msg]),
  {noreply, State}.

go() ->
  gen_server:start_link(?MODULE, [], []).

不成功:

4> {ok, B} = b:go(). {ok,<0.74.0>} 5> gen_server:stop(B). Table Terminating.{1,2}

我想知道的是,我应该寻找什么样的代码会导致我的董事会得到** Reason for termination == ** {terminated停止的理由?

完整来源在这里:git@github.com:QuantumProductions/tunnel.git

编辑:董事会包括这个

handle_call(stop, _From, State) ->
    {stop, normal, shutdown_ok, State};
handle_call(_, _, Board) ->
  {reply, {error, unrecognized, Board}, Board}.

编辑:我最好的猜测是eunit在其测试中创建的自动终止进程?

4

1 回答 1

0

为什么borad最后一条消息是[]?根据您使用的代码gen_server:stop/1which calls gen:stop/1which calls gen:stop/3which calls proc_lib:stop/3which finally call sys:terminate/3。当您调用sys:terminate/3gen_server 时,sys 会调用gen_server:system_terminate/4gen_server:terminate/6[]作为最后一条消息调用!

为什么borad有理由终止{terminated, ...}?根据io代码,io:format/2finally 调用io:execute_request/2并且你得到了this,因为 io 进程终止了,在这里它变成了{terminated, ...}.

于 2017-07-22T05:04:04.383 回答