emqx/test/emqx_lib_SUITE.erl

174 lines
5.4 KiB
Erlang

%% Copyright (c) 2013-2019 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
-module(emqx_lib_SUITE).
-include_lib("eunit/include/eunit.hrl").
-compile(export_all).
-compile(nowarn_export_all).
-define(SOCKOPTS, [
binary,
{packet, raw},
{reuseaddr, true},
{backlog, 512},
{nodelay, true}
]).
-define(PQ, emqx_pqueue).
-define(BASE62, emqx_base62).
all() -> [{group, guid}, {group, opts},
{group, ?PQ}, {group, time},
{group, node}, {group, base62}].
groups() ->
[{guid, [], [guid_gen, guid_hexstr, guid_base62]},
{opts, [], [opts_merge]},
{?PQ, [], [priority_queue_plen,
priority_queue_out2]},
{time, [], [time_now_to_]},
{node, [], [node_is_aliving, node_parse_name]},
{base62, [], [base62_encode]}].
%%--------------------------------------------------------------------
%% emqx_guid
%%--------------------------------------------------------------------
guid_gen(_) ->
Guid1 = emqx_guid:gen(),
Guid2 = emqx_guid:gen(),
<<_:128>> = Guid1,
true = (Guid2 >= Guid1),
{Ts1, _, 0} = emqx_guid:new(),
Ts2 = emqx_guid:timestamp(emqx_guid:gen()),
true = Ts2 > Ts1.
guid_hexstr(_) ->
Guid = emqx_guid:gen(),
?assertEqual(Guid, emqx_guid:from_hexstr(emqx_guid:to_hexstr(Guid))).
guid_base62(_) ->
Guid = emqx_guid:gen(),
?assertEqual(Guid, emqx_guid:from_base62(emqx_guid:to_base62(Guid))).
%%--------------------------------------------------------------------
%% emqx_opts
%%--------------------------------------------------------------------
opts_merge(_) ->
Opts = emqx_misc:merge_opts(?SOCKOPTS, [raw,
binary,
{backlog, 1024},
{nodelay, false},
{max_clients, 1024},
{acceptors, 16}]),
1024 = proplists:get_value(backlog, Opts),
1024 = proplists:get_value(max_clients, Opts),
[binary, raw,
{acceptors, 16},
{backlog, 1024},
{max_clients, 1024},
{nodelay, false},
{packet, raw},
{reuseaddr, true}] = lists:sort(Opts).
%%--------------------------------------------------------------------
%% priority_queue
%%--------------------------------------------------------------------
priority_queue_plen(_) ->
Q = ?PQ:new(),
0 = ?PQ:plen(0, Q),
Q0 = ?PQ:in(z, Q),
1 = ?PQ:plen(0, Q0),
Q1 = ?PQ:in(x, 1, Q0),
1 = ?PQ:plen(1, Q1),
Q2 = ?PQ:in(y, 2, Q1),
1 = ?PQ:plen(2, Q2),
Q3 = ?PQ:in(z, 2, Q2),
2 = ?PQ:plen(2, Q3),
{_, Q4} = ?PQ:out(1, Q3),
0 = ?PQ:plen(1, Q4),
{_, Q5} = ?PQ:out(Q4),
1 = ?PQ:plen(2, Q5),
{_, Q6} = ?PQ:out(Q5),
0 = ?PQ:plen(2, Q6),
1 = ?PQ:len(Q6),
{_, Q7} = ?PQ:out(Q6),
0 = ?PQ:len(Q7).
priority_queue_out2(_) ->
Els = [a, {b, 1}, {c, 1}, {d, 2}, {e, 2}, {f, 2}],
Q = ?PQ:new(),
Q0 = lists:foldl(
fun({El, P}, Acc) ->
?PQ:in(El, P, Acc);
(El, Acc) ->
?PQ:in(El, Acc)
end, Q, Els),
{Val, Q1} = ?PQ:out(Q0),
{value, d} = Val,
{Val1, Q2} = ?PQ:out(2, Q1),
{value, e} = Val1,
{Val2, Q3} = ?PQ:out(1, Q2),
{value, b} = Val2,
{Val3, Q4} = ?PQ:out(Q3),
{value, f} = Val3,
{Val4, Q5} = ?PQ:out(Q4),
{value, c} = Val4,
{Val5, Q6} = ?PQ:out(Q5),
{value, a} = Val5,
{empty, _Q7} = ?PQ:out(Q6).
%%--------------------------------------------------------------------
%% emqx_time
%%--------------------------------------------------------------------
time_now_to_(_) ->
emqx_time:seed(),
emqx_time:now_secs(),
emqx_time:now_ms().
%%--------------------------------------------------------------------
%% emqx_node
%%--------------------------------------------------------------------
node_is_aliving(_) ->
io:format("Node: ~p~n", [node()]),
true = ekka_node:is_aliving(node()),
false = ekka_node:is_aliving('x@127.0.0.1').
node_parse_name(_) ->
'a@127.0.0.1' = ekka_node:parse_name("a@127.0.0.1"),
'b@127.0.0.1' = ekka_node:parse_name("b").
%%--------------------------------------------------------------------
%% base62 encode decode
%%--------------------------------------------------------------------
base62_encode(_) ->
<<"10">> = ?BASE62:decode(?BASE62:encode(<<"10">>)),
<<"100">> = ?BASE62:decode(?BASE62:encode(<<"100">>)),
<<"9999">> = ?BASE62:decode(?BASE62:encode(<<"9999">>)),
<<"65535">> = ?BASE62:decode(?BASE62:encode(<<"65535">>)),
<<X:128/unsigned-big-integer>> = emqx_guid:gen(),
<<Y:128/unsigned-big-integer>> = emqx_guid:gen(),
X = ?BASE62:decode(?BASE62:encode(X), integer),
Y = ?BASE62:decode(?BASE62:encode(Y), integer),
<<"helloworld">> = ?BASE62:decode(?BASE62:encode("helloworld")),
"helloworld" = ?BASE62:decode(?BASE62:encode("helloworld", string), string).