From: Razvan Becheriu Date: Thu, 18 Feb 2021 15:04:35 +0000 (+0200) Subject: [#899] refactored unittests to use fast timer X-Git-Tag: Kea-1.9.5~45 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fab98c91aad5630a49ef6f42cabfb2c4ccc2f7d7;p=thirdparty%2Fkea.git [#899] refactored unittests to use fast timer --- diff --git a/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc index 59ff4d42f7..749cebe30c 100644 --- a/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc @@ -218,13 +218,42 @@ public: void setTestTime(const uint32_t ms) { IntervalTimer timer(*io_service_); timer.setup([this]() { - io_service_->stop(); + io_service_->stop(); }, ms, IntervalTimer::ONE_SHOT); io_service_->run(); io_service_->get_io_service().reset(); } + /// @brief Waits for the specified process to finish. + /// + /// @param process An object which started the process. + /// @param timeout Timeout in seconds. + /// + /// @return true if the process ended, false otherwise + bool waitForProcess(const Memfile_LeaseMgr& lease_mgr, + const uint8_t timeout) { + const uint32_t iterations_max = timeout * 1000; + IntervalTimer fast_path_timer(*io_service_); + IntervalTimer timer(*io_service_); + bool elapsed = false; + timer.setup([&]() { + io_service_->stop(); + elapsed = true; + }, iterations_max, IntervalTimer::ONE_SHOT); + + fast_path_timer.setup([&]() { + if (!lease_mgr.isLFCRunning()) { + io_service_->stop(); + } + }, 1, IntervalTimer::REPEATING); + + io_service_->run(); + io_service_->get_io_service().reset(); + return (!elapsed); + } + + /// @brief Generates a DHCPv4 lease with random content. /// /// The following lease parameters are randomly generated: @@ -522,7 +551,7 @@ TEST_F(MemfileLeaseMgrTest, leaseFileCleanup4) { EXPECT_EQ(new_file_contents, current_file.readFile()); // Wait for the LFC process to complete. - setTestTime(500); + ASSERT_TRUE(waitForProcess(*lease_mgr, 2)); // And make sure it has returned an exit status of 0. EXPECT_EQ(0, lease_mgr->getLFCExitStatus()) @@ -603,7 +632,7 @@ TEST_F(MemfileLeaseMgrTest, leaseFileCleanup6) { EXPECT_EQ(new_file_contents, current_file.readFile()); // Wait for the LFC process to complete. - setTestTime(500); + ASSERT_TRUE(waitForProcess(*lease_mgr, 2)); // And make sure it has returned an exit status of 0. EXPECT_EQ(0, lease_mgr->getLFCExitStatus()) @@ -672,7 +701,7 @@ TEST_F(MemfileLeaseMgrTest, leaseFileCleanupStartFail) { ASSERT_NO_THROW(lease_mgr->lfcCallback()); // Wait for the LFC process to complete. - setTestTime(100); + ASSERT_TRUE(waitForProcess(*lease_mgr, 2)); // And make sure it has returned an error. EXPECT_EQ(EXIT_FAILURE, lease_mgr->getLFCExitStatus()) @@ -725,7 +754,7 @@ TEST_F(MemfileLeaseMgrTest, leaseFileFinish) { EXPECT_EQ(current_file_contents, current_file.readFile()); // Wait for the LFC process to complete. - setTestTime(100); + ASSERT_TRUE(waitForProcess(*lease_mgr, 5)); // And make sure it has returned an exit status of 0. EXPECT_EQ(0, lease_mgr->getLFCExitStatus()) @@ -790,7 +819,7 @@ TEST_F(MemfileLeaseMgrTest, leaseFileCopy) { EXPECT_EQ(current_file_contents, current_file.readFile()); // Wait for the LFC process to complete. - setTestTime(100); + ASSERT_TRUE(waitForProcess(*lease_mgr, 5)); // And make sure it has returned an exit status of 0. EXPECT_EQ(0, lease_mgr->getLFCExitStatus()) @@ -1834,7 +1863,7 @@ TEST_F(MemfileLeaseMgrTest, leaseUpgrade4) { // Wait for the LFC process to complete and // make sure it has returned an exit status of 0. - setTestTime(100); + ASSERT_TRUE(waitForProcess(*lease_mgr, 2)); ASSERT_EQ(0, lease_mgr->getLFCExitStatus()) << "Executing the LFC process failed: make sure that" @@ -1907,7 +1936,7 @@ TEST_F(MemfileLeaseMgrTest, leaseUpgrade6) { // Wait for the LFC process to complete and // make sure it has returned an exit status of 0. - setTestTime(100); + ASSERT_TRUE(waitForProcess(*lease_mgr, 2)); ASSERT_EQ(0, lease_mgr->getLFCExitStatus()) << "Executing the LFC process failed: make sure that"