package link.roadio.riff.ui import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Test class IndexerStatusSamplerTest { @Test fun burstKeepsOnlyTheLatestWithoutPostingPerMessage() { val f = Fixture() repeat(50_000) { f.sampler.offer("Status " + it) } assertEquals(1, f.loop.queued) f.loop.advanceTo(999) assertTrue(f.shown.isEmpty()) f.loop.advanceTo(1000) assertEquals(listOf(1000L to "Status 49999"), f.shown) } @Test fun continuousTrafficDoesNotPostponeTheFixedBoundary() { val f = Fixture() for (i in 1..40) { f.loop.advanceTo(i * 100L - 1) f.sampler.offer("Status " + i) f.loop.advanceTo(i * 100L) } assertEquals(listOf(1000L to "Status 10", 2000L to "Status 20", 3000L to "Status 30", 4000L to "Status 40"), f.shown) assertEquals(1, f.loop.queued) } @Test fun arrivalsAfterIdleStillUseTheNextTick() { val f = Fixture() f.loop.advanceTo(1001) f.sampler.offer("After idle") f.loop.advanceTo(1999) assertTrue(f.shown.isEmpty()) f.loop.advanceTo(2000) assertEquals(listOf(2000L to "After idle"), f.shown) f.sampler.offer("After boundary") f.loop.advanceTo(3000) assertEquals(3000L to "After boundary", f.shown.last()) } @Test fun emptyMessagesClearAtTheBoundaryAndDuplicatesDoNotPublish() { val f = Fixture() f.sampler.offer("Working") f.loop.advanceTo(1000) f.sampler.offer("Working") f.loop.advanceTo(2000) assertEquals(1, f.shown.size) f.sampler.offer("") f.loop.advanceTo(2999) assertEquals(1, f.shown.size) f.loop.advanceTo(3000) f.sampler.offer("Never displayed") f.sampler.offer("") f.loop.advanceTo(4000) assertEquals(listOf(1000L to "Working", 3000L to ""), f.shown) } @Test fun stalledUiSamplesTheLatestOnceWithoutCatchUpBursts() { val f = Fixture() f.sampler.offer("Old") f.loop.stallTo(3500) f.sampler.offer("Latest at dispatch") f.loop.advanceTo(3500) assertEquals(listOf(3500L to "Latest at dispatch"), f.shown) f.sampler.offer("Next") f.loop.advanceTo(4499) assertEquals(1, f.shown.size) f.loop.advanceTo(4500) assertEquals(4500L to "Next", f.shown.last()) } @Test fun receiptDuringPresentationBelongsToTheNextWindow() { val loop = Loop() val shown = mutableListOf() lateinit var sampler: IndexerStatusSampler sampler = IndexerStatusSampler(loop::post, loop::cancel) { text -> shown += text if (text == "First") sampler.offer("Received during presentation") } sampler.start() sampler.offer("First") loop.advanceTo(1000) assertEquals(listOf("First"), shown) loop.advanceTo(2000) assertEquals(listOf("First", "Received during presentation"), shown) } @Test fun closeCancelsTheTickAndRejectsLateReceiptsOrRestart() { val f = Fixture() f.sampler.start() assertEquals(1, f.loop.queued) f.sampler.offer("Closing") f.sampler.close() assertEquals(0, f.loop.queued) f.sampler.offer("Late") f.sampler.start() f.loop.advanceTo(10_000) assertTrue(f.shown.isEmpty()) assertEquals(0, f.loop.queued) } private class Fixture { val loop = Loop() val shown = mutableListOf>() val sampler = IndexerStatusSampler(loop::post, loop::cancel) { shown += loop.now to it } init { sampler.start() } } private class Loop { private data class Task( val at: Long, val action: Runnable, ) var now = 0L private set private val tasks = mutableListOf() val queued get() = tasks.size fun post( action: Runnable, delay: Long, ) { tasks += Task(now + delay, action) } fun cancel(action: Runnable) { tasks.removeAll { it.action === action } } fun stallTo(time: Long) { require(time >= now) now = time } fun advanceTo(time: Long) { require(time >= now) while (true) { val next = tasks.minByOrNull(Task::at)?.takeIf { it.at <= time } ?: break tasks.remove(next) now = maxOf(now, next.at) next.action.run() } now = time } } }