|
|
@@ -65,7 +65,8 @@ groups() ->
|
|
|
t_sqlselect_1,
|
|
|
t_sqlselect_2,
|
|
|
t_sqlselect_3,
|
|
|
- t_sqlselect_message_publish_event,
|
|
|
+ t_sqlselect_message_publish_event_keep_original_props_1,
|
|
|
+ t_sqlselect_message_publish_event_keep_original_props_2,
|
|
|
t_sqlparse_event_1,
|
|
|
t_sqlparse_event_2,
|
|
|
t_sqlparse_event_3,
|
|
|
@@ -941,8 +942,7 @@ t_sqlselect_001(_Config) ->
|
|
|
t_sqlselect_inject_props(_Config) ->
|
|
|
SQL =
|
|
|
"SELECT json_decode(payload) as p, payload, "
|
|
|
- "map_put('discard', 'discard', pub_props) as pub_props, "
|
|
|
- "map_put('inject_key', 'inject_val', pub_props.'User-Property') as pub_props.'User-Property' "
|
|
|
+ "map_put('inject_key', 'inject_val', user_properties) as user_properties "
|
|
|
"FROM \"t3/#\", \"t1\" "
|
|
|
"WHERE p.x = 1",
|
|
|
Repub = republish_action(<<"t2">>),
|
|
|
@@ -958,13 +958,12 @@ t_sqlselect_inject_props(_Config) ->
|
|
|
{ok, _} = emqtt:connect(Client),
|
|
|
{ok, _, _} = emqtt:subscribe(Client, <<"t2">>, 0),
|
|
|
emqtt:publish(Client, <<"t1">>, #{}, <<"{\"x\":1}">>, [{qos, 0}]),
|
|
|
- ct:sleep(100),
|
|
|
receive
|
|
|
{publish, #{topic := T, payload := Payload, properties := Props2}} ->
|
|
|
?assertEqual(Props, Props2),
|
|
|
?assertEqual(<<"t2">>, T),
|
|
|
?assertEqual(<<"{\"x\":1}">>, Payload)
|
|
|
- after 1000 ->
|
|
|
+ after 2000 ->
|
|
|
ct:fail(wait_for_t2)
|
|
|
end,
|
|
|
emqtt:stop(Client),
|
|
|
@@ -972,10 +971,10 @@ t_sqlselect_inject_props(_Config) ->
|
|
|
|
|
|
t_sqlselect_01(_Config) ->
|
|
|
SQL =
|
|
|
- "SELECT json_decode(payload) as p, payload, pub_props "
|
|
|
+ "SELECT json_decode(payload) as p, payload "
|
|
|
"FROM \"t3/#\", \"t1\" "
|
|
|
"WHERE p.x = 1",
|
|
|
- Repub = republish_action(<<"t2">>),
|
|
|
+ Repub = republish_action(<<"t2">>, <<"${payload}">>, <<"${pub_props.'User-Property'}">>),
|
|
|
{ok, TopicRule1} = emqx_rule_engine:create_rule(
|
|
|
#{
|
|
|
sql => SQL,
|
|
|
@@ -988,12 +987,11 @@ t_sqlselect_01(_Config) ->
|
|
|
{ok, _} = emqtt:connect(Client),
|
|
|
{ok, _, _} = emqtt:subscribe(Client, <<"t2">>, 0),
|
|
|
emqtt:publish(Client, <<"t1">>, Props, <<"{\"x\":1}">>, [{qos, 0}]),
|
|
|
- ct:sleep(100),
|
|
|
receive
|
|
|
{publish, #{topic := T, payload := Payload}} ->
|
|
|
?assertEqual(<<"t2">>, T),
|
|
|
?assertEqual(<<"{\"x\":1}">>, Payload)
|
|
|
- after 1000 ->
|
|
|
+ after 2000 ->
|
|
|
ct:fail(wait_for_t2)
|
|
|
end,
|
|
|
|
|
|
@@ -1001,7 +999,7 @@ t_sqlselect_01(_Config) ->
|
|
|
receive
|
|
|
{publish, #{topic := <<"t2">>, payload := _}} ->
|
|
|
ct:fail(unexpected_t2)
|
|
|
- after 1000 ->
|
|
|
+ after 2000 ->
|
|
|
ok
|
|
|
end,
|
|
|
|
|
|
@@ -1011,8 +1009,8 @@ t_sqlselect_01(_Config) ->
|
|
|
?assertEqual(Props, Props2),
|
|
|
?assertEqual(<<"t2">>, T3),
|
|
|
?assertEqual(<<"{\"x\":1}">>, Payload3)
|
|
|
- after 1000 ->
|
|
|
- ct:fail(wait_for_t2)
|
|
|
+ after 2000 ->
|
|
|
+ ct:fail(wait_for_t3)
|
|
|
end,
|
|
|
|
|
|
emqtt:stop(Client),
|
|
|
@@ -1080,13 +1078,12 @@ t_sqlselect_1(_Config) ->
|
|
|
{ok, Client} = emqtt:start_link([{username, <<"emqx">>}]),
|
|
|
{ok, _} = emqtt:connect(Client),
|
|
|
{ok, _, _} = emqtt:subscribe(Client, <<"t2">>, 0),
|
|
|
- ct:sleep(200),
|
|
|
emqtt:publish(Client, <<"t1">>, <<"{\"x\":1,\"y\":2}">>, 0),
|
|
|
receive
|
|
|
{publish, #{topic := T, payload := Payload}} ->
|
|
|
?assertEqual(<<"t2">>, T),
|
|
|
?assertEqual(<<"{\"x\":1,\"y\":2}">>, Payload)
|
|
|
- after 1000 ->
|
|
|
+ after 2000 ->
|
|
|
ct:fail(wait_for_t2)
|
|
|
end,
|
|
|
|
|
|
@@ -1149,14 +1146,13 @@ t_sqlselect_3(_Config) ->
|
|
|
{ok, Client} = emqtt:start_link([{clientid, <<"emqx0">>}, {username, <<"emqx0">>}]),
|
|
|
{ok, _} = emqtt:connect(Client),
|
|
|
{ok, _, _} = emqtt:subscribe(Client, <<"t2">>, 0),
|
|
|
- ct:sleep(200),
|
|
|
{ok, Client1} = emqtt:start_link([{clientid, <<"c_emqx1">>}, {username, <<"emqx1">>}]),
|
|
|
{ok, _} = emqtt:connect(Client1),
|
|
|
receive
|
|
|
{publish, #{topic := T, payload := Payload}} ->
|
|
|
?assertEqual(<<"t2">>, T),
|
|
|
?assertEqual(<<"clientid=c_emqx1">>, Payload)
|
|
|
- after 1000 ->
|
|
|
+ after 2000 ->
|
|
|
ct:fail(wait_for_t2)
|
|
|
end,
|
|
|
|
|
|
@@ -1171,11 +1167,51 @@ t_sqlselect_3(_Config) ->
|
|
|
emqtt:stop(Client),
|
|
|
delete_rule(TopicRule).
|
|
|
|
|
|
-t_sqlselect_message_publish_event(_Config) ->
|
|
|
+t_sqlselect_message_publish_event_keep_original_props_1(_Config) ->
|
|
|
+ %% republish the client.connected msg
|
|
|
+ Topic = <<"foo/bar/1">>,
|
|
|
+ SQL = <<
|
|
|
+ "SELECT clientid "
|
|
|
+ "FROM \"$events/message_dropped\" "
|
|
|
+ >>,
|
|
|
+
|
|
|
+ %"WHERE topic = \"", Topic/binary, "\"">>,
|
|
|
+ Repub = republish_action(
|
|
|
+ <<"t2">>,
|
|
|
+ <<"clientid=${clientid}">>,
|
|
|
+ <<"${pub_props.'User-Property'}">>
|
|
|
+ ),
|
|
|
+ {ok, TopicRule} = emqx_rule_engine:create_rule(
|
|
|
+ #{
|
|
|
+ sql => SQL,
|
|
|
+ id => ?TMP_RULEID,
|
|
|
+ actions => [Repub]
|
|
|
+ }
|
|
|
+ ),
|
|
|
+ {ok, Client1} = emqtt:start_link([{clientid, <<"sub-01">>}, {proto_ver, v5}]),
|
|
|
+ {ok, _} = emqtt:connect(Client1),
|
|
|
+ {ok, _, _} = emqtt:subscribe(Client1, <<"t2">>, 1),
|
|
|
+ {ok, Client2} = emqtt:start_link([{clientid, <<"pub-02">>}, {proto_ver, v5}]),
|
|
|
+ {ok, _} = emqtt:connect(Client2),
|
|
|
+ Props = user_properties(#{<<"mykey">> => <<"111111">>}),
|
|
|
+ emqtt:publish(Client2, Topic, Props, <<"{\"x\":1}">>, [{qos, 1}]),
|
|
|
+ receive
|
|
|
+ {publish, #{topic := T, payload := Payload, properties := Props1}} ->
|
|
|
+ ?assertEqual(Props1, Props),
|
|
|
+ ?assertEqual(<<"t2">>, T),
|
|
|
+ ?assertEqual(<<"clientid=pub-02">>, Payload)
|
|
|
+ after 2000 ->
|
|
|
+ ct:fail(wait_for_t2)
|
|
|
+ end,
|
|
|
+ emqtt:stop(Client2),
|
|
|
+ emqtt:stop(Client1),
|
|
|
+ delete_rule(TopicRule).
|
|
|
+
|
|
|
+t_sqlselect_message_publish_event_keep_original_props_2(_Config) ->
|
|
|
%% republish the client.connected msg
|
|
|
Topic = <<"foo/bar/1">>,
|
|
|
SQL = <<
|
|
|
- "SELECT clientid, pub_props "
|
|
|
+ "SELECT clientid, pub_props.'User-Property' as user_properties "
|
|
|
"FROM \"$events/message_dropped\" "
|
|
|
>>,
|
|
|
|
|
|
@@ -1191,7 +1227,6 @@ t_sqlselect_message_publish_event(_Config) ->
|
|
|
{ok, Client1} = emqtt:start_link([{clientid, <<"sub-01">>}, {proto_ver, v5}]),
|
|
|
{ok, _} = emqtt:connect(Client1),
|
|
|
{ok, _, _} = emqtt:subscribe(Client1, <<"t2">>, 1),
|
|
|
- ct:sleep(200),
|
|
|
{ok, Client2} = emqtt:start_link([{clientid, <<"pub-02">>}, {proto_ver, v5}]),
|
|
|
{ok, _} = emqtt:connect(Client2),
|
|
|
Props = user_properties(#{<<"mykey">> => <<"222222222222">>}),
|
|
|
@@ -1201,7 +1236,7 @@ t_sqlselect_message_publish_event(_Config) ->
|
|
|
?assertEqual(Props1, Props),
|
|
|
?assertEqual(<<"t2">>, T),
|
|
|
?assertEqual(<<"clientid=pub-02">>, Payload)
|
|
|
- after 1000 ->
|
|
|
+ after 2000 ->
|
|
|
ct:fail(wait_for_t2)
|
|
|
end,
|
|
|
emqtt:stop(Client2),
|
|
|
@@ -2553,10 +2588,20 @@ t_get_basic_usage_info_1(_Config) ->
|
|
|
|
|
|
republish_action(Topic) ->
|
|
|
republish_action(Topic, <<"${payload}">>).
|
|
|
+
|
|
|
republish_action(Topic, Payload) ->
|
|
|
+ republish_action(Topic, Payload, <<"${user_properties}">>).
|
|
|
+
|
|
|
+republish_action(Topic, Payload, UserProperties) ->
|
|
|
#{
|
|
|
function => republish,
|
|
|
- args => #{payload => Payload, topic => Topic, qos => 0, retain => false}
|
|
|
+ args => #{
|
|
|
+ payload => Payload,
|
|
|
+ topic => Topic,
|
|
|
+ qos => 0,
|
|
|
+ retain => false,
|
|
|
+ user_properties => UserProperties
|
|
|
+ }
|
|
|
}.
|
|
|
|
|
|
make_simple_rule_with_ts(RuleId, Ts) when is_binary(RuleId) ->
|