Browse Source

initial timeout test

git-svn-id: svn://bind10.isc.org/svn/bind10/branches/trac296@2647 e5f2f494-b856-4b98-b285-d166d9295462
Jelte Jansen 15 years ago
parent
commit
61fb173df9
2 changed files with 142 additions and 2 deletions
  1. 1 1
      src/lib/cc/session.cc
  2. 141 1
      src/lib/cc/session_unittests.cc

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

@@ -250,7 +250,7 @@ Session::establish(const char* socket_file) {
     }
 
     impl_->establish(*socket_file);
-
+    
     // once established, encapsulate the implementation object so that we
     // can safely release the internal resource when exception happens
     // below.

+ 141 - 1
src/lib/cc/session_unittests.cc

@@ -24,8 +24,12 @@
 #include <gtest/gtest.h>
 #include <session.h>
 
+#include <cc/data.h>
 #include <exceptions/exceptions.h>
 
+#include <asio.hpp>
+#include <boost/bind.hpp>
+
 using namespace isc::cc;
 
 TEST(AsioSession, establish) {
@@ -45,5 +49,141 @@ TEST(AsioSession, establish) {
                        "/aaaaaaaaaa/aaaaaaaaaa/aaaaaaaaaa/aaaaaaaaaa/"
                   ), isc::cc::SessionError
     );
-                  
 }
+
+
+void
+my_data_handler_no_answer(const asio::error_code& error, size_t bytes_transferred) {
+    std::cout << "[XX] got data" << std::endl;
+}
+
+void
+my_accept_handler(const asio::error_code& error) {
+    std::cout << "[XX] accepted" << std::endl;
+}
+
+/*
+class TestDomainSocket {
+
+public:
+    TestDomainSocket(asio::io_service& io_service, const char* file) : io_service_(io_service).
+                                                                       ep_(file),
+                                                                       acceptor_(io_service_, ep_),
+                                                                       socket_(io_service_) {
+        acceptor_.async_accept(socket_,
+                               boost::bind(&TestDomainSocket::acceptHandler,
+                                           this, _1));
+    }
+    
+    ~TestDomainSocket() {
+        std::cout << "[XX] destroy test domain socket" << std::endl;
+    }
+
+    void
+    start(const char* file)
+    {
+    }
+
+    void
+    acceptHandler(const asio::error_code& error) {
+        std::cout << "[XX] accepted" << std::endl;
+    }
+    
+    
+    void
+    my_data_handler_echo(const asio::error_code& error, size_t bytes_transferred, asio::local::stream_protocol::socket& socket) {
+        char data[4] = {0x01, 0x02, 0x03, 0x04};
+        socket.send(asio::buffer(data, 4));
+    }
+
+    void
+    close() {
+        //socket_.close();
+    }
+
+private:
+    asio::io_service& io_service_;
+    asio::local::stream_protocol::endpoint ep_;
+    asio::local::stream_protocol::acceptor acceptor_;
+    asio::local::stream_protocol::socket socket_;
+};
+*/
+
+class TestDomainSocket {
+
+public:
+    TestDomainSocket(asio::io_service& io_service, const char* file) : io_service_(io_service),
+                                                                       ep_(file),
+                                                                       acceptor_(io_service_, ep_),
+                                                                       socket_(io_service_)
+    {
+        acceptor_.async_accept(socket_,
+                               boost::bind(&TestDomainSocket::acceptHandler,
+                                           this, _1));
+    }
+    
+    ~TestDomainSocket() {
+        socket_.close();
+    }
+
+    void
+    acceptHandler(const asio::error_code& error) {
+    }
+
+    void
+    sendmsg(isc::data::ElementPtr& env, isc::data::ElementPtr& msg) {
+        std::string header_wire = env->toWire();
+        std::string body_wire = msg->toWire();
+        unsigned int length = 2 + header_wire.length() + body_wire.length();
+        unsigned int length_net = htonl(length);
+        unsigned short header_length = header_wire.length();
+        unsigned short header_length_net = htons(header_length);
+    
+        socket_.send(asio::buffer(&length_net, sizeof(length_net)));
+        socket_.send(asio::buffer(&header_length_net, sizeof(header_length_net)));
+        socket_.send(asio::buffer(header_wire.data(), header_length));
+        socket_.send(asio::buffer(body_wire.data(), body_wire.length()));
+    }
+
+    void
+    sendLname() {
+        isc::data::ElementPtr lname_answer1 = isc::data::Element::fromJSON("{ \"type\": \"lname\" }");
+        isc::data::ElementPtr lname_answer2 = isc::data::Element::fromJSON("{ \"lname\": \"foobar\" }");
+        sendmsg(lname_answer1, lname_answer2);
+    }
+
+    void
+    setSendLname() {
+        // ignore whatever data we get, send back an lname
+        asio::async_read(socket_,  asio::buffer(data_buf, 1024), boost::bind(&TestDomainSocket::sendLname, this));
+    }
+    
+private:
+    asio::io_service& io_service_;
+    asio::local::stream_protocol::endpoint ep_;
+    asio::local::stream_protocol::acceptor acceptor_;
+    asio::local::stream_protocol::socket socket_;
+    char data_buf[1024];
+};
+
+
+TEST(Session, timeout_on_connect) {
+    asio::io_service my_io_service;
+    ::unlink("/tmp/mysock.sock");
+    TestDomainSocket tds(my_io_service, "/tmp/mysock.sock");
+    Session sess(my_io_service);
+
+    // no answer, should timeout
+    EXPECT_THROW(sess.establish("/tmp/mysock.sock"), isc::cc::SessionTimeout);
+}
+
+TEST(Session, connect_ok) {
+    asio::io_service my_io_service;
+    ::unlink("/tmp/mysock.sock");
+    TestDomainSocket tds(my_io_service, "/tmp/mysock.sock");
+    tds.setSendLname();
+
+    Session sess(my_io_service);
+    sess.establish("/tmp/mysock.sock");
+}
+