2017-07-15 48 views
0

我有一個table進程創建一個pidboard作爲其狀態的一部分go()gen_server停止不匹配子進程停止

我測試的是終止該表還終止板。但董事會並沒有按預期停止。

我的測試:

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 

編輯:

我看到的文字 「終止」 ,但我明白了該站點將在返回之前等待終止完成。

+2

您的意思是做'gen_server:調用(表,停止)''的替代gen_server:停止(表)'? – Dogbert

+0

* doh *,是的,謝謝! (再次,你是英雄) – quantumpotato

回答

1

您的自定義停止行爲是handle_call(stop, ...),但您打電話gen_server:stop/1將不會調用該代碼。你可能打算做gen_server:call(Table, stop)

這就是說,你可能會想遷移停止行爲Module:terminate/2,這通過gen_server:stop自動調用(除非你有充分的理由來實現在handle_call這種行爲):

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

現在, gen_server:stop(Table)會調用這個回調並自動停止Board