oficina / meet Public

Clone
git clone https://amadan.net/oficina/meet

Plain git — no account needed to clone.

Download

Download this file

1import XCTest
2
3@testable import CallsKit
4
5/// A connection that stops working is mended without anyone asking (spec
6/// §15.28, 2026-10-21): when it counts as broken, the backoff, the
7/// escalation to a rebuild, who acts, and when the failure is said.
8final class ConnectionRecoveryTests: XCTestCase {
9 private typealias Look = ConnectionRecovery.Look
10
11 /// Runs the recovery at 5 Hz from `from` to `to`, with the look `look(t)`
12 /// gives; returns each action with the time it was asked for.
13 private func run(
14 _ r: inout ConnectionRecovery, from: Double, to: Double, look: (Double) -> Look
15 ) -> [(Double, ConnectionRecovery.Action)] {
16 var out: [(Double, ConnectionRecovery.Action)] = []
17 // Whole ticks of a fifth of a second, so the clock does not drift.
18 for tick in Int((from * 5).rounded())...Int((to * 5).rounded()) {
19 let t = Double(tick) / 5
20 if let a = r.observe(at: t, look(t)) { out.append((t, a)) }
21 }
22 return out
23 }
24
25 func testAFailedConnectionIsRestartedTwiceThenRebuiltOnTheBackoff() {
26 var r = ConnectionRecovery()
27 let actions = run(&r, from: 0, to: 160) { _ in Look(ice: .failed, builtAt: -100, leader: true) }
28 XCTAssertEqual(actions.map(\.0), [0, 2, 7, 17, 37, 67, 97, 127, 157])
29 XCTAssertEqual(
30 actions.map(\.1),
31 [.restart, .restart, .rebuild, .restart, .restart, .rebuild, .restart, .restart, .rebuild])
32 XCTAssertEqual(r.restarts, 6)
33 XCTAssertEqual(r.rebuilds, 3)
34 }
35
36 func testDisconnectedCountsAfterFiveSeconds() {
37 var r = ConnectionRecovery()
38 let actions = run(&r, from: 0, to: 4.8) { _ in Look(ice: .disconnected, builtAt: -100, leader: true) }
39 XCTAssertTrue(actions.isEmpty, "a blip is not a failure")
40 XCTAssertFalse(r.trying)
41 XCTAssertEqual(r.observe(at: 5, Look(ice: .disconnected, builtAt: -100, leader: true)), .restart)
42 XCTAssertTrue(r.trying)
43 }
44
45 func testADisconnectionThatComesBackInTimeIsNothing() {
46 var r = ConnectionRecovery()
47 let actions = run(&r, from: 0, to: 20) { t in
48 Look(ice: Int(t) % 6 < 4 ? .disconnected : .connected, builtAt: -100, leader: true)
49 }
50 XCTAssertTrue(actions.isEmpty)
51 }
52
53 /// The incident's shape: a connection that never comes up, which
54 /// libwebrtc leaves in `checking` rather than calling failed.
55 func testAConnectionThatNeverComesUpIsTroubleAfterFifteenSeconds() {
56 var r = ConnectionRecovery()
57 XCTAssertTrue(run(&r, from: 0, to: 14.8) { _ in Look(ice: .checking, builtAt: 0, leader: true) }.isEmpty)
58 XCTAssertEqual(r.observe(at: 15, Look(ice: .checking, builtAt: 0, leader: true)), .restart)
59 var fresh = ConnectionRecovery()
60 XCTAssertTrue(run(&fresh, from: 0, to: 14.8) { _ in Look(ice: .new, builtAt: 0, leader: true) }.isEmpty)
61 XCTAssertEqual(fresh.observe(at: 15, Look(ice: .new, builtAt: 0, leader: true)), .restart)
62 }
63
64 func testRecoveryEndsTheEpisodeAndClearsTheWords() {
65 var r = ConnectionRecovery()
66 _ = run(&r, from: 0, to: 61) { _ in Look(ice: .failed, builtAt: -100, leader: true) }
67 XCTAssertTrue(r.trying)
68 XCTAssertTrue(r.failing(at: 61))
69 XCTAssertNil(r.observe(at: 61.2, Look(ice: .connected, builtAt: -100, leader: true)))
70 XCTAssertFalse(r.trying)
71 XCTAssertFalse(r.failing(at: 61.2))
72 }
73
74 func testTheFailureIsSaidOnlyAfterAMinute() {
75 var r = ConnectionRecovery()
76 _ = run(&r, from: 0, to: 59.8) { _ in Look(ice: .failed, builtAt: -100, leader: true) }
77 XCTAssertTrue(r.trying)
78 XCTAssertFalse(r.failing(at: 59.8))
79 XCTAssertTrue(r.failing(at: 60))
80 }
81
82 /// A link that comes and goes is not hammered from nought each time.
83 func testTroubleSoonAfterARecoveryCarriesOnWhereTheScheduleWas() {
84 var r = ConnectionRecovery()
85 _ = run(&r, from: 0, to: 8) { _ in Look(ice: .failed, builtAt: -100, leader: true) } // 0, 2, 7
86 XCTAssertEqual(r.attempts, 3)
87 XCTAssertNil(r.observe(at: 9, Look(ice: .connected, builtAt: 8, leader: true)))
88 let again = run(&r, from: 20, to: 60) { _ in Look(ice: .failed, builtAt: 8, leader: true) }
89 // Attempt 4 is due its own gap (10 s) after the trouble, not at once.
90 XCTAssertEqual(again.first?.0, 30)
91 XCTAssertEqual(again.first?.1, .restart)
92 // Well after a recovery it starts from nought again.
93 var later = ConnectionRecovery()
94 _ = run(&later, from: 0, to: 8) { _ in Look(ice: .failed, builtAt: -100, leader: true) }
95 XCTAssertNil(later.observe(at: 9, Look(ice: .connected, builtAt: 8, leader: true)))
96 XCTAssertEqual(later.observe(at: 80, Look(ice: .failed, builtAt: 8, leader: true)), .restart)
97 }
98
99 func testNothingIsAttemptedWhileTheSignallingStreamIsDown() {
100 var r = ConnectionRecovery()
101 let down = run(&r, from: 0, to: 30) { _ in Look(ice: .failed, builtAt: -100, leader: true, signalling: false) }
102 XCTAssertTrue(down.isEmpty)
103 XCTAssertTrue(r.trying, "still in trouble, and still said")
104 XCTAssertEqual(r.observe(at: 30.2, Look(ice: .failed, builtAt: -100, leader: true)), .restart)
105 }
106
107 func testANegotiationStepThatThrewHastensTroubleOnlyWhileNotConnected() {
108 var r = ConnectionRecovery()
109 XCTAssertEqual(r.observe(at: 1, Look(ice: .checking, builtAt: 0, leader: true, errored: true)), .restart)
110 var up = ConnectionRecovery()
111 XCTAssertNil(up.observe(at: 1, Look(ice: .connected, builtAt: 0, leader: true, errored: true)))
112 XCTAssertFalse(up.trying)
113 }
114
115 func testAClosedConnectionIsLeftAlone() {
116 var r = ConnectionRecovery()
117 XCTAssertTrue(run(&r, from: 0, to: 30) { _ in Look(ice: .closed, builtAt: -100, leader: true) }.isEmpty)
118 XCTAssertFalse(r.trying)
119 }
120
121 /// The polite side follows three seconds behind, and stands back for
122 /// whatever the leader has already done.
123 func testTheFollowerActsOnlyWhenTheLeaderDoesNot() {
124 // A leader from before this release: it never acts, so the follower
125 // runs the whole schedule three seconds late.
126 var alone = ConnectionRecovery()
127 let actions = run(&alone, from: 0, to: 21) { _ in Look(ice: .failed, builtAt: -100, leader: false) }
128 XCTAssertEqual(actions.map(\.0), [3, 5, 10, 20])
129 XCTAssertEqual(actions.map(\.1), [.restart, .restart, .rebuild, .restart])
130
131 // A leader that does act: its restart offers arrive, and its rebuild
132 // gives this side a new connection; the follower asks for nothing.
133 var follower = ConnectionRecovery()
134 var built = -100.0
135 let none = run(&follower, from: 0, to: 40) { t in
136 // The leader's restarts land 0.1 s after 0 and 2, its rebuild after 7.
137 let offer: Double? = t >= 7.1 ? (t >= 17.1 ? 17.1 : 7.1) : t >= 2.1 ? 2.1 : t >= 0.1 ? 0.1 : nil
138 if t >= 7.1 { built = 7.1 }
139 return Look(ice: .failed, builtAt: built, leader: false, remoteOfferAt: offer)
140 }
141 XCTAssertTrue(none.isEmpty, "the follower acted beside the leader: \(none)")
142 }
143
144 /// Two sides whose episodes started a beat apart still rebuild once.
145 func testAFollowerAStepBehindDoesNotRebuildAgain() {
146 var follower = ConnectionRecovery()
147 // The follower notices one second after the leader: the leader's
148 // rebuild (at its 7 s) lands at the follower's 6.1 s.
149 let actions = run(&follower, from: 0, to: 12) { t in
150 Look(ice: .failed, builtAt: t >= 6.1 ? 6.1 : -100, leader: false, remoteOfferAt: t >= 1.1 ? 1.1 : nil)
151 }
152 XCTAssertFalse(actions.contains { $0.1 == .rebuild }, "\(actions)")
153 }
154
155 func testTheVersionNoticeRule() {
156 XCTAssertEqual(
157 ClientVersion.notice(name: "Farshad", client: "mac 0.4.1", failing: false),
158 "Farshad is on an old version of Calls — ask them to update")
159 XCTAssertEqual(
160 ClientVersion.notice(name: "Farshad", client: "iphone 0.3.9", failing: true),
161 "Farshad is on an old version of Calls — ask them to update")
162 XCTAssertNil(ClientVersion.notice(name: "Farshad", client: "mac 0.4.2", failing: true))
163 XCTAssertNil(ClientVersion.notice(name: "Farshad", client: "mac 0.4.12", failing: true))
164 // No version at all: an app from before §15.28, only worth a word
165 // once the connection to it has been failing.
166 XCTAssertNil(ClientVersion.notice(name: "Farshad", client: "native", failing: false))
167 XCTAssertEqual(
168 ClientVersion.notice(name: "Farshad", client: "native", failing: true),
169 "Farshad may be on an old version of Calls — ask them to update")
170 // A browser, or a server from before the key: never.
171 XCTAssertNil(ClientVersion.notice(name: "Farshad", client: nil, failing: true))
172 XCTAssertNil(ClientVersion.notice(name: "Farshad", client: "web 882eab8", failing: true))
173 XCTAssertNil(ClientVersion.notice(name: "Farshad", client: "mac latest", failing: true))
174 }
175
176 func testVersionsCompareNumerically() {
177 XCTAssertEqual(ClientVersion.compare("0.4.10", "0.4.2"), 1)
178 XCTAssertEqual(ClientVersion.compare("0.4.1", "0.4.2"), -1)
179 XCTAssertEqual(ClientVersion.compare("0.4", "0.4.0"), 0)
180 XCTAssertEqual(ClientVersion.compare("0.10", "0.9.9"), 1)
181 XCTAssertEqual(ClientVersion.compare("1", "0.99"), 1)
182 }
183
184 func testTheMinimumIsTheServers() throws {
185 // protocol.MinNativeVersion, which carries the reason; the two must agree.
186 let url = URL(fileURLWithPath: #filePath).deletingLastPathComponent()
187 .appendingPathComponent("../../../../protocol/client.go").standardized
188 let source = try String(contentsOf: url, encoding: .utf8)
189 XCTAssertTrue(
190 source.contains("const MinNativeVersion = \"\(ClientVersion.minimum)\""),
191 "ClientVersion.minimum and protocol.MinNativeVersion disagree")
192 }
193}
194