import XCTest @testable import CallsKit /// A connection that stops working is mended without anyone asking (spec /// §15.28, 2026-10-21): when it counts as broken, the backoff, the /// escalation to a rebuild, who acts, and when the failure is said. final class ConnectionRecoveryTests: XCTestCase { private typealias Look = ConnectionRecovery.Look /// Runs the recovery at 5 Hz from `from` to `to`, with the look `look(t)` /// gives; returns each action with the time it was asked for. private func run( _ r: inout ConnectionRecovery, from: Double, to: Double, look: (Double) -> Look ) -> [(Double, ConnectionRecovery.Action)] { var out: [(Double, ConnectionRecovery.Action)] = [] // Whole ticks of a fifth of a second, so the clock does not drift. for tick in Int((from * 5).rounded())...Int((to * 5).rounded()) { let t = Double(tick) / 5 if let a = r.observe(at: t, look(t)) { out.append((t, a)) } } return out } func testAFailedConnectionIsRestartedTwiceThenRebuiltOnTheBackoff() { var r = ConnectionRecovery() let actions = run(&r, from: 0, to: 160) { _ in Look(ice: .failed, builtAt: -100, leader: true) } XCTAssertEqual(actions.map(\.0), [0, 2, 7, 17, 37, 67, 97, 127, 157]) XCTAssertEqual( actions.map(\.1), [.restart, .restart, .rebuild, .restart, .restart, .rebuild, .restart, .restart, .rebuild]) XCTAssertEqual(r.restarts, 6) XCTAssertEqual(r.rebuilds, 3) } func testDisconnectedCountsAfterFiveSeconds() { var r = ConnectionRecovery() let actions = run(&r, from: 0, to: 4.8) { _ in Look(ice: .disconnected, builtAt: -100, leader: true) } XCTAssertTrue(actions.isEmpty, "a blip is not a failure") XCTAssertFalse(r.trying) XCTAssertEqual(r.observe(at: 5, Look(ice: .disconnected, builtAt: -100, leader: true)), .restart) XCTAssertTrue(r.trying) } func testADisconnectionThatComesBackInTimeIsNothing() { var r = ConnectionRecovery() let actions = run(&r, from: 0, to: 20) { t in Look(ice: Int(t) % 6 < 4 ? .disconnected : .connected, builtAt: -100, leader: true) } XCTAssertTrue(actions.isEmpty) } /// The incident's shape: a connection that never comes up, which /// libwebrtc leaves in `checking` rather than calling failed. func testAConnectionThatNeverComesUpIsTroubleAfterFifteenSeconds() { var r = ConnectionRecovery() XCTAssertTrue(run(&r, from: 0, to: 14.8) { _ in Look(ice: .checking, builtAt: 0, leader: true) }.isEmpty) XCTAssertEqual(r.observe(at: 15, Look(ice: .checking, builtAt: 0, leader: true)), .restart) var fresh = ConnectionRecovery() XCTAssertTrue(run(&fresh, from: 0, to: 14.8) { _ in Look(ice: .new, builtAt: 0, leader: true) }.isEmpty) XCTAssertEqual(fresh.observe(at: 15, Look(ice: .new, builtAt: 0, leader: true)), .restart) } func testRecoveryEndsTheEpisodeAndClearsTheWords() { var r = ConnectionRecovery() _ = run(&r, from: 0, to: 61) { _ in Look(ice: .failed, builtAt: -100, leader: true) } XCTAssertTrue(r.trying) XCTAssertTrue(r.failing(at: 61)) XCTAssertNil(r.observe(at: 61.2, Look(ice: .connected, builtAt: -100, leader: true))) XCTAssertFalse(r.trying) XCTAssertFalse(r.failing(at: 61.2)) } func testTheFailureIsSaidOnlyAfterAMinute() { var r = ConnectionRecovery() _ = run(&r, from: 0, to: 59.8) { _ in Look(ice: .failed, builtAt: -100, leader: true) } XCTAssertTrue(r.trying) XCTAssertFalse(r.failing(at: 59.8)) XCTAssertTrue(r.failing(at: 60)) } /// A link that comes and goes is not hammered from nought each time. func testTroubleSoonAfterARecoveryCarriesOnWhereTheScheduleWas() { var r = ConnectionRecovery() _ = run(&r, from: 0, to: 8) { _ in Look(ice: .failed, builtAt: -100, leader: true) } // 0, 2, 7 XCTAssertEqual(r.attempts, 3) XCTAssertNil(r.observe(at: 9, Look(ice: .connected, builtAt: 8, leader: true))) let again = run(&r, from: 20, to: 60) { _ in Look(ice: .failed, builtAt: 8, leader: true) } // Attempt 4 is due its own gap (10 s) after the trouble, not at once. XCTAssertEqual(again.first?.0, 30) XCTAssertEqual(again.first?.1, .restart) // Well after a recovery it starts from nought again. var later = ConnectionRecovery() _ = run(&later, from: 0, to: 8) { _ in Look(ice: .failed, builtAt: -100, leader: true) } XCTAssertNil(later.observe(at: 9, Look(ice: .connected, builtAt: 8, leader: true))) XCTAssertEqual(later.observe(at: 80, Look(ice: .failed, builtAt: 8, leader: true)), .restart) } func testNothingIsAttemptedWhileTheSignallingStreamIsDown() { var r = ConnectionRecovery() let down = run(&r, from: 0, to: 30) { _ in Look(ice: .failed, builtAt: -100, leader: true, signalling: false) } XCTAssertTrue(down.isEmpty) XCTAssertTrue(r.trying, "still in trouble, and still said") XCTAssertEqual(r.observe(at: 30.2, Look(ice: .failed, builtAt: -100, leader: true)), .restart) } func testANegotiationStepThatThrewHastensTroubleOnlyWhileNotConnected() { var r = ConnectionRecovery() XCTAssertEqual(r.observe(at: 1, Look(ice: .checking, builtAt: 0, leader: true, errored: true)), .restart) var up = ConnectionRecovery() XCTAssertNil(up.observe(at: 1, Look(ice: .connected, builtAt: 0, leader: true, errored: true))) XCTAssertFalse(up.trying) } func testAClosedConnectionIsLeftAlone() { var r = ConnectionRecovery() XCTAssertTrue(run(&r, from: 0, to: 30) { _ in Look(ice: .closed, builtAt: -100, leader: true) }.isEmpty) XCTAssertFalse(r.trying) } /// The polite side follows three seconds behind, and stands back for /// whatever the leader has already done. func testTheFollowerActsOnlyWhenTheLeaderDoesNot() { // A leader from before this release: it never acts, so the follower // runs the whole schedule three seconds late. var alone = ConnectionRecovery() let actions = run(&alone, from: 0, to: 21) { _ in Look(ice: .failed, builtAt: -100, leader: false) } XCTAssertEqual(actions.map(\.0), [3, 5, 10, 20]) XCTAssertEqual(actions.map(\.1), [.restart, .restart, .rebuild, .restart]) // A leader that does act: its restart offers arrive, and its rebuild // gives this side a new connection; the follower asks for nothing. var follower = ConnectionRecovery() var built = -100.0 let none = run(&follower, from: 0, to: 40) { t in // The leader's restarts land 0.1 s after 0 and 2, its rebuild after 7. let offer: Double? = t >= 7.1 ? (t >= 17.1 ? 17.1 : 7.1) : t >= 2.1 ? 2.1 : t >= 0.1 ? 0.1 : nil if t >= 7.1 { built = 7.1 } return Look(ice: .failed, builtAt: built, leader: false, remoteOfferAt: offer) } XCTAssertTrue(none.isEmpty, "the follower acted beside the leader: \(none)") } /// Two sides whose episodes started a beat apart still rebuild once. func testAFollowerAStepBehindDoesNotRebuildAgain() { var follower = ConnectionRecovery() // The follower notices one second after the leader: the leader's // rebuild (at its 7 s) lands at the follower's 6.1 s. let actions = run(&follower, from: 0, to: 12) { t in Look(ice: .failed, builtAt: t >= 6.1 ? 6.1 : -100, leader: false, remoteOfferAt: t >= 1.1 ? 1.1 : nil) } XCTAssertFalse(actions.contains { $0.1 == .rebuild }, "\(actions)") } func testTheVersionNoticeRule() { XCTAssertEqual( ClientVersion.notice(name: "Farshad", client: "mac 0.4.1", failing: false), "Farshad is on an old version of Calls — ask them to update") XCTAssertEqual( ClientVersion.notice(name: "Farshad", client: "iphone 0.3.9", failing: true), "Farshad is on an old version of Calls — ask them to update") XCTAssertNil(ClientVersion.notice(name: "Farshad", client: "mac 0.4.2", failing: true)) XCTAssertNil(ClientVersion.notice(name: "Farshad", client: "mac 0.4.12", failing: true)) // No version at all: an app from before §15.28, only worth a word // once the connection to it has been failing. XCTAssertNil(ClientVersion.notice(name: "Farshad", client: "native", failing: false)) XCTAssertEqual( ClientVersion.notice(name: "Farshad", client: "native", failing: true), "Farshad may be on an old version of Calls — ask them to update") // A browser, or a server from before the key: never. XCTAssertNil(ClientVersion.notice(name: "Farshad", client: nil, failing: true)) XCTAssertNil(ClientVersion.notice(name: "Farshad", client: "web 882eab8", failing: true)) XCTAssertNil(ClientVersion.notice(name: "Farshad", client: "mac latest", failing: true)) } func testVersionsCompareNumerically() { XCTAssertEqual(ClientVersion.compare("0.4.10", "0.4.2"), 1) XCTAssertEqual(ClientVersion.compare("0.4.1", "0.4.2"), -1) XCTAssertEqual(ClientVersion.compare("0.4", "0.4.0"), 0) XCTAssertEqual(ClientVersion.compare("0.10", "0.9.9"), 1) XCTAssertEqual(ClientVersion.compare("1", "0.99"), 1) } func testTheMinimumIsTheServers() throws { // protocol.MinNativeVersion, which carries the reason; the two must agree. let url = URL(fileURLWithPath: #filePath).deletingLastPathComponent() .appendingPathComponent("../../../../protocol/client.go").standardized let source = try String(contentsOf: url, encoding: .utf8) XCTAssertTrue( source.contains("const MinNativeVersion = \"\(ClientVersion.minimum)\""), "ClientVersion.minimum and protocol.MinNativeVersion disagree") } }