diff --git a/android/app/src/main/java/tv/orbit/classic/Rooms.kt b/android/app/src/main/java/tv/orbit/classic/Rooms.kt index 8d80991d..58adce2b 100644 --- a/android/app/src/main/java/tv/orbit/classic/Rooms.kt +++ b/android/app/src/main/java/tv/orbit/classic/Rooms.kt @@ -69,8 +69,10 @@ internal enum class RoomPart(val title: String, val line: String) { } /** On screen and Arcade settings that may travel; the sound output and anything about this box stay where they are. */ -private val LookKeys = listOf("look", "subtitles_on", "channel_logo", "idents", "ident_sound", "special_idents", "block_ident_sound", "squeeze_back", "up_next", - "cast_on", "drive_in_intro", "reduce_motion") +// Comfort settings (reduce motion, subtitles) stay with each TV (1.0 RC3, Paul: joining mustn't overwrite a room's own +// accessibility, sound or playback choices), as the box's sound and picture switches always have. +private val LookKeys = listOf("look", "channel_logo", "idents", "ident_sound", "special_idents", "block_ident_sound", "squeeze_back", "up_next", + "cast_on", "drive_in_intro") private val ArcadeKeys = listOf("snake_best", "block_drop_best", "brick_break_best", "pairs_best", "patience_wins", "orb_man_best", "blast_best", "skydoku_solved", "snake_music") private val DirectorFiles = listOf("director-decisions-0.json", "director-learning-0.json", "event-ideas-history-0.json") private const val BUNDLE_LIMIT = 12 * 1024 * 1024 diff --git a/android/app/src/main/java/tv/orbit/classic/ServerRequests.kt b/android/app/src/main/java/tv/orbit/classic/ServerRequests.kt index 123e6447..8a74fa43 100644 --- a/android/app/src/main/java/tv/orbit/classic/ServerRequests.kt +++ b/android/app/src/main/java/tv/orbit/classic/ServerRequests.kt @@ -176,15 +176,25 @@ internal object BackgroundPacing { } private var lastSetupMs = 0L private val setupLock = Any() + private val setupSent = ArrayDeque() + /** Setup requests a household may make in any 60 minutes at full speed (see [beforeRequest]). */ + const val SETUP_HOURLY = 600 /** Called before each request; waits its turn for background work, or refuses once the hour's budget is spent. */ fun beforeRequest() { if (!active) { // Its own lock: a background request waiting out its 1.5 s must never hold up setup (seen on the emulator, 0.129 audit). if (inSetup.get() == true) synchronized(setupLock) { - val wait = lastSetupMs + SETUP_GAP_MS - clock() + // Setup counts too (1.0 RC3, Paul: no path past every protection): one a second up to the household's share of + // SETUP_HOURLY in any 60 minutes, then slower, so the hour never goes over it. Never refused, so a channel being + // set up always finishes; it just takes longer. + val now0 = clock() + while (setupSent.isNotEmpty() && setupSent.first() <= now0 - 60 * 60_000L) setupSent.removeFirst() + val cap = share(SETUP_HOURLY) + val gap = if (setupSent.size >= cap) 60 * 60_000L / cap else SETUP_GAP_MS + val wait = lastSetupMs + gap - now0 if (wait > 0) sleep(wait) - lastSetupMs = clock() + lastSetupMs = clock(); setupSent.addLast(lastSetupMs) } return } @@ -200,7 +210,7 @@ internal object BackgroundPacing { lastMs = now; sent.addLast(now) } } - internal fun resetForTest() { lastMs = 0; lastSetupMs = 0; sent.clear(); ceilingHitMs = null; budget = HOURLY_BUDGET; tvs = 1; gapMs = GAP_MS; clock = System::currentTimeMillis; sleep = { Thread.sleep(it) } } + internal fun resetForTest() { lastMs = 0; lastSetupMs = 0; sent.clear(); setupSent.clear(); ceilingHitMs = null; budget = HOURLY_BUDGET; tvs = 1; gapMs = GAP_MS; clock = System::currentTimeMillis; sleep = { Thread.sleep(it) } } @Synchronized internal fun usedThisHour(): Int { val now = clock(); return sent.count { it > now - 60 * 60_000L } } } diff --git a/android/app/src/test/java/tv/orbit/classic/HouseholdBudgetTest.kt b/android/app/src/test/java/tv/orbit/classic/HouseholdBudgetTest.kt index 1875cc65..3c4c3a75 100644 --- a/android/app/src/test/java/tv/orbit/classic/HouseholdBudgetTest.kt +++ b/android/app/src/test/java/tv/orbit/classic/HouseholdBudgetTest.kt @@ -20,3 +20,19 @@ class HouseholdBudgetTest { assertEquals(BackgroundPacing.MIN_CEILING, BackgroundPacing.ceiling) } } + +class SetupBudgetTest { + @org.junit.After fun reset() = BackgroundPacing.resetForTest() + @Test fun setupSlowsOnceTheHouseholdsHourIsUsedButNeverStops() { + var now = 0L + val waits = mutableListOf() + BackgroundPacing.resetForTest() + BackgroundPacing.clock = { now } + BackgroundPacing.sleep = { ms -> waits += ms; now += ms } + BackgroundPacing.tvs = 2 + BackgroundPacing.setup { repeat(301) { BackgroundPacing.beforeRequest() } } + // 300 at a second apart, then the 301st waits 12 s (300 an hour for each of two TVs). + assertEquals(1_000L, waits[waits.size - 2]) + assertEquals(12_000L, waits.last()) + } +}