/// @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);
#include <utility>
#include <vector>
+#include <list>
#include <string>
#include <iostream>
using namespace isc::cc;
using std::pair;
using std::vector;
+using std::list;
using std::string;
using isc::data::ConstElementPtr;
using isc::data::Element;
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<SentMessage> sent_messages_;
+};
+
class SessionTest : public ::testing::Test {
protected:
SessionTest() : sess(my_io_service), work(my_io_service) {
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);
}
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;
};
// 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");