Skip to content

Remove wall-clock sleeps from exponential backoff unit test #310

@coderabbitai

Description

@coderabbitai

Problem

The test test_accept_exponential_backoff_doubles_and_caps() in src/server/config/tests.rs (lines 249-295) uses wall-clock sleeps which introduce flakiness and slow down the test suite.

Solution

Replace the wall-clock sleep approach with deterministic assertion of the backoff schedule by extracting a pure function that computes the backoff sequence.

Example Refactor

/// Behaviour test verifying exponential delay doubling and capping (pure logic).
#[test]
fn test_accept_exponential_backoff_doubles_and_caps() {
    use std::time::Duration;

    let initial = Duration::from_millis(10);
    let max = Duration::from_millis(80);
    let attempts = 5usize;

    let mut backoff = initial;
    let mut sequence = Vec::with_capacity(attempts);
    for _ in 0..attempts {
        sequence.push(backoff);
        backoff = std::cmp::min(backoff.saturating_mul(2), max);
    }

    let expected = [
        initial,
        std::cmp::min(initial * 2, max),
        std::cmp::min(initial * 4, max),
        std::cmp::min(initial * 8, max),
        max,
    ];
    assert_eq!(&sequence[..], &expected);
}

Benefits

  • Eliminates test flakiness from timing-dependent assertions
  • Significantly faster test execution
  • More deterministic and reliable test behaviour
  • Validates the core doubling-and-capping logic without timing concerns

Backlinks

Metadata

Metadata

Assignees

Labels

mediumCould be disruptive, but might not happen

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions