Modern Apps git

← Modern-Apps.git

appstore: stub Play backoff sleep in tests and reset global pacing

Commit
68553f9d44dcbacb4b5a48fc9de05d9c4b3f59ed
Author
Vayun Mathur <[email protected]>
When
2026-11-17 02:44 UTC (19 days ago)
Parents
78044c1
PlayHttpClientLocalhostTest's 429 test slept the real production backoff (2s+4s+8s, 15s) and, worse, the client's process-global pacing state (nextRequestAtMs, throttled) leaked into every later test in the JVM, stalling or timing out unrelated clients on a loaded CI box - the suite's 670 tests already carry ~490s of Robolectric time locally.

PlayHttpClient gains an internal sleeper seam (production still sleeps for real) and resetPacingForTest(); the 429 test stubs the sleep (still asserting 1 attempt + 3 retries and the 429 surface) and tearDown resets pacing. The 429 test drops from 15s to 0.02s.

Verified: :appstore:testDevUnitTest PlayHttpClientLocalhostTest green (8 tests, 3.3s); full suite green locally in 12m20s before this change.

Diff

appstore/src/main/java/com/vayunmathur/appstore/data/play/PlayHttpClient.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/data/play/PlayHttpClient.kt b/appstore/src/main/java/com/vayunmathur/appstore/data/play/PlayHttpClient.ktindex 2f87490898..e21fe6238d 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/data/play/PlayHttpClient.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/data/play/PlayHttpClient.kt@@ -61,6 +61,32 @@ class PlayHttpClient : IHttpClient {          /** True while any caller is sitting out a refusal, for a status line rather than an error. */         val throttled: StateFlow<Boolean> = _throttled.asStateFlow()++        /**+         * Test seam: pacing/backoff state is process-global (the limit is+         * per account), so a test that drives a refusal would otherwise+         * leave nextRequestAtMs seconds in the future and stall every+         * later client in the same JVM. Tests reset it in tearDown.+         */+        internal fun resetPacingForTest() {+            synchronized(pacingLock) { nextRequestAtMs = 0L }+            waiting.set(0)+            _throttled.value = false+        }+    }++    /**+     * How waits are slept. Production sleeps for real; tests substitute a+     * no-op so exercising the retry loop does not cost the full+     * seconds-long backoff (2s + 4s + 8s) in wall-clock test time.+     */+    internal var sleeper: (Long) -> Unit = { millis ->+        try {+            Thread.sleep(millis)+        } catch (e: InterruptedException) {+            Thread.currentThread().interrupt()+            throw e+        }     }      private val _responseCode = MutableStateFlow(0)@@ -173,14 +199,7 @@ class PlayHttpClient : IHttpClient {         }     } -    private fun sleep(millis: Long) {-        try {-            Thread.sleep(millis)-        } catch (e: InterruptedException) {-            Thread.currentThread().interrupt()-            throw e-        }-    }+    private fun sleep(millis: Long) = sleeper(millis)      /** `Retry-After` in milliseconds, or 0 when the server did not name a delay in seconds. */     private fun retryAfterMs(conn: HttpURLConnection): Long =
appstore/src/test/java/com/vayunmathur/appstore/data/play/PlayHttpClientLocalhostTest.kt
diff --git a/appstore/src/test/java/com/vayunmathur/appstore/data/play/PlayHttpClientLocalhostTest.kt b/appstore/src/test/java/com/vayunmathur/appstore/data/play/PlayHttpClientLocalhostTest.ktindex 468fde52db..bd2639e194 100644--- a/appstore/src/test/java/com/vayunmathur/appstore/data/play/PlayHttpClientLocalhostTest.kt+++ b/appstore/src/test/java/com/vayunmathur/appstore/data/play/PlayHttpClientLocalhostTest.kt@@ -14,8 +14,8 @@ import org.robolectric.RobolectricTestRunner /**  * PlayHttpClient against a loopback HTTP server: status/body/header  * plumbing, query construction, and POST bodies. No external network.- * (429/503 retry pacing is deliberately not exercised: it sleeps for- * seconds by design.)+ * (429 retries are exercised with the client's sleeper stubbed:+ * the backoff sleeps for seconds by design in production.)  */ @RunWith(RobolectricTestRunner::class) class PlayHttpClientLocalhostTest {@@ -59,7 +59,12 @@ class PlayHttpClientLocalhostTest {     }      @AfterTest-    fun tearDown() = server.stop(0)+    fun tearDown() {+        // Pacing is process-global: never leak a future nextRequestAtMs+        // (or a stuck throttled flag) into the next test class.+        PlayHttpClient.resetPacingForTest()+        server.stop(0)+    }      @Test     fun `get returns the body and records the response code`() {@@ -116,7 +121,11 @@ class PlayHttpClientLocalhostTest {     fun `a 429 is retried three times then surfaced`() {         // Backoff is seconds-long by design (2s + 4s + 8s); the hit count         // is the deterministic part: one attempt plus three retries.+        // Sleep is stubbed out: the retry count, not the wall-clock+        // backoff, is what this test pins (the real sleeps also used to+        // leave global pacing stalled for later tests in the JVM).         val client = PlayHttpClient()+        client.sleeper = {}         val response = client.get("$base/rate", emptyMap())         assertEquals(429, response.code)         assertEquals(4, rateHits.get())