From fbb389aea2a7bf4ed2168519f44a6da6f02d069f Mon Sep 17 00:00:00 2001 From: Razvan Becheriu Date: Tue, 8 Jun 2021 13:38:58 +0300 Subject: [PATCH] [#1878] fixed unittests --- src/hooks/dhcp/run_script/run_script.cc | 5 ++--- src/hooks/dhcp/run_script/run_script.h | 7 +++++++ src/lib/asiolink/tests/process_spawn_unittest.cc | 12 ++++++++++-- src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc | 7 ++++++- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/hooks/dhcp/run_script/run_script.cc b/src/hooks/dhcp/run_script/run_script.cc index 9c28e4e960..ab710f18da 100644 --- a/src/hooks/dhcp/run_script/run_script.cc +++ b/src/hooks/dhcp/run_script/run_script.cc @@ -32,9 +32,8 @@ RunScriptImpl::configure(LibraryHandle& handle) { if (name->getType() != Element::string) { isc_throw(InvalidParameter, "The 'name' parameter must be a string"); } - IOServicePtr io_service(new asiolink::IOService()); try { - ProcessSpawn process(io_service, name->stringValue()); + ProcessSpawn process(IOServicePtr(), name->stringValue()); } catch (const isc::Exception& ex) { isc_throw(InvalidParameter, "Invalid 'name' parameter: " << ex.what()); } @@ -50,7 +49,7 @@ RunScriptImpl::configure(LibraryHandle& handle) { void RunScriptImpl::runScript(const ProcessArgs& args, const ProcessEnvVars& vars) { - ProcessSpawn process(io_service_, name_, args, vars); + ProcessSpawn process(getIOService(), name_, args, vars); process.spawn(true); } diff --git a/src/hooks/dhcp/run_script/run_script.h b/src/hooks/dhcp/run_script/run_script.h index 874c64d212..c66c82e4b7 100644 --- a/src/hooks/dhcp/run_script/run_script.h +++ b/src/hooks/dhcp/run_script/run_script.h @@ -37,6 +37,13 @@ public: io_service_ = io_service; } + /// @brief Gets IO service to be used by the @ref ProcessSpawn instance. + /// + /// @return The IOService object, used for all ASIO operations. + static isc::asiolink::IOServicePtr getIOService() { + return (io_service_); + } + /// @brief Extract boolean data and append to environment. /// /// @param value The value to be exported to target script environment. diff --git a/src/lib/asiolink/tests/process_spawn_unittest.cc b/src/lib/asiolink/tests/process_spawn_unittest.cc index 478fb92a60..51aed64f9c 100644 --- a/src/lib/asiolink/tests/process_spawn_unittest.cc +++ b/src/lib/asiolink/tests/process_spawn_unittest.cc @@ -34,6 +34,12 @@ public: /// @brief IOService instance to process IO. asiolink::IOServicePtr io_service_; + /// @brief Single instance of IOService. + static asiolink::IOServicePtr getIOService() { + static asiolink::IOServicePtr io_service(new asiolink::IOService()); + return (io_service); + } + /// @brief Failsafe timer to ensure test(s) do not hang. isc::asiolink::IntervalTimer test_timer_; @@ -48,7 +54,7 @@ public: /// @brief Constructor. ProcessSpawnTest() : - io_service_(new asiolink::IOService()), test_timer_(*io_service_), + io_service_(getIOService()), test_timer_(*io_service_), test_time_ms_(0), io_signal_set_(), processed_signals_() { io_signal_set_.reset(new IOSignalSet(io_service_, @@ -58,7 +64,9 @@ public: } /// @brief Destructor. - ~ProcessSpawnTest() {} + ~ProcessSpawnTest() { + io_signal_set_->remove(SIGCHLD); + } /// @brief Method used as the IOSignalSet handler. /// diff --git a/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc index 5137b5b0ab..caa65ab8bc 100644 --- a/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc @@ -104,7 +104,7 @@ public: MemfileLeaseMgrTest() : io4_(getLeaseFilePath("leasefile4_0.csv")), io6_(getLeaseFilePath("leasefile6_0.csv")), - io_service_(new IOService()), + io_service_(getIOService()), timer_mgr_(TimerMgr::instance()) { timer_mgr_->setIOService(io_service_); @@ -253,6 +253,11 @@ public: return (!elapsed); } + /// @brief Single instance of IOService. + static asiolink::IOServicePtr getIOService() { + static asiolink::IOServicePtr io_service(new asiolink::IOService()); + return (io_service); + } /// @brief Generates a DHCPv4 lease with random content. ///