Skip to content

Commit 27d42ee

Browse files
committed
Make both server starts throw on error.
There's an asymmetry between the error reporting in the asynchronous and synchronous server implementations. The update to the test makes sure the symmetry is enforced. Fixes cpp-netlib#166
1 parent 2e2e50b commit 27d42ee

File tree

2 files changed

+50
-29
lines changed

2 files changed

+50
-29
lines changed

boost/network/protocol/http/server/sync_server.hpp

+16-13
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,18 @@ namespace boost { namespace network { namespace http {
6868
boost::mutex listening_mutex_;
6969
bool listening_;
7070

71-
void handle_accept(boost::system::error_code const & ec) {
72-
if (!ec) {
73-
socket_options_base::socket_options(new_connection->socket());
74-
new_connection->start();
75-
new_connection.reset(new sync_connection<Tag,Handler>(service_, handler_));
76-
acceptor_.async_accept(new_connection->socket(),
77-
boost::bind(&sync_server_base<Tag,Handler>::handle_accept,
78-
this, boost::asio::placeholders::error));
79-
}
71+
void handle_accept(boost::system::error_code const& ec) {
72+
if (ec) {
73+
}
74+
socket_options_base::socket_options(new_connection->socket());
75+
new_connection->start();
76+
new_connection.reset(
77+
new sync_connection<Tag, Handler>(service_, handler_));
78+
acceptor_.async_accept(
79+
new_connection->socket(),
80+
boost::bind(&sync_server_base<Tag, Handler>::handle_accept,
81+
this,
82+
boost::asio::placeholders::error));
8083
}
8184

8285
void start_listening() {
@@ -87,24 +90,24 @@ namespace boost { namespace network { namespace http {
8790
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query, error);
8891
if (error) {
8992
BOOST_NETWORK_MESSAGE("Error resolving address: " << address_ << ':' << port_);
90-
return;
93+
boost::throw_exception(std::runtime_error("Error resolving address."));
9194
}
9295
tcp::endpoint endpoint = *endpoint_iterator;
9396
acceptor_.open(endpoint.protocol(), error);
9497
if (error) {
9598
BOOST_NETWORK_MESSAGE("Error opening socket: " << address_ << ':' << port_ << " -- reason: '" << error << '\'');
96-
return;
99+
boost::throw_exception(std::runtime_error("Error opening socket."));
97100
}
98101
socket_options_base::acceptor_options(acceptor_);
99102
acceptor_.bind(endpoint, error);
100103
if (error) {
101104
BOOST_NETWORK_MESSAGE("Error binding to socket: " << address_ << ':' << port_ << " -- reason: '" << error << '\'');
102-
return;
105+
boost::throw_exception(std::runtime_error("Error binding to socket."));
103106
}
104107
acceptor_.listen(tcp::socket::max_connections, error);
105108
if (error) {
106109
BOOST_NETWORK_MESSAGE("Error listening on socket: " << address_ << ':' << port_ << " -- reason: '" << error << '\'');
107-
return;
110+
boost::throw_exception(std::runtime_error("Error listening on socket."));
108111
}
109112
new_connection.reset(new sync_connection<Tag,Handler>(service_, handler_));
110113
acceptor_.async_accept(new_connection->socket(),

libs/network/test/http/server_constructor_test.cpp

+34-16
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,41 @@ struct dummy_async_handler {
3535
};
3636

3737
BOOST_AUTO_TEST_CASE(minimal_constructor) {
38-
dummy_sync_handler sync_handler;
39-
dummy_async_handler async_handler;
40-
41-
sync_server::options sync_options(sync_handler);
42-
async_server::options async_options(async_handler);
43-
BOOST_CHECK_NO_THROW(sync_server sync_instance(sync_options.address("127.0.0.1").port("80")) );
44-
BOOST_CHECK_NO_THROW(async_server async_instance(async_options.address("127.0.0.1").port("80")) );
38+
dummy_sync_handler sync_handler;
39+
dummy_async_handler async_handler;
40+
sync_server::options sync_options(sync_handler);
41+
async_server::options async_options(async_handler);
42+
BOOST_CHECK_NO_THROW(
43+
sync_server sync_instance(sync_options.address("127.0.0.1").port("80")));
44+
BOOST_CHECK_NO_THROW(async_server async_instance(
45+
async_options.address("127.0.0.1").port("80")));
4546
}
4647

4748
BOOST_AUTO_TEST_CASE(with_io_service_parameter) {
48-
dummy_sync_handler sync_handler;
49-
dummy_async_handler async_handler;
50-
boost::shared_ptr<util::thread_pool> thread_pool;
51-
boost::shared_ptr<boost::asio::io_service> io_service;
52-
sync_server::options sync_options(sync_handler);
53-
async_server::options async_options(async_handler);
54-
55-
BOOST_CHECK_NO_THROW(sync_server sync_instance(sync_options.address("127.0.0.1").port("80").io_service(io_service).thread_pool(thread_pool)));
56-
BOOST_CHECK_NO_THROW(async_server async_instance(async_options.address("127.0.0.1").port("80").io_service(io_service).thread_pool(thread_pool)));
49+
dummy_sync_handler sync_handler;
50+
dummy_async_handler async_handler;
51+
boost::shared_ptr<util::thread_pool> thread_pool;
52+
boost::shared_ptr<boost::asio::io_service> io_service;
53+
sync_server::options sync_options(sync_handler);
54+
async_server::options async_options(async_handler);
55+
56+
BOOST_CHECK_NO_THROW(sync_server sync_instance(sync_options.address(
57+
"127.0.0.1").port("80").io_service(io_service).thread_pool(thread_pool)));
58+
BOOST_CHECK_NO_THROW(async_server async_instance(async_options.address(
59+
"127.0.0.1").port("80").io_service(io_service).thread_pool(thread_pool)));
60+
}
61+
62+
BOOST_AUTO_TEST_CASE(throws_on_failure) {
63+
dummy_sync_handler sync_handler;
64+
dummy_async_handler async_handler;
65+
boost::shared_ptr<util::thread_pool> thread_pool;
66+
boost::shared_ptr<boost::asio::io_service> io_service;
67+
sync_server::options sync_options(sync_handler);
68+
async_server::options async_options(async_handler);
69+
sync_server sync_instance(sync_options.address("127.0.0.1").port(
70+
"80").io_service(io_service).thread_pool(thread_pool));
71+
async_server async_instance(async_options.address("127.0.0.1").port(
72+
"80").io_service(io_service).thread_pool(thread_pool));
73+
BOOST_CHECK_THROW(sync_instance.run(), std::runtime_error);
74+
BOOST_CHECK_THROW(async_instance.run(), std::runtime_error);
5775
}

0 commit comments

Comments
 (0)