Просмотр исходного кода

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

fix(retainer): Handle zero-length topic levels
ieQu1 3 лет назад
Родитель
Сommit
48e2f0969a

+ 2 - 2
apps/emqx_retainer/src/emqx_retainer_index.erl

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

+ 9 - 1
apps/emqx_retainer/test/emqx_retainer_SUITE.erl

@@ -220,14 +220,22 @@ t_wildcard_subscription(_) ->
         <<"this is a retained message 2">>,
         [{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/+/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/1">>, <<"">>, [{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).
 
 t_message_expiry(_) ->

+ 8 - 0
apps/emqx_retainer/test/emqx_retainer_index_SUITE.erl

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