Skip to content

fix(tests): make dotnet_trx mtime-dependent tests deterministic - #4048

Merged
KuSh merged 2 commits into
rtk-ai:developfrom
TechWizard9999:fix/4037-flaky-dotnet-trx-mtime
Sep 17, 2026
Merged

KuSh merged 2 commits into
rtk-ai:developfrom
TechWizard9999:fix/4037-flaky-dotnet-trx-mtime

Conversation

@TechWizard9999

@TechWizard9999 TechWizard9999 commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4037

Problem: Three tests were flaky because they relied on filesystem mtime granularity and write ordering:

  1. dotnet_trx::tests::test_parse_trx_files_in_dir_since_ignores_older_files
  2. dotnet_trx::tests::test_find_recent_trx_in_dir_picks_newest_trx
  3. dotnet_cmd::tests::test_merge_test_summary_from_trx_ignores_stale_fallback_file

All three used std::thread::sleep (5-10 ms) to create a time gap between file writes, then compared mtimes against a threshold. On filesystems with coarse mtime granularity (1 s on some CI filesystems, FAT 2 s buckets) the mtimes become indistinguishable, so "older" and "newer" can no longer be told apart and the assertion flips.

Solution: set mtimes explicitly with wide gaps (±10 seconds), which is immune to any realistic filesystem granularity.

  • Old files are set to now - 10s, new files to now + 10s, with the threshold at now
  • Uses std::fs::File::set_modified (stable since Rust 1.75, and the crate requires 1.91), so no new dependency is needed
  • Verified: 40 consecutive runs of each test pass deterministically

Note: the first draft of this PR added the filetime crate as a dev-dependency. That introduced a second [dev-dependencies] table in Cargo.toml, which collided with the one develop gained in the edition-2024 series (#3782) and made the merged manifest fail with error: duplicate key. The maintainer fixup in 1281273 switches to the std equivalent, which drops the Cargo.toml/Cargo.lock changes entirely, and extends the same treatment to the third test above.

@CLAassistant

CLAassistant commented Sep 14, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟢 Risk low

Summary

Fixes two flaky tests in dotnet_trx.rs that relied on thread::sleep and small mtime deltas to establish file ordering, which could fail on filesystems with coarse mtime granularity. Replaces this with explicit mtime setting via the filetime crate using wide 10-second gaps, added as a new dev-dependency.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #4037


Analyzed automatically by wshm · This is an automated analysis, not a human review.

@TechWizard9999

Copy link
Copy Markdown
Contributor Author

@KuSh Could you please review it once ?
Thanks!!

@TechWizard9999
TechWizard9999 force-pushed the fix/4037-flaky-dotnet-trx-mtime branch from 5c7dfb4 to e174518 Compare September 14, 2026 11:04
test_parse_trx_files_in_dir_since_ignores_older_files flaked when
filesystem mtime granularity quantized new.trx's mtime at or below
the 'since' threshold, leaving zero summaries and panicking on
.expect("merged summary") (fixes rtk-ai#4037).

Set explicit mtimes (now-10s / now+10s) via the filetime dev crate
instead of deriving them from write order, and apply the same fix to
test_find_recent_trx_in_dir_picks_newest_trx, which relied on a 5ms
sleep plus max_by_key over unspecified read_dir order.
@TechWizard9999
TechWizard9999 force-pushed the fix/4037-flaky-dotnet-trx-mtime branch from e174518 to 9082031 Compare September 14, 2026 11:05
std::fs::File::set_modified has covered this since Rust 1.75, well under
the crate's 1.91 rust-version, so the filetime dev-dependency buys nothing.
Dropping it also drops the Cargo.toml hunk, which added a second
[dev-dependencies] table and made the manifest a duplicate key once develop
grew its own.

Extend the same treatment to the remaining sleep-ordered mtime test,
merge_test_summary_from_trx_ignores_stale_fallback_file, whose 5 ms sleep
left the stale/fresh distinction to filesystem mtime granularity.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

@KuSh KuSh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Round 1 of 3 — APPROVED

Claim: make the two mtime-dependent dotnet_trx tests deterministic by setting mtimes explicitly instead of relying on thread::sleep plus filesystem granularity (Fixes #4037).

Scope: accept. The diagnosis in #4037 is right, and it is fixed at the right level — the ordering guarantee moves from "whatever the filesystem records" to an explicit value.

Ran: two implementation mutations; 40× determinism loop; cargo test --all against the PR merged into current develop; cargo check on that same merge before the fixup.

CI @1281273e: all 11 green, Windows included.

Blocks merge — resolved

  1. Cargo.toml: merged with current develop, the tree did not build. The PR added a [dev-dependencies] table after which = "8"; develop grew its own (temp-env) in the edition-2024 series. Git merges both without a conflict marker, and cargo then rejects the manifest outright:
error: duplicate key
  --> Cargo.toml:60:2
   |
60 | [dev-dependencies]

Your CI was green and still is — it just could not have seen this. Your run finished 2026-09-14 11:05; #3782 landed on develop at 20:21 the same day. Green on the branch, unbuildable after merge.

Fixup pushed — 1281273e

Rather than reshuffle the manifest, the fixup removes the need for one. std::fs::File::set_modified has done this in std since Rust 1.75, comfortably under the crate's rust-version = "1.91", so filetime was not buying anything. Dropping it takes the Cargo.toml and Cargo.lock hunks with it, and that is what makes the merge clean.

It also extends the same treatment to test_merge_test_summary_from_trx_ignores_stale_fallback_file in dotnet_cmd.rs, the one remaining test where a 5 ms sleep was the only thing separating "stale" from "fresh". Forcing that fixture's mtime 1 s forward makes it fail, so the mechanism was live, just with a narrower trigger than the two you found. There are now no sleep-ordered mtime tests left in src/cmds/dotnet/.

Verified on the fixup merged into current develop: one [dev-dependencies] table, cargo fmt --check and cargo clippy --all-targets clean, cargo test --all green (3613 unit + all integration), 40/40 on the two tests.

Checked and correct

  • Your fix does not hollow out either assertion. Flipping max_by_key to min_by_key in find_recent_trx_in_dir still fails test_find_recent_trx_in_dir_picks_newest_trx; disabling the since filter still fails test_parse_trx_files_in_dir_since_ignores_older_files (left: 5, right: 3). That was the thing worth checking — a determinism fix that quietly stops testing anything is the usual way this goes wrong, and yours does not.
  • The future (now + 10s) mtimes are deliberate and harmless: they widen the gap on both sides of since, and the production path only ever reads mtimes.

Your questions

  • "Could you please review it once?" → Done. Good catch and a correct diagnosis; the only thing between it and merge was a manifest collision that appeared on develop nine hours after your CI ran.

Next

Nothing — approving. CI is green on 1281273e.

Rounds: 1/3. Threads: 0 open, 0 resolved.

@KuSh
KuSh merged commit 0a71fcf into rtk-ai:develop Sep 17, 2026
12 checks passed
@rtk-release-bot rtk-release-bot Bot mentioned this pull request Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky test: dotnet_trx test_parse_trx_files_in_dir_since_ignores_older_files depends on file mtimes

3 participants