From f105cbfa6afb2412097b3ab2d357a7cf34c764a2 Mon Sep 17 00:00:00 2001 From: Molecule AI Fullstack Engineer Date: Sat, 16 May 2026 02:36:15 +0000 Subject: [PATCH] fix(handlers): replace time.Sleep with explicit async drain in 4 tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue #1264 — CI/Platform Go tests flake under parallel load. The 4 tests that were failing used time.Sleep(N) to wait for goroutines launched by goAsync() to complete before assertions ran. Under CI parallelism, goroutines from prior tests could still be writing to the next test's sqlmock, and the fixed sleep durations (50–200ms) were insufficient under load. Fix: replace each time.Sleep with handler.waitAsyncForTest(), which calls h.asyncWG.Wait() and returns only when all goroutines started by this handler have terminated. This is deterministic regardless of parallelism or machine speed. Tests changed: - TestProxyA2A_Upstream502_TriggersContainerDeadCheck - TestProxyA2A_Upstream502_AliveAgent_PropagatesAsIs - TestGracefulPreRestart_URLResolutionError - TestRestartWorkspaceAuto_RoutesToDockerWhenOnlyDocker Co-Authored-By: Claude Opus 4.7 --- workspace-server/internal/handlers/a2a_proxy_test.go | 5 ++--- workspace-server/internal/handlers/restart_signals_test.go | 2 +- .../internal/handlers/workspace_provision_auto_test.go | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/workspace-server/internal/handlers/a2a_proxy_test.go b/workspace-server/internal/handlers/a2a_proxy_test.go index 7fa22dac5..6270a7491 100644 --- a/workspace-server/internal/handlers/a2a_proxy_test.go +++ b/workspace-server/internal/handlers/a2a_proxy_test.go @@ -294,8 +294,7 @@ func TestProxyA2A_Upstream502_TriggersContainerDeadCheck(t *testing.T) { c.Request.Header.Set("Content-Type", "application/json") handler.ProxyA2A(c) - - time.Sleep(80 * time.Millisecond) + handler.waitAsyncForTest() // Caller sees a structured 503 (NOT the upstream 502 which CF would mask). if w.Code != http.StatusServiceUnavailable { @@ -350,7 +349,7 @@ func TestProxyA2A_Upstream502_AliveAgent_PropagatesAsIs(t *testing.T) { c.Request.Header.Set("Content-Type", "application/json") handler.ProxyA2A(c) - time.Sleep(50 * time.Millisecond) + handler.waitAsyncForTest() if w.Code != http.StatusBadGateway { t.Fatalf("alive agent 502 should propagate as 502; got %d: %s", w.Code, w.Body.String()) diff --git a/workspace-server/internal/handlers/restart_signals_test.go b/workspace-server/internal/handlers/restart_signals_test.go index be0b70779..8e1020750 100644 --- a/workspace-server/internal/handlers/restart_signals_test.go +++ b/workspace-server/internal/handlers/restart_signals_test.go @@ -273,7 +273,7 @@ func TestGracefulPreRestart_URLResolutionError(t *testing.T) { } hWrapper.gracefulPreRestart(context.Background(), "ws-url-err-111") - time.Sleep(200 * time.Millisecond) + hWrapper.waitAsyncForTest() // No panic or error expected — proceeds with stop as documented } diff --git a/workspace-server/internal/handlers/workspace_provision_auto_test.go b/workspace-server/internal/handlers/workspace_provision_auto_test.go index 779f673df..0e0a2ce0b 100644 --- a/workspace-server/internal/handlers/workspace_provision_auto_test.go +++ b/workspace-server/internal/handlers/workspace_provision_auto_test.go @@ -686,7 +686,7 @@ func TestRestartWorkspaceAuto_RoutesToDockerWhenOnlyDocker(t *testing.T) { // recovered by logProvisionPanic. Without this wait, the goroutine // outlives the test and writes to a sqlmock that the NEXT test // owns, causing a `was not expected` race. - time.Sleep(200 * time.Millisecond) + h.waitAsyncForTest() // Stop call is synchronous on the Docker leg. if len(stub.stopped) == 0 || stub.stopped[0] != wsID { -- 2.52.0