Commit 72827eb1 authored by Marcin Siodelski's avatar Marcin Siodelski
Browse files

[5200] First attempt to address issues with the unix socket hangs.

parent 7ffa8f05
......@@ -19,7 +19,7 @@ TestServerUnixSocket::TestServerUnixSocket(IOService& io_service,
: io_service_(io_service),
server_endpoint_(socket_file_path),
server_acceptor_(io_service_.get_io_service()),
server_socket_(io_service_.get_io_service()),
server_sockets_(),
test_timer_(io_service_),
custom_response_(custom_response),
stop_after_count_(1),
......@@ -28,6 +28,12 @@ TestServerUnixSocket::TestServerUnixSocket(IOService& io_service,
test_timeout, IntervalTimer::ONE_SHOT);
}
TestServerUnixSocket::~TestServerUnixSocket() {
for (auto sock = server_sockets_.begin(); sock != server_sockets_.end(); ++sock) {
sock->close();
}
}
void
TestServerUnixSocket::bindServerSocket(const unsigned int stop_after_count) {
server_acceptor_.open();
......@@ -40,15 +46,16 @@ TestServerUnixSocket::bindServerSocket(const unsigned int stop_after_count) {
void
TestServerUnixSocket::acceptHandler(const boost::system::error_code&) {
server_socket_.async_read_some(boost::asio::buffer(&raw_buf_[0],
raw_buf_.size()),
boost::bind(&TestServerUnixSocket::
readHandler, this, _1, _2));
server_sockets_.back().async_read_some(boost::asio::buffer(&raw_buf_[0],
raw_buf_.size()),
boost::bind(&TestServerUnixSocket::readHandler, this, _1, _2));
}
void
TestServerUnixSocket::accept() {
server_acceptor_.async_accept(server_socket_,
server_sockets_.push_back(boost::asio::local::stream_protocol::
socket(io_service_.get_io_service()));
server_acceptor_.async_accept(server_sockets_.back(),
boost::bind(&TestServerUnixSocket::
acceptHandler, this, _1));
}
......@@ -58,20 +65,17 @@ void
TestServerUnixSocket::readHandler(const boost::system::error_code&,
size_t bytes_transferred) {
if (!custom_response_.empty()) {
boost::asio::write(server_socket_, boost::asio::buffer(custom_response_.c_str(),
custom_response_.size()));
boost::asio::write(server_sockets_.back(),
boost::asio::buffer(custom_response_.c_str(),
custom_response_.size()));
} else {
std::string received(&raw_buf_[0], bytes_transferred);
std::string response("received " + received);
boost::asio::write(server_socket_, boost::asio::buffer(response.c_str(),
response.size()));
boost::asio::write(server_sockets_.back(),
boost::asio::buffer(response.c_str(), response.size()));
}
// Close the connection as we might be expecting another connection over the
// same socket.
server_socket_.close();
// Stop IO service if we have reached the maximum number of read messages.
if (++read_count_ >= stop_after_count_) {
io_service_.stop();
......
......@@ -13,6 +13,7 @@
#include <boost/shared_ptr.hpp>
#include <gtest/gtest.h>
#include <array>
#include <list>
#include <string>
namespace isc {
......@@ -34,6 +35,11 @@ public:
const long test_timeout,
const std::string& custom_respons_ = "");
/// @brief Destructor.
///
/// Closes active connections.
~TestServerUnixSocket();
/// @brief Creates and binds server socket.
///
/// @param stop_after_count Number of received messages after which the
......@@ -70,8 +76,8 @@ private:
/// @brief Server acceptor.
boost::asio::local::stream_protocol::acceptor server_acceptor_;
/// @brief Server side unix domain socket.
boost::asio::local::stream_protocol::socket server_socket_;
/// @brief Server side unix domain sockets.
std::list<boost::asio::local::stream_protocol::socket> server_sockets_;
/// @brief Receive buffer.
std::array<char, 1024> raw_buf_;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment