react-native-audio-browser 0.2.0-next.83 → 0.2.0-next.84
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/android/src/main/java/com/audiobrowser/player/InterceptingPlayer.kt +31 -9
- package/android/src/main/java/com/audiobrowser/player/MediaFactory.kt +39 -46
- package/android/src/main/java/com/audiobrowser/player/Player.kt +219 -54
- package/android/src/main/java/com/audiobrowser/player/PlayerEngineFactory.kt +5 -6
- package/android/src/main/java/com/audiobrowser/player/PlayerListener.kt +88 -73
- package/android/src/main/java/com/audiobrowser/player/ReconnectPacer.kt +8 -9
- package/android/src/main/java/com/audiobrowser/player/RetryFocus.kt +126 -0
- package/android/src/main/java/com/audiobrowser/player/RetryLoadErrorHandlingPolicy.kt +55 -121
- package/android/src/main/java/com/audiobrowser/player/RetryLocks.kt +52 -0
- package/android/src/main/java/com/audiobrowser/player/RetryLoop.kt +310 -0
- package/android/src/main/java/com/audiobrowser/player/RetryView.kt +31 -0
- package/android/src/main/java/com/audiobrowser/player/SessionViewListener.kt +148 -0
- package/android/src/main/java/com/audiobrowser/remote/RemoteOutputsController.kt +4 -1
- package/android/src/main/java/com/audiobrowser/remote/RemoteSwitchingPlayer.kt +3 -1
- package/android/src/main/java/com/audiobrowser/util/NetworkConnectivityMonitor.kt +4 -6
- package/android/src/test/java/androidx/media3/session/SessionViews.kt +9 -0
- package/android/src/test/java/com/audiobrowser/NetworkFixtures.kt +25 -0
- package/android/src/test/java/com/audiobrowser/player/LiveStreamEndTest.kt +15 -53
- package/android/src/test/java/com/audiobrowser/player/PlayerRetryTest.kt +190 -53
- package/android/src/test/java/com/audiobrowser/player/RetryFocusTest.kt +97 -0
- package/android/src/test/java/com/audiobrowser/player/RetryLoadErrorHandlingPolicyTest.kt +90 -152
- package/android/src/test/java/com/audiobrowser/player/RetryLocksTest.kt +48 -0
- package/android/src/test/java/com/audiobrowser/player/RetryLoopCaseTest.kt +181 -0
- package/android/src/test/java/com/audiobrowser/player/RetryLoopTest.kt +221 -0
- package/android/src/test/java/com/audiobrowser/player/RetryMaskingTest.kt +176 -0
- package/android/src/test/java/com/audiobrowser/player/SessionPlayTest.kt +2 -0
- package/android/src/test/java/com/audiobrowser/player/SessionViewListenerTest.kt +21 -0
- package/android/src/test/java/com/audiobrowser/player/SessionViewTest.kt +2 -0
- package/android/src/test/java/com/audiobrowser/util/NetworkConnectivityMonitorTest.kt +13 -4
- package/lib/commonjs/features/player/setup.js +5 -4
- package/lib/commonjs/features/player/setup.js.map +1 -1
- package/lib/module/features/player/setup.js +5 -4
- package/lib/module/features/player/setup.js.map +1 -1
- package/lib/typescript/src/features/player/setup.d.ts +19 -13
- package/lib/typescript/src/features/player/setup.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/features/player/setup.ts +19 -13
- package/website/guide/errors.md +42 -38
- package/website/guide/network.md +4 -3
- package/android/src/main/java/com/audiobrowser/player/StuckRecoveryPolicy.kt +0 -37
- package/android/src/test/java/com/audiobrowser/player/StuckRecoveryPolicyTest.kt +0 -40
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
package com.audiobrowser.player
|
|
2
2
|
|
|
3
|
+
import android.net.Uri
|
|
3
4
|
import androidx.media3.common.C
|
|
4
5
|
import androidx.media3.common.ParserException
|
|
5
6
|
import androidx.media3.datasource.DataSourceException
|
|
@@ -9,32 +10,48 @@ import androidx.media3.exoplayer.source.LoadEventInfo
|
|
|
9
10
|
import androidx.media3.exoplayer.source.MediaLoadData
|
|
10
11
|
import androidx.media3.exoplayer.upstream.LoadErrorHandlingPolicy
|
|
11
12
|
import androidx.media3.exoplayer.upstream.Loader
|
|
13
|
+
import com.audiobrowser.player.RetryLoadErrorHandlingPolicy.Sources
|
|
12
14
|
import java.io.FileNotFoundException
|
|
13
15
|
import java.io.IOException
|
|
14
16
|
import java.net.SocketTimeoutException
|
|
15
17
|
import java.net.UnknownHostException
|
|
16
|
-
import java.time.Duration
|
|
17
18
|
import org.junit.Assert.assertEquals
|
|
18
|
-
import org.junit.Assert.assertFalse
|
|
19
|
-
import org.junit.Assert.assertNotEquals
|
|
20
19
|
import org.junit.Assert.assertTrue
|
|
21
20
|
import org.junit.Test
|
|
22
21
|
import org.junit.runner.RunWith
|
|
23
22
|
import org.mockito.Mockito.mock
|
|
24
23
|
import org.robolectric.RobolectricTestRunner
|
|
25
|
-
import org.robolectric.shadows.ShadowSystemClock
|
|
26
24
|
|
|
27
25
|
@RunWith(RobolectricTestRunner::class)
|
|
28
26
|
class RetryLoadErrorHandlingPolicyTest {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
private
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
27
|
+
private val failures = mutableListOf<Boolean>()
|
|
28
|
+
private var current = true
|
|
29
|
+
|
|
30
|
+
private fun policy(sources: Sources) =
|
|
31
|
+
RetryLoadErrorHandlingPolicy(sources, { current }) { _, inPlace -> failures += inPlace }
|
|
32
|
+
|
|
33
|
+
private fun errorInfo(
|
|
34
|
+
exception: IOException = UnknownHostException("dead host"),
|
|
35
|
+
errorCount: Int = 1,
|
|
36
|
+
headers: Map<String, List<String>> = emptyMap(),
|
|
37
|
+
loadTaskId: Long = 1,
|
|
38
|
+
) =
|
|
39
|
+
LoadErrorHandlingPolicy.LoadErrorInfo(
|
|
40
|
+
LoadEventInfo(
|
|
41
|
+
loadTaskId,
|
|
42
|
+
DataSpec(Uri.parse("https://example.com/stream")),
|
|
43
|
+
Uri.parse("https://example.com/stream"),
|
|
44
|
+
headers,
|
|
45
|
+
/* elapsedRealtimeMs= */ 0,
|
|
46
|
+
/* loadDurationMs= */ 0,
|
|
47
|
+
/* bytesLoaded= */ 0,
|
|
48
|
+
),
|
|
49
|
+
MediaLoadData(C.DATA_TYPE_MEDIA),
|
|
50
|
+
exception,
|
|
51
|
+
errorCount,
|
|
52
|
+
)
|
|
36
53
|
|
|
37
|
-
private fun httpError(responseCode: Int)
|
|
54
|
+
private fun httpError(responseCode: Int) =
|
|
38
55
|
HttpDataSource.InvalidResponseCodeException(
|
|
39
56
|
responseCode,
|
|
40
57
|
"response message",
|
|
@@ -44,180 +61,101 @@ class RetryLoadErrorHandlingPolicyTest {
|
|
|
44
61
|
ByteArray(0),
|
|
45
62
|
)
|
|
46
63
|
|
|
47
|
-
private fun assertNonRecoverable(exception: IOException) {
|
|
48
|
-
var retryPending = false
|
|
49
|
-
val policy = RetryLoadErrorHandlingPolicy(onRetryPending = { retryPending = true })
|
|
50
|
-
val delay = policy.getRetryDelayMsFor(errorInfo(exception))
|
|
51
|
-
assertEquals(C.TIME_UNSET, delay)
|
|
52
|
-
assertFalse(retryPending)
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
@Test
|
|
56
|
-
fun `offline network IOException is retried and reports the pending retry`() {
|
|
57
|
-
var retryPending = false
|
|
58
|
-
val policy =
|
|
59
|
-
RetryLoadErrorHandlingPolicy(
|
|
60
|
-
maxRetries = null,
|
|
61
|
-
isOnline = { false },
|
|
62
|
-
onRetryPending = { retryPending = true },
|
|
63
|
-
)
|
|
64
|
-
|
|
65
|
-
val delay = policy.getRetryDelayMsFor(errorInfo(UnknownHostException("Unable to resolve host")))
|
|
66
|
-
|
|
67
|
-
assertNotEquals(C.TIME_UNSET, delay)
|
|
68
|
-
// The offline delay.
|
|
69
|
-
assertEquals(1000L, delay)
|
|
70
|
-
assertTrue(retryPending)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
64
|
@Test
|
|
74
|
-
fun `
|
|
75
|
-
|
|
76
|
-
val policy =
|
|
77
|
-
RetryLoadErrorHandlingPolicy(isOnline = { true }, onRetryPending = { retryPending = true })
|
|
65
|
+
fun `an adaptive stream retries in place with the backoff, without end`() {
|
|
66
|
+
val policy = policy(Sources.ADAPTIVE)
|
|
78
67
|
|
|
79
|
-
val
|
|
68
|
+
val delays = (1..8).map { policy.getRetryDelayMsFor(errorInfo(errorCount = it)) }
|
|
80
69
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
assertTrue(retryPending)
|
|
70
|
+
assertEquals(listOf(1000L, 1500, 2250, 3375, 5000, 5000, 5000, 5000), delays)
|
|
71
|
+
assertEquals(List(8) { true }, failures)
|
|
84
72
|
}
|
|
85
73
|
|
|
86
74
|
@Test
|
|
87
|
-
fun `
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
policy.getRetryDelayMsFor(errorInfo(UnknownHostException("offline"), errorCount = 4))
|
|
91
|
-
assertEquals(C.TIME_UNSET, delay)
|
|
75
|
+
fun `a live stream's failure ends the connection for the loop`() {
|
|
76
|
+
assertEquals(C.TIME_UNSET, policy(Sources.LIVE).getRetryDelayMsFor(errorInfo()))
|
|
77
|
+
assertEquals(listOf(false), failures)
|
|
92
78
|
}
|
|
93
79
|
|
|
94
80
|
@Test
|
|
95
|
-
fun `
|
|
96
|
-
|
|
97
|
-
val policy =
|
|
98
|
-
RetryLoadErrorHandlingPolicy(isOnline = { true }, onRetryPending = { retryPending = true })
|
|
81
|
+
fun `a progressive response of known length retries in place`() {
|
|
82
|
+
val policy = policy(Sources.PROGRESSIVE)
|
|
99
83
|
|
|
100
|
-
val
|
|
84
|
+
val length = errorInfo(headers = mapOf("Content-Length" to listOf("1234")))
|
|
85
|
+
val range = errorInfo(headers = mapOf("content-range" to listOf("bytes 0-99/1234")))
|
|
101
86
|
|
|
102
|
-
|
|
103
|
-
|
|
87
|
+
assertEquals(1000L, policy.getRetryDelayMsFor(length))
|
|
88
|
+
assertEquals(1000L, policy.getRetryDelayMsFor(range))
|
|
89
|
+
assertEquals(listOf(true, true), failures)
|
|
104
90
|
}
|
|
105
91
|
|
|
106
92
|
@Test
|
|
107
|
-
fun `
|
|
108
|
-
|
|
109
|
-
val policy = RetryLoadErrorHandlingPolicy(onRetryPending = { retryPending = true })
|
|
93
|
+
fun `a file of known length stays in place when a reopen fails without headers`() {
|
|
94
|
+
val policy = policy(Sources.PROGRESSIVE)
|
|
110
95
|
|
|
111
|
-
|
|
96
|
+
policy.getRetryDelayMsFor(errorInfo(headers = mapOf("Content-Length" to listOf("1234"))))
|
|
97
|
+
val reopen = policy.getRetryDelayMsFor(errorInfo(errorCount = 2))
|
|
98
|
+
val another = policy.getRetryDelayMsFor(errorInfo(loadTaskId = 2))
|
|
112
99
|
|
|
113
|
-
assertEquals(
|
|
114
|
-
|
|
100
|
+
assertEquals(1500L, reopen)
|
|
101
|
+
assertEquals(C.TIME_UNSET, another)
|
|
102
|
+
assertEquals(listOf(true, true, false), failures)
|
|
115
103
|
}
|
|
116
104
|
|
|
117
105
|
@Test
|
|
118
|
-
fun `
|
|
119
|
-
|
|
120
|
-
}
|
|
106
|
+
fun `an item that only preloads retries in place and reports nothing`() {
|
|
107
|
+
current = false
|
|
121
108
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
assertNonRecoverable(FileNotFoundException("missing"))
|
|
109
|
+
assertEquals(1000L, policy(Sources.LIVE).getRetryDelayMsFor(errorInfo()))
|
|
110
|
+
assertTrue(failures.isEmpty())
|
|
125
111
|
}
|
|
126
112
|
|
|
127
113
|
@Test
|
|
128
|
-
fun `
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
@Test
|
|
133
|
-
@Suppress("DEPRECATION") // DataSourceException(reason) is the only way to build this in a test
|
|
134
|
-
fun `reads past the available position are non-recoverable`() {
|
|
135
|
-
assertNonRecoverable(DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE))
|
|
136
|
-
}
|
|
137
|
-
}
|
|
114
|
+
fun `a progressive response of unknown length ends the connection`() {
|
|
115
|
+
val policy = policy(Sources.PROGRESSIVE)
|
|
138
116
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
private val loadEventInfo = org.mockito.Mockito.mock(LoadEventInfo::class.java)
|
|
142
|
-
private val mediaLoadData = MediaLoadData(C.DATA_TYPE_MEDIA)
|
|
117
|
+
val none = errorInfo(SocketTimeoutException("timeout"))
|
|
118
|
+
val unknownTotal = errorInfo(headers = mapOf("Content-Range" to listOf("bytes 0-99/*")))
|
|
143
119
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
mediaLoadData,
|
|
148
|
-
UnknownHostException("dead host"),
|
|
149
|
-
errorCount,
|
|
150
|
-
)
|
|
151
|
-
|
|
152
|
-
@Test
|
|
153
|
-
fun `never-played load gives up after the first-connect budget`() {
|
|
154
|
-
val policy =
|
|
155
|
-
RetryLoadErrorHandlingPolicy(firstConnectMaxRetryDurationMs = 100, hasPlayed = { false })
|
|
156
|
-
|
|
157
|
-
assertNotEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo()))
|
|
158
|
-
ShadowSystemClock.advanceBy(Duration.ofMillis(150))
|
|
159
|
-
assertEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo(errorCount = 2)))
|
|
120
|
+
assertEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(none))
|
|
121
|
+
assertEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(unknownTotal))
|
|
122
|
+
assertEquals(listOf(false, false), failures)
|
|
160
123
|
}
|
|
161
124
|
|
|
162
125
|
@Test
|
|
163
|
-
fun `
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
assertNotEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo()))
|
|
168
|
-
ShadowSystemClock.advanceBy(Duration.ofMillis(150))
|
|
169
|
-
assertNotEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo(errorCount = 2)))
|
|
126
|
+
fun `a retryable HTTP status (503) is recoverable`() {
|
|
127
|
+
assertEquals(1000L, policy(Sources.ADAPTIVE).getRetryDelayMsFor(errorInfo(httpError(503))))
|
|
128
|
+
assertEquals(listOf(true), failures)
|
|
170
129
|
}
|
|
171
130
|
|
|
172
131
|
@Test
|
|
173
|
-
fun `
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
isOnline = { online },
|
|
179
|
-
hasPlayed = { false },
|
|
180
|
-
)
|
|
181
|
-
|
|
182
|
-
assertNotEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo()))
|
|
183
|
-
ShadowSystemClock.advanceBy(Duration.ofMillis(150))
|
|
184
|
-
assertNotEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo(errorCount = 2)))
|
|
185
|
-
|
|
186
|
-
online = true
|
|
187
|
-
assertNotEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo(errorCount = 3)))
|
|
188
|
-
ShadowSystemClock.advanceBy(Duration.ofMillis(150))
|
|
189
|
-
assertEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo(errorCount = 4)))
|
|
132
|
+
fun `media3 never gives up on a loadable itself`() {
|
|
133
|
+
assertEquals(
|
|
134
|
+
Int.MAX_VALUE,
|
|
135
|
+
policy(Sources.ADAPTIVE).getMinimumLoadableRetryCount(C.DATA_TYPE_MEDIA),
|
|
136
|
+
)
|
|
190
137
|
}
|
|
191
138
|
|
|
192
139
|
@Test
|
|
193
|
-
fun `
|
|
194
|
-
|
|
195
|
-
val
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
140
|
+
fun `fatal errors surface and reach no one`() {
|
|
141
|
+
@Suppress("DEPRECATION") // DataSourceException(reason) is the only way to build this in a test
|
|
142
|
+
val fatal =
|
|
143
|
+
listOf(
|
|
144
|
+
httpError(404),
|
|
145
|
+
ParserException.createForMalformedContainer("bad container", null),
|
|
146
|
+
FileNotFoundException("missing"),
|
|
147
|
+
Loader.UnexpectedLoaderException(IllegalStateException("boom")),
|
|
148
|
+
DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE),
|
|
200
149
|
)
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
@Test
|
|
214
|
-
fun `reset restarts the first-connect clock`() {
|
|
215
|
-
val policy =
|
|
216
|
-
RetryLoadErrorHandlingPolicy(firstConnectMaxRetryDurationMs = 100, hasPlayed = { false })
|
|
217
|
-
|
|
218
|
-
assertNotEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo()))
|
|
219
|
-
ShadowSystemClock.advanceBy(Duration.ofMillis(150))
|
|
220
|
-
policy.reset()
|
|
221
|
-
assertNotEquals(C.TIME_UNSET, policy.getRetryDelayMsFor(errorInfo()))
|
|
150
|
+
for (sources in Sources.entries) {
|
|
151
|
+
for (exception in fatal) {
|
|
152
|
+
assertEquals(
|
|
153
|
+
"$sources: $exception",
|
|
154
|
+
C.TIME_UNSET,
|
|
155
|
+
policy(sources).getRetryDelayMsFor(errorInfo(exception)),
|
|
156
|
+
)
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
assertTrue(failures.isEmpty())
|
|
222
160
|
}
|
|
223
161
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
package com.audiobrowser.player
|
|
2
|
+
|
|
3
|
+
import android.os.Handler
|
|
4
|
+
import android.os.Looper
|
|
5
|
+
import com.margelo.nitro.audiobrowser.AndroidPlayerWakeMode
|
|
6
|
+
import java.time.Duration
|
|
7
|
+
import org.junit.Assert.assertFalse
|
|
8
|
+
import org.junit.Assert.assertTrue
|
|
9
|
+
import org.junit.Test
|
|
10
|
+
import org.junit.runner.RunWith
|
|
11
|
+
import org.robolectric.RobolectricTestRunner
|
|
12
|
+
import org.robolectric.RuntimeEnvironment
|
|
13
|
+
import org.robolectric.Shadows.shadowOf
|
|
14
|
+
import org.robolectric.annotation.Config
|
|
15
|
+
|
|
16
|
+
@RunWith(RobolectricTestRunner::class)
|
|
17
|
+
@Config(sdk = [28])
|
|
18
|
+
class RetryLocksTest {
|
|
19
|
+
private val locks =
|
|
20
|
+
RetryLocks(
|
|
21
|
+
RuntimeEnvironment.getApplication(),
|
|
22
|
+
AndroidPlayerWakeMode.NETWORK,
|
|
23
|
+
maxHoldMs = 1_000,
|
|
24
|
+
Handler(Looper.getMainLooper()),
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
@Test
|
|
28
|
+
fun `the Wi-Fi lock is let go past the backstop if nothing releases it`() {
|
|
29
|
+
locks.hold(true)
|
|
30
|
+
assertTrue(locks.wifiLockHeld)
|
|
31
|
+
|
|
32
|
+
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(1_000))
|
|
33
|
+
|
|
34
|
+
assertFalse(locks.wifiLockHeld)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
@Test
|
|
38
|
+
fun `a hold that starts again moves the backstop`() {
|
|
39
|
+
locks.hold(true)
|
|
40
|
+
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(600))
|
|
41
|
+
locks.hold(false)
|
|
42
|
+
locks.hold(true)
|
|
43
|
+
|
|
44
|
+
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(600))
|
|
45
|
+
|
|
46
|
+
assertTrue(locks.wifiLockHeld)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
package com.audiobrowser.player
|
|
2
|
+
|
|
3
|
+
import androidx.media3.common.PlaybackException
|
|
4
|
+
import kotlinx.serialization.json.JsonNull
|
|
5
|
+
import kotlinx.serialization.json.JsonObject
|
|
6
|
+
import kotlinx.serialization.json.JsonPrimitive
|
|
7
|
+
import kotlinx.serialization.json.jsonArray
|
|
8
|
+
import kotlinx.serialization.json.jsonPrimitive
|
|
9
|
+
import kotlinx.serialization.json.longOrNull
|
|
10
|
+
import org.junit.Assert.assertEquals
|
|
11
|
+
import org.junit.Assert.assertFalse
|
|
12
|
+
import org.junit.Assert.assertTrue
|
|
13
|
+
import org.junit.Test
|
|
14
|
+
import org.junit.runner.RunWith
|
|
15
|
+
import org.junit.runners.Parameterized
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Runs every case in spec/retry-cases.json through [RetryLoop], with a fake clock, timer and
|
|
19
|
+
* engine: each connection's start is the loop's own reconnect, checked against the case's
|
|
20
|
+
* `startedAt`.
|
|
21
|
+
*/
|
|
22
|
+
@RunWith(Parameterized::class)
|
|
23
|
+
class RetryLoopCaseTest(private val name: String, private val case: JsonObject) {
|
|
24
|
+
companion object {
|
|
25
|
+
@JvmStatic @Parameterized.Parameters(name = "{0}") fun cases() = RetryCaseTest.cases()
|
|
26
|
+
|
|
27
|
+
private fun JsonPrimitive.long(what: String): Long =
|
|
28
|
+
takeUnless { it.isString }?.longOrNull ?: error("$what is not an integer")
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private var now = 0L
|
|
32
|
+
private val tasks = mutableListOf<Pair<Long, () -> Unit>>()
|
|
33
|
+
private var lastStartAt: Long? = null
|
|
34
|
+
private var audioSinceStart = false
|
|
35
|
+
private var audible = false
|
|
36
|
+
private var engineIdle = false
|
|
37
|
+
private var gaveUp = false
|
|
38
|
+
private var intentUp = false
|
|
39
|
+
|
|
40
|
+
/** A failure since audio last played, which Player shows as a retrying error. */
|
|
41
|
+
private var retrying = false
|
|
42
|
+
|
|
43
|
+
private val loop by lazy {
|
|
44
|
+
RetryLoop(
|
|
45
|
+
clock = { now },
|
|
46
|
+
timer = { delayMs, action ->
|
|
47
|
+
val task = (now + delayMs) to action
|
|
48
|
+
tasks += task
|
|
49
|
+
val cancel: () -> Unit = { tasks.remove(task) }
|
|
50
|
+
cancel
|
|
51
|
+
},
|
|
52
|
+
engine =
|
|
53
|
+
object : RetryLoop.Engine {
|
|
54
|
+
override fun reconnect() = start()
|
|
55
|
+
|
|
56
|
+
override fun awaitNetwork() {}
|
|
57
|
+
|
|
58
|
+
override fun holdChanged(held: Boolean, kept: Boolean) {}
|
|
59
|
+
|
|
60
|
+
override fun giveUp(cause: PlaybackException?) {
|
|
61
|
+
gaveUp = true
|
|
62
|
+
engineIdle = true
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
maxRetries = case["maxRetries"]?.jsonPrimitive?.long("maxRetries")?.toInt(),
|
|
66
|
+
recoveryMs = case["recoveryMs"]?.jsonPrimitive?.long("recoveryMs") ?: 120_000,
|
|
67
|
+
firstConnectMs = case["firstConnectMs"]?.jsonPrimitive?.long("firstConnectMs") ?: 12_000,
|
|
68
|
+
online = true,
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private fun start() {
|
|
73
|
+
engineIdle = false
|
|
74
|
+
lastStartAt = now
|
|
75
|
+
audioSinceStart = audible
|
|
76
|
+
loop.connectionStarted()
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Runs the timer's tasks due by [until], each at its own time. */
|
|
80
|
+
private fun runUntil(until: Long) {
|
|
81
|
+
while (true) {
|
|
82
|
+
val next = tasks.filter { it.first <= until }.minByOrNull { it.first } ?: break
|
|
83
|
+
tasks.remove(next)
|
|
84
|
+
now = maxOf(now, next.first)
|
|
85
|
+
next.second()
|
|
86
|
+
}
|
|
87
|
+
now = until
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private fun expectedDeadline(step: JsonObject): Long? =
|
|
91
|
+
when (val deadline = step.getValue("deadline")) {
|
|
92
|
+
is JsonNull -> null
|
|
93
|
+
else -> (deadline as JsonPrimitive).long("deadline")
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
@Test
|
|
97
|
+
fun runs() {
|
|
98
|
+
for (element in case.getValue("steps").jsonArray) {
|
|
99
|
+
val step = element as JsonObject
|
|
100
|
+
val at = step.getValue("at").jsonPrimitive.long("at")
|
|
101
|
+
runUntil(at)
|
|
102
|
+
val event = step.getValue("event").jsonPrimitive.content
|
|
103
|
+
when (event) {
|
|
104
|
+
"load" -> {
|
|
105
|
+
gaveUp = false
|
|
106
|
+
engineIdle = false
|
|
107
|
+
lastStartAt = at
|
|
108
|
+
audible = false
|
|
109
|
+
audioSinceStart = false
|
|
110
|
+
loop.loadStarted()
|
|
111
|
+
}
|
|
112
|
+
"intentUp",
|
|
113
|
+
"intentDown" -> {
|
|
114
|
+
val up = event == "intentUp"
|
|
115
|
+
val changed = up != intentUp
|
|
116
|
+
intentUp = up
|
|
117
|
+
loop.playIntentChanged(up)
|
|
118
|
+
// As Player does: a pause over a retrying error stops the engine, and a play prepares an
|
|
119
|
+
// idle one.
|
|
120
|
+
if (!up && retrying) engineIdle = true
|
|
121
|
+
if (up && changed && engineIdle && !gaveUp) start()
|
|
122
|
+
}
|
|
123
|
+
"audioStarts",
|
|
124
|
+
"audioStops" -> {
|
|
125
|
+
audible = event == "audioStarts"
|
|
126
|
+
if (audible) {
|
|
127
|
+
audioSinceStart = true
|
|
128
|
+
retrying = false
|
|
129
|
+
}
|
|
130
|
+
loop.isPlayingChanged(audible)
|
|
131
|
+
}
|
|
132
|
+
"offline",
|
|
133
|
+
"online" -> loop.onlineChanged(event == "online")
|
|
134
|
+
"ended" -> {
|
|
135
|
+
retrying = true
|
|
136
|
+
// A deadline already passed gave up when it did: there is no connection to end.
|
|
137
|
+
if (gaveUp && expectedDeadline(step)?.let { it <= at } == true) continue
|
|
138
|
+
assertEquals(
|
|
139
|
+
"at $at: the connection that ended started then",
|
|
140
|
+
step.getValue("startedAt").jsonPrimitive.long("startedAt"),
|
|
141
|
+
lastStartAt,
|
|
142
|
+
)
|
|
143
|
+
assertEquals(
|
|
144
|
+
"at $at: what the connection delivered",
|
|
145
|
+
step.getValue("delivered").jsonPrimitive.content == "audio",
|
|
146
|
+
audioSinceStart,
|
|
147
|
+
)
|
|
148
|
+
loop.connectionFailed(inPlace = false, atMs = at)
|
|
149
|
+
engineIdle = true
|
|
150
|
+
assertTrue("at $at: the loop takes the error", loop.onPlayerError(null, stuck = false))
|
|
151
|
+
}
|
|
152
|
+
else -> error("at $at: unknown event $event")
|
|
153
|
+
}
|
|
154
|
+
val deadline = expectedDeadline(step)
|
|
155
|
+
assertEquals("deadline at $at ($event)", deadline, loop.deadlineMs)
|
|
156
|
+
step["wait"]?.let { wait ->
|
|
157
|
+
wait as JsonPrimitive
|
|
158
|
+
when {
|
|
159
|
+
wait.isString && wait.content == "giveUp" -> assertTrue("gave up at $at", gaveUp)
|
|
160
|
+
// A deadline in the past gives up at once, whatever the pacer would wait.
|
|
161
|
+
deadline != null && deadline <= at -> {
|
|
162
|
+
runUntil(at)
|
|
163
|
+
assertTrue("gave up at $at, past the deadline", gaveUp)
|
|
164
|
+
}
|
|
165
|
+
wait.isString && wait.content == "online" -> {
|
|
166
|
+
assertFalse("at $at: gave up", gaveUp)
|
|
167
|
+
assertEquals("at $at: no reconnect is scheduled", null, loop.reconnectDueMs)
|
|
168
|
+
// On both branches: with a connection still open, the wait applies once it ends.
|
|
169
|
+
if (engineIdle) assertTrue("at $at: waits for the network", loop.reconnecting)
|
|
170
|
+
}
|
|
171
|
+
else -> {
|
|
172
|
+
assertFalse("at $at: gave up", gaveUp)
|
|
173
|
+
if (engineIdle)
|
|
174
|
+
assertEquals("reconnect at $at", at + wait.long("wait"), loop.reconnectDueMs)
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
runUntil(at)
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|