Skip to content

Fix a failing time-sensitive test. #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions Tests/TestingTests/Traits/TimeLimitTraitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,24 @@ struct TimeLimitTraitTests {
@available(_clockAPI, *)
@Test("Test does not block until end of time limit")
func doesNotWaitUntilEndOfTimeLimit() async throws {
let timeAwaited = await Test.Clock().measure {
var configuration = Configuration()
configuration.testTimeLimitGranularity = .milliseconds(1)
configuration.maximumTestTimeLimit = .seconds(60)
var configuration = Configuration()
configuration.testTimeLimitGranularity = .milliseconds(1)
configuration.maximumTestTimeLimit = .seconds(60)

await Test {
try await Test.Clock.sleep(for: .nanoseconds(1))
}.run(configuration: configuration)
}
// Do not use Clock.measure {} here because it will include the time spent
// waiting for the test's task to be scheduled by the Swift runtime. We
// only want to measure the time from the start of the test until the call
// to run(configuration:) returns.
let timeStarted = Locked<Test.Clock.Instant?>()
await Test {
timeStarted.withLock { timeStarted in
timeStarted = .now
}
try await Test.Clock.sleep(for: .nanoseconds(1))
}.run(configuration: configuration)
let timeEnded = Test.Clock.Instant.now

let timeAwaited = try #require(timeStarted.rawValue).duration(to: timeEnded)
#expect(timeAwaited < .seconds(1))
}

Expand Down