aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ic/test/erl_client_c_server_SUITE.erl
blob: a42dbb9604a9451e73a85caf4d4884b680226cf5 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2002-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%
%%
%%

%%----------------------------------------------------------------------
%% Purpose : Test suite for erl-client/c-server
%%----------------------------------------------------------------------


-module(erl_client_c_server_SUITE).
-include_lib("common_test/include/ct.hrl").

-export([init_per_testcase/2, end_per_testcase/2,all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, init_per_group/2,end_per_group/2, void_test/1,
	 long_test/1, longlong_test/1, ushort_test/1, ulong_test/1,
	 ulonglong_test/1, double_test/1, char_test/1, wchar_test/1,
	 octet_test/1, bool_test/1, struct_test/1, struct2_test/1,
	 seq1_test/1, seq2_test/1, seq3_test/1, seq4_test/1,
	 seq5_test/1, array1_test/1, array2_test/1, enum_test/1,
	 string1_test/1, string2_test/1, string3_test/1,
	 string4_test/1, pid_test/1, port_test/1, ref_test/1,
	 term_test/1, typedef_test/1, inline_sequence_test/1,
	 term_sequence_test/1, term_struct_test/1, wstring1_test/1]).

-define(DEFAULT_TIMEOUT, 20000).
-define(PORT_TIMEOUT, 15000).
-define(CALL_TIMEOUT, 5000).

-define(C_SERVER_NODE_NAME, idl_c_server_test).
  
%% Add/remove code path and watchdog before/after each test case.
%%
init_per_testcase(_Case, Config) ->
    DataDir = ?config(data_dir, Config),
    code:add_patha(DataDir),

    %% Since other test suites use the module m_i, we have
    %% to make sure we are using the right m_i module.
    code:purge(m_i),
    code:load_file(m_i),

    WatchDog = test_server:timetrap(?DEFAULT_TIMEOUT),
    [{watchdog, WatchDog}| Config].

end_per_testcase(_Case, Config) ->
    DataDir = ?config(data_dir, Config),
    code:del_path(DataDir),
    WatchDog = ?config(watchdog, Config),
    test_server:timetrap_cancel(WatchDog).

suite() -> [{ct_hooks,[ts_install_cth]}].

all() -> 
[void_test, long_test, longlong_test, ushort_test,
 ulong_test, ulonglong_test, double_test, char_test,
 wchar_test, octet_test, bool_test, struct_test,
 struct2_test, seq1_test, seq2_test, seq3_test,
 seq4_test, seq5_test, array1_test, array2_test,
 enum_test, string1_test, string2_test, string3_test,
 string4_test, pid_test, port_test, ref_test, term_test,
 typedef_test, inline_sequence_test, term_sequence_test,
 term_struct_test, wstring1_test].

groups() -> 
    [].

init_per_suite(Config) ->
    Config.

end_per_suite(_Config) ->
    ok.

init_per_group(_GroupName, Config) ->
	Config.

end_per_group(_GroupName, Config) ->
	Config.



array1_test(doc) -> "";
array1_test(suite) -> [];
array1_test(Config) ->
    do_test(array1_test, Config). 

array2_test(doc) -> "";
array2_test(suite) -> [];
array2_test(Config) ->
    do_test(array2_test, Config).

bool_test(doc) -> "";
bool_test(suite) -> [];
bool_test(Config) ->
    do_test(bool_test, Config).

char_test(doc) -> "";
char_test(suite) -> [];
char_test(Config) ->
    do_test(char_test, Config).

double_test(doc) -> "";
double_test(suite) -> [];
double_test(Config) ->
    do_test(double_test, Config).

enum_test(doc) -> "";
enum_test(suite) -> [];
enum_test(Config) ->
    do_test(enum_test, Config).

inline_sequence_test(doc) -> "";
inline_sequence_test(suite) -> [];
inline_sequence_test(Config) ->
    do_test(inline_sequence_test, Config).

longlong_test(doc) -> "";
longlong_test(suite) -> [];
longlong_test(Config) ->
    do_test(longlong_test, Config).

long_test(doc) -> "";
long_test(suite) -> [];
long_test(Config) ->
    do_test(long_test, Config).

octet_test(doc) -> "";
octet_test(suite) -> [];
octet_test(Config) ->
    do_test(octet_test, Config).

pid_test(doc) -> "";
pid_test(suite) -> [];
pid_test(Config) ->
    do_test(pid_test, Config).

port_test(doc) -> "";
port_test(suite) -> [];
port_test(Config) ->
    do_test(port_test, Config).

ref_test(doc) -> "";
ref_test(suite) -> [];
ref_test(Config) ->
    do_test(ref_test, Config).

seq1_test(doc) -> "";
seq1_test(suite) -> [];
seq1_test(Config) ->
    do_test(seq1_test, Config).

seq2_test(doc) -> "";
seq2_test(suite) -> [];
seq2_test(Config) ->
    do_test(seq2_test, Config).

seq3_test(doc) -> "";
seq3_test(suite) -> [];
seq3_test(Config) ->
    do_test(seq3_test, Config).

seq4_test(doc) -> "";
seq4_test(suite) -> [];
seq4_test(Config) ->
    do_test(seq4_test, Config).

seq5_test(doc) -> "";
seq5_test(suite) -> [];
seq5_test(Config) ->
    do_test(seq5_test, Config).

string1_test(doc) -> "";
string1_test(suite) -> [];
string1_test(Config) ->
    do_test(string1_test, Config).

string2_test(doc) -> "";
string2_test(suite) -> [];
string2_test(Config) ->
    do_test(string2_test, Config).

