Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MainThreadMonitor: don't crash if there is no test in progress #2838

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Tests/BackendIntegrationTests/MainThreadMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
}
}
Expand Down
4 changes: 4 additions & 0 deletions Tests/UnitTests/TestHelpers/CurrentTestCaseTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down