test(emqx_resource): improve emqx_resource test coverage to 80%
This commit is contained in:
parent
d312f315ac
commit
8cfbdc2730
|
@ -156,7 +156,7 @@ t_healthy(_) ->
|
||||||
?TEST_RESOURCE,
|
?TEST_RESOURCE,
|
||||||
#{name => <<"test_resource">>},
|
#{name => <<"test_resource">>},
|
||||||
#{async_create => true}),
|
#{async_create => true}),
|
||||||
timer:sleep(300),
|
timer:sleep(400),
|
||||||
|
|
||||||
emqx_resource_health_check:create_checker(?ID, 15000, 10000),
|
emqx_resource_health_check:create_checker(?ID, 15000, 10000),
|
||||||
#{pid := Pid} = emqx_resource:query(?ID, get_state),
|
#{pid := Pid} = emqx_resource:query(?ID, get_state),
|
||||||
|
@ -278,6 +278,19 @@ t_create_dry_run_local(_) ->
|
||||||
|
|
||||||
?assertEqual(undefined, whereis(test_resource)).
|
?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(_) ->
|
t_test_func(_) ->
|
||||||
?assertEqual(ok, erlang:apply(emqx_resource_validator:not_empty("not_empty"), [<<"someval">>])),
|
?assertEqual(ok, erlang:apply(emqx_resource_validator:not_empty("not_empty"), [<<"someval">>])),
|
||||||
?assertEqual(ok, erlang:apply(emqx_resource_validator:min(int, 3), [4])),
|
?assertEqual(ok, erlang:apply(emqx_resource_validator:min(int, 3), [4])),
|
||||||
|
|
|
@ -43,12 +43,28 @@ register(nullable) -> false;
|
||||||
register(default) -> false;
|
register(default) -> false;
|
||||||
register(_) -> undefined.
|
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) ->
|
on_start(InstId, #{name := Name} = Opts) ->
|
||||||
Register = maps:get(register, Opts, false),
|
Register = maps:get(register, Opts, false),
|
||||||
{ok, #{name => Name,
|
{ok, #{name => Name,
|
||||||
id => InstId,
|
id => InstId,
|
||||||
pid => spawn_dummy_process(Name, Register)}}.
|
pid => spawn_dummy_process(Name, Register)}}.
|
||||||
|
|
||||||
|
on_stop(_InstId, #{stop_error := true}) ->
|
||||||
|
{error, stop_error};
|
||||||
on_stop(_InstId, #{pid := Pid}) ->
|
on_stop(_InstId, #{pid := Pid}) ->
|
||||||
erlang:exit(Pid, shutdown),
|
erlang:exit(Pid, shutdown),
|
||||||
ok.
|
ok.
|
||||||
|
@ -60,6 +76,8 @@ on_query(_InstId, get_state_failed, AfterQuery, State) ->
|
||||||
emqx_resource:query_failed(AfterQuery),
|
emqx_resource:query_failed(AfterQuery),
|
||||||
State.
|
State.
|
||||||
|
|
||||||
|
on_health_check(_InstId, State = #{health_check_error := true}) ->
|
||||||
|
{error, dead, State};
|
||||||
on_health_check(_InstId, State = #{pid := Pid}) ->
|
on_health_check(_InstId, State = #{pid := Pid}) ->
|
||||||
timer:sleep(300),
|
timer:sleep(300),
|
||||||
case is_process_alive(Pid) of
|
case is_process_alive(Pid) of
|
||||||
|
|
Loading…
Reference in New Issue