Skip to content

Commit

Permalink
Use WaitForSingleObject to wait for process termination.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle committed Sep 14, 2023
1 parent fc62b55 commit 6cd87f6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
10 changes: 7 additions & 3 deletions Sources/WinAppDriver/Win32ProcessTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
}

Expand Down
7 changes: 2 additions & 5 deletions Sources/WinAppDriver/WinAppDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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).")
}
Expand Down

0 comments on commit 6cd87f6

Please sign in to comment.