Skip to content

Commit

Permalink
More gateway discovery unittests for client library.
Browse files Browse the repository at this point in the history
  • Loading branch information
arobenko committed Jun 12, 2024
1 parent 854feb5 commit cf995e5
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 6 deletions.
6 changes: 5 additions & 1 deletion client/lib/doxygen/main.dox
Original file line number Diff line number Diff line change
Expand Up @@ -491,14 +491,18 @@
/// from withing the operation completion callback to update the stored gateway address.
/// @code
/// CC_MqttsnGatewayInfo info;
/// cc_mqttsn_client_init_gateway_info(&info);
/// info.m_gwId = ...;
/// info.m_addr = ...;
/// info.m_addrLen = ...;
/// CC_MqttsnErrorCode ec = cc_mqttsn_client_set_available_gateway_info(client, info);
/// CC_MqttsnErrorCode ec = cc_mqttsn_client_set_available_gateway_info(client, &info);
/// if (ec != CC_MqttsnErrorCode_Success) {
/// printf("Something is wrong");
/// }
/// @endcode
/// It is recommended to initialize @ref CC_MqttsnGatewayInfo structure using the
/// @b cc_mqttsn_client_init_gateway_info() function before update, even though all
/// the member fields are assigned new values.
///
/// The application can force the library to discard the available gateway information
/// by issuing the @b cc_mqttsn_client_discard_available_gateway_info() function.
Expand Down
2 changes: 1 addition & 1 deletion client/lib/src/op/SearchOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void SearchOp::timeoutInternal()

void SearchOp::opTimeoutCb(void* data)
{
asSearchOp(data)->sendInternal();
asSearchOp(data)->timeoutInternal();
}

} // namespace op
Expand Down
6 changes: 6 additions & 0 deletions client/lib/templ/client.cpp.templ
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ unsigned cc_mqttsn_##NAME##client_get_available_gateways_count(CC_MqttsnClientHa
return static_cast<unsigned>(clientFromHandle(client)->clientState().m_gwInfos.size());
}

void cc_mqttsn_##NAME##client_init_gateway_info(CC_MqttsnGatewayInfo* info)
{
COMMS_ASSERT(info != nullptr);
*info = CC_MqttsnGatewayInfo();
}

CC_MqttsnErrorCode cc_mqttsn_##NAME##client_get_available_gateway_info(CC_MqttsnClientHandle client, unsigned idx, CC_MqttsnGatewayInfo* info)
{
COMMS_ASSERT(client != nullptr);
Expand Down
6 changes: 6 additions & 0 deletions client/lib/templ/client.h.templ
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,16 @@ unsigned cc_mqttsn_##NAME##client_get_default_broadcast_radius(CC_MqttsnClientHa
/// @ingroup client
unsigned cc_mqttsn_##NAME##client_get_available_gateways_count(CC_MqttsnClientHandle client);

/// @brief Initialize the @ref CC_MqttsnGatewayInfo structure.
/// @details Zeroes all the member fields.
void cc_mqttsn_##NAME##client_init_gateway_info(CC_MqttsnGatewayInfo* info);

/// @brief Retrieve stored available gateway information
/// @param[in] client Handle returned by @ref cc_mqttsn_##NAME##client_alloc() function.
/// @param[in] idx Index of the available gateway information.
/// @param[out] info Stored gateway information.
/// @return Result code of the call.
/// @ingroup client
CC_MqttsnErrorCode cc_mqttsn_##NAME##client_get_available_gateway_info(CC_MqttsnClientHandle client, unsigned idx, CC_MqttsnGatewayInfo* info);

/// @brief Update stored available gateway information
Expand All @@ -141,6 +146,7 @@ CC_MqttsnErrorCode cc_mqttsn_##NAME##client_get_available_gateway_info(CC_Mqttsn
/// @param[in] client Handle returned by @ref cc_mqttsn_##NAME##client_alloc() function.
/// @param[in] info Updated gateway information.
/// @return Result code of the call.
/// @ingroup client
CC_MqttsnErrorCode cc_mqttsn_##NAME##client_set_available_gateway_info(CC_MqttsnClientHandle client, const CC_MqttsnGatewayInfo* info);

/// @brief Discard stored available gateway information
Expand Down
62 changes: 61 additions & 1 deletion client/lib/test/UnitTestCommonBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ UnitTestCommonBase::UnitTestCommonBase(const LibFuncs& funcs) :
test_assert(m_funcs.m_set_default_retry_count != nullptr);
test_assert(m_funcs.m_get_default_retry_count != nullptr);
test_assert(m_funcs.m_set_default_broadcast_radius != nullptr);
test_assert(m_funcs.m_init_gateway_info != nullptr);
test_assert(m_funcs.m_get_available_gateway_info != nullptr);
test_assert(m_funcs.m_set_available_gateway_info != nullptr);
test_assert(m_funcs.m_discard_available_gateway_info != nullptr);
Expand Down Expand Up @@ -97,6 +98,13 @@ UnitTestCommonBase::UnitTestSearchCompleteReport::UnitTestSearchCompleteReport(C
}
}

