Skip to content

Replace fixed timing waits in tests with condition-based polling #1943

@sanity

Description

@sanity

Problem

Tests use fixed tokio::time::sleep() calls that assume specific timing, making them flaky in CI environments with variable performance. Example: test_multiple_clients_subscription had a 40s startup wait that was insufficient for CI.

Root Causes

  • Fixed waits don't adapt to actual system conditions
  • CI environments have variable performance (slower than local)
  • Network formation, contract propagation, and peer connections are non-deterministic
  • No feedback loop - tests don't know when conditions are actually met

Solution Pattern

Replace fixed waits with:

  1. Condition polling loops - Check for actual readiness (e.g., "do nodes have ring connections?")
  2. Retry logic with exponential backoff - For operations that may initially fail
  3. Adaptive timeouts - Longer total timeout, shorter per-attempt timeout with retries

Example Fix (from PR #1937)

  • Before: sleep(40s) → hope nodes are ready
  • After: sleep(60s) + retry GET up to 5x with exponential backoff (2s, 4s, 8s, 16s, 32s)
  • Result: Test adapts to actual conditions, passes reliably in 50-120s range

Action Items

  1. Audit test suite for tokio::time::sleep usage
  2. Identify which sleeps are waiting for conditions (vs intentional delays)
  3. Replace with proper readiness checks or retry logic
  4. Consider creating helper functions: wait_for_connections(), retry_operation(), etc.

Files to Check

  • crates/core/tests/*.rs - Integration tests
  • Look for patterns like "wait for X seconds for Y to happen"

[AI-assisted debugging and comment]

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-developer-xpArea: developer experienceA-networkingArea: Networking, ring protocol, peer discoveryE-mediumExperience needed to fix/implement: Medium / intermediateP-mediumMedium 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