Skip to content

Commit aa0ceca

Browse files
authored
test: Fix flaky HangTracker deallocated test (#7639)
1 parent 11cad11 commit aa0ceca

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

Sources/Swift/HangTracker.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ final class DefaultHangTracker<T: RunLoopObserver> {
7676
mainQueueState = .init()
7777
}
7878

79+
// It's safe to access mainQueueState here regardless of the thread
80+
// because this is the only reference to `self` while
81+
// it is being deallocated.
7982
deinit {
8083
guard let observer = mainQueueState.observer else {
8184
return

Tests/SentryTests/Integrations/ANR/HangTrackerTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ final class HangTrackerTests: XCTestCase {
208208

209209
sut = nil
210210

211+
// Allow the hang tracker's background thread to finish since it holds
212+
// a strong reference while it is running
213+
let expectation = XCTestExpectation()
214+
queue.async {
215+
expectation.fulfill()
216+
}
217+
wait(for: [expectation])
218+
211219
XCTAssertNil(weakSut, "Expected observer to be deallocated")
212220
}
213221

0 commit comments

Comments
 (0)