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 the helper's wait and a folder's question where they run

Țugui Dragoș5df4b7c50a7b
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FileSizeTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FileSizeTests.swift
index 335193f1..e57495c7 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/FileSizeTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/FileSizeTests.swift
@@ -339,9 +339,9 @@ struct FileSizeTests {
withdrawn.cancel()
#expect(await withdrawn.value == nil)
- let asked = ContinuousClock.now
- #expect(await FileSize.contents(of: folder, within: 5, walking: walking) != nil)
- #expect(ContinuousClock.now - asked < .seconds(4), "the question waited on the walk that was stopped")
+ let question = await timedWhereItRuns { await FileSize.contents(of: folder, within: 5, walking: walking) }
+ #expect(question.value != nil)
+ #expect(question.took < .seconds(4), "the question waited on the walk that was stopped")
}
/// A late answer describes the folder as it was when the walk ended. It is handed over while that is recent,
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HelperAnswerTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HelperAnswerTests.swift
index b5c7697e..d1d95b3e 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/HelperAnswerTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/HelperAnswerTests.swift
@@ -63,10 +63,12 @@ struct HelperAnswerTests {
@Test func aHelperThatNeverAnswersIsNotAnsweringOnceItsOwnWaitEnds() async {
let helper = StandInHelper(answering: nil)
defer { helper.stop() }
- let start = ContinuousClock.now
+ let connecting = helper.connecting
+
+ let question = await timedWhereItRuns { await PrivilegedHelper.isResponding(connecting: connecting) }
- #expect(await !PrivilegedHelper.isResponding(connecting: helper.connecting))
- #expect(ContinuousClock.now - start < .seconds(PrivilegedHelper.requestWait))
+ #expect(!question.value)
+ #expect(question.took < .seconds(PrivilegedHelper.requestWait))
}
@Test func theHelperOfThisVersionIsAnswering() async {
@@ -90,18 +92,19 @@ struct HelperAnswerTests {
@Test func aRequestIsNeverSentToAHelperThatDoesNotAnswer() async {
let helper = StandInHelper(answering: nil)
defer { helper.stop() }
+ let connecting = helper.connecting
let sent = Record(false)
- let start = ContinuousClock.now
- let answer = await PrivilegedHelper.request(connecting: helper.connecting, fallback: "unanswered") {
- _, finish in
- sent.value.withLock { $0 = true }
- finish("answered")
+ let request = await timedWhereItRuns {
+ await PrivilegedHelper.request(connecting: connecting, fallback: "unanswered") { _, finish in
+ sent.value.withLock { $0 = true }
+ finish("answered")
+ }
}
- #expect(answer == "unanswered")
+ #expect(request.value == "unanswered")
#expect(!sent.value.withLock { $0 })
- #expect(ContinuousClock.now - start < .seconds(PrivilegedHelper.requestWait))
+ #expect(request.took < .seconds(PrivilegedHelper.requestWait))
}
@Test func aRequestIsSentToTheHelperOfThisVersion() async {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/Timing.swift b/Packages/PeelCore/Tests/PeelCoreTests/Timing.swift
new file mode 100644
index 00000000..ba1e7d1d
--- /dev/null
+++ b/Packages/PeelCore/Tests/PeelCoreTests/Timing.swift
@@ -0,0 +1,12 @@
+/// Runs `work` at user-initiated priority and times it where it returns. A task at the default priority resumes only
+/// once every test queued before it has had a thread, and that wait would be counted too.
+func timedWhereItRuns<Value: Sendable>(
+ _ work: @escaping @Sendable () async -> Value
+) async -> (value: Value, took: Duration) {
+ await Task.detached(priority: .userInitiated) {
+ let clock = ContinuousClock()
+ let start = clock.now
+ let value = await work()
+ return (value, clock.now - start)
+ }.value
+}