Skip to content

Conversation

@keivenchang
Copy link
Contributor

@keivenchang keivenchang commented Aug 20, 2025

Overview:

Refactors test helper functions to eliminate code duplication by consolidating shared test utilities into a centralized module and cleaning up unused imports.

Details:

  • Improved code maintainability by reducing duplication across test modules. I consolidated create_test_drt_async and create_test_drt_with_settings_async functions from multiple modules into distributed::test_helpers
  • Removed unused imports from system_status_server.rs (HashMap, body, DefaultMakeSpan, TraceParent)
  • Reorganized test modules to clearly separate regular tests from integration tests
  • Cleaned up redundant feature gate attributes within integration test modules

Where should the reviewer start?

  • lib/runtime/src/distributed.rs - Review the new test_helpers module with consolidated test functions
  • lib/runtime/src/system_status_server.rs - Check the reorganized test module structure and import cleanup
  • lib/runtime/src/metrics.rs - Verify updated imports now use the centralized test helpers

Summary by CodeRabbit

  • Tests

    • Centralized test helpers for distributed runtime, reducing duplication.
    • Reorganized integration tests under a single gated module; unit tests remain separate.
    • Simplified configuration gates so some tests run unconditionally.
    • Updated tests to use shared helpers for runtime creation and server info.
  • Chores

    • Cleaned up imports and minor formatting across test-related code.

No user-facing changes; functionality remains unchanged.

…lpers module

- Move create_test_drt_async and create_test_drt_with_settings_async from metrics and system_status_server modules to centralized location
- Update all test imports to use the consolidated helpers
- Eliminate code duplication across test modules
…us_server

- Remove unused imports (HashMap, body, DefaultMakeSpan, TraceParent)
- Consolidate std::sync imports
- Separate regular tests from integration tests into distinct modules
- Remove redundant feature gates inside integration_tests module
- Move test command comments to appropriate module locations
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 20, 2025

Walkthrough

Centralizes DistributedRuntime test helpers in distributed.rs under cfg(test) with optional integration feature. Removes duplicate helper from metrics.rs and updates tests to import the new helpers. Reorganizes system_status_server.rs tests into unit vs. integration modules and cleans up imports. No production logic changes.

Changes

Cohort / File(s) Summary of Changes
Distributed test helpers
lib/runtime/src/distributed.rs
Added test_helpers module (cfg(test)) exposing async helpers create_test_drt_async and create_test_drt_with_settings_async behind feature = "integration". Helpers construct DistributedRuntime without discovery, optionally loading settings.
Metrics tests refactor
lib/runtime/src/metrics.rs
Removed internal test_helpers::create_test_drt_async. Updated tests to import and use crate::distributed::test_helpers::create_test_drt_async. Removed per-test integration gating for test_metrics_registry_entry_callbacks. No production code changes.
System status server tests reorg & import cleanup
lib/runtime/src/system_status_server.rs
Cleaned imports (removed unused types; consolidated std::sync). Split tests: unit tests remain; integration tests moved under mod integration_tests with #[cfg(all(test, feature = "integration"))]. Tests now use distributed test helpers and fetch server address via system_status_server_info(). Minor timing/import adjustments.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant T as Test
  participant TR as Tokio Runtime
  participant C as Config/Settings
  participant H as RuntimeHandle
  participant R as Runtime
  participant D as DistributedRuntime

  rect rgb(240,248,255)
  note over T: create_test_drt_async()
  T->>TR: Use current runtime
  TR->>R: Build Runtime from handle
  R->>D: from_settings_without_discovery()
  D-->>T: DistributedRuntime
  end

  rect rgb(245,255,245)
  note over T: create_test_drt_with_settings_async()
  T->>TR: spawn_blocking(load settings)
  TR-->>T: Settings
  T->>H: derive handle from settings
  H->>R: build Runtime
  R->>D: from_settings_without_discovery()
  D-->>T: DistributedRuntime
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

A rabbit taps its tiny keys,
Test helpers hop with graceful ease.
One burrow now for DRT,
Metrics and status run bug-free.
Imports trimmed like garden greens,
Integration dreams in tidy scenes. 🥕

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.


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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
lib/runtime/src/distributed.rs (1)

