Skip to content

Move server tests out of module#244

Closed
leynos wants to merge 1 commit intomainfrom
c6o48v-codex/extract-tests-from-src/server.rs
Closed

Move server tests out of module#244
leynos wants to merge 1 commit intomainfrom
c6o48v-codex/extract-tests-from-src/server.rs

Conversation

@leynos
Copy link
Copy Markdown
Owner

@leynos leynos commented Aug 1, 2025

Summary

  • move server helpers to dedicated server_helpers module
  • use tracing macros in worker task
  • hide process_stream and expose helper functions for tests
  • consolidate repeated test fixtures
  • assert panic logs and fix worker factory test
  • address code review comments: use expect instead of unwrap and switch to tracing::error!

Testing

  • make fmt
  • make lint
  • make test

https://chatgpt.com/codex/tasks/task_e_688bf634507c8322a842ee9c9bccda9a

Summary by Sourcery

Refactor the server module by extracting worker logic into a dedicated submodule, improve error logging with tracing macros, and reorganize tests with shared fixtures and dedicated helper functions.

Bug Fixes:

  • Fix worker factory test to assert no factory calls occur when no connections are handled

Enhancements:

  • Extract the worker_task and connection processing logic from server.rs into src/server/worker.rs
  • Hide internal process_stream function and expose worker_task plus has_preamble_success/failure helper methods for testing
  • Switch error reporting and accept error retries to use tracing::error! instead of eprintln
  • Consolidate common test fixtures into tests/server_helpers.rs and split runtime tests into separate integration files

Tests:

  • Update tests to use the new has_preamble_success/has_preamble_failure API and shared fixtures
  • Assert panic logs in connection tasks using tracing_test for log capture

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Aug 1, 2025

Reviewer's Guide

This PR refactors the server module by extracting the worker and stream processing logic into a new worker module, improves error logging using tracing macros, restructures tests into dedicated integration files with shared fixtures, and exposes only the internal APIs needed for tests while cleaning up unwraps per review feedback.

Class diagram for test helpers and fixtures extraction

classDiagram
    class server_helpers {
        <<module>>
        +factory() Fn() -> WireframeApp
        +free_port() SocketAddr
        +bind_server(factory, addr) WireframeServer
        +server_with_preamble(factory) WireframeServer
    }
    class server_runtime {
        <<test module>>
    }
    class server_runtime_more {
        <<test module>>
    }
    server_runtime ..> server_helpers : uses
    server_runtime_more ..> server_helpers : uses
Loading

File-Level Changes

Change Details Files
Extract worker and stream processing to a dedicated module
  • Remove inline worker_task and process_stream from src/server.rs
  • Create src/server/worker.rs containing the extracted implementations
  • Update src/server.rs to import and spawn worker::worker_task
src/server.rs
src/server/worker.rs
Improve error logging with tracing macros
  • Replace eprintln! and manual prints with tracing::error! in accept-error and panic handlers
  • Log preamble callback errors via tracing::error!
src/server/worker.rs
Restructure tests into integration suite and consolidate fixtures
  • Remove inline tests from src/server.rs
  • Add tests/server_runtime.rs and tests/server_runtime_more.rs for runtime behavior tests
  • Introduce tests/server_helpers.rs with shared fixtures (factory, free_port, helper constructors)
src/server.rs
tests/server_runtime.rs
tests/server_runtime_more.rs
tests/server_helpers.rs
Expose only the APIs needed for testing and clean up unwraps
  • Add has_preamble_success and has_preamble_failure methods on WireframeServer
  • Re-export worker_task under #[cfg(test)] and hide process_stream
  • Replace generic unwrap() calls in fixtures and helpers with expect() for clearer error messages
src/server.rs
tests/server_helpers.rs

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Aug 1, 2025

Summary by CodeRabbit

  • Refactor

    • Internal server worker logic has been reorganised into a dedicated module, improving code structure without affecting the public API.
    • Unused imports and legacy test code have been removed for clarity.
  • Tests

    • Introduced extensive new test suites covering server runtime behaviour, shutdown handling, multi-worker management, configuration persistence, and resilience to connection panics.
    • Added shared test helpers and fixtures to streamline server testing.

