Skip to content

Commit 82b4e7c

Browse files
committed
Merge pull request #1996 from pguyot/w47/more-robust-test_net_kernel
Make test_net_kernel more robust These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents ecc5863 + c9173b6 commit 82b4e7c

File tree

1 file changed

+50
-14
lines changed

1 file changed

+50
-14
lines changed

tests/libs/estdlib/test_net_kernel.erl

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,14 @@ test_autoconnect_fail(Platform) ->
160160
test_autoconnect_to_beam(Platform) ->
161161
{ok, _NetKernelPid} = net_kernel_start(Platform, atomvm),
162162
Node = node(),
163+
Parent = self(),
163164
erlang:set_cookie(Node, 'AtomVM'),
164165
{Pid, MonitorRef} = spawn_opt(
165166
fun() ->
166-
[] = execute_command(
167-
Platform,
167+
Command =
168168
"erl -sname otp -setcookie AtomVM -eval \""
169169
"register(beam, self()),"
170+
"io:put_chars(<<114,101,97,100,121,13,10>>),"
170171
"F = fun(G) ->"
171172
" receive"
172173
" {Caller, ping} -> Caller ! {self(), pong}, G(G);"
@@ -177,14 +178,19 @@ test_autoconnect_to_beam(Platform) ->
177178
" after 5000 -> exit(timeout)"
178179
" end "
179180
"end, "
180-
"F(F).\" -s init stop -noshell"
181-
)
181+
"F(F).\" -s init stop -noshell",
182+
Obj = subprocess(Platform, Command),
183+
"ready\r\n" = subprocess_line(Platform, Obj),
184+
Parent ! {self(), ready},
185+
[] = subprocess_loop(Platform, Obj)
182186
end,
183187
[link, monitor]
184188
),
185-
% Wait sufficiently for beam to be up, without connecting to it since
186-
% that's part of the test
187-
timer:sleep(1000),
189+
ok =
190+
receive
191+
{Pid, ready} -> ok
192+
after 5000 -> timeout
193+
end,
188194
[_, Host] = string:split(atom_to_list(Node), "@"),
189195
OTPNode = list_to_atom("otp@" ++ Host),
190196
{beam, OTPNode} ! {self(), ping},
@@ -469,23 +475,53 @@ test_is_alive(Platform) ->
469475
false = is_alive(),
470476
ok.
471477

472-
execute_command("BEAM", Command) ->
473-
os:cmd(Command);
474-
execute_command("ATOM", Command) ->
478+
subprocess("BEAM", Command) ->
479+
open_port({spawn_executable, "/bin/sh"}, [{args, ["-c", Command]}, {line, 256}, eof, in]);
480+
subprocess("ATOM", Command) ->
475481
{ok, _, Fd} = atomvm:subprocess("/bin/sh", ["sh", "-c", Command], undefined, [stdout]),
476-
Result = loop_read(Fd, []),
482+
Fd.
483+
484+
subprocess_line("BEAM", Port) ->
485+
receive
486+
{Port, {data, {eol, Line}}} -> lists:flatten([Line, "\r\n"])
487+
after 5000 ->
488+
exit(timeout)
489+
end;
490+
subprocess_line("ATOM", Fd) ->
491+
{ok, Line} = atomvm:posix_read(Fd, 10),
492+
binary_to_list(Line).
493+
494+
subprocess_loop("BEAM", Port) ->
495+
Result = beam_loop_read(Port, []),
496+
port_close(Port),
497+
Result;
498+
subprocess_loop("ATOM", Fd) ->
499+
Result = atomvm_loop_read(Fd, []),
477500
ok = atomvm:posix_close(Fd),
478501
Result.
479502

480-
loop_read(Fd, Acc) ->
503+
execute_command(Machine, Command) ->
504+
Obj = subprocess(Machine, Command),
505+
subprocess_loop(Machine, Obj).
506+
507+
atomvm_loop_read(Fd, Acc) ->
481508
case atomvm:posix_read(Fd, 10) of
482509
eof ->
483510
lists:flatten(lists:reverse(Acc));
484511
{error, eintr} ->
485512
% used with lldb ;-)
486-
loop_read(Fd, Acc);
513+
atomvm_loop_read(Fd, Acc);
487514
{ok, Line} ->
488-
loop_read(Fd, [binary_to_list(Line) | Acc])
515+
atomvm_loop_read(Fd, [binary_to_list(Line) | Acc])
516+
end.
517+
518+
beam_loop_read(Port, Acc) ->
519+
receive
520+
{Port, {data, {eol, Line}}} -> beam_loop_read(Port, ["\r\n", Line | Acc]);
521+
{Port, {data, {noeol, Line}}} -> beam_loop_read(Port, [Line | Acc]);
522+
{Port, eof} -> lists:flatten(lists:reverse(Acc))
523+
after 5000 ->
524+
exit(timeout)
489525
end.
490526

491527
net_kernel_start("ATOM", Nodename) ->

0 commit comments

Comments
 (0)