Skip to content

Commit

Permalink
More gateway discovery testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
arobenko committed Jul 16, 2024
1 parent 5aea81f commit 39ac743
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
27 changes: 24 additions & 3 deletions client/lib/test/UnitTestCommonBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ void UnitTestCommonBase::unitTestClientInputMessage(CC_MqttsnClient* client, con
unitTestClientInputData(client, data);
}

void UnitTestCommonBase::unitTestPushSearchgwResponseDelay(unsigned val)
{
m_data.m_searchgwResponseDelays.push_back(val);
}

bool UnitTestCommonBase::unitTestHasTickReq() const
{
return !m_data.m_ticks.empty();
Expand Down Expand Up @@ -658,6 +663,16 @@ CC_MqttsnErrorCode UnitTestCommonBase::apiSetVerifyIncomingMsgSubscribed(CC_Mqtt
return m_funcs.m_set_verify_incoming_msg_subscribed(client, enabled);
}

void UnitTestCommonBase::apiInitGatewayInfo(CC_MqttsnGatewayInfo* info)
{
m_funcs.m_init_gateway_info(info);
}

CC_MqttsnErrorCode UnitTestCommonBase::apiSetAvailableGatewayInfo(CC_MqttsnClient* client, const CC_MqttsnGatewayInfo* info)
{
return m_funcs.m_set_available_gateway_info(client, info);
}

CC_MqttsnSearchHandle UnitTestCommonBase::apiSearchPrepare(CC_MqttsnClient* client, CC_MqttsnErrorCode* ec)
{
return m_funcs.m_search_prepare(client, ec);
Expand Down Expand Up @@ -821,9 +836,15 @@ void UnitTestCommonBase::unitTestMessageReportCb(void* data, const CC_MqttsnMess

unsigned UnitTestCommonBase::unitTestGwinfoDelayRequestCb(void* data)
{
// TODO:
static_cast<void>(data);
test_assert(false);
auto* thisPtr = asThis(data);
test_assert(!thisPtr->m_data.m_searchgwResponseDelays.empty());
if (thisPtr->m_data.m_searchgwResponseDelays.empty()) {
return 0U;
}

auto result = thisPtr->m_data.m_searchgwResponseDelays.front();
thisPtr->m_data.m_searchgwResponseDelays.pop_front();
return result;
}

void UnitTestCommonBase::unitTestErrorLogCb([[maybe_unused]] void* data, const char* msg)
Expand Down
5 changes: 5 additions & 0 deletions client/lib/test/UnitTestCommonBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ class UnitTestCommonBase

using UnitTestSearchCompleteCb = std::function<bool (const UnitTestSearchCompleteReport& info)>;
using UnitTestSearchCompleteCbList = std::list<UnitTestSearchCompleteCb>;
using UnitTestSearchgwResponseDelayList = std::list<unsigned>;

struct UnitTestConnectInfo
{
Expand Down Expand Up @@ -266,6 +267,7 @@ class UnitTestCommonBase
UnitTestClientPtr unitTestAllocClient(bool enableLog = false);
void unitTestClientInputData(CC_MqttsnClient* client, const UnitTestData& data);
void unitTestClientInputMessage(CC_MqttsnClient* client, const UnitTestMessage& msg);
void unitTestPushSearchgwResponseDelay(unsigned val);

bool unitTestHasTickReq() const;
const UnitTestTickInfo* unitTestTickInfo(bool mustExist = true) const;
Expand Down Expand Up @@ -319,6 +321,8 @@ class UnitTestCommonBase
CC_MqttsnErrorCode apiSetDefaultRetryPeriod(CC_MqttsnClient* client, unsigned value);
CC_MqttsnErrorCode apiSetDefaultRetryCount(CC_MqttsnClient* client, unsigned value);
CC_MqttsnErrorCode apiSetVerifyIncomingMsgSubscribed(CC_MqttsnClient* client, bool enabled);
void apiInitGatewayInfo(CC_MqttsnGatewayInfo* info);
CC_MqttsnErrorCode apiSetAvailableGatewayInfo(CC_MqttsnClient* client, const CC_MqttsnGatewayInfo* info);

CC_MqttsnSearchHandle apiSearchPrepare(CC_MqttsnClient* client, CC_MqttsnErrorCode* ec = nullptr);
CC_MqttsnErrorCode apiSearchSetRetryPeriod(CC_MqttsnSearchHandle search, unsigned value);
Expand Down Expand Up @@ -361,6 +365,7 @@ class UnitTestCommonBase
UnitTestGwDisconnectReportsList m_gwDisconnectReports;
UnitTestSearchCompleteReportsList m_searchCompleteReports;
UnitTestSearchCompleteCbList m_searchCompleteCallbacks;
UnitTestSearchgwResponseDelayList m_searchgwResponseDelays;
UnitTestConnectCompleteReportList m_connectCompleteReports;
UnitTestDisconnectCompleteReportList m_disconnectCompleteReports;
UnitTestSubscribeCompleteReportList m_subscribeCompleteReports;
Expand Down
58 changes: 57 additions & 1 deletion client/lib/test/UnitTestGwDiscover.th
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public:
void test4();
void test5();
void test6();
void test7();

private:
virtual void setUp() override
Expand Down Expand Up @@ -251,7 +252,6 @@ void UnitTestGwDiscover::test5()
TS_ASSERT(!unitTestHasTickReq());
}


void UnitTestGwDiscover::test6()
{
// Testing search with packet loss
Expand Down Expand Up @@ -320,4 +320,60 @@ void UnitTestGwDiscover::test6()
TS_ASSERT(gwInfoReport->m_info.m_addr.empty());

TS_ASSERT(unitTestHasTickReq()); // For the ADVERTISE
}

void UnitTestGwDiscover::test7()
{
// Testing response on behalf of other gateway

auto clientPtr = unitTestAllocClient();
auto* client = clientPtr.get();

const std::uint8_t GwId = 1U;
const unsigned AdvDurationMin = 10U;

{
UnitTestAdvertiseMsg advertiseMsg;
advertiseMsg.field_gwId().setValue(GwId);
comms::units::setMinutes(advertiseMsg.field_duration(), AdvDurationMin);
unitTestClientInputMessage(client, advertiseMsg);
}

{
auto gwInfoReport = unitTestGetGwInfoReport();
TS_ASSERT_EQUALS(gwInfoReport->m_status, CC_MqttsnGwStatus_AddedByGateway);
TS_ASSERT_EQUALS(gwInfoReport->m_info.m_gwId, GwId);
TS_ASSERT(gwInfoReport->m_info.m_addr.empty());
TS_ASSERT(!unitTestHasGwInfoReport());
}

const UnitTestData GwAddr = {1, 2, 3, 4};
CC_MqttsnGatewayInfo gwInfo;
apiInitGatewayInfo(&gwInfo);
gwInfo.m_gwId = GwId;
gwInfo.m_addr = GwAddr.data();
gwInfo.m_addrLen = static_cast<decltype(gwInfo.m_addrLen)>(GwAddr.size());
apiSetAvailableGatewayInfo(client, &gwInfo);

auto* tickReq = unitTestTickInfo();
TS_ASSERT_LESS_THAN(AdvDurationMin * 60 * 1000, tickReq->m_req); // Extra buffer after expiry is expected
unitTestTick(client, 1000);

unitTestPushSearchgwResponseDelay(1000);
const unsigned BroadcastRadius = 3;
{
UnitTestSearchgwMsg searchgwMsg;
searchgwMsg.field_radius().value() = BroadcastRadius;
unitTestClientInputMessage(client, searchgwMsg);
}

unitTestTick(client); // Tiemout to send GWINFO

{
auto sentMsg = unitTestPopOutputMessage();
auto* gwinfoMsg = dynamic_cast<UnitTestGwinfoMsg*>(sentMsg.get());
TS_ASSERT_EQUALS(gwinfoMsg->field_gwId().value(), GwId);
TS_ASSERT_EQUALS(gwinfoMsg->field_gwAdd().value(), GwAddr);
}

}

0 comments on commit 39ac743

Please sign in to comment.