-
Notifications
You must be signed in to change notification settings - Fork 0
📝 CodeRabbit Chat: Implement requested code changes #131
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
Open
coderabbitai
wants to merge
1
commit into
issue-26-use-deterministic-synchronisation-in-routine-heartbeat-rs
Choose a base branch
from
coderabbitai/chat/93d9d37
base: issue-26-use-deterministic-synchronisation-in-routine-heartbeat-rs
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| //! Deterministic synchronisation helpers for routine engine E2E tests. | ||
| //! | ||
| //! Provides `wait_for_idle` and `wait_for_persisted_run` for coordinating | ||
| //! asynchronous routine execution and database persistence in tests that | ||
| //! trigger `RoutineEngine`'s task-spawning paths. | ||
| //! | ||
| //! This module is intentionally **not** declared in `support/mod.rs`. It is | ||
| //! included directly from `tests/e2e_traces.rs` so that it is only compiled | ||
| //! into the test binary that actually calls these helpers, avoiding spurious | ||
| //! `dead_code` warnings in unrelated test binaries without requiring any lint | ||
| //! suppression. | ||
|
|
||
| #![cfg(feature = "libsql")] | ||
|
|
||
| use std::sync::Arc; | ||
| use std::sync::atomic::Ordering; | ||
| use std::time::Duration; | ||
|
|
||
| use uuid::Uuid; | ||
|
|
||
| use ironclaw::agent::routine_engine::RoutineEngine; | ||
| use ironclaw::db::Database; | ||
|
|
||
| /// Polls until the engine's running count reaches zero or the timeout expires. | ||
| /// | ||
| /// This provides deterministic synchronisation for tests that need to wait | ||
| /// for asynchronous routine execution to complete, eliminating timing-dependent | ||
| /// flakiness without slowing down the test suite on fast machines. | ||
| /// | ||
| /// **Note:** Combine with [`wait_for_persisted_run`] to ensure both execution | ||
| /// completion and database persistence, as the running count may reach zero | ||
| /// before the database record is fully committed. | ||
| pub async fn wait_for_idle(engine: &RoutineEngine, timeout: Duration) { | ||
| let start = std::time::Instant::now(); | ||
| let poll_interval = Duration::from_millis(10); | ||
|
|
||
| loop { | ||
| let count = engine.running_count().load(Ordering::SeqCst); | ||
| if count == 0 { | ||
| return; | ||
| } | ||
|
|
||
| if start.elapsed() >= timeout { | ||
| panic!( | ||
| "Timeout waiting for engine to become idle (running count: {})", | ||
| count | ||
| ); | ||
| } | ||
|
|
||
| tokio::time::sleep(poll_interval).await; | ||
| } | ||
| } | ||
|
|
||
| /// Polls until a routine run is persisted in the database or the timeout expires. | ||
| /// | ||
| /// This helper provides deterministic synchronisation for database persistence, | ||
| /// complementing [`wait_for_idle`] which only waits for in-memory execution | ||
| /// completion. Call this after `wait_for_idle` to ensure the routine run is | ||
| /// durably recorded before asserting on persisted state. | ||
| /// | ||
| /// # Arguments | ||
| /// * `db` - The database to query for persisted runs. | ||
| /// * `routine_id` - The ID of the routine to check for runs. | ||
| /// * `timeout` - Maximum duration to wait for persistence. | ||
| pub async fn wait_for_persisted_run(db: &Arc<dyn Database>, routine_id: Uuid, timeout: Duration) { | ||
| let start = std::time::Instant::now(); | ||
| let poll_interval = Duration::from_millis(10); | ||
| let max_attempts: u32 = 500; // At 10ms intervals, this is ~5 seconds. | ||
|
|
||
| let mut attempts: u32 = 0; | ||
| loop { | ||
| let runs = db | ||
| .list_routine_runs(routine_id, 10) | ||
| .await | ||
| .expect("list_routine_runs should not fail"); | ||
|
|
||
| if !runs.is_empty() { | ||
| return; | ||
| } | ||
|
|
||
| attempts += 1; | ||
| if attempts >= max_attempts || start.elapsed() >= timeout { | ||
| panic!( | ||
| "Timeout waiting for routine run to be persisted (routine_id: {routine_id}, attempts: {attempts})" | ||
| ); | ||
| } | ||
|
|
||
| tokio::time::sleep(poll_interval).await; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
suggestion (bug_risk): Avoid having both
timeoutand a hard-codedmax_attemptsto prevent surprising behaviour in long-running testsBecause
max_attemptsis tied to a fixed 5s window whiletimeoutis configurable, callers using a longertimeout(e.g. 15s in slow CI) would still hit themax_attemptscondition and panic after ~5s. To keep behavior aligned with the requestedtimeout, either derivemax_attemptsfromtimeoutandpoll_interval(e.g.timeout / poll_interval) or removemax_attemptsand rely solely ontimeout.Suggested implementation:
Depending on the exact current implementation, you may also need to:
timeoutandpoll_intervalare bothDurationvalues already in scope at the point wheremax_attemptsis defined.max_attemptsis typed as something other thanu32(e.g.usize), adjust the cast to match:usize:as usizeinstead ofas u32.timeout(e.g. "waits up totimeoutfor persistence").tests/support/routine_sync.rsthat define a hard-codedmax_attempts(for idle or persistence helpers), apply the same pattern to each one so all of them respect the configuredtimeout.