From: Razvan Becheriu Date: Thu, 12 Sep 2019 16:55:53 +0000 (+0300) Subject: [#886, !508] fixed unit test X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f9f7001c24492255ab6fa8f3676871ea2506b153;p=thirdparty%2Fkea.git [#886, !508] fixed unit test --- diff --git a/src/lib/dhcpsrv/tests/thread_resource_mgr_unittest.cc b/src/lib/dhcpsrv/tests/thread_resource_mgr_unittest.cc index 42a67798d9..eef73cf2c0 100644 --- a/src/lib/dhcpsrv/tests/thread_resource_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/thread_resource_mgr_unittest.cc @@ -125,21 +125,37 @@ std::set Resource::set_; class ThreadResourceMgrTest : public ::testing::Test { public: /// @brief Constructor - ThreadResourceMgrTest() : wait_(false) { + ThreadResourceMgrTest() : wait_thread_(false), wait_(false) { } /// @brief Destructor ~ThreadResourceMgrTest() { } + /// @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 wait() { + bool waitMain() { 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_); @@ -163,6 +179,11 @@ public: 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 @@ -217,10 +238,19 @@ 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 (wait() == false); }); + wait_cv_.wait(lk, [&]{ return (waitMain() == false); }); } } @@ -234,6 +264,13 @@ private: 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_; @@ -241,6 +278,10 @@ 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_; @@ -260,12 +301,20 @@ TEST_F(ThreadResourceMgrTest, 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( &ThreadResourceMgrTest::run, this, 2, 2, 0, true))); + // wait for the thread to process + wait(); + // configure wait for test thread + resetWaitThread(); // 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))); + // wait for the thread to process + wait(); // signal all threads signalThreads(); // wait for all threads to finish @@ -283,12 +332,20 @@ TEST_F(ThreadResourceMgrTest, 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( &ThreadResourceMgrTest::run, this, 2, 2, 0, true))); + // wait for the thread to process + wait(); + // configure wait for test thread + resetWaitThread(); // 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))); + // wait for the thread to process + wait(); // signal all threads signalThreads(); // wait for all threads to finish