make grace period configurable and reduce test duration

This commit is contained in:
Valere 2022-05-02 17:19:08 +02:00
parent d0bff5000c
commit f57e20c73c
2 changed files with 8 additions and 7 deletions

View file

@ -52,7 +52,7 @@ data class E2EMessageDetected(
}
}
class UISIDetector : LiveEventListener {
class UISIDetector(private val timeoutMillis: Long = 30_000L) : LiveEventListener {
interface UISIDetectorCallback {
val enabled: Boolean
@ -66,7 +66,6 @@ class UISIDetector : LiveEventListener {
private val trackedEvents = mutableMapOf<String, TimerTask>()
private val executor = Executors.newSingleThreadExecutor()
private val timer = Timer()
private val timeoutMillis = 30_000L
private val enabled: Boolean get() = callback?.enabled.orFalse()
override fun onEventDecrypted(event: Event, clearEvent: JsonDict) {

View file

@ -38,7 +38,8 @@ class UISIDetectorTest {
@Test
fun `trigger detection after grace period`() {
val uisiDetector = UISIDetector()
val gracePeriod = 5_000L
val uisiDetector = UISIDetector(gracePeriod)
var detectedEvent: E2EMessageDetected? = null
uisiDetector.callback = object : UISIDetector.UISIDetectorCallback {
@ -60,7 +61,7 @@ class UISIDetectorTest {
uisiDetector.onEventDecryptionError(event, fakeCryptoError())
runBlocking {
delay(40_000)
delay((gracePeriod * 1.2).toLong())
}
Assert.assertEquals(eventId, detectedEvent?.eventId)
}
@ -68,7 +69,8 @@ class UISIDetectorTest {
@Test
fun `If event decrypted during grace period should not trigger detection`() {
val scope = CoroutineScope(SupervisorJob())
val uisiDetector = UISIDetector()
val gracePeriod = 5_000L
val uisiDetector = UISIDetector(gracePeriod)
uisiDetector.callback = object : UISIDetector.UISIDetectorCallback {
override val enabled = true
@ -89,12 +91,12 @@ class UISIDetectorTest {
// the grace period is 30s
scope.launch(Dispatchers.Default) {
delay(10_000)
delay((gracePeriod * 0.5).toLong())
uisiDetector.onEventDecrypted(event, emptyMap())
}
runBlocking {
delay(60_000)
delay((gracePeriod * 1.2).toLong())
}
}