Implement async event streaming
This commit is contained in:
3
Tests/InotifyIntegrationTests/Utilities/createFile.swift
Normal file
3
Tests/InotifyIntegrationTests/Utilities/createFile.swift
Normal file
@@ -0,0 +1,3 @@
|
||||
func createFile(at path: String, contents: String = "") throws {
|
||||
try contents.write(toFile: path, atomically: false, encoding: .utf8)
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import Inotify
|
||||
|
||||
func getEventsForTrigger(
|
||||
in dir: String,
|
||||
mask: InotifyEventMask,
|
||||
trigger: @escaping (String) async throws -> Void
|
||||
) async throws -> [InotifyEvent] {
|
||||
let watcher = try Inotify()
|
||||
try await watcher.addWatch(path: dir, mask: mask)
|
||||
|
||||
let eventTask = Task {
|
||||
var events: [InotifyEvent] = []
|
||||
for await event in await watcher.events {
|
||||
events.append(event)
|
||||
}
|
||||
return events
|
||||
}
|
||||
|
||||
try await Task.sleep(for: .milliseconds(200))
|
||||
try await trigger(dir)
|
||||
try await Task.sleep(for: .milliseconds(200))
|
||||
|
||||
eventTask.cancel()
|
||||
return await eventTask.value
|
||||
}
|
||||
10
Tests/InotifyIntegrationTests/Utilities/withTempDir.swift
Normal file
10
Tests/InotifyIntegrationTests/Utilities/withTempDir.swift
Normal file
@@ -0,0 +1,10 @@
|
||||
import Foundation
|
||||
|
||||
func withTempDir(_ body: (String) async throws -> Void) async throws {
|
||||
let dir = FileManager.default.temporaryDirectory
|
||||
.appendingPathComponent("InotifyIntegrationTests-\(UUID().uuidString)")
|
||||
.path
|
||||
try FileManager.default.createDirectory(atPath: dir, withIntermediateDirectories: true)
|
||||
defer { try? FileManager.default.removeItem(atPath: dir) }
|
||||
try await body(dir)
|
||||
}
|
||||
Reference in New Issue
Block a user