Skip to content

Commit

Permalink
GH-382: Unsub from ToolsAPI events
Browse files Browse the repository at this point in the history
tool-api: Added: An ability to unsubscribe from either individual events or all events of requests. (GH-382)
  • Loading branch information
AnarManafov committed Mar 16, 2022
1 parent ce7066a commit ceaaf68
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Added: Support for SessionID (GH-411)

### dds-tools-api
Modified: Logs of user processes which use Tools API are moved now to the DDS root log directory, instead of sessions directory.
Added: An ability to unsubscribe from either individual events or all events of requests. (GH-382)

## v3.6 (2022-01-11)

Expand Down
29 changes: 29 additions & 0 deletions dds-tools-lib/src/ToolsProtocolCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,21 +275,50 @@ namespace dds
m_callbackResponse = _callbackResponse;
}

void unsubscribeResponseCallback()
{
m_callbackResponse = nullptr;
}

void setProgressCallback(callbackProgress_t _callbackProgress)
{
m_callbackProgress = _callbackProgress;
}

void unsubscribeProgressCallback()
{
m_callbackProgress = nullptr;
}

void setMessageCallback(callbackMessage_t _callbackMessage)
{
m_callbackMessage = _callbackMessage;
}

void unsubscribeMessageCallback()
{
m_callbackMessage = nullptr;
}

void setDoneCallback(callbackDone_t _callbackDone)
{
m_callbackDone = _callbackDone;
}

void unsubscribeDoneCallback()
{
m_callbackDone = nullptr;
}

/// \brief Use this method to unsubscribe from all events. of this request
void unsubscribeAll()
{
unsubscribeResponseCallback();
unsubscribeProgressCallback();
unsubscribeMessageCallback();
unsubscribeDoneCallback();
}

/// For tests or internal use
void execResponseCallback(const response_t& _arg)
{
Expand Down
55 changes: 55 additions & 0 deletions dds-tools-lib/tests/TestSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,4 +644,59 @@ BOOST_AUTO_TEST_CASE(test_dds_tools_agentCommand)
session.shutdown();
}

BOOST_AUTO_TEST_CASE(test_dds_tools_unsubscribe)
{
const std::chrono::seconds sleepTime(3);
const int tasksCount{ 9 };

CSession session;
boost::uuids::uuid sid = session.create();
BOOST_CHECK(!sid.is_nil());

// test onTaskDone events
// Subscrube on events
SOnTaskDoneRequest::request_t request;
SOnTaskDoneRequest::ptr_t requestPtr = SOnTaskDoneRequest::makeRequest(request);

int* testVarPtr = new int(0);

requestPtr->setResponseCallback([&testVarPtr](const SOnTaskDoneResponseData& /*_info*/)
{ BOOST_CHECK(testVarPtr != nullptr); });
BOOST_CHECK_NO_THROW(session.sendRequest<SOnTaskDoneRequest>(requestPtr));

// Unsubscribe from all events
requestPtr->unsubscribeAll();
// Delete the temp var to trigger an erroneous behaviour in case if unsubscribe didn't work
delete (testVarPtr);
testVarPtr = nullptr;

// Submit DDS agents
const fs::path topoPath(fs::canonical(fs::path("sleep_test.xml")));
CTopology topo(topoPath.string());
auto numAgents = topo.getRequiredNofAgents(tasksCount);
size_t requiredCount{ numAgents.first * numAgents.second };

SSubmitRequest::request_t submitInfo;
submitInfo.m_rms = "localhost";
submitInfo.m_slots = requiredCount;
submitInfo.m_instances = 0;
BOOST_CHECK_NO_THROW(session.syncSendRequest<SSubmitRequest>(submitInfo, kTimeout, &std::cout));

std::this_thread::sleep_for(sleepTime);
STopologyRequest::request_t topoInfo;
topoInfo.m_topologyFile = topoPath.string();
topoInfo.m_updateType = STopologyRequest::request_t::EUpdateType::ACTIVATE;
BOOST_CHECK_NO_THROW(session.syncSendRequest<STopologyRequest>(topoInfo, kTimeout, &std::cout));

std::this_thread::sleep_for(sleepTime);
topoInfo.m_topologyFile = "";
topoInfo.m_updateType = STopologyRequest::request_t::EUpdateType::STOP;
BOOST_CHECK_NO_THROW(session.syncSendRequest<STopologyRequest>(topoInfo, kTimeout, &std::cout));

// give it a chance to receive the OnTaskDone event
std::this_thread::sleep_for(sleepTime * 5);

session.shutdown();
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit ceaaf68

Please sign in to comment.