Skip to content
Merged
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
19 changes: 15 additions & 4 deletions Tests/SamplerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ class SamplerTests: ParselyTestCase {
Timer.scheduledTimer(withTimeInterval: heartbeatDeliveryInterval, repeats: false) { timer in
expectation.fulfill()
}
waitForExpectations(timeout: heartbeatDeliveryInterval, handler: nil)
// Wait slightly longer to reduce the race between waiting for the `expectation` to be
// fulfilled and the fulfillment.
waitForExpectations(timeout: heartbeatDeliveryInterval + 0.01, handler: nil)

let actualUpdatedInterval = samplerUnderTest!.heartbeatInterval
// This value depends on heartbeatInterval, and two magic numbers in the implementation.
Expand Down Expand Up @@ -160,8 +162,12 @@ class SamplerTests: ParselyTestCase {

let accumulatedTime:TimeInterval = samplerUnderTest!.accumulators["sampler-test"]!.accumulatedTime
samplerUnderTest!.pause()
XCTAssert(accumulatedTime >= assertionTimeout - acceptableDifference,
"The sampler should accumulate time constantly after a call to trackKey")
XCTAssertEqual(
accumulatedTime,
assertionTimeout,
accuracy: acceptableDifference,
"The sampler should accumulate time constantly after a call to trackKey"
)

let secondExpectation = self.expectation(description: "Paused sampling")
Timer.scheduledTimer(withTimeInterval: assertionTimeout, repeats: false) { timer in
Expand All @@ -170,6 +176,11 @@ class SamplerTests: ParselyTestCase {
waitForExpectations(timeout: assertionTimeout + acceptableDifference, handler: nil)

let secondAccumulatedTime: TimeInterval = samplerUnderTest!.accumulators["sampler-test"]!.accumulatedTime
XCTAssert(secondAccumulatedTime <= assertionTimeout, "AccumulatedTime was \(secondAccumulatedTime)")
XCTAssertEqual(
secondAccumulatedTime,
assertionTimeout,
accuracy: acceptableDifference,
"AccumulatedTime was \(secondAccumulatedTime)"
)
}
}