File 2082-Add-smoke-test-for-the-erl_uds_dist-example.patch of Package erlang

From 0a2fac71f3b982868f0cc3ac4369dbd632d0c7b2 Mon Sep 17 00:00:00 2001
From: Rickard Green <rickard@erlang.org>
Date: Wed, 3 Jun 2020 18:17:05 +0200
Subject: [PATCH 2/4] Add smoke test for the erl_uds_dist example

---
 .gitignore                                 |   1 +
 lib/kernel/test/Makefile                   |   4 +
 lib/kernel/test/erl_distribution_SUITE.erl | 181 ++++++++++++++++++++-
 3 files changed, 184 insertions(+), 2 deletions(-)

diff --git a/lib/kernel/test/Makefile b/lib/kernel/test/Makefile
index 6a3696f92e..fdc7476ebc 100644
--- a/lib/kernel/test/Makefile
+++ b/lib/kernel/test/Makefile
@@ -65,6 +65,7 @@ MODULES= \
 	erl_distribution_SUITE \
 	erl_distribution_wb_SUITE \
 	erl_prim_loader_SUITE \
+	erl_uds_dist \
 	error_handler_SUITE \
 	error_logger_SUITE \
 	error_logger_warn_SUITE \
@@ -165,6 +166,9 @@ SOCKET_TARGETS = $(SOCKET_MODULES:%=$(EBIN)/%.$(EMULATOR))
 gen_tcp_dist.erl: ../examples/gen_tcp_dist/src/gen_tcp_dist.erl
 	cp $< $@
 
+erl_uds_dist.erl: ../examples/erl_uds_dist/src/erl_uds_dist.erl
+	cp $< $@
+
 make_emakefile: $(ERL_FILES)
 	$(ERL_TOP)/make/make_emakefile $(ERL_COMPILE_FLAGS) -o$(EBIN) '*_SUITE_make' \
 	> $(EMAKEFILE)
diff --git a/lib/kernel/test/erl_distribution_SUITE.erl b/lib/kernel/test/erl_distribution_SUITE.erl
index cf5b2d537d..da27992a77 100644
--- a/lib/kernel/test/erl_distribution_SUITE.erl
+++ b/lib/kernel/test/erl_distribution_SUITE.erl
@@ -46,6 +46,7 @@
          monitor_nodes_down_up/1,
          dist_ctrl_proc_smoke/1,
          dist_ctrl_proc_reject/1,
+         erl_uds_dist_smoke_test/1,
          erl_1424/1,
          net_kernel_start/1]).
 
@@ -63,6 +64,8 @@
 
 -export([pinger/1]).
 
+-export([start_uds_rpc_server/1]).
+
 -define(DUMMY_NODE,dummy@test01).
 -define(ALT_EPMD_PORT, "12321").
 -define(ALT_EPMD_CMD, "epmd -port "++?ALT_EPMD_PORT).
@@ -85,6 +88,7 @@ all() ->
      hidden_node, setopts,
      table_waste, net_setuptime, inet_dist_options_options,
      {group, monitor_nodes},
+     erl_uds_dist_smoke_test,
      erl_1424, net_kernel_start].
 
 groups() -> 
@@ -1706,6 +1710,177 @@ smoke_communicate(Node, OLoopMod, OLoopFun) ->
     ok.
 
 
+erl_uds_dist_smoke_test(Config) when is_list(Config) ->
+    Me = self(),
+    [Node1, Node2] = lists:map(fun (Name) ->
+                                       list_to_atom(atom_to_list(Name) ++ "@localhost")
+                               end,
+                               get_nodenames(2, erl_uds_dist_smoke_test)),
+    {LPort, Acceptor} = uds_listen(),
+    start_uds_node(Node1, LPort),
+    start_uds_node(Node2, LPort),
+    receive
+        {uds_nodeup, N1} ->
+            io:format("~p is up~n", [N1])
+    end,
+    receive
+        {uds_nodeup, N2} ->
+            io:format("~p is up~n", [N2])
+    end,
+    
+    io:format("Testing ping net_adm:ping(~p) on ~p~n", [Node2, Node1]),
+    Node1 ! {self(), {net_adm, ping, [Node2]}},
+    receive
+        {Node1, PingRes} ->
+            io:format("~p~n", [PingRes]),
+            pong = PingRes
+    end,
+
+    io:format("Testing nodes() on ~p~n", [Node1]),
+    Node1 ! {self(), {erlang, nodes, []}},
+    receive
+        {Node1, N1List} ->
+            io:format("~p~n", [N1List]),
+            [Node2] = N1List
+    end,
+
+    io:format("Testing nodes() on ~p~n", [Node2]),
+    Node2 ! {self(), {erlang, nodes, []}},
+    receive
+        {Node2, N2List} ->
+            io:format("~p~n", [N2List]),
+            [Node1] = N2List
+    end,
+
+    io:format("Shutting down~n", []),
+
+    Node1 ! {self(), close},
+    Node2 ! {self(), close},
+
+    receive {Node1, C1} -> ok = C1 end,
+    receive {Node2, C2} -> ok = C2 end,
+
+    unlink(Acceptor),
+    exit(Acceptor, kill),
+
+    io:format("ok~n", []),
+
+    ok.
+
+%% Helpers for testing the erl_uds_dist example
+
+uds_listen() ->
+    Me = self(),
+    {ok, LSock} = gen_tcp:listen(0, [binary, {packet, 4}, {active, false}]),
+    {ok, LPort} = inet:port(LSock),
+    {LPort, spawn_link(fun () ->
+                               uds_accept_loop(LSock, Me)
+                       end)}.
+
+uds_accept_loop(LSock, TestProc) ->
+    {ok, Sock} = gen_tcp:accept(LSock),
+    _ = spawn_link(fun () ->
+                           uds_rpc_client_init(Sock, TestProc)
+                   end),
+    uds_accept_loop(LSock, TestProc).
+
+uds_rpc(Sock, MFA) ->
+    ok = gen_tcp:send(Sock, term_to_binary(MFA)),
+    case gen_tcp:recv(Sock, 0) of
+        {error, Reason} ->
+            error({recv_failed, Reason});
+        {ok, Packet} ->
+            binary_to_term(Packet)
+    end.
+
+uds_rpc_client_init(Sock, TestProc) ->
+    case uds_rpc(Sock, {erlang, node, []}) of
+        nonode@nohost ->
+            %% Wait for distribution to come up...
+            receive after 100 -> ok end,
+            uds_rpc_client_init(Sock, TestProc);
+        Node when is_atom(Node) ->
+            register(Node, self()),
+            TestProc ! {uds_nodeup, Node},
+            uds_rpc_client_loop(Sock, Node)
+    end.
+
+uds_rpc_client_loop(Sock, Node) ->
+    receive
+        {From, close} ->
+            ok = gen_tcp:send(Sock, term_to_binary(close)),
+            From ! {Node, gen_tcp:close(Sock)},
+            exit(normal);
+        {From, ApplyData} ->
+            From ! {Node, uds_rpc(Sock, ApplyData)},
+            uds_rpc_client_loop(Sock, Node)
+    end.
+
+uds_rpc_server_loop(Sock) ->
+    case gen_tcp:recv(Sock, 0) of
+        {error, Reason} ->
+            error({recv_failed, Reason});
+        {ok, Packet} ->
+            case binary_to_term(Packet) of
+                {M, F, A} when is_atom(M), is_atom(F), is_list(A) ->
+                    ok = gen_tcp:send(Sock, term_to_binary(apply(M, F, A)));
+                {F, A} when is_function(F), is_list(A) ->
+                    ok = gen_tcp:send(Sock, term_to_binary(apply(F, A)));
+                close ->
+                    ok = gen_tcp:close(Sock),
+                    exit(normal);
+                Other ->
+                    error({unexpected_data, Other})
+            end
+    end,
+    uds_rpc_server_loop(Sock).
+
+start_uds_rpc_server([PortString]) ->
+    Port = list_to_integer(PortString),
+    {Pid, Mon} = spawn_monitor(fun () ->
+                                       {ok, Sock} = gen_tcp:connect({127,0,0,1}, Port,
+                                                                    [binary, {packet, 4},
+                                                                     {active, false}]),
+                                       uds_rpc_server_loop(Sock)
+                               end),
+    receive
+        {'DOWN', Mon, process, Pid, Reason} ->
+            if Reason == normal ->
+                    halt();
+               true ->
+                    EStr = lists:flatten(io_lib:format("uds rpc server crashed: ~p", [Reason])),
+                    (catch file:write_file("uds_rpc_server_crash."++os:getpid(), EStr)),
+                    halt(EStr)
+            end
+    end.
+
+start_uds_node(NodeName, LPort) ->
+    Static = "-detached -noinput -proto_dist erl_uds",
+    Pa = filename:dirname(code:which(?MODULE)),
+    Prog = case catch init:get_argument(progname) of
+	       {ok,[[P]]} -> P;
+	       _ -> error(no_progname_argument_found)
+	   end,
+    {ok, Pwd} = file:get_cwd(),
+    NameStr = atom_to_list(NodeName),
+    CmdLine = Prog ++ " "
+	++ Static
+	++ " -sname " ++ NameStr
+	++ " -pa " ++ Pa
+	++ " -env ERL_CRASH_DUMP " ++ Pwd ++ "/erl_crash_dump." ++ NameStr
+	++ " -setcookie " ++ atom_to_list(erlang:get_cookie())
+        ++ " -run " ++ atom_to_list(?MODULE) ++ " start_uds_rpc_server "
+        ++ integer_to_list(LPort),
+    io:format("Starting: ~p~n", [CmdLine]),
+    case open_port({spawn, CmdLine}, []) of
+	Port when is_port(Port) ->
+	    unlink(Port),
+	    erlang:port_close(Port);
+	Error ->
+	    error({open_port_failed, Error})
+    end,
+    ok.
+
 erl_1424(Config) when is_list(Config) ->
     {error, Reason} = erl_epmd:names("."),
     {comment, lists:flatten(io_lib:format("Reason: ~p", [Reason]))}.
-- 
2.26.2

openSUSE Build Service is sponsored by