Swift retry lifecycle: SPEC-correct onRetry numbering, re-auth on every retry path, no events from catch

Merged
JE
4mo ago
+282-52

Fixes #508. Fixes #509.

Two commits, both proven red-first against pristine HTTPClient (all new/changed tests written and run before any fix; 7 failing assertions across 4 tests, then 250/250 green after).

Commit 1 — onRetry numbering (#508)

SPEC §7 step 3.j emits (failed attempt in RequestInfo, upcoming attempt standalone) — (1, 2) for the first retry. Swift passed bare attempt at both call sites, emitting (1, 1). Go/Ruby/Python/Kotlin/TS all emit the SPEC pair. Two-token fix + the hooks test now pins both halves (upcoming == 2, failed == 1 — the failed half was previously unasserted) + the ambiguous protocol doc now says "upcoming".

Red: XCTAssertEqual failed: ("Optional(1)") is not equal to ("Optional(2)") - Standalone attempt is the upcoming attempt: first retry = attempt 2

Commit 2 — retry lifecycle (#509)

Three defects in the same loop:

  1. Network-path retries never re-authenticated. The status path re-ran authenticate(&request) after backoff; the network path slept and continued — SPEC §7 step 3.l requires the refresh on every retry path. Live for idempotent operations since #417.
    Red: XCTAssertEqual failed: ("Optional("Bearer token-1")") is not equal to ("Optional("Bearer token-2")") - The retry after a network error must re-authenticate
  2. The existing re-auth test was false proof — rotating token-1/2/3 provider, a comment saying "should have a different auth token", and two XCTAssertNotNils that pass even if re-auth never runs. It now asserts the actual header values.
  3. Sleep + re-auth ran inside the transport do/catch, so CancellationError from the backoff or a non-BasecampError from a failing token refresh fell into the network-error catch: a second onRequestEnd (statusCode 0) and a second onRetry for an attempt that had already emitted both, then a retry with stale auth.
    Red: XCTAssertEqual failed: ("3") is not equal to ("1") - A failed reauth must not emit a phantom onRequestEnd / ("2") is not equal to ("1") - A cancelled backoff must not emit a second onRetry / failed - Expected RefreshError

The restructure: the do/catch now only computes an AttemptDirective (done / retry / fail); the loop tail — outside any catch — performs SPEC steps 3.j/3.k/3.l in order (onRetry, sleep, re-authenticate), shared by both retry paths. Cancellation and auth failures propagate raw with no phantom events.

Preserved exactly (each pinned by an existing test): BasecampError from the transport rethrown untouched with no request-end event; network errors wrapped as .network only on exhaustion; the maxAttempts >= 1 guard; 304/ETag cache behavior; one onRequestStart/onRequestEnd per attempt; both idempotency-gate layers.

Behavior change worth a release-notes line: an error thrown by authenticate during a retry now propagates raw instead of being swallowed into a stale-header retry — strictly a bug fix, but observable.

SPEC needed no edit — §7 already states the contract Swift now matches. swift test: 250 tests, 0 failures (commit 1 is independently green at 247/247).


Summary by cubic

Fixes retry lifecycle in the Swift client: onRetry now reports the upcoming attempt, and re-auth runs on every retry path. Prevents duplicate hook events and stale auth, and lets cancellation/auth errors propagate cleanly.

  • Bug Fixes
    • onRetry reports the upcoming attempt (first retry = 2); RequestInfo.attempt remains the attempt that just failed.
    • Re-authenticate before each retry, including after network errors.
    • Backoff sleep and re-auth moved outside the transport catch: no duplicate onRequestEnd/onRetry; cancellation and refresh errors propagate raw; no retries with stale headers.
    • Tests tightened to assert Authorization header values and cover the network retry path; hooks test pins both halves of the attempt pair.