fix(message_validation): take `enable` into account

This commit is contained in:
Thales Macedo Garitezi 2024-03-20 13:57:12 -03:00
parent 7aa287c6c1
commit e767f01e0a
2 changed files with 40 additions and 2 deletions

View File

@ -153,24 +153,26 @@ do_reindex_positions(Validations) ->
do_insert(Pos, Validation) ->
#{
enable := Enabled,
name := Name,
topics := Topics
} = Validation,
maybe_create_metrics(Name),
do_insert_into_tab(Name, Validation, Pos),
update_topic_index(Name, Pos, Topics),
Enabled andalso update_topic_index(Name, Pos, Topics),
ok.
do_update(OldValidation, Pos, NewValidation) ->
#{topics := OldTopics} = OldValidation,
#{
enable := Enabled,
name := Name,
topics := NewTopics
} = NewValidation,
maybe_create_metrics(Name),
do_insert_into_tab(Name, NewValidation, Pos),
delete_topic_index(Name, OldTopics),
update_topic_index(Name, Pos, NewTopics),
Enabled andalso update_topic_index(Name, Pos, NewTopics),
ok.
do_delete(Validation) ->

View File

@ -508,6 +508,42 @@ t_reorder(_Config) ->
ok.
t_enable_disable_via_update(_Config) ->
Topic = <<"t">>,
Name1 = <<"foo">>,
AlwaysFailCheck = sql_check(<<"select * where false">>),
Validation1 = validation(Name1, [AlwaysFailCheck], #{<<"topics">> => Topic}),
{201, _} = insert(Validation1#{<<"enable">> => false}),
?assertIndexOrder([], Topic),
C = connect(<<"c1">>),
{ok, _, [_]} = emqtt:subscribe(C, Topic),
ok = publish(C, Topic, #{}),
?assertReceive({publish, _}),
{200, _} = update(Validation1#{<<"enable">> => true}),
?assertIndexOrder([Name1], Topic),
ok = publish(C, Topic, #{}),
?assertNotReceive({publish, _}),
{200, _} = update(Validation1#{<<"enable">> => false}),
?assertIndexOrder([], Topic),
ok = publish(C, Topic, #{}),
?assertReceive({publish, _}),
%% Test index after delete; ensure it's in the index before
{200, _} = update(Validation1#{<<"enable">> => true}),
?assertIndexOrder([Name1], Topic),
{204, _} = delete(Name1),
?assertIndexOrder([], Topic),
ok.
%% Check the `all_pass' strategy
t_all_pass(_Config) ->
Name1 = <<"foo">>,