From: Michal 'vorner' Vaner Date: Wed, 6 Feb 2013 16:16:41 +0000 (+0100) Subject: [1924] Don't use real network IO in the test X-Git-Tag: bind10-1.1.0beta1-release~104^2~22^2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2a723e7e4bfe970673f1ea0a61262d1ba0a475d9;p=thirdparty%2Fkea.git [1924] Don't use real network IO in the test Mock the low-level sending method and don't rely on network IO to read it from the other end. TODO: Remove the support for reading the other end. --- diff --git a/src/lib/cc/session.h b/src/lib/cc/session.h index de01c09397..64baa7394b 100644 --- a/src/lib/cc/session.h +++ b/src/lib/cc/session.h @@ -166,9 +166,14 @@ namespace isc { /// @param returns socket descriptor used for session connection virtual int getSocketDesc() const; private: - void sendmsg(isc::data::ConstElementPtr msg); - void sendmsg(isc::data::ConstElementPtr env, - isc::data::ConstElementPtr msg); + // The following two methods are virtual to allow tests steal and + // replace them. It is not expected to be specialized by a derived + // class. Actually, it is not expected to inherit from this class + // to begin with. + virtual void sendmsg(isc::data::ConstElementPtr msg); + virtual void sendmsg(isc::data::ConstElementPtr env, + isc::data::ConstElementPtr msg); + bool recvmsg(isc::data::ConstElementPtr& msg, bool nonblock = true, int seq = -1); diff --git a/src/lib/cc/tests/session_unittests.cc b/src/lib/cc/tests/session_unittests.cc index eb7101ac05..0f0fd335e6 100644 --- a/src/lib/cc/tests/session_unittests.cc +++ b/src/lib/cc/tests/session_unittests.cc @@ -30,12 +30,14 @@ #include #include +#include #include #include using namespace isc::cc; using std::pair; using std::vector; +using std::list; using std::string; using isc::data::ConstElementPtr; using isc::data::Element; @@ -178,6 +180,30 @@ private: char data_buf[1024]; }; +// We specialize the tested class a little. We replace some low-level +// methods so we can examine the rest without relying on real network IO +class TestSession : public Session { +public: + TestSession(asio::io_service& ioservice) : + Session(ioservice) + {} + SentMessage getSentMessage() { + assert(!sent_messages_.empty()); + SentMessage result(sent_messages_.front()); + sent_messages_.pop_front(); + return (result); + } +private: + virtual void sendmsg(ConstElementPtr msg) { + sendmsg(msg, ConstElementPtr(new isc::data::NullElement)); + } + virtual void sendmsg(ConstElementPtr env, ConstElementPtr msg) { + sent_messages_.push_back(SentMessage(env, msg)); + } + + list sent_messages_; +}; + class SessionTest : public ::testing::Test { protected: SessionTest() : sess(my_io_service), work(my_io_service) { @@ -229,7 +255,7 @@ protected: const char* description) { SCOPED_TRACE(description); - const SentMessage msg(tds->readmsg()); + const SentMessage &msg(sess.getSentMessage()); elementsEqual(expected_hdr, msg.first); elementsEqual("{\"test\": 42}", msg.second); } @@ -256,7 +282,7 @@ public: protected: asio::io_service my_io_service; TestDomainSocket* tds; - Session sess; + TestSession sess; // Keep run() from stopping right away by informing it it has work to do asio::io_service::work work; }; @@ -352,10 +378,12 @@ TEST_F(SessionTest, get_socket_descr) { // Test the group_sendmsg sends the correct data. TEST_F(SessionTest, group_sendmsg) { - // Connect + // Connect (to set the lname, so we can see it sets the from) tds->setSendLname(); sess.establish(BIND10_TEST_SOCKET_FILE); - elementsEqual("{\"type\": \"getlname\"}", tds->readmsg().first); + // Eat the "get_lname" message, so it doesn't confuse the + // test below. + sess.getSentMessage(); const ConstElementPtr msg(Element::fromJSON("{\"test\": 42}")); sess.group_sendmsg(msg, "group");