Files
swift-inotify/Tests/InotifyIntegrationTests/Utilities/withLowInotifyWatchLimit.swift
T. R. Bernstein 76f91f67a6 Add integration tests for inofity limits
inotify exposes a /proc interface to limit kernel memory usage. If those
limits are set too low, inotify cannot add all watches. The integration
test verifies, that Inotify yields an error in that case.
2026-03-12 14:52:33 +01:00

23 lines
812 B
Swift

import Foundation
func withInotifyWatchLimit(of limit: Int, _ body: () async throws -> Void) async throws {
let confPath = URL(filePath: "/proc/sys/fs/inotify")
let filenames = ["max_user_watches", "max_user_instances", "max_queued_events"]
var previousLimits: [String: String] = [:]
for filename in filenames {
let filePath = confPath.appending(path: filename)
let currentLimit = try String(contentsOf: filePath, encoding: .utf8)
previousLimits[filename] = currentLimit
try "\(limit)".write(to: filePath, atomically: false, encoding: .utf8)
}
try await body()
for filename in filenames {
let filePath = confPath.appending(path: filename)
guard let previousLimit = previousLimits[filename] else { continue }
try previousLimit.write(to: filePath, atomically: false, encoding: .utf8)
}
}