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.

Time a stopped scan and a fast check by the work itself

Țugui Dragoș6dd3a160d7a9
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/InstalledAppTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/InstalledAppTests.swift
index 6ea063f6..b658b8b8 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/InstalledAppTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/InstalledAppTests.swift
@@ -7,9 +7,10 @@ struct InstalledAppTests {
let app = InstalledApp(url: URL(filePath: "/Applications/Editor.app"), bundleIdentifier: "org.example.editor", name: "Editor")
var answers = 0
- let took = ContinuousClock().measure {
- for _ in 0..<200_000 where app.isPeelItself { answers += 1 }
- }
+ // The time the processor spent on this thread, which other tests running beside it do not stretch.
+ let started = clock_gettime_nsec_np(CLOCK_THREAD_CPUTIME_ID)
+ for _ in 0..<200_000 where app.isPeelItself { answers += 1 }
+ let took = Duration.nanoseconds(Int64(clock_gettime_nsec_np(CLOCK_THREAD_CPUTIME_ID) - started))
#expect(answers == 0)
#expect(took < .milliseconds(100))
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ProjectArtifactsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ProjectArtifactsTests.swift
index 4b5219dc..b1eaba03 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/ProjectArtifactsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ProjectArtifactsTests.swift
@@ -31,21 +31,22 @@ struct ProjectArtifactsTests {
let never = DispatchSemaphore(value: 0)
defer { never.signal() }
let listing = Mutex(false)
- let walk = Task {
- await ProjectArtifacts.artifacts(in: root, exclusions: .none, measure: { _ in nil }) { _ in
+ let clock = ContinuousClock()
+ let walk = Task(priority: .userInitiated) {
+ _ = await ProjectArtifacts.artifacts(in: root, exclusions: .none, measure: { _ in nil }) { _ in
listing.withLock { $0 = true }
never.wait()
return nil
}
+ return clock.now
}
while !listing.withLock({ $0 }) { await Task.yield() }
- let clock = ContinuousClock()
let stopped = clock.now
walk.cancel()
- _ = await walk.value
+ let returned = await walk.value
- #expect(clock.now - stopped < .seconds(1))
+ #expect(returned - stopped < .seconds(1))
}
@Test func suggestsTheProjectFoldersThatExistInTheHomeFolder() throws {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/Unanswered.swift b/Packages/PeelCore/Tests/PeelCoreTests/Unanswered.swift
index 6143a66b..11235a4b 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/Unanswered.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/Unanswered.swift
@@ -40,15 +40,17 @@ final class Unanswered: Sendable {
/// Runs `scan` and cancels it 50 ms after its first request, once everything it asks at the same time is
/// waiting. Returns how long the scan took to return after the cancel, and how many folders it asked about
- /// before and after it.
+ /// before and after it. The scan runs at the priority SwiftUI gives a page's task, as a page's scan does, and is
+ /// timed where it returns, so the other tests running beside it do not stretch what is measured.
func stop(
_ scan: @escaping @Sendable () async -> Void
) async throws -> (took: Duration, askedBefore: Int, askedAfter: Int) {
- let running = Task { [self] in
+ let clock = ContinuousClock()
+ let running = Task(priority: .userInitiated) { [self] in
await scan()
finished.withLock { $0 = true }
+ return clock.now
}
- let clock = ContinuousClock()
while count == 0 {
guard !finished.withLock({ $0 }) else {
Issue.record("the scan finished without asking about a folder")
@@ -60,7 +62,7 @@ final class Unanswered: Sendable {
let before = count
let stopped = clock.now
running.cancel()
- await running.value
- return (clock.now - stopped, before, count - before)
+ let returned = await running.value
+ return (returned - stopped, before, count - before)
}
}