Commit fa0ac913 authored by Ian Roddis's avatar Ian Roddis

Merge branch 'master' of http://github.com/oktal/pistache into streaming_test

parents d90b9f58 d59eee7b
......@@ -59,6 +59,7 @@ namespace Const {
static constexpr size_t MaxBacklog = 128;
static constexpr size_t MaxEvents = 1024;
static constexpr size_t MaxBuffer = 4096;
static constexpr size_t DefaultWorkers = 1;
// Defined from CMakeLists.txt in project root
static constexpr size_t DefaultMaxPayload = 4096;
......
......@@ -42,7 +42,7 @@ public:
Listener();
~Listener();
Listener(const Address& address);
explicit Listener(const Address& address);
void init(
size_t workers,
Flags<Options> options = Options::None,
......@@ -80,7 +80,7 @@ private:
std::shared_ptr<Transport> transport_;
std::shared_ptr<Handler> handler_;
std::shared_ptr<Aio::Reactor> reactor_;
Aio::Reactor reactor_;
Aio::Reactor::Key transportKey;
void handleNewConnection();
......
......@@ -74,16 +74,27 @@ void setSocketOptions(Fd fd, Flags<Options> options) {
}
Listener::Listener()
: listen_fd(-1)
: addr_()
, listen_fd(-1)
, backlog_(Const::MaxBacklog)
, reactor_(Aio::Reactor::create())
, shutdownFd()
, poller()
, options_()
, workers_(Const::DefaultWorkers)
, reactor_()
, transportKey()
{ }
Listener::Listener(const Address& address)
: addr_(address)
, listen_fd(-1)
, backlog_(Const::MaxBacklog)
, reactor_(Aio::Reactor::create())
, shutdownFd()
, poller()
, options_()
, workers_(Const::DefaultWorkers)
, reactor_()
, transportKey()
{
}
......@@ -190,8 +201,8 @@ Listener::bind(const Address& address) {
transport_.reset(new Transport(handler_));
reactor_->init(Aio::AsyncContext(workers_));
transportKey = reactor_->addHandler(transport_);
reactor_.init(Aio::AsyncContext(workers_));
transportKey = reactor_.addHandler(transport_);
return true;
}
......@@ -203,7 +214,7 @@ Listener::isBound() const {
void
Listener::run() {
reactor_->run();
reactor_.run();
for (;;) {
std::vector<Polling::Event> events;
......@@ -238,12 +249,12 @@ Listener::runThreaded() {
void
Listener::shutdown() {
if (shutdownFd.isBound()) shutdownFd.notify();
reactor_->shutdown();
reactor_.shutdown();
}
Async::Promise<Listener::Load>
Listener::requestLoad(const Listener::Load& old) {
auto handlers = reactor_->handlers(transportKey);
auto handlers = reactor_.handlers(transportKey);
std::vector<Async::Promise<rusage>> loads;
for (const auto& handler: handlers) {
......@@ -321,7 +332,7 @@ Listener::handleNewConnection() {
void
Listener::dispatchPeer(const std::shared_ptr<Peer>& peer) {
auto handlers = reactor_->handlers(transportKey);
auto handlers = reactor_.handlers(transportKey);
auto idx = peer->fd() % handlers.size();
auto transport = std::static_pointer_cast<Transport>(handlers[idx]);
......
......@@ -109,7 +109,7 @@ TEST(listener_test, listener_bind_port_free) {
if (port_nb == 0) {
FAIL() << "Could not find a free port. Abord test.\n";
FAIL() << "Could not find a free port. Abort test.\n";
}
Pistache::Port port(port_nb);
......@@ -123,6 +123,34 @@ TEST(listener_test, listener_bind_port_free) {
ASSERT_TRUE(true);
}
// Listener should not crash if an additional member is added to the listener class. This test
// is there to prevent regression for PR 303
TEST(listener_test, listener_uses_default) {
uint16_t port_nb;
// This is just done to get the value of a free port. The socket will be closed
// after the closing curly bracket and the port will be free again (SO_REUSEADDR option).
// In theory, it is possible that some application grab this port before we bind it again...
{
SocketWrapper s = bind_free_port();
port_nb = s.port();
}
if (port_nb == 0) {
FAIL() << "Could not find a free port. Abort test.\n";
}
Pistache::Port port(port_nb);
Pistache::Address address(Pistache::Ipv4::any(), port);
Pistache::Tcp::Listener listener;
listener.setHandler(Pistache::Http::make_handler<DummyHandler>());
listener.bind(address);
ASSERT_TRUE(true);
}
TEST(listener_test, listener_bind_port_not_free_throw_runtime) {
......@@ -130,7 +158,7 @@ TEST(listener_test, listener_bind_port_not_free_throw_runtime) {
uint16_t port_nb = s.port();
if (port_nb == 0) {
FAIL() << "Could not find a free port. Abord test.\n";
FAIL() << "Could not find a free port. Abort test.\n";
}
Pistache::Port port(port_nb);
......
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