Files
qt-migration-legatus/qt6-after/qt6-migrated-legatus/Lts/DDS/DdsUnitTests/function_topic_tests.cpp
T

241 lines
8.1 KiB
C++

#include "function_topic_tests.h"
#include <DDS/DDSConnector/publisher.h>
#include <DDS/DDSConnector/subscriber.h>
#include <Lts/Common/ProjectConfig/project_defines.h>
#include <NGLLogger/INGLLogger.h>
#include <QtTest/QTest>
#include <nglapplication/nglcoreapplication.h>
int main(int argc, char* argv[])
{
NGLCoreApplication app(argc, argv);
app.createLogger("UnitTests", "log4cxx_ddsUnitTests.props");
FunctionTopicTests* tc = new FunctionTopicTests;
int retCode = QTest::qExec(tc, argc, argv);
delete tc;
return retCode;
}
FunctionTopicTests::FunctionTopicTests()
: m_logger(NGL::NGLLoggerBootstrap::getNGLRootLogger().getNGLLogger("UnitTests.FunctionTopicTests"))
, m_mainThreadId(QThread::currentThreadId())
{
qRegisterMetaType<DdsUnitTests::FunctionReply>("DdsUnitTests::FunctionReply");
qRegisterMetaType<DdsUnitTests::FunctionReplyWithState>("DdsUnitTests::FunctionReplyWithState");
}
void FunctionTopicTests::initTestCase()
{
DEBUG_LOG(m_logger) << "FunctionTopicTests: initTestCase";
const int domainId = 0;
m_ddsConnectorClient = std::make_shared<DDSConnector::DdsConnector>(domainId, IDL::PartitionType::Global);
QVERIFY2(m_ddsConnectorClient->init(), "Initialisation of ddsconnector (Client) failed");
m_ddsConnectorClient->initSubscriber();
QVERIFY2(m_ddsConnectorClient->getSubscriber(), "Initialisation of Subscriber (DdsConnectorClient) failed");
m_ddsConnectorClient->initPublisher();
QVERIFY2(m_ddsConnectorClient->getPublisher(), "Initialisation of Publisher (DdsConnectorClient) failed");
m_ddsConnectorServer = std::make_shared<DDSConnector::DdsConnector>(domainId, IDL::PartitionType::Global);
QVERIFY2(m_ddsConnectorServer->init(), "Initialisation of ddsconnector (Server) failed");
m_ddsConnectorServer->initSubscriber();
QVERIFY2(m_ddsConnectorServer->getSubscriber(), "Initialisation of Subscriber (DdsConnectorServer) failed");
m_ddsConnectorServer->initPublisher();
QVERIFY2(m_ddsConnectorServer->getPublisher(), "Initialisation of Publisher (DdsConnectorServer) failed");
}
void FunctionTopicTests::initFTSTestCase()
{
m_fts.reset(m_ddsConnectorServer->createFTS<FTS::FunctionTopicTest>());
QVERIFY2(m_fts.get(), "Initialisation of FTS Replier failed");
m_fts->setSubscriberCallback(
[this](const rti::core::SampleIdentity& identity, const DdsUnitTests::FunctionRequest& request) {
this->onFunctionTopicRequest(identity, request);
});
m_ftsWS.reset(m_ddsConnectorServer->createFTS<FTS::FunctionTopicTestWithState>());
QVERIFY2(m_ftsWS.get(), "Initialisation of FTS Replier failed");
m_ftsWS->setSubscriberCallback(
[this](const rti::core::SampleIdentity& identity, const DdsUnitTests::FunctionRequestWithState& request) {
this->onFunctionTopicRequest(identity, request);
});
}
void FunctionTopicTests::initFTCTestCase()
{
m_ftc.reset(m_ddsConnectorClient->createFTC<FTC::FunctionTopicTest>(this));
QVERIFY2(m_ftc.get(), "Initialisation of FTC Requester failed");
m_ftcWS.reset(m_ddsConnectorClient->createFTC<FTC::FunctionTopicTestWithState>(this));
QVERIFY2(m_ftc.get(), "Initialisation of FTC Requester failed");
}
void FunctionTopicTests::requestTestCase1()
{
requestWithState();
QCOMPARE(m_ddsConnectorServer->getWaitSetHandler()->attachedConditionCount(), 3);
requestWithState();
QCOMPARE(m_ddsConnectorServer->getWaitSetHandler()->attachedConditionCount(), 3);
}
void FunctionTopicTests::requestWithState()
{
QSignalSpy spyRequest(this, &FunctionTopicTests::functionTopicRequestFinished);
QSignalSpy spyReply(this, &FunctionTopicTests::functionTopicReply);
DEBUG_LOG(m_logger) << "FunctionTopicTests: request";
DdsUnitTests::FunctionRequestWithState data;
data.id = 1;
data.info = std::string("Test request");
data.value = 1000.0;
int uniqueId = 0;
bool bRet = m_ftcWS->requestDataClassic(
data, SLOT(slotRequestedData(DdsUnitTests::FunctionTopicTestWithStateReply,int,bool)),
SLOT(slotRequestedDataError(int,int,int)), uniqueId);
QVERIFY2(bRet, "Could not request data!");
QTRY_COMPARE(spyRequest.count(), 1);
QTRY_COMPARE(spyReply.count(), 2);
}
void FunctionTopicTests::requestTestCase2()
{
QSignalSpy spyRequest(this, &FunctionTopicTests::functionTopicRequestFinished);
QSignalSpy spyReply(this, &FunctionTopicTests::functionTopicReply);
DEBUG_LOG(m_logger) << "FunctionTopicTests: request";
DdsUnitTests::FunctionRequest data;
data.id = 1;
data.info = std::string("Test request");
data.value = 1000.0;
int uniqueId = 0;
bool bRet =
m_ftc->requestDataClassic(data, SLOT(slotRequestedData(DdsUnitTests::FunctionTopicTestReply,int,bool)),
SLOT(slotRequestedDataError(int,int,int)), uniqueId);
QVERIFY2(bRet, "Could not request data!");
QTRY_COMPARE(spyRequest.count(), 1);
QTRY_COMPARE(spyReply.count(), 1);
}
void FunctionTopicTests::cleanupTestCase()
{
DEBUG_LOG(m_logger) << "FunctionTopicTests: cleanupTestCase";
m_fts = nullptr;
m_ftc = nullptr;
m_ftsWS = nullptr;
m_ftcWS = nullptr;
m_ddsConnectorServer = nullptr;
m_ddsConnectorClient = nullptr;
}
void FunctionTopicTests::onFunctionTopicRequest(const rti::core::SampleIdentity& sampleId,
const DdsUnitTests::FunctionRequestWithState& request)
{
QCOMPARE(m_mainThreadId, QThread::currentThreadId());
INFO_LOG(m_logger) << "Got function topic request (with status member)";
m_requestTopicWithState = request;
QCOMPARE(m_requestTopicWithState.id, 1);
QCOMPARE(m_requestTopicWithState.info.get(), std::string("Test request"));
QCOMPARE(m_requestTopicWithState.value, 1000.0);
DdsUnitTests::FunctionReplyWithState reply;
DEBUG_LOG(m_logger) << "Send sample with status REPLY_COMPLETED";
reply.id = 1;
reply.errorString = "Hello";
reply.replyState = Core::FtReplyState::Completed;
bool bRet = m_ftsWS->sendReply(sampleId, reply);
QVERIFY2(bRet, "Sending Reply failed");
emit functionTopicRequestFinished();
}
void FunctionTopicTests::onFunctionTopicRequest(const rti::core::SampleIdentity& sampleId,
const DdsUnitTests::FunctionRequest& request)
{
QCOMPARE(m_mainThreadId, QThread::currentThreadId());
INFO_LOG(m_logger) << "Got function topic request (without status member)";
m_requestTopic = request;
DdsUnitTests::FunctionReply reply;
reply.id = 2;
reply.errorString = "What?";
bool bRet = m_fts->sendReply(sampleId, reply);
QVERIFY2(bRet, "Sending Reply failed");
emit functionTopicRequestFinished();
}
void FunctionTopicTests::slotRequestedData(const DdsUnitTests::FunctionTopicTestWithStateReply& reply, int seqNo,
bool bFinished)
{
Q_UNUSED(seqNo);
QCOMPARE(m_mainThreadId, QThread::currentThreadId());
INFO_LOG(m_logger) << "Got reply from FTS: " << reply.replyState;
m_replyTopicWithState = reply;
if (m_replyTopicWithState.replyState == Core::FtReplyState::InProgress)
{
QVERIFY(!bFinished);
QCOMPARE(m_replyTopicWithState.id, 0);
}
else
{
QVERIFY(bFinished);
QCOMPARE(m_replyTopicWithState.id, 1);
QCOMPARE(m_replyTopicWithState.errorString, std::string("Hello"));
QCOMPARE(static_cast<int>(m_replyTopicWithState.replyState), static_cast<int>(Core::FtReplyState::Completed));
}
emit functionTopicReply();
}
void FunctionTopicTests::slotRequestedData(const DdsUnitTests::FunctionTopicTestReply& reply, int seqNo, bool bFinished)
{
Q_UNUSED(seqNo);
QVERIFY(bFinished);
QCOMPARE(m_mainThreadId, QThread::currentThreadId());
m_replyTopic = reply;
QCOMPARE(m_replyTopic.id, 2);
QCOMPARE(m_replyTopic.errorString, std::string("What?"));
emit functionTopicReply();
}
void FunctionTopicTests::slotRequestedDataError(int reason, int seqNo, int requestId)
{
Q_UNUSED(seqNo);
Q_UNUSED(requestId);
QFAIL(qPrintable(QStringLiteral("Error when trying to request data: %1").arg(reason)));
}