Walkthrough

Refactor the internal worker task and connection processing logic from src/server.rs into a dedicated src/server/worker.rs module. Remove local worker and connection handling code, related imports, and tests from src/server.rs. Add a new test utility module and expand runtime test coverage across multiple new test files, focusing on server lifecycle, configuration, and callback behaviour.

Changes

Cohort / File(s) Change Summary
Worker Task Refactor
src/server.rs, src/server/worker.rs
Move all worker task and connection processing logic from src/server.rs to a new worker module. Remove related imports and tests from src/server.rs. Re-export worker_task from the new module for testing. Update server to spawn worker tasks using the new module. Add hidden helper methods for preamble callback checks.
Test Utilities
tests/server_helpers.rs
Introduce a new test helper module providing fixtures, a serialisable test preamble struct, and utility functions for binding servers and creating preamble-configured servers.
Server Runtime Tests
tests/server_runtime.rs
Add a comprehensive suite of runtime tests for server creation, binding, callback registration, method chaining, and shutdown handling. Use fixtures and helpers for setup and validation.
Advanced Runtime Tests
tests/server_runtime_more.rs
Add advanced asynchronous tests for graceful shutdown, multiple worker threads, configuration persistence, preamble callback resets, extreme worker counts, multi-address binding, debug mode, and panic resilience. Capture logs and verify server robustness under error and concurrency scenarios.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant TcpListener
    participant WorkerTask
    participant WireframeApp
    participant Callback

    loop Worker Loop
        Client->>TcpListener: Connect
        TcpListener->>WorkerTask: Accept connection
        WorkerTask->>WorkerTask: Decode preamble
        alt Success
            WorkerTask->>Callback: on_success (optional)
            WorkerTask->>WireframeApp: Create app instance
        else Failure
            WorkerTask->>Callback: on_failure (optional)
            WorkerTask-->>Client: Close connection
        end
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Poem

The worker’s moved, the code’s anew,
With helpers, tests, and modules too.
Connections handled, panics caught,
Callbacks fired as they ought.
Refactored flows, robust and clean—
The server’s stronger, lean and keen!
🚦🦀


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 79244ac and e55f2e7.