string3_test(doc) -> "";
string3_test(suite) -> [];
string3_test(Config) ->
    do_test(string3_test, Config).

string4_test(doc) -> "";
string4_test(suite) -> [];
string4_test(Config) ->
    do_test(string4_test, Config).

struct2_test(doc) -> "";
struct2_test(suite) -> [];
struct2_test(Config) ->
    do_test(struct2_test, Config).

struct_test(doc) -> "";
struct_test(suite) -> [];
struct_test(Config) ->
    do_test(struct_test, Config).

term_sequence_test(doc) -> "";
term_sequence_test(suite) -> [];
term_sequence_test(Config) ->
    do_test(term_sequence_test, Config).

term_struct_test(doc) -> "";
term_struct_test(suite) -> [];
term_struct_test(Config) ->
    do_test(term_struct_test, Config).

term_test(doc) -> "";
term_test(suite) -> [];
term_test(Config) ->
    do_test(term_test, Config).

typedef_test(doc) -> "";
typedef_test(suite) -> [];
typedef_test(Config) ->
    do_test(typedef_test, Config).

ulonglong_test(doc) -> "";
ulonglong_test(suite) -> [];
ulonglong_test(Config) ->
    do_test(ulonglong_test, Config).

ulong_test(doc) -> "";
ulong_test(suite) -> [];
ulong_test(Config) ->
    do_test(ulong_test, Config).

ushort_test(doc) -> "";
ushort_test(suite) -> [];
ushort_test(Config) ->
    do_test(ushort_test, Config).

void_test(doc) -> "";
void_test(suite) -> [];
void_test(Config) ->
    do_test(void_test, Config).

wchar_test(doc) -> "";
wchar_test(suite) -> [];
wchar_test(Config) ->
    do_test(wchar_test, Config).

wstring1_test(doc) -> "";
wstring1_test(suite) -> [];
wstring1_test(Config) ->
    do_test(wstring1_test, Config).


do_test(Case, Config) ->
    %% Trap exits
    process_flag(trap_exit, true),
    Node = atom_to_list(node()),
    [_NodeName, HostName] = string:tokens(Node, "@"),
    DataDir = ?config(data_dir, Config),
    %% io:format("~p: data directory: ~p~n", [?MODULE, DataDir]),
    Cookie = atom_to_list(erlang:get_cookie()),
    ServerNodeName = atom_to_list(?C_SERVER_NODE_NAME), 
    %% Start C-server node as a port program. We wait for the node
    %% to connect to us.
    Cmd = filename:join([DataDir, "c_server"]) ++
	" -this-node-name " ++ ServerNodeName ++ 
	" -peer-node " ++ Node ++
	" -cookie " ++ Cookie, 
    Port = open_port({spawn, Cmd}, [exit_status, eof, stderr_to_stdout]),
    ServerNode = list_to_atom(ServerNodeName ++ "@" ++ HostName),
    Res = case wait_for_hidden_node(ServerNode) of
	      ok ->
		  %% Need a port for port_test and typedef_test
		  put(port_test_port, Port),
		  R = (catch erl_client:Case(ServerNode, ?CALL_TIMEOUT)),
		  case wait_for_completion(Port) of
		      {error, timeout} ->
			  kill_off_node(ServerNode);
		      _ ->
			  ok
		  end,
		  R;
	      {error, timeout} ->
		  case wait_for_completion(Port) of
		      {error, timeout} ->
			  kill_off_node(ServerNode);
		      _ ->
			  ok
		  end,
		  {error, timeout}
	  end,
    process_flag(trap_exit, false),
    true = Res.


%% Wait for eof *and* exit status, but return if exit status indicates
%% an error, or we have been waiting more than PORT_TIMEOUT seconds.
%%
wait_for_completion(Port) ->
    wait_for_completion(Port, 0).

wait_for_completion(Port, N) when N < 2 ->
    receive
	{Port, {data, Bytes}} ->
	    %% Relay output
	    io:format("~s", [Bytes]),
	    wait_for_completion(Port, N);
	{Port, {exit_status, 0}} ->
	    wait_for_completion(Port, N + 1);
	{Port, {exit_status, Status}} ->
	    {error, Status};
	{Port, eof} ->
	    wait_for_completion(Port, N + 1);
	{'EXIT', Port, Reason} ->
	    io:format("Port exited with reason: ~w~n", [Reason]),
	    wait_for_completion(Port, N);
	{'EXIT', From, Reason} ->
	    io:format("Got unexpected exit: ~p~n", [{'EXIT', From, Reason}]),
	    wait_for_completion(Port, N)
    after ?PORT_TIMEOUT ->
	    {error, timeout}
    end;
wait_for_completion(_, _) ->
    ok.
	    
wait_for_hidden_node(Node) ->
    Times = ?DEFAULT_TIMEOUT div 100,
    wait_for_hidden_node(Node, Times, 100).

wait_for_hidden_node(Node, Times, WaitTime) when Times > 0 ->
    io:format("Waiting for hidden node: ~p~n", [Node]), 
    case lists:member(Node, erlang:nodes(hidden)) of
	true ->
	    ok;
	false ->
	    delay(WaitTime),
	    wait_for_hidden_node(Node, Times - 1, WaitTime)
    end;
wait_for_hidden_node(_Node, _, _WaitTime) ->
    {error, timeout}.

kill_off_node(Node) ->
    catch rpc:cast(Node, erlang, halt, [1]).

delay(Time) ->
    receive
	after Time ->
		ok
	end.