From 096e85b4663abeab22fae3b48ea0119714cb6563 Mon Sep 17 00:00:00 2001 From: Krzysztof Jackiewicz Date: Wed, 17 Jun 2015 12:55:53 +0200 Subject: [PATCH] CommunicationManager returns the number of called listeners [Problem] There's no way to find out if inter-service message reached some listeners. [Solution] SendMessage returns the number of called listeners. [Verification] Run ckm-tests-internal -t MESSAGE_MANAGER_TEST Change-Id: I0f9cba13991cb79e2901a6784a6b18e3b87c7150 --- src/manager/main/communication-manager.h | 14 ++++++++++---- tests/test_comm-manager.cpp | 21 ++++++++++----------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/manager/main/communication-manager.h b/src/manager/main/communication-manager.h index 898d483..1637a07 100644 --- a/src/manager/main/communication-manager.h +++ b/src/manager/main/communication-manager.h @@ -44,10 +44,15 @@ public: } // Sends message of type M to all registered listeners - void SendMessage(const M& msg) + // Returns the number of listeners called + size_t SendMessage(const M& msg) const { - for(auto it : m_listeners) + size_t num = 0; + for(auto& it : m_listeners) { it(msg); + num++; + } + return num; } protected: MessageManager() {} @@ -82,10 +87,11 @@ public: } // M message type + // Sending a message calls an unknown listener callback on the receiving side. It may throw. template - void SendMessage(const M& msg) + size_t SendMessage(const M& msg) const { - MessageManager::SendMessage(msg); + return MessageManager::SendMessage(msg); } }; diff --git a/tests/test_comm-manager.cpp b/tests/test_comm-manager.cpp index ec9ccaf..facd20b 100644 --- a/tests/test_comm-manager.cpp +++ b/tests/test_comm-manager.cpp @@ -63,15 +63,14 @@ BOOST_AUTO_TEST_SUITE(MESSAGE_MANAGER_TEST) BOOST_AUTO_TEST_CASE(TMM_0010_NoListener) { CKM::CommunicationManager mgr; - //int reci = 0; - mgr.SendMessage(MessageA(22)); + BOOST_REQUIRE_MESSAGE(0 == mgr.SendMessage(MessageA(22)), "There should be no listener."); } BOOST_AUTO_TEST_CASE(TMM_0020_Basic) { CKM::CommunicationManager mgr; int received = 0; mgr.Register([&](const MessageA& msg){ received = msg.i; }); - mgr.SendMessage(MessageA(4)); + BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageA(4)), "No listener found"); BOOST_REQUIRE_MESSAGE(received != 0, "Message not received"); BOOST_REQUIRE_MESSAGE(received == 4, "Wrong message received i=" << received); } @@ -82,12 +81,12 @@ BOOST_AUTO_TEST_CASE(TMM_0030_MultipleMessages) { char recc = 0; mgr.Register([&](const MessageA& msg){ reci = msg.i; }); mgr.Register([&](const MessageB& msg){ recc = msg.c; }); - mgr.SendMessage(MessageB('c')); + BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageB('c')), "No listener found"); BOOST_REQUIRE_MESSAGE(reci == 0, "Unexpected message received"); BOOST_REQUIRE_MESSAGE(recc != 0, "Message not received"); BOOST_REQUIRE_MESSAGE(recc == 'c', "Wrong message received c=" << recc); - mgr.SendMessage(MessageA(42)); + BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageA(42)), "No listener found"); BOOST_REQUIRE_MESSAGE(reci!= 0, "Message not received"); BOOST_REQUIRE_MESSAGE(reci == 42, "Wrong message received i=" << reci); BOOST_REQUIRE_MESSAGE(recc == 'c', "Previous message overwritten c=" << recc); @@ -99,12 +98,12 @@ BOOST_AUTO_TEST_CASE(TMM_0040_Listener) { mgr.Register([&](const MessageC& msg){ l.Handle(msg); }); mgr.Register([&](const MessageA& msg){ l.Handle(msg); }); - mgr.SendMessage(MessageC("lorem ipsum")); + BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageC("lorem ipsum")), "No listener found"); BOOST_REQUIRE_MESSAGE(l.i == 0, "Unexpected message received"); BOOST_REQUIRE_MESSAGE(!l.str.empty(), "Message not received"); BOOST_REQUIRE_MESSAGE(l.str == "lorem ipsum", "Wrong message received c=" << l.str); - mgr.SendMessage(MessageA(3)); + BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageA(3)), "No listener found"); BOOST_REQUIRE_MESSAGE(l.i!= 0, "Message not received"); BOOST_REQUIRE_MESSAGE(l.i == 3, "Wrong message received i=" << l.i); BOOST_REQUIRE_MESSAGE(l.str == "lorem ipsum", "Previous message overwritten str=" << l.str); @@ -124,7 +123,7 @@ BOOST_AUTO_TEST_CASE(TMM_0050_2Listeners) { called[1] = true; }); - mgr.SendMessage(MessageA(5)); + BOOST_REQUIRE_MESSAGE(2 == mgr.SendMessage(MessageA(5)), "No listener found"); BOOST_REQUIRE_MESSAGE(called[0], "First listener not called"); BOOST_REQUIRE_MESSAGE(called[1], "Second listener not called"); } @@ -159,15 +158,15 @@ BOOST_AUTO_TEST_CASE(TMM_0060_Stress) { switch(message_dist(generator)) { case 0: - mgr.SendMessage(MessageA(42)); + BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageA(42)), "No listener found"); a--; break; case 1: - mgr.SendMessage(MessageB('c')); + BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageB('c')), "No listener found"); b--; break; case 2: - mgr.SendMessage(MessageC("lorem ipsum")); + BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageC("lorem ipsum")), "No listener found"); c--; break; default: -- 2.7.4