aboutsummaryrefslogtreecommitdiffstats
path: root/lib/erl_interface/test/ei_accept_SUITE.erl
blob: 07c1e32a68dae8e66cc7869d01669ee80ce4a547 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2001-2009. All Rights Reserved.
%% 
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% %CopyrightEnd%
%%

%%
-module(ei_accept_SUITE).

-include("test_server.hrl").
-include("ei_accept_SUITE_data/ei_accept_test_cases.hrl").

-export([all/1, init_per_testcase/2, fin_per_testcase/2,
	 ei_accept/1, ei_threaded_accept/1]).

-import(runner, [get_term/1,send_term/2]).

all(suite) -> [ei_accept, ei_threaded_accept].

init_per_testcase(_Case, Config) ->
    Dog = ?t:timetrap(?t:minutes(0.25)),
    [{watchdog, Dog}|Config].

fin_per_testcase(_Case, Config) ->
    Dog = ?config(watchdog, Config),
    test_server:timetrap_cancel(Dog),
    ok.

ei_accept(Config) when is_list(Config) ->
    ?line P = runner:start(?interpret),
    ?line 0 = ei_connect_init(P, 42, erlang:get_cookie(), 0),

    ?line Myname= hd(tl(string:tokens(atom_to_list(node()), "@"))),
    ?line io:format("Myname ~p ~n",  [Myname]),
    ?line EINode= list_to_atom("c42@"++Myname),
    ?line io:format("EINode ~p ~n",  [EINode]),
    ?line Self= self(),
    ?line TermToSend= {call, Self, "Test"},
    ?line F= fun() ->
		     case waitfornode("c42",20) of
			 true ->
			     {any, EINode} ! TermToSend,
			     Self ! sent_ok;
			 false ->
			     Self ! never_published
		     end,
		     ok
	     end,

    ?line spawn(F),
    ?line Port  = 6543,
    ?line {ok, Fd, _Node} = ei_accept(P, Port),
    ?line TermReceived= ei_receive(P, Fd),
    ?line io:format("Sent ~p received ~p ~n", [TermToSend, TermReceived]),
    ?line TermToSend= TermReceived,
    ?line receive
	      sent_ok ->
		  ok;
	      Unknown ->
		  io:format("~p ~n", [Unknown])
	  after 1000 ->
		  io:format("timeout ~n")
	  end,
    ?line ok= ei_unpublish(P),
    ok.

ei_threaded_accept(Config) when is_list(Config) ->
    ?line Einode = filename:join(?config(data_dir, Config), "eiaccnode"),
    ?line N = 1, % 3,
    ?line Host = atom_to_list(node()),
    ?line Port = 6767,
    ?line start_einode(Einode, N, Host, Port),
    ?line io:format("started eiaccnode"),
    %%?line spawn_link(fun() -> start_einode(Einode, N, Host, Port) end),
    ?line TestServerPid = self(),
    ?line [ spawn_link(fun() -> send_rec_einode(I, TestServerPid) end)
	    || I <- lists:seq(0, N-1) ],
    ?line [ receive I -> ok end
	    || I <- lists:seq(0, N-1) ],
    ok.

waitfornode(String,0) ->
    io:format("~s never published itself.~n",[String]),
    false;
waitfornode(String,N) ->
    Registered = [X || {X,_} <- element(2,erl_epmd:names())],
    case lists:member(String,Registered) of
	true ->
	    true;
	false ->
	    timer:sleep(1000),
	    waitfornode(String,N-1)
    end.

send_rec_einode(N, TestServerPid) ->
    ?line Myname= hd(tl(string:tokens(atom_to_list(node()), "@"))),
    ?line FirstPart = "eiacc" ++ integer_to_list(N),
    ?line EINode= list_to_atom(FirstPart ++ "@" ++ Myname),
    ?line io:format("EINode ~p ~n",  [EINode]),
    ?line Self= self(),
    ?line case waitfornode(FirstPart,20) of
	      true -> ok;
	      false -> test_server:fail({never_published,EINode})
	  end,
    ?line {any, EINode} ! Self,
    ?line receive
	      {N,_}=X ->
		  ?line io:format("Received by ~s ~p~n", [EINode, X]),
		  ?line TestServerPid ! N,
		  ?line X
	  after 10000 ->
		  ?line test_server:fail(EINode)
	  end.

start_einode(Einode, N, Host, Port) ->
    Einodecmd = Einode ++ " " ++ atom_to_list(erlang:get_cookie())
	++ " " ++ integer_to_list(N) ++ " " ++ Host ++ " "
	++ integer_to_list(Port) ++ " nothreads",
    io:format("Einodecmd  ~p ~n", [Einodecmd]),      
    ?line open_port({spawn, Einodecmd}, []),
    ok.



%%% Interface functions for ei (erl_interface) functions.

ei_connect_init(P, Num, Cookie, Creation) ->
    send_command(P, ei_connect_init, [Num,Cookie,Creation]),
    case get_term(P) of
	{term,Int} when is_integer(Int) -> Int
    end.

ei_accept(P, PortNo) ->
    send_command(P, ei_accept, [PortNo]),
    case get_term(P) of
	{term,{Fd, _, Node}} when Fd >= 0 -> {ok, Fd, Node};
	{term,{_Fd, Errno, _Node}} -> {error,Errno}
    end.

ei_receive(P, Fd) ->
    send_command(P, ei_receive, [Fd]),
    {term, T}= get_term(P),
    T.

ei_unpublish(P) ->
    send_command(P, ei_unpublish, []),
    case get_term(P) of
	{term,{0, _}} -> ok;
	{term,{_X, Errno}} -> {error,Errno}
    end.

send_command(P, Name, Args) ->
    runner:send_term(P, {Name,list_to_tuple(Args)}).