Merge pull request #8169 from ieQu1/zero-length-retainer

fix(retainer): Handle zero-length topic levels
This commit is contained in:
ieQu1 2022-06-10 13:05:04 +02:00 committed by GitHub
commit 48e2f0969a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 3 deletions

View File

@ -176,11 +176,11 @@ condition([NIndex | OtherIndex], ['+' | OtherTokens], N, IndexMatch, OtherMatch)
condition(Index, ['+' | OtherTokens], N, IndexMatch, OtherMatch) -> condition(Index, ['+' | OtherTokens], N, IndexMatch, OtherMatch) ->
condition(Index, OtherTokens, N + 1, IndexMatch, ['_' | OtherMatch]); condition(Index, OtherTokens, N + 1, IndexMatch, ['_' | OtherMatch]);
condition([NIndex | OtherIndex], [Token | OtherTokens], N, IndexMatch, OtherMatch) when condition([NIndex | OtherIndex], [Token | OtherTokens], N, IndexMatch, OtherMatch) when
NIndex =:= N, is_binary(Token) NIndex =:= N, is_binary(Token) orelse Token =:= ''
-> ->
condition(OtherIndex, OtherTokens, N + 1, [Token | IndexMatch], OtherMatch); condition(OtherIndex, OtherTokens, N + 1, [Token | IndexMatch], OtherMatch);
condition(Index, [Token | OtherTokens], N, IndexMatch, OtherMatch) when condition(Index, [Token | OtherTokens], N, IndexMatch, OtherMatch) when
is_binary(Token) is_binary(Token) orelse Token =:= ''
-> ->
condition(Index, OtherTokens, N + 1, IndexMatch, [Token | OtherMatch]). condition(Index, OtherTokens, N + 1, IndexMatch, [Token | OtherMatch]).

View File

@ -220,14 +220,22 @@ t_wildcard_subscription(_) ->
<<"this is a retained message 2">>, <<"this is a retained message 2">>,
[{qos, 0}, {retain, true}] [{qos, 0}, {retain, true}]
), ),
emqtt:publish(
C1,
<<"/x/y/z">>,
<<"this is a retained message 3">>,
[{qos, 0}, {retain, true}]
),
{ok, #{}, [0]} = emqtt:subscribe(C1, <<"retained/+">>, 0), {ok, #{}, [0]} = emqtt:subscribe(C1, <<"retained/+">>, 0),
{ok, #{}, [0]} = emqtt:subscribe(C1, <<"retained/+/b/#">>, 0), {ok, #{}, [0]} = emqtt:subscribe(C1, <<"retained/+/b/#">>, 0),
?assertEqual(3, length(receive_messages(3))), {ok, #{}, [0]} = emqtt:subscribe(C1, <<"/+/y/#">>, 0),
?assertEqual(4, length(receive_messages(4))),
emqtt:publish(C1, <<"retained/0">>, <<"">>, [{qos, 0}, {retain, true}]), emqtt:publish(C1, <<"retained/0">>, <<"">>, [{qos, 0}, {retain, true}]),
emqtt:publish(C1, <<"retained/1">>, <<"">>, [{qos, 0}, {retain, true}]), emqtt:publish(C1, <<"retained/1">>, <<"">>, [{qos, 0}, {retain, true}]),
emqtt:publish(C1, <<"retained/a/b/c">>, <<"">>, [{qos, 0}, {retain, true}]), emqtt:publish(C1, <<"retained/a/b/c">>, <<"">>, [{qos, 0}, {retain, true}]),
emqtt:publish(C1, <<"/x/y/z">>, <<"">>, [{qos, 0}, {retain, true}]),
ok = emqtt:disconnect(C1). ok = emqtt:disconnect(C1).
t_message_expiry(_) -> t_message_expiry(_) ->

View File

@ -193,6 +193,14 @@ t_condition_index(_Config) ->
[1], [1],
[<<"a">>, '#'] [<<"a">>, '#']
) )
),
?assertEqual(
{[1, 2, 3], {['', <<"saya">>, '_'], []}},
emqx_retainer_index:condition(
[1, 2, 3],
['', <<"saya">>, '+']
)
). ).
t_restore_topic(_Config) -> t_restore_topic(_Config) ->