Skip to content

Fix flaky test_unique_id by making ID counter thread-local - #4

Merged
cmwright merged 1 commit into
mainfrom
fix-flaky-dagre-test
Jun 1, 2026
Merged

Fix flaky test_unique_id by making ID counter thread-local#4
cmwright merged 1 commit into
mainfrom
fix-flaky-dagre-test

Conversation

@cmwright

@cmwright cmwright commented Jun 1, 2026

Copy link
Copy Markdown
Owner

Problem

util::tests::test_unique_id was flaky, intermittently failing with:

assertion `left == right` failed
  left: "_3"
 right: "_2"

Reproduced reliably (~1 in 18 runs of the dagre test binary).

Root cause

The test resets the global ID counter and asserts exact values (_1, _2, foo3). But ID_COUNTER was a process-global AtomicUsize, and Rust runs tests in parallel within a test binary. Other tests that call unique_id concurrently — chiefly via layout()acyclic's unique_id("rev") — bumped the counter between this test's assertions, so the second call returned _3 instead of _2.

Fix

Make the counter thread_local!. Generated IDs only need to be unique within a single graph, and a dagre layout runs entirely on one thread (no rayon / thread spawning in the layout path), so a per-thread counter is correct. Each test thread now gets its own counter, eliminating cross-test interference while preserving the exact-value assertions.

Nothing relies on the counter being shared across threads, and add_dummy_node only consults unique_id on a name collision, so normal layout output (bare _d/_ep/_root prefixes) is unchanged.

Testing

  • Stress-tested the dagre test binary over 150 consecutive runs — all green (previously failed within ~20).
  • Full workspace test suite green.

🤖 Generated with Claude Code

`test_unique_id` reset the global ID counter and asserted exact values
(_1, _2, foo3), but the counter was a process-global AtomicUsize. Rust
runs tests in parallel within a binary, so other tests calling unique_id
(via layout -> acyclic's unique_id("rev")) concurrently bumped the
counter between assertions, intermittently producing "_3" instead of
"_2".

Generated IDs only need to be unique within a single graph, and a layout
runs entirely on one thread, so a thread-local counter is correct and
removes the cross-test interference. dagre layout has no internal
parallelism, and nothing relies on the counter being shared across
threads.

Verified deterministic over 150 consecutive runs of the dagre test
binary; full workspace suite green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@cmwright
cmwright merged commit 00635de into main Jun 1, 2026
1 check passed
@cmwright cmwright mentioned this pull request Jun 9, 2026
cmwright added a commit that referenced this pull request Jun 9, 2026
Bump workspace crates to 0.1.2 and publish the fixes landed since 0.1.1:

- Fix overlapping parallel edges between same node pair (#3)
- Fix flaky test_unique_id: make ID counter thread-local (#4)

Update inter-crate dependency pins (mermaid-core -> mermaid-dagre,
mermaid-rs-cli -> mermaid-core) to 0.1.2.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

1 participant