Skip to content

Commit bb41341

Browse files
committedMar 13, 2024
✅ [HDK-8201] Emqx: adding tests and formatting code.
- Adding extra tests and formatting code.
1 parent a4fdd96 commit bb41341

14 files changed

+851
-276
lines changed
 

‎Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ compile: $(REBAR)
2626
ct: $(REBAR)
2727
$(REBAR) as test ct -v
2828

29+
.PHONY: format
30+
format: $(REBAR)
31+
$(REBAR) format
32+
2933
.PHONY: eunit
3034
eunit: $(REBAR)
3135
$(REBAR) as test eunit

‎rebar.config

+41-37
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,51 @@
11
%% -*- mode: erlang -*-
22
{deps,
3-
[ %% not all plugins have to depend on emqx
4-
%% but if they do, use git_subdir in the default profile
5-
{emqx, {git_subdir, "https://github.com/emqx/emqx.git", {tag, "v5.1.4"}, "apps/emqx"}}
6-
, {emqx_ctl, {git_subdir, "https://github.com/emqx/emqx.git", {tag, "v5.1.4"}, "apps/emqx_ctl"}}
7-
, {emqx_utils, {git_subdir, "https://github.com/emqx/emqx.git", {tag, "v5.1.4"}, "apps/emqx_utils"}}
8-
, {emqx_durable_storage, {git_subdir, "https://github.com/emqx/emqx.git", {tag, "v5.1.4"}, "apps/emqx_durable_storage"}}
9-
%% this is my plugin's dependency
10-
, {map_sets, "1.1.0"}
11-
, {eredis_cluster, {git, "https://github.com/emqx/eredis_cluster", {tag, "0.8.3"}}}
12-
, {ecpool, {git, "https://github.com/emqx/ecpool", {tag, "v0.4.2"}}}
13-
]}.
3+
[%% not all plugins have to depend on emqx
4+
%% but if they do, use git_subdir in the default profile
5+
{emqx, {git_subdir, "https://github.com/emqx/emqx.git", {tag, "v5.1.4"}, "apps/emqx"}},
6+
{emqx_ctl,
7+
{git_subdir, "https://github.com/emqx/emqx.git", {tag, "v5.1.4"}, "apps/emqx_ctl"}},
8+
{emqx_utils,
9+
{git_subdir, "https://github.com/emqx/emqx.git", {tag, "v5.1.4"}, "apps/emqx_utils"}},
10+
{emqx_durable_storage,
11+
{git_subdir,
12+
"https://github.com/emqx/emqx.git",
13+
{tag, "v5.1.4"},
14+
"apps/emqx_durable_storage"}},
15+
%% this is my plugin's dependency
16+
{map_sets, "1.1.0"},
17+
{eredis_cluster, {git, "https://github.com/emqx/eredis_cluster", {tag, "0.8.3"}}},
18+
{ecpool, {git, "https://github.com/emqx/ecpool", {tag, "v0.4.2"}}}]}.
1419

15-
{plugins, [
16-
{emqx_plugrel, {git, "https://github.com/emqx/emqx_plugrel.git", {tag, "0.3.0"}}}
17-
]}.
20+
{plugins,
21+
[{emqx_plugrel, {git, "https://github.com/emqx/emqx_plugrel.git", {tag, "0.3.0"}}}]}.
1822

1923
{erl_opts, [debug_info]}.
2024

21-
{relx, [ {release, {ubidots_emqx_plugin, "1.0.0"}, %% this is the release version, different from app vsn in .app file
22-
[ ubidots_emqx_plugin
23-
, map_sets
24-
]}
25-
, {dev_mode, false}
26-
, {include_erts, false}
27-
]}.
25+
{relx,
26+
[{release,
27+
{ubidots_emqx_plugin,
28+
"1.0.0"}, %% this is the release version, different from app vsn in .app file
29+
[ubidots_emqx_plugin, map_sets]},
30+
{dev_mode, false},
31+
{include_erts, false}]}.
32+
33+
{project_plugins, [rebar3_format]}.
2834

