Erlang:聊天服务器在启动时崩溃

时间:2015-07-23 22:30:29

标签: erlang

我得到了一组程序应该通过的测试,当我尝试运行服务器崩溃的远程测试时,所有本地测试都可以正常运行。

崩溃消息如下:

=ERROR REPORT==== 23-Jul-2015::23:59:17 === Error in process <0.39.0> on
node 'nodeS@127.0.0.1' with exit value: 
{undef,[{genserver,start,[server, {server_st,[],[]},#Fun<server.loop.2>],[]}]}

我的启动功能如下:

loop(St, {From, Nick, connection_wanted}) ->
    case lists:keymember(Nick, 2, St#server_st.users) of
        false -> {ok, St#server_st{users = St#server_st.users ++ [{From, Nick}]}};
        true -> {{user_already_connected, St}, St}
    end;

记录&#34; server_st&#34;定义为:

-record(server_st, {users = [], channels = []}).

最后,genserver启动和循环功能是:

start(Name, State, F) ->
Pid = spawn(fun() -> loop(State, F) end),
register(Name, Pid),
Pid.

loop(State, F) ->
receive
{request, From, Ref, Data} ->
    case catch(F(State, Data)) of
    {'EXIT', Reason} ->
        From!{exit, Ref, Reason},
        loop(State, F);
    {R, NewState} ->
        From!{result, Ref, R},
        loop(NewState, F)
    end;
{update, From, Ref, NewF} ->
    From ! {ok, Ref},
    loop(State, NewF);
stop ->
    true
end.

然后genserver功能我不允许改变。如果需要,我也可以发布整个测试套件。

修改 进一步深入测试用例并且我不确定它是否真的是导致问题的服务器,我的远程连接功能如下所示:

loop(St, {connect, {_Server, _Machine}}) ->
ServerPID = {list_to_atom(_Server), list_to_atom(_Machine)},
case genserver:request(ServerPID, {self(), St#cl_st.nick, connection_wanted}) of
    ok -> {ok, St#cl_st{connected_to = ServerPID}};
    _ -> {{error, user_already_connected, "A user with the nick " ++ St#cl_st.nick ++ "is already connected to" ++ _Server}, St}
end;

编辑2 找到导致错误的测试套件内的特定行:

-define(HOST, '127.0.0.1').

new_client(Nick, GUIName) ->
ClientName = test_client:find_unique_name("client_"),
ClientAtom = list_to_atom(ClientName),

% Row below is causing the error
Result = slave:start(?HOST, ClientAtom),

assert_ok("start client node "++ClientName, element(1,Result)),
ClientNode = element(2,Result),

InitState = client:initial_state(Nick, GUIName),
Result2 = spawn(ClientNode, genserver, start, [ClientAtom, InitState, fun client:loop/2]),
assert("client startup "++ClientName, is_pid(Result2)),

{Nick, ClientAtom, ClientNode}.

2 个答案:

答案 0 :(得分:1)

您的函数genserver:start/3很可能未导出,或者您运行调用它的代码的节点上没有模块genserver

答案 1 :(得分:0)

解决了它,在一个完全不相关的部分,客户端与其他用户进行通信。仍然使用whereis命令从旧版本的程序中找到其他用户。