diff --git a/Tests/BackendIntegrationTests/MainThreadMonitor.swift b/Tests/BackendIntegrationTests/MainThreadMonitor.swift index 41dbd75eed..6c0898dd53 100644 --- a/Tests/BackendIntegrationTests/MainThreadMonitor.swift +++ b/Tests/BackendIntegrationTests/MainThreadMonitor.swift @@ -44,9 +44,13 @@ final class MainThreadMonitor { let deadline = DispatchTime.now() + Self.threshold + Self.checkInterval let result = semaphore.wait(timeout: deadline) + // `XCTest` sometimes blocks the main thread at the end of a test. + // We don't want to detect that as a deadlock. + let timedOutDuringTest = CurrentTestCaseTracker.shared.testInProgress && result == .timedOut + precondition( - result != .timedOut, - "Main thread was blocked for more than \(Self.threshold.seconds) seconds" + !timedOutDuringTest, + "Main thread was blocked for more than \(Self.threshold.seconds) seconds during a test" ) } } diff --git a/Tests/UnitTests/TestHelpers/CurrentTestCaseTracker.swift b/Tests/UnitTests/TestHelpers/CurrentTestCaseTracker.swift index 057191993c..8da0914838 100644 --- a/Tests/UnitTests/TestHelpers/CurrentTestCaseTracker.swift +++ b/Tests/UnitTests/TestHelpers/CurrentTestCaseTracker.swift @@ -28,6 +28,10 @@ final class CurrentTestCaseTracker: NSObject, XCTestObservation { currentTestCase = nil } + var testInProgress: Bool { + return self.currentTestCase != nil + } + /// Extracts the name of the current running test. /// /// Example: extracts `testLoginCachesForSameUserIDs`