2935
%% Additional info of the plugin
3036
{emqx_plugrel,
31-
[ {authors, ["ubidots"]}
32-
, {builder,
33-
[ {name, "ubidots"}
34-
, {contact, "devel@ubidots.com"}
35-
, {website, "http://ubidots.com"}
36-
]}
37-
, {repo, "https://github.com/jdavidagudelo/emqx-plugin-template-test.git"}
38-
, {functionality, ["Demo"]}
39-
, {compatibility,
40-
[ {emqx, "~> 5.0"}
41-
]}
42-
, {description, "Another amazing EMQX plugin."}
43-
]
44-
}.
37+
[{authors, ["ubidots"]},
38+
{builder,
39+
[{name, "ubidots"}, {contact, "devel@ubidots.com"}, {website, "http://ubidots.com"}]},
40+
{repo, "https://github.com/jdavidagudelo/emqx-plugin-template-test.git"},
41+
{functionality, ["Demo"]},
42+
{compatibility, [{emqx, "~> 5.0"}]},
43+
{description, "Another amazing EMQX plugin."}]}.
4544

46-
{xref_checks,[undefined_function_calls,undefined_functions,locals_not_used,
47-
deprecated_function_calls,warnings_as_errors,deprecated_functions]}.
45+
{xref_checks,
46+
[undefined_function_calls,
47+
undefined_functions,
48+
locals_not_used,
49+
deprecated_function_calls,
50+
warnings_as_errors,
51+
deprecated_functions]}.

‎src/ubidots_emqx_core_redis_cli.erl

+6-5
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,17 @@ connect(_Env) ->
4646

4747
get_variable_key(ValueKind) ->
4848
case ValueKind of
49-
"value" -> "last_value_variables_json:";
50-
"last_value" -> "last_value_variables_string:"
49+
"value" ->
50+
"last_value_variables_json:";
51+
"last_value" ->
52+
"last_value_variables_string:"
5153
end.
5254

5355
get_value_by_key(Pool, VariableKey, Type) ->
5456
case Type of
5557
single ->
56-
ecpool:with_client(Pool, fun(RedisClient) ->
57-
eredis:q(RedisClient, ["GET", VariableKey])
58-
end);
58+
ecpool:with_client(Pool,
59+
fun(RedisClient) -> eredis:q(RedisClient, ["GET", VariableKey]) end);
5960
cluster ->
6061
eredis_cluster:q(Pool, ["GET", VariableKey])
6162
end.

‎src/ubidots_emqx_plugin.app.src

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
{application, ubidots_emqx_plugin,
1+
{application,
2+
ubidots_emqx_plugin,
23
[{description, "Another amazing EMQX plugin."},
34
{vsn, "0.1.0"},
45
{modules, []},
@@ -7,5 +8,4 @@
78
{mod, {ubidots_emqx_plugin_app, []}},
89
{env, []},
910
{licenses, ["Apache-2.0"]},
10-
{maintainers, ["ubidots <ubidots@email.com>"]}
11-
]}.
11+
{maintainers, ["ubidots <ubidots@email.com>"]}]}.

‎src/ubidots_emqx_plugin.erl

+23-30
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,17 @@
55
-include_lib("emqx/include/emqx.hrl").
66
-include_lib("emqx/include/emqx_hooks.hrl").
77
-include_lib("hocon/include/hoconsc.hrl").
8-
98
%% for logging
109
-include_lib("emqx/include/logger.hrl").
1110

1211
-define(POOL_REACTOR, pool_reactor).
13-
1412
-define(POOL_CORE, pool_core).
1513

16-
-export([
17-
load/1,
18-
unload/0
19-
]).
20-
14+
-export([load/1, unload/0]).
2115
%% Client Lifecycle Hooks
22-
-export([
23-
on_client_subscribe/4,
24-
on_client_unsubscribe/4
25-
]).
26-
16+
-export([on_client_subscribe/4, on_client_unsubscribe/4]).
2717
%% Session Lifecycle Hooks
2818
-export([on_session_subscribed/5]).
29-
3019
%% Message Pubsub Hooks
3120
-export([on_message_delivered/3]).
3221

@@ -44,39 +33,43 @@ load(Env) ->
4433
%%--------------------------------------------------------------------
4534

4635
on_client_subscribe(#{username := UserName}, _Properties, TopicFilters, _Env) ->
47-
NewTopicFilters = lists:map(
48-
fun(TopicFilter) -> ubidots_emqx_topic_changer:add_users_topic(UserName, TopicFilter) end,
49-
TopicFilters
50-
),
36+
NewTopicFilters =
37+
lists:map(fun(TopicFilter) ->
38+
ubidots_emqx_topic_changer:add_users_topic(UserName, TopicFilter)
39+
end,
40+
TopicFilters),
5141
{ok, NewTopicFilters}.
5242

5343
on_client_unsubscribe(#{username := UserName}, _Properties, TopicFilters, _Env) ->
54-
NewTopicFilters = lists:map(
55-
fun(TopicFilter) -> ubidots_emqx_topic_changer:add_users_topic(UserName, TopicFilter) end,
56-
TopicFilters
57-
),
44+
NewTopicFilters =
45+
lists:map(fun(TopicFilter) ->
46+
ubidots_emqx_topic_changer:add_users_topic(UserName, TopicFilter)
47+
end,
48+
TopicFilters),
5849
{ok, NewTopicFilters}.
5950

6051
%%--------------------------------------------------------------------
6152
%% Session Lifecycle Hooks
6253
%%--------------------------------------------------------------------
6354

64-
on_session_subscribed(_, _, #{share := ShareName}, _Env, _Config) when ShareName =/= undefined ->
55+
on_session_subscribed(_, _, #{share := ShareName}, _Env, _Config)
56+
when ShareName =/= undefined ->
6557
ok;
6658
on_session_subscribed(_, Topic, #{rh := Rh, is_new := IsNew}, Env, Config) ->
6759
case Rh =:= 0 orelse Rh =:= 1 andalso IsNew of
68-
true -> emqx_pool:async_submit(fun dispatch/4, [self(), Topic, Env, Config]);
69-
_ -> ok
60+
true ->
61+
emqx_pool:async_submit(fun dispatch/4, [self(), Topic, Env, Config]);
62+
_ ->
63+
ok
7064
end.
7165

7266
dispatch(Pid, Topic, _Env, #{pool_reactor := PoolReactor, pool_core := PoolCore}) ->
7367
EnvVariables = ubidots_emqx_retainer_settings:get_settings(),
74-
NewMessages = ubidots_emqx_retainer_payload_changer:get_retained_messages_from_topic(
75-
Topic,
76-
EnvVariables,
77-
PoolReactor,
78-
PoolCore
79-
),
68+
NewMessages =
69+
ubidots_emqx_retainer_payload_changer:get_retained_messages_from_topic(Topic,
70+
EnvVariables,
71+
PoolReactor,
72+
PoolCore),
8073
dispatch_ubidots_message(NewMessages, Pid).
8174

8275
dispatch_ubidots_message([], _) ->

‎src/ubidots_emqx_plugin_app.erl

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44

55
-emqx_plugin(?MODULE).
66

7-
-export([
8-
start/2,
9-
stop/1
10-
]).
7+
-export([start/2, stop/1]).
118

129
start(_StartType, _StartArgs) ->
1310
{ok, Sup} = ubidots_emqx_plugin_sup:start_link(),
14-
ubidots_emqx_plugin:load(application:get_all_env()),
11+
ubidots_emqx_plugin:load(
12+
application:get_all_env()),
1513

1614
emqx_ctl:register_command(ubidots_emqx_plugin, {ubidots_emqx_plugin_cli, cmd}),
1715
{ok, Sup}.

‎src/ubidots_emqx_plugin_sup.erl

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
-behaviour(supervisor).
44

55
-export([start_link/0]).
6-
76
-export([init/1]).
87

98
start_link() ->

0 commit comments

Comments
 (0)
Please sign in to comment.