test(unit): fix flaky mongo connection test

Merged
SA
3mo ago
+36-4

Proposed changes (including videos or screenshots)

Problem

Connection.spec.ts had a flaky reconnect test (should reset retryCount on a successful connection…) that intermittently failed in CI with Expected: "connected" / Received: "connecting".

The reconnect helper blocks waited on connection.once('connection', …), but the 'connection' event fires on every status transition. During a reconnect the sequence is reconnecting → connecting → connected, so once resolved on the very first emission ('reconnecting') — two transitions early. That let handleConnection resolve before the handshake finished, so the synchronous expect(connection.status).toBe('connected') on the next line raced the message delivery. It passed on fast/idle machines and lost under CI load.

Fix

Replaced the once('connection', …) waiters with a value-gated on('connection', …) listener that only resolves once status actually reaches 'connected', then detaches itself. Applied to all four reconnect blocks in the spec. This keeps the same observation channel and leaves every assertion unchanged — it only stops resolving on the intermediate 'reconnecting' state.

No production code changed; this is purely a test-synchronization fix.

Issue(s)

Steps to test or reproduce

Further comments

Summary by CodeRabbit

  • Tests
    • Improved reconnect test reliability by waiting specifically for confirmed connected states.
    • Updated coverage for retry-budget resets and stale socket closure scenarios.

Task: ARCH-2292