#include "function_topic_tests.h" #include #include #include #include #include #include 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_mainThreadId(QThread::currentThreadId()) , m_logger(NGL::NGLLoggerBootstrap::getNGLRootLogger().getNGLLogger("UnitTests.FunctionTopicTests")) { qRegisterMetaType("DdsUnitTests::FunctionReply"); qRegisterMetaType("DdsUnitTests::FunctionReplyWithState"); } void FunctionTopicTests::initTestCase() { DEBUG_LOG(m_logger) <<"FunctionTopicTests: initTestCase"; const int domainId = 0; m_ddsConnectorClient = std::make_shared(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(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()); 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()); 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(this)); QVERIFY2(m_ftc.get(), "Initialisation of FTC Requester failed"); m_ftcWS.reset(m_ddsConnectorClient->createFTC(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(m_replyTopicWithState.replyState().underlying(), 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))); }