Browse Source

[1924] Test for the C++ want_answer parameter

Michal 'vorner' Vaner 12 years ago
parent
commit
e220a4ed92
3 changed files with 72 additions and 23 deletions
  1. 1 1
      src/lib/cc/session.cc
  2. 17 2
      src/lib/cc/session.h
  3. 54 20
      src/lib/cc/tests/session_unittests.cc

+ 1 - 1
src/lib/cc/session.cc

@@ -473,7 +473,7 @@ Session::unsubscribe(std::string group, std::string instance) {
 
 int
 Session::group_sendmsg(ConstElementPtr msg, std::string group,
-                       std::string instance, std::string to)
+                       std::string instance, std::string to, bool)
 {
     LOG_DEBUG(logger, DBG_TRACE_DETAILED, CC_GROUP_SEND).arg(msg->str()).
         arg(group);

+ 17 - 2
src/lib/cc/session.h

@@ -82,7 +82,8 @@ namespace isc {
             virtual int group_sendmsg(isc::data::ConstElementPtr msg,
                                       std::string group,
                                       std::string instance = "*",
-                                      std::string to = "*") = 0;
+                                      std::string to = "*",
+                                      bool want_answer = false) = 0;
             virtual bool group_recvmsg(isc::data::ConstElementPtr& envelope,
                                        isc::data::ConstElementPtr& msg,
                                        bool nonblock = true,
@@ -128,10 +129,24 @@ namespace isc {
                                    std::string instance = "*");
             virtual void unsubscribe(std::string group,
                              std::string instance = "*");
+            /// \brief Send a message to a group.
+            ///
+            /// \todo Can someone explain how the group, instance and to work?
+            ///     What is the desired semantics here?
+            /// \param msg The message to send.
+            /// \param group Part of addressing.
+            /// \param instance Part of addressing.
+            /// \param to Part of addressing.
+            /// \param want_answer Require an answer? If it is true and there's
+            ///     no recipient, the message queue answers by an error
+            ///     instead.
+            /// \return The squence number of the message sent. It can be used
+            ///     to wait for an answer by group_recvmsg.
             virtual int group_sendmsg(isc::data::ConstElementPtr msg,
                                       std::string group,
                                       std::string instance = "*",
-                                      std::string to = "*");
+                                      std::string to = "*",
+                                      bool want_answer = false);
             virtual bool group_recvmsg(isc::data::ConstElementPtr& envelope,
                                        isc::data::ConstElementPtr& msg,
                                        bool nonblock = true,

+ 54 - 20
src/lib/cc/tests/session_unittests.cc

@@ -40,6 +40,8 @@ using std::string;
 using isc::data::ConstElementPtr;
 using isc::data::Element;
 
+namespace {
+
 TEST(AsioSession, establish) {
     asio::io_service io_service_;
     Session sess(io_service_);
@@ -204,6 +206,7 @@ protected:
         alarm(0);
     }
 
+    // Check two elements are equal
     void elementsEqual(const ConstElementPtr& expected,
                        const ConstElementPtr& actual)
     {
@@ -212,6 +215,7 @@ protected:
             ", got: " << actual->toWire();
     }
 
+    // The same, but with one specified as string
     void elementsEqual(const string& expected,
                        const ConstElementPtr& actual)
     {
@@ -219,6 +223,17 @@ protected:
         elementsEqual(expected_el, actual);
     }
 
+    // Check the session sent a message with the given header. The
+    // message is hardcoded.
+    void checkSentMessage(const string& expected_hdr,
+                          const char* description)
+    {
+        SCOPED_TRACE(description);
+        const SentMessage msg(tds->readmsg());
+        elementsEqual(expected_hdr, msg.first);
+        elementsEqual("{\"test\": 42}", msg.second);
+    }
+
 public:
     // used in the handler test
     // This handler first reads (and ignores) whatever message caused
@@ -344,26 +359,45 @@ TEST_F(SessionTest, group_sendmsg) {
 
     const ConstElementPtr msg(Element::fromJSON("{\"test\": 42}"));
     sess.group_sendmsg(msg, "group");
-    const SentMessage m1(tds->readmsg());
-    elementsEqual("{"
-                  "   \"from\": \"foobar\","
-                  "   \"group\": \"group\","
-                  "   \"instance\": \"*\","
-                  "   \"seq\": 0,"
-                  "   \"to\": \"*\","
-                  "   \"type\": \"send\""
-                  "}", m1.first);
-    elementsEqual(msg, m1.second);
+    checkSentMessage("{"
+                     "   \"from\": \"foobar\","
+                     "   \"group\": \"group\","
+                     "   \"instance\": \"*\","
+                     "   \"seq\": 0,"
+                     "   \"to\": \"*\","
+                     "   \"type\": \"send\","
+                     "   \"want_answer\": False"
+                     "}", "No instance");
     sess.group_sendmsg(msg, "group", "instance", "recipient");
-    const SentMessage m2(tds->readmsg());
-    elementsEqual("{"
-                  "   \"from\": \"foobar\","
-                  "   \"group\": \"group\","
-                  "   \"instance\": \"instance\","
-                  "   \"seq\": 1,"
-                  "   \"to\": \"recipient\","
-                  "   \"type\": \"send\""
-                  "}", m2.first);
-    elementsEqual(msg, m2.second);
+    checkSentMessage("{"
+                     "   \"from\": \"foobar\","
+                     "   \"group\": \"group\","
+                     "   \"instance\": \"instance\","
+                     "   \"seq\": 1,"
+                     "   \"to\": \"recipient\","
+                     "   \"type\": \"send\","
+                     "   \"want_answer\": False"
+                     "}", "With instance");
+    sess.group_sendmsg(msg, "group", "*", "*", true);
+    checkSentMessage("{"
+                     "   \"from\": \"foobar\","
+                     "   \"group\": \"group\","
+                     "   \"instance\": \"*\","
+                     "   \"seq\": 2,"
+                     "   \"to\": \"*\","
+                     "   \"type\": \"send\","
+                     "   \"want_answer\": True"
+                     "}", "Want answer");
+    sess.group_sendmsg(msg, "group", "*", "*", false);
+    checkSentMessage("{"
+                     "   \"from\": \"foobar\","
+                     "   \"group\": \"group\","
+                     "   \"instance\": \"*\","
+                     "   \"seq\": 3,"
+                     "   \"to\": \"*\","
+                     "   \"type\": \"send\","
+                     "   \"want_answer\": False"
+                     "}", "Doesn't want answer");
 }
 
+}