fix: add uptime/1 for CT test.

This commit is contained in:
zhongwencool 2022-02-25 15:57:55 +08:00
parent ed41a66c6e
commit b1816a6647
2 changed files with 26 additions and 15 deletions

View File

@ -95,16 +95,14 @@ sysdescr() -> emqx_app:get_description().
-spec(uptime() -> string()).
uptime() ->
{TotalWallClock, _} = erlang:statistics(wall_clock),
{D, {H, M, S}} = calendar:seconds_to_daystime(TotalWallClock div 1000),
List = [{D, " days"}, {H, " hours"}, {M, " minutes"}, {S, " seconds"}],
{_, Uptime} =
lists:foldl(fun({Time, Unit}, {NeedSkip, Acc}) ->
case Time =:= 0 andalso NeedSkip of
true -> {NeedSkip, Acc};
false -> {false, [[integer_to_list(Time), Unit] |Acc]}
end
end, {true, []}, List),
lists:flatten(lists:join(", ", lists:reverse(Uptime))).
uptime(TotalWallClock div 1000).
uptime(Seconds) ->
{D, {H, M, S}} = calendar:seconds_to_daystime(Seconds),
L0 = [{D, " days"}, {H, " hours"}, {M, " minutes"}, {S, " seconds"}],
L1 = lists:dropwhile(fun({K, _}) -> K =:= 0 end, L0),
L2 = lists:map(fun({Time, Unit}) -> [integer_to_list(Time), Unit] end, L1),
lists:flatten(lists:join(", ", L2)).
%% @doc Get sys datetime
-spec(datetime() -> string()).

View File

@ -34,7 +34,7 @@ end_per_suite(_Config) ->
application:unload(emqx),
ok = emqx_logger:set_log_level(error),
ok.
% t_version(_) ->
% error('TODO').
@ -42,10 +42,23 @@ end_per_suite(_Config) ->
% error('TODO').
t_uptime(_) ->
?assertEqual(<<"1 seconds">>, iolist_to_binary(emqx_sys:uptime(seconds, 1))),
?assertEqual(<<"1 minutes, 0 seconds">>, iolist_to_binary(emqx_sys:uptime(seconds, 60))),
?assertEqual(<<"1 hours, 0 minutes, 0 seconds">>, iolist_to_binary(emqx_sys:uptime(seconds, 3600))),
?assertEqual(<<"1 days, 0 hours, 0 minutes, 0 seconds">>, iolist_to_binary(emqx_sys:uptime(seconds, 86400))).
?assert(is_list(emqx_sys:uptime())),
?assertEqual(<<"1 seconds">>, iolist_to_binary(emqx_sys:uptime(1))),
?assertEqual(<<"1 minutes, 0 seconds">>, iolist_to_binary(emqx_sys:uptime(60))),
?assertEqual(<<"1 hours, 0 minutes, 0 seconds">>, iolist_to_binary(emqx_sys:uptime(3600))),
?assertEqual(<<"1 hours, 1 minutes, 1 seconds">>, iolist_to_binary(emqx_sys:uptime(3661))),
?assertEqual(<<"1 days, 0 hours, 0 minutes, 0 seconds">>, iolist_to_binary(emqx_sys:uptime(86400))),
lists:map(fun({D, H, M, S}) ->
Expect = <<
(integer_to_binary(D))/binary, " days, ",
(integer_to_binary(H))/binary, " hours, ",
(integer_to_binary(M))/binary, " minutes, ",
(integer_to_binary(S))/binary, " seconds"
>>,
Actual = iolist_to_binary(emqx_sys:uptime(D * 86400 + H * 3600 + M * 60 + S)),
?assertEqual(Expect, Actual)
end,
[{1, 2, 3, 4}, {10, 20, 30, 40}, {2222, 3, 56, 59}, {59, 23, 59, 59}]).
% t_datetime(_) ->
% error('TODO').