Repository navigation
test(cmux): add rapid spawn/kill IOSurface fixture #4
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| import XCTest | ||
|
|
||
| final class RapidSpawnKillFixtureTests: XCTestCase { | ||
| private struct ProcessResult { | ||
| let status: Int32 | ||
| let stdout: String | ||
| let stderr: String | ||
| let timedOut: Bool | ||
| } | ||
|
|
||
| func testRapidSpawnKillFixtureKeepsIOSurfaceFootprintUnderBudget() throws { | ||
| let repositoryRoot = try Self.repositoryRoot() | ||
| let fixtureURL = repositoryRoot | ||
| .appendingPathComponent("tests/fixtures/rapid_spawn_kill.sh") | ||
|
|
||
| XCTAssertTrue( | ||
| FileManager.default.isExecutableFile(atPath: fixtureURL.path), | ||
| "Expected executable fixture at \(fixtureURL.path)" | ||
| ) | ||
|
|
||
| let appURL = try Self.cmuxAppURL() | ||
| let thresholdMB = ProcessInfo.processInfo.environment["CMUX_RAPID_SPAWN_KILL_IOSURFACE_LIMIT_MB"] | ||
| .flatMap(Double.init) ?? 50 | ||
| let result = runProcess( | ||
| executablePath: "/usr/bin/leaks", | ||
| arguments: [ | ||
| "--atExit", | ||
| "--", | ||
| "/bin/bash", | ||
| fixtureURL.path, | ||
| ], | ||
| environment: [ | ||
| "CMUX_RAPID_SPAWN_KILL_APP_PATH": appURL.path, | ||
| "CMUX_RAPID_SPAWN_KILL_ITERATIONS": "3", | ||
| "CMUX_RAPID_SPAWN_KILL_FORCE_WINDOW": "1", | ||
| "CMUX_RAPID_SPAWN_KILL_READY_TIMEOUT_MS": "8000", | ||
| ], | ||
| timeout: 90 | ||
| ) | ||
|
|
||
| let combinedOutput = [result.stdout, result.stderr] | ||
| .filter { !$0.isEmpty } | ||
| .joined(separator: "\n") | ||
| let attachment = XCTAttachment(string: combinedOutput) | ||
| attachment.name = "rapid-spawn-kill-leaks-output" | ||
| attachment.lifetime = .keepAlways | ||
| add(attachment) | ||
|
|
||
| XCTAssertFalse(result.timedOut, combinedOutput) | ||
| XCTAssertEqual(result.status, 0, combinedOutput) | ||
|
|
||
| let measuredMB = try XCTUnwrap( | ||
| Self.parseIOSurfaceFootprintMB(from: combinedOutput), | ||
| "Expected fixture output to include 'VM: IOSurface = <N> MB'. Output:\n\(combinedOutput)" | ||
| ) | ||
| XCTAssertGreaterThan( | ||
| measuredMB, | ||
| 0, | ||
| "Expected rapid_spawn_kill.sh to force an IOSurface allocation. Output:\n\(combinedOutput)" | ||
| ) | ||
| XCTAssertLessThanOrEqual( | ||
| measuredMB, | ||
| thresholdMB, | ||
| "VM: IOSurface exceeded \(thresholdMB) MB after rapid spawn/kill loop. Output:\n\(combinedOutput)" | ||
| ) | ||
| } | ||
|
|
||
| private static func repositoryRoot(filePath: String = #filePath) throws -> URL { | ||
| var url = URL(fileURLWithPath: filePath) | ||
| while url.path != "/" { | ||
| let candidate = url | ||
| .deletingLastPathComponent() | ||
| .appendingPathComponent("GhosttyTabs.xcodeproj") | ||
| if FileManager.default.fileExists(atPath: candidate.path) { | ||
| return url.deletingLastPathComponent() | ||
| } | ||
| url.deleteLastPathComponent() | ||
| } | ||
| throw XCTSkip("Unable to locate repository root from \(filePath)") | ||
| } | ||
|
|
||
| private static func cmuxAppURL() throws -> URL { | ||
| let environment = ProcessInfo.processInfo.environment | ||
|
|
||
| if let override = environment["CMUX_RAPID_SPAWN_KILL_APP_PATH"], !override.isEmpty { | ||
| let url = URL(fileURLWithPath: override) | ||
| if FileManager.default.fileExists(atPath: url.path) { | ||
| return url | ||
| } | ||
| } | ||
|
|
||
| if let testHost = environment["TEST_HOST"], !testHost.isEmpty, | ||
| let appURL = enclosingAppBundle(for: URL(fileURLWithPath: testHost)) { | ||
| return appURL | ||
| } | ||
|
|
||
| if let builtProductsDir = environment["BUILT_PRODUCTS_DIR"], !builtProductsDir.isEmpty { | ||
| let appURL = URL(fileURLWithPath: builtProductsDir) | ||
| .appendingPathComponent("cmux DEV.app") | ||
| if FileManager.default.fileExists(atPath: appURL.path) { | ||
| return appURL | ||
| } | ||
| } | ||
|
|
||
| if let appURL = enclosingAppBundle(for: Bundle.main.bundleURL) { | ||
| return appURL | ||
| } | ||
|
|
||
| throw XCTSkip("Unable to locate built cmux app bundle for rapid_spawn_kill.sh") | ||
| } | ||
|
|
||
| private static func enclosingAppBundle(for url: URL) -> URL? { | ||
| var current = url | ||
| while current.path != "/" { | ||
| if current.pathExtension == "app" && FileManager.default.fileExists(atPath: current.path) { | ||
| return current | ||
| } | ||
| current.deleteLastPathComponent() | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| private static func parseIOSurfaceFootprintMB(from output: String) -> Double? { | ||
| let pattern = #"VM: IOSurface\s*=\s*([0-9]+(?:\.[0-9]+)?)\s*MB"# | ||
| guard let regex = try? NSRegularExpression(pattern: pattern) else { return nil } | ||
| let range = NSRange(output.startIndex..<output.endIndex, in: output) | ||
| guard | ||
| let match = regex.firstMatch(in: output, range: range), | ||
| let valueRange = Range(match.range(at: 1), in: output) | ||
| else { | ||
| return nil | ||
| } | ||
| return Double(output[valueRange]) | ||
| } | ||
|
|
||
| private func runProcess( | ||
| executablePath: String, | ||
| arguments: [String], | ||
| environment: [String: String], | ||
| timeout: TimeInterval | ||
| ) -> ProcessResult { | ||
| let process = Process() | ||
| let stdoutPipe = Pipe() | ||
| let stderrPipe = Pipe() | ||
|
|
||
| process.executableURL = URL(fileURLWithPath: executablePath) | ||
| process.arguments = arguments | ||
| var mergedEnvironment = ProcessInfo.processInfo.environment | ||
| for (key, value) in environment { | ||
| mergedEnvironment[key] = value | ||
| } | ||
| process.environment = mergedEnvironment | ||
| process.standardInput = FileHandle.nullDevice | ||
| process.standardOutput = stdoutPipe | ||
| process.standardError = stderrPipe | ||
|
|
||
| do { | ||
| try process.run() | ||
| } catch { | ||
| return ProcessResult( | ||
| status: -1, | ||
| stdout: "", | ||
| stderr: String(describing: error), | ||
| timedOut: false | ||
| ) | ||
| } | ||
|
|
||
| let exitSignal = DispatchSemaphore(value: 0) | ||
| DispatchQueue.global(qos: .userInitiated).async { | ||
| process.waitUntilExit() | ||
| exitSignal.signal() | ||
| } | ||
|
|
||
| let timedOut = exitSignal.wait(timeout: .now() + timeout) == .timedOut | ||
| if timedOut { | ||
| process.terminate() | ||
| _ = exitSignal.wait(timeout: .now() + 2) | ||
| } | ||
|
|
||
| return ProcessResult( | ||
| status: process.terminationStatus, | ||
| stdout: String(data: stdoutPipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? "", | ||
| stderr: String(data: stderrPipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? "", | ||
| timedOut: timedOut | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # cmux Test Fixtures | ||
|
|
||
| ## rapid_spawn_kill.sh | ||
|
|
||
| `rapid_spawn_kill.sh` is a stress fixture for the cmux IOSurface regression harness. It repeatedly launches a fresh cmux app process, waits only long enough for the child process to expose `vmmap -summary` data, samples the `IOSurface` resident + swapped footprint, terminates the child, and immediately starts the next iteration. | ||
|
|
||
| The default loop count is 100 to force Mach-port and IOSurface churn without inter-iteration settle time. XCTest uses a lower count for targeted verification so the fixture remains practical in local and CI runs. | ||
|
|
||
| Useful environment variables: | ||
|
|
||
| - `CMUX_RAPID_SPAWN_KILL_APP_PATH`: path to the cmux `.app` bundle. Defaults to `/Applications/cmux.app`. | ||
| - `CMUX_RAPID_SPAWN_KILL_EXECUTABLE_PATH`: direct cmux executable override. | ||
| - `CMUX_RAPID_SPAWN_KILL_ITERATIONS`: loop count. Defaults to `100`. | ||
| - `CMUX_RAPID_SPAWN_KILL_READY_TIMEOUT_MS`: maximum per-child startup sampling wait. Defaults to `2500`. | ||
| - `CMUX_RAPID_SPAWN_KILL_TMPDIR`: scratch directory for per-iteration sockets and logs. | ||
|
|
||
| Example: | ||
|
|
||
| ```bash | ||
| CMUX_RAPID_SPAWN_KILL_APP_PATH="/Applications/cmux.app" \ | ||
| CMUX_RAPID_SPAWN_KILL_ITERATIONS=100 \ | ||
| tests/fixtures/rapid_spawn_kill.sh | ||
| ``` | ||
|
|
||
| The fixture prints `VM: IOSurface = <N> MB`; `RapidSpawnKillFixtureTests` runs it under `leaks --atExit` and asserts that value stays under the configured threshold. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Unused force-window env var has no effect
Medium Severity
The
CMUX_RAPID_SPAWN_KILL_FORCE_WINDOWenvironment variable is set by the test to force IOSurface allocation, but therapid_spawn_kill.shfixture and thecmuxapp don't consume it. This makes the variable inert, and the test's assertion for IOSurface allocation relies on defaultcmuxbehavior, which could be misleading or flaky.Reviewed by Cursor Bugbot for commit 78a38b0. Configure here.