Record bounded call audio traces and report event windows off playback thread

This commit is contained in:
evgeny committed 2026-10-15 02:47:33 +02:00
1 parent 6fb3fb732a
commit 7cf4755637
3 files changed
+246 -12

No files matched your search

@@ -0,0 +1,64 @@
package com.utun.chat.data
import org.junit.Assert.*
import org.junit.Test
class CallAudioTraceTest {
private fun sample(t: Long, queueMs: Long = 40, route: Long = 1): LongArray = LongArray(CallAudioTrace.WIDTH).also {
it[0] = t; it[1] = route; it[2] = 2; it[3] = t * 48 / 1000 + queueMs * 48; it[4] = t * 48 / 1000
it[18] = 60; it[20] = 960; it[21] = 100
}
@Test fun fixedRingCopiesSamplesAndReportsOverflowWithoutLoggingFromProducer() {
val logs = mutableListOf<String>()
val trace = CallAudioTrace(1) { _, text -> logs.add(text) }.apply { enabled = true }
val row = sample(0)
repeat(600) { row[0] = it * 20_000L; trace.record(row) }
assertTrue(logs.isEmpty())
row[0] = -1
trace.flush(true)
assertTrue(logs.any { it.contains("lost=88") })
assertTrue(logs.any { it.contains("triggerUs=1760000") })
}
@Test fun sustainedHighQueueProducesOneEventAndRecoveryRearmsIt() {
val logs = mutableListOf<String>()
val trace = CallAudioTrace(2) { _, text -> logs.add(text) }.apply { enabled = true }
repeat(1500) {
trace.record(sample(it * 20_000L, if (it in 700..899) 40 else 140))
if (it % 10 == 0) trace.flush()
}
trace.flush(true)
assertEquals(2, logs.count { it.contains(" fault=4 queueUs=") })
assertEquals(1, logs.count { it.contains(" recovered ") })
assertEquals(2, logs.count { it.contains("triggerUs=") })
assertTrue(logs.count { it.contains("audio summary") } in 28..31)
}
@Test fun routeEventIncludesHistoryAndPostWindowAndDoesNotRetriggerEveryFrame() {
val logs = mutableListOf<String>()
val trace = CallAudioTrace(3) { _, text -> logs.add(text) }.apply { enabled = true }
repeat(1000) {
trace.record(sample(it * 20_000L, route = if (it < 750) 1 else 2))
if (it % 10 == 0) trace.flush()
}
trace.flush(true)
assertEquals(2, logs.count { it.contains("triggerUs=") })
assertTrue(logs.any { it.contains("triggerUs=15000000 reasons=2") })
assertTrue(logs.any { it.contains("rows=13000000,") })
assertTrue(logs.any { it.contains("18000000,2,2,") })
}
@Test fun disabledLoggingAndShortCallDoNotLoseControlOfBuffer() {
val logs = mutableListOf<String>()
val trace = CallAudioTrace(4) { _, text -> logs.add(text) }
trace.record(sample(0)); trace.flush(true)
assertTrue(logs.isEmpty())
trace.enabled = true
trace.record(sample(20_000)); trace.flush(true)
assertTrue(logs.any { it.contains("partial=true") })
logs.clear(); trace.enabled = false
trace.record(sample(40_000)); trace.flush(true)
assertTrue(logs.isEmpty())
}
}