From: Razvan Becheriu Date: Mon, 4 Nov 2019 18:07:07 +0000 (+0200) Subject: [#886, !508] renamed class and moved sources X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3183acab6fd4fe4f760fa51a5156a0a84550257c;p=thirdparty%2Fkea.git [#886, !508] renamed class and moved sources --- diff --git a/src/lib/dhcpsrv/tests/Makefile.am b/src/lib/dhcpsrv/tests/Makefile.am index c93a91743f..86bc606cf2 100644 --- a/src/lib/dhcpsrv/tests/Makefile.am +++ b/src/lib/dhcpsrv/tests/Makefile.am @@ -124,7 +124,6 @@ libdhcpsrv_unittests_SOURCES += shared_networks_list_parser_unittest.cc libdhcpsrv_unittests_SOURCES += srv_config_unittest.cc libdhcpsrv_unittests_SOURCES += subnet_unittest.cc libdhcpsrv_unittests_SOURCES += test_get_callout_handle.cc test_get_callout_handle.h -libdhcpsrv_unittests_SOURCES += thread_resource_mgr_unittest.cc libdhcpsrv_unittests_SOURCES += triplet_unittest.cc libdhcpsrv_unittests_SOURCES += thread_pool_unittest.cc libdhcpsrv_unittests_SOURCES += test_utils.cc test_utils.h diff --git a/src/lib/dhcpsrv/tests/thread_resource_mgr_unittest.cc b/src/lib/dhcpsrv/tests/thread_resource_mgr_unittest.cc deleted file mode 100644 index 7c1227cd1e..0000000000 --- a/src/lib/dhcpsrv/tests/thread_resource_mgr_unittest.cc +++ /dev/null @@ -1,299 +0,0 @@ -// Copyright (C) 2018-2019 Internet Systems Consortium, Inc. ("ISC") -// -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at http://mozilla.org/MPL/2.0/. - -#include - -#include - -#include - -#include - -#include -#include -#include -#include - -using namespace isc::dhcp; -using namespace std; - -namespace { - -/// @brief test class to keep track of all constructed objects of a specific -/// class type -/// -/// @template parameter class to make this functionality available for a wide -/// range of 'similar' but distinct classes -template -class Resource : public boost::noncopyable { -public: - /// @brief Constructor - Resource() : data_() { - lock_guard lk(mutex_); - // increase current number of instances of this class - Resource::count_++; - // increase the total number of instances ever created - Resource::created_count_++; - // check that this instance in new and should not be found in the - // verification set - EXPECT_TRUE(Resource::set_.find(&data_) == Resource::set_.end()); - // add this instance to the verification set - Resource::set_.emplace(&data_); - } - - /// @brief Destructor - virtual ~Resource() { - lock_guard lk(mutex_); - // decrease current number of instances of this class - Resource::count_--; - // increase the total number of instances ever destroyed - Resource::destroyed_count_++; - // check that this instance is found in the verification set - EXPECT_FALSE(Resource::set_.find(&data_) == Resource::set_.end()); - // remove this instance from the verification set - Resource::set_.erase(&data_); - } - - /// @brief count number of current allocated instances of the class - /// - /// @return number of current allocated instances of the class - static uint32_t count() { - lock_guard lk(mutex_); - return Resource::count_; - } - - /// @brief count number of class instances ever created - /// - /// @return number of class instances ever created - static uint32_t createdCount() { - lock_guard lk(mutex_); - return Resource::created_count_; - } - - /// @brief count number of class instances ever destroyed - /// - /// @return number of class instances ever destroyed - static uint32_t destroyedCount() { - lock_guard lk(mutex_); - return Resource::destroyed_count_; - } - - /// @brief reset all statistics for this class - static void reset() { - lock_guard lk(mutex_); - // reset all statistics for this class - Resource::count_ = 0; - Resource::created_count_ = 0; - Resource::destroyed_count_ = 0; - Resource::set_.clear(); - } - -private: - /// @brief data element - T data_; - - /// @brief total number of instances at any given time - static uint32_t count_; - - /// @brief total number of instances ever created - static uint32_t created_count_; - - /// @brief total number of instances ever destroyed - static uint32_t destroyed_count_; - - /// @brief mutex used to keep the internal state consistent - static std::mutex mutex_; - - /// @brief set to fold - static std::set set_; -}; - -template -uint32_t Resource::count_; -template -uint32_t Resource::created_count_; -template -uint32_t Resource::destroyed_count_; -template -std::mutex Resource::mutex_; -template -std::set Resource::set_; - -/// @brief Test Fixture for testing isc::dhcp::ThreadResourceMgr -class ThreadResourceMgrTest : public ::testing::Test { -public: - /// @brief Constructor - ThreadResourceMgrTest() : wait_(false) { - } - - /// @brief Destructor - ~ThreadResourceMgrTest() { - } - - /// @brief flag which indicates if working thread should wait for main - /// thread signal - /// - /// @return the wait flag - bool wait() { - return wait_; - } - - /// @brief function used by main thread to unblock processing threads - void signalThreads() { - lock_guard lk(wait_mutex_); - // clear the wait flag so that threads will no longer wait for the main - // thread signal - wait_ = false; - // wake all threads if waiting for main thread signal - wait_cv_.notify_all(); - } - - /// @brief reset resource manager for the template class and perform sanity - /// checks - template - void reset() { - get() = make_shared>>(); - sanityCheck(); - wait_ = true; - } - - /// @brief check statistics - /// - /// @param expected_count check equality of this value with the number of - /// class instances - /// @param expected_created check equality of this value with the number of - /// class instances ever created - /// @param expected_destroyed check equality of this value with the number - /// of class instances ever destroyed - template - void checkInstances(uint32_t expected_count, - uint32_t expected_created, - uint32_t expected_destroyed) { - ASSERT_EQ(Resource::count(), expected_count); - ASSERT_EQ(Resource::createdCount(), expected_created); - ASSERT_EQ(Resource::destroyedCount(), expected_destroyed); - } - - /// @brief sanity check that the number of created instances is equal to the - /// number of destroyed instances - template - void sanityCheck() { - ASSERT_EQ(Resource::createdCount(), Resource::destroyedCount()); - } - - /// @brief get the instance of the resource manager responsible for a - /// specific class - /// - /// @return the resource manager responsible for a specific class - template - shared_ptr>> &get() { - static shared_ptr>> container; - return container; - } - - /// @brief run function which accesses the resource allocated for the - /// calling thread and verifies the class statistics - /// @param expected_count check equality of this value with the number of - /// class instances - /// @param expected_created check equality of this value with the number of - /// class instances ever created - /// @param expected_destroyed check equality of this value with the number - /// of class instances ever destroyed - /// @param signal indicate if the function should wait for signal from main - /// thread or exit immediately - template - void run(uint32_t expected_count, - uint32_t expected_created, - uint32_t expected_destroyed, - bool signal = false) { - // get resource for this thread - auto left = get()->resource().get(); - // verify statistics - checkInstances(expected_count, expected_created, expected_destroyed); - // get the resource for this thread once more - auto right = get()->resource().get(); - // check that it is the same resource - ASSERT_EQ(left, right); - // verify statistics which should have not changed on multiple - // sequential requests for the same resource - checkInstances(expected_count, expected_created, expected_destroyed); - - if (signal) { - unique_lock lk(wait_mutex_); - // if specified, wait for signal from main thread - wait_cv_.wait(lk, [&]{ return (wait() == false); }); - } - } - -private: - /// @brief mutex used to keep the internal state consistent - /// related to the control of the main thread over the working threads exit - std::mutex wait_mutex_; - - /// @brief condition variable used to signal working threads to exit - condition_variable wait_cv_; - - /// @brief flag which indicates if working thread should wait for main - /// thread signal - bool wait_; -}; - -/// @brief This test verifies that each thread can access it's own allocated -/// resource. Multiple threads are created and run in parallel. The checks are -/// done while threads are still running. -/// It is very important for the threads to run in parallel and not just run and -/// join the thread as this will cause newer threads to use the old thread id -/// and receive the same resource. -/// If destroying threads, the resource manager should also be reset. -TEST_F(ThreadResourceMgrTest, testThreadResources) { - std::list> threads; - - // reset statistics for uint_32 type - reset(); - // call run function on main thread and verify statistics - run(1, 1, 0); - // call run on a different thread and verify statistics - threads.push_back(std::make_shared(std::bind( - &ThreadResourceMgrTest::run, this, 2, 2, 0, true))); - // call run again on a different thread and verify statistics - threads.push_back(std::make_shared(std::bind( - &ThreadResourceMgrTest::run, this, 3, 3, 0, true))); - // signal all threads - signalThreads(); - // wait for all threads to finish - for (auto &thread : threads) { - thread->join(); - } - // reset statistics for uint_32 type - reset(); - // verify statistics 0 instances, 3 created, 3 destroyed - checkInstances(0, 3, 3); - - threads.clear(); - - // reset statistics for bool type - reset(); - // call run function on main thread and verify statistics - run(1, 1, 0); - // call run on a different thread and verify statistics - threads.push_back(std::make_shared(std::bind( - &ThreadResourceMgrTest::run, this, 2, 2, 0, true))); - // call run again on a different thread and verify statistics - threads.push_back(std::make_shared(std::bind( - &ThreadResourceMgrTest::run, this, 3, 3, 0, true))); - // signal all threads - signalThreads(); - // wait for all threads to finish - for (auto &thread : threads) { - thread->join(); - } - // reset statistics for bool type - reset(); - // verify statistics 0 instances, 3 created, 3 destroyed - checkInstances(0, 3, 3); -} - -} // namespace diff --git a/src/lib/dhcpsrv/thread_resource_mgr.h b/src/lib/dhcpsrv/thread_resource_mgr.h deleted file mode 100644 index 405e92f611..0000000000 --- a/src/lib/dhcpsrv/thread_resource_mgr.h +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright (C) 2019 Internet Systems Consortium, Inc. ("ISC") -// -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at http://mozilla.org/MPL/2.0/. - -#ifndef THREAD_RESOURCE_MGR_H -#define THREAD_RESOURCE_MGR_H - -#include -#include -#include -#include - -namespace isc { -namespace dhcp { - -template -class ThreadResourceMgr { - typedef std::shared_ptr ResourcePtr; -public: - /// @brief function to retrieve the specific resource of calling thread - /// This function returns the resource of the calling thread from the map - /// container or, in case it is not found, it creates a resource and adds it - /// to the map container - /// - /// @return the specific resource of the calling thread - ResourcePtr resource() { - std::lock_guard lock(mutex_); - auto id = std::this_thread::get_id(); - if (map_.find(id) != map_.end()) { - return map_[id]; - } - ResourcePtr result(std::make_shared()); - map_[id] = result; - return result; - } -private: - /// @brief mutex used to keep the internal state consistent - std::mutex mutex_; - - /// @brief map container which holds the resources for each thread - std::unordered_map map_; -}; - -} // namespace dhcp -} // namespace isc - -#endif // THREAD_RESOURCE_MGR_H diff --git a/src/lib/util/tests/thread_resource_unittest.cc b/src/lib/util/tests/thread_resource_unittest.cc index 4977ced90e..9ce1c122a5 100644 --- a/src/lib/util/tests/thread_resource_unittest.cc +++ b/src/lib/util/tests/thread_resource_unittest.cc @@ -37,7 +37,8 @@ public: Resource::count_++; // increase the total number of instances ever created Resource::created_count_++; - // check that this instance is not found in the verification set + // check that this instance in new and should not be found in the + // verification set EXPECT_TRUE(Resource::set_.find(&data_) == Resource::set_.end()); // add this instance to the verification set Resource::set_.emplace(&data_); @@ -106,7 +107,7 @@ private: /// @brief mutex used to keep the internal state consistent static std::mutex mutex_; - /// @brief set to hold the distinct identification data of each instance + /// @brief set to fold static std::set set_; }; @@ -132,29 +133,14 @@ public: ~ThreadResourceTest() { } - /// @brief flag which indicates if main thread should wait for the test - /// thread to start - /// - /// @return the wait flag - bool waitThread() { - return wait_thread_; - } - /// @brief flag which indicates if working thread should wait for main /// thread signal /// /// @return the wait flag - bool waitMain() { + bool wait() { return wait_; } - /// @brief block main thread until testing thread has processed the task - void wait() { - unique_lock lck(mutex_); - // wait for the testing thread to process - cv_.wait(lck, [&]{ return (waitThread() == false); }); - } - /// @brief function used by main thread to unblock processing threads void signalThreads() { lock_guard lk(wait_mutex_); @@ -174,15 +160,9 @@ public: get() = make_shared>>(); // perform sanity checks sanityCheck(); - // reset the wait flag wait_ = true; } - /// @brief reset wait thread flag - void resetWaitThread() { - wait_thread_ = true; - } - /// @brief check statistics /// /// @param expected_count check equality of this value with the number of @@ -237,39 +217,14 @@ public: // sequential requests for the same resource checkInstances(expected_count, expected_created, expected_destroyed); - { - // make sure this thread has started - lock_guard lk(mutex_); - // reset wait thread flag - wait_thread_ = false; - // wake main thread if it is waiting for this thread to process - cv_.notify_all(); - } - if (signal) { unique_lock lk(wait_mutex_); // if specified, wait for signal from main thread - wait_cv_.wait(lk, [&]{ return (waitMain() == false); }); + wait_cv_.wait(lk, [&]{ return (wait() == false); }); } } private: - /// @brief sanity check that the number of created instances is equal to the - /// number of destroyed instances - template - void sanityCheck() { - // the number of created instances should match the number of destroyed - // instances - ASSERT_EQ(Resource::createdCount(), Resource::destroyedCount()); - } - - /// @brief mutex used to keep the internal state consistent - std::mutex mutex_; - - /// @brief condition variable used to signal main thread that test thread - /// has started processing - condition_variable cv_; - /// @brief mutex used to keep the internal state consistent /// related to the control of the main thread over the working threads exit std::mutex wait_mutex_; @@ -277,10 +232,6 @@ private: /// @brief condition variable used to signal working threads to exit condition_variable wait_cv_; - /// @brief flag which indicates if main thread should wait for test thread - /// to start - bool wait_thread_; - /// @brief flag which indicates if working thread should wait for main /// thread signal bool wait_; @@ -300,8 +251,6 @@ TEST_F(ThreadResourceTest, testThreadResources) { reset(); // call run function on main thread and verify statistics run(1, 1, 0); - // configure wait for test thread - resetWaitThread(); // call run on a different thread and verify statistics threads.push_back(std::make_shared(std::bind( &ThreadResourceTest::run, this, 2, 2, 0, true))); @@ -331,8 +280,6 @@ TEST_F(ThreadResourceTest, testThreadResources) { reset(); // call run function on main thread and verify statistics run(1, 1, 0); - // configure wait for test thread - resetWaitThread(); // call run on a different thread and verify statistics threads.push_back(std::make_shared(std::bind( &ThreadResourceTest::run, this, 2, 2, 0, true))); diff --git a/src/lib/util/thread_resource.h b/src/lib/util/thread_resource.h index 9a8e44a604..bd360cbc7c 100644 --- a/src/lib/util/thread_resource.h +++ b/src/lib/util/thread_resource.h @@ -4,8 +4,8 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. -#ifndef THREAD_RESOURCE_H -#define THREAD_RESOURCE_H +#ifndef THREAD_RESOURCE_MGR_H +#define THREAD_RESOURCE_MGR_H #include #include @@ -46,4 +46,4 @@ private: } // namespace dhcp } // namespace isc -#endif // THREAD_RESOURCE_H +#endif // THREAD_RESOURCE_MGR_H