]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#4049] Added wait time constant and a UT
authorThomas Markwalder <tmark@isc.org>
Thu, 7 Aug 2025 16:56:16 +0000 (12:56 -0400)
committerThomas Markwalder <tmark@isc.org>
Thu, 14 Aug 2025 13:15:52 +0000 (13:15 +0000)
modified:   src/bin/d2/d2_process.cc
modified:   src/bin/d2/d2_process.h
modified:   src/lib/asiolink/tests/io_service_unittest.cc

src/bin/d2/d2_process.cc
src/bin/d2/d2_process.h
src/lib/asiolink/tests/io_service_unittest.cc

index 1236cff62656a0f8fd09c317b65c9c3c2ea10dc0..3c1d2d07bee699a391df41166b592307f364f213 100644 (file)
@@ -56,6 +56,9 @@ namespace d2 {
 // be configurable.
 const unsigned int D2Process::QUEUE_RESTART_PERCENT = 80;
 
+// IOService run time duration is 100 ms.
+const unsigned int D2Process::IO_SERVICE_RUN_TIME_USECS = (100 * 1000);
+
 D2Process::D2Process(const char* name, const asiolink::IOServicePtr& io_service)
     : DProcessBase(name, io_service, DCfgMgrBasePtr(new D2CfgMgr())),
       reconf_queue_flag_(false), shutdown_type_(SD_NORMAL) {
@@ -162,9 +165,7 @@ D2Process::runIO() {
         // main service. If the service is stopped it will return immediately
         //  with a cnt of zero and timed_out set to false.
         bool timed_out;
-        /// @todo TKM wait time should probably be configurable.
-        /// Currently microseconds, should be milliseconds?
-        cnt = getIOService()->runOneFor(100 * 1000, timed_out);
+        cnt = getIOService()->runOneFor(IO_SERVICE_RUN_TIME_USECS, timed_out);
         if (timed_out) {
             // Return 1 so caller knows the service has not stopped.
             return (1);
index 3b03b26a68be7c346852cd6928720fad2396ae84..8c6db0e4e1ff065ac7a5d51cde1c1f8fcb3a6f30 100644 (file)
@@ -47,6 +47,10 @@ public:
     /// by restarting the queue manager.
     static const unsigned int QUEUE_RESTART_PERCENT;
 
+    /// @brief Amount of time to allow the main IOSerivce to wait
+    /// for ready handlers before timimg out in microseconds.
+    static const unsigned int IO_SERVICE_RUN_TIME_USECS;
+
     /// @brief Constructor
     ///
     /// Construction creates the configuration manager, the queue
index dc1ab5fd9773f90cb0e424117cd7cf8ea1eefc77..596ced0e3a94609492609329499edff3d1b67d0b 100644 (file)
@@ -46,19 +46,19 @@ TEST(IOService, post) {
     EXPECT_EQ(3, called[2]);
 }
 
-// Check the runOneFor() operates correctly.
+// Verify that runOneFor() operates correctly.
 TEST(IOService, runOneFor) {
     IOServicePtr io_service(new IOService());
 
     // Setup up a timer to expire in 200 ms.
     IntervalTimer timer(io_service);
-    size_t wait_ms = 200; 
+    size_t wait_ms = 200;
     bool timer_fired = false;
-    timer.setup([&timer_fired] { timer_fired = true; }, 
+    timer.setup([&timer_fired] { timer_fired = true; },
                 wait_ms, IntervalTimer::ONE_SHOT);
 
     size_t time_outs = 0;
-    while (timer_fired == false && time_outs < 5) { 
+    while (timer_fired == false && time_outs < 5) {
         // Call runOneFor() with 1/4 of the timer duration.
         bool timed_out = false;
         auto cnt = io_service->runOneFor(50 * 1000, timed_out);
@@ -77,4 +77,38 @@ TEST(IOService, runOneFor) {
     EXPECT_EQ(timer_fired, 1);
 }
 
+// Verify that runOneFor() handles service stop correctly.
+TEST(IOService, runOneForStopped) {
+    IOServicePtr io_service(new IOService());
+
+    // Setup up a timer to expire in 200 ms.
+    IntervalTimer timer(io_service);
+    size_t wait_ms = 200;
+    bool timer_fired = false;
+    timer.setup([&timer_fired] { timer_fired = true; },
+                wait_ms, IntervalTimer::ONE_SHOT);
+
+    // Call runOneFor() with runtime of 500ms in a thread.
+    size_t cnt = 0;
+    bool timed_out = false;
+    std::thread th([this, io_service, &timed_out, &cnt]() {
+        cnt = io_service->runOneFor(500 * 1000, timed_out);
+    });
+
+    // Sleep for 5 ms.
+    usleep(5 * 1000);
+
+    // Stop the service.
+    io_service->stop();
+
+    // Wait for the thread to complete.
+    th.join();
+
+    // Handle count should be 0, we should not have timed out, and
+    // the timer should not have fired.
+    EXPECT_EQ(0, cnt);
+    EXPECT_FALSE(timed_out);
+    EXPECT_FALSE(timer_fired);
+}
+
 }