Use erlang:system_time/1

This commit is contained in:
zhouzb 2019-12-09 17:41:25 +08:00 committed by Feng Lee
parent 08e234f61e
commit ec2f758c9f
2 changed files with 9 additions and 11 deletions

View File

@ -105,7 +105,7 @@ init(_) ->
{ok, []}. {ok, []}.
handle_event({set_alarm, {AlarmId, AlarmDesc = #alarm{timestamp = undefined}}}, State) -> handle_event({set_alarm, {AlarmId, AlarmDesc = #alarm{timestamp = undefined}}}, State) ->
handle_event({set_alarm, {AlarmId, AlarmDesc#alarm{timestamp = os:timestamp()}}}, State); handle_event({set_alarm, {AlarmId, AlarmDesc#alarm{timestamp = erlang:system_time(second)}}}, State);
handle_event({set_alarm, Alarm = {AlarmId, AlarmDesc}}, State) -> handle_event({set_alarm, Alarm = {AlarmId, AlarmDesc}}, State) ->
?LOG(warning, "New Alarm: ~p, Alarm Info: ~p", [AlarmId, AlarmDesc]), ?LOG(warning, "New Alarm: ~p, Alarm Info: ~p", [AlarmId, AlarmDesc]),
case encode_alarm(Alarm) of case encode_alarm(Alarm) of
@ -158,7 +158,7 @@ encode_alarm({AlarmId, #alarm{severity = Severity,
{desc, [{severity, Severity}, {desc, [{severity, Severity},
{title, iolist_to_binary(Title)}, {title, iolist_to_binary(Title)},
{summary, iolist_to_binary(Summary)}, {summary, iolist_to_binary(Summary)},
{timestamp, emqx_misc:now_to_secs(Ts)}]}]); {timestamp, Ts}]}]);
encode_alarm({AlarmId, undefined}) -> encode_alarm({AlarmId, undefined}) ->
emqx_json:safe_encode([{id, maybe_to_binary(AlarmId)}]); emqx_json:safe_encode([{id, maybe_to_binary(AlarmId)}]);
encode_alarm({AlarmId, AlarmDesc}) -> encode_alarm({AlarmId, AlarmDesc}) ->
@ -194,5 +194,5 @@ clear_alarm_(Id) ->
set_alarm_history(Id, Desc) -> set_alarm_history(Id, Desc) ->
His = #alarm_history{id = Id, His = #alarm_history{id = Id,
desc = Desc, desc = Desc,
clear_at = os:timestamp()}, clear_at = erlang:system_time(second)},
mnesia:dirty_write(?ALARM_HISTORY_TAB, His). mnesia:dirty_write(?ALARM_HISTORY_TAB, His).

View File

@ -141,13 +141,13 @@ t_is_awaiting_full_true(_) ->
t_puback(_) -> t_puback(_) ->
Msg = emqx_message:make(test, ?QOS_1, <<"t">>, <<>>), Msg = emqx_message:make(test, ?QOS_1, <<"t">>, <<>>),
Inflight = emqx_inflight:insert(1, {Msg, os:timestamp()}, emqx_inflight:new()), Inflight = emqx_inflight:insert(1, {Msg, erlang:system_time(millisecond)}, emqx_inflight:new()),
Session = set_field(inflight, Inflight, session()), Session = set_field(inflight, Inflight, session()),
{ok, Msg, NSession} = emqx_session:puback(1, Session), {ok, Msg, NSession} = emqx_session:puback(1, Session),
?assertEqual(0, emqx_session:info(inflight_cnt, NSession)). ?assertEqual(0, emqx_session:info(inflight_cnt, NSession)).
t_puback_error_packet_id_in_use(_) -> t_puback_error_packet_id_in_use(_) ->
Inflight = emqx_inflight:insert(1, {pubrel, os:timestamp()}, emqx_inflight:new()), Inflight = emqx_inflight:insert(1, {pubrel, erlang:system_time(millisecond)}, emqx_inflight:new()),
Session = set_field(inflight, Inflight, session()), Session = set_field(inflight, Inflight, session()),
{error, ?RC_PACKET_IDENTIFIER_IN_USE} = emqx_session:puback(1, Session). {error, ?RC_PACKET_IDENTIFIER_IN_USE} = emqx_session:puback(1, Session).
@ -156,13 +156,13 @@ t_puback_error_packet_id_not_found(_) ->
t_pubrec(_) -> t_pubrec(_) ->
Msg = emqx_message:make(test, ?QOS_2, <<"t">>, <<>>), Msg = emqx_message:make(test, ?QOS_2, <<"t">>, <<>>),
Inflight = emqx_inflight:insert(2, {Msg, os:timestamp()}, emqx_inflight:new()), Inflight = emqx_inflight:insert(2, {Msg, erlang:system_time(millisecond)}, emqx_inflight:new()),
Session = set_field(inflight, Inflight, session()), Session = set_field(inflight, Inflight, session()),
{ok, Msg, NSession} = emqx_session:pubrec(2, Session), {ok, Msg, NSession} = emqx_session:pubrec(2, Session),
?assertMatch([{pubrel, _}], emqx_inflight:values(emqx_session:info(inflight, NSession))). ?assertMatch([{pubrel, _}], emqx_inflight:values(emqx_session:info(inflight, NSession))).
t_pubrec_packet_id_in_use_error(_) -> t_pubrec_packet_id_in_use_error(_) ->
Inflight = emqx_inflight:insert(1, {pubrel, ts()}, emqx_inflight:new()), Inflight = emqx_inflight:insert(1, {pubrel, erlang:system_time(millisecond)}, emqx_inflight:new()),
Session = set_field(inflight, Inflight, session()), Session = set_field(inflight, Inflight, session()),
{error, ?RC_PACKET_IDENTIFIER_IN_USE} = emqx_session:puback(1, Session). {error, ?RC_PACKET_IDENTIFIER_IN_USE} = emqx_session:puback(1, Session).
@ -170,7 +170,7 @@ t_pubrec_packet_id_not_found_error(_) ->
{error, ?RC_PACKET_IDENTIFIER_NOT_FOUND} = emqx_session:pubrec(1, session()). {error, ?RC_PACKET_IDENTIFIER_NOT_FOUND} = emqx_session:pubrec(1, session()).
t_pubrel(_) -> t_pubrel(_) ->
Session = set_field(awaiting_rel, #{1 => os:timestamp()}, session()), Session = set_field(awaiting_rel, #{1 => erlang:system_time(millisecond)}, session()),
{ok, NSession} = emqx_session:pubrel(1, Session), {ok, NSession} = emqx_session:pubrel(1, Session),
?assertEqual(#{}, emqx_session:info(awaiting_rel, NSession)). ?assertEqual(#{}, emqx_session:info(awaiting_rel, NSession)).
@ -178,7 +178,7 @@ t_pubrel_id_not_found(_) ->
{error, ?RC_PACKET_IDENTIFIER_NOT_FOUND} = emqx_session:pubrel(1, session()). {error, ?RC_PACKET_IDENTIFIER_NOT_FOUND} = emqx_session:pubrel(1, session()).
t_pubcomp(_) -> t_pubcomp(_) ->
Inflight = emqx_inflight:insert(2, {pubrel, os:timestamp()}, emqx_inflight:new()), Inflight = emqx_inflight:insert(2, {pubrel, erlang:system_time(millisecond)}, emqx_inflight:new()),
Session = emqx_session:set_field(inflight, Inflight, session()), Session = emqx_session:set_field(inflight, Inflight, session()),
{ok, NSession} = emqx_session:pubcomp(2, Session), {ok, NSession} = emqx_session:pubcomp(2, Session),
?assertEqual(0, emqx_session:info(inflight_cnt, NSession)). ?assertEqual(0, emqx_session:info(inflight_cnt, NSession)).
@ -252,5 +252,3 @@ subopts(Init) ->
delivery(QoS, Topic) -> delivery(QoS, Topic) ->
{deliver, Topic, emqx_message:make(test, QoS, Topic, <<"payload">>)}. {deliver, Topic, emqx_message:make(test, QoS, Topic, <<"payload">>)}.
ts() -> erlang:system_time(second).