31-31: Duplicate/ambiguous import of error

Both use super::{error, ...}; (Line 27) and use figment::error; (this line) bring the name error into scope, likely triggering E0252 (name defined multiple times). The figment::error import is unused in this file; drop or alias it.

-use figment::error;
+// removed: unused/duplicate import of `figment::error`
lib/runtime/src/metrics.rs (1)

1343-1366: Fix order-sensitive comparison: sort the actual metric names before asserting equality

actual_drt_nats_metrics_sorted is not actually sorted; comparing to a sorted expected list can cause flaky test failures.

-        let actual_drt_nats_metrics_sorted: Vec<&str> = drt_nats_metric_lines
-            .iter()
-            .map(|line| {
-                let without_labels = line.split('{').next().unwrap_or(line);
-                // Remove the value part (everything after the last space)
-                without_labels.split(' ').next().unwrap_or(without_labels)
-            })
-            .collect();
+        let mut actual_drt_nats_metrics_sorted: Vec<&str> = drt_nats_metric_lines
+            .iter()
+            .map(|line| {
+                let without_labels = line.split('{').next().unwrap_or(line);
+                // Remove the value part (everything after the last space)
+                without_labels.split(' ').next().unwrap_or(without_labels)
+            })
+            .collect();
+        actual_drt_nats_metrics_sorted.sort_unstable();
🧹 Nitpick comments (5)
lib/runtime/src/distributed.rs (2)

359-373: Docstring contradicts cfg(feature="integration") gating

The helper is described as “for basic unit tests” but is compiled only with the integration feature. Update the doc to avoid confusion.

-    /// Helper function to create a DRT instance for basic unit tests
-    /// Uses from_current to leverage existing tokio runtime without environment configuration
+    /// Helper function to create a DRT instance for integration tests
+    /// Uses `from_current` to leverage the test tokio runtime; relies on env for configuration

379-400: Keep the two helpers DRY

With the above fix, both helpers become equivalent. Either alias the “with_settings” variant to the base helper or remove one to avoid duplication.

-    pub async fn create_test_drt_with_settings_async() -> crate::DistributedRuntime {
-        let rt = crate::Runtime::from_current().unwrap();
-        crate::DistributedRuntime::from_settings_without_discovery(rt)
-            .await
-            .unwrap()
-    }
+    pub async fn create_test_drt_with_settings_async() -> crate::DistributedRuntime {
+        // Delegate to the simpler helper
+        create_test_drt_async().await
+    }
lib/runtime/src/system_status_server.rs (3)

451-454: Redundant cfg attribute inside an already integration-gated module

The module is gated with #[cfg(all(test, feature = "integration"))]; function-level #[cfg(feature = "integration")] is redundant.

-    #[tokio::test]
-    #[cfg(feature = "integration")]
+    #[tokio::test]

490-491: Harden reqwest clients with timeouts to reduce test flakiness

Building clients with a reasonable default timeout avoids hangs if the server fails to bind/respond.

-                let client = reqwest::Client::new();
+                let client = reqwest::Client::builder()
+                    .timeout(std::time::Duration::from_secs(5))
+                    .build()
+                    .unwrap();

Apply the same change to other tests constructing reqwest::Client (Lines 568 and 726).


606-707: High-iteration health polling: consider reducing iterations or adding jitter

200 requests with strict success threshold can be slow on constrained CI runners. Add small jitter or reduce the loop count to improve stability; keep the essence of the test.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between bc290e7 and bd49475.