📒 Files selected for processing (5)
  • src/server.rs (3 hunks)
  • src/server/worker.rs (1 hunks)
  • tests/server_helpers.rs (1 hunks)
  • tests/server_runtime.rs (1 hunks)
  • tests/server_runtime_more.rs (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

⚙️ CodeRabbit Configuration File

**/*.rs: * Seek to keep the cyclomatic complexity of functions no more than 12.

  • Adhere to single responsibility and CQRS

  • Place function attributes after doc comments.

  • Do not use return in single-line functions.

  • Move conditionals with >2 branches into a predicate function.

  • Avoid unsafe unless absolutely necessary.

  • Every module must begin with a //! doc comment that explains the module's purpose and utility.

  • Comments and docs must follow en-GB-oxendict (-ize / -our) spelling and grammar

  • Lints must not be silenced except as a last resort.

    • #[allow] is forbidden.
    • Only narrowly scoped #[expect(lint, reason = "...")] is allowed.
    • No lint groups, no blanket or file-wide suppression.
    • Include FIXME: with link if a fix is expected.
  • Use rstest fixtures for shared setup and to avoid repetition between tests.

  • Replace duplicated tests with #[rstest(...)] parameterised cases.

  • Prefer mockall for mocks/stubs.

  • Prefer .expect() over .unwrap()

  • Ensure that any API or behavioural changes are reflected in the documentation in docs/

  • Ensure that any completed roadmap steps are recorded in the appropriate roadmap in docs/

  • Files must not exceed 400 lines in length

    • Large modules must be decomposed
    • Long match statements or dispatch tables should be decomposed by domain and collocated with targets
    • Large blocks of inline data (e.g., test fixtures, constants or templates) must be moved to external files and inlined at compile-time or loaded at run-time.

Files:

  • src/server/worker.rs
  • tests/server_helpers.rs
  • src/server.rs
  • tests/server_runtime.rs
  • tests/server_runtime_more.rs
🧬 Code Graph Analysis (3)
src/server/worker.rs (3)
src/extractor.rs (2)
  • std (270-270)
  • peer_addr (362-362)
tests/server_helpers.rs (1)
  • factory (18-20)
src/server.rs (2)
  • new (91-107)
  • bind (268-274)
tests/server_helpers.rs (3)
tests/server_runtime.rs (2)
  • server (56-56)
  • WireframeServer (156-158)
src/server/worker.rs (1)
  • factory (148-150)
src/server.rs (2)
  • new (91-107)
  • bind (268-274)
tests/server_runtime.rs (3)
tests/server_helpers.rs (5)
  • WireframeServer (51-51)
  • bind_server (36-43)
  • factory (18-20)
  • free_port (23-27)
  • server_with_preamble (47-52)
src/server/worker.rs (1)
  • factory (148-150)
src/server.rs (5)
  • new (91-107)
  • worker_count (227-227)
  • local_addr (231-233)
  • has_preamble_success (235-235)
  • has_preamble_failure (237-237)
🔇 Additional comments (15)
tests/server_helpers.rs (4)

1-4: Module documentation looks good.

The module-level documentation clearly explains its purpose and relationship to the test files.


11-20: Test utilities are well-structured.

The TestPreamble struct and factory fixture are appropriately designed for their testing purpose.


29-43: Helper function is appropriate for test usage.

The bind_server function correctly uses .expect() for test scenarios where panics are acceptable, and the documentation clearly states this behaviour.


45-52: Test helper correctly configures preamble type.

The function provides a clean abstraction for creating servers with the test preamble type.

src/server/worker.rs (3)

1-2: Module documentation is complete.

The doc comment clearly describes the module's purpose.


78-134: Connection processing logic is well-implemented.

The function correctly handles preamble decoding, callback invocation, and stream processing with appropriate error handling.


136-176: Test correctly verifies shutdown behaviour.

The test appropriately validates that the worker task responds to cancellation signals and shuts down cleanly.

src/server.rs (3)

33-36: Module structure properly refactored.

The worker logic extraction and conditional test export are well-organized.


234-237: Hidden test helpers are appropriate.

The methods provide clean test access to internal state without polluting the public API.


358-360: Clean delegation to worker module.

The refactored code properly delegates to the extracted worker task while maintaining the same behaviour.

tests/server_runtime_more.rs (4)

1-23: Test module setup is comprehensive.

Good use of imports and proper module structure with server_helpers.


25-51: Graceful shutdown test is well-designed.

The test properly verifies shutdown timing and completion with appropriate timeouts.


53-87: Worker factory test correctly validates lazy invocation.

The test properly verifies that factories are only called when connections are actually handled.


89-228: Comprehensive test coverage for server edge cases.

The remaining tests thoroughly cover configuration persistence, callback management, edge cases, and panic resilience. The connection panic test is particularly well-designed with proper log assertions.

tests/server_runtime.rs (1)

1-224: Fundamental server tests are comprehensive and well-structured.

The test suite thoroughly covers server creation, configuration, binding, callbacks, and error cases. Good use of fixtures and appropriate panic testing.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch c6o48v-codex/extract-tests-from-src/server.rs

🪧 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.
    • Explain this complex logic.
    • 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. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • 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 src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

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

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • 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
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey @leynos - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `tests/server_helpers.rs:17` </location>
<code_context>
-    #[expect(dead_code, reason = "test helper for unused preamble type")]
-    struct EmptyPreamble;
-
-    #[fixture]
-    fn factory() -> impl Fn() -> WireframeApp + Send + Sync + Clone + 'static {
-        || WireframeApp::default()
</code_context>

<issue_to_address>
Potential race condition in free_port fixture.

Binding and releasing a port can allow another process to claim it before your test does. To prevent this, use the listener directly or pass it to the server.

Suggested implementation:

```rust
#[fixture]
/// Returns a bound TcpListener on a free port for use in tests.
/// This prevents race conditions by keeping the port reserved until the test is ready.
pub fn free_listener() -> std::net::TcpListener {
    let addr = SocketAddr::new(Ipv4Addr::LOCALHOST.into(), 0);
    std::net::TcpListener::bind(addr).expect("Failed to bind to localhost:0")
}

```

- Update any test or helper that previously used `free_port()` to use `free_listener()` and extract the address with `listener.local_addr().unwrap()` if needed.
- Pass the `TcpListener` directly to the server setup code instead of just the address.
</issue_to_address>

### Comment 2
<location> `src/server/worker.rs:24` </location>
<code_context>
+/// Each accepted connection is handled in a separate task, with optional callbacks for preamble
+/// decode success or failure. The worker listens for shutdown signals to terminate gracefully.
+/// Accept errors are retried with exponential backoff.
+pub async fn worker_task<F, T>(
+    listener: Arc<TcpListener>,
+    factory: F,
</code_context>

<issue_to_address>
Consider extracting the backoff accept loop and panic-catching spawn logic into separate helper functions to keep `worker_task` focused on high-level flow.

```markdown
Consider pulling out the backoff/accept‐loop and the panic‐wrapping spawn into small helpers. That keeps `worker_task` focused on “what” to do, not “how” to retry or catch panics.

1) Extract retry/backoff into its own async fn:

```rust
async fn accept_with_backoff(
    listener: &TcpListener,
    shutdown: &CancellationToken,
) -> Option<tokio::net::TcpStream> {
    let mut delay = Duration::from_millis(10);
    loop {
        tokio::select! {
            () = shutdown.cancelled() => return None,
            res = listener.accept() => match res {
                Ok((stream, _)) => return Some(stream),
                Err(e) => {
                    tracing::error!(error = %e, "accept error");
                    sleep(delay).await;
                    delay = (delay * 2).min(Duration::from_secs(1));
                }
            }
        }
    }
}
```

2) Extract panic‐catching spawn into a helper:

```rust
fn spawn_conn_task<F, T>(
    tracker: TaskTracker,
    stream: TcpStream,
    factory: F,
    on_success: Option<PreambleCallback<T>>,
    on_failure: Option<PreambleErrorCallback>,
) 
where
    F: Fn() -> WireframeApp + Send + Sync + Clone + 'static,
    T: Preamble + 'static,
{
    let peer_addr = stream.peer_addr().ok();
    tracker.spawn(async move {
        let res = std::panic::AssertUnwindSafe(process_stream(stream, factory, on_success, on_failure))
            .catch_unwind()
            .await;
        if let Err(panic) = res {
            let msg = panic
                .downcast_ref::<&str>()
                .copied()
                .or_else(|| panic.downcast_ref::<String>().map(String::as_str))
                .unwrap_or("<non-string panic>");
            tracing::error!(panic = %msg, ?peer_addr, "connection task panicked");
        }
    });
}
```

3) Now `worker_task` becomes:

```rust
pub async fn worker_task<…>(…) {
    loop {
        if let Some(stream) = accept_with_backoff(&listener, &shutdown).await {
            spawn_conn_task(tracker.clone(), stream, factory.clone(), on_success.clone(), on_failure.clone());
        } else {
            break;
        }
    }
}
```

This keeps the same behavior but reduces nesting and separates concerns.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread tests/server_helpers.rs
Comment thread src/server/worker.rs
@leynos
Copy link
Copy Markdown
Owner Author

leynos commented Aug 3, 2025

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Aug 3, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@leynos
Copy link
Copy Markdown
Owner Author

leynos commented Aug 3, 2025

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Aug 3, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Aug 4, 2025

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Aug 4, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@leynos
Copy link
Copy Markdown
Owner Author

leynos commented Aug 4, 2025

No

@leynos leynos closed this Aug 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant