Skip to content

Avoid reassigning task-local runtime state in a Runner's event handler unless it's nested #339

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 2 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
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
22 changes: 13 additions & 9 deletions Sources/Testing/Running/Runner.RuntimeState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ extension Runner {
/// The test case that is running on the current task, if any.
var testCase: Test.Case?

/// The runtime state related to the runner running on the current task.
/// The runtime state related to the runner running on the current task,
/// if any.
@TaskLocal
static var current = Self()
static var current: Self?
}
}

Expand All @@ -42,7 +43,10 @@ extension Runner {
/// In practice, the primary scenario where this is important is when running
/// the testing library's own tests.
mutating func configureEventHandlerRuntimeState() {
let existingRuntimeState = RuntimeState.current
guard let existingRuntimeState = RuntimeState.current else {
return
}

configuration.eventHandler = { [eventHandler = configuration.eventHandler] event, context in
RuntimeState.$current.withValue(existingRuntimeState) {
eventHandler(event, context)
Expand All @@ -56,7 +60,7 @@ extension Runner {
extension Configuration {
/// The configuration for the current task, if any.
public static var current: Self? {
Runner.RuntimeState.current.configuration
Runner.RuntimeState.current?.configuration
}

/// Call a function while the value of ``Configuration/current`` is set.
Expand All @@ -74,7 +78,7 @@ extension Configuration {
configuration._removeFromAll(identifiedBy: id)
}

var runtimeState = Runner.RuntimeState.current
var runtimeState = Runner.RuntimeState.current ?? .init()
runtimeState.configuration = configuration
return try await Runner.RuntimeState.$current.withValue(runtimeState, operation: body)
}
Expand Down Expand Up @@ -144,7 +148,7 @@ extension Test {
/// or [`DispatchQueue.async(execute:)`](https://developer.apple.com/documentation/dispatch/dispatchqueue/2016103-async)),
/// the value of this property may be `nil`.
public static var current: Self? {
Runner.RuntimeState.current.test
Runner.RuntimeState.current?.test
}

/// Call a function while the value of ``Test/current`` is set.
Expand All @@ -157,7 +161,7 @@ extension Test {
///
/// - Throws: Whatever is thrown by `body`.
static func withCurrent<R>(_ test: Self, perform body: () async throws -> R) async rethrows -> R {
var runtimeState = Runner.RuntimeState.current
var runtimeState = Runner.RuntimeState.current ?? .init()
runtimeState.test = test
return try await Runner.RuntimeState.$current.withValue(runtimeState, operation: body)
}
Expand All @@ -177,7 +181,7 @@ extension Test.Case {
/// or [`DispatchQueue.async(execute:)`](https://developer.apple.com/documentation/dispatch/dispatchqueue/2016103-async)),
/// the value of this property may be `nil`.
public static var current: Self? {
Runner.RuntimeState.current.testCase
Runner.RuntimeState.current?.testCase
}

/// Call a function while the value of ``Test/Case/current`` is set.
Expand All @@ -190,7 +194,7 @@ extension Test.Case {
///
/// - Throws: Whatever is thrown by `body`.
static func withCurrent<R>(_ testCase: Self, perform body: () async throws -> R) async rethrows -> R {
var runtimeState = Runner.RuntimeState.current
var runtimeState = Runner.RuntimeState.current ?? .init()
runtimeState.testCase = testCase
return try await Runner.RuntimeState.$current.withValue(runtimeState, operation: body)
}
Expand Down
17 changes: 17 additions & 0 deletions Tests/TestingTests/Runner.RuntimeStateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,21 @@ struct Runner_RuntimeStateTests {

await Test(name: "foo") {}.run(configuration: configuration)
}

// This confirms that an event posted within the closure of `withTaskGroup`
// (and similar APIs) in regular tests (those which are not testing the
// testing library itself) are handled appropriately and do not crash.
@Test func eventPostingInTaskGroup() async {
// Enable delivery of "expectation checked" events, merely as a way to allow
// an event to be posted during the test below without causing any real
// issues to be recorded or otherwise confuse the testing harness.
var configuration = Configuration.current ?? .init()
configuration.deliverExpectationCheckedEvents = true

await Configuration.withCurrent(configuration) {
await withTaskGroup(of: Void.self) { group in
#expect(Bool(true))
}
}
}
}
7 changes: 7 additions & 0 deletions Tests/TestingTests/RunnerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ struct NeverRunTests {
}

final class RunnerTests: XCTestCase {
func testInitialTaskLocalState() {
// These are expected to be `nil` since this is an XCTest.
XCTAssertNil(Test.current)
XCTAssertNil(Test.Case.current)
XCTAssertNil(Configuration.current)
}

func testDefaultInit() async throws {
let runner = await Runner()
XCTAssertFalse(runner.tests.contains(where: \.isHidden))
Expand Down