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.
This commit is contained in:
T. R. Bernstein
2026-03-12 14:52:33 +01:00
parent ffac6d17a5
commit 76f91f67a6
5 changed files with 123 additions and 2 deletions

View File

@@ -0,0 +1,53 @@
import Foundation
import SystemPackage
func createSubdirectorytree(at dir: String, foldersPerLevel: Int, levels: Int) throws {
let fileManager = FileManager.default
for path in SubfolderTreeIterator(basePath: dir, foldersPerLevel: foldersPerLevel, levels: levels) {
try fileManager.createDirectory(
at: path,
withIntermediateDirectories: true,
attributes: nil
)
}
}
struct SubfolderTreeIterator: IteratorProtocol, Sequence {
let basePath: URL
let foldersPerLevel: Int
let levels: Int
private var indices: [Int]
private var done = false
init(basePath: String, foldersPerLevel: Int, levels: Int) {
self.basePath = URL(filePath: basePath)
self.foldersPerLevel = foldersPerLevel
self.levels = levels
self.indices = Array(repeating: 1, count: levels)
}
mutating func next() -> URL? {
guard !done else { return nil }
let path = indices.reduce(basePath) { partialPath, index in
partialPath.appending(path: "Folder \(index)")
}
// Advance indices (odometer-style, rightmost increments first)
var carry = true
for i in (0..<levels).reversed() {
if carry {
indices[i] += 1
if indices[i] > foldersPerLevel {
indices[i] = 1
} else {
carry = false
}
}
}
if carry { done = true }
return path
}
}

View File

@@ -0,0 +1,22 @@
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)
}
}