From: Marcin Siodelski Date: Mon, 2 Jan 2017 16:57:55 +0000 (+0100) Subject: [5094] Implemented TCPAcceptor::close. X-Git-Tag: trac5090_base~1^2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=66cc74a4cbaf2d8d8bc06972f6101c02767fc096;p=thirdparty%2Fkea.git [5094] Implemented TCPAcceptor::close. --- diff --git a/src/lib/asiolink/tcp_acceptor.h b/src/lib/asiolink/tcp_acceptor.h index a5f1630e3d..e9a1869290 100644 --- a/src/lib/asiolink/tcp_acceptor.h +++ b/src/lib/asiolink/tcp_acceptor.h @@ -115,6 +115,11 @@ public: return (acceptor_->is_open()); } + /// @brief Closes the acceptor. + void close() const { + acceptor_->close(); + } + private: /// @brief Underlying ASIO acceptor implementation. diff --git a/src/lib/asiolink/tests/tcp_acceptor_unittest.cc b/src/lib/asiolink/tests/tcp_acceptor_unittest.cc index 275252b69f..ac2c9c89c7 100644 --- a/src/lib/asiolink/tests/tcp_acceptor_unittest.cc +++ b/src/lib/asiolink/tests/tcp_acceptor_unittest.cc @@ -138,7 +138,7 @@ public: /// will be called. /// @param callback Callback function for the asyncAccept. explicit Acceptor(IOService& io_service, TestTCPAcceptor& acceptor, - const TCPAcceptorCallback& callback) + const TCPAcceptorCallback& callback) : socket_(io_service), acceptor_(acceptor), callback_(callback) { } @@ -194,7 +194,8 @@ public: asio_endpoint_(boost::asio::ip::address::from_string(SERVER_ADDRESS), SERVER_PORT), endpoint_(asio_endpoint_), test_timer_(io_service_), connections_(), - clients_(), connections_num_(0), max_connections_(1) { + clients_(), connections_num_(0), aborted_connections_num_(0), + max_connections_(1) { test_timer_.setup(boost::bind(&TCPAcceptorTest::timeoutHandler, this), TEST_TIMEOUT, IntervalTimer::ONE_SHOT); } @@ -260,18 +261,22 @@ public: /// @param ec Error code. void acceptHandler(const boost::system::error_code& ec) { if (ec) { - ADD_FAILURE() << "error occurred while accepting connection: " - << ec.message(); + if (ec != boost::asio::error::operation_aborted) { + ADD_FAILURE() << "error occurred while accepting connection: " + << ec.message(); + } else { + ++aborted_connections_num_; + } io_service_.stop(); } // We have reached the maximum number of connections - end the test. if (++connections_num_ >= max_connections_) { io_service_.stop(); - - } else { - accept(); + return; } + + accept(); } /// @brief Callback function invoke upon test timeout. @@ -306,6 +311,9 @@ public: /// @brief Current number of established connections. unsigned int connections_num_; + /// @brief Current number of aborted connections. + unsigned int aborted_connections_num_; + /// @brief Connections limit. unsigned int max_connections_; }; @@ -383,4 +391,32 @@ TEST_F(TCPAcceptorTest, getNative) { EXPECT_GE(acceptor_.getNative(), 0); } + +// Test that TCPAcceptor::close works properly. +TEST_F(TCPAcceptorTest, close) { + // Initialize acceptor. + acceptor_.open(endpoint_); + acceptor_.bind(endpoint_); + acceptor_.listen(); + + // Start accepting new connections. + accept(); + + // Create 10 new TCP connections (client side). + for (unsigned int i = 0; i < 10; ++i) { + connect(); + } + + // Close the acceptor before connections are accepted. + acceptor_.close(); + + // Run the IO service. + io_service_.run(); + + // The connections should have been aborted. + EXPECT_EQ(1, connections_num_); + EXPECT_EQ(1, aborted_connections_num_); + EXPECT_EQ(1, connections_.size()); +} + }