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(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() } val userPin="208f703ed92bf795bfeb78a1cef89b67bed2e45da80038a56157fd1ecd4cfa93" val userKey=StartupAssetKey(31,1,userPin,"avatar/a01-black-tulle-roomy720-correct03/",3) val charPin="f8a69041da51e1a551c6991bfad6d7f034f0f320f726f58568afd43d81cb9604" val charKey=StartupAssetKey(36,1,charPin,"avatar/char-expressions-r02/",3) check(charKey.fingerprint!=userKey.fingerprint) check(runCatching { StartupAssetKey(36,1,userPin,charKey.root,3) }.isFailure) check(runCatching { StartupAssetKey(36,1,charPin,userKey.root,3) }.isFailure) check(runCatching { StartupAssetKey(36,1,charPin,charKey.root,2) }.isFailure) check(userKey.fingerprint!=key(pin=userPin,validator=3).fingerprint) for((root,pin,version) in listOf(Triple("avatar/unknown/",userPin,3), Triple(userKey.root,"b".repeat(64),3),Triple(userKey.root,userPin,2))) { check(runCatching { StartupAssetKey(31,1,pin,root,version) }.isFailure) } 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(width: Int=480,height: Int=848): Triple { TestScheduler.reset();CodecFixture.reset();CodecFixture.emitNativeSurface=true CodecFixture.width=width;CodecFixture.height=height val index=mediaFixtureIndex(width,height);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().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) val (large,largeBase,largeFull)=rig(720,1280) large.update(largeBase,null,true);TestScheduler.advanceBy(250) large.update(largeBase,largeFull,true);TestScheduler.advanceBy(100) check((large.children.last() as EmotionActionAvatarView).alpha==0f);cases++ TestScheduler.until(6000) { (large.children.last() as EmotionActionAvatarView).alpha==1f } TestScheduler.until(100) { CodecFixture.releases>=1 } check(android.opengl.GLFixture.bodySize==720f to 1280f);cases++ large.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") }