feat: exponential backoff policy for webview reload
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
</manifest>
|
||||
@@ -0,0 +1,19 @@
|
||||
package cz.c3c.webviewkiosk
|
||||
|
||||
class BackoffPolicy(
|
||||
private val initialDelayMs: Long,
|
||||
private val maxDelayMs: Long,
|
||||
private val factor: Double = 2.0,
|
||||
) {
|
||||
private var nextMs = initialDelayMs
|
||||
|
||||
fun nextDelayMs(): Long {
|
||||
val current = nextMs
|
||||
nextMs = (nextMs * factor).toLong().coerceAtMost(maxDelayMs)
|
||||
return current
|
||||
}
|
||||
|
||||
fun reset() {
|
||||
nextMs = initialDelayMs
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package cz.c3c.webviewkiosk
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
class BackoffPolicyTest {
|
||||
|
||||
@Test
|
||||
fun firstDelayIsInitial() {
|
||||
val b = BackoffPolicy(initialDelayMs = 2_000, maxDelayMs = 60_000)
|
||||
assertEquals(2_000, b.nextDelayMs())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun delayDoublesOnEachCall() {
|
||||
val b = BackoffPolicy(initialDelayMs = 2_000, maxDelayMs = 60_000)
|
||||
b.nextDelayMs()
|
||||
assertEquals(4_000, b.nextDelayMs())
|
||||
assertEquals(8_000, b.nextDelayMs())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun delayCapsAtMax() {
|
||||
val b = BackoffPolicy(initialDelayMs = 2_000, maxDelayMs = 5_000)
|
||||
b.nextDelayMs() // 2000
|
||||
b.nextDelayMs() // 4000
|
||||
assertEquals(5_000, b.nextDelayMs())
|
||||
assertEquals(5_000, b.nextDelayMs())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun resetReturnsToInitial() {
|
||||
val b = BackoffPolicy(initialDelayMs = 2_000, maxDelayMs = 60_000)
|
||||
b.nextDelayMs()
|
||||
b.nextDelayMs()
|
||||
b.reset()
|
||||
assertEquals(2_000, b.nextDelayMs())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user