📒 Files selected for processing (3)
  • lib/runtime/src/distributed.rs (1 hunks)
  • lib/runtime/src/metrics.rs (5 hunks)
  • lib/runtime/src/system_status_server.rs (3 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-06-13T22:07:24.843Z
Learnt from: kthui
PR: ai-dynamo/dynamo#1424
File: lib/runtime/src/pipeline/network/egress/push_router.rs:204-209
Timestamp: 2025-06-13T22:07:24.843Z
Learning: The codebase uses async-nats version 0.40, not the older nats crate. Error handling should use async_nats::error::Error variants, not nats::Error variants.

Applied to files:

  • lib/runtime/src/metrics.rs
🧬 Code Graph Analysis (3)
lib/runtime/src/distributed.rs (2)
lib/runtime/src/config.rs (2)
  • from_settings (33-40)
  • from_settings (207-211)
lib/runtime/src/runtime.rs (1)
  • from_handle (67-71)
lib/runtime/src/system_status_server.rs (2)
lib/runtime/src/distributed.rs (2)
  • create_test_drt_async (367-372)
  • create_test_drt_with_settings_async (379-400)
lib/runtime/src/logging.rs (1)
  • load_log (867-896)
lib/runtime/src/metrics.rs (1)
lib/runtime/src/distributed.rs (1)
  • create_test_drt_async (367-372)
🔇 Additional comments (6)
lib/runtime/src/metrics.rs (4)

916-922: Good switch to centralized test helper

Importing and using crate::distributed::test_helpers::create_test_drt_async keeps tests consistent and removes duplication. Looks good.


1052-1058: LGTM on helper usage

Consistent adoption of the shared helper improves maintainability.


1312-1316: LGTM on helper usage in NATS tests

Consistent usage across integration tests is good.


682-910: ✔ test_metrics_registry_entry_callbacks is fully hermetic

I’ve verified that the test_metrics_registry_entry_callbacks in lib/runtime/src/metrics.rs (lines 817–910) is a plain, synchronous Rust unit test:

  • It only uses crate::MetricsRegistryEntry, std::sync::Arc, and AtomicUsize/Ordering.
  • It does not invoke any async runtimes (#[tokio::test]), networking crates (nats, reqwest, hyper), or external services (etcd, std::net, etc.).
  • All callbacks are in-process closures returning Ok(())/Err(...), and there are no global or integration feature-gated dependencies.

No integration dependencies were detected—this test remains fully self-contained.

lib/runtime/src/system_status_server.rs (2)

300-328: Great: basic HTTP lifecycle test is lightweight and hermetic

Nice quick check for graceful shutdown via CancellationToken and timeout.


137-226: Server spawn pathway looks correct

State initialization, route wiring, graceful shutdown with a child token, and early address bind checks look solid.

@rmccorm4
Copy link
Contributor

rmccorm4 commented Aug 20, 2025

  1. Please address code rabbit feedback
  2. Woah! The cargo cache has increased again from ~3.X GB to ~7 GB. I'm guessing this is from recent KVBM merge or something (not related to this PR). However, I recently freed up a bunch of disk space in the actions and there is ~53GB on runner before starting - I don't understand why this still appears to be having disk space issues. CC @nv-anants if you have any ideas here - this is becoming too flaky at this point.

- Simplified create_test_drt_with_settings_async to use Runtime::from_current()
- Merged create_test_drt_with_settings_async into create_test_drt_async
- Both functions were identical after simplification
- Updated all call sites to use the single unified function
- All 188 tests pass with the simplified implementation
Removed 9 unused imports:
- crate::logging::tests::load_log
- anyhow::anyhow (kept Result)
- chrono::{DateTime, Utc}
- jsonschema::{Draft, JSONSchema}
- serde_json::Value
- std::fs::File
- std::io::{BufRead, BufReader}
- stdio_override::*
- tokio::time::sleep (kept Duration)
@keivenchang keivenchang changed the title refactor: share same drt test functions refactor: share common drt test functions Aug 21, 2025
@keivenchang keivenchang merged commit 5b5ca80 into main Aug 21, 2025
13 of 15 checks passed
@keivenchang keivenchang deleted the keivenchang/share_same_drt_test_fn branch August 21, 2025 15:50
hhzhang16 pushed a commit that referenced this pull request Aug 27, 2025
Co-authored-by: Keiven Chang <[email protected]>
Signed-off-by: Hannah Zhang <[email protected]>
nv-anants pushed a commit that referenced this pull request Aug 28, 2025
KrishnanPrash pushed a commit that referenced this pull request Sep 2, 2025
Co-authored-by: Keiven Chang <[email protected]>
Signed-off-by: Krishnan Prashanth <[email protected]>
nnshah1 pushed a commit that referenced this pull request Sep 8, 2025
Co-authored-by: Keiven Chang <[email protected]>
Signed-off-by: nnshah1 <[email protected]>
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.

3 participants