Skip to content
This repository was archived by the owner on Aug 27, 2026. It is now read-only.

fix(rotation): use toBeCloseTo for time-dependent token assertion - #556

Merged
giuliovv merged 1 commit into
mainfrom
fix/rotation-flaky-test
Apr 28, 2026
Merged

giuliovv merged 1 commit into
mainfrom
fix/rotation-flaky-test

Conversation

@giuliovv

Copy link
Copy Markdown
Collaborator

getTokens() applies sub-millisecond regeneration between consume calls, causing exact equality to flake. Aligns with other time-dependent assertions in the same file.

getTokens() applies sub-millisecond regeneration between consume calls,
causing exact equality to flake. Aligns with other time-dependent
assertions in the same file.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Apr 28, 2026 •

Copy link
Copy Markdown
Contributor

Caution

Review failed

Pull request was closed or merged during review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: c6264605-c1bc-405a-9e18-9eb3c2cf82f3

📥 Commits

Reviewing files that changed from the base of the PR and between 1a57eab and c54de60.

📒 Files selected for processing (1)
  • src/plugin/rotation.test.ts
📜 Recent review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Greptile Review
  • GitHub Check: Test on Node.js
  • GitHub Check: Kilo Code Review
🔇 Additional comments (1)
src/plugin/rotation.test.ts (1)

271-277: LGTM — make the time-dependent token assertion tolerant.

Switching the “handles multiple consumes” check from exact equality to toBeCloseTo(20, 2) matches the flakiness described in the PR and is consistent with the neighboring toBeCloseTo assertions/comments that account for micro-regeneration between consume() and getTokens().


Walkthrough

A test assertion in the TokenBucketTracker test suite was modified to use approximate comparison instead of exact equality, addressing floating-point precision considerations when retrieving token balance after multiple consume operations.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: switching from exact equality to approximate comparison for a time-dependent token assertion in rotation tests.
Description check ✅ Passed The description is directly related to the changeset, explaining why the assertion needed to change (sub-millisecond regeneration causing flakiness) and how it aligns with existing patterns.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@giuliovv
giuliovv merged commit 23d02ff into main Apr 28, 2026
3 of 4 checks passed
@greptile-apps

greptile-apps Bot commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a flaky test assertion in TokenBucketTracker by replacing toBe(20) with toBeCloseTo(20, 2) in the "handles multiple consumes" test. Because getTokens() applies real-time token regeneration on every call, any elapsed time between the last consume() and the expect() produces a non-integer result — the same pattern already addressed in the surrounding tests at lines 255, 268, 304, and 307.

Confidence Score: 5/5

Safe to merge — single test assertion fix with no production code changes.

The change is a one-line test fix that aligns an outlier assertion with the established toBeCloseTo(20, 2) pattern already used in four adjacent tests in the same file. The root cause (time-based regeneration in getTokens()) is real and confirmed by reading the implementation.

No files require special attention.

Important Files Changed

Filename Overview
src/plugin/rotation.test.ts Single-line change replacing toBe(20) with toBeCloseTo(20, 2) to handle sub-millisecond token regeneration between consume() calls, consistent with the same pattern already used in adjacent tests.

Sequence Diagram

sequenceDiagram
    participant T as Test
    participant TBT as TokenBucketTracker
    participant Clock as Date.now()

    T->>TBT: new TokenBucketTracker({ initialTokens: 50 })
    T->>TBT: consume(0, 10) → true
    TBT->>Clock: Date.now() → T1
    Note over TBT: stored: { tokens: 40, lastUpdated: T1 }
    T->>TBT: consume(0, 10) → true
    TBT->>Clock: getTokens calls Date.now() → T2
    Note over TBT: tokens = 40 + regen(T2-T1) ≈ 40
    TBT->>Clock: Date.now() → T2
    Note over TBT: stored: { tokens: ≈30, lastUpdated: T2 }
    T->>TBT: consume(0, 10) → true
    TBT->>Clock: getTokens calls Date.now() → T3
    Note over TBT: tokens = ≈30 + regen(T3-T2) ≈ 30
    TBT->>Clock: Date.now() → T3
    Note over TBT: stored: { tokens: ≈20, lastUpdated: T3 }
    T->>TBT: getTokens(0)
    TBT->>Clock: Date.now() → T4
    Note over TBT: returns ≈20 + regen(T4-T3)
    TBT-->>T: ≈20.000x (not exactly 20)
    Note over T: toBeCloseTo(20, 2) ✓ passes within ±0.005
Loading

Reviews (1): Last reviewed commit: "fix(rotation): use toBeCloseTo for time-..." | Re-trigger Greptile

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant