Skip to content

fix(test): Make jitter deterministic via config override in the flusher test - #69264

Merged
haacked merged 3 commits into
masterfrom
mendral/fix-flusher-test-jitter
Jul 9, 2026
Merged

fix(test): Make jitter deterministic via config override in the flusher test#69264
haacked merged 3 commits into
masterfrom
mendral/fix-flusher-test-jitter

Conversation

@mendral-app

@mendral-app mendral-app Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add a #[cfg(test)] jitter_override field to BillingAggregatorConfig so test_flusher_swallows_initial_tick_then_flushes_on_interval can pin jitter to a known value (100ms), eliminating the non-deterministic timing race
  • The test was flaky (~21 failures over 36 days on master) because pick_jitter uses rand::thread_rng(), making the interval creation time unpredictable under paused tokio time

How it works

With jitter_override: Some(Duration::from_millis(100)), the flusher sleeps for exactly 100ms. After advance(101ms), it wakes and creates the interval at t≈100ms. The first real tick fires at t=1100ms. The test advances to t=1099ms and asserts zero flushes — this now passes deterministically regardless of RNG state.

Insight

https://app.mendral.com/insights/01KT4HT5VY3AR9V53KRG28G9N4


Note

Created by Mendral. Tag @mendral-app with feedback or questions.

mendral-app Bot added 2 commits July 8, 2026 05:23
Add a #[cfg(test)] jitter_override field to BillingAggregatorConfig so
the flusher timing test can pin jitter to a known value (100ms),
eliminating the non-deterministic timing race caused by
rand::thread_rng() in pick_jitter().

The test_flusher_swallows_initial_tick_then_flushes_on_interval test
was flaky (~21 failures over 36 days) because random jitter made the
interval creation time unpredictable under paused tokio time.
The field needs to exist in non-test builds too since clippy runs with
--all-targets (which enables cfg(test)) and config.rs constructs the
struct outside of #[cfg(test)] modules. The field is #[doc(hidden)]
and defaults to None with zero runtime cost.
@mendral-app
mendral-app Bot marked this pull request as ready for review July 8, 2026 12:58
@pr-assigner-resolver-posthog
pr-assigner-resolver-posthog Bot requested a review from a team July 8, 2026 12:58
@posthog-project-board-bot posthog-project-board-bot Bot moved this to In Review in Feature Flags Jul 8, 2026
@trunk-io

trunk-io Bot commented Jul 8, 2026

Copy link
Copy Markdown

Static BadgeStatic BadgeStatic BadgeStatic Badge

View Full Report ↗︎Docs

@haacked haacked self-assigned this Jul 9, 2026
…in mapping test

- fix off-by-one-ms timing comments in test_flusher_swallows_initial_tick_then_flushes_on_interval (t=1101ms, not t=1100ms)
- clarify the jitter is pinned, not a random range, in this test
- assert jitter_override stays None in get_billing_aggregator_config_maps_fields_correctly
@haacked haacked added the stamphog Request AI approval (no full review) label Jul 9, 2026
@stamphog

stamphog Bot commented Jul 9, 2026

Copy link
Copy Markdown

stamphog does not review bot-authored PRs — removing the stamphog label. This change needs a human reviewer.

@stamphog stamphog Bot removed the stamphog Request AI approval (no full review) label Jul 9, 2026
@haacked
haacked enabled auto-merge (squash) July 9, 2026 19:28

@haacked haacked left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@github-project-automation github-project-automation Bot moved this from In Review to Approved in Feature Flags Jul 9, 2026
@haacked
haacked merged commit 443491b into master Jul 9, 2026
257 of 297 checks passed
@haacked
haacked deleted the mendral/fix-flusher-test-jitter branch July 9, 2026 19:33
@github-project-automation github-project-automation Bot moved this from Approved to Done in Feature Flags Jul 9, 2026
@deployment-status-posthog

deployment-status-posthog Bot commented Jul 9, 2026

Copy link
Copy Markdown

Deploy status

Environment Status Deployed At Workflow
dev ✅ Deployed 2026-07-09 20:07 UTC Run
prod-us ✅ Deployed 2026-07-09 20:20 UTC Run
prod-eu ✅ Deployed 2026-07-09 20:27 UTC Run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant