gen_server停止子进程停止的badmatch

时间:2017-07-15 22:52:19

标签: erlang gen-server eunit

我有一个table进程,pidboard创建一个go()作为其kills_board_test() -> {ok, Table} = table:go(), {BoardPid, _ ,_ } = s:s(Table, info), gen_server:stop(Table), undefined = process_info(BoardPid). 状态的一部分。

我正在测试终止表还终止了主板。但董事会没有按预期停止。

我的测试:

table.erl

handle_call(stop, _From, State = {Board, _, _}) -> gen_server:stop(Board), {stop, normal, shutdown_ok, State}; 我实施:

> 6>table_test:test(). table_test: kills_board_test (module
> 'table_test')...*failed* in function table_test:kills_board_test/0
> (table_test.erl, line 8)
> **error:{badmatch,[{current_function,{gen_server,loop,6}},
>            {initial_call,{proc_lib,init_p,5}},
>            {status,waiting},
>            {message_queue_len,0},
>            {messages,[]},
>            {links,[]},
>            {dictionary,[{'$initial_call',{board,init,1}},
>                         {'$ancestors',[<0.351.0>,<0.349.0>]}]},
>            {trap_exit,false},
>            {error_handler,error_handler},
>            {priority,normal},
>            {group_leader,<0.350.0>},
>            {total_heap_size,233},
>            {heap_size,233},
>            {stack_size,9},
>            {reductions,152},
>            {garbage_collection,...},
>            {...}]}   output:<<"Table Terminating.{<0.352.0>,
>                    #{current_player => x,result => playing},
>                    #{o => {<0.355.0>,<0.356.0>},x => {<0.353.0>,<0.354.0>}}} ">>
> 
> =======================================================   Failed: 1.  Skipped: 0.  Passed: 0. error 

我的结果是:

S

编辑:

我看到文本“终止”,但我知道停止会在返回之前等待终止完成。

1 个答案:

答案 0 :(得分:1)

您的自定义停止行为位于handle_call(stop, ...),但您正在调用gen_server:stop/1,但不会调用该代码。您可能打算改为gen_server:call(Table, stop)

也就是说,您可能希望将此停止行为移至Module:terminate/2gen_server:stop自动调用 (除非您有理由在handle_call):

terminate(_Reason, _State = {Board, _, _}) ->
  gen_server:stop(Board)

现在,gen_server:stop(Table)将调用此回调并自动停止Board