Skip to content

Commit 74de1e0

Browse files
authored
Fix a failing time-sensitive test. (#190)
One of the tests for our `.timeLimit()` trait uses `Clock.measure {}` to determine how long it takes to execute, but that's unsafe because it will include time spent waiting for the Swift runtime and the OS to schedule the child task in which the test runs. That time may be significant when tests are running in parallel. This PR modifies that test to explicitly measure the start of the test when the test function is entered and the end of the test when the local test run completes. This avoids accidentally measuring any initial wait time.
1 parent 41ff962 commit 74de1e0

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

Tests/TestingTests/Traits/TimeLimitTraitTests.swift

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,24 @@ struct TimeLimitTraitTests {
152152
@available(_clockAPI, *)
153153
@Test("Test does not block until end of time limit")
154154
func doesNotWaitUntilEndOfTimeLimit() async throws {
155-
let timeAwaited = await Test.Clock().measure {
156-
var configuration = Configuration()
157-
configuration.testTimeLimitGranularity = .milliseconds(1)
158-
configuration.maximumTestTimeLimit = .seconds(60)
155+
var configuration = Configuration()
156+
configuration.testTimeLimitGranularity = .milliseconds(1)
157+
configuration.maximumTestTimeLimit = .seconds(60)
159158

160-
await Test {
161-
try await Test.Clock.sleep(for: .nanoseconds(1))
162-
}.run(configuration: configuration)
163-
}
159+
// Do not use Clock.measure {} here because it will include the time spent
160+
// waiting for the test's task to be scheduled by the Swift runtime. We
161+
// only want to measure the time from the start of the test until the call
162+
// to run(configuration:) returns.
163+
let timeStarted = Locked<Test.Clock.Instant?>()
164+
await Test {
165+
timeStarted.withLock { timeStarted in
166+
timeStarted = .now
167+
}
168+
try await Test.Clock.sleep(for: .nanoseconds(1))
169+
}.run(configuration: configuration)
170+
let timeEnded = Test.Clock.Instant.now
171+
172+
let timeAwaited = try #require(timeStarted.rawValue).duration(to: timeEnded)
164173
#expect(timeAwaited < .seconds(1))
165174
}
166175

0 commit comments

Comments
 (0)