fix(monitors): 限制外部报告输出并可靠终止超时子进程
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import Foundation
|
||||
import Darwin
|
||||
|
||||
@main struct BoundedProcessChecks {
|
||||
static func main() async throws {
|
||||
if CommandLine.arguments.count > 1 {
|
||||
switch CommandLine.arguments[1] {
|
||||
case "valid": FileHandle.standardOutput.write(Data("report".utf8)); return
|
||||
case "large":
|
||||
let bytes = [UInt8](repeating: 42, count: 8192)
|
||||
for _ in 0..<1024 { _ = bytes.withUnsafeBytes { Darwin.write(1, $0.baseAddress, $0.count) } }
|
||||
return
|
||||
case "fail": exit(7)
|
||||
case "closed": Darwin.close(1); fallthrough
|
||||
default:
|
||||
signal(SIGTERM, SIG_IGN)
|
||||
while true { pause() }
|
||||
}
|
||||
}
|
||||
let executable = URL(fileURLWithPath: CommandLine.arguments[0])
|
||||
func run(_ mode: String, limit: Int = 262_144, timeout: Double = 2) async -> Data? {
|
||||
await BoundedProcess.read(executable: executable, arguments: [mode], limit: limit, timeout: timeout)
|
||||
}
|
||||
let valid = await run("valid")
|
||||
precondition(valid == Data("report".utf8))
|
||||
let exact = await run("valid", limit: 6)
|
||||
precondition(exact == valid)
|
||||
let tooSmall = await run("valid", limit: 5)
|
||||
precondition(tooSmall == nil)
|
||||
let large = await run("large", limit: 4096)
|
||||
precondition(large == nil)
|
||||
let failed = await run("fail")
|
||||
precondition(failed == nil)
|
||||
for mode in ["hang", "closed"] {
|
||||
let start = ContinuousClock.now
|
||||
let result = await run(mode, timeout: 0.2)
|
||||
precondition(result == nil && start.duration(to: .now) < .seconds(2))
|
||||
}
|
||||
let cancelStart = ContinuousClock.now
|
||||
let task = Task { await run("hang", timeout: 20) }
|
||||
try await Task.sleep(for: .milliseconds(100))
|
||||
task.cancel()
|
||||
let cancelled = await task.value
|
||||
precondition(cancelled == nil && cancelStart.duration(to: .now) < .seconds(2))
|
||||
let beforeStart = Task { await run("hang", timeout: 20) }
|
||||
beforeStart.cancel()
|
||||
let early = await beforeStart.value
|
||||
precondition(early == nil)
|
||||
let missing = await BoundedProcess.read(executable: URL(fileURLWithPath: "/not/a/kymo/executable"), arguments: [])
|
||||
precondition(missing == nil)
|
||||
var status: Int32 = 0
|
||||
precondition(waitpid(-1, &status, WNOHANG) == -1 && errno == ECHILD,
|
||||
"Every direct test child must be reaped before the request completes")
|
||||
print("PASS: bounded/exact output, overflow, failure, missing executable, closed-output hang, hard deadline, early/active cancellation and child reaping")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user