aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorViktor Söderqvist <[email protected]>2024-01-12 16:33:18 +0100
committerLoïc Hoguin <[email protected]>2025-02-26 14:43:45 +0100
commitd9f9d4dc30cf6bbd6f5fc2b8a8bb6d895e4c209d (patch)
tree707c21e1b83a7904dfac979762cf3c3eae5469c9 /test
parent85fe0a9e315d7af90d6089d0daf21a9bc6a10c0a (diff)
downloadgun-d9f9d4dc30cf6bbd6f5fc2b8a8bb6d895e4c209d.tar.gz
gun-d9f9d4dc30cf6bbd6f5fc2b8a8bb6d895e4c209d.tar.bz2
gun-d9f9d4dc30cf6bbd6f5fc2b8a8bb6d895e4c209d.zip
Allow specifying functions for reply_to
LH: I have fixed types, extended tests and tweaked a bit.
Diffstat (limited to 'test')
-rw-r--r--test/gun_SUITE.erl47
1 files changed, 37 insertions, 10 deletions
diff --git a/test/gun_SUITE.erl b/test/gun_SUITE.erl
index 230bd99..98799c9 100644
--- a/test/gun_SUITE.erl
+++ b/test/gun_SUITE.erl
@@ -264,13 +264,25 @@ postpone_request_while_not_connected(_) ->
reply_to_http(_) ->
doc("The reply_to option allows using a separate process for requests."),
- do_reply_to(http).
+ do_reply_to(http, pid).
reply_to_http2(_) ->
doc("The reply_to option allows using a separate process for requests."),
- do_reply_to(http2).
+ do_reply_to(http2, pid).
-do_reply_to(Protocol) ->
+reply_to_http_f(_) ->
+ doc("The reply_to option allows using a fun for requests."),
+ do_reply_to(http, f).
+
+reply_to_http_fa(_) ->
+ doc("The reply_to option allows using a fun/args tuple for requests."),
+ do_reply_to(http, fa).
+
+reply_to_http_mfa(_) ->
+ doc("The reply_to option allows using an MFA tuple for requests."),
+ do_reply_to(http, mfa).
+
+do_reply_to(Protocol, ReplyToType) ->
{ok, OriginPid, OriginPort} = init_origin(tcp, Protocol,
fun(_, _, ClientSocket, ClientTransport) ->
{ok, _} = ClientTransport:recv(ClientSocket, 0, infinity),
@@ -302,23 +314,38 @@ do_reply_to(Protocol) ->
ok = ClientTransport:send(ClientSocket, ResponseData),
timer:sleep(1000)
end),
- {ok, Pid} = gun:open("localhost", OriginPort, #{protocols => [Protocol]}),
- {ok, Protocol} = gun:await_up(Pid),
+ {ok, GunPid} = gun:open("localhost", OriginPort, #{protocols => [Protocol]}),
+ {ok, Protocol} = gun:await_up(GunPid),
handshake_completed = receive_from(OriginPid),
+ do_reply_to_req(GunPid, ReplyToType),
+ gun:close(GunPid).
+
+do_reply_to_req(GunPid, pid) ->
Self = self(),
ReplyTo = spawn(fun() ->
receive Ref when is_reference(Ref) ->
- Response = gun:await(Pid, Ref, infinity),
+ Response = gun:await(GunPid, Ref, infinity),
Self ! Response
end
end),
- Ref = gun:get(Pid, "/", [], #{reply_to => ReplyTo}),
+ Ref = gun:get(GunPid, "/", [], #{reply_to => ReplyTo}),
ReplyTo ! Ref,
receive
Msg ->
- {response, _, _, _} = Msg,
- gun:close(Pid)
- end.
+ {response, _, _, _} = Msg
+ end;
+do_reply_to_req(GunPid, ReplyToType) ->
+ ReplyTo = case ReplyToType of
+ f -> Self = self(), fun(Reply) -> Self ! Reply end;
+ fa -> {fun do_reply_to_fun/2, [self()]};
+ mfa -> {?MODULE, do_reply_to_fun, [self()]}
+ end,
+ Ref = gun:get(GunPid, "/", [], #{reply_to => ReplyTo}),
+ {response, _, _, _} = gun:await(GunPid, Ref, infinity),
+ ok.
+
+do_reply_to_fun(Reply, DstPid) ->
+ DstPid ! Reply.
retry_0(_) ->
doc("Ensure Gun gives up immediately with retry=0."),