From: Razvan Becheriu Date: Mon, 17 May 2021 11:16:20 +0000 (+0300) Subject: [#1818] make http thread pool state private and add missing interface functions X-Git-Tag: Kea-1.9.8~81 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=78ff7f1a8eeffcbd33af8299a89b32e94ba82a52;p=thirdparty%2Fkea.git [#1818] make http thread pool state private and add missing interface functions --- diff --git a/src/hooks/dhcp/high_availability/tests/ha_mt_unittest.cc b/src/hooks/dhcp/high_availability/tests/ha_mt_unittest.cc index d6c046a1f7..8313e0ca3e 100644 --- a/src/hooks/dhcp/high_availability/tests/ha_mt_unittest.cc +++ b/src/hooks/dhcp/high_availability/tests/ha_mt_unittest.cc @@ -203,10 +203,12 @@ TEST_F(HAMtServiceTest, multiThreadingBasics) { // Client should exist but be stopped. ASSERT_TRUE(service->client_); ASSERT_TRUE(service->client_->isStopped()); + EXPECT_TRUE(service->client_->getThreadIOService()->stopped()); - // Listener should exist but be stopped.. + // Listener should exist but be stopped. ASSERT_TRUE(service->listener_); - ASSERT_TRUE(service->client_->isStopped()); + ASSERT_TRUE(service->listener_->isStopped()); + EXPECT_TRUE(service->listener_->getThreadIOService()->stopped()); // Start client and listener. ASSERT_NO_THROW_LOG(service->startClientAndListener()); @@ -402,6 +404,7 @@ TEST_F(HAMtServiceTest, multiThreadingConfigStartup) { EXPECT_TRUE(service->client_->isRunning()); EXPECT_TRUE(service->client_->getThreadIOService()); EXPECT_EQ(service->client_->getThreadPoolSize(), scenario.exp_client_threads_); + // Currently thread count should be the same as thread pool size. This might // change if we go to so some sort of dynamic thread instance management. EXPECT_EQ(service->client_->getThreadCount(), scenario.exp_client_threads_); @@ -415,6 +418,7 @@ TEST_F(HAMtServiceTest, multiThreadingConfigStartup) { // We should have a running listener with the expected number of threads. ASSERT_TRUE(service->listener_); EXPECT_TRUE(service->listener_->isRunning()); + ASSERT_TRUE(service->listener_->getThreadIOService()); EXPECT_EQ(service->listener_->getThreadPoolSize(), scenario.exp_listener_threads_); // Currently thread count should be the same as thread pool size. This might diff --git a/src/lib/asiolink/io_service.cc b/src/lib/asiolink/io_service.cc index 927a1b1ba9..aad5dc7070 100644 --- a/src/lib/asiolink/io_service.cc +++ b/src/lib/asiolink/io_service.cc @@ -82,7 +82,9 @@ public: /// \brief Stop the underlying event loop. /// /// This will return the control to the caller of the \c run() method. - void stop() { io_service_.stop();} ; + void stop() { + io_service_.stop(); + } /// \brief Indicates if the IOService has been stopped. /// @@ -108,7 +110,9 @@ public: /// that share the same \c io_service with the authoritative server. /// It will eventually be removed once the wrapper interface is /// generalized. - boost::asio::io_service& get_io_service() { return io_service_; }; + boost::asio::io_service& get_io_service() { + return (io_service_); + } /// \brief Post a callback on the IO service /// @@ -117,6 +121,7 @@ public: const CallbackWrapper wrapper(callback); io_service_.post(wrapper); } + private: boost::asio::io_service io_service_; boost::shared_ptr work_; diff --git a/src/lib/config/cmd_http_listener.cc b/src/lib/config/cmd_http_listener.cc index 55cb2d856d..7063e17b4e 100644 --- a/src/lib/config/cmd_http_listener.cc +++ b/src/lib/config/cmd_http_listener.cc @@ -29,7 +29,7 @@ namespace config { CmdHttpListener::CmdHttpListener(const IOAddress& address, const uint16_t port, const uint16_t thread_pool_size /* = 1 */) : address_(address), port_(port), thread_io_service_(), http_listener_(), - thread_pool_size_(thread_pool_size), threads_() { + thread_pool_size_(thread_pool_size), thread_pool_() { } CmdHttpListener::~CmdHttpListener() { @@ -61,13 +61,12 @@ CmdHttpListener::start() { // Create the HTTP listener. It will open up a TCP socket and be // prepared to accept incoming connections. TlsContextPtr tls_context; - http_listener_.reset(new HttpListener(*thread_io_service_, address_, port_, - tls_context, rcf, + http_listener_.reset(new HttpListener(*thread_io_service_, address_, port_, tls_context, rcf, HttpListener::RequestTimeout(TIMEOUT_AGENT_RECEIVE_COMMAND), HttpListener::IdleTimeout(TIMEOUT_AGENT_IDLE_CONNECTION_TIMEOUT))); // Create the thread pool with immediate start. - threads_.reset(new HttpThreadPool(thread_io_service_, thread_pool_size_)); + thread_pool_.reset(new HttpThreadPool(thread_io_service_, thread_pool_size_)); // Instruct the HTTP listener to actually open socket, install // callback and start listening. @@ -85,15 +84,15 @@ CmdHttpListener::start() { void CmdHttpListener::pause() { - if (threads_) { - threads_->pause(); + if (thread_pool_) { + thread_pool_->pause(); } } void CmdHttpListener::resume() { - if (threads_) { - threads_->run(); + if (thread_pool_) { + thread_pool_->run(); } } @@ -109,7 +108,7 @@ CmdHttpListener::stop() { .arg(port_); // Stop the thread pool. - threads_->stop(); + thread_pool_->stop(); // Get rid of the listener. http_listener_.reset(); @@ -122,20 +121,10 @@ CmdHttpListener::stop() { .arg(port_); } -HttpThreadPool::RunState -CmdHttpListener::getRunState() const { - if (!threads_) { - isc_throw(InvalidOperation, - "CmdHttpListener::getRunState - no thread pool!"); - } - - return (threads_->getRunState()); -} - bool CmdHttpListener::isRunning() { - if (threads_) { - return (threads_->getRunState() == HttpThreadPool::RunState::RUNNING); + if (thread_pool_) { + return (thread_pool_->isRunning()); } return (false); @@ -143,8 +132,8 @@ CmdHttpListener::isRunning() { bool CmdHttpListener::isStopped() { - if (threads_) { - return (threads_->getRunState() == HttpThreadPool::RunState::STOPPED); + if (thread_pool_) { + return (thread_pool_->isStopped()); } return (true); @@ -152,8 +141,8 @@ CmdHttpListener::isStopped() { bool CmdHttpListener::isPaused() { - if (threads_) { - return (threads_->getRunState() == HttpThreadPool::RunState::PAUSED); + if (thread_pool_) { + return (thread_pool_->isPaused()); } return (false); diff --git a/src/lib/config/cmd_http_listener.h b/src/lib/config/cmd_http_listener.h index 0baacafbe6..0a1121b810 100644 --- a/src/lib/config/cmd_http_listener.h +++ b/src/lib/config/cmd_http_listener.h @@ -50,14 +50,9 @@ public: /// @brief Stops the listener's thread pool. void stop(); - /// @brief Fetches the run state of the thread pool. + /// @brief Indicates if the thread pool is running. /// - /// @return Run state of the pool. - http::HttpThreadPool::RunState getRunState() const; - - /// @brief Indicates if the thread pool processing is running. - /// - /// @return True if the thread pool exists and is in the RUN state, + /// @return True if the thread pool exists and is in the RUNNING state, /// false otherwise. bool isRunning(); @@ -98,11 +93,11 @@ public: /// /// @return uint16_t containing the number of running threads. uint16_t getThreadCount() const { - if (!threads_) { + if (!thread_pool_) { return (0); } - return (threads_->getThreadCount()); + return (thread_pool_->getThreadCount()); } asiolink::IOServicePtr getThreadIOService() const { @@ -126,7 +121,7 @@ private: std::size_t thread_pool_size_; /// @brief The pool of threads that do IO work. - http::HttpThreadPoolPtr threads_; + http::HttpThreadPoolPtr thread_pool_; }; /// @brief Defines a shared pointer to CmdHttpListener. diff --git a/src/lib/config/tests/cmd_http_listener_unittests.cc b/src/lib/config/tests/cmd_http_listener_unittests.cc index 13e12a0aa2..4abf781437 100644 --- a/src/lib/config/tests/cmd_http_listener_unittests.cc +++ b/src/lib/config/tests/cmd_http_listener_unittests.cc @@ -753,8 +753,6 @@ TEST_F(CmdHttpListenerTest, basics) { ASSERT_FALSE(listener_->getThreadIOService()); EXPECT_TRUE(listener_->isStopped()); EXPECT_EQ(listener_->getThreadCount(), 0); - ASSERT_THROW_MSG(listener_->getRunState(), InvalidOperation, - "CmdHttpListener::getRunState - no thread pool!"); // Verify that we cannot start it when multi-threading is disabled. ASSERT_FALSE(MultiThreadingMgr::instance().getMode()); @@ -775,7 +773,7 @@ TEST_F(CmdHttpListenerTest, basics) { EXPECT_EQ(listener_->getThreadCount(), 1); ASSERT_TRUE(listener_->getThreadIOService()); EXPECT_FALSE(listener_->getThreadIOService()->stopped()); - EXPECT_EQ(listener_->getRunState(), HttpThreadPool::RunState::RUNNING); + EXPECT_TRUE(listener_->isRunning()); // Trying to start it again should fail. ASSERT_THROW_MSG(listener_->start(), InvalidOperation, @@ -785,7 +783,7 @@ TEST_F(CmdHttpListenerTest, basics) { ASSERT_NO_THROW_LOG(listener_->stop()); ASSERT_TRUE(listener_->isStopped()); EXPECT_EQ(listener_->getThreadCount(), 0); - EXPECT_EQ(listener_->getRunState(), HttpThreadPool::RunState::STOPPED); + EXPECT_TRUE(listener_->isStopped()); ASSERT_FALSE(listener_->getThreadIOService()); // Make sure we can call stop again without problems. @@ -832,7 +830,7 @@ TEST_F(CmdHttpListenerTest, basics) { ASSERT_TRUE(listener_->isStopped()); EXPECT_EQ(listener_->getThreadCount(), 0); ASSERT_FALSE(listener_->getThreadIOService()); - EXPECT_EQ(listener_->getRunState(), HttpThreadPool::RunState::STOPPED); + EXPECT_TRUE(listener_->isStopped()); } // This test verifies that an HTTP connection can be established and used to diff --git a/src/lib/http/client.cc b/src/lib/http/client.cc index cc1c5d2345..48df1b87b6 100644 --- a/src/lib/http/client.cc +++ b/src/lib/http/client.cc @@ -1749,14 +1749,14 @@ public: /// is greater than zero. HttpClientImpl(IOService& io_service, size_t thread_pool_size = 0, bool defer_thread_start = false) - : thread_pool_size_(thread_pool_size), threads_() { + : thread_pool_size_(thread_pool_size), thread_pool_() { if (thread_pool_size_ > 0) { // Create our own private IOService. thread_io_service_.reset(new IOService()); // Create the thread pool. - threads_.reset(new HttpThreadPool(thread_io_service_, thread_pool_size_, - defer_thread_start)); + thread_pool_.reset(new HttpThreadPool(thread_io_service_, thread_pool_size_, + defer_thread_start)); // Create the connection pool. Note that we use the thread_pool_size // as the maximum connections per URL value. @@ -1780,8 +1780,8 @@ public: /// @brief Starts client's thread pool, if multi-threaded. void start() { - if (threads_) { - threads_->run(); + if (thread_pool_) { + thread_pool_->run(); } } @@ -1792,8 +1792,8 @@ public: conn_pool_->closeAll(); // Stop the thread pool. - if (threads_) { - threads_->stop(); + if (thread_pool_) { + thread_pool_->stop(); } } @@ -1801,35 +1801,24 @@ public: /// /// Suspends thread pool event processing. void pause() { - if (!threads_) { + if (!thread_pool_) { isc_throw(InvalidOperation, "HttpClient::pause - no thread pool"); } // Pause the thread pool. - threads_->pause(); + thread_pool_->pause(); } /// @brief Resumes the thread pool operation. /// /// Resumes thread pool event processing. void resume() { - if (!threads_) { + if (!thread_pool_) { isc_throw(InvalidOperation, "HttpClient::resume - no thread pool"); } // Resume running the thread pool. - threads_->run(); - } - - /// @brief Fetches the thread pool's run state. - /// - /// @return Operational state of the thread pool. - HttpThreadPool::RunState getRunState() const { - if (!threads_) { - isc_throw(InvalidOperation, "HttpClient::getRunState - no thread pool"); - } - - return (threads_->getRunState()); + thread_pool_->run(); } /// @brief Indicates if the thread pool processing is running. @@ -1837,8 +1826,8 @@ public: /// @return True if the thread pool exists and is in the RUNNING state, /// false otherwise. bool isRunning() { - if (threads_) { - return (threads_->getRunState() == HttpThreadPool::RunState::RUNNING); + if (thread_pool_) { + return (thread_pool_->isRunning()); } return (false); @@ -1849,8 +1838,8 @@ public: /// @return True if the thread pool exists and is in the STOPPED state, /// false otherwise bool isStopped() { - if (threads_) { - return (threads_->getRunState() == HttpThreadPool::RunState::STOPPED); + if (thread_pool_) { + return (thread_pool_->isStopped()); } return (false); @@ -1861,8 +1850,8 @@ public: /// @return True if the thread pool exists and is in the PAUSED state, /// false otherwise. bool isPaused() { - if (threads_) { - return (threads_->getRunState() == HttpThreadPool::RunState::PAUSED); + if (thread_pool_) { + return (thread_pool_->isPaused()); } return (false); @@ -1887,10 +1876,10 @@ public: /// /// @return the number of running threads. uint16_t getThreadCount() { - if (!threads_) { + if (!thread_pool_) { return (0); } - return (threads_->getThreadCount()); + return (thread_pool_->getThreadCount()); } /// @brief Holds a pointer to the connection pool. @@ -1906,7 +1895,7 @@ private: /// @brief Pool of threads used to service connections in multi-threaded /// mode. - HttpThreadPoolPtr threads_; + HttpThreadPoolPtr thread_pool_; }; HttpClient::HttpClient(IOService& io_service, size_t thread_pool_size, @@ -2002,11 +1991,6 @@ HttpClient::getThreadCount() const { return (impl_->getThreadCount()); } -HttpThreadPool::RunState -HttpClient::getRunState() const { - return (impl_->getRunState()); -} - bool HttpClient::isRunning() { return (impl_->isRunning()); diff --git a/src/lib/http/client.h b/src/lib/http/client.h index 2de0265f36..bfe85ab031 100644 --- a/src/lib/http/client.h +++ b/src/lib/http/client.h @@ -304,13 +304,7 @@ public: /// @return the number of running threads. uint16_t getThreadCount() const; - /// @brief Fetches the thread pool's run state. - /// - /// @return Operational state of the thread pool. - /// @throw InvalidOperation if the thread pool does not exist. - HttpThreadPool::RunState getRunState() const; - - /// @brief Indicates if the thread pool processing is running. + /// @brief Indicates if the thread pool is running. /// /// @return True if the thread pool exists and is in the RUNNING state, /// false otherwise. diff --git a/src/lib/http/http_thread_pool.cc b/src/lib/http/http_thread_pool.cc index a709cb80c4..288bb912f5 100644 --- a/src/lib/http/http_thread_pool.cc +++ b/src/lib/http/http_thread_pool.cc @@ -33,7 +33,7 @@ using namespace isc::util; HttpThreadPool::HttpThreadPool(IOServicePtr io_service, size_t pool_size, bool defer_start /* = false */) : pool_size_(pool_size), io_service_(io_service), - run_state_(RunState::STOPPED), mutex_(), thread_cv_(), + run_state_(State::STOPPED), mutex_(), thread_cv_(), main_cv_(), paused_(0), running_(0), exited_(0) { if (!pool_size) { isc_throw(BadValue, "HttpThreadPool::ctor pool_size must be > 0"); @@ -56,37 +56,37 @@ HttpThreadPool::~HttpThreadPool() { void HttpThreadPool::run() { - setRunState(RunState::RUNNING); + setState(State::RUNNING); } void HttpThreadPool::pause() { - setRunState(RunState::PAUSED); + setState(State::PAUSED); } void HttpThreadPool::stop() { - setRunState(RunState::STOPPED); + setState(State::STOPPED); } -HttpThreadPool::RunState -HttpThreadPool::getRunState() { +HttpThreadPool::State +HttpThreadPool::getState() { std::lock_guard lck(mutex_); return (run_state_); } bool -HttpThreadPool::validateStateChange(RunState new_state) const { +HttpThreadPool::validateStateChange(State new_state) const { bool is_valid = false; switch(run_state_) { - case RunState::STOPPED: - is_valid = (new_state == RunState::RUNNING); + case State::STOPPED: + is_valid = (new_state == State::RUNNING); break; - case RunState::RUNNING: - is_valid = (new_state != RunState::RUNNING); + case State::RUNNING: + is_valid = (new_state != State::RUNNING); break; - case RunState::PAUSED: - is_valid = (new_state != RunState::PAUSED); + case State::PAUSED: + is_valid = (new_state != State::PAUSED); break; } @@ -94,7 +94,7 @@ HttpThreadPool::validateStateChange(RunState new_state) const { } void -HttpThreadPool::setRunState (RunState new_state) { +HttpThreadPool::setState(State new_state) { std::unique_lock main_lck(mutex_); // Bail if the transition is invalid. @@ -107,7 +107,7 @@ HttpThreadPool::setRunState (RunState new_state) { thread_cv_.notify_all(); switch(new_state) { - case RunState::RUNNING: { + case State::RUNNING: { // Restart the IOService. io_service_->restart(); @@ -130,7 +130,7 @@ HttpThreadPool::setRunState (RunState new_state) { break; } - case RunState::PAUSED: { + case State::PAUSED: { // Stop IOService. if (!io_service_->stopped()) { io_service_->poll(); @@ -146,7 +146,7 @@ HttpThreadPool::setRunState (RunState new_state) { break; } - case RunState::STOPPED: { + case State::STOPPED: { // Stop IOService. if (!io_service_->stopped()) { io_service_->poll(); @@ -172,8 +172,8 @@ void HttpThreadPool::threadWork() { bool done = false; while (!done) { - switch (getRunState()) { - case RunState::RUNNING: { + switch (getState()) { + case State::RUNNING: { { std::unique_lock lck(mutex_); running_++; @@ -195,7 +195,7 @@ HttpThreadPool::threadWork() { break; } - case RunState::PAUSED: { + case State::PAUSED: { std::unique_lock lck(mutex_); paused_++; @@ -207,14 +207,14 @@ HttpThreadPool::threadWork() { // Wait here till I'm released. thread_cv_.wait(lck, [&]() { - return (run_state_ != RunState::PAUSED); + return (run_state_ != State::PAUSED); }); paused_--; break; } - case RunState::STOPPED: { + case State::STOPPED: { done = true; break; }} diff --git a/src/lib/http/http_thread_pool.h b/src/lib/http/http_thread_pool.h index e3bb91dc87..8f7dba5914 100644 --- a/src/lib/http/http_thread_pool.h +++ b/src/lib/http/http_thread_pool.h @@ -23,8 +23,8 @@ namespace http { /// @brief Implements a pausable pool of IOService driven threads. class HttpThreadPool { public: - /// @brief Describes the possible operational state of the pool. - enum class RunState { + /// @brief Describes the possible operational state of the thread pool. + enum class State { STOPPED, /// Pool is not operational. RUNNING, /// Pool is populated with running threads. PAUSED, /// Pool is populated with threads that are paused. @@ -45,19 +45,19 @@ public: /// @brief Destructor /// - /// Ensures the pool is stopped prior to destruction. + /// Ensures the thread pool is stopped prior to destruction. ~HttpThreadPool(); /// @brief Transitions the pool from STOPPED or PAUSED to RUNNING. /// - /// When called from the STOPPED state, the pool threads are created + /// When called from the STOPPED state, the pool threads are created and /// begin processing events. /// When called from the PAUSED state, the pool threads are released - /// from PAUSED and resume processing event.s + /// from PAUSED and resume processing events. /// Has no effect if the pool is already in the RUNNING state. void run(); - /// @brief Transitions the pool from RUNNING to PAUSED state. + /// @brief Transitions the pool from RUNNING to PAUSED. /// /// Pool threads suspend event processing and pause until they /// are released to either resume running or stop. @@ -71,10 +71,26 @@ public: /// Has no effect if the pool is already in the STOPPED state. void stop(); - /// @brief Thread-safe fetch of the pool's operational state. + /// @brief Check if the thread pool is running. + /// + /// @return True if the thread pool is running, false otherwise. + bool isRunning() { + return (getState() == State::RUNNING); + } + + /// @brief Check if the thread pool is paused. + /// + /// @return True if the thread pool is paused, false otherwise. + bool isPaused() { + return (getState() == State::PAUSED); + } + + /// @brief Check if the thread pool is stopped. /// - /// @return Pool run state. - RunState getRunState(); + /// @return True if the thread pool is stopped, false otherwise. + bool isStopped() { + return (getState() == State::STOPPED); + } private: /// @brief Thread-safe change of the pool's run state. @@ -86,7 +102,7 @@ private: /// -# Notifies threads of state change. /// -# Restarts the IOService. /// -# Creates the threads if they do not yet exist (true only - /// when transitioning from STOPPED). + /// when transitioning from STOPPED). /// -# Waits until threads are running. /// -# Sets the count of exited threads to 0. /// -# Returns to caller. @@ -109,14 +125,19 @@ private: /// -# Returns to caller. /// /// @param state new state for the pool. - void setRunState(RunState state); + void setState(State state); + + /// @brief Thread-safe fetch of the pool's operational state. + /// + /// @return Thread pool state. + State getState(); /// @brief Validates whether the pool can change to a given state. /// /// @param state new state for the pool. - /// @return true if the Chang's is valid, false otherwise. + /// @return true if the change is valid, false otherwise. /// @note Must be called from a thread-safe context. - bool validateStateChange(RunState state) const; + bool validateStateChange(State state) const; /// @brief Work function executed by each thread in the pool. /// @@ -174,7 +195,7 @@ private: asiolink::IOServicePtr io_service_; /// @brief Tracks the operational state of the pool. - RunState run_state_; + State run_state_; /// @brief Mutex to protect the internal state. std::mutex mutex_; @@ -198,7 +219,6 @@ private: /// @brief Pool of threads used to service connections in multi-threaded /// mode. std::list > threads_; - }; /// @brief Defines a pointer to a thread pool. @@ -208,4 +228,3 @@ typedef boost::shared_ptr HttpThreadPoolPtr; } // end of namespace isc #endif - diff --git a/src/lib/http/tests/http_thread_pool_unittests.cc b/src/lib/http/tests/http_thread_pool_unittests.cc index 0f21d092ed..c3958c9734 100644 --- a/src/lib/http/tests/http_thread_pool_unittests.cc +++ b/src/lib/http/tests/http_thread_pool_unittests.cc @@ -61,7 +61,7 @@ TEST_F(HttpThreadPoolTest, deferredStartConstruction) { // IOService should be there. // IOService is new, so it should not be stopped, // No threads in the pool. - ASSERT_EQ(HttpThreadPool::RunState::STOPPED, pool->getRunState()); + ASSERT_TRUE(pool->isStopped()); EXPECT_EQ(pool->getPoolSize(), 3); ASSERT_TRUE(pool->getIOService()); EXPECT_FALSE(pool->getIOService()->stopped()); @@ -79,7 +79,7 @@ TEST_F(HttpThreadPoolTest, startDuringConstruction) { // Pool size should be 3, state should be RUNNING, IOService should // set but not stopped, and we should have 3 threads in the pool. - ASSERT_EQ(HttpThreadPool::RunState::RUNNING, pool->getRunState()); + ASSERT_TRUE(pool->isRunning()); EXPECT_EQ(pool->getPoolSize(), 3); ASSERT_TRUE(pool->getIOService()); EXPECT_FALSE(pool->getIOService()->stopped()); @@ -95,14 +95,14 @@ TEST_F(HttpThreadPoolTest, stoppedToRunning) { // Create a stopped pool. ASSERT_NO_THROW_LOG(pool.reset(new HttpThreadPool(io_service_, 3, true))); - ASSERT_EQ(HttpThreadPool::RunState::STOPPED, pool->getRunState()); + ASSERT_TRUE(pool->isStopped()); // Call run from STOPPED. ASSERT_NO_THROW_LOG(pool->run()); // State should be RUNNING, IOService should not be stopped, we should // have 3 threads in the pool. - ASSERT_EQ(HttpThreadPool::RunState::RUNNING, pool->getRunState()); + ASSERT_TRUE(pool->isRunning()); EXPECT_FALSE(pool->getIOService()->stopped()); EXPECT_EQ(pool->getThreadCount(), 3); @@ -111,7 +111,7 @@ TEST_F(HttpThreadPoolTest, stoppedToRunning) { // State should be RUNNING, IOService should not be stopped, we should // have 3 threads in the pool. - ASSERT_EQ(HttpThreadPool::RunState::RUNNING, pool->getRunState()); + ASSERT_TRUE(pool->isRunning()); EXPECT_FALSE(pool->getIOService()->stopped()); EXPECT_EQ(pool->getThreadCount(), 3); @@ -125,14 +125,14 @@ TEST_F(HttpThreadPoolTest, runningToStopped) { // Create a running pool. ASSERT_NO_THROW_LOG(pool.reset(new HttpThreadPool(io_service_, 3, false))); - ASSERT_EQ(HttpThreadPool::RunState::RUNNING, pool->getRunState()); + ASSERT_TRUE(pool->isRunning()); // Call stop. ASSERT_NO_THROW_LOG(pool->stop()); // State should be STOPPED, IOService should be stopped, we should // have 0 threads in the pool. - ASSERT_EQ(HttpThreadPool::RunState::STOPPED, pool->getRunState()); + ASSERT_TRUE(pool->isStopped()); EXPECT_TRUE(pool->getIOService()->stopped()); EXPECT_EQ(pool->getThreadCount(), 0); @@ -141,7 +141,7 @@ TEST_F(HttpThreadPoolTest, runningToStopped) { // State should be STOPPED, IOService should be stopped, we should // have 0 threads in the pool. - ASSERT_EQ(HttpThreadPool::RunState::STOPPED, pool->getRunState()); + ASSERT_TRUE(pool->isStopped()); EXPECT_TRUE(pool->getIOService()->stopped()); EXPECT_EQ(pool->getThreadCount(), 0); @@ -155,14 +155,14 @@ TEST_F(HttpThreadPoolTest, runningToPaused) { // Create a running pool. ASSERT_NO_THROW_LOG(pool.reset(new HttpThreadPool(io_service_, 3, false))); - ASSERT_EQ(HttpThreadPool::RunState::RUNNING, pool->getRunState()); + ASSERT_TRUE(pool->isRunning()); // Call pause from RUNNING. ASSERT_NO_THROW_LOG(pool->pause()); // State should be PAUSED, IOService should be stopped, we should // have 3 threads in the pool. - ASSERT_EQ(HttpThreadPool::RunState::PAUSED, pool->getRunState()); + ASSERT_TRUE(pool->isPaused()); EXPECT_TRUE(pool->getIOService()->stopped()); EXPECT_EQ(pool->getThreadCount(), 3); @@ -171,7 +171,7 @@ TEST_F(HttpThreadPoolTest, runningToPaused) { // State should be PAUSED, IOService should be stopped, we should // have 3 threads in the pool. - ASSERT_EQ(HttpThreadPool::RunState::PAUSED, pool->getRunState()); + ASSERT_TRUE(pool->isPaused()); EXPECT_TRUE(pool->getIOService()->stopped()); EXPECT_EQ(pool->getThreadCount(), 3); @@ -185,18 +185,18 @@ TEST_F(HttpThreadPoolTest, pausedToRunning) { // Create a running pool. ASSERT_NO_THROW_LOG(pool.reset(new HttpThreadPool(io_service_, 3, false))); - ASSERT_EQ(HttpThreadPool::RunState::RUNNING, pool->getRunState()); + ASSERT_TRUE(pool->isRunning()); // Call pause from RUNNING. ASSERT_NO_THROW_LOG(pool->pause()); - ASSERT_EQ(HttpThreadPool::RunState::PAUSED, pool->getRunState()); + ASSERT_TRUE(pool->isPaused()); // Call run. ASSERT_NO_THROW_LOG(pool->run()); // State should be RUNNING, IOService should not be stopped, we should // have 3 threads in the pool. - ASSERT_EQ(HttpThreadPool::RunState::RUNNING, pool->getRunState()); + ASSERT_TRUE(pool->isRunning()); EXPECT_FALSE(pool->getIOService()->stopped()); EXPECT_EQ(pool->getThreadCount(), 3); @@ -210,18 +210,18 @@ TEST_F(HttpThreadPoolTest, pausedToStopped) { // Create a running pool. ASSERT_NO_THROW_LOG(pool.reset(new HttpThreadPool(io_service_, 3, false))); - ASSERT_EQ(HttpThreadPool::RunState::RUNNING, pool->getRunState()); + ASSERT_TRUE(pool->isRunning()); // Call pause from RUNNING. ASSERT_NO_THROW_LOG(pool->pause()); - ASSERT_EQ(HttpThreadPool::RunState::PAUSED, pool->getRunState()); + ASSERT_TRUE(pool->isPaused()); // Call stop. ASSERT_NO_THROW_LOG(pool->stop()); // State should be STOPPED, IOService should be stopped, we should // have 0 threads in the pool. - ASSERT_EQ(HttpThreadPool::RunState::STOPPED, pool->getRunState()); + ASSERT_TRUE(pool->isStopped()); EXPECT_TRUE(pool->getIOService()->stopped()); EXPECT_EQ(pool->getThreadCount(), 0); @@ -235,11 +235,11 @@ TEST_F(HttpThreadPoolTest, stoppedToPaused) { // Create a stopped pool. ASSERT_NO_THROW_LOG(pool.reset(new HttpThreadPool(io_service_, 3, true))); - ASSERT_EQ(HttpThreadPool::RunState::STOPPED, pool->getRunState()); + ASSERT_TRUE(pool->isStopped()); // State should be STOPPED, IOService won't be stopped because it was // never started. We should have 0 threads in the pool. - ASSERT_EQ(HttpThreadPool::RunState::STOPPED, pool->getRunState()); + ASSERT_TRUE(pool->isStopped()); EXPECT_FALSE(pool->getIOService()->stopped()); EXPECT_EQ(pool->getThreadCount(), 0); @@ -247,11 +247,11 @@ TEST_F(HttpThreadPoolTest, stoppedToPaused) { ASSERT_NO_THROW_LOG(pool->pause()); // Should have no effect. - ASSERT_EQ(HttpThreadPool::RunState::STOPPED, pool->getRunState()); + ASSERT_TRUE(pool->isStopped()); // State should be STOPPED, IOService won't be stopped because it was // never started. We should have 0 threads in the pool. - ASSERT_EQ(HttpThreadPool::RunState::STOPPED, pool->getRunState()); + ASSERT_TRUE(pool->isStopped()); EXPECT_FALSE(pool->getIOService()->stopped()); EXPECT_EQ(pool->getThreadCount(), 0); diff --git a/src/lib/http/tests/mt_client_unittests.cc b/src/lib/http/tests/mt_client_unittests.cc index ab313e81d1..ed7adefc6f 100644 --- a/src/lib/http/tests/mt_client_unittests.cc +++ b/src/lib/http/tests/mt_client_unittests.cc @@ -809,9 +809,9 @@ TEST_F(MtHttpClientTest, basics) { // Verify that it has an internal IOService and that thread pool size // and thread count match. ASSERT_TRUE(client->getThreadIOService()); + EXPECT_FALSE(client->getThreadIOService()->stopped()); ASSERT_EQ(client->getThreadPoolSize(), 3); ASSERT_EQ(client->getThreadCount(), 3); - ASSERT_EQ(client->getRunState(), HttpThreadPool::RunState::RUNNING); // Check convenience functions. ASSERT_TRUE(client->isRunning()); @@ -859,7 +859,6 @@ TEST_F(MtHttpClientTest, deferredStart) { ASSERT_TRUE(client->getThreadIOService()); ASSERT_EQ(client->getThreadPoolSize(), thread_pool_size); ASSERT_EQ(client->getThreadCount(), 0); - ASSERT_EQ(client->getRunState(), HttpThreadPool::RunState::STOPPED); // Check convenience functions. ASSERT_FALSE(client->isRunning()); @@ -873,7 +872,6 @@ TEST_F(MtHttpClientTest, deferredStart) { ASSERT_EQ(client->getThreadCount(), 3); ASSERT_TRUE(client->getThreadIOService()); ASSERT_FALSE(client->getThreadIOService()->stopped()); - ASSERT_EQ(client->getRunState(), HttpThreadPool::RunState::RUNNING); // Check convenience functions. ASSERT_TRUE(client->isRunning()); @@ -885,7 +883,7 @@ TEST_F(MtHttpClientTest, deferredStart) { // Verify we didn't break it. ASSERT_EQ(client->getThreadCount(), 3); - ASSERT_EQ(client->getRunState(), HttpThreadPool::RunState::RUNNING); + ASSERT_TRUE(client->isRunning()); // Make sure destruction doesn't throw. ASSERT_NO_THROW_LOG(client.reset()); @@ -905,7 +903,7 @@ TEST_F(MtHttpClientTest, restartAfterStop) { ASSERT_EQ(client->getThreadCount(), 3); ASSERT_TRUE(client->getThreadIOService()); ASSERT_FALSE(client->getThreadIOService()->stopped()); - ASSERT_EQ(client->getRunState(), HttpThreadPool::RunState::RUNNING); + ASSERT_TRUE(client->isRunning()); // Stop should succeed. ASSERT_NO_THROW_LOG(client->stop()); @@ -914,14 +912,14 @@ TEST_F(MtHttpClientTest, restartAfterStop) { ASSERT_EQ(client->getThreadCount(), 0); ASSERT_TRUE(client->getThreadIOService()); ASSERT_TRUE(client->getThreadIOService()->stopped()); - ASSERT_EQ(client->getRunState(), HttpThreadPool::RunState::STOPPED); + ASSERT_TRUE(client->isStopped()); // Starting again should succeed. ASSERT_NO_THROW_LOG(client->start()); ASSERT_EQ(client->getThreadCount(), 3); ASSERT_TRUE(client->getThreadIOService()); ASSERT_FALSE(client->getThreadIOService()->stopped()); - ASSERT_EQ(client->getRunState(), HttpThreadPool::RunState::RUNNING); + ASSERT_TRUE(client->isRunning()); // Make sure destruction doesn't throw. ASSERT_NO_THROW_LOG(client.reset());