From 8252dfb5b0128107ab32b3448071b10c887507fb Mon Sep 17 00:00:00 2001 From: Prajwal Nadig Date: Wed, 29 Oct 2025 11:22:53 +0000 Subject: [PATCH] Speed up directory monitoring tests The directory monitoring tests check for whether the documentation is rebuilt if the documentation catalog is edited when running a live server with `docc preview`. These tests assumed an extremely generous timeout for receiving the directory change signal, and ran much slower than the rest of the test suite. The timeouts have been reduced to a more reasonable threshold that succeeds even with throttled resources on a single-core machine. --- .../DirectoryMonitorTests.swift | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/Tests/SwiftDocCUtilitiesTests/DirectoryMonitorTests.swift b/Tests/SwiftDocCUtilitiesTests/DirectoryMonitorTests.swift index 555d258222..efd9b23b15 100644 --- a/Tests/SwiftDocCUtilitiesTests/DirectoryMonitorTests.swift +++ b/Tests/SwiftDocCUtilitiesTests/DirectoryMonitorTests.swift @@ -88,10 +88,13 @@ class DirectoryMonitorTests: XCTestCase { } } - /// - Warning: Please do not overuse this method as it takes 10s of wait time and can potentially slow down running the test suite. private func monitorNoUpdates(url: URL, testBlock: @escaping () throws -> Void, file: StaticString = #filePath, line: UInt = #line) throws { + let fileUpdateEvent = expectation(description: "Unexpectedly triggered an update event") + // This test does not expect any file change events + fileUpdateEvent.isInverted = true + let monitor = try DirectoryMonitor(root: url) { rootURL, url in - XCTFail("Did produce file update event for a hidden file", file: file, line: line) + fileUpdateEvent.fulfill() } try monitor.start() @@ -99,17 +102,11 @@ class DirectoryMonitorTests: XCTestCase { monitor.stop() } - let didNotTriggerUpdateForHiddenFile = expectation(description: "Doesn't trigger update") - DispatchQueue.global().async { - try? testBlock() - } - - // For the test purposes we assume a file change event will be delivered within generous 10 seconds. - DispatchQueue.global().asyncAfter(deadline: .now() + 10) { - didNotTriggerUpdateForHiddenFile.fulfill() - } - - wait(for: [didNotTriggerUpdateForHiddenFile], timeout: 20) + // For test purposes, we assume a file change event will be delivered within 1.5 seconds. + // This also aligns with the `monitor()` method above, that ensures that file change events + // in tests are received within 1.5 seconds. If this works too eagerly, then the other tests + // in this suite will fail. + waitForExpectations(timeout: 1.5) } #endif