Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Test Stop's wait for a job's process without racing a clock

Țugui Dragoș0fced520ca5c
diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift
index d48e4d5c..3fd5b3d2 100644
--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift
+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift
@@ -37,7 +37,8 @@ public enum BackgroundItemActions {
static func stop(
_ item: BackgroundItem,
waitingUpTo limit: TimeInterval,
- launchctl: ([String]) async -> (status: Int32, output: String)
+ launchctl: ([String]) async -> (status: Int32, output: String),
+ untilEnded: (pid_t, TimeInterval) async -> Bool = { await ProcessEnds.ends($0, within: $1) }
) async throws(Failure) {
if let refusal = refusal(for: item) { throw refusal }
do throws(Failure) {
@@ -54,7 +55,7 @@ public enum BackgroundItemActions {
guard case .details(let details) = Launchctl.answer(status: printed.status, output: printed.output),
let process = details.pid
else { return }
- _ = await ProcessEnds.ends(process, within: limit)
+ _ = await untilEnded(process, limit)
}
/// How an item is started: loaded from its file with `bootstrap` when launchd does not have it, or
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
index a9d73a4b..1e922acb 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
@@ -186,32 +186,43 @@ struct BackgroundItemActionsTests {
@Test func stopWaitsForTheProcessLaunchdStillNamesToEnd() async throws {
// `bootout` returns at once while launchd keeps a job that ignores the signal until its time runs out.
- let directory = try TemporaryDirectory()
- let program = try directory.runningProgram("job")
- let job = item(state: .running(pid: program.processIdentifier), plist: nil)
+ let job = item(state: .running(pid: 4242), plist: nil)
let printed = """
gui/501/com.example.agent = {
\tpath = /Users/me/Library/LaunchAgents/com.example.agent.plist
\tstate = SIGTERMed
- \tpid = \(program.processIdentifier)
+ \tpid = 4242
}
"""
let asked = Mutex<[String]>([])
- let clock = ContinuousClock()
- Task {
- try? await Task.sleep(for: .milliseconds(300))
- program.terminate()
- }
+ let waited = Mutex<[String]>([])
- let started = clock.now
- try await BackgroundItemActions.stop(job, waitingUpTo: 5) { arguments in
+ try await BackgroundItemActions.stop(job, waitingUpTo: 30, launchctl: { arguments in
asked.withLock { $0.append(arguments[0]) }
return arguments[0] == "print" ? (0, printed) : (0, "")
- }
+ }, untilEnded: { process, limit in
+ waited.withLock { $0.append("\(process) within \(Int(limit))") }
+ return true
+ })
#expect(asked.withLock { $0 } == ["bootout", "print"])
- #expect(clock.now - started >= .milliseconds(250), "Stop returned while the process still ran")
- #expect(!ProcessEnds.isRunning(program.processIdentifier))
+ #expect(waited.withLock { $0 } == ["4242 within 30"])
+ }
+
+ @Test func stopWaitsForNothingOnceLaunchdNamesNoProcess() async throws {
+ let job = item(state: .running(pid: 4242), plist: nil)
+ let waited = Mutex(false)
+
+ for printed in [(Int32(113), "Could not find service"), (0, "gui/501/com.example.agent = {\n\tstate = not running\n}")] {
+ try await BackgroundItemActions.stop(job, waitingUpTo: 30, launchctl: { arguments in
+ arguments[0] == "print" ? printed : (0, "")
+ }, untilEnded: { _, _ in
+ waited.withLock { $0 = true }
+ return true
+ })
+ }
+
+ #expect(!waited.withLock { $0 })
}
@Test func restartsOnlyAJobPeelMayControlAndAsksLaunchdToKillAndStartIt() async {