test(emqx_resource): improve emqx_resource test coverage to 80%

This commit is contained in:
EMQ-YangM 2022-01-25 17:59:29 +08:00
parent d312f315ac
commit 8cfbdc2730
2 changed files with 32 additions and 1 deletions

View File

@ -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])),

View File

@ -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