Files
baji-vc/tests/avatar/StartupCheck.kt
T

69 lines
5.9 KiB
Kotlin

import com.gorios.lanvoice.avatar.*
import android.os.TestScheduler
import android.media.CodecFixture
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
fun startupCacheCheck() {
fun key(version: Long=22,install: Long=1,pin: String="a".repeat(64),validator: Int=1)=StartupAssetKey(version,install,pin,"avatar/a01-expression-action-v1/",validator)
val worker=java.util.concurrent.Executors.newSingleThreadExecutor()
try {
val cache=StartupLoadCache<String>(worker);val entered=CountDownLatch(1);val release=CountDownLatch(1);val done=CountDownLatch(2)
var calls=0;var cancelledCalled=false
val cancel=cache.request(key(),{ calls++;entered.countDown();check(release.await(2,TimeUnit.SECONDS));"verified" },{ cancelledCalled=true })
check(entered.await(2,TimeUnit.SECONDS))
repeat(2) { cache.request(key(),{ error("duplicate verification") },{ check(it.getOrThrow()=="verified");done.countDown() }) }
cancel();release.countDown();check(done.await(2,TimeUnit.SECONDS));check(!cancelledCalled && calls==1)
var hits=0;cache.request(key(),{ error("repeated cached read") },{ check(it.getOrThrow()=="verified") },{hits++});check(hits==1)
val failed=CountDownLatch(1);cache.request(key(version=23),{error("invalid assets")},{check(it.isFailure);failed.countDown()});check(failed.await(2,TimeUnit.SECONDS))
val retry=CountDownLatch(1);cache.request(key(version=23),{"retryverified"},{check(it.getOrThrow()=="retryverified");retry.countDown()});check(retry.await(2,TimeUnit.SECONDS))
val folder=java.nio.file.Files.createTempDirectory("avatar-receipt-test").toFile()
try {
val receipt=BundledValidationReceipt(folder);check(!receipt.contains(key()));receipt.complete(key());check(receipt.contains(key()))
for(changed in listOf(key(version=23),key(install=2),key(pin="b".repeat(64)),key(validator=2))) check(!receipt.contains(changed))
java.io.File(folder,key().fingerprint+".complete").writeText("partial");check(!receipt.contains(key()))
receipt.complete(key());java.io.File(folder,"incomplete.tmp").writeText("complete");check(receipt.contains(key()))
receipt.complete(key(install=2));check(!receipt.contains(key()) && receipt.contains(key(install=2)))
} finally { folder.deleteRecursively() }
} finally { worker.shutdownNow() }
println("PASS: production startup shared-future concurrency/cancel/cache/failure-retry and atomic immutable APK receipt corruption/install/pin/version/validator invalidation")
}
fun startupViewCheck() {
var cases=0
fun rig(): Triple<StartupAvatarView,ContinuousAvatarPack,AvatarPack> {
TestScheduler.reset();CodecFixture.reset();CodecFixture.emitNativeSurface=true
val index=mediaFixtureIndex();val neutral=index.packages.getValue(index.fallbackId)
index.packages.values.forEach { CodecFixture.timelines[it.root+it.body.path]=it.body.ptsUs }
index.edges.mapNotNull {it.mouthRuntime}.forEach {CodecFixture.timelines[it.root+it.body.path]=it.body.ptsUs}
return Triple(StartupAvatarView(android.content.Context(),AvatarMediaBinding(),{},{}),neutral.copy(patches=emptyList(),curves=emptyMap()),AvatarPack(fallback="fixture.png",emotionAction=index))
}
var visible=0
val (unused,base,pack)=rig();unused.dispose()
val view=StartupAvatarView(android.content.Context(),AvatarMediaBinding(),{},{visible++})
view.update(base,null,true);TestScheduler.advanceBy(250);check(visible==0);cases++
view.update(base,pack,true);TestScheduler.advanceBy(250);check(visible==0);cases++ // targetready is not sufficient: source actual0 required
TestScheduler.until(6000) { visible==1 };check(CodecFixture.releases>=1);cases++
val full=view.children.last() as EmotionActionAvatarView
check(full.alpha==1f && view.children.filterIsInstance<android.view.TextureView>().single().alpha==0f);cases++
view.update(base,pack,true);TestScheduler.advanceBy(100);check(visible==1);cases++
view.update(base,pack,false);TestScheduler.advanceBy(100);view.update(base,pack,true);TestScheduler.advanceBy(100);check(visible==1);cases++
view.dispose();TestScheduler.advanceBy(300);check(visible==1);cases++
val (background,b2,p2)=rig();background.update(b2,p2,true);background.update(b2,p2,false);TestScheduler.advanceBy(200)
check((background.children.last() as EmotionActionAvatarView).alpha==0f);cases++
background.update(b2,p2,true);TestScheduler.advanceBy(4500);check((background.children.last() as EmotionActionAvatarView).alpha==1f);cases++
background.dispose();TestScheduler.advanceBy(100)
val (bad,b3,p3)=rig();val index=p3.emotionAction!!
val wrong=index.copy(packages=index.packages+ (index.fallbackId to index.packages.getValue(index.fallbackId).copy(body=index.packages.getValue(index.fallbackId).body.copy(sha256="b".repeat(64)))))
check(runCatching { bad.update(b3,p3.copy(emotionAction=wrong),true) }.isFailure);bad.dispose();cases++
val (prototype,b4,p4)=rig();prototype.dispose();var failures=0;var recovered=0
val retrying=StartupAvatarView(android.content.Context(),AvatarMediaBinding(),{},{recovered++},{failures++})
retrying.update(b4,null,true,"preparing");TestScheduler.advanceBy(100)
CodecFixture.configureFails=true;retrying.update(b4,p4,true,"preparing");TestScheduler.advanceBy(100)
check(failures==1 && recovered==0 && (retrying.children.last() as EmotionActionAvatarView).alpha==0f) { "failure_count=$failures recovered=$recovered" };cases++
retrying.update(b4,p4,true,"failed");CodecFixture.configureFails=false
retrying.update(b4,p4,true,"preparing");TestScheduler.until(6000) { recovered==1 };cases++
retrying.dispose();TestScheduler.advanceBy(100)
println("PASS: $cases production startup host renderer callback cases: held exact target0/source0, decoderrelease before capability, persistent host, lifecycle and bodySHA mismatch")
}