Skip to content

Commit

Permalink
Merge pull request #702 from DataDog/maxep/RUMM-1779/keep-view-active
Browse files Browse the repository at this point in the history
RUMM-1779 Keep view active until all resources are consumed
  • Loading branch information
maxep authored Dec 31, 2021
2 parents e3468ed + 92a8105 commit 87161de
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
6 changes: 4 additions & 2 deletions Sources/Datadog/RUM/RUMMonitor/Scopes/RUMViewScope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,10 @@ internal class RUMViewScope: RUMScope, RUMContextProvider {
version += 1
attributes.merge(rumCommandAttributes: command.attributes)

let timeSpent = command.time.timeIntervalSince(viewStartTime)
// RUMM-1779 Keep view active as long as we have ongoing resources
let isActive = isActiveView || !resourceScopes.isEmpty

let timeSpent = command.time.timeIntervalSince(viewStartTime)
let cpuInfo = vitalInfoSampler.cpu
let memoryInfo = vitalInfoSampler.memory
let refreshRateInfo = vitalInfoSampler.refreshRate
Expand Down Expand Up @@ -384,7 +386,7 @@ internal class RUMViewScope: RUMScope, RUMContextProvider {
frozenFrame: .init(count: frozenFramesCount),
id: viewUUID.toRUMDataFormat,
inForegroundPeriods: nil,
isActive: isActiveView,
isActive: isActive,
isSlowRendered: isSlowRendered,
largestContentfulPaint: nil,
loadEvent: nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,14 +416,19 @@ class RUMViewScopeTests: XCTestCase {
scope.process(command: RUMStopResourceCommand.mockWith(resourceKey: "/resource/1")),
"The View should be kept alive as all its Resources haven't yet finished loading"
)

var event = try XCTUnwrap(output.recordedEvents(ofType: RUMEvent<RUMViewEvent>.self).last)
XCTAssertTrue(event.model.view.isActive ?? false, "View should stay active")

XCTAssertFalse(
scope.process(command: RUMStopResourceWithErrorCommand.mockWithErrorMessage(resourceKey: "/resource/2")),
"The View should stop as all its Resources finished loading"
)

let event = try XCTUnwrap(output.recordedEvents(ofType: RUMEvent<RUMViewEvent>.self).last)
event = try XCTUnwrap(output.recordedEvents(ofType: RUMEvent<RUMViewEvent>.self).last)
XCTAssertEqual(event.model.view.resource.count, 1, "View should record 1 successful Resource")
XCTAssertEqual(event.model.view.error.count, 1, "View should record 1 error due to second Resource failure")
XCTAssertFalse(event.model.view.isActive ?? true, "View should be inactive")
}

// MARK: - User Action Tracking
Expand Down
33 changes: 18 additions & 15 deletions Tests/DatadogTests/Matchers/RUMSessionMatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,23 +213,26 @@ internal class RUMSessionMatcher {

// Validate ViewVisit's view.isActive for each events
try visits.forEach { visit in
var viewWasPreviouslyActive = false
var viewIsInactive = false
try visit.viewEvents.enumerated().forEach { index, viewEvent in
let viewIsActive = viewEvent.view.isActive!
if index == 0 {
if !viewIsActive {
throw RUMSessionConsistencyException(
description: "A `RUMSessionMatcher.ViewVisit` can't have a first event with an inactive `View`."
)
}
} else {
if !viewWasPreviouslyActive && viewIsActive {
throw RUMSessionConsistencyException(
description: "A `RUMSessionMatcher.ViewVisit` can't have an event where a `View` is active after the `View` was marked as inactive."
)
}
guard let viewIsActive = viewEvent.view.isActive else {
throw RUMSessionConsistencyException(
description: "A `RUMSessionMatcher.ViewVisit` can't have an event without the `isActive` parameter set."
)
}

if index == 0 && !viewIsActive {
throw RUMSessionConsistencyException(
description: "A `RUMSessionMatcher.ViewVisit` can't have a first event with an inactive `View`."
)
}

if viewIsInactive {
throw RUMSessionConsistencyException(
description: "A `RUMSessionMatcher.ViewVisit` can't have an event after the `View` was marked as inactive."
)
}
viewWasPreviouslyActive = viewIsActive
viewIsInactive = !viewIsActive
}
}

Expand Down

0 comments on commit 87161de

Please sign in to comment.