diff --git a/Sources/WinAppDriver/Win32ProcessTree.swift b/Sources/WinAppDriver/Win32ProcessTree.swift index b198f92..52a6c8e 100644 --- a/Sources/WinAppDriver/Win32ProcessTree.swift +++ b/Sources/WinAppDriver/Win32ProcessTree.swift @@ -28,14 +28,18 @@ internal class Win32ProcessTree { } } - func terminate(waitTime: TimeInterval? = nil) throws { + func terminate(waitTime: TimeInterval?) throws { + precondition((waitTime ?? 0) >= 0) + if !TerminateJobObject(jobHandle, UINT.max) { throw Win32Error.getLastError(apiName: "TerminateJobObject") } if let waitTime { - // Should we throw if we don't get an exit code in time? - _ = try poll(timeout: waitTime) { try exitCode != nil } + let milliseconds = waitTime * 1000 + let millisecondsDword = milliseconds > Double(DWORD.max) ? INFINITE : DWORD(milliseconds) + let waitResult = WaitForSingleObject(handle, millisecondsDword) + assert(waitResult == WAIT_OBJECT_0, "The process did not terminate within the expected time interval.") } } diff --git a/Sources/WinAppDriver/WinAppDriver.swift b/Sources/WinAppDriver/WinAppDriver.swift index 0665927..7f74467 100644 --- a/Sources/WinAppDriver/WinAppDriver.swift +++ b/Sources/WinAppDriver/WinAppDriver.swift @@ -18,7 +18,6 @@ public class WinAppDriver: WebDriver { private let httpWebDriver: HTTPWebDriver private let processTree: Win32ProcessTree? - private var terminationWaitTime: TimeInterval? private init(httpWebDriver: HTTPWebDriver, processTree: Win32ProcessTree? = nil) { self.httpWebDriver = httpWebDriver @@ -55,15 +54,13 @@ public class WinAppDriver: WebDriver { } } - let result = WinAppDriver(httpWebDriver: httpWebDriver, processTree: processTree) - result.terminationWaitTime = waitTime - return result + return WinAppDriver(httpWebDriver: httpWebDriver, processTree: processTree) } deinit { if let processTree { do { - try processTree.terminate(waitTime: terminationWaitTime) + try processTree.terminate(waitTime: TimeInterval.infinity) } catch { assertionFailure("WinAppDriver did not terminate within the expected time: \(error).") }