]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#899] refactored unittests to use fast timer
authorRazvan Becheriu <razvan@isc.org>
Thu, 18 Feb 2021 15:04:35 +0000 (17:04 +0200)
committerRazvan Becheriu <razvan@isc.org>
Thu, 18 Feb 2021 17:23:57 +0000 (19:23 +0200)
src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc

index 59ff4d42f766560359bd797546d58154f67724ef..749cebe30c7ca7590fbae6cacd59ea8f70218a12 100644 (file)
@@ -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"