-
Notifications
You must be signed in to change notification settings - Fork 404
[circt-lsp-server] Make time source injectable #9082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
8c2dbd6 to
f9b6de3
Compare
|
Cool, thank you for improving this! We need to make sure not to use raw now()/sleep_for() but that sounds reasonable |
Thanks for spotting the issue and fixing it on short notice! |
6599683 to
ebf3012
Compare
uenoku
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thank you for improving this! Maybe we might want to check abort is called in deconstructor of PendingChangesMap (RAII also works, but I personally prefer requiring explicit abort ).
ebf3012 to
1ade794
Compare
That's a good point; I added a destructor that calls abort. This should help avoid accidental thread orphaning, and if there are no threads to wait on, this should also not cost anything if |
892431b to
46730e4
Compare
Introduce an injectable clock and test-only wait shim to remove real sleeps
from `PendingChanges` logic in tests, while preserving production behavior.
- Add an **injectable time source**:
- `using SteadyClock = std::chrono::steady_clock;`
- `using NowFn = std::function<SteadyClock::time_point()>;`
- Store `NowFn nowFn` and a `bool useManualClock` in `PendingChangesMap`.
- Replace direct calls to `std::chrono::steady_clock::now()` with `nowFn()`.
- Add `waitForMinMs(uint64_t ms, SteadyClock::time_point start)`:
- **Prod path**: `std::this_thread::sleep_for(ms)`.
- **Test path** (`useManualClock`): busy-wait with `std::this_thread::yield()` until `nowFn()` reaches target.
- New ctor overload for tests:
- `PendingChangesMap(unsigned maxThreads, NowFn now)` -> enables manual clock mode.
- Dtor now calls `abort()` to ensure all pending work is cleared on teardown.
- Replace sleep in debounce worker:
- `sleep_for(ms)` -> `waitForMinMs(ms, scheduleTime)`.
- Add `ManualClock` with manual advancement (`advanceMs`) and `now()` accessor.
- Utility `advanceTestTime(clock, ms)` that advances test time and yields.
- `CallbackCapture::waitFor()` now uses a tight fixed 100ms window (no per-call timeout arg).
- Refactor tests to:
- Construct `PendingChangesMap` with manual clock (`PendingChangesMap(2, [&]{ return clock.now(); })`).
- Advance time deterministically instead of sleeping.
- Validate debounce min/obsolete/max-cap behaviors without flakiness.
- Adjust debounce cap in `MaxCapForcesFlushDuringContinuousTyping` to 1ms to tighten assertions.
- Sprinkle end-of-test large time advances to flush any queued workers.
46730e4 to
72bca03
Compare
Introduce an injectable clock and test-only wait shim to remove real sleeps
from
PendingChangeslogic in tests, while preserving production behavior.PendingChangesMap Changes
using SteadyClock = std::chrono::steady_clock;using NowFn = std::function<SteadyClock::time_point()>;NowFn nowFnand abool useManualClockinPendingChangesMap.std::chrono::steady_clock::now()withnowFn().waitForMinMs(uint64_t ms, SteadyClock::time_point start):std::this_thread::sleep_for(ms).useManualClock): busy-wait withstd::this_thread::yield()untilnowFn()reaches target.PendingChangesMap(unsigned maxThreads, NowFn now)-> enables manual clock mode.abort()to ensure all pending work is cleared on teardown.sleep_for(ms)->waitForMinMs(ms, scheduleTime).PendingChangesMap Unit Test Changes
ManualClockwith manual advancement (advanceMs) andnow()accessor.advanceTestTime(clock, ms)that advances test time and yields.CallbackCapture::waitFor()now uses a tight fixed 1ms window (no per-call timeout arg).PendingChangesMapwith manual clock (PendingChangesMap(2, [&]{ return clock.now(); })).MaxCapForcesFlushDuringContinuousTypingto 100ms to tighten assertions.