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

View File

@ -42,10 +42,23 @@ end_per_suite(_Config) ->
% error('TODO'). % error('TODO').
t_uptime(_) -> t_uptime(_) ->
?assertEqual(<<"1 seconds">>, iolist_to_binary(emqx_sys:uptime(seconds, 1))), ?assert(is_list(emqx_sys:uptime())),
?assertEqual(<<"1 minutes, 0 seconds">>, iolist_to_binary(emqx_sys:uptime(seconds, 60))), ?assertEqual(<<"1 seconds">>, iolist_to_binary(emqx_sys:uptime(1))),
?assertEqual(<<"1 hours, 0 minutes, 0 seconds">>, iolist_to_binary(emqx_sys:uptime(seconds, 3600))), ?assertEqual(<<"1 minutes, 0 seconds">>, iolist_to_binary(emqx_sys:uptime(60))),
?assertEqual(<<"1 days, 0 hours, 0 minutes, 0 seconds">>, iolist_to_binary(emqx_sys:uptime(seconds, 86400))). ?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(_) -> % t_datetime(_) ->
% error('TODO'). % error('TODO').