-
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 process = AsyncProcess(arguments: try args(forTestAt: path), environment: self.testEnv, outputRedirection: outputRedirection) | ||
guard let terminationKey = self.cancellator.register(process) else { | ||
return .failure // terminating | ||
let processConfig = Subprocess.Configuration(.path(.init(args[0])), arguments: .init(Array(args.dropFirst())), environment: .custom(env)) |
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 introduce a convenience extension on Subprocess.Configuration that takes an array as an input? This pattern seems likely to appear elsewhere.
extension Subprocess.Configuration {
init(commandLine: [String], environment: Environment) throws {
guard let arg0 = commandLine.first else {
throw ItsEmpty()
}
self.init(.path(arg0), .init(Array(commandLine.dropFirst())), environment: environment)
}
}
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?
self.ranSuccessfully = false | ||
var pendingTests: [UnitTest] = tests | ||
var processedTests: [TestResult] = [] | ||
observabilityScope.emit(error: "wefhfehjef") |
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.
remove debugging code
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.
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
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