From bd569bcfb4b249cded1cc68caddabcdc2db9b223 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Fri, 16 Jun 2023 14:42:34 +0200 Subject: added ZenServerInstance::SpawnServerAndWait (#334) this change adds `ZenServerInstance::SpawnServerAndWait()` which as the name implies spawns a server and then waits for the instance to reach a usable state before returning to the caller. It also changes the behaviour of `ZenServerInstance::AttachToRunningServer()` so it matches the Spawn behaviour wrt automatic termination on ZenServerInstance destruction. Previously it would always terminate the subprocess on exit. --- src/zenutil/include/zenutil/zenserverprocess.h | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/zenutil/include') diff --git a/src/zenutil/include/zenutil/zenserverprocess.h b/src/zenutil/include/zenutil/zenserverprocess.h index a0a705f64..7b78261bc 100644 --- a/src/zenutil/include/zenutil/zenserverprocess.h +++ b/src/zenutil/include/zenutil/zenserverprocess.h @@ -58,6 +58,7 @@ struct ZenServerInstance void WaitUntilReady(); [[nodiscard]] bool WaitUntilReady(int Timeout); void EnableTermination() { m_Terminate = true; } + void DisableShutdownOnDestroy() { m_ShutdownOnDestroy = false; } void Detach(); inline int GetPid() { return m_Process.Pid(); } inline void SetOwnerPid(int Pid) { m_OwnerPid = Pid; } @@ -68,7 +69,16 @@ struct ZenServerInstance m_TestDir = TestDir; } - void SpawnServer(int BasePort = 0, std::string_view AdditionalServerArgs = std::string_view()); + inline void SpawnServer(int BasePort = 0, std::string_view AdditionalServerArgs = std::string_view()) + { + SpawnServer(BasePort, AdditionalServerArgs, /* WaitTimeoutMs */ 0); + } + + inline void SpawnServerAndWait(int BasePort = 0, std::string_view AdditionalServerArgs = std::string_view(), int WaitTimeoutMs = 10000) + { + SpawnServer(BasePort, AdditionalServerArgs, WaitTimeoutMs); + } + void AttachToRunningServer(int BasePort = 0); std::string GetBaseUri() const; @@ -77,12 +87,14 @@ private: ProcessHandle m_Process; NamedEvent m_ReadyEvent; NamedEvent m_ShutdownEvent; - bool m_Terminate = false; + bool m_Terminate = false; + bool m_ShutdownOnDestroy = true; std::filesystem::path m_TestDir; int m_BasePort = 0; std::optional m_OwnerPid; void CreateShutdownEvent(int BasePort); + void SpawnServer(int BasePort, std::string_view AdditionalServerArgs, int WaitTimeoutMs); }; /** Shared Zen system state -- cgit v1.2.3