Skip to content

Improve unit test coverage to prevent recurring bug patterns #1885

@sanity

Description

@sanity

Context

Analysis of 55 bug fixes over the past 4 months reveals recurring patterns that could be caught by better unit tests. Many bugs were only discovered in integration tests or production.

Key Bug Patterns Identified

1. Isolated Node Issues (3+ bugs)

Gap: Operations tested in multi-node setups break on isolated nodes

2. Self-Routing Prevention (4+ bugs)

Gap: No unit tests validating "never route to self" invariant

3. Actor/Router Delivery (3+ bugs)

Gap: ResultRouter → SessionActor flow has minimal unit tests (only creation test!)

4. Race Conditions (4+ flaky tests)

Gap: No systematic race condition testing framework

5. Contract Initialization (#1842)

Gap: No tests for operations during initialization

6. Edge Cases

Gap: No systematic edge case testing

Proposed Test Improvements

Priority 1: Isolated Node Test Suite

// Generic test pattern for ANY operation on isolated node
#[tokio::test]
async fn test_operation_on_isolated_node<Op>() {
    // Verify: no self-routing, no network timeout, uses local cache
}
  • Expand isolated_node_regression.rs to cover UPDATE
  • Create reusable isolated node test framework

Priority 2: Self-Routing Prevention

Create routing_invariants.rs:

#[test]
fn test_never_routes_to_self() {
    let node = create_node();
    let target = same_location_as_node();
    let selected_peer = select_peer_for_target(target);
    assert_ne!(selected_peer, node.peer_id());
}

#[test]
fn test_connection_rejects_self() {
    // Should reject connection attempts to own peer ID
}

Priority 3: Actor/Router Delivery Path Tests

Expand result_router.rs tests (currently only 2 trivial tests!):

#[tokio::test]
async fn test_result_router_delivers_to_session_actor() {
    // Send result through router, verify session actor receives it
}

#[tokio::test]
async fn test_instant_completion_race() {
    // Operation completes before client 2 registers
    // Verify both clients still get results
}

#[tokio::test]
async fn test_multiple_clients_receive_response() {
    // Verify message delivery to multiple WebSocket clients
}

Priority 4: Race Condition Testing Framework

Add Loom dependency for deterministic race detection:

#[test]
fn test_state_cache_concurrent_updates() {
    loom::model(|| {
        // Model concurrent cache updates deterministically
    });
}

Priority 5: Contract Initialization Edge Cases

Create initialization_edge_cases.rs:

#[tokio::test]
async fn test_operations_queued_during_init() {
    let contract = start_initializing();
    send_operation(Op1);
    send_operation(Op2);
    complete_init();
    // Verify ops executed in order with correct params
}

Priority 6: Edge Case Response Tests

#[test]
fn test_update_no_state_change_returns_response() {
    let result = update(same_state);
    assert!(matches!(result, UpdateResponse { .. }));
}

Concrete Action Plan

  1. Add comprehensive tests to result_router.rs (currently minimal coverage)
  2. Create routing_invariants.rs with self-routing prevention tests
  3. Expand isolated_node_regression.rs to cover UPDATE operation
  4. Add Loom dependency for deterministic race condition testing
  5. Create initialization_edge_cases.rs for contract init scenarios
  6. Add edge case matrix tests for all operations (empty, max size, over-limit, no-change)

Expected Impact

These test improvements would have caught most of the 55 bugs before they reached integration testing or production, significantly improving development velocity and code quality.

Current Test Coverage

  • 52 files with unit tests in src/
  • 3 integration test files in tests/
  • Many critical paths (result router, request router, self-routing prevention) have minimal unit test coverage

[AI-assisted debugging and comment]

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-contractsArea: Contract runtime, SDK, and executionA-developer-xpArea: developer experienceA-networkingArea: Networking, ring protocol, peer discoveryE-mediumExperience needed to fix/implement: Medium / intermediateP-highHigh priorityT-enhancementType: Improvement to existing functionality

    Type

    No type

    Projects

    Status

    Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions