diff options
author | Micael Karlberg <[email protected]> | 2018-06-28 21:57:03 +0200 |
---|---|---|
committer | Micael Karlberg <[email protected]> | 2018-09-18 14:50:18 +0200 |
commit | b09136301525b0717e897ec0864c3d2ea7708758 (patch) | |
tree | ab233bd30a8d903e391b2f5f76e832a2ec49390f | |
parent | 9b45f4dd2cc8862a183941bb4925223bcec35987 (diff) | |
download | otp-b09136301525b0717e897ec0864c3d2ea7708758.tar.gz otp-b09136301525b0717e897ec0864c3d2ea7708758.tar.bz2 otp-b09136301525b0717e897ec0864c3d2ea7708758.zip |
[socket-nit-test] Added some more test code
Added some more features o the simple socket test server
and client.
OTP-14831
-rw-r--r-- | lib/kernel/test/Makefile | 13 | ||||
-rw-r--r-- | lib/kernel/test/socket_client.erl | 23 | ||||
-rw-r--r-- | lib/kernel/test/socket_server.erl | 73 |
3 files changed, 103 insertions, 6 deletions
diff --git a/lib/kernel/test/Makefile b/lib/kernel/test/Makefile index 4a86265a4a..07e7922d3d 100644 --- a/lib/kernel/test/Makefile +++ b/lib/kernel/test/Makefile @@ -24,6 +24,10 @@ include $(ERL_TOP)/make/$(TARGET)/otp.mk # Target Specs # ---------------------------------------------------- +SOCKET_MODULES = \ + socket_server \ + socket_client + MODULES= \ rpc_SUITE \ pdict_SUITE \ @@ -90,7 +94,8 @@ MODULES= \ sendfile_SUITE \ standard_error_SUITE \ multi_load_SUITE \ - zzz_SUITE + zzz_SUITE \ + $(SOCKET_MODULES) APP_FILES = \ appinc.app \ @@ -127,6 +132,9 @@ ERL_COMPILE_FLAGS += EBIN = . +SOCKET_TARGETS = $(SOCKET_MODULES:%=$(EBIN)/%.$(EMULATOR)) + + # ---------------------------------------------------- # Targets # ---------------------------------------------------- @@ -147,6 +155,9 @@ clean: docs: +socket: $(SOCKET_TARGETS) + + # ---------------------------------------------------- # Release Target # ---------------------------------------------------- diff --git a/lib/kernel/test/socket_client.erl b/lib/kernel/test/socket_client.erl index 6d74d19442..13e87f4109 100644 --- a/lib/kernel/test/socket_client.erl +++ b/lib/kernel/test/socket_client.erl @@ -6,7 +6,7 @@ %%% @end %%% Created : 27 Jun 2018 by Micael Karlberg <[email protected]> %%%------------------------------------------------------------------- --module(client). +-module(socket_client). -export([start/1]). @@ -62,6 +62,7 @@ connect(Sock, Domain, Port) -> case socket:connect(Sock, SA) of ok -> i("connected"), + send_loop(Sock), ok; {error, Reason} -> e("connect failure: " @@ -70,6 +71,26 @@ connect(Sock, Domain, Port) -> end. +send_loop(Sock) -> + send_loop(Sock, 1). + +send_loop(Sock, N) -> + case socket:send(Sock, <<0:32, N:32, "hejsan">>) of + ok -> + case send:recv(Sock, 0) of + {ok, <<1:32, N:32, "hejsan">>} -> + send_loop(Sock, N+1); + {error, RReason} -> + e("Failed recv response for request ~w: " + "~n ~p", [RReason]), + exit({failed_recv, RReason}) + end; + {error, SReason} -> + e("Failed send request ~w: " + "~n ~p", [SReason]), + exit({failed_send, SReason}) + end. + which_addr(_Domain, []) -> throw(no_address); which_addr(Domain, [{Name, IFO}|_IFL]) when (Name =/= "lo") -> diff --git a/lib/kernel/test/socket_server.erl b/lib/kernel/test/socket_server.erl index 039998ba92..0effc7c0ff 100644 --- a/lib/kernel/test/socket_server.erl +++ b/lib/kernel/test/socket_server.erl @@ -6,10 +6,12 @@ %%% @end %%% Created : 27 Jun 2018 by Micael Karlberg <[email protected]> %%%------------------------------------------------------------------- --module(server). +-module(socket_server). -export([start/0]). +-record(handler, {socket, parent}). + start() -> start_tcp(). @@ -17,6 +19,7 @@ start_tcp() -> start(inet, stream, tcp). start(Domain, Type, Proto) -> + put(sname, "starter"), try do_init(Domain, Type, Proto) of Sock -> accept_loop(Sock) @@ -79,14 +82,23 @@ which_addr2(Domain, [_|IFO]) -> accept_loop(LSock) -> + put(sname, "accept-loop"), accept_loop(LSock, []). -accept_loop(LSock, Socks) -> +accept_loop(LSock, Handlers) -> i("try accept"), case socket:accept(LSock, infinity) of {ok, Sock} -> i("accepted: ~p", [Sock]), - accept_loop(LSock, [Sock|Socks]); + case handle_accept_success(Sock) of + {ok, Handler} -> + accept_loop(LSock, [Handler|Handlers]); + {error, HReason} -> + e("Failed starting handler: " + "~n ~p", [HReason]), + socket:close(Sock), + exit({failed_starting_handler, HReason}) + end; {error, Reason} -> e("accept failure: " "~n ~p", [Reason]), @@ -94,6 +106,56 @@ accept_loop(LSock, Socks) -> end. +handle_accept_success(Sock) -> + Self = self(), + Handler = spawn_link(fun() -> handler_init(Self, Sock) end), + case socket:setopt(Sock, otp, controlling_process, Handler) of + ok -> + %% Normally we should have a msgs collection here + %% (of messages we receive before the control was + %% handled over to Handler), but since we don't + %% have active implemented yet... + handler_continue(Handler), + {ok, {Handler, Sock}}; + {error, _} = ERROR -> + exit(Handler, kill), + ERROR + end. + + +handler_init(Parent, Socket) -> + put(sname, "handler"), + receive + {handler, Parent, continue} -> + handler_loop(#handler{parent = Parent, + socket = Socket}) + end. + +handler_continue(Handler) -> + Handler ! {handler, self(), continue}. + +handler_loop(#handler{socket = Socket} = H) -> + case socket:read(Socket, 0) of + {ok, <<0:32, N:32, ReqData/binary>>} -> + i("received request ~w: " + "~n ~p", [N, ReqData]), + Reply = <<1:32, N:32, ReqData/binary>>, + case socket:send(Socket, Reply) of + ok -> + i("successfully sent reply ~w", [N]), + handler_loop(H); + {error, SReason} -> + e("failed sending reply ~w:" + "~n ~p", [N, SReason]), + exit({failed_sending_reply, SReason}) + end; + {error, RReason} -> + e("failed reading request: " + "~n ~p", [RReason]), + exit({failed_sending_reply, RReason}) + end. + + e(F, A) -> p("<ERROR> " ++ F, A). @@ -103,5 +165,8 @@ i(F, A) -> p("*** " ++ F, A). p(F, A) -> - io:format("[server] " ++ F ++ "~n", A). + p(get(sname), F, A). + +p(SName, F, A) -> + io:format("[server,~s] " ++ F ++ "~n", [SName|A]). |