aboutsummaryrefslogtreecommitdiffstats
path: root/lib/erl_interface/test/ei_connect_SUITE.erl
blob: 603a300a260cfdbcd1ff86d12565cc911e93529e (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2001-2010. 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_connect_SUITE).

-include_lib("test_server/include/test_server.hrl").
-include("ei_connect_SUITE_data/ei_connect_test_cases.hrl").

-export([
	all/0,groups/0,init_per_group/2,end_per_group/2, 
	init_per_testcase/2, 
	end_per_testcase/2,

	ei_send/1, 
	ei_reg_send/1, 
	ei_format_pid/1,
	ei_rpc/1, 
	rpc_test/1, 
	ei_send_funs/1,
	ei_threaded_send/1,
	ei_set_get_tracelevel/1
	]).

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

all() -> 
[ei_send, ei_reg_send, ei_rpc, ei_format_pid, ei_send_funs,
 ei_threaded_send, ei_set_get_tracelevel].

groups() -> 
    [].

init_per_group(_GroupName, Config) ->
	Config.

end_per_group(_GroupName, Config) ->
	Config.

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

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

ei_send(Config) when is_list(Config) ->
    ?line P = runner:start(?interpret),
    ?line 0 = ei_connect_init(P, 42, erlang:get_cookie(), 0),
    ?line {ok,Fd} = ei_connect(P, node()),

    ?line ok = ei_send(P, Fd, self(), AMsg={a,message}),
    ?line receive AMsg -> ok end,

    ?line runner:send_eot(P),
    ?line runner:recv_eot(P),
    ok.

ei_format_pid(Config) when is_list(Config) ->
    ?line S = self(),
    ?line P = runner:start(?interpret),
    ?line 0 = ei_connect_init(P, 42, erlang:get_cookie(), 0),
    ?line {ok,Fd} = ei_connect(P, node()),

    ?line ok = ei_format_pid(P, Fd, S),
    ?line receive S -> ok end,

    ?line runner:send_eot(P),
    ?line runner:recv_eot(P),
    ok.

ei_send_funs(Config) when is_list(Config) ->
    ?line P = runner:start(?interpret),
    ?line 0 = ei_connect_init(P, 42, erlang:get_cookie(), 0),
    ?line {ok,Fd} = ei_connect(P, node()),
    
    ?line Fun1 = fun ei_send/1,
    ?line Fun2 = fun(X) -> P, X, Fd, Fun1 end,

    ?line AMsg={Fun1,Fun2},
    %%AMsg={wait_with_funs, new_dist_format},
    ?line ok = ei_send_funs(P, Fd, self(), AMsg),
    ?line EIMsg = receive M -> M end,
    ?line EIMsg = AMsg,

    ?line runner:send_eot(P),
    ?line runner:recv_eot(P),
    ok.

ei_reg_send(Config) when is_list(Config) ->
    ?line P = runner:start(?interpret),
    ?line 0 = ei_connect_init(P, 42, erlang:get_cookie(), 0),
    ?line {ok,Fd} = ei_connect(P, node()),

    ARegName = a_strange_registred_name,
    ?line register(ARegName, self()),
    ?line ok = ei_reg_send(P, Fd, ARegName, AMsg={another,[strange],message}),
    ?line receive AMsg -> ok end,

    ?line runner:send_eot(P),
    ?line runner:recv_eot(P),
    ok.

ei_threaded_send(Config) when is_list(Config) ->
    ?line Einode = filename:join(?config(data_dir, Config), "einode"),
    ?line N = 15,
    ?line Host = atom_to_list(node()),
    ?line TestServerPid = self(),
    ?line [ spawn_link(fun() -> rec_einode(I, TestServerPid) end)
	    || I <- lists:seq(0, N-1) ],
    ?line [ receive {I,registered} -> ok end
	    || I <- lists:seq(0, N-1) ],
    ?line spawn_link(fun() -> start_einode(Einode, N, Host) end),
    ?line [ receive I -> ok end
	    || I <- lists:seq(0, N-1) ],
    ok.

rec_einode(N, TestServerPid) ->
    ?line Regname = list_to_atom("mth"++integer_to_list(N)),
    ?line register(Regname, self()),
    ?line TestServerPid ! {N, registered},
    ?line io:format("~p waiting~n", [Regname]),
    ?line receive
	      X ->
		  ?line io:format("Received by ~s ~p~n", [Regname, X]),
		  ?line TestServerPid ! N,
		  ?line X
	  after 10000 ->
		  ?line test_server:fail(Regname)
	  end.

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

ei_rpc(Config) when is_list(Config) ->
    ?line P = runner:start(?interpret),
    ?line 0 = ei_connect_init(P, 42, erlang:get_cookie(), 0),
    ?line {ok,Fd} = ei_connect(P, node()),

    ?line S= "Hej du glade!", SRev = lists:reverse(S),
    ?line X = ei_rpc(P, Fd, self(), {?MODULE, rpc_test}, [SRev]),
    ?line {term, S}= X,

    ?line runner:send_eot(P),
    ?line runner:recv_eot(P),
    ok.

ei_set_get_tracelevel(Config) when is_list(Config) ->
    ?line P = runner:start(?interpret),
    ?line 5 = ei_set_get_tracelevel(P, 5),
    ?line 0 = ei_connect_init(P, 42, erlang:get_cookie(), 0),
    ?line {ok,Fd} = ei_connect(P, node()),

    ?line S= "Hej du glade!", SRev = lists:reverse(S),
    ?line X = ei_rpc(P, Fd, self(), {?MODULE, rpc_test}, [SRev]),
    ?line {term, S}= X,

    ?line 0 = ei_set_get_tracelevel(P, 0),

    ?line runner:send_eot(P),
    ?line runner:recv_eot(P),
    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_connect(P, Node) ->
    send_command(P, ei_connect, [Node]),
    case get_term(P) of
	{term,{Fd,_}} when Fd >= 0 -> {ok,Fd};
	{term,{-1,Errno}} -> {error,Errno}
    end.

ei_set_get_tracelevel(P, Tracelevel) ->
    send_command(P, ei_set_get_tracelevel, [Tracelevel]),
    case get_term(P) of
	{term,{tracelevel, Level}} when is_integer(Level) -> Level
    end.

ei_send(P, Fd, To, Msg) ->
    send_command(P, ei_send, [Fd,To,Msg]),
    get_send_result(P).

ei_format_pid(P, Fd, To) ->
    send_command(P, ei_format_pid, [Fd, To]),
    get_send_result(P).

ei_send_funs(P, Fd, To, Msg) ->
    send_command(P, ei_send_funs, [Fd,To,Msg]),
    get_send_result(P).

ei_reg_send(P, Fd, To, Msg) ->
    send_command(P, ei_reg_send, [Fd,To,Msg]),
    get_send_result(P).

ei_rpc(P, Fd, To, Func, Msg) ->
    send_command(P, ei_rpc, [Fd, To, Func, Msg]),
    get_term(P).


get_send_result(P) ->
    case get_term(P) of
	{term,{0,_}} -> ok;
	{term,{1,_}} -> ok;
	{term,{-1,Errno}} -> {error,Errno};
	{term,{Res,Errno}}->
	    io:format("Return value: ~p\nerl_errno: ~p", [Res,Errno]),
	    ?t:fail(bad_return_value)
    end.

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

%%% Test function for RPC

rpc_test(S) ->    
    lists:reverse(S).