Skip to content

[RUM-9129] Make ViewLoadingTimeMetrics tests deterministic and faster with controlled time #3154

Merged
hamorillo merged 3 commits intodevelopfrom
hector.morilloprieto/RUM-9129
Jan 27, 2026
Merged

[RUM-9129] Make ViewLoadingTimeMetrics tests deterministic and faster with controlled time #3154
hamorillo merged 3 commits intodevelopfrom
hector.morilloprieto/RUM-9129

Conversation

@hamorillo
Copy link
Contributor

@hamorillo hamorillo commented Jan 26, 2026

What does this PR do?

This PR refactors ViewLoadingTimeMetricsTests to use a controllable time provider instead of Thread.sleep(). It adds an advanceTimeBy() method to StubSDKCore that allows tests to control time advancement, making tests deterministic and significantly faster (~25 seconds saved per test run). All test methods are updated to use explicit time advancement, and DatadogRumMonitor.stopResourceWithError() now uses getEventTime() to respect the controllable time provider.

Before After
image image
image image

Motivation

ViewLoadingTimeMetricsTests contained the same flaky test multiple times, and I believe some of the flakiness was due to how we were managing time within DatadogRumMonitor. We were instantiating Time directly inside the monitor instead of using the values provided by a time provider. With that approach, tests were able to modify time values, which made them non-deterministic because they depended on real time.

With the proposed changes, we can fully control time values and switch to deterministic tests.

Additional Notes

I plan to open a separate PR to remove the default value for eventTime in RumRawEvent so that we use the timeProvider values everywhere.

For example:

internal data class ActionDropped(
    val viewId: String,
    override val eventTime: Time
) : RumRawEvent()

Any reasons not do it?

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
  • Make sure you discussed the feature or bugfix with the maintaining team in an Issue
  • Make sure each commit and the PR mention the Issue number (cf the CONTRIBUTING doc)

…e" instead of instantiating Time() directly.
…ovider instead of Thread.sleep(), making tests deterministic and ~25 seconds faster.

- Adds StubSDKCore.advanceTimeBy() method to advance simulated time, and updates all test methods to use explicit time advancement rather than actual sleeping.
- Updates StopResourceWithError in DatadogRumMonitor to use getEventTime() so it respects the controllable timeProvider.
@datadog-datadog-prod-us1

This comment has been minimized.

@hamorillo
Copy link
Contributor Author

@codex review

@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 85.71429% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 70.92%. Comparing base (47826d1) to head (230ad6c).

Files with missing lines Patch % Lines
.../android/rum/internal/monitor/DatadogRumMonitor.kt 85.71% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #3154      +/-   ##
===========================================
- Coverage    70.94%   70.92%   -0.02%     
===========================================
  Files          912      912              
  Lines        33525    33528       +3     
  Branches      5635     5635              
===========================================
- Hits         23784    23779       -5     
- Misses        8166     8172       +6     
- Partials      1575     1577       +2     
Files with missing lines Coverage Δ
.../android/rum/internal/monitor/DatadogRumMonitor.kt 88.34% <85.71%> (+0.09%) ⬆️

... and 32 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@chatgpt-codex-connector
Copy link

Codex Review: Didn't find any major issues. Hooray!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@hamorillo hamorillo marked this pull request as ready for review January 27, 2026 06:46
@hamorillo hamorillo requested a review from a team as a code owner January 27, 2026 06:46
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 230ad6c0e8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +267 to 270
val eventTime = getEventTime(attributes)
handleEvent(
RumRawEvent.StopResourceWithError(
key,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Apply eventTime to ResourceId error stops consistently

Only the String-key stopResourceWithError now uses getEventTime(attributes); the ResourceId overload later in this file still constructs RumRawEvent.StopResourceWithError without an explicit eventTime, so _dd.timestamp and controllable time providers are ignored for ResourceId-based resource errors (e.g., network instrumentation). When tests advance time via StubSDKCore or when a non-default time provider is used, error events can end up on wall-clock while start/stop resource events use the controlled clock, leading to inconsistent ordering or durations.

Useful? React with 👍 / 👎.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good observation that should probably be fixed. However, this method is most likely not used from cross-platform SDKs, so it is probably fine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to fix it for both ResourceId option and legacy string key option as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ticket created. I’ll fix it in a separate PR. Thanks to both of you. 🙂

Copy link
Contributor

@aleksandr-gringauz aleksandr-gringauz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

@hamorillo hamorillo merged commit 9a70499 into develop Jan 27, 2026
26 checks passed
@hamorillo hamorillo deleted the hector.morilloprieto/RUM-9129 branch January 27, 2026 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants