diff --git a/Makefile b/Makefile index ba657fabd..76e3802bd 100644 --- a/Makefile +++ b/Makefile @@ -31,6 +31,10 @@ get-dashboard: eunit: $(REBAR) $(REBAR) eunit +.PHONY: proper +proper: $(REBAR) + $(REBAR) as test proper -d test/props -c + .PHONY: ct ct: $(REBAR) $(REBAR) ct --name 'test@127.0.0.1' -c -v @@ -40,7 +44,7 @@ cover: $(REBAR) $(REBAR) cover .PHONY: coveralls -cover: $(REBAR) +coveralls: $(REBAR) $(REBAR) as test coveralls send .PHONY: $(REL_PROFILES) diff --git a/apps/emqx_web_hook/test/props/prop_webhook_hooks.erl b/apps/emqx_web_hook/test/props/prop_webhook_hooks.erl index 650e02842..4e51573a6 100644 --- a/apps/emqx_web_hook/test/props/prop_webhook_hooks.erl +++ b/apps/emqx_web_hook/test/props/prop_webhook_hooks.erl @@ -290,58 +290,22 @@ prop_message_acked() -> true end). -prop_try_again() -> - Setup = fun() -> - logger:set_module_level(emqx_web_hook, emergency), - meck:new(httpc, [passthrough, no_history]), - meck:expect(httpc, request, - fun(Method, {Url, [], ContentType, Body}, _HttpOpts, _Opt) -> - self() ! {Method, Url, ContentType, Body}, {error, get(code)} - end), - meck:new(emqx_metrics, [passthrough, no_history]), - meck:expect(emqx_metrics, inc, fun(_) -> ok end) - end, - Teardown = fun() -> - meck:unload(httpc), - meck:unload(emqx_metrics), - logger:set_module_level(emqx_web_hook, debug) - end, - ?SETUP(fun() -> Setup(), Teardown end, - ?FORALL({ConnInfo, ConnProps, Env, Code}, - {conninfo(), conn_properties(), empty_env(), http_code()}, - begin - %% pre-set error code - put(code, Code), - %% run hook - ok = emqx_web_hook:on_client_connect(ConnInfo, ConnProps, Env), - - Bodys = receive_http_request_bodys(), - Body = emqx_json:encode( - #{action => client_connect, - node => stringfy(node()), - clientid => maps:get(clientid, ConnInfo), - username => maybe(maps:get(username, ConnInfo)), - ipaddress => peer2addr(maps:get(peername, ConnInfo)), - keepalive => maps:get(keepalive, ConnInfo), - proto_ver => maps:get(proto_ver, ConnInfo) - }), - [ B = Body || B <- Bodys], - if Code == socket_closed_remotely -> - 4 = length(Bodys); - true -> ok - end, - true - end)). - %%-------------------------------------------------------------------- %% Helper %%-------------------------------------------------------------------- do_setup() -> + %% Pre-defined envs + application:set_env(emqx_web_hook, path, "path"), + application:set_env(emqx_web_hook, headers, []), + + meck:new(ehttpc_pool, [passthrough, no_history]), + meck:expect(ehttpc_pool, pick_worker, fun(_, _) -> ok end), + Self = self(), - meck:new(httpc, [passthrough, no_history]), - meck:expect(httpc, request, - fun(Method, {Url, [], ContentType, Body}, _HttpOpts, _Opt) -> - Self ! {Method, Url, ContentType, Body}, {ok, ok} + meck:new(ehttpc, [passthrough, no_history]), + meck:expect(ehttpc, request, + fun(_ClientId, Method, {Path, Headers, Body}) -> + Self ! {Method, Path, Headers, Body}, {ok, ok, ok} end), meck:new(emqx_metrics, [passthrough, no_history]), @@ -349,7 +313,8 @@ do_setup() -> ok. do_teardown(_) -> - meck:unload(httpc), + meck:unload(ehttpc_pool), + meck:unload(ehttpc), meck:unload(emqx_metrics). maybe(undefined) -> null; @@ -372,7 +337,7 @@ stringfy(Term) -> receive_http_request_body() -> receive - {post, "http://127.0.0.1", "application/json", Body} -> + {post, _, _, Body} -> Body after 100 -> exit(waiting_message_timeout) @@ -383,7 +348,7 @@ receive_http_request_bodys() -> receive_http_request_bodys_(Acc) -> receive - {post, "http://127.0.0.1", "application/json", Body} -> + {post, _, _, Body} -> receive_http_request_bodys_([Body|Acc]) after 1000 -> lists:reverse(Acc) diff --git a/rebar.config.erl b/rebar.config.erl index 3e240b2c3..90caf5c60 100644 --- a/rebar.config.erl +++ b/rebar.config.erl @@ -28,7 +28,8 @@ plugins() -> ]. test_plugins() -> - [ {coveralls, {git, "https://github.com/emqx/coveralls-erl", {branch, "github"}}} + [ rebar3_proper, + {coveralls, {git, "https://github.com/emqx/coveralls-erl", {branch, "github"}}} ]. test_deps() ->