From: Razvan Becheriu Date: Mon, 18 Nov 2019 17:45:27 +0000 (+0200) Subject: [#883, !506] updated unit tests X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5b3085382d84dc400adcd2f9be134de01cf6fdd9;p=thirdparty%2Fkea.git [#883, !506] updated unit tests --- diff --git a/src/lib/util/tests/thread_pool_unittest.cc b/src/lib/util/tests/thread_pool_unittest.cc index 85a0340a5b..7e209c097a 100644 --- a/src/lib/util/tests/thread_pool_unittest.cc +++ b/src/lib/util/tests/thread_pool_unittest.cc @@ -8,10 +8,12 @@ #include +#include #include #include +using namespace isc; using namespace isc::util; using namespace std; @@ -205,14 +207,14 @@ TEST_F(ThreadPoolTest, testAddAndCount) { // add items to stopped thread pool for (uint32_t i = 0; i < items_count; ++i) { - thread_pool.add(make_shared(call_back)); + EXPECT_NO_THROW(thread_pool.add(make_shared(call_back))); } // the item count should match ASSERT_EQ(thread_pool.count(), items_count); // calling reset should clear all threads and should remove all queued items - thread_pool.reset(); + EXPECT_NO_THROW(thread_pool.reset()); // the item count should be 0 ASSERT_EQ(thread_pool.count(), 0); // the thread count should be 0 @@ -240,28 +242,30 @@ TEST_F(ThreadPoolTest, testStartAndStop) { call_back = std::bind(&ThreadPoolTest::runAndWait, this); // calling start should create the threads and should keep the queued items - thread_pool.start(thread_count); + EXPECT_THROW(thread_pool.start(0), InvalidParameter); + // calling start should create the threads and should keep the queued items + EXPECT_NO_THROW(thread_pool.start(thread_count)); // the item count should be 0 ASSERT_EQ(thread_pool.count(), 0); // the thread count should match ASSERT_EQ(thread_pool.size(), thread_count); // do it once again to check if it works - thread_pool.start(thread_count); + EXPECT_THROW(thread_pool.start(thread_count), InvalidOperation); // the item count should be 0 ASSERT_EQ(thread_pool.count(), 0); // the thread count should match ASSERT_EQ(thread_pool.size(), thread_count); // calling stop should clear all threads and should keep queued items - thread_pool.stop(); + EXPECT_NO_THROW(thread_pool.stop()); // the item count should be 0 ASSERT_EQ(thread_pool.count(), 0); // the thread count should be 0 ASSERT_EQ(thread_pool.size(), 0); // do it once again to check if it works - thread_pool.stop(); + EXPECT_THROW(thread_pool.stop(), InvalidOperation); // the item count should be 0 ASSERT_EQ(thread_pool.count(), 0); // the thread count should be 0 @@ -269,7 +273,7 @@ TEST_F(ThreadPoolTest, testStartAndStop) { // add items to stopped thread pool for (uint32_t i = 0; i < items_count; ++i) { - thread_pool.add(make_shared(call_back)); + EXPECT_NO_THROW(thread_pool.add(make_shared(call_back))); } // the item count should match @@ -278,28 +282,28 @@ TEST_F(ThreadPoolTest, testStartAndStop) { ASSERT_EQ(thread_pool.size(), 0); // calling stop should clear all threads and should keep queued items - thread_pool.stop(); + EXPECT_THROW(thread_pool.stop(), InvalidOperation); // the item count should match ASSERT_EQ(thread_pool.count(), items_count); // the thread count should be 0 ASSERT_EQ(thread_pool.size(), 0); // calling reset should clear all threads and should remove all queued items - thread_pool.reset(); + EXPECT_NO_THROW(thread_pool.reset()); // the item count should be 0 ASSERT_EQ(thread_pool.count(), 0); // the thread count should be 0 ASSERT_EQ(thread_pool.size(), 0); // do it once again to check if it works - thread_pool.reset(); + EXPECT_NO_THROW(thread_pool.reset()); // the item count should be 0 ASSERT_EQ(thread_pool.count(), 0); // the thread count should be 0 ASSERT_EQ(thread_pool.size(), 0); // calling start should create the threads and should keep the queued items - thread_pool.start(thread_count); + EXPECT_NO_THROW(thread_pool.start(thread_count)); // the item count should be 0 ASSERT_EQ(thread_pool.count(), 0); // the thread count should be 0 @@ -307,7 +311,7 @@ TEST_F(ThreadPoolTest, testStartAndStop) { // add items to running thread pool for (uint32_t i = 0; i < items_count; ++i) { - thread_pool.add(make_shared(call_back)); + EXPECT_NO_THROW(thread_pool.add(make_shared(call_back))); } // wait for all items to be processed @@ -329,7 +333,7 @@ TEST_F(ThreadPoolTest, testStartAndStop) { signalThreads(); // calling stop should clear all threads and should keep queued items - thread_pool.stop(); + EXPECT_NO_THROW(thread_pool.stop()); // the item count should be 0 ASSERT_EQ(thread_pool.count(), 0); // the thread count should be 0 @@ -347,7 +351,7 @@ TEST_F(ThreadPoolTest, testStartAndStop) { // add items to stopped thread pool for (uint32_t i = 0; i < items_count; ++i) { - thread_pool.add(make_shared(call_back)); + EXPECT_NO_THROW(thread_pool.add(make_shared(call_back))); } // the item count should match @@ -356,7 +360,7 @@ TEST_F(ThreadPoolTest, testStartAndStop) { ASSERT_EQ(thread_pool.size(), 0); // calling start should create the threads and should keep the queued items - thread_pool.start(thread_count); + EXPECT_NO_THROW(thread_pool.start(thread_count)); // the thread count should match ASSERT_EQ(thread_pool.size(), thread_count); @@ -373,7 +377,7 @@ TEST_F(ThreadPoolTest, testStartAndStop) { checkRunHistory(items_count); // calling stop should clear all threads and should keep queued items - thread_pool.stop(); + EXPECT_NO_THROW(thread_pool.stop()); // the item count should be 0 ASSERT_EQ(thread_pool.count(), 0); // the thread count should be 0 diff --git a/src/lib/util/thread_pool.h b/src/lib/util/thread_pool.h index cde06b0c0b..78544f8e57 100644 --- a/src/lib/util/thread_pool.h +++ b/src/lib/util/thread_pool.h @@ -39,8 +39,10 @@ struct ThreadPool { /// @brief reset the thread pool stopping threads and clearing the internal /// queue + /// + /// It can be called several times even when the thread pool is stopped void reset() { - stop(); + stopInternal(); queue_.clear(); } @@ -48,13 +50,24 @@ struct ThreadPool { /// /// @param thread_count specifies the number of threads to be created and /// started + /// + /// @throw InvalidOperation if thread pool already started + /// @throw InvalidParameter if thread count is 0 void start(uint32_t thread_count) { if (!thread_count) { isc_throw(InvalidParameter, "thread count is 0"); } if (running_) { - isc_throw(InvalidParameter, "thread pool already started"); + isc_throw(InvalidOperation, "thread pool already started"); } + startInternal(thread_count); + } + + /// @brief start all the threads + /// + /// @param thread_count specifies the number of threads to be created and + /// started + void startInternal(uint32_t thread_count) { queue_.enable(); running_ = true; for (uint32_t i = 0; i < thread_count; ++i) { @@ -63,10 +76,17 @@ struct ThreadPool { } /// @brief stop all the threads + /// + /// @throw InvalidOperation if thread pool already stopped void stop() { if (!running_) { - isc_throw(InvalidParameter, "thread pool already stopped"); + isc_throw(InvalidOperation, "thread pool already stopped"); } + stopInternal(); + } + + /// @brief stop all the threads + void stopInternal() { running_ = false; queue_.disable(); for (auto thread : threads_) { @@ -181,7 +201,7 @@ private: /// Removes all queued work items void clear() { std::lock_guard lock(mutex_); - queue_ = std::queue(); + queue_ = QueueContainer(); } /// @brief enable the queue @@ -226,6 +246,7 @@ private: try { (*item)(); } catch (...) { + // catch all exceptions } } }