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.

Tests wait for what they wait on instead of for a set time

Țugui Dragoș120e96fa7a70
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FileSizeTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FileSizeTests.swift
index f2973795..0f48216e 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/FileSizeTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/FileSizeTests.swift
@@ -322,7 +322,8 @@ struct FileSizeTests {
let start = ContinuousClock.now
_ = await FileSize.reclaimableSize(of: folder, within: FileSize.budget)
_ = await FileSize.reclaimableSize(of: withSlash, within: FileSize.budget)
- #expect(ContinuousClock.now - start < .seconds(2), "a question about a folder already being walked paid a budget of its own")
+ let took = ContinuousClock.now - start
+ #expect(took < .seconds(FileSize.budget), "a question about a folder being walked paid a budget of its own")
}
/// The walk that measures a folder also says whether a repository is in it, at any depth, because that
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FolderWatchTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FolderWatchTests.swift
index 7e5ee79b..dba4a1de 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/FolderWatchTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/FolderWatchTests.swift
@@ -29,11 +29,12 @@ struct FolderWatchTests {
/// The control: without this, a watcher that reported constantly would pass the tests above.
@Test func staysSilentWhileNothingHappens() async throws {
let directory = try TemporaryDirectory()
+ // macOS reports a change a moment after it happens. The fixture's own change is heard by a watch of its
+ // own first, so the watch under test, which begins after it, cannot hear it as its first change.
+ let setup = FolderWatch.changes(in: [directory.url])
try directory.directory("watched/Untouched.app")
+ try #require(await firstChange(of: setup, within: .seconds(10)), "the fixture's own change was never heard")
let folder = directory.url.appending(path: "watched", directoryHint: .isDirectory)
- // macOS reports a change a moment after it happens. The wait lets the fixture's own change be reported
- // before the watch begins, so the watch does not hear it as its first change.
- try await Task.sleep(for: .seconds(3))
let changes = FolderWatch.changes(in: [folder])
#expect(await firstChange(of: changes, within: .seconds(2)) == false, "a change was reported out of nothing")
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SubprocessTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SubprocessTests.swift
index ac070a23..bb5caaac 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/SubprocessTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/SubprocessTests.swift
@@ -64,21 +64,43 @@ struct SubprocessTests {
}
/// A run without a time limit, which Homebrew's upgrades need, still stops when whoever asked goes away.
- @Test func aRunWithNoTimeLimitStopsWhenAsked() async {
- let task = Task { await Subprocess.run("/bin/sleep", ["600"], timeout: nil) }
- try? await Task.sleep(for: .milliseconds(300))
+ @Test func aRunWithNoTimeLimitStopsWhenAsked() async throws {
+ let task = try await sleeping(timeout: nil)
task.cancel()
#expect(await task.value == .failure(.canceled))
}
- @Test func aToolIsStoppedWhenWhoeverAskedGoesAway() async {
- let task = Task { await Subprocess.run("/bin/sleep", ["600"], timeout: 3_600) }
- // Long enough for the tool to start, even on a busy machine.
- try? await Task.sleep(for: .milliseconds(300))
+ @Test func aToolIsStoppedWhenWhoeverAskedGoesAway() async throws {
+ let task = try await sleeping(timeout: 3_600)
task.cancel()
#expect(await task.value == .failure(.canceled))
}
+ /// Runs a tool that says it has started and then sleeps for ten minutes, and returns once it has said so, so
+ /// that a cancel reaches a tool that runs rather than one not started yet.
+ private func sleeping(
+ timeout: TimeInterval?
+ ) async throws -> Task<Result<Subprocess.Output, Subprocess.Failure>, Never> {
+ let started = Mutex(false)
+ let task = Task {
+ await Subprocess.run("/bin/sh", ["-c", "echo started; exec /bin/sleep 600"], timeout: timeout) { _ in
+ started.withLock { $0 = true }
+ }
+ }
+ let clock = ContinuousClock()
+ let start = clock.now
+ while !started.withLock({ $0 }), clock.now - start < .seconds(10) {
+ try await Task.sleep(for: .milliseconds(5))
+ }
+ guard started.withLock({ $0 }) else {
+ task.cancel()
+ throw Unstarted()
+ }
+ return task
+ }
+
+ private struct Unstarted: Error {}
+
/// A task that was already canceled starts no tool at all: a scan stopped between two steps must not launch
/// the next one only to kill it, which on a busy Mac takes longer than a Stop is allowed. Asked for a tool
/// that is not there, such a task answers that it was canceled, since it never tried to start one.