react-native-audio-browser 0.2.0-next.81 → 0.2.0-next.82
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ios/Browser/BrowseGate.swift +14 -1
- package/ios/CarPlay/CarPlayNowPlayingManager.swift +23 -19
- package/ios/CarPlay/CarPlaySession+TabBar.swift +3 -5
- package/ios/CarPlay/MediaIntentHandler.swift +7 -2
- package/ios/CarPlay/MediaIntentPlayback.swift +23 -12
- package/ios/HybridAudioBrowser+Options.swift +1 -1
- package/ios/Model/NitroTypeStubs.swift +25 -0
- package/ios/Player/PlaybackStateMachine.swift +4 -3
- package/ios/Player/Player.swift +31 -18
- package/ios/Player/RetryLoop.swift +186 -193
- package/ios/Tests/BrowseGateTests.swift +50 -0
- package/ios/Tests/ConnectivityTests.swift +13 -0
- package/ios/Tests/MediaIntentDeadlineTests.swift +75 -0
- package/ios/Tests/PlaybackStateMachineTests.swift +6 -6
- package/ios/Tests/PlayerPolicyTests.swift +163 -50
- package/ios/Tests/PlayerTests.swift +11 -85
- package/ios/Tests/RetryLoopTests.swift +296 -120
- package/ios/Tests/SupersedingTaskTests.swift +148 -0
- package/ios/Tests/Support/TestTypes.swift +99 -8
- package/ios/Util/Connectivity.swift +12 -0
- package/ios/Util/Deadline.swift +38 -0
- package/ios/Util/NetworkMonitor.swift +2 -2
- package/ios/Util/SupersedingTask.swift +45 -0
- package/package.json +1 -1
|
@@ -4,65 +4,311 @@ import Testing
|
|
|
4
4
|
|
|
5
5
|
@testable import AudioBrowserTestable
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
/// A source as the stream server serves it, and the loop wired as the Player wires it, on a virtual
|
|
8
|
+
/// clock: each connect runs `connection`, and the terminal error drops the connection in flight.
|
|
9
|
+
@MainActor
|
|
10
|
+
private final class Rig {
|
|
11
|
+
let clock = VirtualClock()
|
|
12
|
+
let loop: RetryLoop
|
|
13
|
+
private(set) var connects: [Int] = []
|
|
14
|
+
private(set) var errors: [(atMs: Int, error: TrackPlayerError.PlaybackError)] = []
|
|
15
|
+
private var connection: (Rig) -> Void = { _ in }
|
|
16
|
+
private var current = 0
|
|
17
|
+
|
|
18
|
+
init(maxRetries: Int? = nil) {
|
|
19
|
+
loop = RetryLoop(clock: clock)
|
|
20
|
+
loop.updatePolicy(from: .second(RetryConfig(
|
|
21
|
+
maxRetries: maxRetries.map(Double.init), maxRetryDurationMs: nil, firstConnectMaxRetryDurationMs: nil,
|
|
22
|
+
)))
|
|
23
|
+
loop.onRetry = { [unowned self] _ in connect() }
|
|
24
|
+
loop.onError = { [unowned self] in
|
|
25
|
+
errors.append((clock.nowMs, $0))
|
|
26
|
+
current += 1
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/// A play with intent: a new Load and its first connect.
|
|
31
|
+
func play(_ connection: @escaping (Rig) -> Void) {
|
|
32
|
+
self.connection = connection
|
|
33
|
+
loop.playIntentChanged(true)
|
|
34
|
+
load()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/// A new Load of the same source, as a track change or a restart from the error makes one.
|
|
38
|
+
func load() {
|
|
39
|
+
loop.loadStarted()
|
|
40
|
+
connect()
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/// A connect: the loop's reconnect, or a reload the Player starts in place of the connection.
|
|
44
|
+
func connect() {
|
|
45
|
+
current += 1
|
|
46
|
+
connects.append(clock.nowMs)
|
|
47
|
+
loop.connectionStarted()
|
|
48
|
+
connection(self)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/// Runs `action` `afterMs` from now, unless the connection it belongs to has gone.
|
|
52
|
+
func later(_ afterMs: Int, _ action: @escaping (RetryLoop) -> Void) {
|
|
53
|
+
let connection = current
|
|
54
|
+
clock.at(clock.nowMs + afterMs) { [unowned self] in
|
|
55
|
+
if connection == current { action(loop) }
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
func run(untilMs: Int) async { await clock.run(until: untilMs) }
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
extension Rig {
|
|
63
|
+
/// Audio after `connectMs`, then a clean close `closeAfterMs` into it.
|
|
64
|
+
static func closes(afterMs closeAfterMs: Int, connectMs: Int = 300) -> (Rig) -> Void {
|
|
65
|
+
{ rig in
|
|
66
|
+
rig.later(connectMs) { $0.playbackBecameAudible() }
|
|
67
|
+
rig.later(connectMs + closeAfterMs) { loop in
|
|
68
|
+
loop.playbackStoppedBeingAudible()
|
|
69
|
+
loop.handleFailure(.liveEnd)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/// A connection that is refused, or times out, `afterMs` after it starts.
|
|
75
|
+
static func fails(afterMs: Int, with error: URLError.Code = .cannotConnectToHost) -> (Rig) -> Void {
|
|
76
|
+
{ rig in rig.later(afterMs) { $0.handleError(URLError(error), context: .mediaLoad) } }
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/// The gaps between each connection's close and the next connect, for one that closes on
|
|
80
|
+
/// `closeAfterMs` of audio after `connectMs`.
|
|
81
|
+
func gaps(closeAfterMs: Int, connectMs: Int = 300) -> Set<Int> {
|
|
82
|
+
Set(zip(connects, connects.dropFirst()).map { $1 - ($0 + connectMs + closeAfterMs) })
|
|
83
|
+
}
|
|
10
84
|
}
|
|
11
85
|
|
|
12
|
-
///
|
|
13
|
-
|
|
14
|
-
@Suite("RetryLoop budgets")
|
|
86
|
+
/// The plan's cases (docs/adr/0014), each as the stream server serves it on the simulator.
|
|
87
|
+
@Suite("RetryLoop cases")
|
|
15
88
|
@MainActor
|
|
16
|
-
struct
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
))
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
89
|
+
struct RetryLoopCaseTests {
|
|
90
|
+
@Test func aStreamThatClosesEvery10sPlaysOn_reconnectingAtOnce() async {
|
|
91
|
+
let rig = Rig()
|
|
92
|
+
rig.play(Rig.closes(afterMs: 10_000))
|
|
93
|
+
await rig.run(untilMs: 600_000)
|
|
94
|
+
#expect(rig.errors.isEmpty)
|
|
95
|
+
#expect(rig.connects.count > 50)
|
|
96
|
+
#expect(rig.gaps(closeAfterMs: 10_000) == [0])
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
@Test func aStreamThatClosesEvery30sPlaysOn() async {
|
|
100
|
+
let rig = Rig()
|
|
101
|
+
rig.play(Rig.closes(afterMs: 30_000))
|
|
102
|
+
await rig.run(untilMs: 600_000)
|
|
103
|
+
#expect(rig.errors.isEmpty)
|
|
104
|
+
#expect(rig.gaps(closeAfterMs: 30_000) == [0])
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/// No run of audio reaches 5 s, so the silence that began with the play never ends; the burst
|
|
108
|
+
/// playing at 120 s ends past the deadline, which gives up as it ends.
|
|
109
|
+
@Test func aStreamThatPlays2sPerConnectionEndsAt120s() async {
|
|
110
|
+
let rig = Rig()
|
|
111
|
+
rig.play(Rig.closes(afterMs: 2_000))
|
|
112
|
+
await rig.run(untilMs: 300_000)
|
|
113
|
+
#expect(rig.errors.map(\.error) == [.playbackStalled])
|
|
114
|
+
let endedAt = rig.errors.first?.atMs ?? 0
|
|
115
|
+
#expect((120_000 ... 122_300).contains(endedAt))
|
|
116
|
+
#expect(rig.connects.allSatisfy { $0 < endedAt })
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
@Test func aConnectionOpenWithNoDataEndsAt120s() async {
|
|
120
|
+
let rig = Rig()
|
|
121
|
+
rig.play { _ in }
|
|
122
|
+
await rig.run(untilMs: 300_000)
|
|
123
|
+
#expect(rig.errors.map(\.atMs) == [120_000])
|
|
124
|
+
#expect(rig.errors.map(\.error) == [.playbackStalled])
|
|
125
|
+
#expect(rig.connects == [0])
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
@Test func aDeadAddressThatNeverPlayedEnds12sAfterItsFirstFailure() async {
|
|
129
|
+
let rig = Rig()
|
|
130
|
+
rig.play(Rig.fails(afterMs: 50))
|
|
131
|
+
await rig.run(untilMs: 300_000)
|
|
132
|
+
#expect(rig.errors.map(\.atMs) == [12_050])
|
|
133
|
+
#expect(rig.errors.map(\.error) == [.hostUnreachable])
|
|
134
|
+
// Each wait grows from the failure before it: 1, 1.5, 2.25, 3.375 s.
|
|
135
|
+
#expect(rig.connects == [0, 1_050, 2_600, 4_900, 8_325])
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/// The deadline wins over a connect in flight: it gives up as it passes, and the connect's own
|
|
139
|
+
/// end, which the Player no longer hears, starts nothing.
|
|
140
|
+
@Test func theDeadlineGivesUpOnAConnectInFlight() async {
|
|
141
|
+
let rig = Rig()
|
|
142
|
+
rig.play(Rig.fails(afterMs: 5_000, with: .timedOut))
|
|
143
|
+
await rig.run(untilMs: 60_000)
|
|
144
|
+
#expect(rig.connects == [0, 6_000, 12_500])
|
|
145
|
+
#expect(rig.errors.map(\.atMs) == [17_000])
|
|
146
|
+
#expect(rig.errors.map(\.error) == [.hostUnreachable])
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
@Test func timeOfflineDuringAPlayedStreamsSilenceCounts() async {
|
|
150
|
+
let rig = Rig()
|
|
151
|
+
rig.play { rig in
|
|
152
|
+
rig.later(300) { $0.playbackBecameAudible() }
|
|
153
|
+
// As the Player reports it: the failure arrives while its state still reads .playing.
|
|
154
|
+
rig.later(60_300) { loop in
|
|
155
|
+
loop.onlineChanged(false)
|
|
156
|
+
loop.handleFailure(.playback)
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
await rig.run(untilMs: 400_000)
|
|
160
|
+
#expect(rig.connects == [0])
|
|
161
|
+
#expect(rig.errors.map(\.atMs) == [180_300])
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
@Test func theNetworkReturningReconnectsWithinHalfASecond() async {
|
|
165
|
+
let rig = Rig()
|
|
166
|
+
rig.play { rig in
|
|
167
|
+
rig.later(300) { $0.playbackBecameAudible() }
|
|
168
|
+
rig.later(60_300) { loop in
|
|
169
|
+
loop.onlineChanged(false)
|
|
170
|
+
loop.handleFailure(.playback)
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
rig.clock.at(90_000) { rig.loop.onlineChanged(true) }
|
|
174
|
+
await rig.run(untilMs: 91_000)
|
|
175
|
+
// The wait asks the pacer again every half second.
|
|
176
|
+
#expect(rig.connects.count == 2)
|
|
177
|
+
#expect((90_000 ... 90_500).contains(rig.connects.last ?? 0))
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
@Test func aRestartAfterTheTerminalErrorStartsFresh() async {
|
|
181
|
+
let rig = Rig()
|
|
182
|
+
rig.play(Rig.fails(afterMs: 50))
|
|
183
|
+
rig.clock.at(20_000) { rig.load() }
|
|
184
|
+
await rig.run(untilMs: 300_000)
|
|
185
|
+
#expect(rig.errors.map(\.atMs) == [12_050, 32_050])
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
@Test func aNewLoadStartsFresh() async {
|
|
189
|
+
let rig = Rig()
|
|
190
|
+
rig.play(Rig.fails(afterMs: 50))
|
|
191
|
+
rig.clock.at(8_000) { rig.load() }
|
|
192
|
+
await rig.run(untilMs: 300_000)
|
|
193
|
+
#expect(rig.errors.map(\.atMs) == [20_050])
|
|
32
194
|
}
|
|
33
195
|
|
|
34
|
-
@Test func
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
196
|
+
@Test func maxRetriesCountsConnectionsInARowThatDeliveredNothing() async {
|
|
197
|
+
let rig = Rig(maxRetries: 2)
|
|
198
|
+
rig.play(Rig.fails(afterMs: 50))
|
|
199
|
+
await rig.run(untilMs: 300_000)
|
|
200
|
+
#expect(rig.connects == [0, 1_050, 2_600])
|
|
201
|
+
#expect(rig.errors.map(\.atMs) == [2_650])
|
|
202
|
+
}
|
|
39
203
|
|
|
40
|
-
|
|
41
|
-
|
|
204
|
+
@Test func aPauseClearsTheDeadline() async {
|
|
205
|
+
let rig = Rig()
|
|
206
|
+
rig.play { _ in }
|
|
207
|
+
rig.clock.at(60_000) { rig.loop.playIntentChanged(false) }
|
|
208
|
+
await rig.run(untilMs: 300_000)
|
|
209
|
+
#expect(rig.errors.isEmpty)
|
|
42
210
|
}
|
|
43
211
|
|
|
44
|
-
@Test func
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
212
|
+
@Test func aCancelClearsTheDeadline_untilTheNextConnect() async {
|
|
213
|
+
let rig = Rig()
|
|
214
|
+
rig.play { _ in }
|
|
215
|
+
rig.clock.at(60_000) { rig.loop.cancel() }
|
|
216
|
+
await rig.run(untilMs: 300_000)
|
|
217
|
+
#expect(rig.errors.isEmpty)
|
|
218
|
+
}
|
|
49
219
|
|
|
50
|
-
|
|
51
|
-
|
|
220
|
+
/// A media reset or `retryNow` reloads in place of a connection that played: its end starts the
|
|
221
|
+
/// count over, so the next connection may fail once and still reconnect.
|
|
222
|
+
@Test func aReloadThePlayerStartsEndsTheConnectionItReplaces() async {
|
|
223
|
+
let rig = Rig(maxRetries: 1)
|
|
224
|
+
var connection = 0
|
|
225
|
+
rig.play { rig in
|
|
226
|
+
connection += 1
|
|
227
|
+
if connection == 2 {
|
|
228
|
+
rig.later(300) { $0.playbackBecameAudible() }
|
|
229
|
+
} else {
|
|
230
|
+
rig.later(50) { $0.handleError(URLError(.cannotConnectToHost), context: .mediaLoad) }
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
rig.clock.at(60_000) {
|
|
234
|
+
rig.loop.playbackStoppedBeingAudible()
|
|
235
|
+
rig.connect()
|
|
236
|
+
}
|
|
237
|
+
await rig.run(untilMs: 61_060)
|
|
238
|
+
#expect(rig.connects == [0, 1_050, 60_000, 61_050])
|
|
239
|
+
#expect(rig.errors.isEmpty)
|
|
52
240
|
}
|
|
53
241
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
let
|
|
242
|
+
/// `retryNow` or a seek to the live edge during a reconnect cuts it off before it delivers
|
|
243
|
+
/// anything: that is the listener's, not the source's failure.
|
|
244
|
+
@Test func aReloadThatCutsOffAConnectDoesNotCountAgainstTheSource() async {
|
|
245
|
+
let rig = Rig(maxRetries: 2)
|
|
246
|
+
var connection = 0
|
|
247
|
+
rig.play { rig in
|
|
248
|
+
connection += 1
|
|
249
|
+
if !(2 ... 4).contains(connection) {
|
|
250
|
+
rig.later(50) { $0.handleError(URLError(.cannotConnectToHost), context: .mediaLoad) }
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
// The second connect hangs: three reloads replace it while it connects.
|
|
254
|
+
rig.clock.at(2_000) { rig.connect() }
|
|
255
|
+
rig.clock.at(2_100) { rig.connect() }
|
|
256
|
+
rig.clock.at(2_200) { rig.connect() }
|
|
257
|
+
await rig.run(untilMs: 3_760)
|
|
258
|
+
#expect(rig.connects == [0, 1_050, 2_000, 2_100, 2_200, 3_750])
|
|
259
|
+
#expect(rig.errors.isEmpty)
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/// The first-connect clock starts at the first failure seen online.
|
|
263
|
+
@Test func timeOfflineDoesNotBurnTheFirstConnectBudget() async {
|
|
264
|
+
let rig = Rig()
|
|
265
|
+
rig.loop.onlineChanged(false)
|
|
266
|
+
rig.play(Rig.fails(afterMs: 50))
|
|
267
|
+
rig.clock.at(60_000) { rig.loop.onlineChanged(true) }
|
|
268
|
+
await rig.run(untilMs: 300_000)
|
|
269
|
+
// Offline, the wait asks again every half second from the failure at 50 ms.
|
|
270
|
+
#expect(rig.connects.prefix(2) == [0, 60_050])
|
|
271
|
+
#expect(rig.errors.map(\.atMs) == [72_100])
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/// A silence that ends with no failure after audio played is a stall, not the failure before it.
|
|
275
|
+
@Test func theDeadlineReportsAStall_notAFailureAudioHasPlayedSince() async {
|
|
276
|
+
let rig = Rig()
|
|
277
|
+
var connection = 0
|
|
278
|
+
rig.play { rig in
|
|
279
|
+
connection += 1
|
|
280
|
+
if connection == 1 {
|
|
281
|
+
rig.later(50) { $0.handleError(nil, context: .mediaLoad, httpStatusCode: 503) }
|
|
282
|
+
} else {
|
|
283
|
+
rig.later(300) { $0.playbackBecameAudible() }
|
|
284
|
+
rig.later(60_000) { $0.playbackStoppedBeingAudible() }
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
await rig.run(untilMs: 300_000)
|
|
288
|
+
#expect(rig.connects == [0, 1_050])
|
|
289
|
+
#expect(rig.errors.map(\.atMs) == [181_050])
|
|
290
|
+
#expect(rig.errors.map(\.error) == [.playbackStalled])
|
|
291
|
+
}
|
|
58
292
|
|
|
59
|
-
|
|
60
|
-
|
|
293
|
+
@Test func aPlayWithNothingLoadedNeverGivesUp() async {
|
|
294
|
+
let rig = Rig()
|
|
295
|
+
rig.loop.playIntentChanged(true)
|
|
296
|
+
await rig.run(untilMs: 300_000)
|
|
297
|
+
#expect(rig.errors.isEmpty)
|
|
298
|
+
}
|
|
61
299
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
300
|
+
/// AVFoundation can report one failure through the item, its notification and the player.
|
|
301
|
+
@Test func aFailureReportedTwiceEndsItsConnectionOnce() async {
|
|
302
|
+
let rig = Rig(maxRetries: 2)
|
|
303
|
+
rig.play { rig in
|
|
304
|
+
rig.later(50) { loop in
|
|
305
|
+
loop.handleError(URLError(.cannotConnectToHost), context: .mediaLoad)
|
|
306
|
+
loop.handleError(URLError(.cannotConnectToHost), context: .playback)
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
await rig.run(untilMs: 300_000)
|
|
310
|
+
#expect(rig.connects == [0, 1_050, 2_600])
|
|
311
|
+
#expect(rig.errors.map(\.atMs) == [2_650])
|
|
66
312
|
}
|
|
67
313
|
}
|
|
68
314
|
|
|
@@ -115,7 +361,6 @@ struct RetryLoopHandleErrorTests {
|
|
|
115
361
|
#expect(record.errors.isEmpty)
|
|
116
362
|
// Session release waits on `pendingTask`, so a finished retry must clear it.
|
|
117
363
|
#expect(loop.pendingTask == nil)
|
|
118
|
-
#expect(loop.attempts == 2)
|
|
119
364
|
}
|
|
120
365
|
|
|
121
366
|
@Test func theAdvisoryPrecedesTheRetry() async {
|
|
@@ -173,20 +418,11 @@ struct RetryLoopHandleErrorTests {
|
|
|
173
418
|
#expect(record.errors == [.failedToLoadKeyValue])
|
|
174
419
|
}
|
|
175
420
|
|
|
176
|
-
@Test func
|
|
177
|
-
let (loop, _) = makeLoop()
|
|
178
|
-
loop.handleError(URLError(.timedOut), context: .playback)
|
|
179
|
-
await loop.pendingTask?.value
|
|
180
|
-
#expect(loop.attempts == 1)
|
|
181
|
-
loop.reset()
|
|
182
|
-
#expect(loop.attempts == 0)
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
@Test func resetDropsTheRetryInFlightSilently() async {
|
|
421
|
+
@Test func aNewLoadDropsTheRetryInFlightSilently() async {
|
|
186
422
|
let (loop, record) = makeLoop(delay: 0.02)
|
|
187
423
|
loop.handleError(URLError(.timedOut), context: .playback)
|
|
188
424
|
let task = loop.pendingTask
|
|
189
|
-
loop.
|
|
425
|
+
loop.loadStarted()
|
|
190
426
|
#expect(loop.pendingTask == nil)
|
|
191
427
|
await task?.value
|
|
192
428
|
#expect(record.errors.isEmpty)
|
|
@@ -201,7 +437,7 @@ struct RetryLoopHandleErrorTests {
|
|
|
201
437
|
try await Task.sleep(for: .seconds(60))
|
|
202
438
|
})
|
|
203
439
|
loop.updatePolicy(from: .first(true))
|
|
204
|
-
loop.
|
|
440
|
+
loop.playIntentChanged(true)
|
|
205
441
|
let record = Record()
|
|
206
442
|
loop.onError = { record.errors.append($0) }
|
|
207
443
|
loop.onRetry = { record.reloads.append($0) }
|
|
@@ -229,7 +465,7 @@ struct RetryLoopHandleErrorTests {
|
|
|
229
465
|
})
|
|
230
466
|
holder.loop = loop
|
|
231
467
|
loop.updatePolicy(from: .first(true))
|
|
232
|
-
loop.
|
|
468
|
+
loop.playIntentChanged(true)
|
|
233
469
|
let record = Record()
|
|
234
470
|
loop.onError = { record.errors.append($0) }
|
|
235
471
|
loop.onRetry = { record.reloads.append($0) }
|
|
@@ -241,67 +477,7 @@ struct RetryLoopHandleErrorTests {
|
|
|
241
477
|
}
|
|
242
478
|
}
|
|
243
479
|
|
|
244
|
-
@Suite("RetryLoop.handleFailure")
|
|
245
|
-
@MainActor
|
|
246
|
-
struct RetryLoopHandleFailureTests {
|
|
247
|
-
private final class Sleeps: @unchecked Sendable {
|
|
248
|
-
var seconds: [TimeInterval] = []
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
@Test func anImmediateFailureSkipsTheBackoff_butSpendsAnAttempt() async {
|
|
252
|
-
let sleeps = Sleeps()
|
|
253
|
-
let loop = RetryLoop(sleep: { sleeps.seconds.append($0) })
|
|
254
|
-
loop.updatePolicy(from: .first(true))
|
|
255
|
-
var reloads: [Bool] = []
|
|
256
|
-
loop.onRetry = { reloads.append($0) }
|
|
257
|
-
|
|
258
|
-
loop.handleFailure(.stall)
|
|
259
|
-
await loop.pendingTask?.value
|
|
260
|
-
loop.handleFailure(.stall, immediately: true)
|
|
261
|
-
await loop.pendingTask?.value
|
|
262
|
-
|
|
263
|
-
#expect(sleeps.seconds == [1, 0])
|
|
264
|
-
#expect(reloads == [true, true])
|
|
265
|
-
#expect(loop.attempts == 2)
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
480
|
@MainActor
|
|
270
481
|
private final class LoopHolder {
|
|
271
482
|
weak var loop: RetryLoop?
|
|
272
483
|
}
|
|
273
|
-
|
|
274
|
-
@Suite("RetryLoop healthy playback")
|
|
275
|
-
@MainActor
|
|
276
|
-
struct RetryLoopHealthyPlaybackTests {
|
|
277
|
-
private func makeAfterOneRetry() async -> RetryLoop {
|
|
278
|
-
let loop = makeRetryLoop()
|
|
279
|
-
loop.handleError(URLError(.timedOut), context: .playback)
|
|
280
|
-
await loop.pendingTask?.value
|
|
281
|
-
return loop
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
@Test func audiblePlaybackForTheWindowRefillsTheBudgets() async {
|
|
285
|
-
let loop = await makeAfterOneRetry()
|
|
286
|
-
#expect(loop.attempts == 1)
|
|
287
|
-
loop.healthyPlaybackDuration = .milliseconds(10)
|
|
288
|
-
|
|
289
|
-
loop.playbackBecameAudible()
|
|
290
|
-
await loop.healthyPlaybackTask?.value
|
|
291
|
-
|
|
292
|
-
#expect(loop.attempts == 0)
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
@Test func stoppingBeforeTheWindowKeepsTheBudgets() async {
|
|
296
|
-
let loop = await makeAfterOneRetry()
|
|
297
|
-
loop.healthyPlaybackDuration = .milliseconds(500)
|
|
298
|
-
|
|
299
|
-
loop.playbackBecameAudible()
|
|
300
|
-
let refill = loop.healthyPlaybackTask
|
|
301
|
-
loop.playbackStoppedBeingAudible()
|
|
302
|
-
await refill?.value
|
|
303
|
-
|
|
304
|
-
#expect(loop.attempts == 1)
|
|
305
|
-
#expect(loop.healthyPlaybackTask == nil)
|
|
306
|
-
}
|
|
307
|
-
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import Testing
|
|
2
|
+
|
|
3
|
+
@testable import AudioBrowserTestable
|
|
4
|
+
|
|
5
|
+
@MainActor
|
|
6
|
+
@Suite("SupersedingTask")
|
|
7
|
+
struct SupersedingTaskTests {
|
|
8
|
+
/// Work that never answers unless cancelled, recording how each run ended.
|
|
9
|
+
@MainActor
|
|
10
|
+
final class SilentWork {
|
|
11
|
+
var started = 0
|
|
12
|
+
var cancelled = 0
|
|
13
|
+
|
|
14
|
+
func run() async throws -> Int? {
|
|
15
|
+
started += 1
|
|
16
|
+
do {
|
|
17
|
+
return try await awaitCallback { (_: @escaping @Sendable (Result<Int?, Error>) -> Void) in }
|
|
18
|
+
} catch {
|
|
19
|
+
if error is CancellationError { cancelled += 1 }
|
|
20
|
+
throw error
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
private func yieldUntil(_ condition: () -> Bool, sourceLocation: SourceLocation = #_sourceLocation) async {
|
|
26
|
+
for _ in 0 ..< 1000 where !condition() {
|
|
27
|
+
await Task.yield()
|
|
28
|
+
}
|
|
29
|
+
#expect(condition(), sourceLocation: sourceLocation)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
@Test func aNewRunCancelsTheSilentOneBefore() async {
|
|
33
|
+
let task = SupersedingTask<String, Int>()
|
|
34
|
+
let work = SilentWork()
|
|
35
|
+
var delivered: [Result<Int?, Error>] = []
|
|
36
|
+
task.run(key: nil) { try await work.run() } deliver: { delivered.append($0) }
|
|
37
|
+
await yieldUntil { work.started == 1 }
|
|
38
|
+
task.run(key: nil) { try await work.run() } deliver: { delivered.append($0) }
|
|
39
|
+
await yieldUntil { work.cancelled == 1 }
|
|
40
|
+
#expect(work.started == 2)
|
|
41
|
+
#expect(delivered.isEmpty)
|
|
42
|
+
task.cancel()
|
|
43
|
+
await yieldUntil { work.cancelled == 2 }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
@Test func cancelEndsTheRunWithoutDelivering() async {
|
|
47
|
+
let task = SupersedingTask<String, Int>()
|
|
48
|
+
let work = SilentWork()
|
|
49
|
+
var delivered = 0
|
|
50
|
+
task.run(key: nil) { try await work.run() } deliver: { _ in delivered += 1 }
|
|
51
|
+
await yieldUntil { work.started == 1 }
|
|
52
|
+
task.cancel()
|
|
53
|
+
await yieldUntil { work.cancelled == 1 }
|
|
54
|
+
for _ in 0 ..< 10 {
|
|
55
|
+
await Task.yield()
|
|
56
|
+
}
|
|
57
|
+
#expect(delivered == 0)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
@Test func deliversOnlyTheLatestResult() async {
|
|
61
|
+
let task = SupersedingTask<String, Int>()
|
|
62
|
+
var resumeFirst: CheckedContinuation<Void, Never>?
|
|
63
|
+
var delivered: [Int?] = []
|
|
64
|
+
task.run(key: nil) {
|
|
65
|
+
// Ignores cancellation, so its answer arrives after it was superseded.
|
|
66
|
+
await withCheckedContinuation { resumeFirst = $0 }
|
|
67
|
+
return 1
|
|
68
|
+
} deliver: { delivered.append(try! $0.get()) }
|
|
69
|
+
await yieldUntil { resumeFirst != nil }
|
|
70
|
+
task.run(key: nil) { 2 } deliver: { delivered.append(try! $0.get()) }
|
|
71
|
+
await yieldUntil { delivered == [2] }
|
|
72
|
+
resumeFirst?.resume()
|
|
73
|
+
for _ in 0 ..< 10 {
|
|
74
|
+
await Task.yield()
|
|
75
|
+
}
|
|
76
|
+
#expect(delivered == [2])
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
@Test func aRunForTheLatestKeyIsSkipped() async {
|
|
80
|
+
let task = SupersedingTask<String, Int>()
|
|
81
|
+
let work = SilentWork()
|
|
82
|
+
task.run(key: "a") { try await work.run() } deliver: { _ in }
|
|
83
|
+
task.run(key: "a") { try await work.run() } deliver: { _ in }
|
|
84
|
+
await yieldUntil { work.started == 1 }
|
|
85
|
+
task.run(key: "b") { try await work.run() } deliver: { _ in }
|
|
86
|
+
await yieldUntil { work.started == 2 }
|
|
87
|
+
#expect(work.cancelled == 1)
|
|
88
|
+
task.cancel()
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
@Test func aDeliveredKeyIsSkippedUntilCancel() async {
|
|
92
|
+
let task = SupersedingTask<String, Int>()
|
|
93
|
+
var runs = 0
|
|
94
|
+
var delivered: [Int?] = []
|
|
95
|
+
task.run(key: "a") { runs += 1; return 1 } deliver: { delivered.append(try! $0.get()) }
|
|
96
|
+
await yieldUntil { delivered == [1] }
|
|
97
|
+
task.run(key: "a") { runs += 1; return 2 } deliver: { delivered.append(try! $0.get()) }
|
|
98
|
+
for _ in 0 ..< 10 {
|
|
99
|
+
await Task.yield()
|
|
100
|
+
}
|
|
101
|
+
#expect(runs == 1)
|
|
102
|
+
task.cancel()
|
|
103
|
+
task.run(key: "a") { runs += 1; return 3 } deliver: { delivered.append(try! $0.get()) }
|
|
104
|
+
await yieldUntil { delivered == [1, 3] }
|
|
105
|
+
#expect(runs == 2)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
@Test func aFailedKeyRunsAgain() async {
|
|
109
|
+
struct Failure: Error {}
|
|
110
|
+
let task = SupersedingTask<String, Int>()
|
|
111
|
+
var runs = 0
|
|
112
|
+
var failed = false
|
|
113
|
+
task.run(key: "a") { runs += 1; throw Failure() } deliver: { _ in failed = true }
|
|
114
|
+
await yieldUntil { failed }
|
|
115
|
+
task.run(key: "a") { runs += 1; return 1 } deliver: { _ in }
|
|
116
|
+
await yieldUntil { runs == 2 }
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
@Test func aNilAnswerRunsAgain() async {
|
|
120
|
+
let task = SupersedingTask<String, Int>()
|
|
121
|
+
var runs = 0
|
|
122
|
+
var answered = false
|
|
123
|
+
task.run(key: "a") { runs += 1; return nil } deliver: { _ in answered = true }
|
|
124
|
+
await yieldUntil { answered }
|
|
125
|
+
task.run(key: "a") { runs += 1; return 1 } deliver: { _ in }
|
|
126
|
+
await yieldUntil { runs == 2 }
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
@Test func aNilKeyAlwaysRuns() async {
|
|
130
|
+
let task = SupersedingTask<String, Int>()
|
|
131
|
+
let work = SilentWork()
|
|
132
|
+
task.run(key: nil) { try await work.run() } deliver: { _ in }
|
|
133
|
+
await yieldUntil { work.started == 1 }
|
|
134
|
+
task.run(key: nil) { try await work.run() } deliver: { _ in }
|
|
135
|
+
await yieldUntil { work.started == 2 }
|
|
136
|
+
task.cancel()
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
@Test func deliversAFailure() async {
|
|
140
|
+
struct Failure: Error {}
|
|
141
|
+
let task = SupersedingTask<String, Int>()
|
|
142
|
+
var failed = false
|
|
143
|
+
task.run(key: nil) { throw Failure() } deliver: { result in
|
|
144
|
+
if case .failure(is Failure) = result { failed = true }
|
|
145
|
+
}
|
|
146
|
+
await yieldUntil { failed }
|
|
147
|
+
}
|
|
148
|
+
}
|