void UnitTestCommonBase::UnitTestSearchCompleteReport::assignInfo(CC_MqttsnGatewayInfo& info) const
{
info.m_gwId = m_info.m_gwId;
info.m_addr = m_info.m_addr.data();
info.m_addrLen = static_cast<decltype(info.m_addrLen)>(m_info.m_addr.size());
}

void UnitTestCommonBase::unitTestSetUp()
{
}
Expand Down Expand Up @@ -217,6 +225,7 @@ std::vector<UniTestsMsgPtr> UnitTestCommonBase::unitTestPopAllOuputMessages(bool
// readPtr is advanced in read operation above
}

m_data.m_outData.pop_front();
} while (false);

test_assert((!mustExist) || (!result.empty()))
Expand Down Expand Up @@ -270,7 +279,7 @@ const UnitTestCommonBase::UnitTestSearchCompleteReport* UnitTestCommonBase::unit
return &m_data.m_searchCompleteReports.front();
}

void UnitTestCommonBase::unitTestPopSearchCompletereport()
void UnitTestCommonBase::unitTestPopSearchCompleteReport()
{
test_assert(unitTestHasSearchCompleteReport());
m_data.m_searchCompleteReports.pop_front();
Expand All @@ -294,16 +303,67 @@ void UnitTestCommonBase::unitTestSearch(CC_MqttsnClient* client, UnitTestSearchC
m_funcs.m_search(client, &UnitTestCommonBase::unitTestSearchCompleteCb, this);
}

void UnitTestCommonBase::unitTestSearchUpdateAddr(CC_MqttsnClient* client, const UnitTestData& addr)
{
unitTestSearch(
client,
[this, client, addr](const UnitTestSearchCompleteReport& report)
{
if (report.m_status != CC_MqttsnAsyncOpStatus_Complete) {
return false;
}

auto prevCount = m_funcs.m_get_available_gateways_count(client);

CC_MqttsnGatewayInfo updInfo;
m_funcs.m_init_gateway_info(&updInfo);
updInfo.m_gwId = report.m_info.m_gwId;
updInfo.m_addr = addr.data();
updInfo.m_addrLen = static_cast<decltype(updInfo.m_addrLen)>(addr.size());
auto ec = m_funcs.m_set_available_gateway_info(client, &updInfo);
test_assert(ec == CC_MqttsnErrorCode_Success);

auto afterUpdateCount = m_funcs.m_get_available_gateways_count(client);
test_assert(prevCount == afterUpdateCount); // Mustn't change
return false;
});
}

void UnitTestCommonBase::apiProcessData(CC_MqttsnClient* client, const unsigned char* buf, unsigned bufLen)
{
m_funcs.m_process_data(client, buf, bufLen);
}

CC_MqttsnErrorCode UnitTestCommonBase::apiSetDefaultRetryPeriod(CC_MqttsnClient* client, unsigned value)
{
return m_funcs.m_set_default_retry_period(client, value);
}

CC_MqttsnErrorCode UnitTestCommonBase::apiSetDefaultRetryCount(CC_MqttsnClient* client, unsigned value)
{
return m_funcs.m_set_default_retry_count(client, value);
}

CC_MqttsnSearchHandle UnitTestCommonBase::apiSearchPrepare(CC_MqttsnClient* client, CC_MqttsnErrorCode* ec)
{
return m_funcs.m_search_prepare(client, ec);
}

CC_MqttsnErrorCode UnitTestCommonBase::apiSearchSetRetryPeriod(CC_MqttsnSearchHandle search, unsigned value)
{
return m_funcs.m_search_set_retry_period(search, value);
}

CC_MqttsnErrorCode UnitTestCommonBase::apiSearchSetRetryCount(CC_MqttsnSearchHandle search, unsigned value)
{
return m_funcs.m_search_set_retry_count(search, value);
}

CC_MqttsnErrorCode UnitTestCommonBase::apiSearchSetBroadcastRadius(CC_MqttsnSearchHandle search, unsigned value)
{
return m_funcs.m_search_set_broadcast_radius(search, value);
}

void UnitTestCommonBase::unitTestTickProgramCb(void* data, unsigned duration)
{
auto* thisPtr = asThis(data);
Expand Down
13 changes: 11 additions & 2 deletions client/lib/test/UnitTestCommonBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class UnitTestCommonBase
CC_MqttsnErrorCode (*m_set_default_broadcast_radius)(CC_MqttsnClientHandle, unsigned) = nullptr;
unsigned (*m_get_default_broadcast_radius)(CC_MqttsnClientHandle) = nullptr;
unsigned (*m_get_available_gateways_count)(CC_MqttsnClientHandle) = nullptr;
void (*m_init_gateway_info)(CC_MqttsnGatewayInfo* info) = nullptr;
CC_MqttsnErrorCode (*m_get_available_gateway_info)(CC_MqttsnClientHandle, unsigned, CC_MqttsnGatewayInfo*) = nullptr;
CC_MqttsnErrorCode (*m_set_available_gateway_info)(CC_MqttsnClientHandle, const CC_MqttsnGatewayInfo*) = nullptr;
CC_MqttsnErrorCode (*m_discard_available_gateway_info)(CC_MqttsnClientHandle, unsigned char) = nullptr;
Expand Down Expand Up @@ -96,7 +97,7 @@ class UnitTestCommonBase

struct UnitTestGwInfo
{
unsigned m_gwId = 0U;
std::uint8_t m_gwId = 0U;
UnitTestData m_addr;

UnitTestGwInfo() = default;
Expand All @@ -121,6 +122,7 @@ class UnitTestCommonBase
UnitTestGwInfo m_info;

UnitTestSearchCompleteReport(CC_MqttsnAsyncOpStatus status, const CC_MqttsnGatewayInfo* info);
void assignInfo(CC_MqttsnGatewayInfo& info) const;
};
using UnitTestSearchCompleteReportsList = std::list<UnitTestSearchCompleteReport>;

Expand Down Expand Up @@ -152,13 +154,20 @@ class UnitTestCommonBase

bool unitTestHasSearchCompleteReport() const;
const UnitTestSearchCompleteReport* unitTestSearchCompleteReport(bool mustExist = true) const;
void unitTestPopSearchCompletereport();
void unitTestPopSearchCompleteReport();

void unitTestSearchSend(CC_MqttsnSearchHandle search, UnitTestSearchCompleteCb&& cb = UnitTestSearchCompleteCb());
void unitTestSearch(CC_MqttsnClient* client, UnitTestSearchCompleteCb&& cb = UnitTestSearchCompleteCb());
void unitTestSearchUpdateAddr(CC_MqttsnClient* client, const UnitTestData& addr);

void apiProcessData(CC_MqttsnClient* client, const unsigned char* buf, unsigned bufLen);
CC_MqttsnErrorCode apiSetDefaultRetryPeriod(CC_MqttsnClient* client, unsigned value);
CC_MqttsnErrorCode apiSetDefaultRetryCount(CC_MqttsnClient* client, unsigned value);
CC_MqttsnSearchHandle apiSearchPrepare(CC_MqttsnClient* client, CC_MqttsnErrorCode* ec = nullptr);
CC_MqttsnErrorCode apiSearchSetRetryPeriod(CC_MqttsnSearchHandle search, unsigned value);
CC_MqttsnErrorCode apiSearchSetRetryCount(CC_MqttsnSearchHandle search, unsigned value);
CC_MqttsnErrorCode apiSearchSetBroadcastRadius(CC_MqttsnSearchHandle search, unsigned value);


protected:
explicit UnitTestCommonBase(const LibFuncs& funcs);
Expand Down
1 change: 1 addition & 0 deletions client/lib/test/UnitTestDefaultBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const UnitTestDefaultBase::LibFuncs& UnitTestDefaultBase::getFuncs()
funcs.m_set_default_broadcast_radius = &cc_mqttsn_client_set_default_broadcast_radius;
funcs.m_get_default_broadcast_radius = &cc_mqttsn_client_get_default_broadcast_radius;
funcs.m_get_available_gateways_count = &cc_mqttsn_client_get_available_gateways_count;
funcs.m_init_gateway_info = &cc_mqttsn_client_init_gateway_info;
funcs.m_get_available_gateway_info = &cc_mqttsn_client_get_available_gateway_info;
funcs.m_set_available_gateway_info = &cc_mqttsn_client_set_available_gateway_info;
funcs.m_discard_available_gateway_info = &cc_mqttsn_client_discard_available_gateway_info;
Expand Down
Loading

0 comments on commit cf995e5

Please sign in to comment.