Skip to content

Don't use a class to store the current exit test. #1065

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 9, 2025
Merged
Changes from 1 commit
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
33 changes: 10 additions & 23 deletions Sources/Testing/ExitTests/ExitTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,15 @@ public struct ExitTest: Sendable, ~Copyable {

@_spi(Experimental)
extension ExitTest {
/// A container type to hold the current exit test.
///
/// This class is temporarily necessary until `ManagedBuffer` is updated to
/// support storing move-only values. For more information, see [SE-NNNN](https://github.com/swiftlang/swift-evolution/pull/2657).
private final class _CurrentContainer: Sendable {
/// The exit test represented by this container.
///
/// The value of this property must be optional to avoid a copy when reading
/// the value in ``ExitTest/current``.
let exitTest: ExitTest?

init(exitTest: borrowing ExitTest) {
self.exitTest = ExitTest(id: exitTest.id, body: exitTest.body, _observedValues: exitTest._observedValues)
}
}

/// Storage for ``current``.
private static let _current = Locked<_CurrentContainer?>()
///
/// A pointer is used for indirection because `ManagedBuffer` cannot yet hold
/// move-only types.
private static nonisolated(unsafe) let _current: Locked<UnsafeMutablePointer<ExitTest?>> = {
let result = UnsafeMutablePointer<ExitTest?>.allocate(capacity: 1)
result.initialize(to: nil)
return Locked(rawValue: result)
}()

/// The exit test that is running in the current process, if any.
///
Expand All @@ -144,11 +135,7 @@ extension ExitTest {
/// process.
public static var current: ExitTest? {
_read {
if let current = _current.rawValue {
yield current.exitTest
} else {
yield nil
}
yield _current.rawValue.pointee
}
}
}
Expand Down Expand Up @@ -235,7 +222,7 @@ extension ExitTest {

// Set ExitTest.current before the test body runs.
Self._current.withLock { current in
current = _CurrentContainer(exitTest: self)
current.pointee = ExitTest(id: id, body: body, _observedValues: _observedValues)
}

do {
Expand Down