react-native-audio-browser 0.2.0-next.81 → 0.2.0-next.83
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/ErrorStatus.swift +4 -0
- package/ios/Player/PlaybackStateMachine.swift +4 -3
- package/ios/Player/Player.swift +53 -26
- package/ios/Player/RetryLoop.swift +198 -201
- 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 +273 -51
- package/ios/Tests/PlayerTests.swift +43 -85
- package/ios/Tests/RetryLoopTests.swift +299 -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
|
@@ -11,7 +11,7 @@ private func makeCoordinator() -> (
|
|
|
11
11
|
) {
|
|
12
12
|
let clock = ManualSleepTimerScheduler()
|
|
13
13
|
let engine = MockPlayerEngine()
|
|
14
|
-
let player = makePlayer(engine: engine,
|
|
14
|
+
let player = makePlayer(engine: engine, sleepTimerScheduler: clock)
|
|
15
15
|
let callbacks = MockPlayerCallbacks()
|
|
16
16
|
player.callbacks = callbacks
|
|
17
17
|
return (player, engine, callbacks, clock)
|
|
@@ -113,6 +113,8 @@ private func makeRetrying() -> (
|
|
|
113
113
|
let retryHandler = makeRetryLoop()
|
|
114
114
|
let engine = MockPlayerEngine()
|
|
115
115
|
let player = makePlayer(engine: engine, retry: retryHandler)
|
|
116
|
+
// The loop's intent alone, so a test drives the states without the engine calls a play makes.
|
|
117
|
+
retryHandler.playIntentChanged(true)
|
|
116
118
|
let callbacks = MockPlayerCallbacks()
|
|
117
119
|
player.callbacks = callbacks
|
|
118
120
|
return (player, retryHandler, callbacks, engine)
|
|
@@ -192,6 +194,8 @@ struct RetryingErrorTests {
|
|
|
192
194
|
c.retry.handleError(URLError(.cannotConnectToHost), context: .mediaLoad)
|
|
193
195
|
await c.retry.pendingTask?.value
|
|
194
196
|
|
|
197
|
+
// With no track, the reload starts no connection: the second failure needs one of its own.
|
|
198
|
+
c.retry.connectionStarted()
|
|
195
199
|
c.retry.handleError(URLError(.cannotConnectToHost), context: .mediaLoad)
|
|
196
200
|
await c.retry.pendingTask?.value
|
|
197
201
|
|
|
@@ -254,14 +258,12 @@ struct RetryingErrorTests {
|
|
|
254
258
|
maxRetries: 2, maxRetryDurationMs: nil, firstConnectMaxRetryDurationMs: nil,
|
|
255
259
|
)))
|
|
256
260
|
let stall = NSError(domain: "CoreMediaErrorDomain", code: -16830)
|
|
257
|
-
// One retry spent before the pause; the
|
|
261
|
+
// One retry spent before the pause; the play after it starts the count over.
|
|
258
262
|
eh.emit(.failed(stall, httpStatusCode: nil))
|
|
259
263
|
await c.retry.pendingTask?.value
|
|
260
|
-
#expect(c.retry.attempts == 1)
|
|
261
264
|
|
|
262
265
|
c.pause()
|
|
263
266
|
eh.emit(.failed(stall, httpStatusCode: nil))
|
|
264
|
-
#expect(c.retry.attempts == 0)
|
|
265
267
|
c.play()
|
|
266
268
|
eh.resolveAndLoadCalls.removeAll()
|
|
267
269
|
|
|
@@ -394,7 +396,7 @@ struct RetryingErrorTests {
|
|
|
394
396
|
@MainActor
|
|
395
397
|
private func makePlayingWithRetries() throws -> (Player, MockPlayerEngine, MockPlayerCallbacks) {
|
|
396
398
|
let engine = MockPlayerEngine()
|
|
397
|
-
let player = makePlayer(engine: engine, retry: makeRetryLoop(delay: 0.05)
|
|
399
|
+
let player = makePlayer(engine: engine, retry: makeRetryLoop(delay: 0.05))
|
|
398
400
|
let callbacks = MockPlayerCallbacks()
|
|
399
401
|
player.callbacks = callbacks
|
|
400
402
|
try startPlaying(player, engine)
|
|
@@ -963,69 +965,6 @@ struct NaturalEndPlayIntentTests {
|
|
|
963
965
|
}
|
|
964
966
|
}
|
|
965
967
|
|
|
966
|
-
// MARK: - Healthy playback refills the retry budget
|
|
967
|
-
|
|
968
|
-
/// Sustained playback proves the stream recovered, so the retry budget refills (as on
|
|
969
|
-
/// Android); otherwise a long-lived stream loses retry after its first recovered blip.
|
|
970
|
-
@Suite("Player - healthy playback resets retry budget")
|
|
971
|
-
struct HealthyPlaybackRetryResetTests {
|
|
972
|
-
/// A nonzero delay skips the retry, leaving the caller to start one that stays pending.
|
|
973
|
-
@MainActor
|
|
974
|
-
private func makeAfterOneRetry(delay: TimeInterval = 0) async -> (Player, RetryLoop, MockPlayerEngine) {
|
|
975
|
-
let retry = makeRetryLoop(delay: delay)
|
|
976
|
-
let engine = MockPlayerEngine()
|
|
977
|
-
let player = makePlayer(engine: engine, retry: retry)
|
|
978
|
-
if delay == 0 {
|
|
979
|
-
retry.handleError(URLError(.timedOut), context: .playback)
|
|
980
|
-
await retry.pendingTask?.value
|
|
981
|
-
}
|
|
982
|
-
return (player, retry, engine)
|
|
983
|
-
}
|
|
984
|
-
|
|
985
|
-
@Test @MainActor
|
|
986
|
-
func sustainedPlayback_resetsRetryBudget() async {
|
|
987
|
-
let (player, retry, _) = await makeAfterOneRetry()
|
|
988
|
-
#expect(retry.attempts == 1)
|
|
989
|
-
retry.healthyPlaybackDuration = .milliseconds(10)
|
|
990
|
-
|
|
991
|
-
player.transition(.avPlayerPlaying)
|
|
992
|
-
await retry.healthyPlaybackTask?.value
|
|
993
|
-
|
|
994
|
-
#expect(retry.attempts == 0)
|
|
995
|
-
}
|
|
996
|
-
|
|
997
|
-
/// reset() cancels a pending retry: a refill now would silently kill an in-flight reconnect.
|
|
998
|
-
@Test @MainActor
|
|
999
|
-
func refill_skipsWhileRetryIsPending() async {
|
|
1000
|
-
let (player, retry, _) = await makeAfterOneRetry(delay: 1)
|
|
1001
|
-
retry.healthyPlaybackDuration = .milliseconds(10)
|
|
1002
|
-
|
|
1003
|
-
player.transition(.avPlayerPlaying)
|
|
1004
|
-
retry.handleError(URLError(.timedOut), context: .playback)
|
|
1005
|
-
await retry.healthyPlaybackTask?.value
|
|
1006
|
-
|
|
1007
|
-
#expect(retry.pendingTask != nil)
|
|
1008
|
-
retry.cancel()
|
|
1009
|
-
}
|
|
1010
|
-
|
|
1011
|
-
@Test @MainActor
|
|
1012
|
-
func playbackInterruptedBeforeThreshold_doesNotReset() async {
|
|
1013
|
-
let (player, retry, engine) = await makeAfterOneRetry()
|
|
1014
|
-
engine.hasLoadedAsset = true
|
|
1015
|
-
retry.healthyPlaybackDuration = .milliseconds(500)
|
|
1016
|
-
|
|
1017
|
-
player.transition(.avPlayerPlaying)
|
|
1018
|
-
let refill = retry.healthyPlaybackTask
|
|
1019
|
-
player.transition(.avPlayerWaiting)
|
|
1020
|
-
|
|
1021
|
-
// Leaving .playing cancels the refill; awaiting the cancelled task reaches, deterministically,
|
|
1022
|
-
// the point where it would have fired.
|
|
1023
|
-
await refill?.value
|
|
1024
|
-
|
|
1025
|
-
#expect(retry.attempts == 1)
|
|
1026
|
-
}
|
|
1027
|
-
}
|
|
1028
|
-
|
|
1029
968
|
// MARK: - Sleep Timer Fade
|
|
1030
969
|
|
|
1031
970
|
@Suite("Player - sleep timer fade")
|
|
@@ -1373,27 +1312,13 @@ struct LoadTests {
|
|
|
1373
1312
|
let (c, eh, _, _) = makeCoordinator()
|
|
1374
1313
|
try c.setQueue([track("a"), track("b")])
|
|
1375
1314
|
let first = c.load
|
|
1376
|
-
|
|
1377
|
-
#expect(c.
|
|
1315
|
+
c.transition(.avPlayerPlaying)
|
|
1316
|
+
#expect(c.retry.hasPlayed)
|
|
1378
1317
|
|
|
1379
1318
|
c.next()
|
|
1380
1319
|
|
|
1381
1320
|
#expect(c.load != nil && c.load?.id != first?.id)
|
|
1382
|
-
#expect(c.
|
|
1383
|
-
}
|
|
1384
|
-
|
|
1385
|
-
/// The healthy-playback refill resets the retry budget mid-play; the load must survive it.
|
|
1386
|
-
@Test @MainActor
|
|
1387
|
-
func aRetryResetKeepsTheLoad() throws {
|
|
1388
|
-
let (c, eh, _, _) = makeCoordinator()
|
|
1389
|
-
try c.setQueue([track("a")])
|
|
1390
|
-
eh.emit(.audioDidStart)
|
|
1391
|
-
let load = c.load
|
|
1392
|
-
|
|
1393
|
-
c.retry.reset()
|
|
1394
|
-
|
|
1395
|
-
#expect(c.load == load)
|
|
1396
|
-
#expect(c.load?.hasPlayed == true)
|
|
1321
|
+
#expect(!c.retry.hasPlayed)
|
|
1397
1322
|
}
|
|
1398
1323
|
|
|
1399
1324
|
@Test
|
|
@@ -1462,6 +1387,7 @@ struct StopCancelsRetryTests {
|
|
|
1462
1387
|
let errorHandler = makeRetryLoop(delay: 0.05)
|
|
1463
1388
|
let engine = MockPlayerEngine()
|
|
1464
1389
|
let player = makePlayer(engine: engine, retry: errorHandler)
|
|
1390
|
+
errorHandler.playIntentChanged(true)
|
|
1465
1391
|
let callbacks = MockPlayerCallbacks()
|
|
1466
1392
|
player.callbacks = callbacks
|
|
1467
1393
|
|
|
@@ -1476,6 +1402,22 @@ struct StopCancelsRetryTests {
|
|
|
1476
1402
|
#expect(player.state == .stopped)
|
|
1477
1403
|
#expect(callbacks.errorEvents.isEmpty)
|
|
1478
1404
|
}
|
|
1405
|
+
|
|
1406
|
+
/// Guards where stop() cancels the retry, not the order of its render: lowering the intent with
|
|
1407
|
+
/// a retry pending would park it for a reload on play, and a stop is no pause.
|
|
1408
|
+
@Test @MainActor
|
|
1409
|
+
func stop_duringARetry_goesStraightToStopped() throws {
|
|
1410
|
+
let (c, eh, cb, _) = makeCoordinator()
|
|
1411
|
+
try startPlaying(c, eh)
|
|
1412
|
+
eh.emit(.failed(URLError(.timedOut), httpStatusCode: nil))
|
|
1413
|
+
#expect(c.retry.pendingTask != nil)
|
|
1414
|
+
cb.playbackChanges.removeAll()
|
|
1415
|
+
|
|
1416
|
+
c.stop()
|
|
1417
|
+
|
|
1418
|
+
#expect(cb.playbackChanges.map(\.state) == [.stopped])
|
|
1419
|
+
#expect(c.load?.reloadOnPlay == false)
|
|
1420
|
+
}
|
|
1479
1421
|
}
|
|
1480
1422
|
|
|
1481
1423
|
// MARK: - Audio session release (#60)
|
|
@@ -1637,6 +1579,22 @@ struct MediaStackEffectTests {
|
|
|
1637
1579
|
#expect(try cleared < #require(cb.publishOrder.firstIndex(of: "renderNowPlaying")))
|
|
1638
1580
|
}
|
|
1639
1581
|
|
|
1582
|
+
@Test @MainActor
|
|
1583
|
+
func stop_rendersTheStoppedStateWithThePlayIntentDown() throws {
|
|
1584
|
+
let (c, eh, _, _) = makeCoordinator()
|
|
1585
|
+
try startPlaying(c, eh)
|
|
1586
|
+
var renders: [(PlaybackState, Bool)] = []
|
|
1587
|
+
eh.onSeamCall = { [unowned c] in
|
|
1588
|
+
if $0 == "renderNowPlaying" { renders.append((c.state, c.playWhenReady)) }
|
|
1589
|
+
}
|
|
1590
|
+
|
|
1591
|
+
c.stop()
|
|
1592
|
+
|
|
1593
|
+
let stopped = renders.filter { $0.0 == .stopped }
|
|
1594
|
+
#expect(!stopped.isEmpty)
|
|
1595
|
+
#expect(stopped.allSatisfy { !$0.1 })
|
|
1596
|
+
}
|
|
1597
|
+
|
|
1640
1598
|
@Test @MainActor
|
|
1641
1599
|
func pause_keepsTheStreamTitle() throws {
|
|
1642
1600
|
let (c, eh, cb) = makeTraced()
|
|
@@ -4,65 +4,314 @@ 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
|
+
/// The watch gives the silent connect up at 30 s, a failure that starts the first-connect
|
|
120
|
+
/// budget; having delivered nothing, it is replaced 1 s later.
|
|
121
|
+
@Test func aConnectionOpenWithNoDataIsReplacedAt30s_andEndsAt42s() async {
|
|
122
|
+
let rig = Rig()
|
|
123
|
+
rig.play { _ in }
|
|
124
|
+
await rig.run(untilMs: 300_000)
|
|
125
|
+
#expect(rig.connects == [0, 31_000])
|
|
126
|
+
#expect(rig.errors.map(\.atMs) == [42_000])
|
|
127
|
+
#expect(rig.errors.map(\.error) == [.playbackStalled])
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
@Test func aDeadAddressThatNeverPlayedEnds12sAfterItsFirstFailure() async {
|
|
131
|
+
let rig = Rig()
|
|
132
|
+
rig.play(Rig.fails(afterMs: 50))
|
|
133
|
+
await rig.run(untilMs: 300_000)
|
|
134
|
+
#expect(rig.errors.map(\.atMs) == [12_050])
|
|
135
|
+
#expect(rig.errors.map(\.error) == [.hostUnreachable])
|
|
136
|
+
// Each wait grows from the failure before it: 1, 1.5, 2.25, 3.375 s.
|
|
137
|
+
#expect(rig.connects == [0, 1_050, 2_600, 4_900, 8_325])
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/// The deadline wins over a connect in flight: it gives up as it passes, and the connect's own
|
|
141
|
+
/// end, which the Player no longer hears, starts nothing.
|
|
142
|
+
@Test func theDeadlineGivesUpOnAConnectInFlight() async {
|
|
143
|
+
let rig = Rig()
|
|
144
|
+
rig.play(Rig.fails(afterMs: 5_000, with: .timedOut))
|
|
145
|
+
await rig.run(untilMs: 60_000)
|
|
146
|
+
#expect(rig.connects == [0, 6_000, 12_500])
|
|
147
|
+
#expect(rig.errors.map(\.atMs) == [17_000])
|
|
148
|
+
#expect(rig.errors.map(\.error) == [.hostUnreachable])
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
@Test func timeOfflineDuringAPlayedStreamsSilenceCounts() async {
|
|
152
|
+
let rig = Rig()
|
|
153
|
+
rig.play { rig in
|
|
154
|
+
rig.later(300) { $0.playbackBecameAudible() }
|
|
155
|
+
// As the Player reports it: the failure arrives while its state still reads .playing.
|
|
156
|
+
rig.later(60_300) { loop in
|
|
157
|
+
loop.onlineChanged(false)
|
|
158
|
+
loop.handleFailure(.playback)
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
await rig.run(untilMs: 400_000)
|
|
162
|
+
#expect(rig.connects == [0])
|
|
163
|
+
#expect(rig.errors.map(\.atMs) == [180_300])
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
@Test func theNetworkReturningReconnectsWithinHalfASecond() async {
|
|
167
|
+
let rig = Rig()
|
|
168
|
+
rig.play { rig in
|
|
169
|
+
rig.later(300) { $0.playbackBecameAudible() }
|
|
170
|
+
rig.later(60_300) { loop in
|
|
171
|
+
loop.onlineChanged(false)
|
|
172
|
+
loop.handleFailure(.playback)
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
rig.clock.at(90_000) { rig.loop.onlineChanged(true) }
|
|
176
|
+
await rig.run(untilMs: 91_000)
|
|
177
|
+
// The wait asks the pacer again every half second.
|
|
178
|
+
#expect(rig.connects.count == 2)
|
|
179
|
+
#expect((90_000 ... 90_500).contains(rig.connects.last ?? 0))
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
@Test func aRestartAfterTheTerminalErrorStartsFresh() async {
|
|
183
|
+
let rig = Rig()
|
|
184
|
+
rig.play(Rig.fails(afterMs: 50))
|
|
185
|
+
rig.clock.at(20_000) { rig.load() }
|
|
186
|
+
await rig.run(untilMs: 300_000)
|
|
187
|
+
#expect(rig.errors.map(\.atMs) == [12_050, 32_050])
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
@Test func aNewLoadStartsFresh() async {
|
|
191
|
+
let rig = Rig()
|
|
192
|
+
rig.play(Rig.fails(afterMs: 50))
|
|
193
|
+
rig.clock.at(8_000) { rig.load() }
|
|
194
|
+
await rig.run(untilMs: 300_000)
|
|
195
|
+
#expect(rig.errors.map(\.atMs) == [20_050])
|
|
32
196
|
}
|
|
33
197
|
|
|
34
|
-
@Test func
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
198
|
+
@Test func maxRetriesCountsConnectionsInARowThatDeliveredNothing() async {
|
|
199
|
+
let rig = Rig(maxRetries: 2)
|
|
200
|
+
rig.play(Rig.fails(afterMs: 50))
|
|
201
|
+
await rig.run(untilMs: 300_000)
|
|
202
|
+
#expect(rig.connects == [0, 1_050, 2_600])
|
|
203
|
+
#expect(rig.errors.map(\.atMs) == [2_650])
|
|
204
|
+
}
|
|
39
205
|
|
|
40
|
-
|
|
41
|
-
|
|
206
|
+
@Test func aPauseClearsTheDeadline() async {
|
|
207
|
+
let rig = Rig()
|
|
208
|
+
rig.play { _ in }
|
|
209
|
+
// Before the watch gives the connect up at 30 s.
|
|
210
|
+
rig.clock.at(20_000) { rig.loop.playIntentChanged(false) }
|
|
211
|
+
await rig.run(untilMs: 300_000)
|
|
212
|
+
#expect(rig.errors.isEmpty)
|
|
42
213
|
}
|
|
43
214
|
|
|
44
|
-
@Test func
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
215
|
+
@Test func aCancelClearsTheDeadline_untilTheNextConnect() async {
|
|
216
|
+
let rig = Rig()
|
|
217
|
+
rig.play { _ in }
|
|
218
|
+
rig.clock.at(20_000) { rig.loop.cancel() }
|
|
219
|
+
await rig.run(untilMs: 300_000)
|
|
220
|
+
#expect(rig.errors.isEmpty)
|
|
221
|
+
}
|
|
49
222
|
|
|
50
|
-
|
|
51
|
-
|
|
223
|
+
/// A media reset or `retryNow` reloads in place of a connection that played: its end starts the
|
|
224
|
+
/// count over, so the next connection may fail once and still reconnect.
|
|
225
|
+
@Test func aReloadThePlayerStartsEndsTheConnectionItReplaces() async {
|
|
226
|
+
let rig = Rig(maxRetries: 1)
|
|
227
|
+
var connection = 0
|
|
228
|
+
rig.play { rig in
|
|
229
|
+
connection += 1
|
|
230
|
+
if connection == 2 {
|
|
231
|
+
rig.later(300) { $0.playbackBecameAudible() }
|
|
232
|
+
} else {
|
|
233
|
+
rig.later(50) { $0.handleError(URLError(.cannotConnectToHost), context: .mediaLoad) }
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
rig.clock.at(60_000) {
|
|
237
|
+
rig.loop.playbackStoppedBeingAudible()
|
|
238
|
+
rig.connect()
|
|
239
|
+
}
|
|
240
|
+
await rig.run(untilMs: 61_060)
|
|
241
|
+
#expect(rig.connects == [0, 1_050, 60_000, 61_050])
|
|
242
|
+
#expect(rig.errors.isEmpty)
|
|
52
243
|
}
|
|
53
244
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
let
|
|
245
|
+
/// `retryNow` or a seek to the live edge during a reconnect cuts it off before it delivers
|
|
246
|
+
/// anything: that is the listener's, not the source's failure.
|
|
247
|
+
@Test func aReloadThatCutsOffAConnectDoesNotCountAgainstTheSource() async {
|
|
248
|
+
let rig = Rig(maxRetries: 2)
|
|
249
|
+
var connection = 0
|
|
250
|
+
rig.play { rig in
|
|
251
|
+
connection += 1
|
|
252
|
+
if !(2 ... 4).contains(connection) {
|
|
253
|
+
rig.later(50) { $0.handleError(URLError(.cannotConnectToHost), context: .mediaLoad) }
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
// The second connect hangs: three reloads replace it while it connects.
|
|
257
|
+
rig.clock.at(2_000) { rig.connect() }
|
|
258
|
+
rig.clock.at(2_100) { rig.connect() }
|
|
259
|
+
rig.clock.at(2_200) { rig.connect() }
|
|
260
|
+
await rig.run(untilMs: 3_760)
|
|
261
|
+
#expect(rig.connects == [0, 1_050, 2_000, 2_100, 2_200, 3_750])
|
|
262
|
+
#expect(rig.errors.isEmpty)
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/// The first-connect clock starts at the first failure seen online.
|
|
266
|
+
@Test func timeOfflineDoesNotBurnTheFirstConnectBudget() async {
|
|
267
|
+
let rig = Rig()
|
|
268
|
+
rig.loop.onlineChanged(false)
|
|
269
|
+
rig.play(Rig.fails(afterMs: 50))
|
|
270
|
+
rig.clock.at(60_000) { rig.loop.onlineChanged(true) }
|
|
271
|
+
await rig.run(untilMs: 300_000)
|
|
272
|
+
// Offline, the wait asks again every half second from the failure at 50 ms.
|
|
273
|
+
#expect(rig.connects.prefix(2) == [0, 60_050])
|
|
274
|
+
#expect(rig.errors.map(\.atMs) == [72_100])
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/// A silence that ends with no failure after audio played is a stall, not the failure before it.
|
|
278
|
+
@Test func theDeadlineReportsAStall_notAFailureAudioHasPlayedSince() async {
|
|
279
|
+
let rig = Rig()
|
|
280
|
+
var connection = 0
|
|
281
|
+
rig.play { rig in
|
|
282
|
+
connection += 1
|
|
283
|
+
if connection == 1 {
|
|
284
|
+
rig.later(50) { $0.handleError(nil, context: .mediaLoad, httpStatusCode: 503) }
|
|
285
|
+
} else {
|
|
286
|
+
rig.later(300) { $0.playbackBecameAudible() }
|
|
287
|
+
rig.later(60_000) { $0.playbackStoppedBeingAudible() }
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
await rig.run(untilMs: 300_000)
|
|
291
|
+
#expect(rig.connects == [0, 1_050])
|
|
292
|
+
#expect(rig.errors.map(\.atMs) == [181_050])
|
|
293
|
+
#expect(rig.errors.map(\.error) == [.playbackStalled])
|
|
294
|
+
}
|
|
58
295
|
|
|
59
|
-
|
|
60
|
-
|
|
296
|
+
@Test func aPlayWithNothingLoadedNeverGivesUp() async {
|
|
297
|
+
let rig = Rig()
|
|
298
|
+
rig.loop.playIntentChanged(true)
|
|
299
|
+
await rig.run(untilMs: 300_000)
|
|
300
|
+
#expect(rig.errors.isEmpty)
|
|
301
|
+
}
|
|
61
302
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
303
|
+
/// AVFoundation can report one failure through the item, its notification and the player.
|
|
304
|
+
@Test func aFailureReportedTwiceEndsItsConnectionOnce() async {
|
|
305
|
+
let rig = Rig(maxRetries: 2)
|
|
306
|
+
rig.play { rig in
|
|
307
|
+
rig.later(50) { loop in
|
|
308
|
+
loop.handleError(URLError(.cannotConnectToHost), context: .mediaLoad)
|
|
309
|
+
loop.handleError(URLError(.cannotConnectToHost), context: .playback)
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
await rig.run(untilMs: 300_000)
|
|
313
|
+
#expect(rig.connects == [0, 1_050, 2_600])
|
|
314
|
+
#expect(rig.errors.map(\.atMs) == [2_650])
|
|
66
315
|
}
|
|
67
316
|
}
|
|
68
317
|
|
|
@@ -115,7 +364,6 @@ struct RetryLoopHandleErrorTests {
|
|
|
115
364
|
#expect(record.errors.isEmpty)
|
|
116
365
|
// Session release waits on `pendingTask`, so a finished retry must clear it.
|
|
117
366
|
#expect(loop.pendingTask == nil)
|
|
118
|
-
#expect(loop.attempts == 2)
|
|
119
367
|
}
|
|
120
368
|
|
|
121
369
|
@Test func theAdvisoryPrecedesTheRetry() async {
|
|
@@ -173,20 +421,11 @@ struct RetryLoopHandleErrorTests {
|
|
|
173
421
|
#expect(record.errors == [.failedToLoadKeyValue])
|
|
174
422
|
}
|
|
175
423
|
|
|
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 {
|
|
424
|
+
@Test func aNewLoadDropsTheRetryInFlightSilently() async {
|
|
186
425
|
let (loop, record) = makeLoop(delay: 0.02)
|
|
187
426
|
loop.handleError(URLError(.timedOut), context: .playback)
|
|
188
427
|
let task = loop.pendingTask
|
|
189
|
-
loop.
|
|
428
|
+
loop.loadStarted()
|
|
190
429
|
#expect(loop.pendingTask == nil)
|
|
191
430
|
await task?.value
|
|
192
431
|
#expect(record.errors.isEmpty)
|
|
@@ -201,7 +440,7 @@ struct RetryLoopHandleErrorTests {
|
|
|
201
440
|
try await Task.sleep(for: .seconds(60))
|
|
202
441
|
})
|
|
203
442
|
loop.updatePolicy(from: .first(true))
|
|
204
|
-
loop.
|
|
443
|
+
loop.playIntentChanged(true)
|
|
205
444
|
let record = Record()
|
|
206
445
|
loop.onError = { record.errors.append($0) }
|
|
207
446
|
loop.onRetry = { record.reloads.append($0) }
|
|
@@ -229,7 +468,7 @@ struct RetryLoopHandleErrorTests {
|
|
|
229
468
|
})
|
|
230
469
|
holder.loop = loop
|
|
231
470
|
loop.updatePolicy(from: .first(true))
|
|
232
|
-
loop.
|
|
471
|
+
loop.playIntentChanged(true)
|
|
233
472
|
let record = Record()
|
|
234
473
|
loop.onError = { record.errors.append($0) }
|
|
235
474
|
loop.onRetry = { record.reloads.append($0) }
|
|
@@ -241,67 +480,7 @@ struct RetryLoopHandleErrorTests {
|
|
|
241
480
|
}
|
|
242
481
|
}
|
|
243
482
|
|
|
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
483
|
@MainActor
|
|
270
484
|
private final class LoopHolder {
|
|
271
485
|
weak var loop: RetryLoop?
|
|
272
486
|
}
|
|
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
|
-
}
|