Files
bitchat/bitchatTests/Sync/RequestSyncManagerTests.swift
T
jackandClaude Opus 5 ac13a0c22f Make the flake class unrepeatable, not just fixed
Raising four deadlines fixed the four tests that happened to fire. The
class was still there: fourteen separate waitUntil helpers, most defaulting
to 1.0s, plus wait call sites with literal budgets. The fifth instance
would have landed the same way, on someone else's unrelated PR.

The rule, now written down in TestConstants.settleTimeout: **a wait
deadline is not a latency budget.** It exists so a genuine hang eventually
fails the suite, so size it for the worst-case scheduler, never for how
long the operation should take. Waits return as soon as their condition
holds, so a generous deadline is free in the passing case and only extends
genuine failures.

- Every wait helper now defaults to TestConstants.settleTimeout (30s), and
  the literal wait call sites below the floor were converted too — sixteen
  sites across ten files.
- TestTimingHygieneTests enforces it by scanning the test sources: wait
  defaults and wait call sites must be at least minimumSettleTimeout, and
  no test may assert an upper bound on elapsed wall-clock time (the
  assertion that started this, which cannot separate correct behaviour from
  a slow machine).
- Both rules waive per line with "test-timing-ok: <reason>", accepted on
  the line or in the comment block above it so the reason has room to be a
  sentence. One legitimate use so far: a NEGATIVE wait in NoiseCoverageTests
  asserting a promotion has *not* completed within 50ms, where a long
  deadline would only make the suite slow while still passing.

Injected production timeouts are deliberately not matched — the Noise
handshake timeouts are the behaviour under test, and short values are
correct there.

Verified the guard actually fails: a canary file with both banned shapes
was flagged with file and line, and the suite went green again once it was
removed. Full suite passes with all 18 cores saturated.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-26 22:23:40 +01:00

87 lines
3.0 KiB
Swift

import XCTest
import BitFoundation
@testable import bitchat
final class RequestSyncManagerTests: XCTestCase {
func test_isValidResponse_returnsFalseWhenPacketIsNotRSR() {
let clock = MutableSyncClock(now: 1_000)
let manager = RequestSyncManager(responseWindow: 30, now: { clock.now })
manager.registerRequest(to: PeerID(str: "aaaaaaaaaaaaaaaa"))
XCTAssertFalse(manager.isValidResponse(from: PeerID(str: "aaaaaaaaaaaaaaaa"), isRSR: false))
}
func test_isValidResponse_returnsFalseForUnsolicitedRSR() {
let clock = MutableSyncClock(now: 1_000)
let manager = RequestSyncManager(responseWindow: 30, now: { clock.now })
XCTAssertFalse(manager.isValidResponse(from: PeerID(str: "bbbbbbbbbbbbbbbb"), isRSR: true))
}
func test_isValidResponse_acceptsRecentRequest() async {
let clock = MutableSyncClock(now: 1_000)
let manager = RequestSyncManager(responseWindow: 30, now: { clock.now })
let peerID = PeerID(str: "cccccccccccccccc")
manager.registerRequest(to: peerID)
let registered = await waitUntil {
manager.debugPendingRequestCount == 1
}
XCTAssertTrue(registered)
clock.now = 1_020
XCTAssertTrue(manager.isValidResponse(from: peerID, isRSR: true))
}
func test_cleanup_removesExpiredRequestsAndPreservesFreshOnes() async {
let clock = MutableSyncClock(now: 1_000)
let manager = RequestSyncManager(responseWindow: 30, now: { clock.now })
let expiredPeer = PeerID(str: "dddddddddddddddd")
let freshPeer = PeerID(str: "eeeeeeeeeeeeeeee")
manager.registerRequest(to: expiredPeer)
_ = await waitUntil { manager.debugPendingRequestCount == 1 }
clock.now = 1_010
manager.registerRequest(to: freshPeer)
let bothRegistered = await waitUntil {
manager.debugPendingRequestCount == 2
}
XCTAssertTrue(bothRegistered)
clock.now = 1_035
XCTAssertFalse(manager.isValidResponse(from: expiredPeer, isRSR: true))
XCTAssertTrue(manager.isValidResponse(from: freshPeer, isRSR: true))
manager.cleanup()
let cleaned = await waitUntil {
manager.debugPendingRequestCount == 1
}
XCTAssertTrue(cleaned)
XCTAssertFalse(manager.isValidResponse(from: expiredPeer, isRSR: true))
XCTAssertTrue(manager.isValidResponse(from: freshPeer, isRSR: true))
}
private func waitUntil(
timeout: TimeInterval = TestConstants.settleTimeout,
condition: @escaping () -> Bool
) async -> Bool {
let deadline = Date().addingTimeInterval(timeout)
while Date() < deadline {
if condition() {
return true
}
try? await Task.sleep(nanoseconds: 10_000_000)
}
return condition()
}
}
private final class MutableSyncClock {
var now: TimeInterval
init(now: TimeInterval) {
self.now = now
}
}