Skip to content

Commit 7c8df46

Browse files
test: Fix ANRTrackerTests.clearDirectlyAfterStart (#4773)
The tests testClearDirectlyAfterStart, testClearDirectlyAfterStart_FullyBlocking_NotReported sometimes failed with the started and finished invocations being greater than 1. This is fixed now by asserting the number of starts matches the number of finish invocations.
1 parent bc0c36e commit 7c8df46

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

Tests/SentryTests/Integrations/ANR/SentryANRTrackerV1Tests.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class SentryANRTrackerV1Tests: XCTestCase, SentryANRTrackerDelegate {
189189
wait(for: [anrDetectedExpectation, anrStoppedExpectation], timeout: 0.0)
190190
}
191191

192-
func testClearDirectlyAfterStart() {
192+
func testClearDirectlyAfterStart_FinishesThread() {
193193
anrDetectedExpectation.isInverted = true
194194

195195
let invocations = 10
@@ -201,8 +201,9 @@ class SentryANRTrackerV1Tests: XCTestCase, SentryANRTrackerDelegate {
201201
wait(for: [anrDetectedExpectation, anrStoppedExpectation], timeout: 1)
202202

203203
XCTAssertEqual(0, fixture.threadWrapper.threads.count)
204-
XCTAssertEqual(1, fixture.threadWrapper.threadStartedInvocations.count)
205-
XCTAssertEqual(1, fixture.threadWrapper.threadFinishedInvocations.count)
204+
// As it can take a while until a new thread is started, the thread tracker may start
205+
// and finish multiple times. Most importantly, the code covers every start with one finish.
206+
XCTAssertEqual(fixture.threadWrapper.threadStartedInvocations.count, fixture.threadWrapper.threadFinishedInvocations.count, "The number of started and finished threads should be equal, otherwise the ANR tracker could run.")
206207
}
207208

208209
// swiftlint:disable test_case_accessibility

Tests/SentryTests/Integrations/ANR/SentryANRTrackerV2Tests.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,10 @@ class SentryANRTrackerV2Tests: XCTestCase {
403403

404404
wait(for: [listener.anrDetectedExpectation, listener.anrStoppedExpectation], timeout: self.waitTimeout)
405405

406-
XCTAssertEqual(0, threadWrapper.threads.count)
407-
XCTAssertEqual(1, threadWrapper.threadStartedInvocations.count)
408-
XCTAssertEqual(1, threadWrapper.threadFinishedInvocations.count)
406+
XCTAssertEqual(0, threadWrapper.threads.count)
407+
// As it can take a while until a new thread is started, the thread tracker may start
408+
// and finish multiple times. Most importantly, the code covers every start with one finish.
409+
XCTAssertEqual(threadWrapper.threadStartedInvocations.count, threadWrapper.threadFinishedInvocations.count, "The number of started and finished threads should be equal, otherwise the ANR tracker could run.")
409410
}
410411

411412
func testNoFrameDelayData_FullyBlocking_NotReported() throws {

0 commit comments

Comments
 (0)