-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Adopt swift-subprocess when running unit tests #9260
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
base: main
Are you sure you want to change the base?
Conversation
| let processConfig = Subprocess.Configuration(.path(.init(args[0])), arguments: .init(Array(args.dropFirst())), environment: .custom(env)) | ||
| let result = try await Subprocess.run(processConfig, input: .none, error: .standardOutput) { execution, outputSequence in | ||
| let token = cancellator.register(name: "Test Execution", handler: { _ in | ||
| await execution.teardown(using: [.gracefulShutDown(allowedDurationToNextStep: .seconds(5))]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is unsafe. Because deregister is synchronous, it may return before the cancellation block has finished executing, meaning teardown could be called when the process is already dead. It is NOT safe to use execution once the body block has returned, as the pid may have already been recycled or the pidfd may have become invalid. This is one reason why Execution would probably ideally be ~Escapable...
Subprocess.run already response to Swift Concurrency task cancellation, so you'd want something like this:
extension Cancellator {
func run<T: Sendable>(_ block: @escaping @Sendable () async throws -> T) async throws -> T {
let task = Task { try await block() }
let token = register(name: "Test Execution", handler: { _ in task.cancel() })
defer {
token.map { deregister($0) }
}
return try await task.value
}
}
try await cancellator.run {
try await Subprocess.run(...)
}That said, do you even need the cancellator, or is cooperative cancellation on its own, enough?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed. I think we do need the explicit cancellation here to ensure tests can be interrupted interactively
| library: .xctest // swift-testing does not use ParallelTestRunner | ||
| ) | ||
| var output = "" | ||
| let outputLock = NSLock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit but also kinda seriously: I'd either use an AsyncStream or at least use a LockedValue or similar abstraction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied this in the refactor, but looking at it more closely this lock seems unnecessary, I deleted it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we refactor the SwiftTestCommand so we can write 'small' or 'medium' tests, that is, we test the modified functions/code in isolation, as opposed to relying on 'large' (ie: end-to-end tests)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Meant to mark this as required changed instead of approve
0a067be to
d4aa4ca
Compare
|
Depends on swiftlang/swift-subprocess#201 for the CMake build |
This PR isn intended to be NFC so I've been relying on the existing tests so far |
d4aa4ca to
02696e4
Compare
|
@swift-ci test |
02696e4 to
e9601b4
Compare
I understand we are relying on existing tests, but we really should use the Test Pyramid to validate the code changes instead of relying on all the tests. Although the code changes may be exercises via existing tests, they tests to no guarantee we will exercise all the code paths. As such, we should try to write tests at different layers of the Test Pyramid. |
Alternative to #9191
Begin adopting swift-subprocess, beginning with test execution, in an effort to standardize on one implementation for process output streaming across platforms.
This required a bit of additional refactoring of the parallel test runner to make it async
Depends on swiftlang/swift#84947