|
|
@@ -59,11 +59,13 @@ groups() ->
|
|
|
t_sqlselect_0,
|
|
|
t_sqlselect_00,
|
|
|
t_sqlselect_001,
|
|
|
+ t_sqlselect_inject_props,
|
|
|
t_sqlselect_01,
|
|
|
t_sqlselect_02,
|
|
|
t_sqlselect_1,
|
|
|
t_sqlselect_2,
|
|
|
t_sqlselect_3,
|
|
|
+ t_sqlselect_message_publish_event,
|
|
|
t_sqlparse_event_1,
|
|
|
t_sqlparse_event_2,
|
|
|
t_sqlparse_event_3,
|
|
|
@@ -936,9 +938,41 @@ 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' "
|
|
|
+ "FROM \"t3/#\", \"t1\" "
|
|
|
+ "WHERE p.x = 1",
|
|
|
+ Repub = republish_action(<<"t2">>),
|
|
|
+ {ok, TopicRule1} = emqx_rule_engine:create_rule(
|
|
|
+ #{
|
|
|
+ sql => SQL,
|
|
|
+ id => ?TMP_RULEID,
|
|
|
+ actions => [Repub]
|
|
|
+ }
|
|
|
+ ),
|
|
|
+ Props = user_properties(#{<<"inject_key">> => <<"inject_val">>}),
|
|
|
+ {ok, Client} = emqtt:start_link([{username, <<"emqx">>}, {proto_ver, v5}]),
|
|
|
+ {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 ->
|
|
|
+ ct:fail(wait_for_t2)
|
|
|
+ end,
|
|
|
+ emqtt:stop(Client),
|
|
|
+ delete_rule(TopicRule1).
|
|
|
+
|
|
|
t_sqlselect_01(_Config) ->
|
|
|
SQL =
|
|
|
- "SELECT json_decode(payload) as p, payload "
|
|
|
+ "SELECT json_decode(payload) as p, payload, pub_props "
|
|
|
"FROM \"t3/#\", \"t1\" "
|
|
|
"WHERE p.x = 1",
|
|
|
Repub = republish_action(<<"t2">>),
|
|
|
@@ -949,10 +983,11 @@ t_sqlselect_01(_Config) ->
|
|
|
actions => [Repub]
|
|
|
}
|
|
|
),
|
|
|
- {ok, Client} = emqtt:start_link([{username, <<"emqx">>}]),
|
|
|
+ Props = user_properties(#{<<"mykey">> => <<"myval">>}),
|
|
|
+ {ok, Client} = emqtt:start_link([{username, <<"emqx">>}, {proto_ver, v5}]),
|
|
|
{ok, _} = emqtt:connect(Client),
|
|
|
{ok, _, _} = emqtt:subscribe(Client, <<"t2">>, 0),
|
|
|
- emqtt:publish(Client, <<"t1">>, <<"{\"x\":1}">>, 0),
|
|
|
+ emqtt:publish(Client, <<"t1">>, Props, <<"{\"x\":1}">>, [{qos, 0}]),
|
|
|
ct:sleep(100),
|
|
|
receive
|
|
|
{publish, #{topic := T, payload := Payload}} ->
|
|
|
@@ -962,7 +997,7 @@ t_sqlselect_01(_Config) ->
|
|
|
ct:fail(wait_for_t2)
|
|
|
end,
|
|
|
|
|
|
- emqtt:publish(Client, <<"t1">>, <<"{\"x\":2}">>, 0),
|
|
|
+ emqtt:publish(Client, <<"t1">>, Props, <<"{\"x\":2}">>, [{qos, 0}]),
|
|
|
receive
|
|
|
{publish, #{topic := <<"t2">>, payload := _}} ->
|
|
|
ct:fail(unexpected_t2)
|
|
|
@@ -970,9 +1005,10 @@ t_sqlselect_01(_Config) ->
|
|
|
ok
|
|
|
end,
|
|
|
|
|
|
- emqtt:publish(Client, <<"t3/a">>, <<"{\"x\":1}">>, 0),
|
|
|
+ emqtt:publish(Client, <<"t3/a">>, Props, <<"{\"x\":1}">>, [{qos, 0}]),
|
|
|
receive
|
|
|
- {publish, #{topic := T3, payload := Payload3}} ->
|
|
|
+ {publish, #{topic := T3, payload := Payload3, properties := Props2}} ->
|
|
|
+ ?assertEqual(Props, Props2),
|
|
|
?assertEqual(<<"t2">>, T3),
|
|
|
?assertEqual(<<"{\"x\":1}">>, Payload3)
|
|
|
after 1000 ->
|
|
|
@@ -1135,6 +1171,43 @@ t_sqlselect_3(_Config) ->
|
|
|
emqtt:stop(Client),
|
|
|
delete_rule(TopicRule).
|
|
|
|
|
|
+t_sqlselect_message_publish_event(_Config) ->
|
|
|
+ %% republish the client.connected msg
|
|
|
+ Topic = <<"foo/bar/1">>,
|
|
|
+ SQL = <<
|
|
|
+ "SELECT clientid, pub_props "
|
|
|
+ "FROM \"$events/message_dropped\" "
|
|
|
+ >>,
|
|
|
+
|
|
|
+ %"WHERE topic = \"", Topic/binary, "\"">>,
|
|
|
+ Repub = republish_action(<<"t2">>, <<"clientid=${clientid}">>),
|
|
|
+ {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),
|
|
|
+ ct:sleep(200),
|
|
|
+ {ok, Client2} = emqtt:start_link([{clientid, <<"pub-02">>}, {proto_ver, v5}]),
|
|
|
+ {ok, _} = emqtt:connect(Client2),
|
|
|
+ Props = user_properties(#{<<"mykey">> => <<"222222222222">>}),
|
|
|
+ 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 1000 ->
|
|
|
+ ct:fail(wait_for_t2)
|
|
|
+ end,
|
|
|
+ emqtt:stop(Client2),
|
|
|
+ emqtt:stop(Client1),
|
|
|
+ delete_rule(TopicRule).
|
|
|
+
|
|
|
t_sqlparse_event_1(_Config) ->
|
|
|
Sql =
|
|
|
"select topic as tp "
|
|
|
@@ -2869,6 +2942,9 @@ verify_ipaddr(IPAddrS) ->
|
|
|
init_events_counters() ->
|
|
|
ets:new(events_record_tab, [named_table, bag, public]).
|
|
|
|
|
|
+user_properties(PairsMap) ->
|
|
|
+ #{'User-Property' => maps:to_list(PairsMap)}.
|
|
|
+
|
|
|
%%------------------------------------------------------------------------------
|
|
|
%% Start Apps
|
|
|
%%------------------------------------------------------------------------------
|