From 8cfbdc2730d930835d40183e125449f5f120b824 Mon Sep 17 00:00:00 2001 From: EMQ-YangM Date: Tue, 25 Jan 2022 17:59:29 +0800 Subject: [PATCH] test(emqx_resource): improve emqx_resource test coverage to 80% --- .../emqx_resource/test/emqx_resource_SUITE.erl | 15 ++++++++++++++- apps/emqx_resource/test/emqx_test_resource.erl | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/apps/emqx_resource/test/emqx_resource_SUITE.erl b/apps/emqx_resource/test/emqx_resource_SUITE.erl index 3a352d252..41945f295 100644 --- a/apps/emqx_resource/test/emqx_resource_SUITE.erl +++ b/apps/emqx_resource/test/emqx_resource_SUITE.erl @@ -156,7 +156,7 @@ t_healthy(_) -> ?TEST_RESOURCE, #{name => <<"test_resource">>}, #{async_create => true}), - timer:sleep(300), + timer:sleep(400), emqx_resource_health_check:create_checker(?ID, 15000, 10000), #{pid := Pid} = emqx_resource:query(?ID, get_state), @@ -278,6 +278,19 @@ t_create_dry_run_local(_) -> ?assertEqual(undefined, whereis(test_resource)). +t_create_dry_run_local_failed(_) -> + {Res, _} = emqx_resource:create_dry_run_local(?TEST_RESOURCE, + #{cteate_error => true}), + ?assertEqual(error, Res), + + {Res, _} = emqx_resource:create_dry_run_local(?TEST_RESOURCE, + #{name => test_resource, health_check_error => true}), + ?assertEqual(error, Res), + + {Res, _} = emqx_resource:create_dry_run_local(?TEST_RESOURCE, + #{name => test_resource, stop_error => true}), + ?assertEqual(error, Res). + t_test_func(_) -> ?assertEqual(ok, erlang:apply(emqx_resource_validator:not_empty("not_empty"), [<<"someval">>])), ?assertEqual(ok, erlang:apply(emqx_resource_validator:min(int, 3), [4])), diff --git a/apps/emqx_resource/test/emqx_test_resource.erl b/apps/emqx_resource/test/emqx_test_resource.erl index f185ded5b..1849e329b 100644 --- a/apps/emqx_resource/test/emqx_test_resource.erl +++ b/apps/emqx_resource/test/emqx_test_resource.erl @@ -43,12 +43,28 @@ register(nullable) -> false; register(default) -> false; register(_) -> undefined. +on_start(_InstId, #{create_error := true}) -> + error("some error"); +on_start(InstId, #{name := Name, stop_error := true} = Opts) -> + Register = maps:get(register, Opts, false), + {ok, #{name => Name, + id => InstId, + stop_error => true, + pid => spawn_dummy_process(Name, Register)}}; +on_start(InstId, #{name := Name, health_check_error := true} = Opts) -> + Register = maps:get(register, Opts, false), + {ok, #{name => Name, + id => InstId, + health_check_error => true, + pid => spawn_dummy_process(Name, Register)}}; on_start(InstId, #{name := Name} = Opts) -> Register = maps:get(register, Opts, false), {ok, #{name => Name, id => InstId, pid => spawn_dummy_process(Name, Register)}}. +on_stop(_InstId, #{stop_error := true}) -> + {error, stop_error}; on_stop(_InstId, #{pid := Pid}) -> erlang:exit(Pid, shutdown), ok. @@ -60,6 +76,8 @@ on_query(_InstId, get_state_failed, AfterQuery, State) -> emqx_resource:query_failed(AfterQuery), State. +on_health_check(_InstId, State = #{health_check_error := true}) -> + {error, dead, State}; on_health_check(_InstId, State = #{pid := Pid}) -> timer:sleep(300), case is_process_alive(Pid) of