From 3dba3f392432950e289fc24523a6758bd51c411c Mon Sep 17 00:00:00 2001 From: Astrid Gealer Date: Mon, 26 Oct 2026 08:02:32 +0000 Subject: [PATCH] test(scheduler): capture logs in a lock-guarded buffer in the completeness test main's Integration Tests (migrations, rest) failed on 89e11403 with a data race in TestAWSRecommendationCompletenessPersistence/SP_empty. The test points the process-wide logger at a plain bytes.Buffer and reads it with String(). Since #1400 puts every subtest's database on one shared Postgres server, a connection pool from an earlier subtest can still be creating idle connections in a background goroutine (pgxpool createIdleResources), and its "context canceled" connect error is logged into the current subtest's buffer while the test reads it. The race detector fails the run. It's timing-dependent: the previous main run passed. Wrap the buffer in a small mutex-guarded type. Assertions unchanged. Co-Authored-By: Claude Opus 5.5 --- ...mendation_completeness_integration_test.go | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/internal/scheduler/recommendation_completeness_integration_test.go b/internal/scheduler/recommendation_completeness_integration_test.go index 414bf9f02..93bc72789 100644 --- a/internal/scheduler/recommendation_completeness_integration_test.go +++ b/internal/scheduler/recommendation_completeness_integration_test.go @@ -26,6 +26,27 @@ import ( "github.com/stretchr/testify/require" ) +// lockedBuffer is a bytes.Buffer safe for concurrent use. The test captures +// the process-wide logger, and connection pools from other subtests on the +// shared Postgres server (#1400) can still log from background goroutines +// (pgxpool's idle-connection creation) while the test reads the capture. +type lockedBuffer struct { + mu sync.Mutex + buf bytes.Buffer +} + +func (b *lockedBuffer) Write(p []byte) (int, error) { + b.mu.Lock() + defer b.mu.Unlock() + return b.buf.Write(p) +} + +func (b *lockedBuffer) String() string { + b.mu.Lock() + defer b.mu.Unlock() + return b.buf.String() +} + type completenessHTTP struct { resourceType string accounts []any @@ -205,7 +226,7 @@ func TestAWSRecommendationCompletenessPersistence(t *testing.T) { } { t.Run(tc.name, func(t *testing.T) { ctx := context.Background() - var diagnostics bytes.Buffer + var diagnostics lockedBuffer previous := logging.SetOutput(&diagnostics) t.Cleanup(func() { logging.SetOutput(previous) }) container := testhelpers.RequireMigratedDatabase(ctx, t, "../database/postgres/migrations")