Skip to content

Commit

Permalink
RUMM-2079 time_spent can't be lower than 1ns
Browse files Browse the repository at this point in the history
  • Loading branch information
maxep committed Apr 5, 2022
1 parent 16a9b24 commit 3c8731a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Sources/Datadog/RUM/RUMMonitor/Scopes/RUMViewScope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ internal class RUMViewScope: RUMScope, RUMContextProvider {

// RUMM-1779 Keep view active as long as we have ongoing resources
let isActive = isActiveView || !resourceScopes.isEmpty

let timeSpent = command.time.timeIntervalSince(viewStartTime)
// RUMM-2079 `time_spent` can't be lower than 1ns
let timeSpent = max(1e-9, command.time.timeIntervalSince(viewStartTime))
let cpuInfo = vitalInfoSampler.cpu
let memoryInfo = vitalInfoSampler.memory
let refreshRateInfo = vitalInfoSampler.refreshRate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class RUMViewScopeTests: XCTestCase {
XCTAssertEqual(event.view.name, "ViewName")
let viewIsActive = try XCTUnwrap(event.view.isActive)
XCTAssertTrue(viewIsActive)
XCTAssertEqual(event.view.timeSpent, 0)
XCTAssertEqual(event.view.timeSpent, 1) // Minimum `time_spent of 1 nanosecond
XCTAssertEqual(event.view.action.count, isInitialView ? 1 : 0, "It must track application start action only if this is an initial view")
XCTAssertEqual(event.view.error.count, 0)
XCTAssertEqual(event.view.resource.count, 0)
Expand Down
42 changes: 42 additions & 0 deletions Tests/DatadogTests/Datadog/RUMMonitorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,48 @@ class RUMMonitorTests: XCTestCase {
}
}

func testViewUpdateIdentifiedByViewController() throws {
let dateProvider = RelativeDateProvider(startingFrom: Date(), advancingBySeconds: 1)
let randomServiceName: String = .mockRandom()
RUMFeature.instance = .mockByRecordingRUMEventMatchers(
directories: temporaryFeatureDirectories,
configuration: .mockWith(
common: .mockWith(serviceName: randomServiceName)
),
dependencies: .mockWith(
dateProvider: dateProvider
)
)
defer { RUMFeature.instance?.deinitialize() }

let monitor = RUMMonitor.initialize()
setGlobalAttributes(of: monitor)

monitor.startView(viewController: mockView)
monitor.stopView(viewController: mockView)
monitor.startView(viewController: mockView)

let rumEventMatchers = try RUMFeature.waitAndReturnRUMEventMatchers(count: 4)
verifyGlobalAttributes(in: rumEventMatchers)
try rumEventMatchers[0].model(ofType: RUMActionEvent.self) { rumModel in
XCTAssertEqual(rumModel.action.type, .applicationStart)
XCTAssertEqual(rumModel.service, randomServiceName)
}
try rumEventMatchers[1].model(ofType: RUMViewEvent.self) { rumModel in
XCTAssertEqual(rumModel.view.action.count, 1)
XCTAssertEqual(rumModel.service, randomServiceName)
}
try rumEventMatchers[2].model(ofType: RUMViewEvent.self) { rumModel in
XCTAssertEqual(rumModel.view.action.count, 1)
XCTAssertEqual(rumModel.view.timeSpent, 1_000_000_000)
XCTAssertEqual(rumModel.service, randomServiceName)
}
try rumEventMatchers[3].model(ofType: RUMViewEvent.self) { rumModel in
XCTAssertEqual(rumModel.view.action.count, 0)
XCTAssertEqual(rumModel.service, randomServiceName)
}
}

// MARK: - Tracking Consent

func testWhenChangingConsentValues_itUploadsOnlyAuthorizedRUMEvents() throws {
Expand Down

0 comments on commit 3c8731a

Please sign in to comment.