fix(bridge_v2): more fixes thanks to PR comments from @thalesmg

This commit is contained in:
Kjell Winblad 2023-11-01 15:27:06 +01:00
parent b06d05eaac
commit 357b664c8d
2 changed files with 17 additions and 18 deletions

View File

@ -133,7 +133,6 @@ setup_mocks() ->
ok. ok.
init_per_suite(Config) -> init_per_suite(Config) ->
snabbkaffe:fix_ct_logging(),
Apps = emqx_cth_suite:start( Apps = emqx_cth_suite:start(
app_specs(), app_specs(),
#{work_dir => emqx_cth_suite:work_dir(Config)} #{work_dir => emqx_cth_suite:work_dir(Config)}

View File

@ -578,7 +578,7 @@ add_channels(Data) ->
Channels = Data#data.added_channels, Channels = Data#data.added_channels,
NewChannels = lists:foldl( NewChannels = lists:foldl(
fun({ChannelID, _Conf}, Acc) -> fun({ChannelID, _Conf}, Acc) ->
maps:put(ChannelID, channel_status_new(), Acc) maps:put(ChannelID, channel_status(), Acc)
end, end,
Channels, Channels,
ChannelIDConfigTuples ChannelIDConfigTuples
@ -617,7 +617,7 @@ add_channels_in_list([{ChannelID, ChannelConfig} | Rest], Data) ->
AddedChannelsMap = Data#data.added_channels, AddedChannelsMap = Data#data.added_channels,
NewAddedChannelsMap = maps:put( NewAddedChannelsMap = maps:put(
ChannelID, ChannelID,
channel_status_new(Error), channel_status(Error),
AddedChannelsMap AddedChannelsMap
), ),
NewData = Data#data{ NewData = Data#data{
@ -703,7 +703,7 @@ handle_add_channel(From, Data, ChannelId, Config) ->
maps:get( maps:get(
ChannelId, ChannelId,
Channels, Channels,
channel_status_new() channel_status()
) )
) )
of of
@ -714,7 +714,7 @@ handle_add_channel(From, Data, ChannelId, Config) ->
NewChannels = maps:put(ChannelId, channel_status_new_with_config(Config), Channels), NewChannels = maps:put(ChannelId, channel_status_new_with_config(Config), Channels),
NewData = Data#data{added_channels = NewChannels}, NewData = Data#data{added_channels = NewChannels},
{keep_state, update_state(NewData, Data), [ {keep_state, update_state(NewData, Data), [
{reply, From, ok}, {state_timeout, 0, health_check} {reply, From, ok}
]}; ]};
true -> true ->
%% The channel is already installed in the connector state %% The channel is already installed in the connector state
@ -732,7 +732,7 @@ handle_remove_channel(From, ChannelId, Data) ->
Channels = Data#data.added_channels, Channels = Data#data.added_channels,
%% Deactivate alarm %% Deactivate alarm
_ = maybe_clear_alarm(ChannelId), _ = maybe_clear_alarm(ChannelId),
case channel_status_is_channel_added(maps:get(ChannelId, Channels, channel_status_new())) of case channel_status_is_channel_added(maps:get(ChannelId, Channels, channel_status())) of
false -> false ->
%% The channel is already not installed in the connector state. %% The channel is already not installed in the connector state.
%% We still need to remove it from the added_channels map %% We still need to remove it from the added_channels map
@ -792,7 +792,7 @@ handle_manually_health_check(From, Data) ->
). ).
handle_manually_channel_health_check(From, #data{state = undefined}, _ChannelId) -> handle_manually_channel_health_check(From, #data{state = undefined}, _ChannelId) ->
{keep_state_and_data, [{reply, From, channel_status_new({error, resource_disconnected})}]}; {keep_state_and_data, [{reply, From, channel_status({error, resource_disconnected})}]};
handle_manually_channel_health_check( handle_manually_channel_health_check(
From, From,
#data{added_channels = Channels} = _Data, #data{added_channels = Channels} = _Data,
@ -806,11 +806,11 @@ handle_manually_channel_health_check(
_Data, _Data,
_ChannelId _ChannelId
) -> ) ->
{keep_state_and_data, [{reply, From, channel_status_new({error, channel_not_found})}]}. {keep_state_and_data, [{reply, From, channel_status({error, channel_not_found})}]}.
get_channel_status_channel_added(#data{id = ResId, mod = Mod, state = State}, ChannelId) -> get_channel_status_channel_added(#data{id = ResId, mod = Mod, state = State}, ChannelId) ->
RawStatus = emqx_resource:call_channel_health_check(ResId, ChannelId, Mod, State), RawStatus = emqx_resource:call_channel_health_check(ResId, ChannelId, Mod, State),
channel_status_new(RawStatus). channel_status(RawStatus).
handle_connecting_health_check(Data) -> handle_connecting_health_check(Data) ->
with_health_check( with_health_check(
@ -884,7 +884,7 @@ channels_health_check(connecting, Data0) ->
], ],
ChannelsWithNewStatuses = ChannelsWithNewStatuses =
[ [
{ChannelId, channel_status_new({connecting, resource_is_connecting})} {ChannelId, channel_status({connecting, resource_is_connecting})}
|| ChannelId <- ChannelsToChangeStatusFor || ChannelId <- ChannelsToChangeStatusFor
], ],
%% Update the channels map %% Update the channels map
@ -924,7 +924,7 @@ channels_health_check(ResourceStatus, Data0) ->
ChannelsWithNewAndOldStatuses = ChannelsWithNewAndOldStatuses =
[ [
{ChannelId, OldStatus, {ChannelId, OldStatus,
channel_status_new( channel_status(
{error, {error,
resource_not_connected_channel_error_msg( resource_not_connected_channel_error_msg(
ResourceStatus, ResourceStatus,
@ -1151,7 +1151,7 @@ safe_call(ResId, Message, Timeout) ->
%% Helper functions for chanel status data %% Helper functions for chanel status data
channel_status_new() -> channel_status() ->
#{ #{
%% The status of the channel. Can be one of the following: %% The status of the channel. Can be one of the following:
%% - disconnected: the channel is not added to the resource (error may contain the reason)) %% - disconnected: the channel is not added to the resource (error may contain the reason))
@ -1181,29 +1181,29 @@ channel_status_new_waiting_for_health_check() ->
error => no_health_check_yet error => no_health_check_yet
}. }.
channel_status_new({connecting, Error}) -> channel_status({connecting, Error}) ->
#{ #{
status => connecting, status => connecting,
error => Error error => Error
}; };
channel_status_new(connecting) -> channel_status(connecting) ->
#{ #{
status => connecting, status => connecting,
error => <<"Not connected for unknown reason">> error => <<"Not connected for unknown reason">>
}; };
channel_status_new(connected) -> channel_status(connected) ->
#{ #{
status => connected, status => connected,
error => undefined error => undefined
}; };
%% Probably not so useful but it is permitted to set an error even when the %% Probably not so useful but it is permitted to set an error even when the
%% status is connected %% status is connected
channel_status_new({connected, Error}) -> channel_status({connected, Error}) ->
#{ #{
status => connected, status => connected,
error => Error error => Error
}; };
channel_status_new({error, Reason}) -> channel_status({error, Reason}) ->
#{ #{
status => disconnected, status => disconnected,
error => Reason error => Reason
@ -1226,7 +1226,7 @@ add_channel_status_if_not_exists(Data, ChannelId, State) ->
true -> true ->
Data; Data;
false -> false ->
ChannelStatus = channel_status_new({error, resource_not_operational}), ChannelStatus = channel_status({error, resource_not_operational}),
NewChannels = maps:put(ChannelId, ChannelStatus, Channels), NewChannels = maps:put(ChannelId, ChannelStatus, Channels),
maybe_alarm(State, ChannelId, ChannelStatus, no_prev), maybe_alarm(State, ChannelId, ChannelStatus, no_prev),
Data#data{added_channels = NewChannels} Data#data{added_channels = NewChannels}